reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +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 | */ |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColorPriv.h" |
| 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkString.h" |
| 14 | #include "include/private/SkTo.h" |
| 15 | #include "include/utils/SkRandom.h" |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 16 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 17 | static void draw_into_bitmap(const SkBitmap& bm) { |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 18 | const int w = bm.width(); |
| 19 | const int h = bm.height(); |
| 20 | |
| 21 | SkCanvas canvas(bm); |
| 22 | SkPaint p; |
| 23 | p.setAntiAlias(true); |
| 24 | p.setColor(SK_ColorRED); |
| 25 | canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2, |
| 26 | SkIntToScalar(SkMin32(w, h))*3/8, p); |
| 27 | |
| 28 | SkRect r; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 29 | r.setWH(SkIntToScalar(w), SkIntToScalar(h)); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 30 | p.setStyle(SkPaint::kStroke_Style); |
| 31 | p.setStrokeWidth(SkIntToScalar(4)); |
| 32 | p.setColor(SK_ColorBLUE); |
| 33 | canvas.drawRect(r, p); |
| 34 | } |
| 35 | |
| 36 | /* Variants for bitmaprect |
| 37 | src : entire bitmap, subset, fractional subset |
| 38 | dst : same size as src, diff size |
| 39 | paint : filter-p |
| 40 | */ |
| 41 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 42 | class BitmapRectBench : public Benchmark { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 43 | SkBitmap fBitmap; |
| 44 | bool fSlightMatrix; |
| 45 | uint8_t fAlpha; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 46 | SkFilterQuality fFilterQuality; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 47 | SkString fName; |
| 48 | SkRect fSrcR, fDstR; |
| 49 | |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 50 | static const int kWidth = 128; |
| 51 | static const int kHeight = 128; |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 52 | public: |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 53 | BitmapRectBench(U8CPU alpha, SkFilterQuality filterQuality, |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 54 | bool slightMatrix) { |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 55 | fAlpha = SkToU8(alpha); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 56 | fFilterQuality = filterQuality; |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 57 | fSlightMatrix = slightMatrix; |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 58 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 59 | fBitmap.setInfo(SkImageInfo::MakeN32Premul(kWidth, kHeight)); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 63 | const char* onGetName() override { |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 64 | fName.printf("bitmaprect_%02X_%sfilter_%s", |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 65 | fAlpha, |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 66 | kNone_SkFilterQuality == fFilterQuality ? "no" : "", |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 67 | fSlightMatrix ? "trans" : "identity"); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 68 | return fName.c_str(); |
| 69 | } |
| 70 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 71 | void onDelayedSetup() override { |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 72 | fBitmap.allocPixels(); |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 73 | fBitmap.setAlphaType(kOpaque_SkAlphaType); |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 74 | fBitmap.eraseColor(SK_ColorBLACK); |
| 75 | draw_into_bitmap(fBitmap); |
| 76 | |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 77 | fSrcR.setWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
| 78 | fDstR.setWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
commit-bot@chromium.org | 7fb83c8 | 2013-08-01 15:58:07 +0000 | [diff] [blame] | 79 | |
| 80 | if (fSlightMatrix) { |
| 81 | // want fractional translate |
| 82 | fDstR.offset(SK_Scalar1 / 3, SK_Scalar1 * 5 / 7); |
| 83 | // want enough to create a scale matrix, but not enough to scare |
| 84 | // off our sniffer which tries to see if the matrix is "effectively" |
| 85 | // translate-only. |
| 86 | fDstR.fRight += SK_Scalar1 / (kWidth * 60); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 91 | void onDraw(int loops, SkCanvas* canvas) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 92 | SkRandom rand; |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 93 | |
| 94 | SkPaint paint; |
| 95 | this->setupPaint(&paint); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 96 | paint.setFilterQuality(fFilterQuality); |
reed@google.com | b8b92ea | 2012-10-16 15:57:13 +0000 | [diff] [blame] | 97 | paint.setAlpha(fAlpha); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 98 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 99 | for (int i = 0; i < loops; i++) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 100 | canvas->drawBitmapRect(fBitmap, fSrcR, fDstR, &paint, |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 101 | SkCanvas::kStrict_SrcRectConstraint); |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 106 | typedef Benchmark INHERITED; |
reed@google.com | 3d407a1 | 2012-10-12 14:42:38 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 109 | DEF_BENCH(return new BitmapRectBench(0xFF, kNone_SkFilterQuality, false)) |
| 110 | DEF_BENCH(return new BitmapRectBench(0x80, kNone_SkFilterQuality, false)) |
| 111 | DEF_BENCH(return new BitmapRectBench(0xFF, kLow_SkFilterQuality, false)) |
| 112 | DEF_BENCH(return new BitmapRectBench(0x80, kLow_SkFilterQuality, false)) |
reed@google.com | 9318231 | 2013-01-15 20:21:19 +0000 | [diff] [blame] | 113 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 114 | DEF_BENCH(return new BitmapRectBench(0xFF, kNone_SkFilterQuality, true)) |
| 115 | DEF_BENCH(return new BitmapRectBench(0xFF, kLow_SkFilterQuality, true)) |