blob: 51dd47a94a5068090f612d886619cad1cdb5073e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comf523e252009-01-26 23:15:37 +00008#include "SkBenchmark.h"
9#include "SkBitmap.h"
10#include "SkPaint.h"
11#include "SkCanvas.h"
12#include "SkColorPriv.h"
13#include "SkRandom.h"
14#include "SkString.h"
15
16static const char* gTileName[] = {
17 "clamp", "repeat", "mirror"
18};
19
20static const char* gConfigName[] = {
21 "ERROR", "a1", "a8", "index8", "565", "4444", "8888"
22};
23
24static void drawIntoBitmap(const SkBitmap& bm) {
25 const int w = bm.width();
26 const int h = bm.height();
27
28 SkCanvas canvas(bm);
29 SkPaint p;
30 p.setAntiAlias(true);
31 p.setColor(SK_ColorRED);
32 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
33 SkIntToScalar(SkMin32(w, h))*3/8, p);
34
35 SkRect r;
36 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
37 p.setStyle(SkPaint::kStroke_Style);
38 p.setStrokeWidth(SkIntToScalar(4));
39 p.setColor(SK_ColorBLUE);
40 canvas.drawRect(r, p);
41}
42
43static int conv6ToByte(int x) {
44 return x * 0xFF / 5;
45}
46
47static int convByteTo6(int x) {
48 return x * 5 / 255;
49}
50
51static uint8_t compute666Index(SkPMColor c) {
52 int r = SkGetPackedR32(c);
53 int g = SkGetPackedG32(c);
54 int b = SkGetPackedB32(c);
55
56 return convByteTo6(r) * 36 + convByteTo6(g) * 6 + convByteTo6(b);
57}
58
59static void convertToIndex666(const SkBitmap& src, SkBitmap* dst) {
60 SkColorTable* ctable = new SkColorTable(216);
61 SkPMColor* colors = ctable->lockColors();
62 // rrr ggg bbb
63 for (int r = 0; r < 6; r++) {
64 int rr = conv6ToByte(r);
65 for (int g = 0; g < 6; g++) {
66 int gg = conv6ToByte(g);
67 for (int b = 0; b < 6; b++) {
68 int bb = conv6ToByte(b);
69 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb);
70 }
71 }
72 }
73 ctable->unlockColors(true);
74 dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height());
75 dst->allocPixels(ctable);
76 ctable->unref();
77
78 SkAutoLockPixels alps(src);
79 SkAutoLockPixels alpd(*dst);
80
81 for (int y = 0; y < src.height(); y++) {
82 const SkPMColor* srcP = src.getAddr32(0, y);
83 uint8_t* dstP = dst->getAddr8(0, y);
84 for (int x = src.width() - 1; x >= 0; --x) {
85 *dstP++ = compute666Index(*srcP++);
86 }
87 }
88}
89
90/* Variants for bitmaps
91
92 - src depth (32 w+w/o alpha), 565, 4444, index, a8
93 - paint options: filtering, dither, alpha
94 - matrix options: translate, scale, rotate, persp
95 - tiling: none, repeat, mirror, clamp
96
97 */
98
99class BitmapBench : public SkBenchmark {
100 SkBitmap fBitmap;
101 SkPaint fPaint;
reed@android.com11ec1862009-10-19 19:01:45 +0000102 bool fIsOpaque;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000103 bool fForceUpdate; //bitmap marked as dirty before each draw. forces bitmap to be updated on device cache
reed@android.comf523e252009-01-26 23:15:37 +0000104 int fTileX, fTileY; // -1 means don't use shader
105 SkString fName;
tomhudson@google.comca529d32011-10-28 15:34:49 +0000106 enum { N = SkBENCHLOOP(300) };
reed@android.comf523e252009-01-26 23:15:37 +0000107public:
reed@android.com11ec1862009-10-19 19:01:45 +0000108 BitmapBench(void* param, bool isOpaque, SkBitmap::Config c,
junov@google.com4ee7ae52011-06-30 17:30:49 +0000109 bool forceUpdate = false, bool bitmapVolatile = false,
reed@android.com11ec1862009-10-19 19:01:45 +0000110 int tx = -1, int ty = -1)
junov@google.com4ee7ae52011-06-30 17:30:49 +0000111 : INHERITED(param), fIsOpaque(isOpaque), fForceUpdate(forceUpdate), fTileX(tx), fTileY(ty) {
reed@android.comf523e252009-01-26 23:15:37 +0000112 const int w = 128;
113 const int h = 128;
114 SkBitmap bm;
115
116 if (SkBitmap::kIndex8_Config == c) {
117 bm.setConfig(SkBitmap::kARGB_8888_Config, w, h);
118 } else {
119 bm.setConfig(c, w, h);
120 }
121 bm.allocPixels();
reed@android.com11ec1862009-10-19 19:01:45 +0000122 bm.eraseColor(isOpaque ? SK_ColorBLACK : 0);
reed@android.comf523e252009-01-26 23:15:37 +0000123
124 drawIntoBitmap(bm);
125
126 if (SkBitmap::kIndex8_Config == c) {
127 convertToIndex666(bm, &fBitmap);
128 } else {
129 fBitmap = bm;
130 }
reed@android.com11ec1862009-10-19 19:01:45 +0000131
132 if (fBitmap.getColorTable()) {
133 fBitmap.getColorTable()->setIsOpaque(isOpaque);
134 }
135 fBitmap.setIsOpaque(isOpaque);
junov@google.com4ee7ae52011-06-30 17:30:49 +0000136 fBitmap.setIsVolatile(bitmapVolatile);
reed@android.comf523e252009-01-26 23:15:37 +0000137 }
138
139protected:
140 virtual const char* onGetName() {
141 fName.set("bitmap");
142 if (fTileX >= 0) {
143 fName.appendf("_%s", gTileName[fTileX]);
144 if (fTileY != fTileX) {
145 fName.appendf("_%s", gTileName[fTileY]);
146 }
147 }
reed@android.com11ec1862009-10-19 19:01:45 +0000148 fName.appendf("_%s%s", gConfigName[fBitmap.config()],
149 fIsOpaque ? "" : "_A");
junov@google.com4ee7ae52011-06-30 17:30:49 +0000150 if (fForceUpdate)
151 fName.append("_update");
152 if (fBitmap.isVolatile())
153 fName.append("_volatile");
154
reed@android.comf523e252009-01-26 23:15:37 +0000155 return fName.c_str();
156 }
157
158 virtual void onDraw(SkCanvas* canvas) {
159 SkIPoint dim = this->getSize();
160 SkRandom rand;
161
162 SkPaint paint(fPaint);
163 this->setupPaint(&paint);
164
165 const SkBitmap& bitmap = fBitmap;
166 const SkScalar x0 = SkIntToScalar(-bitmap.width() / 2);
167 const SkScalar y0 = SkIntToScalar(-bitmap.height() / 2);
168
169 for (int i = 0; i < N; i++) {
170 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
171 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000172
173 if (fForceUpdate)
174 bitmap.notifyPixelsChanged();
175
reed@android.comf523e252009-01-26 23:15:37 +0000176 canvas->drawBitmap(bitmap, x, y, &paint);
177 }
178 }
179
180private:
181 typedef SkBenchmark INHERITED;
182};
183
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000184/** Explicitly invoke some filter types to improve coverage of acceleration
185 procs. */
186
187class FilterBitmapBench : public BitmapBench {
188 bool fScale;
189 bool fRotate;
190 bool fFilter;
191 SkString fFullName;
192 enum { N = SkBENCHLOOP(300) };
193public:
194 FilterBitmapBench(void* param, bool isOpaque, SkBitmap::Config c,
195 bool forceUpdate = false, bool bitmapVolatile = false,
196 int tx = -1, int ty = -1, bool addScale = false,
197 bool addRotate = false, bool addFilter = false)
198 : INHERITED(param, isOpaque, c, forceUpdate, bitmapVolatile, tx, ty)
199 , fScale(addScale), fRotate(addRotate), fFilter(addFilter) {
200
201 }
202
203protected:
204 virtual const char* onGetName() {
205 fFullName.set(INHERITED::onGetName());
206 if (fScale)
207 fFullName.append("_scale");
208 if (fRotate)
209 fFullName.append("_rotate");
210 if (fFilter)
211 fFullName.append("_filter");
212
213 return fFullName.c_str();
214 }
215
216 virtual void onDraw(SkCanvas* canvas) {
217 SkISize dim = canvas->getDeviceSize();
218 if (fScale) {
219 const SkScalar x = SkIntToScalar(dim.fWidth) / 2;
220 const SkScalar y = SkIntToScalar(dim.fHeight) / 2;
221
222 canvas->translate(x, y);
223 // just enough so we can't take the sprite case
224 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
225 canvas->translate(-x, -y);
226 }
227 if (fRotate) {
228 const SkScalar x = SkIntToScalar(dim.fWidth) / 2;
229 const SkScalar y = SkIntToScalar(dim.fHeight) / 2;
230
231 canvas->translate(x, y);
232 canvas->rotate(SkIntToScalar(35));
233 canvas->translate(-x, -y);
234 }
235
236 this->setForceFilter(fFilter);
237 INHERITED::onDraw(canvas);
238 }
239
240private:
241 typedef BitmapBench INHERITED;
242};
243
reed@android.com11ec1862009-10-19 19:01:45 +0000244static SkBenchmark* Fact0(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_8888_Config); }
245static SkBenchmark* Fact1(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config); }
246static SkBenchmark* Fact2(void* p) { return new BitmapBench(p, true, SkBitmap::kRGB_565_Config); }
247static SkBenchmark* Fact3(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_4444_Config); }
248static SkBenchmark* Fact4(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_4444_Config); }
249static SkBenchmark* Fact5(void* p) { return new BitmapBench(p, false, SkBitmap::kIndex8_Config); }
250static SkBenchmark* Fact6(void* p) { return new BitmapBench(p, true, SkBitmap::kIndex8_Config); }
junov@google.com4ee7ae52011-06-30 17:30:49 +0000251static SkBenchmark* Fact7(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, true); }
252static SkBenchmark* Fact8(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, false); }
reed@android.comf523e252009-01-26 23:15:37 +0000253
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000254// scale filter -> S32_opaque_D32_filter_DX_{SSE2,SSSE3}
255static SkBenchmark* Fact9(void* p) { return new FilterBitmapBench(p, false, SkBitmap::kARGB_8888_Config, false, false, -1, -1, true, false, true); }
256static SkBenchmark* Fact10(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, false, false, -1, -1, true, false, true); }
257static SkBenchmark* Fact11(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, true, -1, -1, true, false, true); }
258static SkBenchmark* Fact12(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, false, -1, -1, true, false, true); }
259
260// scale rotate filter -> S32_opaque_D32_filter_DXDY_{SSE2,SSSE3}
261static SkBenchmark* Fact13(void* p) { return new FilterBitmapBench(p, false, SkBitmap::kARGB_8888_Config, false, false, -1, -1, true, true, true); }
262static SkBenchmark* Fact14(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, false, false, -1, -1, true, true, true); }
263static SkBenchmark* Fact15(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, true, -1, -1, true, true, true); }
264static SkBenchmark* Fact16(void* p) { return new FilterBitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, false, -1, -1, true, true, true); }
265
reed@android.comf523e252009-01-26 23:15:37 +0000266static BenchRegistry gReg0(Fact0);
267static BenchRegistry gReg1(Fact1);
268static BenchRegistry gReg2(Fact2);
269static BenchRegistry gReg3(Fact3);
reed@android.com11ec1862009-10-19 19:01:45 +0000270static BenchRegistry gReg4(Fact4);
271static BenchRegistry gReg5(Fact5);
272static BenchRegistry gReg6(Fact6);
junov@google.com4ee7ae52011-06-30 17:30:49 +0000273static BenchRegistry gReg7(Fact7);
274static BenchRegistry gReg8(Fact8);
tomhudson@google.comc3be34d2012-05-15 20:09:33 +0000275
276static BenchRegistry gReg9(Fact9);
277static BenchRegistry gReg10(Fact10);
278static BenchRegistry gReg11(Fact11);
279static BenchRegistry gReg12(Fact12);
280
281static BenchRegistry gReg13(Fact13);
282static BenchRegistry gReg14(Fact14);
283static BenchRegistry gReg15(Fact15);
284static BenchRegistry gReg16(Fact16);
285