blob: 1cc23474033475585d02073c69b4144c13640ec1 [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"
Florin Malitaab244f02017-05-03 19:16:58 +000012#include "SkShader.h"
reed@google.comcc586512011-11-15 15:29:45 +000013#include "SkString.h"
14#include "SkTemplates.h"
15
16#define STR "Hamburgefons"
17
18enum FontQuality {
19 kBW,
20 kAA,
21 kLCD
22};
23
24static const char* fontQualityName(const SkPaint& paint) {
25 if (!paint.isAntiAlias()) {
26 return "BW";
27 }
28 if (paint.isLCDRenderText()) {
29 return "LCD";
30 }
31 return "AA";
32}
33
tfarinaf168b862014-06-19 12:32:29 -070034class ShaderMaskBench : public Benchmark {
reed@google.comcc586512011-11-15 15:29:45 +000035 SkPaint fPaint;
36 SkString fText;
37 SkString fName;
38 FontQuality fFQ;
reed@google.comcc586512011-11-15 15:29:45 +000039public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000040 ShaderMaskBench(bool isOpaque, FontQuality fq) {
reed@google.comcc586512011-11-15 15:29:45 +000041 fFQ = fq;
42 fText.set(STR);
43
44 fPaint.setAntiAlias(kBW != fq);
45 fPaint.setLCDRenderText(kLCD == fq);
reedfe630452016-03-25 09:08:00 -070046 fPaint.setShader(SkShader::MakeColorShader(isOpaque ? 0xFFFFFFFF : 0x80808080));
reed@google.comcc586512011-11-15 15:29:45 +000047 }
48
49protected:
50 virtual const char* onGetName() {
tomhudson@google.com8afae612012-08-14 15:03:35 +000051 fName.printf("shadermask");
reed@google.comcc586512011-11-15 15:29:45 +000052 fName.appendf("_%s", fontQualityName(fPaint));
53 fName.appendf("_%02X", fPaint.getAlpha());
54 return fName.c_str();
55 }
56
mtkleina1ebeb22015-10-01 09:43:39 -070057 virtual void onDraw(int loops, SkCanvas* canvas) {
reed@google.comcc586512011-11-15 15:29:45 +000058 const SkIPoint dim = this->getSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000059 SkRandom rand;
reed@google.comcc586512011-11-15 15:29:45 +000060
61 SkPaint paint(fPaint);
62 this->setupPaint(&paint);
63 // explicitly need these
64 paint.setAlpha(fPaint.getAlpha());
65 paint.setAntiAlias(kBW != fFQ);
66 paint.setLCDRenderText(kLCD == fFQ);
67
68 const SkScalar x0 = SkIntToScalar(-10);
69 const SkScalar y0 = SkIntToScalar(-10);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000070
reed@google.comcc586512011-11-15 15:29:45 +000071 paint.setTextSize(SkIntToScalar(12));
commit-bot@chromium.org33614712013-12-03 18:17:16 +000072 for (int i = 0; i < loops; i++) {
reed@google.comcc586512011-11-15 15:29:45 +000073 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
74 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
Cary Clark2a475ea2017-04-28 15:35:12 -040075 canvas->drawString(fText, x, y, paint);
reed@google.comcc586512011-11-15 15:29:45 +000076 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000077
reed@google.comcc586512011-11-15 15:29:45 +000078 paint.setTextSize(SkIntToScalar(48));
commit-bot@chromium.org33614712013-12-03 18:17:16 +000079 for (int i = 0; i < loops / 4 ; i++) {
reed@google.comcc586512011-11-15 15:29:45 +000080 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
81 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
Cary Clark2a475ea2017-04-28 15:35:12 -040082 canvas->drawString(fText, x, y, paint);
reed@google.comcc586512011-11-15 15:29:45 +000083 }
84 }
85
86private:
tfarinaf168b862014-06-19 12:32:29 -070087 typedef Benchmark INHERITED;
reed@google.comcc586512011-11-15 15:29:45 +000088};
89
90///////////////////////////////////////////////////////////////////////////////
91
mtklein@google.com410e6e82013-09-13 19:52:27 +000092DEF_BENCH( return new ShaderMaskBench(true, kBW); )
93DEF_BENCH( return new ShaderMaskBench(false, kBW); )
94DEF_BENCH( return new ShaderMaskBench(true, kAA); )
95DEF_BENCH( return new ShaderMaskBench(false, kAA); )
96DEF_BENCH( return new ShaderMaskBench(true, kLCD); )
97DEF_BENCH( return new ShaderMaskBench(false, kLCD); )