blob: f92277a14bc0a726b25323aad1f24c85d8521da7 [file] [log] [blame]
reed@android.combd700c32009-01-05 03:34:50 +00001#include "SkCanvas.h"
reed@android.comf523e252009-01-26 23:15:37 +00002#include "SkColorPriv.h"
reed@android.com3a859a02009-01-28 00:56:29 +00003#include "SkGraphics.h"
reed@android.comb398fe82009-01-07 11:47:57 +00004#include "SkImageEncoder.h"
reed@android.com6c924ad2009-03-31 03:48:49 +00005#include "SkNWayCanvas.h"
6#include "SkPicture.h"
reed@android.combd700c32009-01-05 03:34:50 +00007#include "SkString.h"
reed@android.com4bc19832009-01-19 20:08:35 +00008#include "SkTime.h"
reed@android.combd700c32009-01-05 03:34:50 +00009
10#include "SkBenchmark.h"
11
reed@android.com6c924ad2009-03-31 03:48:49 +000012static void erase(SkBitmap& bm) {
13 if (bm.config() == SkBitmap::kA8_Config) {
14 bm.eraseColor(0);
15 } else {
16 bm.eraseColor(SK_ColorWHITE);
17 }
18}
19
20static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) {
21 if (bm1.width() != bm2.width() ||
22 bm1.height() != bm2.height() ||
23 bm1.config() != bm2.config()) {
24 return false;
25 }
26
27 size_t pixelBytes = bm1.width() * bm1.bytesPerPixel();
28 for (int y = 0; y < bm1.height(); y++) {
29 if (memcmp(bm1.getAddr(0, y), bm2.getAddr(0, y), pixelBytes)) {
30 return false;
31 }
32 }
33
34 return true;
35}
36
reed@android.combd700c32009-01-05 03:34:50 +000037class Iter {
38public:
39 Iter() {
40 fBench = BenchRegistry::Head();
41 }
42
43 SkBenchmark* next() {
44 if (fBench) {
45 BenchRegistry::Factory f = fBench->factory();
46 fBench = fBench->next();
reed@android.coma70ba362009-01-22 17:41:57 +000047 return f(0);
reed@android.combd700c32009-01-05 03:34:50 +000048 }
49 return NULL;
50 }
51
52private:
53 const BenchRegistry* fBench;
54};
55
56static void make_filename(const char name[], SkString* path) {
57 path->set(name);
58 for (int i = 0; name[i]; i++) {
59 switch (name[i]) {
60 case '/':
61 case '\\':
62 case ' ':
63 case ':':
64 path->writable_str()[i] = '-';
65 break;
66 default:
67 break;
68 }
69 }
70}
71
reed@android.com4c7d3d62009-01-21 03:15:13 +000072static void saveFile(const char name[], const char config[], const char dir[],
73 const SkBitmap& bm) {
reed@android.com4c7d3d62009-01-21 03:15:13 +000074 SkBitmap copy;
75 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) {
76 return;
77 }
reed@android.comf523e252009-01-26 23:15:37 +000078
79 if (bm.config() == SkBitmap::kA8_Config) {
80 // turn alpha into gray-scale
81 size_t size = copy.getSize() >> 2;
82 SkPMColor* p = copy.getAddr32(0, 0);
83 for (size_t i = 0; i < size; i++) {
84 int c = (*p >> SK_A32_SHIFT) & 0xFF;
85 c = 255 - c;
86 c |= (c << 24) | (c << 16) | (c << 8);
87 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
88 }
89 }
reed@android.com4c7d3d62009-01-21 03:15:13 +000090
91 SkString str;
92 make_filename(name, &str);
93 str.appendf("_%s.png", config);
94 str.prepend(dir);
95 ::remove(str.c_str());
96 SkImageEncoder::EncodeFile(str.c_str(), copy, SkImageEncoder::kPNG_Type,
97 100);
reed@android.com4c7d3d62009-01-21 03:15:13 +000098}
99
100static void performClip(SkCanvas* canvas, int w, int h) {
101 SkRect r;
102
103 r.set(SkIntToScalar(10), SkIntToScalar(10),
104 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
105 canvas->clipRect(r, SkRegion::kIntersect_Op);
106
107 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
108 SkIntToScalar(w-10), SkIntToScalar(h-10));
109 canvas->clipRect(r, SkRegion::kXOR_Op);
110}
111
112static void performRotate(SkCanvas* canvas, int w, int h) {
113 const SkScalar x = SkIntToScalar(w) / 2;
114 const SkScalar y = SkIntToScalar(h) / 2;
115
116 canvas->translate(x, y);
117 canvas->rotate(SkIntToScalar(35));
118 canvas->translate(-x, -y);
119}
120
reed@android.com6c924ad2009-03-31 03:48:49 +0000121static void compare_pict_to_bitmap(SkPicture* pict, const SkBitmap& bm) {
122 SkBitmap bm2;
123
124 bm2.setConfig(bm.config(), bm.width(), bm.height());
125 bm2.allocPixels();
126 erase(bm2);
127
128 SkCanvas canvas(bm2);
129 canvas.drawPicture(*pict);
130
131 if (!equal(bm, bm2)) {
132 SkDebugf("----- compare_pict_to_bitmap failed\n");
133 }
134}
135
reed@android.com4bc19832009-01-19 20:08:35 +0000136static const struct {
137 SkBitmap::Config fConfig;
138 const char* fName;
139} gConfigs[] = {
140 { SkBitmap::kARGB_8888_Config, "8888" },
141 { SkBitmap::kRGB_565_Config, "565", },
142 { SkBitmap::kARGB_4444_Config, "4444", },
143 { SkBitmap::kA8_Config, "A8", }
144};
145
reed@android.com4c7d3d62009-01-21 03:15:13 +0000146static int findConfig(const char config[]) {
147 for (size_t i = 0; i < SK_ARRAY_COUNT(gConfigs); i++) {
148 if (!strcmp(config, gConfigs[i].fName)) {
149 return i;
150 }
151 }
152 return -1;
153}
154
reed@android.com3a859a02009-01-28 00:56:29 +0000155class SkAutoGraphics {
156public:
reed@android.com5e5adfd2009-03-07 03:39:23 +0000157 SkAutoGraphics() {
158 SkGraphics::Init();
reed@android.com3a859a02009-01-28 00:56:29 +0000159 }
160 ~SkAutoGraphics() {
161 SkGraphics::Term();
162 }
163};
164
reed@android.combd700c32009-01-05 03:34:50 +0000165int main (int argc, char * const argv[]) {
reed@android.com3a859a02009-01-28 00:56:29 +0000166 SkAutoGraphics ag;
167
reed@android.com4bc19832009-01-19 20:08:35 +0000168 int repeatDraw = 1;
169 int forceAlpha = 0xFF;
170 bool forceAA = true;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000171 bool doRotate = false;
172 bool doClip = false;
reed@android.com6c924ad2009-03-31 03:48:49 +0000173 bool doPict = false;
reed@android.com4bc19832009-01-19 20:08:35 +0000174
reed@android.comb398fe82009-01-07 11:47:57 +0000175 SkString outDir;
176 SkBitmap::Config outConfig = SkBitmap::kARGB_8888_Config;
reed@android.combd700c32009-01-05 03:34:50 +0000177
reed@android.comb398fe82009-01-07 11:47:57 +0000178 char* const* stop = argv + argc;
179 for (++argv; argv < stop; ++argv) {
180 if (strcmp(*argv, "-o") == 0) {
181 argv++;
182 if (argv < stop && **argv) {
183 outDir.set(*argv);
184 if (outDir.c_str()[outDir.size() - 1] != '/') {
185 outDir.append("/");
186 }
187 }
reed@android.com6c924ad2009-03-31 03:48:49 +0000188 } else if (strcmp(*argv, "-pict") == 0) {
189 doPict = true;
reed@android.com4bc19832009-01-19 20:08:35 +0000190 } else if (strcmp(*argv, "-repeat") == 0) {
191 argv++;
192 if (argv < stop) {
193 repeatDraw = atoi(*argv);
194 if (repeatDraw < 1) {
195 repeatDraw = 1;
196 }
197 } else {
198 fprintf(stderr, "missing arg for -repeat\n");
199 return -1;
200 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000201 } else if (!strcmp(*argv, "-rotate")) {
202 doRotate = true;
203 } else if (!strcmp(*argv, "-clip")) {
204 doClip = true;
reed@android.com4bc19832009-01-19 20:08:35 +0000205 } else if (strcmp(*argv, "-forceAA") == 0) {
206 forceAA = true;
207 } else if (strcmp(*argv, "-forceBW") == 0) {
208 forceAA = false;
209 } else if (strcmp(*argv, "-forceBlend") == 0) {
210 forceAlpha = 0x80;
211 } else if (strcmp(*argv, "-forceOpaque") == 0) {
212 forceAlpha = 0xFF;
reed@android.com4c7d3d62009-01-21 03:15:13 +0000213 } else {
214 int index = findConfig(*argv);
215 if (index >= 0) {
216 outConfig = gConfigs[index].fConfig;
217 }
reed@android.comb398fe82009-01-07 11:47:57 +0000218 }
219 }
reed@android.com4bc19832009-01-19 20:08:35 +0000220
221 const char* configName = "";
222 int configCount = SK_ARRAY_COUNT(gConfigs);
223
reed@android.combd700c32009-01-05 03:34:50 +0000224 Iter iter;
225 SkBenchmark* bench;
226 while ((bench = iter.next()) != NULL) {
reed@android.comb398fe82009-01-07 11:47:57 +0000227 SkIPoint dim = bench->getSize();
228 if (dim.fX <= 0 || dim.fY <= 0) {
229 continue;
230 }
reed@android.comb398fe82009-01-07 11:47:57 +0000231
reed@android.com4bc19832009-01-19 20:08:35 +0000232 bench->setForceAlpha(forceAlpha);
233 bench->setForceAA(forceAA);
234
reed@android.comf523e252009-01-26 23:15:37 +0000235 printf("running bench %16s", bench->getName());
reed@android.com4bc19832009-01-19 20:08:35 +0000236
237 for (int configIndex = 0; configIndex < configCount; configIndex++) {
238 if (configCount > 1) {
239 outConfig = gConfigs[configIndex].fConfig;
240 configName = gConfigs[configIndex].fName;
241 }
242
243 SkBitmap bm;
244 bm.setConfig(outConfig, dim.fX, dim.fY);
245 bm.allocPixels();
reed@android.com6c924ad2009-03-31 03:48:49 +0000246 erase(bm);
reed@android.comf523e252009-01-26 23:15:37 +0000247
reed@android.com4bc19832009-01-19 20:08:35 +0000248 SkCanvas canvas(bm);
reed@android.comf523e252009-01-26 23:15:37 +0000249
reed@android.com4c7d3d62009-01-21 03:15:13 +0000250 if (doClip) {
251 performClip(&canvas, dim.fX, dim.fY);
252 }
253 if (doRotate) {
254 performRotate(&canvas, dim.fX, dim.fY);
255 }
256
reed@android.com4bc19832009-01-19 20:08:35 +0000257 SkMSec now = SkTime::GetMSecs();
258 for (int i = 0; i < repeatDraw; i++) {
reed@android.com6c924ad2009-03-31 03:48:49 +0000259 SkCanvas* c = &canvas;
260
261 SkNWayCanvas nway;
262 SkPicture* pict = NULL;
263 if (doPict) {
264 pict = new SkPicture;
265 nway.addCanvas(pict->beginRecording(bm.width(), bm.height()));
266 nway.addCanvas(&canvas);
267 c = &nway;
268 }
269
270 SkAutoCanvasRestore acr(c, true);
271 bench->draw(c);
272
273 if (pict) {
274 compare_pict_to_bitmap(pict, bm);
275 pict->unref();
276 }
reed@android.com4bc19832009-01-19 20:08:35 +0000277 }
278 if (repeatDraw > 1) {
279 printf(" %4s:%7.2f", configName,
280 (SkTime::GetMSecs() - now) / (double)repeatDraw);
281 }
reed@android.com4c7d3d62009-01-21 03:15:13 +0000282 if (outDir.size() > 0) {
283 saveFile(bench->getName(), configName, outDir.c_str(), bm);
284 }
reed@android.com4bc19832009-01-19 20:08:35 +0000285 }
286 printf("\n");
reed@android.combd700c32009-01-05 03:34:50 +0000287 }
288
289 return 0;
290}