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