reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 7 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 10 | #include "SkPaint.h" |
| 11 | #include "SkRandom.h" |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
| 13 | #include "SkTemplates.h" |
| 14 | |
| 15 | #define STR "Hamburgefons" |
| 16 | |
| 17 | enum FontQuality { |
| 18 | kBW, |
| 19 | kAA, |
| 20 | kLCD |
| 21 | }; |
| 22 | |
| 23 | static 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 33 | class ShaderMaskBench : public Benchmark { |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 34 | SkPaint fPaint; |
| 35 | SkString fText; |
| 36 | SkString fName; |
| 37 | FontQuality fFQ; |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 38 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 39 | ShaderMaskBench(bool isOpaque, FontQuality fq) { |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 40 | fFQ = fq; |
| 41 | fText.set(STR); |
| 42 | |
| 43 | fPaint.setAntiAlias(kBW != fq); |
| 44 | fPaint.setLCDRenderText(kLCD == fq); |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 45 | fPaint.setShader(SkShader::MakeColorShader(isOpaque ? 0xFFFFFFFF : 0x80808080)); |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | protected: |
| 49 | virtual const char* onGetName() { |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 50 | fName.printf("shadermask"); |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 51 | fName.appendf("_%s", fontQualityName(fPaint)); |
| 52 | fName.appendf("_%02X", fPaint.getAlpha()); |
| 53 | return fName.c_str(); |
| 54 | } |
| 55 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 56 | virtual void onDraw(int loops, SkCanvas* canvas) { |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 57 | const SkIPoint dim = this->getSize(); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 58 | SkRandom rand; |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 59 | |
| 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 69 | |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 70 | paint.setTextSize(SkIntToScalar(12)); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 71 | for (int i = 0; i < loops; i++) { |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 72 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 76 | |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 77 | paint.setTextSize(SkIntToScalar(48)); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 78 | for (int i = 0; i < loops / 4 ; i++) { |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 79 | 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 | |
| 85 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 86 | typedef Benchmark INHERITED; |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 91 | DEF_BENCH( return new ShaderMaskBench(true, kBW); ) |
| 92 | DEF_BENCH( return new ShaderMaskBench(false, kBW); ) |
| 93 | DEF_BENCH( return new ShaderMaskBench(true, kAA); ) |
| 94 | DEF_BENCH( return new ShaderMaskBench(false, kAA); ) |
| 95 | DEF_BENCH( return new ShaderMaskBench(true, kLCD); ) |
| 96 | DEF_BENCH( return new ShaderMaskBench(false, kLCD); ) |