blob: b59182c2989e32e1e6734c638f50c999402813c1 [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,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000123 const SkString& name, const char* modeDescriptor,
124 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000125 SkBitmap copy;
126 const SkBitmap* bm = &target;
127 if (target.config() != SkBitmap::kARGB_8888_Config) {
128 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
129 bm = &copy;
130 }
131
132 force_all_opaque(*bm);
133
134 const int w = bm->width();
135 const int h = bm->height();
136 if (w != base.width() || h != base.height()) {
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000137 SkDebugf("---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
138 modeDescriptor, name.c_str(),
139 base.width(), base.height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000140 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000141 }
142
143 SkAutoLockPixels bmLock(*bm);
144 SkAutoLockPixels baseLock(base);
145
146 for (int y = 0; y < h; y++) {
147 for (int x = 0; x < w; x++) {
148 SkPMColor c0 = *base.getAddr32(x, y);
149 SkPMColor c1 = *bm->getAddr32(x, y);
150 if (c0 != c1) {
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000151 SkDebugf("----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
152 modeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000153
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000154 if (diff) {
155 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
156 diff->allocPixels();
157 compute_diff(*bm, base, diff);
158 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000159 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000160 }
161 }
162 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000163
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000164 // they're equal
165 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000166}
reed@android.com00dae862009-06-10 15:38:48 +0000167
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000168static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) {
169 SkFILEWStream stream(path.c_str());
170 return stream.write(pdf.getStream(), pdf.getOffset());
reed@google.com07700442010-12-20 19:46:07 +0000171}
172
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000173enum Backend {
174 kRaster_Backend,
175 kGPU_Backend,
176 kPDF_Backend,
177};
178
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000179struct ConfigData {
180 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000181 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000182 const char* fName;
183};
184
185/// Returns true if processing should continue, false to skip the
186/// remainder of this config for this GM.
187//@todo thudson 22 April 2011 - could refactor this to take in
188// a factory to generate the context, always call readPixels()
189// (logically a noop for rasters, if wasted time), and thus collapse the
190// GPU special case and also let this be used for SkPicture testing.
191static void setup_bitmap(const ConfigData& gRec, SkISize& size,
192 SkBitmap* bitmap) {
193 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
194 bitmap->allocPixels();
195 bitmap->eraseColor(0);
196}
197
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000198// Returns true if the test should continue, false if the test should
199// halt.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000200static bool generate_image(GM* gm, const ConfigData& gRec,
201 GrContext* context,
202 SkBitmap& bitmap) {
203 SkISize size (gm->getISize());
204 setup_bitmap(gRec, size, &bitmap);
205 SkCanvas canvas(bitmap);
206
207 if (gRec.fBackend == kRaster_Backend) {
208 gm->draw(&canvas);
209 } else { // GPU
210 if (NULL == context) {
211 return false;
212 }
213 SkGpuCanvas gc(context,
214 SkGpuDevice::Current3DApiRenderTarget());
215 gc.setDevice(gc.createDevice(bitmap.config(),
216 bitmap.width(),
217 bitmap.height(),
218 bitmap.isOpaque(),
219 false))->unref();
220 gm->draw(&gc);
221 gc.readPixels(&bitmap); // overwrite our previous allocation
222 }
223 return true;
224}
225
226static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
227 SkPicture* pict, SkBitmap* bitmap) {
228 SkISize size = gm->getISize();
229 setup_bitmap(gRec, size, bitmap);
230 SkCanvas canvas(*bitmap);
231 canvas.drawPicture(*pict);
232}
233
234static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
235#ifdef SK_SUPPORT_PDF
236 SkISize size = gm->getISize();
237 SkMatrix identity;
238 identity.reset();
239 SkPDFDevice* dev = new SkPDFDevice(size.width(), size.height(),
240 identity);
241 SkAutoUnref aur(dev);
242
243 SkCanvas c(dev);
244 gm->draw(&c);
245
246 SkPDFDocument doc;
247 doc.appendPage(dev);
248 doc.emitPDF(&pdf);
249#endif
250}
251
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000252static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000253 const char writePath [],
254 const char writePathSuffix [],
255 const SkString& name,
256 SkBitmap& bitmap,
257 SkDynamicMemoryWStream* pdf) {
258 SkString path;
259 bool success = false;
260 if (gRec.fBackend != kPDF_Backend) {
261 path = make_filename(writePath, writePathSuffix, name, "png");
262 success = write_bitmap(path, bitmap);
263 } else if (pdf) {
264 path = make_filename(writePath, writePathSuffix, name, "pdf");
265 success = write_pdf(path, *pdf);
266 }
267 if (!success) {
268 fprintf(stderr, "FAILED to write %s\n", path.c_str());
269 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000270 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000271}
272
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000273static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000274 const SkString& name,
275 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000276 const char diffPath [],
277 const char modeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000278 SkString path = make_filename(readPath, "", name, "png");
279 SkBitmap orig;
280 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
281 SkBitmap::kARGB_8888_Config,
282 SkImageDecoder::kDecodePixels_Mode, NULL);
283 if (success) {
284 SkBitmap diffBitmap;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000285 success = compare(bitmap, orig, name, modeDescriptor,
286 diffPath ? &diffBitmap : NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000287 if (!success && diffPath) {
288 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
289 fprintf(stderr, "Writing %s\n", diffName.c_str());
290 write_bitmap(diffName, diffBitmap);
291 }
292 } else {
293 fprintf(stderr, "FAILED to read %s\n", path.c_str());
294 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000295 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000296}
297
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000298static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000299 const ConfigData& gRec,
300 const char writePath [],
301 const char readPath [],
302 const char diffPath [],
303 const char writePathSuffix [],
304 SkBitmap& bitmap,
305 SkDynamicMemoryWStream* pdf) {
306 SkString name = make_name(gm->shortName(), gRec.fName);
307
308 if (writePath) {
309 write_reference_image(gRec, writePath, writePathSuffix,
310 name, bitmap, pdf);
311 // TODO: Figure out a way to compare PDFs.
312 } else if (readPath && gRec.fBackend != kPDF_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000313 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000314 diffPath, writePathSuffix);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000315 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000316 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000317}
318
319static SkPicture* generate_new_picture(GM* gm) {
320 // Pictures are refcounted so must be on heap
321 SkPicture* pict = new SkPicture;
322 SkCanvas* cv = pict->beginRecording(1000, 1000);
323 gm->draw(cv);
324 pict->endRecording();
325
326 return pict;
327}
328
329static SkPicture* stream_to_new_picture(const SkPicture& src) {
330
331 // To do in-memory commiunications with a stream, we need to:
332 // * create a dynamic memory stream
333 // * copy it into a buffer
334 // * create a read stream from it
335 // ?!?!
336
337 SkDynamicMemoryWStream storage;
338 src.serialize(&storage);
339
340 int streamSize = storage.getOffset();
341 SkAutoMalloc dstStorage(streamSize);
342 void* dst = dstStorage.get();
343 //char* dst = new char [streamSize];
344 //@todo thudson 22 April 2011 when can we safely delete [] dst?
345 storage.copyTo(dst);
346 SkMemoryStream pictReadback(dst, streamSize);
347 SkPicture* retval = new SkPicture (&pictReadback);
348 return retval;
349}
350
351// Test: draw into a bitmap or pdf.
352// Depending on flags, possibly compare to an expected image
353// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000354static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000355 const ConfigData& gRec,
356 const char writePath [],
357 const char readPath [],
358 const char diffPath [],
359 GrContext* context) {
360 SkBitmap bitmap;
361 SkDynamicMemoryWStream pdf;
362
363 if (gRec.fBackend == kRaster_Backend ||
364 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000365 // Early exit if we can't generate the image, but this is
366 // expected in some cases, so don't report a test failure.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000367 if (!generate_image(gm, gRec, context, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000368 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000369 }
370 }
371 // TODO: Figure out a way to compare PDFs.
372 if (gRec.fBackend == kPDF_Backend && writePath) {
373 generate_pdf(gm, pdf);
374 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000375 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000376 "", bitmap, &pdf);
377}
378
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000379static bool test_picture_playback(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000380 const ConfigData& gRec,
381 const char writePath [],
382 const char readPath [],
383 const char diffPath []) {
384 SkPicture* pict = generate_new_picture(gm);
385 SkAutoUnref aur(pict);
386
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000387 if (kRaster_Backend == gRec.fBackend) {
388 SkBitmap bitmap;
389 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000390 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000391 "-replay", bitmap, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000392 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000393 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000394}
395
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000396static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000397 const ConfigData& gRec,
398 const char writePath [],
399 const char readPath [],
400 const char diffPath []) {
401 SkPicture* pict = generate_new_picture(gm);
402 SkAutoUnref aurp(pict);
403 SkPicture* repict = stream_to_new_picture(*pict);
404 SkAutoUnref aurr(repict);
405
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000406 if (kRaster_Backend == gRec.fBackend) {
407 SkBitmap bitmap;
408 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000409 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000410 "-serialize", bitmap, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000411 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000412 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000413}
414
415static void usage(const char * argv0) {
416 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000417 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000418 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000419 SkDebugf(
420" readPath: directory to read reference images from;\n"
421" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000422 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000423 SkDebugf(" --replay: exercise SkPicture replay.\n");
424 SkDebugf(
425" --serialize: exercise SkPicture serialization & deserialization.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000426}
427
428static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000429 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
430 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
431 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
432 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
433#ifdef SK_SUPPORT_PDF
434 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
435#endif
reed@android.com00dae862009-06-10 15:38:48 +0000436};
437
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000438int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000439 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000440
reed@android.com8015dd82009-06-21 00:49:18 +0000441 const char* writePath = NULL; // if non-null, where we write the originals
442 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000443 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000444
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000445 bool doReplay = false;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000446 bool doSerialize = false;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000447 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000448 char* const* stop = argv + argc;
449 for (++argv; argv < stop; ++argv) {
450 if (strcmp(*argv, "-w") == 0) {
451 argv++;
452 if (argv < stop && **argv) {
453 writePath = *argv;
454 }
455 } else if (strcmp(*argv, "-r") == 0) {
456 argv++;
457 if (argv < stop && **argv) {
458 readPath = *argv;
459 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000460 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000461 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000462 if (argv < stop && **argv) {
463 diffPath = *argv;
464 }
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000465 } else if (strcmp(*argv, "--replay") == 0) {
466 doReplay = true;
467 } else if (strcmp(*argv, "--serialize") == 0) {
468 doSerialize = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000469 } else {
470 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000471 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000472 }
473 }
474 if (argv != stop) {
475 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000476 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000477 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000478
479 // setup a GL context for drawing offscreen
reed@google.com37df17d2010-12-23 20:20:51 +0000480 GrContext* context = NULL;
reed@google.com873cb1e2010-12-23 15:00:45 +0000481 SkEGLContext eglContext;
reed@google.com37df17d2010-12-23 20:20:51 +0000482 if (eglContext.init(1024, 1024)) {
483 context = GrContext::CreateGLShaderContext();
484 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000485
reed@android.com00dae862009-06-10 15:38:48 +0000486 Iter iter;
487 GM* gm;
reed@android.com00f883e2010-12-14 17:46:14 +0000488
489 if (readPath) {
490 fprintf(stderr, "reading from %s\n", readPath);
491 } else if (writePath) {
492 fprintf(stderr, "writing to %s\n", writePath);
493 }
494
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000495 // Accumulate success of all tests so we can flag error in any
496 // one with the return value.
497 bool testSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000498 while ((gm = iter.next()) != NULL) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000499 SkISize size = gm->getISize();
reed@google.com3d3f0922010-12-20 21:10:29 +0000500 SkDebugf("drawing... %s [%d %d]\n", gm->shortName(),
reed@android.com8015dd82009-06-21 00:49:18 +0000501 size.width(), size.height());
502
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000503 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000504 testSuccess &= test_drawing(gm, gRec[i],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000505 writePath, readPath, diffPath, context);
reed@android.com00dae862009-06-10 15:38:48 +0000506
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000507 if (doReplay) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000508 testSuccess &= test_picture_playback(gm, gRec[i],
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000509 writePath, readPath, diffPath);
510 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000511
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000512 if (doSerialize) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000513 testSuccess &= test_picture_serialization(gm, gRec[i],
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000514 writePath, readPath, diffPath);
515 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000516 }
reed@android.com00dae862009-06-10 15:38:48 +0000517 SkDELETE(gm);
518 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000519 if (false == testSuccess) {
520 return -1;
521 }
reed@android.com00dae862009-06-10 15:38:48 +0000522 return 0;
523}
reed@android.comdd0ac282009-06-20 02:38:16 +0000524
525///////////////////////////////////////////////////////////////////////////////
526
527using namespace skiagm;
528
529GM::GM() {}
530GM::~GM() {}
531
532void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000533 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000534}