blob: 0e8e4bbee9f922871d7c21a1ec40ce2e2c384807 [file] [log] [blame]
reed@google.comcc586512011-11-15 15:29:45 +00001
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"
reed@google.comcc586512011-11-15 15:29:45 +000014#include "SkString.h"
15#include "SkTemplates.h"
16
17#define STR "Hamburgefons"
18
19enum FontQuality {
20 kBW,
21 kAA,
22 kLCD
23};
24
25static const char* fontQualityName(const SkPaint& paint) {
26 if (!paint.isAntiAlias()) {
27 return "BW";
28 }
29 if (paint.isLCDRenderText()) {
30 return "LCD";
31 }
32 return "AA";
33}
34
35class ShaderMaskBench : public SkBenchmark {
36 SkPaint fPaint;
37 SkString fText;
38 SkString fName;
39 FontQuality fFQ;
40 enum { N = SkBENCHLOOP(500) };
41public:
42 ShaderMaskBench(void* param, bool isOpaque, FontQuality fq) : INHERITED(param) {
43 fFQ = fq;
44 fText.set(STR);
45
46 fPaint.setAntiAlias(kBW != fq);
47 fPaint.setLCDRenderText(kLCD == fq);
48 fPaint.setAlpha(isOpaque ? 0xFF : 0x80);
49 fPaint.setShader(new SkColorShader)->unref();
50 }
51
52protected:
53 virtual const char* onGetName() {
tomhudson@google.com8afae612012-08-14 15:03:35 +000054 fName.printf("shadermask");
reed@google.comcc586512011-11-15 15:29:45 +000055 fName.appendf("_%s", fontQualityName(fPaint));
56 fName.appendf("_%02X", fPaint.getAlpha());
57 return fName.c_str();
58 }
59
60 virtual void onDraw(SkCanvas* canvas) {
61 const SkIPoint dim = this->getSize();
62 SkRandom rand;
63
64 SkPaint paint(fPaint);
65 this->setupPaint(&paint);
66 // explicitly need these
67 paint.setAlpha(fPaint.getAlpha());
68 paint.setAntiAlias(kBW != fFQ);
69 paint.setLCDRenderText(kLCD == fFQ);
70
71 const SkScalar x0 = SkIntToScalar(-10);
72 const SkScalar y0 = SkIntToScalar(-10);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000073
reed@google.comcc586512011-11-15 15:29:45 +000074 paint.setTextSize(SkIntToScalar(12));
75 for (int i = 0; i < N; i++) {
76 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
77 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
78 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
79 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000080
reed@google.comcc586512011-11-15 15:29:45 +000081 paint.setTextSize(SkIntToScalar(48));
82 for (int i = 0; i < N/4; i++) {
83 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
84 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
85 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
86 }
87 }
88
89private:
90 typedef SkBenchmark INHERITED;
91};
92
93///////////////////////////////////////////////////////////////////////////////
94
95static SkBenchmark* Fact00(void* p) { return new ShaderMaskBench(p, true, kBW); }
96static SkBenchmark* Fact01(void* p) { return new ShaderMaskBench(p, false, kBW); }
97static SkBenchmark* Fact10(void* p) { return new ShaderMaskBench(p, true, kAA); }
98static SkBenchmark* Fact11(void* p) { return new ShaderMaskBench(p, false, kAA); }
99static SkBenchmark* Fact20(void* p) { return new ShaderMaskBench(p, true, kLCD); }
100static SkBenchmark* Fact21(void* p) { return new ShaderMaskBench(p, false, kLCD); }
101
102static BenchRegistry gReg00(Fact00);
103static BenchRegistry gReg01(Fact01);
104static BenchRegistry gReg10(Fact10);
105static BenchRegistry gReg11(Fact11);
106static BenchRegistry gReg20(Fact20);
107static BenchRegistry gReg21(Fact21);