blob: bd9eba0224e0246eb470e9f697fe64486adf06a7 [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() {
bsalomon@google.com39149582011-06-13 21:55:32 +000029 this->reset();
30 }
31
32 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000033 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000034 }
reed@google.comd4dfd102011-01-18 21:05:42 +000035
reed@android.comdd0ac282009-06-20 02:38:16 +000036 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000037 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000038 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000039 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000040 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000041 }
42 return NULL;
43 }
reed@google.comd4dfd102011-01-18 21:05:42 +000044
reed@android.com00dae862009-06-10 15:38:48 +000045 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000046 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000047 int count = 0;
48 while (reg) {
49 count += 1;
50 reg = reg->next();
51 }
52 return count;
53 }
reed@google.comd4dfd102011-01-18 21:05:42 +000054
reed@android.com00dae862009-06-10 15:38:48 +000055private:
56 const GMRegistry* fReg;
57};
58
reed@android.com8015dd82009-06-21 00:49:18 +000059static SkString make_name(const char shortName[], const char configName[]) {
60 SkString name(shortName);
61 name.appendf("_%s", configName);
62 return name;
63}
64
tomhudson@google.com9875dd12011-04-25 15:49:53 +000065static SkString make_filename(const char path[],
66 const char pathSuffix[],
67 const SkString& name,
68 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +000069 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +000070 if (filename.endsWith("/")) {
71 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +000072 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +000073 filename.append(pathSuffix);
74 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +000075 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +000076 return filename;
77}
78
reed@android.comb9b9a182009-07-08 02:54:47 +000079/* since PNG insists on unpremultiplying our alpha, we take no precision chances
80 and force all pixels to be 100% opaque, otherwise on compare we may not get
81 a perfect match.
82 */
83static void force_all_opaque(const SkBitmap& bitmap) {
84 SkAutoLockPixels lock(bitmap);
85 for (int y = 0; y < bitmap.height(); y++) {
86 for (int x = 0; x < bitmap.width(); x++) {
87 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
88 }
89 }
90}
91
92static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
93 SkBitmap copy;
94 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
95 force_all_opaque(copy);
96 return SkImageEncoder::EncodeFile(path.c_str(), copy,
97 SkImageEncoder::kPNG_Type, 100);
98}
99
reed@google.com3d3f0922010-12-20 21:10:29 +0000100static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000101 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
102 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
103 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
104 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000105}
106
107static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000108 SkBitmap* diff) {
109 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000110
111 const int w = target.width();
112 const int h = target.height();
113 for (int y = 0; y < h; y++) {
114 for (int x = 0; x < w; x++) {
115 SkPMColor c0 = *base.getAddr32(x, y);
116 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000117 SkPMColor d = 0;
118 if (c0 != c1) {
119 d = compute_diff_pmcolor(c0, c1);
120 }
121 *diff->getAddr32(x, y) = d;
122 }
123 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000124}
125
126static bool compare(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.comea325432011-06-09 20:30:03 +0000127 const SkString& name, const char* renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000128 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000129 SkBitmap copy;
130 const SkBitmap* bm = &target;
131 if (target.config() != SkBitmap::kARGB_8888_Config) {
132 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
133 bm = &copy;
134 }
135
136 force_all_opaque(*bm);
137
138 const int w = bm->width();
139 const int h = bm->height();
140 if (w != base.width() || h != base.height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000141 SkDebugf(
142"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
143 renderModeDescriptor, name.c_str(),
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000144 base.width(), base.height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000145 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000146 }
147
148 SkAutoLockPixels bmLock(*bm);
149 SkAutoLockPixels baseLock(base);
150
151 for (int y = 0; y < h; y++) {
152 for (int x = 0; x < w; x++) {
153 SkPMColor c0 = *base.getAddr32(x, y);
154 SkPMColor c1 = *bm->getAddr32(x, y);
155 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000156 SkDebugf(
157"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
158 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000159
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000160 if (diff) {
161 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
162 diff->allocPixels();
163 compute_diff(*bm, base, diff);
164 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000165 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000166 }
167 }
168 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000169
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000170 // they're equal
171 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000172}
reed@android.com00dae862009-06-10 15:38:48 +0000173
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000174static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) {
175 SkFILEWStream stream(path.c_str());
176 return stream.write(pdf.getStream(), pdf.getOffset());
reed@google.com07700442010-12-20 19:46:07 +0000177}
178
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000179enum Backend {
180 kRaster_Backend,
181 kGPU_Backend,
182 kPDF_Backend,
183};
184
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000185struct ConfigData {
186 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000187 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000188 const char* fName;
189};
190
191/// Returns true if processing should continue, false to skip the
192/// remainder of this config for this GM.
193//@todo thudson 22 April 2011 - could refactor this to take in
194// a factory to generate the context, always call readPixels()
195// (logically a noop for rasters, if wasted time), and thus collapse the
196// GPU special case and also let this be used for SkPicture testing.
197static void setup_bitmap(const ConfigData& gRec, SkISize& size,
198 SkBitmap* bitmap) {
199 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
200 bitmap->allocPixels();
201 bitmap->eraseColor(0);
202}
203
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000204// Returns true if the test should continue, false if the test should
205// halt.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000206static bool generate_image(GM* gm, const ConfigData& gRec,
207 GrContext* context,
208 SkBitmap& bitmap) {
209 SkISize size (gm->getISize());
210 setup_bitmap(gRec, size, &bitmap);
211 SkCanvas canvas(bitmap);
212
213 if (gRec.fBackend == kRaster_Backend) {
214 gm->draw(&canvas);
215 } else { // GPU
216 if (NULL == context) {
217 return false;
218 }
219 SkGpuCanvas gc(context,
220 SkGpuDevice::Current3DApiRenderTarget());
221 gc.setDevice(gc.createDevice(bitmap.config(),
222 bitmap.width(),
223 bitmap.height(),
224 bitmap.isOpaque(),
225 false))->unref();
226 gm->draw(&gc);
227 gc.readPixels(&bitmap); // overwrite our previous allocation
228 }
229 return true;
230}
231
232static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
233 SkPicture* pict, SkBitmap* bitmap) {
234 SkISize size = gm->getISize();
235 setup_bitmap(gRec, size, bitmap);
236 SkCanvas canvas(*bitmap);
237 canvas.drawPicture(*pict);
238}
239
240static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
241#ifdef SK_SUPPORT_PDF
242 SkISize size = gm->getISize();
243 SkMatrix identity;
244 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000245 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000246 SkAutoUnref aur(dev);
247
248 SkCanvas c(dev);
249 gm->draw(&c);
250
251 SkPDFDocument doc;
252 doc.appendPage(dev);
253 doc.emitPDF(&pdf);
254#endif
255}
256
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000257static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000258 const char writePath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000259 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000260 const SkString& name,
261 SkBitmap& bitmap,
262 SkDynamicMemoryWStream* pdf) {
263 SkString path;
264 bool success = false;
265 if (gRec.fBackend != kPDF_Backend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000266 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000267 success = write_bitmap(path, bitmap);
268 } else if (pdf) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000269 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000270 success = write_pdf(path, *pdf);
271 }
272 if (!success) {
273 fprintf(stderr, "FAILED to write %s\n", path.c_str());
274 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000275 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000276}
277
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000278static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000279 const SkString& name,
280 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000281 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000282 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000283 SkString path = make_filename(readPath, "", name, "png");
284 SkBitmap orig;
285 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
286 SkBitmap::kARGB_8888_Config,
287 SkImageDecoder::kDecodePixels_Mode, NULL);
288 if (success) {
289 SkBitmap diffBitmap;
tomhudson@google.comea325432011-06-09 20:30:03 +0000290 success = compare(bitmap, orig, name, renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000291 diffPath ? &diffBitmap : NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000292 if (!success && diffPath) {
293 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
294 fprintf(stderr, "Writing %s\n", diffName.c_str());
295 write_bitmap(diffName, diffBitmap);
296 }
297 } else {
298 fprintf(stderr, "FAILED to read %s\n", path.c_str());
299 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000300 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000301}
302
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000303static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000304 const ConfigData& gRec,
305 const char writePath [],
306 const char readPath [],
307 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000308 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000309 SkBitmap& bitmap,
310 SkDynamicMemoryWStream* pdf) {
311 SkString name = make_name(gm->shortName(), gRec.fName);
312
313 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000314 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000315 name, bitmap, pdf);
316 // TODO: Figure out a way to compare PDFs.
317 } else if (readPath && gRec.fBackend != kPDF_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000318 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000319 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000320 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000321 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000322}
323
324static SkPicture* generate_new_picture(GM* gm) {
325 // Pictures are refcounted so must be on heap
326 SkPicture* pict = new SkPicture;
327 SkCanvas* cv = pict->beginRecording(1000, 1000);
328 gm->draw(cv);
329 pict->endRecording();
330
331 return pict;
332}
333
334static SkPicture* stream_to_new_picture(const SkPicture& src) {
335
336 // To do in-memory commiunications with a stream, we need to:
337 // * create a dynamic memory stream
338 // * copy it into a buffer
339 // * create a read stream from it
340 // ?!?!
341
342 SkDynamicMemoryWStream storage;
343 src.serialize(&storage);
344
345 int streamSize = storage.getOffset();
346 SkAutoMalloc dstStorage(streamSize);
347 void* dst = dstStorage.get();
348 //char* dst = new char [streamSize];
349 //@todo thudson 22 April 2011 when can we safely delete [] dst?
350 storage.copyTo(dst);
351 SkMemoryStream pictReadback(dst, streamSize);
352 SkPicture* retval = new SkPicture (&pictReadback);
353 return retval;
354}
355
356// Test: draw into a bitmap or pdf.
357// Depending on flags, possibly compare to an expected image
358// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000359static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000360 const ConfigData& gRec,
361 const char writePath [],
362 const char readPath [],
363 const char diffPath [],
364 GrContext* context) {
365 SkBitmap bitmap;
366 SkDynamicMemoryWStream pdf;
367
368 if (gRec.fBackend == kRaster_Backend ||
369 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000370 // Early exit if we can't generate the image, but this is
371 // expected in some cases, so don't report a test failure.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000372 if (!generate_image(gm, gRec, context, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000373 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000374 }
375 }
376 // TODO: Figure out a way to compare PDFs.
377 if (gRec.fBackend == kPDF_Backend && writePath) {
378 generate_pdf(gm, pdf);
379 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000380 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000381 "", bitmap, &pdf);
382}
383
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000384static bool test_picture_playback(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000385 const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000386 const char readPath [],
387 const char diffPath []) {
388 SkPicture* pict = generate_new_picture(gm);
389 SkAutoUnref aur(pict);
390
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000391 if (kRaster_Backend == gRec.fBackend) {
392 SkBitmap bitmap;
393 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000394 return handle_test_results(gm, gRec, NULL, readPath, diffPath,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000395 "-replay", bitmap, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000396 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000397 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000398}
399
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000400static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000401 const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000402 const char readPath [],
403 const char diffPath []) {
404 SkPicture* pict = generate_new_picture(gm);
405 SkAutoUnref aurp(pict);
406 SkPicture* repict = stream_to_new_picture(*pict);
407 SkAutoUnref aurr(repict);
408
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000409 if (kRaster_Backend == gRec.fBackend) {
410 SkBitmap bitmap;
411 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000412 return handle_test_results(gm, gRec, NULL, readPath, diffPath,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000413 "-serialize", bitmap, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000414 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000415 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000416}
417
418static void usage(const char * argv0) {
419 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000420 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000421 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000422 SkDebugf(
423" readPath: directory to read reference images from;\n"
424" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000425 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000426 SkDebugf(" --replay: exercise SkPicture replay.\n");
427 SkDebugf(
428" --serialize: exercise SkPicture serialization & deserialization.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000429}
430
431static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000432 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
433 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
434 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
435 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
436#ifdef SK_SUPPORT_PDF
437 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
438#endif
reed@android.com00dae862009-06-10 15:38:48 +0000439};
440
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000441int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000442 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000443
reed@android.com8015dd82009-06-21 00:49:18 +0000444 const char* writePath = NULL; // if non-null, where we write the originals
445 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000446 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000447
reed@google.comb8b09832011-05-26 15:57:56 +0000448 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000449 bool doSerialize = false;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000450 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000451 char* const* stop = argv + argc;
452 for (++argv; argv < stop; ++argv) {
453 if (strcmp(*argv, "-w") == 0) {
454 argv++;
455 if (argv < stop && **argv) {
456 writePath = *argv;
457 }
458 } else if (strcmp(*argv, "-r") == 0) {
459 argv++;
460 if (argv < stop && **argv) {
461 readPath = *argv;
462 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000463 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000464 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000465 if (argv < stop && **argv) {
466 diffPath = *argv;
467 }
reed@google.comb8b09832011-05-26 15:57:56 +0000468 } else if (strcmp(*argv, "--noreplay") == 0) {
469 doReplay = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000470 } else if (strcmp(*argv, "--serialize") == 0) {
471 doSerialize = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000472 } else {
473 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000474 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000475 }
476 }
477 if (argv != stop) {
478 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000479 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000480 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000481
bsalomon@google.com39149582011-06-13 21:55:32 +0000482 int maxW = -1;
483 int maxH = -1;
484 Iter iter;
485 GM* gm;
486 while ((gm = iter.next()) != NULL) {
487 SkISize size = gm->getISize();
488 maxW = SkMax32(size.width(), maxW);
489 maxH = SkMax32(size.height(), maxH);
490 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000491 // setup a GL context for drawing offscreen
reed@google.com37df17d2010-12-23 20:20:51 +0000492 GrContext* context = NULL;
reed@google.com873cb1e2010-12-23 15:00:45 +0000493 SkEGLContext eglContext;
bsalomon@google.com39149582011-06-13 21:55:32 +0000494 if (eglContext.init(maxW, maxH)) {
reed@google.com37df17d2010-12-23 20:20:51 +0000495 context = GrContext::CreateGLShaderContext();
496 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000497
reed@android.com00f883e2010-12-14 17:46:14 +0000498
499 if (readPath) {
500 fprintf(stderr, "reading from %s\n", readPath);
501 } else if (writePath) {
502 fprintf(stderr, "writing to %s\n", writePath);
503 }
504
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000505 // Accumulate success of all tests so we can flag error in any
506 // one with the return value.
bsalomon@google.com39149582011-06-13 21:55:32 +0000507 iter.reset();
tomhudson@google.comea325432011-06-09 20:30:03 +0000508 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000509 while ((gm = iter.next()) != NULL) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000510 SkISize size = gm->getISize();
reed@google.com3d3f0922010-12-20 21:10:29 +0000511 SkDebugf("drawing... %s [%d %d]\n", gm->shortName(),
reed@android.com8015dd82009-06-21 00:49:18 +0000512 size.width(), size.height());
513
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000514 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000515 bool testSuccess = test_drawing(gm, gRec[i],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000516 writePath, readPath, diffPath, context);
tomhudson@google.comea325432011-06-09 20:30:03 +0000517 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000518
tomhudson@google.comea325432011-06-09 20:30:03 +0000519 if (doReplay && testSuccess) {
520 testSuccess = test_picture_playback(gm, gRec[i],
521 readPath, diffPath);
522 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000523 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000524
tomhudson@google.comea325432011-06-09 20:30:03 +0000525 if (doSerialize && testSuccess) {
526 overallSuccess &= test_picture_serialization(gm, gRec[i],
527 readPath, diffPath);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000528 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000529 }
reed@android.com00dae862009-06-10 15:38:48 +0000530 SkDELETE(gm);
531 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000532 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000533 return -1;
534 }
reed@android.com00dae862009-06-10 15:38:48 +0000535 return 0;
536}
reed@android.comdd0ac282009-06-20 02:38:16 +0000537
538///////////////////////////////////////////////////////////////////////////////
539
540using namespace skiagm;
541
542GM::GM() {}
543GM::~GM() {}
544
545void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000546 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000547}