sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
reed | 8c0c7b0 | 2014-06-27 05:49:53 -0700 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkFont.h" |
| 11 | #include "include/core/SkSurface.h" |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 12 | #include "include/effects/SkImageFilters.h" |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 13 | |
sugoi@google.com | ca425d9 | 2013-04-23 14:37:38 +0000 | [diff] [blame] | 14 | #define FILTER_WIDTH_SMALL 32 |
| 15 | #define FILTER_HEIGHT_SMALL 32 |
| 16 | #define FILTER_WIDTH_LARGE 256 |
| 17 | #define FILTER_HEIGHT_LARGE 256 |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 18 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 19 | class DisplacementBaseBench : public Benchmark { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 20 | public: |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 21 | DisplacementBaseBench(bool small) : fInitialized(false), fIsSmall(small) { } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 22 | |
| 23 | protected: |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 24 | void onDelayedSetup() override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 25 | if (!fInitialized) { |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 26 | this->makeBitmap(); |
| 27 | this->makeCheckerboard(); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 28 | fInitialized = true; |
| 29 | } |
| 30 | } |
| 31 | |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 32 | void makeBitmap() { |
| 33 | const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; |
Greg Kaiser | fb7a713 | 2019-02-08 17:00:36 -0800 | [diff] [blame] | 34 | const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 35 | fBitmap.allocN32Pixels(w, h); |
| 36 | SkCanvas canvas(fBitmap); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 37 | canvas.clear(0x00000000); |
| 38 | SkPaint paint; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 39 | paint.setColor(0xFF884422); |
Mike Reed | 89126e4 | 2019-01-03 12:59:14 -0500 | [diff] [blame] | 40 | |
| 41 | SkFont font; |
| 42 | font.setSize(SkIntToScalar(96)); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 43 | canvas.drawSimpleText("g", 1, SkTextEncoding::kUTF8, SkIntToScalar(15), SkIntToScalar(55), font, paint); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 44 | } |
| 45 | |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 46 | void makeCheckerboard() { |
| 47 | const int w = this->isSmall() ? FILTER_WIDTH_SMALL : FILTER_WIDTH_LARGE; |
Greg Kaiser | fb7a713 | 2019-02-08 17:00:36 -0800 | [diff] [blame] | 48 | const int h = this->isSmall() ? FILTER_HEIGHT_SMALL : FILTER_HEIGHT_LARGE; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 49 | auto surface(SkSurface::MakeRasterN32Premul(w, h)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 50 | SkCanvas* canvas = surface->getCanvas(); |
| 51 | canvas->clear(0x00000000); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 52 | SkPaint darkPaint; |
| 53 | darkPaint.setColor(0xFF804020); |
| 54 | SkPaint lightPaint; |
| 55 | lightPaint.setColor(0xFF244484); |
| 56 | for (int y = 0; y < h; y += 16) { |
| 57 | for (int x = 0; x < w; x += 16) { |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 58 | canvas->save(); |
| 59 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 60 | canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 61 | canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 62 | canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 63 | canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 64 | canvas->restore(); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 67 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 68 | fCheckerboard = surface->makeImageSnapshot(); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) { |
| 72 | canvas->save(); |
| 73 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
skia.committer@gmail.com | 70402c3 | 2013-10-29 07:01:50 +0000 | [diff] [blame] | 74 | SkIntToScalar(fBitmap.width()), |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 75 | SkIntToScalar(fBitmap.height()))); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 76 | canvas->drawBitmap(fBitmap, SkIntToScalar(x), SkIntToScalar(y), &paint); |
| 77 | canvas->restore(); |
| 78 | } |
| 79 | |
| 80 | inline bool isSmall() const { return fIsSmall; } |
| 81 | |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 82 | SkBitmap fBitmap; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 83 | sk_sp<SkImage> fCheckerboard; |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 84 | |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 85 | private: |
| 86 | bool fInitialized; |
| 87 | bool fIsSmall; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame^] | 88 | using INHERITED = Benchmark; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | class DisplacementZeroBench : public DisplacementBaseBench { |
| 92 | public: |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 93 | DisplacementZeroBench(bool small) : INHERITED(small) { } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 94 | |
| 95 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 96 | const char* onGetName() override { |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 97 | return this->isSmall() ? "displacement_zero_small" : "displacement_zero_large"; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 98 | } |
| 99 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 100 | void onDraw(int loops, SkCanvas* canvas) override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 101 | SkPaint paint; |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 102 | sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard)); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 103 | // No displacement effect |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 104 | paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kG, |
| 105 | 0.0f, std::move(displ), nullptr)); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 106 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 107 | for (int i = 0; i < loops; i++) { |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 108 | this->drawClippedBitmap(canvas, 0, 0, paint); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 109 | } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame^] | 113 | using INHERITED = DisplacementBaseBench; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | class DisplacementAlphaBench : public DisplacementBaseBench { |
| 117 | public: |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 118 | DisplacementAlphaBench(bool small) : INHERITED(small) { } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 119 | |
| 120 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 121 | const char* onGetName() override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 122 | return isSmall() ? "displacement_alpha_small" : "displacement_alpha_large"; |
| 123 | } |
| 124 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 125 | void onDraw(int loops, SkCanvas* canvas) override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 126 | SkPaint paint; |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 127 | sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard)); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 128 | // Displacement, with 1 alpha component (which isn't pre-multiplied) |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 129 | paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kB, SkColorChannel::kA, |
| 130 | 16.0f, std::move(displ), nullptr)); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 131 | for (int i = 0; i < loops; i++) { |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 132 | this->drawClippedBitmap(canvas, 100, 0, paint); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 133 | } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame^] | 137 | using INHERITED = DisplacementBaseBench; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | class DisplacementFullBench : public DisplacementBaseBench { |
| 141 | public: |
robertphillips | bfe11fc | 2016-04-15 07:17:36 -0700 | [diff] [blame] | 142 | DisplacementFullBench(bool small) : INHERITED(small) { } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 143 | |
| 144 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 145 | const char* onGetName() override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 146 | return isSmall() ? "displacement_full_small" : "displacement_full_large"; |
| 147 | } |
| 148 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 149 | void onDraw(int loops, SkCanvas* canvas) override { |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 150 | SkPaint paint; |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 151 | sk_sp<SkImageFilter> displ(SkImageFilters::Image(fCheckerboard)); |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 152 | // Displacement, with 2 non-alpha components |
Michael Ludwig | 2300318 | 2019-08-05 11:25:23 -0400 | [diff] [blame] | 153 | paint.setImageFilter(SkImageFilters::DisplacementMap(SkColorChannel::kR, SkColorChannel::kB, |
| 154 | 32.0f, std::move(displ), nullptr)); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 155 | for (int i = 0; i < loops; ++i) { |
robertphillips@google.com | 26e30c5 | 2013-10-28 18:07:44 +0000 | [diff] [blame] | 156 | this->drawClippedBitmap(canvas, 200, 0, paint); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 157 | } |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame^] | 161 | using INHERITED = DisplacementBaseBench; |
sugoi@google.com | 580a172 | 2013-04-23 14:20:45 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | /////////////////////////////////////////////////////////////////////////////// |
| 165 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 166 | DEF_BENCH( return new DisplacementZeroBench(true); ) |
| 167 | DEF_BENCH( return new DisplacementAlphaBench(true); ) |
| 168 | DEF_BENCH( return new DisplacementFullBench(true); ) |
| 169 | DEF_BENCH( return new DisplacementZeroBench(false); ) |
| 170 | DEF_BENCH( return new DisplacementAlphaBench(false); ) |
| 171 | DEF_BENCH( return new DisplacementFullBench(false); ) |