blob: 952f386740028d3df1c8b60431ad756511b018e0 [file] [log] [blame]
reed@android.com00dae862009-06-10 15:38:48 +00001#include "gm.h"
reed@android.comb9b9a182009-07-08 02:54:47 +00002#include "SkColorPriv.h"
reed@android.com8015dd82009-06-21 00:49:18 +00003#include "SkGraphics.h"
4#include "SkImageDecoder.h"
5#include "SkImageEncoder.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +00006#include "SkPicture.h"
reed@google.com07700442010-12-20 19:46:07 +00007#include "SkStream.h"
8#include "SkRefCnt.h"
9
reed@google.com873cb1e2010-12-23 15:00:45 +000010#include "GrContext.h"
11#include "SkGpuCanvas.h"
reed@google.comd4dfd102011-01-18 21:05:42 +000012#include "SkGpuDevice.h"
reed@google.com873cb1e2010-12-23 15:00:45 +000013#include "SkEGLContext.h"
14#include "SkDevice.h"
15
reed@google.com07700442010-12-20 19:46:07 +000016#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000017 #include "SkPDFDevice.h"
18 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000019#endif
reed@android.com00dae862009-06-10 15:38:48 +000020
21using namespace skiagm;
22
23// need to explicitly declare this, or we get some weird infinite loop llist
24template GMRegistry* GMRegistry::gHead;
25
26class Iter {
27public:
28 Iter() {
reed@android.comdd0ac282009-06-20 02:38:16 +000029 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000030 }
reed@google.comd4dfd102011-01-18 21:05:42 +000031
reed@android.comdd0ac282009-06-20 02:38:16 +000032 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000033 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000034 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000035 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000036 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000037 }
38 return NULL;
39 }
reed@google.comd4dfd102011-01-18 21:05:42 +000040
reed@android.com00dae862009-06-10 15:38:48 +000041 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000042 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000043 int count = 0;
44 while (reg) {
45 count += 1;
46 reg = reg->next();
47 }
48 return count;
49 }
reed@google.comd4dfd102011-01-18 21:05:42 +000050
reed@android.com00dae862009-06-10 15:38:48 +000051private:
52 const GMRegistry* fReg;
53};
54
reed@android.com8015dd82009-06-21 00:49:18 +000055static SkString make_name(const char shortName[], const char configName[]) {
56 SkString name(shortName);
57 name.appendf("_%s", configName);
58 return name;
59}
60
tomhudson@google.com9875dd12011-04-25 15:49:53 +000061static SkString make_filename(const char path[],
62 const char pathSuffix[],
63 const SkString& name,
64 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +000065 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +000066 if (filename.endsWith("/")) {
67 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +000068 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +000069 filename.append(pathSuffix);
70 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +000071 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +000072 return filename;
73}
74
reed@android.comb9b9a182009-07-08 02:54:47 +000075/* since PNG insists on unpremultiplying our alpha, we take no precision chances
76 and force all pixels to be 100% opaque, otherwise on compare we may not get
77 a perfect match.
78 */
79static void force_all_opaque(const SkBitmap& bitmap) {
80 SkAutoLockPixels lock(bitmap);
81 for (int y = 0; y < bitmap.height(); y++) {
82 for (int x = 0; x < bitmap.width(); x++) {
83 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
84 }
85 }
86}
87
88static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
89 SkBitmap copy;
90 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
91 force_all_opaque(copy);
92 return SkImageEncoder::EncodeFile(path.c_str(), copy,
93 SkImageEncoder::kPNG_Type, 100);
94}
95
reed@google.com3d3f0922010-12-20 21:10:29 +000096static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +000097 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
98 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
99 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
100 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000101}
102
103static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000104 SkBitmap* diff) {
105 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000106
107 const int w = target.width();
108 const int h = target.height();
109 for (int y = 0; y < h; y++) {
110 for (int x = 0; x < w; x++) {
111 SkPMColor c0 = *base.getAddr32(x, y);
112 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000113 SkPMColor d = 0;
114 if (c0 != c1) {
115 d = compute_diff_pmcolor(c0, c1);
116 }
117 *diff->getAddr32(x, y) = d;
118 }
119 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000120}
121
122static bool compare(const SkBitmap& target, const SkBitmap& base,
123 const SkString& name, SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000124 SkBitmap copy;
125 const SkBitmap* bm = &target;
126 if (target.config() != SkBitmap::kARGB_8888_Config) {
127 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
128 bm = &copy;
129 }
130
131 force_all_opaque(*bm);
132
133 const int w = bm->width();
134 const int h = bm->height();
135 if (w != base.width() || h != base.height()) {
136 SkDebugf("---- dimensions mismatch for %s base [%d %d] current [%d %d]\n",
137 name.c_str(), base.width(), base.height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000138 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000139 }
140
141 SkAutoLockPixels bmLock(*bm);
142 SkAutoLockPixels baseLock(base);
143
144 for (int y = 0; y < h; y++) {
145 for (int x = 0; x < w; x++) {
146 SkPMColor c0 = *base.getAddr32(x, y);
147 SkPMColor c1 = *bm->getAddr32(x, y);
148 if (c0 != c1) {
149 SkDebugf("----- pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
150 name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000151
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000152 if (diff) {
153 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
154 diff->allocPixels();
155 compute_diff(*bm, base, diff);
156 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000157 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000158 }
159 }
160 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000161
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000162 // they're equal
163 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000164}
reed@android.com00dae862009-06-10 15:38:48 +0000165
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000166static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) {
167 SkFILEWStream stream(path.c_str());
168 return stream.write(pdf.getStream(), pdf.getOffset());
reed@google.com07700442010-12-20 19:46:07 +0000169}
170
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000171enum Backend {
172 kRaster_Backend,
173 kGPU_Backend,
174 kPDF_Backend,
175};
176
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000177struct ConfigData {
178 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000179 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000180 const char* fName;
181};
182
183/// Returns true if processing should continue, false to skip the
184/// remainder of this config for this GM.
185//@todo thudson 22 April 2011 - could refactor this to take in
186// a factory to generate the context, always call readPixels()
187// (logically a noop for rasters, if wasted time), and thus collapse the
188// GPU special case and also let this be used for SkPicture testing.
189static void setup_bitmap(const ConfigData& gRec, SkISize& size,
190 SkBitmap* bitmap) {
191 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
192 bitmap->allocPixels();
193 bitmap->eraseColor(0);
194}
195
196static bool generate_image(GM* gm, const ConfigData& gRec,
197 GrContext* context,
198 SkBitmap& bitmap) {
199 SkISize size (gm->getISize());
200 setup_bitmap(gRec, size, &bitmap);
201 SkCanvas canvas(bitmap);
202
203 if (gRec.fBackend == kRaster_Backend) {
204 gm->draw(&canvas);
205 } else { // GPU
206 if (NULL == context) {
207 return false;
208 }
209 SkGpuCanvas gc(context,
210 SkGpuDevice::Current3DApiRenderTarget());
211 gc.setDevice(gc.createDevice(bitmap.config(),
212 bitmap.width(),
213 bitmap.height(),
214 bitmap.isOpaque(),
215 false))->unref();
216 gm->draw(&gc);
217 gc.readPixels(&bitmap); // overwrite our previous allocation
218 }
219 return true;
220}
221
222static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
223 SkPicture* pict, SkBitmap* bitmap) {
224 SkISize size = gm->getISize();
225 setup_bitmap(gRec, size, bitmap);
226 SkCanvas canvas(*bitmap);
227 canvas.drawPicture(*pict);
228}
229
230static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
231#ifdef SK_SUPPORT_PDF
232 SkISize size = gm->getISize();
233 SkMatrix identity;
234 identity.reset();
235 SkPDFDevice* dev = new SkPDFDevice(size.width(), size.height(),
236 identity);
237 SkAutoUnref aur(dev);
238
239 SkCanvas c(dev);
240 gm->draw(&c);
241
242 SkPDFDocument doc;
243 doc.appendPage(dev);
244 doc.emitPDF(&pdf);
245#endif
246}
247
248static void write_reference_image(const ConfigData& gRec,
249 const char writePath [],
250 const char writePathSuffix [],
251 const SkString& name,
252 SkBitmap& bitmap,
253 SkDynamicMemoryWStream* pdf) {
254 SkString path;
255 bool success = false;
256 if (gRec.fBackend != kPDF_Backend) {
257 path = make_filename(writePath, writePathSuffix, name, "png");
258 success = write_bitmap(path, bitmap);
259 } else if (pdf) {
260 path = make_filename(writePath, writePathSuffix, name, "pdf");
261 success = write_pdf(path, *pdf);
262 }
263 if (!success) {
264 fprintf(stderr, "FAILED to write %s\n", path.c_str());
265 }
266}
267
268static void compare_to_reference_image(const char readPath [],
269 const SkString& name,
270 SkBitmap &bitmap,
271 const char diffPath []) {
272 SkString path = make_filename(readPath, "", name, "png");
273 SkBitmap orig;
274 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
275 SkBitmap::kARGB_8888_Config,
276 SkImageDecoder::kDecodePixels_Mode, NULL);
277 if (success) {
278 SkBitmap diffBitmap;
279 success = compare(bitmap, orig, name, diffPath ? &diffBitmap : NULL);
280 if (!success && diffPath) {
281 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
282 fprintf(stderr, "Writing %s\n", diffName.c_str());
283 write_bitmap(diffName, diffBitmap);
284 }
285 } else {
286 fprintf(stderr, "FAILED to read %s\n", path.c_str());
287 }
288
289}
290
291static void handle_test_results(GM* gm,
292 const ConfigData& gRec,
293 const char writePath [],
294 const char readPath [],
295 const char diffPath [],
296 const char writePathSuffix [],
297 SkBitmap& bitmap,
298 SkDynamicMemoryWStream* pdf) {
299 SkString name = make_name(gm->shortName(), gRec.fName);
300
301 if (writePath) {
302 write_reference_image(gRec, writePath, writePathSuffix,
303 name, bitmap, pdf);
304 // TODO: Figure out a way to compare PDFs.
305 } else if (readPath && gRec.fBackend != kPDF_Backend) {
306 compare_to_reference_image(readPath, name, bitmap,
307 diffPath);
308 }
309}
310
311static SkPicture* generate_new_picture(GM* gm) {
312 // Pictures are refcounted so must be on heap
313 SkPicture* pict = new SkPicture;
314 SkCanvas* cv = pict->beginRecording(1000, 1000);
315 gm->draw(cv);
316 pict->endRecording();
317
318 return pict;
319}
320
321static SkPicture* stream_to_new_picture(const SkPicture& src) {
322
323 // To do in-memory commiunications with a stream, we need to:
324 // * create a dynamic memory stream
325 // * copy it into a buffer
326 // * create a read stream from it
327 // ?!?!
328
329 SkDynamicMemoryWStream storage;
330 src.serialize(&storage);
331
332 int streamSize = storage.getOffset();
333 SkAutoMalloc dstStorage(streamSize);
334 void* dst = dstStorage.get();
335 //char* dst = new char [streamSize];
336 //@todo thudson 22 April 2011 when can we safely delete [] dst?
337 storage.copyTo(dst);
338 SkMemoryStream pictReadback(dst, streamSize);
339 SkPicture* retval = new SkPicture (&pictReadback);
340 return retval;
341}
342
343// Test: draw into a bitmap or pdf.
344// Depending on flags, possibly compare to an expected image
345// and possibly output a diff image if it fails to match.
346static void test_drawing(GM* gm,
347 const ConfigData& gRec,
348 const char writePath [],
349 const char readPath [],
350 const char diffPath [],
351 GrContext* context) {
352 SkBitmap bitmap;
353 SkDynamicMemoryWStream pdf;
354
355 if (gRec.fBackend == kRaster_Backend ||
356 gRec.fBackend == kGPU_Backend) {
357 if (!generate_image(gm, gRec, context, bitmap)) {
358 return;
359 }
360 }
361 // TODO: Figure out a way to compare PDFs.
362 if (gRec.fBackend == kPDF_Backend && writePath) {
363 generate_pdf(gm, pdf);
364 }
365 handle_test_results(gm, gRec, writePath, readPath, diffPath,
366 "", bitmap, &pdf);
367}
368
369static void test_picture_playback(GM* gm,
370 const ConfigData& gRec,
371 const char writePath [],
372 const char readPath [],
373 const char diffPath []) {
374 SkPicture* pict = generate_new_picture(gm);
375 SkAutoUnref aur(pict);
376
377 //@todo thudson 22 April 2011 wrap GM with a proxy so we can
378 // pass this pict to generate_image()?
379 if (kRaster_Backend == gRec.fBackend) {
380 SkBitmap bitmap;
381 generate_image_from_picture(gm, gRec, pict, &bitmap);
382 handle_test_results(gm, gRec, writePath, readPath, diffPath,
383 "-pict", bitmap, NULL);
384 }
385}
386
387static void test_picture_serialization(GM* gm,
388 const ConfigData& gRec,
389 const char writePath [],
390 const char readPath [],
391 const char diffPath []) {
392 SkPicture* pict = generate_new_picture(gm);
393 SkAutoUnref aurp(pict);
394 SkPicture* repict = stream_to_new_picture(*pict);
395 SkAutoUnref aurr(repict);
396
397 //@todo thudson 22 April 2011 wrap GM with a proxy so we can
398 // pass this pict to generate_image()?
399 if (kRaster_Backend == gRec.fBackend) {
400 SkBitmap bitmap;
401 generate_image_from_picture(gm, gRec, repict, &bitmap);
402 handle_test_results(gm, gRec, writePath, readPath, diffPath,
403 "-replay", bitmap, NULL);
404 }
405}
406
407static void usage(const char * argv0) {
408 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
409 SkDebugf(" writePath: directory to write rendered images in.\n");
410 SkDebugf(" readPath: directory to read reference images from;\n"
411 " reports if any pixels mismatch between reference and newly rendered\n");
412 SkDebugf(" diffPath: directory to write difference images in.\n");
413}
414
415static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000416 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
417 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
418 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
419 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
420#ifdef SK_SUPPORT_PDF
421 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
422#endif
reed@android.com00dae862009-06-10 15:38:48 +0000423};
424
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000425int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000426 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000427
reed@android.com8015dd82009-06-21 00:49:18 +0000428 const char* writePath = NULL; // if non-null, where we write the originals
429 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000430 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000431
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000432 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000433 char* const* stop = argv + argc;
434 for (++argv; argv < stop; ++argv) {
435 if (strcmp(*argv, "-w") == 0) {
436 argv++;
437 if (argv < stop && **argv) {
438 writePath = *argv;
439 }
440 } else if (strcmp(*argv, "-r") == 0) {
441 argv++;
442 if (argv < stop && **argv) {
443 readPath = *argv;
444 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000445 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000446 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000447 if (argv < stop && **argv) {
448 diffPath = *argv;
449 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000450 } else {
451 usage(commandName);
452 return 0;
453 }
454 }
455 if (argv != stop) {
456 usage(commandName);
457 return 0;
458 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000459
460 // setup a GL context for drawing offscreen
reed@google.com37df17d2010-12-23 20:20:51 +0000461 GrContext* context = NULL;
reed@google.com873cb1e2010-12-23 15:00:45 +0000462 SkEGLContext eglContext;
reed@google.com37df17d2010-12-23 20:20:51 +0000463 if (eglContext.init(1024, 1024)) {
464 context = GrContext::CreateGLShaderContext();
465 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000466
reed@android.com00dae862009-06-10 15:38:48 +0000467 Iter iter;
468 GM* gm;
reed@android.com00f883e2010-12-14 17:46:14 +0000469
470 if (readPath) {
471 fprintf(stderr, "reading from %s\n", readPath);
472 } else if (writePath) {
473 fprintf(stderr, "writing to %s\n", writePath);
474 }
475
reed@android.com00dae862009-06-10 15:38:48 +0000476 while ((gm = iter.next()) != NULL) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000477 SkISize size = gm->getISize();
reed@google.com3d3f0922010-12-20 21:10:29 +0000478 SkDebugf("drawing... %s [%d %d]\n", gm->shortName(),
reed@android.com8015dd82009-06-21 00:49:18 +0000479 size.width(), size.height());
480
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000481 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
482 test_drawing(gm, gRec[i],
483 writePath, readPath, diffPath, context);
reed@android.com00dae862009-06-10 15:38:48 +0000484
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000485 test_picture_playback(gm, gRec[i],
486 writePath, readPath, diffPath);
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000487
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000488 test_picture_serialization(gm, gRec[i],
489 writePath, readPath, diffPath);
490 }
reed@android.com00dae862009-06-10 15:38:48 +0000491 SkDELETE(gm);
492 }
493 return 0;
494}
reed@android.comdd0ac282009-06-20 02:38:16 +0000495
496///////////////////////////////////////////////////////////////////////////////
497
498using namespace skiagm;
499
500GM::GM() {}
501GM::~GM() {}
502
503void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000504 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000505}