• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
7 
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/utils/SkRandom.h"
15 #include "tools/ToolUtils.h"
16 
17 /*  Variants for bitmaps
18 
19     - 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
23 
24  */
25 
26 class BitmapBench : public Benchmark {
27     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
30     const bool          fIsVolatile;
31     const bool          fDoScale;
32 
33     SkBitmap            fBitmap;
34     SkPaint             fPaint;
35     SkString            fName;
36 
37     enum { W = 128 };
38     enum { H = 128 };
39 public:
BitmapBench(SkColorType ct,SkAlphaType at,bool forceUpdate,bool isVolatile,bool doScale)40     BitmapBench(SkColorType ct, SkAlphaType at, bool forceUpdate, bool isVolatile, bool doScale)
41         : fColorType(ct)
42         , fAlphaType(at)
43         , fForceUpdate(forceUpdate)
44         , fIsVolatile(isVolatile)
45         , fDoScale(doScale)
46     {}
47 
48 protected:
onGetName()49     const char* onGetName() override {
50         fName.set("bitmap");
51         fName.appendf("_%s%s",
52                       ToolUtils::colortype_name(fColorType),
53                       kOpaque_SkAlphaType == fAlphaType ? "" : "_A");
54         if (fDoScale) {
55             fName.append("_scale");
56         }
57         if (fForceUpdate) {
58             fName.append("_update");
59         }
60         if (fIsVolatile) {
61             fName.append("_volatile");
62         }
63 
64         return fName.c_str();
65     }
66 
onDelayedSetup()67     void onDelayedSetup() override {
68         SkBitmap bm;
69 
70         bm.allocPixels(SkImageInfo::Make(W, H, fColorType, fAlphaType));
71         bm.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorBLACK : 0);
72 
73         this->onDrawIntoBitmap(bm);
74 
75         fBitmap = bm;
76         fBitmap.setIsVolatile(fIsVolatile);
77     }
78 
onDraw(int loops,SkCanvas * canvas)79     void onDraw(int loops, SkCanvas* canvas) override {
80         if (fDoScale) {
81             canvas->scale(.99f, .99f);
82         }
83         SkIPoint dim = this->getSize();
84         SkRandom rand;
85 
86         SkPaint paint(fPaint);
87         this->setupPaint(&paint);
88 
89         const SkBitmap& bitmap = fBitmap;
90         const SkScalar x0 = SkIntToScalar(-bitmap.width() / 2);
91         const SkScalar y0 = SkIntToScalar(-bitmap.height() / 2);
92 
93         for (int i = 0; i < loops; i++) {
94             SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
95             SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
96 
97             if (fForceUpdate)
98                 bitmap.notifyPixelsChanged();
99 
100             canvas->drawBitmap(bitmap, x, y, &paint);
101         }
102     }
103 
onDrawIntoBitmap(const SkBitmap & bm)104      virtual void onDrawIntoBitmap(const SkBitmap& bm) {
105         const int w = bm.width();
106         const int h = bm.height();
107 
108         SkCanvas canvas(bm);
109         SkPaint p;
110         p.setAntiAlias(true);
111         p.setColor(SK_ColorRED);
112         canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
113                           SkIntToScalar(SkMin32(w, h))*3/8, p);
114 
115         SkRect r;
116         r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
117         p.setStyle(SkPaint::kStroke_Style);
118         p.setStrokeWidth(SkIntToScalar(4));
119         p.setColor(SK_ColorBLUE);
120         canvas.drawRect(r, p);
121     }
122 
123 private:
124     typedef Benchmark INHERITED;
125 };
126 
127 /** Explicitly invoke some filter types to improve coverage of acceleration
128     procs. */
129 
130 enum Flags {
131     kScale_Flag             = 1 << 0,
132     kRotate_Flag            = 1 << 1,
133     kBilerp_Flag            = 1 << 2,
134     kBicubic_Flag           = 1 << 3,
135 };
136 
isBilerp(uint32_t flags)137 static bool isBilerp(uint32_t flags) {
138     return (flags & (kBilerp_Flag | kBicubic_Flag)) == (kBilerp_Flag);
139 }
140 
isBicubic(uint32_t flags)141 static bool isBicubic(uint32_t flags) {
142     return (flags & (kBilerp_Flag | kBicubic_Flag)) == (kBilerp_Flag | kBicubic_Flag);
143 }
144 
145 class FilterBitmapBench : public BitmapBench {
146     uint32_t    fFlags;
147     SkString    fFullName;
148 public:
FilterBitmapBench(SkColorType ct,SkAlphaType at,bool forceUpdate,bool isVolitile,uint32_t flags)149     FilterBitmapBench(SkColorType ct, SkAlphaType at,
150                       bool forceUpdate, bool isVolitile, uint32_t flags)
151         : INHERITED(ct, at, forceUpdate, isVolitile, false)
152         , fFlags(flags) {
153     }
154 
155 protected:
onGetName()156     const char* onGetName() override {
157         fFullName.set(INHERITED::onGetName());
158         if (fFlags & kScale_Flag) {
159             fFullName.append("_scale");
160         }
161         if (fFlags & kRotate_Flag) {
162             fFullName.append("_rotate");
163         }
164         if (isBilerp(fFlags)) {
165             fFullName.append("_bilerp");
166         } else if (isBicubic(fFlags)) {
167             fFullName.append("_bicubic");
168         }
169 
170         return fFullName.c_str();
171     }
172 
onDraw(int loops,SkCanvas * canvas)173     void onDraw(int loops, SkCanvas* canvas) override {
174         SkISize dim = canvas->getBaseLayerSize();
175         if (fFlags & kScale_Flag) {
176             const SkScalar x = SkIntToScalar(dim.fWidth) / 2;
177             const SkScalar y = SkIntToScalar(dim.fHeight) / 2;
178 
179             canvas->translate(x, y);
180             // just enough so we can't take the sprite case
181             canvas->scale(1.1f, 1.1f);
182             canvas->translate(-x, -y);
183         }
184         if (fFlags & kRotate_Flag) {
185             const SkScalar x = SkIntToScalar(dim.fWidth) / 2;
186             const SkScalar y = SkIntToScalar(dim.fHeight) / 2;
187             canvas->rotate(SkIntToScalar(35), x, y);
188         }
189         INHERITED::onDraw(loops, canvas);
190     }
191 
setupPaint(SkPaint * paint)192     void setupPaint(SkPaint* paint) override {
193         this->INHERITED::setupPaint(paint);
194 
195         int index = 0;
196         if (fFlags & kBilerp_Flag) {
197             index |= 1;
198         }
199         if (fFlags & kBicubic_Flag) {
200             index |= 2;
201         }
202         static const SkFilterQuality gQualitys[] = {
203             kNone_SkFilterQuality,
204             kLow_SkFilterQuality,
205             kMedium_SkFilterQuality,
206             kHigh_SkFilterQuality
207         };
208         paint->setFilterQuality(gQualitys[index]);
209 }
210 
211 private:
212     typedef BitmapBench INHERITED;
213 };
214 
215 /** Verify optimizations that test source alpha values. */
216 
217 class SourceAlphaBitmapBench : public BitmapBench {
218 public:
219     enum SourceAlpha { kOpaque_SourceAlpha, kTransparent_SourceAlpha,
220                        kTwoStripes_SourceAlpha, kThreeStripes_SourceAlpha};
221 private:
222     SkString    fFullName;
223     SourceAlpha fSourceAlpha;
224 public:
SourceAlphaBitmapBench(SourceAlpha alpha,SkColorType ct,bool forceUpdate=false,bool bitmapVolatile=false)225     SourceAlphaBitmapBench(SourceAlpha alpha, SkColorType ct,
226                 bool forceUpdate = false, bool bitmapVolatile = false)
227         : INHERITED(ct, kPremul_SkAlphaType, forceUpdate, bitmapVolatile, false)
228         , fSourceAlpha(alpha) {
229     }
230 
231 protected:
onGetName()232     const char* onGetName() override {
233         fFullName.set(INHERITED::onGetName());
234 
235         if (fSourceAlpha == kOpaque_SourceAlpha) {
236                 fFullName.append("_source_opaque");
237         } else if (fSourceAlpha == kTransparent_SourceAlpha) {
238                 fFullName.append("_source_transparent");
239         } else if (fSourceAlpha == kTwoStripes_SourceAlpha) {
240                 fFullName.append("_source_stripes_two");
241         } else if (fSourceAlpha == kThreeStripes_SourceAlpha) {
242                 fFullName.append("_source_stripes_three");
243         }
244 
245         return fFullName.c_str();
246     }
247 
onDrawIntoBitmap(const SkBitmap & bm)248     void onDrawIntoBitmap(const SkBitmap& bm) override {
249         const int w = bm.width();
250         const int h = bm.height();
251 
252         if (kOpaque_SourceAlpha == fSourceAlpha) {
253             bm.eraseColor(SK_ColorBLACK);
254         } else if (kTransparent_SourceAlpha == fSourceAlpha) {
255             bm.eraseColor(0);
256         } else if (kTwoStripes_SourceAlpha == fSourceAlpha) {
257             bm.eraseColor(0);
258 
259             SkCanvas canvas(bm);
260             SkPaint p;
261             p.setAntiAlias(false);
262             p.setStyle(SkPaint::kFill_Style);
263             p.setColor(SK_ColorRED);
264 
265             // Draw red vertical stripes on transparent background
266             SkRect r;
267             for (int x = 0; x < w; x+=2)
268             {
269                 r.set(SkIntToScalar(x), 0, SkIntToScalar(x+1), SkIntToScalar(h));
270                 canvas.drawRect(r, p);
271             }
272 
273         } else if (kThreeStripes_SourceAlpha == fSourceAlpha) {
274             bm.eraseColor(0);
275 
276             SkCanvas canvas(bm);
277             SkPaint p;
278             p.setAntiAlias(false);
279             p.setStyle(SkPaint::kFill_Style);
280 
281             // Draw vertical stripes on transparent background with a pattern
282             // where the first pixel is fully transparent, the next is semi-transparent
283             // and the third is fully opaque.
284             SkRect r;
285             for (int x = 0; x < w; x++)
286             {
287                 if (x % 3 == 0) {
288                     continue; // Keep transparent
289                 } else if (x % 3 == 1) {
290                     p.setColor(SkColorSetARGB(127, 127, 127, 127)); // Semi-transparent
291                 } else if (x % 3 == 2) {
292                     p.setColor(SK_ColorRED); // Opaque
293                 }
294                 r.set(SkIntToScalar(x), 0, SkIntToScalar(x+1), SkIntToScalar(h));
295                 canvas.drawRect(r, p);
296             }
297         }
298     }
299 
300 private:
301     typedef BitmapBench INHERITED;
302 };
303 
304 DEF_BENCH( return new BitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, false, false); )
305 DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, false, false); )
306 DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, false, true); )
307 DEF_BENCH( return new BitmapBench(kRGB_565_SkColorType, kOpaque_SkAlphaType, false, false, false); )
308 DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, true, false); )
309 DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, false, false); )
310 
311 // scale filter -> S32_opaque_D32_filter_DX_{SSE2,SSSE3} and Fact9 is also for S32_D16_filter_DX_SSE2
312 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, false, kScale_Flag | kBilerp_Flag); )
313 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, false, kScale_Flag | kBilerp_Flag); )
314 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, true, kScale_Flag | kBilerp_Flag); )
315 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, false, kScale_Flag | kBilerp_Flag); )
316 
317 // The following two cases test the performance regression of b/70172912 .
318 DEF_BENCH( return new FilterBitmapBench(kRGB_565_SkColorType, kOpaque_SkAlphaType, false, false, kScale_Flag | kBilerp_Flag); )
319 DEF_BENCH( return new BitmapBench(kRGB_565_SkColorType, kOpaque_SkAlphaType, false, false, true); )
320 
321 // scale rotate filter -> S32_opaque_D32_filter_DXDY_{SSE2,SSSE3}
322 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, false, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
323 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, false, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
324 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, true, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
325 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, false, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
326 
327 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, false, kScale_Flag | kBilerp_Flag | kBicubic_Flag); )
328 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, false, kScale_Flag | kRotate_Flag | kBilerp_Flag | kBicubic_Flag); )
329 
330 // source alpha tests -> S32A_Opaque_BlitRow32_{arm,neon}
331 DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kOpaque_SourceAlpha, kN32_SkColorType); )
332 DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kTransparent_SourceAlpha, kN32_SkColorType); )
333 DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kTwoStripes_SourceAlpha, kN32_SkColorType); )
334 DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kThreeStripes_SourceAlpha, kN32_SkColorType); )
335