reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +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 "SkBitmap.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkRandom.h" |
| 14 | #include "SkString.h" |
| 15 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 16 | static void draw_into_bitmap(const SkBitmap& bm) { |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 17 | const int w = bm.width(); |
| 18 | const int h = bm.height(); |
| 19 | |
| 20 | SkCanvas canvas(bm); |
| 21 | SkPaint p; |
| 22 | p.setAntiAlias(true); |
| 23 | p.setColor(SK_ColorRED); |
| 24 | canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2, |
| 25 | SkIntToScalar(SkMin32(w, h))*3/8, p); |
| 26 | |
| 27 | SkRect r; |
| 28 | r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); |
| 29 | p.setStyle(SkPaint::kStroke_Style); |
| 30 | p.setStrokeWidth(SkIntToScalar(4)); |
| 31 | p.setColor(SK_ColorBLUE); |
| 32 | canvas.drawRect(r, p); |
| 33 | } |
| 34 | |
| 35 | /* Variants for bitmaprect |
| 36 | src : entire bitmap, subset, fractional subset |
| 37 | dst : same size as src, diff size |
| 38 | paint : filter-p |
| 39 | */ |
| 40 | |
| 41 | class BitmapRectBench : public SkBenchmark { |
| 42 | SkBitmap fBitmap; |
| 43 | bool fDoFilter; |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 44 | bool fSlightMatrix; |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 45 | uint8_t fAlpha; |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 46 | SkString fName; |
| 47 | SkRect fSrcR, fDstR; |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 48 | static const int kWidth = 128; |
| 49 | static const int kHeight = 128; |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 50 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 51 | BitmapRectBench(U8CPU alpha, bool doFilter, bool slightMatrix) { |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 52 | fAlpha = SkToU8(alpha); |
| 53 | fDoFilter = doFilter; |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 54 | fSlightMatrix = slightMatrix; |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 55 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 56 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, kWidth, kHeight); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | protected: |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 60 | virtual const char* onGetName() SK_OVERRIDE { |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 61 | fName.printf("bitmaprect_%02X_%sfilter_%s", |
| 62 | fAlpha, fDoFilter ? "" : "no", |
| 63 | fSlightMatrix ? "trans" : "identity"); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 64 | return fName.c_str(); |
| 65 | } |
| 66 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 67 | virtual void onPreDraw() SK_OVERRIDE { |
| 68 | fBitmap.allocPixels(); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 69 | fBitmap.setAlphaType(kOpaque_SkAlphaType); |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 70 | fBitmap.eraseColor(SK_ColorBLACK); |
| 71 | draw_into_bitmap(fBitmap); |
| 72 | |
| 73 | fSrcR.iset(0, 0, kWidth, kHeight); |
| 74 | fDstR.iset(0, 0, kWidth, kHeight); |
| 75 | |
| 76 | if (fSlightMatrix) { |
| 77 | // want fractional translate |
| 78 | fDstR.offset(SK_Scalar1 / 3, SK_Scalar1 * 5 / 7); |
| 79 | // want enough to create a scale matrix, but not enough to scare |
| 80 | // off our sniffer which tries to see if the matrix is "effectively" |
| 81 | // translate-only. |
| 82 | fDstR.fRight += SK_Scalar1 / (kWidth * 60); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 88 | SkRandom rand; |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 89 | |
| 90 | SkPaint paint; |
| 91 | this->setupPaint(&paint); |
| 92 | paint.setFilterBitmap(fDoFilter); |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 93 | paint.setAlpha(fAlpha); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 94 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 95 | for (int i = 0; i < this->getLoops(); i++) { |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 96 | canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR, &paint); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | typedef SkBenchmark INHERITED; |
| 102 | }; |
| 103 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 104 | DEF_BENCH(return new BitmapRectBench(0xFF, false, false)) |
| 105 | DEF_BENCH(return new BitmapRectBench(0x80, false, false)) |
| 106 | DEF_BENCH(return new BitmapRectBench(0xFF, true, false)) |
| 107 | DEF_BENCH(return new BitmapRectBench(0x80, true, false)) |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 108 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 109 | DEF_BENCH(return new BitmapRectBench(0xFF, false, true)) |
| 110 | DEF_BENCH(return new BitmapRectBench(0xFF, true, true)) |