blob: 9db4ae399884f48c5dadfba82f666d41f169f3af [file] [log] [blame]
reed@android.comf523e252009-01-26 23:15:37 +00001#include "SkBenchmark.h"
2#include "SkBitmap.h"
3#include "SkPaint.h"
4#include "SkCanvas.h"
5#include "SkColorPriv.h"
6#include "SkRandom.h"
7#include "SkString.h"
8
9static const char* gTileName[] = {
10 "clamp", "repeat", "mirror"
11};
12
13static const char* gConfigName[] = {
14 "ERROR", "a1", "a8", "index8", "565", "4444", "8888"
15};
16
17static void drawIntoBitmap(const SkBitmap& bm) {
18 const int w = bm.width();
19 const int h = bm.height();
20
21 SkCanvas canvas(bm);
22 SkPaint p;
23 p.setAntiAlias(true);
24 p.setColor(SK_ColorRED);
25 canvas.drawCircle(SkIntToScalar(w)/2, SkIntToScalar(h)/2,
26 SkIntToScalar(SkMin32(w, h))*3/8, p);
27
28 SkRect r;
29 r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
30 p.setStyle(SkPaint::kStroke_Style);
31 p.setStrokeWidth(SkIntToScalar(4));
32 p.setColor(SK_ColorBLUE);
33 canvas.drawRect(r, p);
34}
35
36static int conv6ToByte(int x) {
37 return x * 0xFF / 5;
38}
39
40static int convByteTo6(int x) {
41 return x * 5 / 255;
42}
43
44static uint8_t compute666Index(SkPMColor c) {
45 int r = SkGetPackedR32(c);
46 int g = SkGetPackedG32(c);
47 int b = SkGetPackedB32(c);
48
49 return convByteTo6(r) * 36 + convByteTo6(g) * 6 + convByteTo6(b);
50}
51
52static void convertToIndex666(const SkBitmap& src, SkBitmap* dst) {
53 SkColorTable* ctable = new SkColorTable(216);
54 SkPMColor* colors = ctable->lockColors();
55 // rrr ggg bbb
56 for (int r = 0; r < 6; r++) {
57 int rr = conv6ToByte(r);
58 for (int g = 0; g < 6; g++) {
59 int gg = conv6ToByte(g);
60 for (int b = 0; b < 6; b++) {
61 int bb = conv6ToByte(b);
62 *colors++ = SkPreMultiplyARGB(0xFF, rr, gg, bb);
63 }
64 }
65 }
66 ctable->unlockColors(true);
67 dst->setConfig(SkBitmap::kIndex8_Config, src.width(), src.height());
68 dst->allocPixels(ctable);
69 ctable->unref();
70
71 SkAutoLockPixels alps(src);
72 SkAutoLockPixels alpd(*dst);
73
74 for (int y = 0; y < src.height(); y++) {
75 const SkPMColor* srcP = src.getAddr32(0, y);
76 uint8_t* dstP = dst->getAddr8(0, y);
77 for (int x = src.width() - 1; x >= 0; --x) {
78 *dstP++ = compute666Index(*srcP++);
79 }
80 }
81}
82
83/* Variants for bitmaps
84
85 - src depth (32 w+w/o alpha), 565, 4444, index, a8
86 - paint options: filtering, dither, alpha
87 - matrix options: translate, scale, rotate, persp
88 - tiling: none, repeat, mirror, clamp
89
90 */
91
92class BitmapBench : public SkBenchmark {
93 SkBitmap fBitmap;
94 SkPaint fPaint;
reed@android.com11ec1862009-10-19 19:01:45 +000095 bool fIsOpaque;
junov@google.com4ee7ae52011-06-30 17:30:49 +000096 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 +000097 int fTileX, fTileY; // -1 means don't use shader
98 SkString fName;
99 enum { N = 300 };
100public:
reed@android.com11ec1862009-10-19 19:01:45 +0000101 BitmapBench(void* param, bool isOpaque, SkBitmap::Config c,
junov@google.com4ee7ae52011-06-30 17:30:49 +0000102 bool forceUpdate = false, bool bitmapVolatile = false,
reed@android.com11ec1862009-10-19 19:01:45 +0000103 int tx = -1, int ty = -1)
junov@google.com4ee7ae52011-06-30 17:30:49 +0000104 : INHERITED(param), fIsOpaque(isOpaque), fForceUpdate(forceUpdate), fTileX(tx), fTileY(ty) {
reed@android.comf523e252009-01-26 23:15:37 +0000105 const int w = 128;
106 const int h = 128;
107 SkBitmap bm;
108
109 if (SkBitmap::kIndex8_Config == c) {
110 bm.setConfig(SkBitmap::kARGB_8888_Config, w, h);
111 } else {
112 bm.setConfig(c, w, h);
113 }
114 bm.allocPixels();
reed@android.com11ec1862009-10-19 19:01:45 +0000115 bm.eraseColor(isOpaque ? SK_ColorBLACK : 0);
reed@android.comf523e252009-01-26 23:15:37 +0000116
117 drawIntoBitmap(bm);
118
119 if (SkBitmap::kIndex8_Config == c) {
120 convertToIndex666(bm, &fBitmap);
121 } else {
122 fBitmap = bm;
123 }
reed@android.com11ec1862009-10-19 19:01:45 +0000124
125 if (fBitmap.getColorTable()) {
126 fBitmap.getColorTable()->setIsOpaque(isOpaque);
127 }
128 fBitmap.setIsOpaque(isOpaque);
junov@google.com4ee7ae52011-06-30 17:30:49 +0000129 fBitmap.setIsVolatile(bitmapVolatile);
reed@android.comf523e252009-01-26 23:15:37 +0000130 }
131
132protected:
133 virtual const char* onGetName() {
134 fName.set("bitmap");
135 if (fTileX >= 0) {
136 fName.appendf("_%s", gTileName[fTileX]);
137 if (fTileY != fTileX) {
138 fName.appendf("_%s", gTileName[fTileY]);
139 }
140 }
reed@android.com11ec1862009-10-19 19:01:45 +0000141 fName.appendf("_%s%s", gConfigName[fBitmap.config()],
142 fIsOpaque ? "" : "_A");
junov@google.com4ee7ae52011-06-30 17:30:49 +0000143 if (fForceUpdate)
144 fName.append("_update");
145 if (fBitmap.isVolatile())
146 fName.append("_volatile");
147
reed@android.comf523e252009-01-26 23:15:37 +0000148 return fName.c_str();
149 }
150
151 virtual void onDraw(SkCanvas* canvas) {
152 SkIPoint dim = this->getSize();
153 SkRandom rand;
154
155 SkPaint paint(fPaint);
156 this->setupPaint(&paint);
157
158 const SkBitmap& bitmap = fBitmap;
159 const SkScalar x0 = SkIntToScalar(-bitmap.width() / 2);
160 const SkScalar y0 = SkIntToScalar(-bitmap.height() / 2);
161
162 for (int i = 0; i < N; i++) {
163 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
164 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
junov@google.com4ee7ae52011-06-30 17:30:49 +0000165
166 if (fForceUpdate)
167 bitmap.notifyPixelsChanged();
168
reed@android.comf523e252009-01-26 23:15:37 +0000169 canvas->drawBitmap(bitmap, x, y, &paint);
170 }
171 }
172
173private:
174 typedef SkBenchmark INHERITED;
175};
176
reed@android.com11ec1862009-10-19 19:01:45 +0000177static SkBenchmark* Fact0(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_8888_Config); }
178static SkBenchmark* Fact1(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config); }
179static SkBenchmark* Fact2(void* p) { return new BitmapBench(p, true, SkBitmap::kRGB_565_Config); }
180static SkBenchmark* Fact3(void* p) { return new BitmapBench(p, false, SkBitmap::kARGB_4444_Config); }
181static SkBenchmark* Fact4(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_4444_Config); }
182static SkBenchmark* Fact5(void* p) { return new BitmapBench(p, false, SkBitmap::kIndex8_Config); }
183static SkBenchmark* Fact6(void* p) { return new BitmapBench(p, true, SkBitmap::kIndex8_Config); }
junov@google.com4ee7ae52011-06-30 17:30:49 +0000184static SkBenchmark* Fact7(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, true); }
185static SkBenchmark* Fact8(void* p) { return new BitmapBench(p, true, SkBitmap::kARGB_8888_Config, true, false); }
reed@android.comf523e252009-01-26 23:15:37 +0000186
187static BenchRegistry gReg0(Fact0);
188static BenchRegistry gReg1(Fact1);
189static BenchRegistry gReg2(Fact2);
190static BenchRegistry gReg3(Fact3);
reed@android.com11ec1862009-10-19 19:01:45 +0000191static BenchRegistry gReg4(Fact4);
192static BenchRegistry gReg5(Fact5);
193static BenchRegistry gReg6(Fact6);
junov@google.com4ee7ae52011-06-30 17:30:49 +0000194static BenchRegistry gReg7(Fact7);
195static BenchRegistry gReg8(Fact8);