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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkColorPriv.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 12 | #include "SkPaint.h" |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 41 | class BitmapRectBench : public Benchmark { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 42 | SkBitmap fBitmap; |
| 43 | bool fSlightMatrix; |
| 44 | uint8_t fAlpha; |
| 45 | SkPaint::FilterLevel fFilterLevel; |
| 46 | SkString fName; |
| 47 | SkRect fSrcR, fDstR; |
| 48 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 49 | static const int kWidth = 128; |
| 50 | static const int kHeight = 128; |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 51 | public: |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 52 | BitmapRectBench(U8CPU alpha, SkPaint::FilterLevel filterLevel, |
| 53 | bool slightMatrix) { |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 54 | fAlpha = SkToU8(alpha); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 55 | fFilterLevel = filterLevel; |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 56 | fSlightMatrix = slightMatrix; |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 57 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 58 | fBitmap.setInfo(SkImageInfo::MakeN32Premul(kWidth, kHeight)); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | protected: |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 62 | virtual const char* onGetName() SK_OVERRIDE { |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 63 | fName.printf("bitmaprect_%02X_%sfilter_%s", |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 64 | fAlpha, |
| 65 | SkPaint::kNone_FilterLevel == fFilterLevel ? "no" : "", |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 66 | fSlightMatrix ? "trans" : "identity"); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 67 | return fName.c_str(); |
| 68 | } |
| 69 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 70 | virtual void onPreDraw() SK_OVERRIDE { |
| 71 | fBitmap.allocPixels(); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 72 | fBitmap.setAlphaType(kOpaque_SkAlphaType); |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 73 | fBitmap.eraseColor(SK_ColorBLACK); |
| 74 | draw_into_bitmap(fBitmap); |
| 75 | |
| 76 | fSrcR.iset(0, 0, kWidth, kHeight); |
| 77 | fDstR.iset(0, 0, kWidth, kHeight); |
| 78 | |
| 79 | if (fSlightMatrix) { |
| 80 | // want fractional translate |
| 81 | fDstR.offset(SK_Scalar1 / 3, SK_Scalar1 * 5 / 7); |
| 82 | // want enough to create a scale matrix, but not enough to scare |
| 83 | // off our sniffer which tries to see if the matrix is "effectively" |
| 84 | // translate-only. |
| 85 | fDstR.fRight += SK_Scalar1 / (kWidth * 60); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 90 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 91 | SkRandom rand; |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 92 | |
| 93 | SkPaint paint; |
| 94 | this->setupPaint(&paint); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 95 | paint.setFilterLevel(fFilterLevel); |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 96 | paint.setAlpha(fAlpha); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 97 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 98 | for (int i = 0; i < loops; i++) { |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 99 | canvas->drawBitmapRectToRect(fBitmap, &fSrcR, fDstR, &paint); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 104 | typedef Benchmark INHERITED; |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 107 | DEF_BENCH(return new BitmapRectBench(0xFF, SkPaint::kNone_FilterLevel, false)) |
| 108 | DEF_BENCH(return new BitmapRectBench(0x80, SkPaint::kNone_FilterLevel, false)) |
| 109 | DEF_BENCH(return new BitmapRectBench(0xFF, SkPaint::kLow_FilterLevel, false)) |
| 110 | DEF_BENCH(return new BitmapRectBench(0x80, SkPaint::kLow_FilterLevel, false)) |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 111 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 112 | DEF_BENCH(return new BitmapRectBench(0xFF, SkPaint::kNone_FilterLevel, true)) |
| 113 | DEF_BENCH(return new BitmapRectBench(0xFF, SkPaint::kLow_FilterLevel, true)) |