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