1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SkBenchmark.h"
9#include "SkCanvas.h"
10#include "SkColorShader.h"
11#include "SkFontHost.h"
12#include "SkPaint.h"
13#include "SkRandom.h"
14#include "SkSfntUtils.h"
15#include "SkString.h"
16#include "SkTemplates.h"
17
18#define STR     "Hamburgefons"
19
20enum FontQuality {
21    kBW,
22    kAA,
23    kLCD
24};
25
26static const char* fontQualityName(const SkPaint& paint) {
27    if (!paint.isAntiAlias()) {
28        return "BW";
29    }
30    if (paint.isLCDRenderText()) {
31        return "LCD";
32    }
33    return "AA";
34}
35
36class ShaderMaskBench : public SkBenchmark {
37    SkPaint     fPaint;
38    SkString    fText;
39    SkString    fName;
40    FontQuality fFQ;
41    enum { N = SkBENCHLOOP(500) };
42public:
43    ShaderMaskBench(void* param, bool isOpaque, FontQuality fq) : INHERITED(param) {
44        fFQ = fq;
45        fText.set(STR);
46
47        fPaint.setAntiAlias(kBW != fq);
48        fPaint.setLCDRenderText(kLCD == fq);
49        fPaint.setAlpha(isOpaque ? 0xFF : 0x80);
50        fPaint.setShader(new SkColorShader)->unref();
51    }
52
53protected:
54    virtual const char* onGetName() {
55        fName.printf("shadermask", SkScalarToFloat(fPaint.getTextSize()));
56        fName.appendf("_%s", fontQualityName(fPaint));
57        fName.appendf("_%02X", fPaint.getAlpha());
58        return fName.c_str();
59    }
60
61    virtual void onDraw(SkCanvas* canvas) {
62        const SkIPoint dim = this->getSize();
63        SkRandom rand;
64
65        SkPaint paint(fPaint);
66        this->setupPaint(&paint);
67        // explicitly need these
68        paint.setAlpha(fPaint.getAlpha());
69        paint.setAntiAlias(kBW != fFQ);
70        paint.setLCDRenderText(kLCD == fFQ);
71
72        const SkScalar x0 = SkIntToScalar(-10);
73        const SkScalar y0 = SkIntToScalar(-10);
74
75        paint.setTextSize(SkIntToScalar(12));
76        for (int i = 0; i < N; i++) {
77            SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
78            SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
79            canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
80        }
81
82        paint.setTextSize(SkIntToScalar(48));
83        for (int i = 0; i < N/4; i++) {
84            SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
85            SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
86            canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
87        }
88    }
89
90private:
91    typedef SkBenchmark INHERITED;
92};
93
94///////////////////////////////////////////////////////////////////////////////
95
96static SkBenchmark* Fact00(void* p) { return new ShaderMaskBench(p, true,  kBW); }
97static SkBenchmark* Fact01(void* p) { return new ShaderMaskBench(p, false, kBW); }
98static SkBenchmark* Fact10(void* p) { return new ShaderMaskBench(p, true,  kAA); }
99static SkBenchmark* Fact11(void* p) { return new ShaderMaskBench(p, false, kAA); }
100static SkBenchmark* Fact20(void* p) { return new ShaderMaskBench(p, true,  kLCD); }
101static SkBenchmark* Fact21(void* p) { return new ShaderMaskBench(p, false, kLCD); }
102
103static BenchRegistry gReg00(Fact00);
104static BenchRegistry gReg01(Fact01);
105static BenchRegistry gReg10(Fact10);
106static BenchRegistry gReg11(Fact11);
107static BenchRegistry gReg20(Fact20);
108static BenchRegistry gReg21(Fact21);
109
110