2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkColorPriv.h"
15 #include "sk_tool_utils.h"
17 static int conv6ToByte(int x) {
21 static int convByteTo6(int x) {
25 static uint8_t compute666Index(SkPMColor c) {
26 int r = SkGetPackedR32(c);
27 int g = SkGetPackedG32(c);
28 int b = SkGetPackedB32(c);
30 return convByteTo6(r) * 36 + convByteTo6(g) * 6 + convByteTo6(b);
33 static void convertToIndex666(const SkBitmap& src, SkBitmap* dst, SkAlphaType aType) {
34 SkPMColor storage[216];
35 SkPMColor* colors = storage;
37 for (int r = 0; r < 6; r++) {
38 int rr = conv6ToByte(r);
39 for (int g = 0; g < 6; g++) {
40 int gg = conv6ToByte(g);
41 for (int b = 0; b < 6; b++) {
42 int bb = conv6ToByte(b);
43 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb);
47 SkColorTable* ctable = new SkColorTable(storage, 216);
48 dst->allocPixels(SkImageInfo::Make(src.width(), src.height(), kIndex_8_SkColorType, aType),
52 SkAutoLockPixels alps(src);
53 SkAutoLockPixels alpd(*dst);
55 for (int y = 0; y < src.height(); y++) {
56 const SkPMColor* srcP = src.getAddr32(0, y);
57 uint8_t* dstP = dst->getAddr8(0, y);
58 for (int x = src.width() - 1; x >= 0; --x) {
59 *dstP++ = compute666Index(*srcP++);
64 /* Variants for bitmaps
66 - src depth (32 w+w/o alpha), 565, 4444, index, a8
67 - paint options: filtering, dither, alpha
68 - matrix options: translate, scale, rotate, persp
69 - tiling: none, repeat, mirror, clamp
73 class BitmapBench : public Benchmark {
74 const SkColorType fColorType;
75 const SkAlphaType fAlphaType;
76 const bool fForceUpdate; //bitmap marked as dirty before each draw. forces bitmap to be updated on device cache
77 const bool fIsVolatile;
87 BitmapBench(SkColorType ct, SkAlphaType at, bool forceUpdate, bool isVolatile, bool doScale)
90 , fForceUpdate(forceUpdate)
91 , fIsVolatile(isVolatile)
96 virtual const char* onGetName() {
98 fName.appendf("_%s%s", sk_tool_utils::colortype_name(fColorType),
99 kOpaque_SkAlphaType == fAlphaType ? "" : "_A");
101 fName.append("_scale");
104 fName.append("_update");
107 fName.append("_volatile");
110 return fName.c_str();
113 virtual void onPreDraw() {
116 if (kIndex_8_SkColorType == fColorType) {
117 bm.allocPixels(SkImageInfo::MakeN32(W, H, fAlphaType));
119 bm.allocPixels(SkImageInfo::Make(W, H, fColorType, fAlphaType));
121 bm.eraseColor(kOpaque_SkAlphaType == fAlphaType ? SK_ColorBLACK : 0);
123 onDrawIntoBitmap(bm);
125 if (kIndex_8_SkColorType == fColorType) {
126 convertToIndex666(bm, &fBitmap, fAlphaType);
131 fBitmap.setIsVolatile(fIsVolatile);
134 virtual void onDraw(const int loops, SkCanvas* canvas) {
136 canvas->scale(.99f, .99f);
138 SkIPoint dim = this->getSize();
141 SkPaint paint(fPaint);
142 this->setupPaint(&paint);
144 const SkBitmap& bitmap = fBitmap;
145 const SkScalar x0 = SkIntToScalar(-bitmap.width() / 2);
146 const SkScalar y0 = SkIntToScalar(-bitmap.height() / 2);
148 for (int i = 0; i < loops; i++) {
149 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
150 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
153 bitmap.notifyPixelsChanged();
155 canvas->drawBitmap(bitmap, x, y, &paint);
159 virtual void onDrawIntoBitmap(const SkBitmap& bm) {
160 const int w = bm.width();
161 const int h = bm.height();
165 p.setAntiAlias(true);
166 p.setColor(SK_ColorRED);
167 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
168 SkIntToScalar(SkMin32(w, h))*3/8, p);
171 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
172 p.setStyle(SkPaint::kStroke_Style);
173 p.setStrokeWidth(SkIntToScalar(4));
174 p.setColor(SK_ColorBLUE);
175 canvas.drawRect(r, p);
179 typedef Benchmark INHERITED;
182 /** Explicitly invoke some filter types to improve coverage of acceleration
186 kScale_Flag = 1 << 0,
187 kRotate_Flag = 1 << 1,
188 kBilerp_Flag = 1 << 2,
189 kBicubic_Flag = 1 << 3,
192 static bool isBilerp(uint32_t flags) {
193 return (flags & (kBilerp_Flag | kBicubic_Flag)) == (kBilerp_Flag);
196 static bool isBicubic(uint32_t flags) {
197 return (flags & (kBilerp_Flag | kBicubic_Flag)) == (kBilerp_Flag | kBicubic_Flag);
200 class FilterBitmapBench : public BitmapBench {
204 FilterBitmapBench(SkColorType ct, SkAlphaType at,
205 bool forceUpdate, bool isVolitile, uint32_t flags)
206 : INHERITED(ct, at, forceUpdate, isVolitile, false)
211 virtual const char* onGetName() {
212 fFullName.set(INHERITED::onGetName());
213 if (fFlags & kScale_Flag) {
214 fFullName.append("_scale");
216 if (fFlags & kRotate_Flag) {
217 fFullName.append("_rotate");
219 if (isBilerp(fFlags)) {
220 fFullName.append("_bilerp");
221 } else if (isBicubic(fFlags)) {
222 fFullName.append("_bicubic");
225 return fFullName.c_str();
228 virtual void onDraw(const int loops, SkCanvas* canvas) {
229 SkISize dim = canvas->getDeviceSize();
230 if (fFlags & kScale_Flag) {
231 const SkScalar x = SkIntToScalar(dim.fWidth) / 2;
232 const SkScalar y = SkIntToScalar(dim.fHeight) / 2;
234 canvas->translate(x, y);
235 // just enough so we can't take the sprite case
236 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
237 canvas->translate(-x, -y);
239 if (fFlags & kRotate_Flag) {
240 const SkScalar x = SkIntToScalar(dim.fWidth) / 2;
241 const SkScalar y = SkIntToScalar(dim.fHeight) / 2;
243 canvas->translate(x, y);
244 canvas->rotate(SkIntToScalar(35));
245 canvas->translate(-x, -y);
247 INHERITED::onDraw(loops, canvas);
250 void setupPaint(SkPaint* paint) SK_OVERRIDE {
251 this->INHERITED::setupPaint(paint);
254 if (fFlags & kBilerp_Flag) {
257 if (fFlags & kBicubic_Flag) {
260 static const SkFilterQuality gQualitys[] = {
261 kNone_SkFilterQuality,
262 kLow_SkFilterQuality,
263 kMedium_SkFilterQuality,
264 kHigh_SkFilterQuality
266 paint->setFilterQuality(gQualitys[index]);
270 typedef BitmapBench INHERITED;
273 /** Verify optimizations that test source alpha values. */
275 class SourceAlphaBitmapBench : public BitmapBench {
277 enum SourceAlpha { kOpaque_SourceAlpha, kTransparent_SourceAlpha,
278 kTwoStripes_SourceAlpha, kThreeStripes_SourceAlpha};
281 SourceAlpha fSourceAlpha;
283 SourceAlphaBitmapBench(SourceAlpha alpha, SkColorType ct,
284 bool forceUpdate = false, bool bitmapVolatile = false)
285 : INHERITED(ct, kPremul_SkAlphaType, forceUpdate, bitmapVolatile, false)
286 , fSourceAlpha(alpha) {
290 virtual const char* onGetName() {
291 fFullName.set(INHERITED::onGetName());
293 if (fSourceAlpha == kOpaque_SourceAlpha) {
294 fFullName.append("_source_opaque");
295 } else if (fSourceAlpha == kTransparent_SourceAlpha) {
296 fFullName.append("_source_transparent");
297 } else if (fSourceAlpha == kTwoStripes_SourceAlpha) {
298 fFullName.append("_source_stripes_two");
299 } else if (fSourceAlpha == kThreeStripes_SourceAlpha) {
300 fFullName.append("_source_stripes_three");
303 return fFullName.c_str();
306 void onDrawIntoBitmap(const SkBitmap& bm) SK_OVERRIDE {
307 const int w = bm.width();
308 const int h = bm.height();
310 if (kOpaque_SourceAlpha == fSourceAlpha) {
311 bm.eraseColor(SK_ColorBLACK);
312 } else if (kTransparent_SourceAlpha == fSourceAlpha) {
314 } else if (kTwoStripes_SourceAlpha == fSourceAlpha) {
319 p.setAntiAlias(false);
320 p.setStyle(SkPaint::kFill_Style);
321 p.setColor(SK_ColorRED);
323 // Draw red vertical stripes on transparent background
325 for (int x = 0; x < w; x+=2)
327 r.set(SkIntToScalar(x), 0, SkIntToScalar(x+1), SkIntToScalar(h));
328 canvas.drawRect(r, p);
331 } else if (kThreeStripes_SourceAlpha == fSourceAlpha) {
336 p.setAntiAlias(false);
337 p.setStyle(SkPaint::kFill_Style);
339 // Draw vertical stripes on transparent background with a pattern
340 // where the first pixel is fully transparent, the next is semi-transparent
341 // and the third is fully opaque.
343 for (int x = 0; x < w; x++)
346 continue; // Keep transparent
347 } else if (x % 3 == 1) {
348 p.setColor(SkColorSetARGB(127, 127, 127, 127)); // Semi-transparent
349 } else if (x % 3 == 2) {
350 p.setColor(SK_ColorRED); // Opaque
352 r.set(SkIntToScalar(x), 0, SkIntToScalar(x+1), SkIntToScalar(h));
353 canvas.drawRect(r, p);
359 typedef BitmapBench INHERITED;
362 DEF_BENCH( return new BitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, false, false); )
363 DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, false, false); )
364 DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, false, true); )
365 DEF_BENCH( return new BitmapBench(kRGB_565_SkColorType, kOpaque_SkAlphaType, false, false, false); )
366 DEF_BENCH( return new BitmapBench(kIndex_8_SkColorType, kPremul_SkAlphaType, false, false, false); )
367 DEF_BENCH( return new BitmapBench(kIndex_8_SkColorType, kOpaque_SkAlphaType, false, false, false); )
368 DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, true, false); )
369 DEF_BENCH( return new BitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, false, false); )
371 // scale filter -> S32_opaque_D32_filter_DX_{SSE2,SSSE3} and Fact9 is also for S32_D16_filter_DX_SSE2
372 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, false, kScale_Flag | kBilerp_Flag); )
373 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, false, kScale_Flag | kBilerp_Flag); )
374 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, true, kScale_Flag | kBilerp_Flag); )
375 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, false, kScale_Flag | kBilerp_Flag); )
377 // scale rotate filter -> S32_opaque_D32_filter_DXDY_{SSE2,SSSE3}
378 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, false, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
379 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, false, false, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
380 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, true, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
381 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kOpaque_SkAlphaType, true, false, kScale_Flag | kRotate_Flag | kBilerp_Flag); )
383 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, false, kScale_Flag | kBilerp_Flag | kBicubic_Flag); )
384 DEF_BENCH( return new FilterBitmapBench(kN32_SkColorType, kPremul_SkAlphaType, false, false, kScale_Flag | kRotate_Flag | kBilerp_Flag | kBicubic_Flag); )
386 // source alpha tests -> S32A_Opaque_BlitRow32_{arm,neon}
387 DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kOpaque_SourceAlpha, kN32_SkColorType); )
388 DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kTransparent_SourceAlpha, kN32_SkColorType); )
389 DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kTwoStripes_SourceAlpha, kN32_SkColorType); )
390 DEF_BENCH( return new SourceAlphaBitmapBench(SourceAlphaBitmapBench::kThreeStripes_SourceAlpha, kN32_SkColorType); )