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 | */ |
| 8 | #include "SkBenchmark.h" |
| 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 | |
| 35 | class ShaderMaskBench : public SkBenchmark { |
| 36 | SkPaint fPaint; |
| 37 | SkString fText; |
| 38 | SkString fName; |
| 39 | FontQuality fFQ; |
| 40 | enum { N = SkBENCHLOOP(500) }; |
| 41 | public: |
| 42 | ShaderMaskBench(void* param, bool isOpaque, FontQuality fq) : INHERITED(param) { |
| 43 | fFQ = fq; |
| 44 | fText.set(STR); |
| 45 | |
| 46 | fPaint.setAntiAlias(kBW != fq); |
| 47 | fPaint.setLCDRenderText(kLCD == fq); |
| 48 | fPaint.setAlpha(isOpaque ? 0xFF : 0x80); |
| 49 | fPaint.setShader(new SkColorShader)->unref(); |
| 50 | } |
| 51 | |
| 52 | protected: |
| 53 | virtual const char* onGetName() { |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 54 | fName.printf("shadermask"); |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 55 | fName.appendf("_%s", fontQualityName(fPaint)); |
| 56 | fName.appendf("_%02X", fPaint.getAlpha()); |
| 57 | return fName.c_str(); |
| 58 | } |
| 59 | |
| 60 | virtual void onDraw(SkCanvas* canvas) { |
| 61 | const SkIPoint dim = this->getSize(); |
| 62 | SkRandom rand; |
| 63 | |
| 64 | SkPaint paint(fPaint); |
| 65 | this->setupPaint(&paint); |
| 66 | // explicitly need these |
| 67 | paint.setAlpha(fPaint.getAlpha()); |
| 68 | paint.setAntiAlias(kBW != fFQ); |
| 69 | paint.setLCDRenderText(kLCD == fFQ); |
| 70 | |
| 71 | const SkScalar x0 = SkIntToScalar(-10); |
| 72 | const SkScalar y0 = SkIntToScalar(-10); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 73 | |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 74 | paint.setTextSize(SkIntToScalar(12)); |
| 75 | for (int i = 0; i < N; i++) { |
| 76 | SkScalar x = x0 + rand.nextUScalar1() * dim.fX; |
| 77 | SkScalar y = y0 + rand.nextUScalar1() * dim.fY; |
| 78 | canvas->drawText(fText.c_str(), fText.size(), x, y, paint); |
| 79 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 80 | |
reed@google.com | cc58651 | 2011-11-15 15:29:45 +0000 | [diff] [blame] | 81 | paint.setTextSize(SkIntToScalar(48)); |
| 82 | for (int i = 0; i < N/4; i++) { |
| 83 | SkScalar x = x0 + rand.nextUScalar1() * dim.fX; |
| 84 | SkScalar y = y0 + rand.nextUScalar1() * dim.fY; |
| 85 | canvas->drawText(fText.c_str(), fText.size(), x, y, paint); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | typedef SkBenchmark INHERITED; |
| 91 | }; |
| 92 | |
| 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | static SkBenchmark* Fact00(void* p) { return new ShaderMaskBench(p, true, kBW); } |
| 96 | static SkBenchmark* Fact01(void* p) { return new ShaderMaskBench(p, false, kBW); } |
| 97 | static SkBenchmark* Fact10(void* p) { return new ShaderMaskBench(p, true, kAA); } |
| 98 | static SkBenchmark* Fact11(void* p) { return new ShaderMaskBench(p, false, kAA); } |
| 99 | static SkBenchmark* Fact20(void* p) { return new ShaderMaskBench(p, true, kLCD); } |
| 100 | static SkBenchmark* Fact21(void* p) { return new ShaderMaskBench(p, false, kLCD); } |
| 101 | |
| 102 | static BenchRegistry gReg00(Fact00); |
| 103 | static BenchRegistry gReg01(Fact01); |
| 104 | static BenchRegistry gReg10(Fact10); |
| 105 | static BenchRegistry gReg11(Fact11); |
| 106 | static BenchRegistry gReg20(Fact20); |
| 107 | static BenchRegistry gReg21(Fact21); |