blob: 90a5037f05a8b47ff2702581121c60f18c0c656e [file] [log] [blame]
reed@google.comcc586512011-11-15 15:29:45 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reedfe630452016-03-25 09:08:00 -07007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.comcc586512011-11-15 15:29:45 +00009#include "SkCanvas.h"
reed@google.comcc586512011-11-15 15:29:45 +000010#include "SkPaint.h"
11#include "SkRandom.h"
reed@google.comcc586512011-11-15 15:29:45 +000012#include "SkString.h"
13#include "SkTemplates.h"
14
15#define STR "Hamburgefons"
16
17enum FontQuality {
18 kBW,
19 kAA,
20 kLCD
21};
22
23static const char* fontQualityName(const SkPaint& paint) {
24 if (!paint.isAntiAlias()) {
25 return "BW";
26 }
27 if (paint.isLCDRenderText()) {
28 return "LCD";
29 }
30 return "AA";
31}
32
tfarinaf168b862014-06-19 12:32:29 -070033class ShaderMaskBench : public Benchmark {
reed@google.comcc586512011-11-15 15:29:45 +000034 SkPaint fPaint;
35 SkString fText;
36 SkString fName;
37 FontQuality fFQ;
reed@google.comcc586512011-11-15 15:29:45 +000038public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000039 ShaderMaskBench(bool isOpaque, FontQuality fq) {
reed@google.comcc586512011-11-15 15:29:45 +000040 fFQ = fq;
41 fText.set(STR);
42
43 fPaint.setAntiAlias(kBW != fq);
44 fPaint.setLCDRenderText(kLCD == fq);
reedfe630452016-03-25 09:08:00 -070045 fPaint.setShader(SkShader::MakeColorShader(isOpaque ? 0xFFFFFFFF : 0x80808080));
reed@google.comcc586512011-11-15 15:29:45 +000046 }
47
48protected:
49 virtual const char* onGetName() {
tomhudson@google.com8afae612012-08-14 15:03:35 +000050 fName.printf("shadermask");
reed@google.comcc586512011-11-15 15:29:45 +000051 fName.appendf("_%s", fontQualityName(fPaint));
52 fName.appendf("_%02X", fPaint.getAlpha());
53 return fName.c_str();
54 }
55
mtkleina1ebeb22015-10-01 09:43:39 -070056 virtual void onDraw(int loops, SkCanvas* canvas) {
reed@google.comcc586512011-11-15 15:29:45 +000057 const SkIPoint dim = this->getSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000058 SkRandom rand;
reed@google.comcc586512011-11-15 15:29:45 +000059
60 SkPaint paint(fPaint);
61 this->setupPaint(&paint);
62 // explicitly need these
63 paint.setAlpha(fPaint.getAlpha());
64 paint.setAntiAlias(kBW != fFQ);
65 paint.setLCDRenderText(kLCD == fFQ);
66
67 const SkScalar x0 = SkIntToScalar(-10);
68 const SkScalar y0 = SkIntToScalar(-10);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000069
reed@google.comcc586512011-11-15 15:29:45 +000070 paint.setTextSize(SkIntToScalar(12));
commit-bot@chromium.org33614712013-12-03 18:17:16 +000071 for (int i = 0; i < loops; i++) {
reed@google.comcc586512011-11-15 15:29:45 +000072 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
73 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
74 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
75 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000076
reed@google.comcc586512011-11-15 15:29:45 +000077 paint.setTextSize(SkIntToScalar(48));
commit-bot@chromium.org33614712013-12-03 18:17:16 +000078 for (int i = 0; i < loops / 4 ; i++) {
reed@google.comcc586512011-11-15 15:29:45 +000079 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
80 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
81 canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
82 }
83 }
84
85private:
tfarinaf168b862014-06-19 12:32:29 -070086 typedef Benchmark INHERITED;
reed@google.comcc586512011-11-15 15:29:45 +000087};
88
89///////////////////////////////////////////////////////////////////////////////
90
mtklein@google.com410e6e82013-09-13 19:52:27 +000091DEF_BENCH( return new ShaderMaskBench(true, kBW); )
92DEF_BENCH( return new ShaderMaskBench(false, kBW); )
93DEF_BENCH( return new ShaderMaskBench(true, kAA); )
94DEF_BENCH( return new ShaderMaskBench(false, kAA); )
95DEF_BENCH( return new ShaderMaskBench(true, kLCD); )
96DEF_BENCH( return new ShaderMaskBench(false, kLCD); )