blob: aa3dadff2ff11fe181ac5f02be2bf23dbf452ca2 [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 */
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.comcc586512011-11-15 15:29:45 +00009#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
tfarinaf168b862014-06-19 12:32:29 -070035class ShaderMaskBench : public Benchmark {
reed@google.comcc586512011-11-15 15:29:45 +000036 SkPaint fPaint;
37 SkString fText;
38 SkString fName;
39 FontQuality fFQ;
reed@google.comcc586512011-11-15 15:29:45 +000040public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000041 ShaderMaskBench(bool isOpaque, FontQuality fq) {
reed@google.comcc586512011-11-15 15:29:45 +000042 fFQ = fq;
43 fText.set(STR);
44
45 fPaint.setAntiAlias(kBW != fq);
46 fPaint.setLCDRenderText(kLCD == fq);
commit-bot@chromium.org76a3b2a2014-04-24 16:54:46 +000047 fPaint.setShader(new SkColorShader(isOpaque ? 0xFFFFFFFF : 0x80808080))->unref();
reed@google.comcc586512011-11-15 15:29:45 +000048 }
49
50protected:
51 virtual const char* onGetName() {
tomhudson@google.com8afae612012-08-14 15:03:35 +000052 fName.printf("shadermask");
reed@google.comcc586512011-11-15 15:29:45 +000053 fName.appendf("_%s", fontQualityName(fPaint));
54 fName.appendf("_%02X", fPaint.getAlpha());
55 return fName.c_str();
56 }
57
commit-bot@chromium.org33614712013-12-03 18:17:16 +000058 virtual void onDraw(const int loops, SkCanvas* canvas) {
reed@google.comcc586512011-11-15 15:29:45 +000059 const SkIPoint dim = this->getSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000060 SkRandom rand;
reed@google.comcc586512011-11-15 15:29:45 +000061
62 SkPaint paint(fPaint);
63 this->setupPaint(&paint);
64 // explicitly need these
65 paint.setAlpha(fPaint.getAlpha());
66 paint.setAntiAlias(kBW != fFQ);
67 paint.setLCDRenderText(kLCD == fFQ);
68
69 const SkScalar x0 = SkIntToScalar(-10);
70 const SkScalar y0 = SkIntToScalar(-10);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071
reed@google.comcc586512011-11-15 15:29:45 +000072 paint.setTextSize(SkIntToScalar(12));
commit-bot@chromium.org33614712013-12-03 18:17:16 +000073 for (int i = 0; i < loops; i++) {
reed@google.comcc586512011-11-15 15:29:45 +000074 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
75 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
76 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
77 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000078
reed@google.comcc586512011-11-15 15:29:45 +000079 paint.setTextSize(SkIntToScalar(48));
commit-bot@chromium.org33614712013-12-03 18:17:16 +000080 for (int i = 0; i < loops / 4 ; i++) {
reed@google.comcc586512011-11-15 15:29:45 +000081 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
82 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
83 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
84 }
85 }
86
87private:
tfarinaf168b862014-06-19 12:32:29 -070088 typedef Benchmark INHERITED;
reed@google.comcc586512011-11-15 15:29:45 +000089};
90
91///////////////////////////////////////////////////////////////////////////////
92
mtklein@google.com410e6e82013-09-13 19:52:27 +000093DEF_BENCH( return new ShaderMaskBench(true, kBW); )
94DEF_BENCH( return new ShaderMaskBench(false, kBW); )
95DEF_BENCH( return new ShaderMaskBench(true, kAA); )
96DEF_BENCH( return new ShaderMaskBench(false, kAA); )
97DEF_BENCH( return new ShaderMaskBench(true, kLCD); )
98DEF_BENCH( return new ShaderMaskBench(false, kLCD); )