blob: 113d9855e3543c0602d5546d058a18c244440ab6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed6c225732014-06-09 19:52:07 -07007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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/utils/SkRandom.h"
15#include "tools/ToolUtils.h"
reed@android.comf523e252009-01-26 23:15:37 +000016
reed@android.comf523e252009-01-26 23:15:37 +000017/* Variants for bitmaps
rmistry@google.comfbfcd562012-08-23 18:09:54 +000018
reed@android.comf523e252009-01-26 23:15:37 +000019 - src depth (32 w+w/o alpha), 565, 4444, index, a8
20 - paint options: filtering, dither, alpha
21 - matrix options: translate, scale, rotate, persp
22 - tiling: none, repeat, mirror, clamp
rmistry@google.comfbfcd562012-08-23 18:09:54 +000023
reed@android.comf523e252009-01-26 23:15:37 +000024 */
25
tfarinaf168b862014-06-19 12:32:29 -070026class BitmapBench : public Benchmark {
reed6c225732014-06-09 19:52:07 -070027 const SkColorType fColorType;
28 const SkAlphaType fAlphaType;
29 const bool fForceUpdate; //bitmap marked as dirty before each draw. forces bitmap to be updated on device cache
mtklein14e4d392014-10-23 14:35:01 -070030 const bool fDoScale;
reed6c225732014-06-09 19:52:07 -070031
32 SkBitmap fBitmap;
33 SkPaint fPaint;
34 SkString fName;
35
djsollen@google.comc2532dd2013-04-09 18:06:06 +000036 enum { W = 128 };
37 enum { H = 128 };
reed@android.comf523e252009-01-26 23:15:37 +000038public:
Mike Reed609ea212020-07-12 15:07:25 -040039 BitmapBench(SkColorType ct, SkAlphaType at, bool forceUpdate, bool doScale)
reed6c225732014-06-09 19:52:07 -070040 : fColorType(ct)
41 , fAlphaType(at)
djsollen@google.comc2532dd2013-04-09 18:06:06 +000042 , fForceUpdate(forceUpdate)
mtklein14e4d392014-10-23 14:35:01 -070043 , fDoScale(doScale)
reed6c225732014-06-09 19:52:07 -070044 {}
reed@android.comf523e252009-01-26 23:15:37 +000045
46protected:
mtkleinf0599002015-07-13 06:18:39 -070047 const char* onGetName() override {
reed@android.comf523e252009-01-26 23:15:37 +000048 fName.set("bitmap");
Mike Kleinea3f0142019-03-20 11:12:10 -050049 fName.appendf("_%s%s",
50 ToolUtils::colortype_name(fColorType),
reed6c225732014-06-09 19:52:07 -070051 kOpaque_SkAlphaType == fAlphaType ? "" : "_A");
mtklein14e4d392014-10-23 14:35:01 -070052 if (fDoScale) {
53 fName.append("_scale");
54 }
55 if (fForceUpdate) {
junov@google.com4ee7ae52011-06-30 17:30:49 +000056 fName.append("_update");
mtklein14e4d392014-10-23 14:35:01 -070057 }
junov@google.com4ee7ae52011-06-30 17:30:49 +000058
reed@android.comf523e252009-01-26 23:15:37 +000059 return fName.c_str();
60 }
61
joshualitt8a6697a2015-09-30 12:11:07 -070062 void onDelayedSetup() override {
djsollen@google.comc2532dd2013-04-09 18:06:06 +000063 SkBitmap bm;
64
Mike Reeda920d362017-07-03 13:36:17 -040065 bm.allocPixels(SkImageInfo::Make(W, H, fColorType, fAlphaType));
reed6c225732014-06-09 19:52:07 -070066 bm.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorBLACK : 0);
djsollen@google.comc2532dd2013-04-09 18:06:06 +000067
mtkleinf0599002015-07-13 06:18:39 -070068 this->onDrawIntoBitmap(bm);
djsollen@google.comc2532dd2013-04-09 18:06:06 +000069
Mike Reeda920d362017-07-03 13:36:17 -040070 fBitmap = bm;
djsollen@google.comc2532dd2013-04-09 18:06:06 +000071 }
72
mtkleina1ebeb22015-10-01 09:43:39 -070073 void onDraw(int loops, SkCanvas* canvas) override {
mtklein14e4d392014-10-23 14:35:01 -070074 if (fDoScale) {
75 canvas->scale(.99f, .99f);
76 }
reed@android.comf523e252009-01-26 23:15:37 +000077 SkIPoint dim = this->getSize();
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000078 SkRandom rand;
reed@android.comf523e252009-01-26 23:15:37 +000079
80 SkPaint paint(fPaint);
81 this->setupPaint(&paint);
82
83 const SkBitmap& bitmap = fBitmap;
84 const SkScalar x0 = SkIntToScalar(-bitmap.width() / 2);
85 const SkScalar y0 = SkIntToScalar(-bitmap.height() / 2);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000086
commit-bot@chromium.org33614712013-12-03 18:17:16 +000087 for (int i = 0; i < loops; i++) {
reed@android.comf523e252009-01-26 23:15:37 +000088 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
89 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
junov@google.com4ee7ae52011-06-30 17:30:49 +000090
91 if (fForceUpdate)
92 bitmap.notifyPixelsChanged();
93
reed@android.comf523e252009-01-26 23:15:37 +000094 canvas->drawBitmap(bitmap, x, y, &paint);
95 }
96 }
97
mtkleinf0599002015-07-13 06:18:39 -070098 virtual void onDrawIntoBitmap(const SkBitmap& bm) {
djsollen@google.comc2532dd2013-04-09 18:06:06 +000099 const int w = bm.width();
100 const int h = bm.height();
101
102 SkCanvas canvas(bm);
103 SkPaint p;
104 p.setAntiAlias(true);
105 p.setColor(SK_ColorRED);
106 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
Brian Osman7f364052020-02-06 11:25:43 -0500107 SkIntToScalar(std::min(w, h))*3/8, p);
djsollen@google.comc2532dd2013-04-09 18:06:06 +0000108
109 SkRect r;
Mike Reed92b33352019-08-24 19:39:13 -0400110 r.setWH(SkIntToScalar(w), SkIntToScalar(h));
djsollen@google.comc2532dd2013-04-09 18:06:06 +0000111 p.setStyle(SkPaint::kStroke_Style);
112 p.setStrokeWidth(SkIntToScalar(4));
113 p.setColor(SK_ColorBLUE);
114 canvas.drawRect(r, p);
115 }
116
reed@android.comf523e252009-01-26 23:15:37 +0000117private:
tfarinaf168b862014-06-19 12:32:29 -0700118 typedef Benchmark INHERITED;
reed@android.comf523e252009-01-26 23:15:37 +0000119};
120
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000121/** Explicitly invoke some filter types to improve coverage of acceleration
122 procs. */
123
reed@google.comef77ec22013-05-29 15:39:54 +0000124enum Flags {
125 kScale_Flag = 1 << 0,
126 kRotate_Flag = 1 << 1,
127 kBilerp_Flag = 1 << 2,
128 kBicubic_Flag = 1 << 3,
129};
130
131static bool isBilerp(uint32_t flags) {
132 return (flags & (kBilerp_Flag | kBicubic_Flag)) == (kBilerp_Flag);
133}
134
135static bool isBicubic(uint32_t flags) {
136 return (flags & (kBilerp_Flag | kBicubic_Flag)) == (kBilerp_Flag | kBicubic_Flag);
137}
138
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000139class FilterBitmapBench : public BitmapBench {
reed@google.comef77ec22013-05-29 15:39:54 +0000140 uint32_t fFlags;
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000141 SkString fFullName;
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000142public:
Mike Reed609ea212020-07-12 15:07:25 -0400143 FilterBitmapBench(SkColorType ct, SkAlphaType at, bool forceUpdate, uint32_t flags)
144 : INHERITED(ct, at, forceUpdate, false)
reed@google.comef77ec22013-05-29 15:39:54 +0000145 , fFlags(flags) {
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000146 }
147
148protected:
mtkleinf0599002015-07-13 06:18:39 -0700149 const char* onGetName() override {
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000150 fFullName.set(INHERITED::onGetName());
reed@google.comef77ec22013-05-29 15:39:54 +0000151 if (fFlags & kScale_Flag) {
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000152 fFullName.append("_scale");
reed@google.comef77ec22013-05-29 15:39:54 +0000153 }
154 if (fFlags & kRotate_Flag) {
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000155 fFullName.append("_rotate");
reed@google.comef77ec22013-05-29 15:39:54 +0000156 }
157 if (isBilerp(fFlags)) {
158 fFullName.append("_bilerp");
159 } else if (isBicubic(fFlags)) {
160 fFullName.append("_bicubic");
161 }
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000162
163 return fFullName.c_str();
164 }
165
mtkleina1ebeb22015-10-01 09:43:39 -0700166 void onDraw(int loops, SkCanvas* canvas) override {
Mike Reed3661bc92017-02-22 13:21:42 -0500167 SkISize dim = canvas->getBaseLayerSize();
reed@google.comef77ec22013-05-29 15:39:54 +0000168 if (fFlags & kScale_Flag) {
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000169 const SkScalar x = SkIntToScalar(dim.fWidth) / 2;
170 const SkScalar y = SkIntToScalar(dim.fHeight) / 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000171
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000172 canvas->translate(x, y);
173 // just enough so we can't take the sprite case
Mike Reed76c6ff02019-07-31 16:33:55 -0400174 canvas->scale(1.1f, 1.1f);
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000175 canvas->translate(-x, -y);
176 }
reed@google.comef77ec22013-05-29 15:39:54 +0000177 if (fFlags & kRotate_Flag) {
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000178 const SkScalar x = SkIntToScalar(dim.fWidth) / 2;
179 const SkScalar y = SkIntToScalar(dim.fHeight) / 2;
bungeman7438bfc2016-07-12 15:01:19 -0700180 canvas->rotate(SkIntToScalar(35), x, y);
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000181 }
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000182 INHERITED::onDraw(loops, canvas);
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000183 }
184
mtklein36352bf2015-03-25 18:17:31 -0700185 void setupPaint(SkPaint* paint) override {
reed@google.com9cfc83c2013-07-22 17:18:18 +0000186 this->INHERITED::setupPaint(paint);
187
188 int index = 0;
189 if (fFlags & kBilerp_Flag) {
190 index |= 1;
191 }
192 if (fFlags & kBicubic_Flag) {
193 index |= 2;
194 }
reed93a12152015-03-16 10:08:34 -0700195 static const SkFilterQuality gQualitys[] = {
196 kNone_SkFilterQuality,
197 kLow_SkFilterQuality,
198 kMedium_SkFilterQuality,
199 kHigh_SkFilterQuality
reed@google.com9cfc83c2013-07-22 17:18:18 +0000200 };
reed93a12152015-03-16 10:08:34 -0700201 paint->setFilterQuality(gQualitys[index]);
reed@google.com9cfc83c2013-07-22 17:18:18 +0000202}
203
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000204private:
205 typedef BitmapBench INHERITED;
206};
207
djsollen@google.comc2532dd2013-04-09 18:06:06 +0000208/** Verify optimizations that test source alpha values. */
209
210class SourceAlphaBitmapBench : public BitmapBench {
211public:
212 enum SourceAlpha { kOpaque_SourceAlpha, kTransparent_SourceAlpha,
213 kTwoStripes_SourceAlpha, kThreeStripes_SourceAlpha};
214private:
215 SkString fFullName;
216 SourceAlpha fSourceAlpha;
217public:
Mike Reed609ea212020-07-12 15:07:25 -0400218 SourceAlphaBitmapBench(SourceAlpha alpha, SkColorType ct, bool forceUpdate = false)
219 : INHERITED(ct, kPremul_SkAlphaType, forceUpdate, false)
djsollen@google.comc2532dd2013-04-09 18:06:06 +0000220 , fSourceAlpha(alpha) {
221 }
222
223protected:
mtkleinf0599002015-07-13 06:18:39 -0700224 const char* onGetName() override {
djsollen@google.comc2532dd2013-04-09 18:06:06 +0000225 fFullName.set(INHERITED::onGetName());
226
227 if (fSourceAlpha == kOpaque_SourceAlpha) {
228 fFullName.append("_source_opaque");
229 } else if (fSourceAlpha == kTransparent_SourceAlpha) {
230 fFullName.append("_source_transparent");
231 } else if (fSourceAlpha == kTwoStripes_SourceAlpha) {
232 fFullName.append("_source_stripes_two");
233 } else if (fSourceAlpha == kThreeStripes_SourceAlpha) {
234 fFullName.append("_source_stripes_three");
235 }
236
237 return fFullName.c_str();
238 }
239
mtklein36352bf2015-03-25 18:17:31 -0700240 void onDrawIntoBitmap(const SkBitmap& bm) override {
djsollen@google.comc2532dd2013-04-09 18:06:06 +0000241 const int w = bm.width();
242 const int h = bm.height();
243
244 if (kOpaque_SourceAlpha == fSourceAlpha) {
245 bm.eraseColor(SK_ColorBLACK);
246 } else if (kTransparent_SourceAlpha == fSourceAlpha) {
247 bm.eraseColor(0);
248 } else if (kTwoStripes_SourceAlpha == fSourceAlpha) {
249 bm.eraseColor(0);
250
251 SkCanvas canvas(bm);
252 SkPaint p;
253 p.setAntiAlias(false);
254 p.setStyle(SkPaint::kFill_Style);
255 p.setColor(SK_ColorRED);
256
257 // Draw red vertical stripes on transparent background
258 SkRect r;
259 for (int x = 0; x < w; x+=2)
260 {
Mike Reed92b33352019-08-24 19:39:13 -0400261 r.setLTRB(SkIntToScalar(x), 0, SkIntToScalar(x+1), SkIntToScalar(h));
djsollen@google.comc2532dd2013-04-09 18:06:06 +0000262 canvas.drawRect(r, p);
263 }
264
265 } else if (kThreeStripes_SourceAlpha == fSourceAlpha) {
266 bm.eraseColor(0);
267
268 SkCanvas canvas(bm);
269 SkPaint p;
270 p.setAntiAlias(false);
271 p.setStyle(SkPaint::kFill_Style);
272
273 // Draw vertical stripes on transparent background with a pattern
274 // where the first pixel is fully transparent, the next is semi-transparent
275 // and the third is fully opaque.
276 SkRect r;
277 for (int x = 0; x < w; x++)
278 {
279 if (x % 3 == 0) {
280 continue; // Keep transparent
281 } else if (x % 3 == 1) {
282 p.setColor(SkColorSetARGB(127, 127, 127, 127)); // Semi-transparent
283 } else if (x % 3 == 2) {
284 p.setColor(SK_ColorRED); // Opaque
285 }
Mike Reed92b33352019-08-24 19:39:13 -0400286 r.setLTRB(SkIntToScalar(x), 0, SkIntToScalar(x+1), SkIntToScalar(h));
djsollen@google.comc2532dd2013-04-09 18:06:06 +0000287 canvas.drawRect(r, p);
288 }
289 }
290 }
291
292private:
293 typedef BitmapBench INHERITED;
294};
reed@google.com2ece2872013-05-28 20:48:14 +0000295
Mike Reed609ea212020-07-12 15:07:25 -0400296DEF_BENCH( return new BitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, false); )
297DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, false); )
298DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, true); )
299DEF_BENCH( return new BitmapBench(kRGB_565_SkColorType, kOpaque_SkAlphaType, false, false); )
300DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, false); )
301DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, false); )
reed@android.comf523e252009-01-26 23:15:37 +0000302
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000303// scale filter -> S32_opaque_D32_filter_DX_{SSE2,SSSE3} and Fact9 is also for S32_D16_filter_DX_SSE2
Mike Reed609ea212020-07-12 15:07:25 -0400304DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, kScale_Flag | kBilerp_Flag); )
305DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, kScale_Flag | kBilerp_Flag); )
306DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, kScale_Flag | kBilerp_Flag); )
307DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, kScale_Flag | kBilerp_Flag); )
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000308
Yuqian Li24e963d2017-12-06 11:30:58 -0500309// The following two cases test the performance regression of b/70172912 .
Mike Reed609ea212020-07-12 15:07:25 -0400310DEF_BENCH( return new FilterBitmapBench(kRGB_565_SkColorType, kOpaque_SkAlphaType, false, kScale_Flag | kBilerp_Flag); )
311DEF_BENCH( return new BitmapBench(kRGB_565_SkColorType, kOpaque_SkAlphaType, false, true); )
Yuqian Li24e963d2017-12-06 11:30:58 -0500312
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000313// scale rotate filter -> S32_opaque_D32_filter_DXDY_{SSE2,SSSE3}
Mike Reed609ea212020-07-12 15:07:25 -0400314DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
315DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
316DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
317DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
reed@google.comef77ec22013-05-29 15:39:54 +0000318
Mike Reed609ea212020-07-12 15:07:25 -0400319DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, kScale_Flag | kBilerp_Flag | kBicubic_Flag); )
320DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, kScale_Flag | kRotate_Flag | kBilerp_Flag | kBicubic_Flag); )
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000321
djsollen@google.comc2532dd2013-04-09 18:06:06 +0000322// source alpha tests -> S32A_Opaque_BlitRow32_{arm,neon}
reed6c225732014-06-09 19:52:07 -0700323DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kOpaque_SourceAlpha, kN32_SkColorType); )
324DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kTransparent_SourceAlpha, kN32_SkColorType); )
325DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kTwoStripes_SourceAlpha, kN32_SkColorType); )
326DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kThreeStripes_SourceAlpha, kN32_SkColorType); )