blob: 89dfa74bc4d0ba99a1cd44cfc1b7a0320857581a [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;
95 int fTileX, fTileY; // -1 means don't use shader
96 SkString fName;
97 enum { N = 300 };
98public:
reed@android.come9d00602009-09-02 21:12:42 +000099 BitmapBench(void* param, SkBitmap::Config c, int tx = -1, int ty = -1)
100 : INHERITED(param), fTileX(tx), fTileY(ty) {
reed@android.comf523e252009-01-26 23:15:37 +0000101 const int w = 128;
102 const int h = 128;
103 SkBitmap bm;
104
105 if (SkBitmap::kIndex8_Config == c) {
106 bm.setConfig(SkBitmap::kARGB_8888_Config, w, h);
107 } else {
108 bm.setConfig(c, w, h);
109 }
110 bm.allocPixels();
111 bm.eraseColor(0);
112
113 drawIntoBitmap(bm);
114
115 if (SkBitmap::kIndex8_Config == c) {
116 convertToIndex666(bm, &fBitmap);
117 } else {
118 fBitmap = bm;
119 }
120 }
121
122protected:
123 virtual const char* onGetName() {
124 fName.set("bitmap");
125 if (fTileX >= 0) {
126 fName.appendf("_%s", gTileName[fTileX]);
127 if (fTileY != fTileX) {
128 fName.appendf("_%s", gTileName[fTileY]);
129 }
130 }
131 fName.appendf("_%s", gConfigName[fBitmap.config()]);
132 return fName.c_str();
133 }
134
135 virtual void onDraw(SkCanvas* canvas) {
136 SkIPoint dim = this->getSize();
137 SkRandom rand;
138
139 SkPaint paint(fPaint);
140 this->setupPaint(&paint);
141
142 const SkBitmap& bitmap = fBitmap;
143 const SkScalar x0 = SkIntToScalar(-bitmap.width() / 2);
144 const SkScalar y0 = SkIntToScalar(-bitmap.height() / 2);
145
146 for (int i = 0; i < N; i++) {
147 SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
148 SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
149 canvas->drawBitmap(bitmap, x, y, &paint);
150 }
151 }
152
153private:
154 typedef SkBenchmark INHERITED;
155};
156
reed@android.come9d00602009-09-02 21:12:42 +0000157static SkBenchmark* Fact0(void* p) { return new BitmapBench(p, SkBitmap::kARGB_8888_Config); }
158static SkBenchmark* Fact1(void* p) { return new BitmapBench(p, SkBitmap::kRGB_565_Config); }
159static SkBenchmark* Fact2(void* p) { return new BitmapBench(p, SkBitmap::kARGB_4444_Config); }
160static SkBenchmark* Fact3(void* p) { return new BitmapBench(p, SkBitmap::kIndex8_Config); }
reed@android.comf523e252009-01-26 23:15:37 +0000161
162static BenchRegistry gReg0(Fact0);
163static BenchRegistry gReg1(Fact1);
164static BenchRegistry gReg2(Fact2);
165static BenchRegistry gReg3(Fact3);