blob: 37c3ee39f52fe5bf140d8777ef0d354990ccca3f [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.comea325432011-06-09 20:30:03 +0000123 const SkString& name, const char* renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000124 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.comea325432011-06-09 20:30:03 +0000137 SkDebugf(
138"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
139 renderModeDescriptor, name.c_str(),
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000140 base.width(), base.height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000141 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000142 }
143
144 SkAutoLockPixels bmLock(*bm);
145 SkAutoLockPixels baseLock(base);
146
147 for (int y = 0; y < h; y++) {
148 for (int x = 0; x < w; x++) {
149 SkPMColor c0 = *base.getAddr32(x, y);
150 SkPMColor c1 = *bm->getAddr32(x, y);
151 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000152 SkDebugf(
153"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
154 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000155
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000156 if (diff) {
157 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
158 diff->allocPixels();
159 compute_diff(*bm, base, diff);
160 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000161 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000162 }
163 }
164 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000165
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000166 // they're equal
167 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000168}
reed@android.com00dae862009-06-10 15:38:48 +0000169
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000170static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) {
171 SkFILEWStream stream(path.c_str());
172 return stream.write(pdf.getStream(), pdf.getOffset());
reed@google.com07700442010-12-20 19:46:07 +0000173}
174
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000175enum Backend {
176 kRaster_Backend,
177 kGPU_Backend,
178 kPDF_Backend,
179};
180
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000181struct ConfigData {
182 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000183 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000184 const char* fName;
185};
186
187/// Returns true if processing should continue, false to skip the
188/// remainder of this config for this GM.
189//@todo thudson 22 April 2011 - could refactor this to take in
190// a factory to generate the context, always call readPixels()
191// (logically a noop for rasters, if wasted time), and thus collapse the
192// GPU special case and also let this be used for SkPicture testing.
193static void setup_bitmap(const ConfigData& gRec, SkISize& size,
194 SkBitmap* bitmap) {
195 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
196 bitmap->allocPixels();
197 bitmap->eraseColor(0);
198}
199
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000200// Returns true if the test should continue, false if the test should
201// halt.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000202static bool generate_image(GM* gm, const ConfigData& gRec,
203 GrContext* context,
204 SkBitmap& bitmap) {
205 SkISize size (gm->getISize());
206 setup_bitmap(gRec, size, &bitmap);
207 SkCanvas canvas(bitmap);
208
209 if (gRec.fBackend == kRaster_Backend) {
210 gm->draw(&canvas);
211 } else { // GPU
212 if (NULL == context) {
213 return false;
214 }
215 SkGpuCanvas gc(context,
216 SkGpuDevice::Current3DApiRenderTarget());
217 gc.setDevice(gc.createDevice(bitmap.config(),
218 bitmap.width(),
219 bitmap.height(),
220 bitmap.isOpaque(),
221 false))->unref();
222 gm->draw(&gc);
223 gc.readPixels(&bitmap); // overwrite our previous allocation
224 }
225 return true;
226}
227
228static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
229 SkPicture* pict, SkBitmap* bitmap) {
230 SkISize size = gm->getISize();
231 setup_bitmap(gRec, size, bitmap);
232 SkCanvas canvas(*bitmap);
233 canvas.drawPicture(*pict);
234}
235
236static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
237#ifdef SK_SUPPORT_PDF
238 SkISize size = gm->getISize();
239 SkMatrix identity;
240 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000241 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000242 SkAutoUnref aur(dev);
243
244 SkCanvas c(dev);
245 gm->draw(&c);
246
247 SkPDFDocument doc;
248 doc.appendPage(dev);
249 doc.emitPDF(&pdf);
250#endif
251}
252
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000253static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000254 const char writePath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000255 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000256 const SkString& name,
257 SkBitmap& bitmap,
258 SkDynamicMemoryWStream* pdf) {
259 SkString path;
260 bool success = false;
261 if (gRec.fBackend != kPDF_Backend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000262 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000263 success = write_bitmap(path, bitmap);
264 } else if (pdf) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000265 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000266 success = write_pdf(path, *pdf);
267 }
268 if (!success) {
269 fprintf(stderr, "FAILED to write %s\n", path.c_str());
270 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000271 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000272}
273
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000274static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000275 const SkString& name,
276 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000277 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000278 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000279 SkString path = make_filename(readPath, "", name, "png");
280 SkBitmap orig;
281 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
282 SkBitmap::kARGB_8888_Config,
283 SkImageDecoder::kDecodePixels_Mode, NULL);
284 if (success) {
285 SkBitmap diffBitmap;
tomhudson@google.comea325432011-06-09 20:30:03 +0000286 success = compare(bitmap, orig, name, renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000287 diffPath ? &diffBitmap : NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000288 if (!success && diffPath) {
289 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
290 fprintf(stderr, "Writing %s\n", diffName.c_str());
291 write_bitmap(diffName, diffBitmap);
292 }
293 } else {
294 fprintf(stderr, "FAILED to read %s\n", path.c_str());
295 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000296 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000297}
298
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000299static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000300 const ConfigData& gRec,
301 const char writePath [],
302 const char readPath [],
303 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000304 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000305 SkBitmap& bitmap,
306 SkDynamicMemoryWStream* pdf) {
307 SkString name = make_name(gm->shortName(), gRec.fName);
308
309 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000310 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000311 name, bitmap, pdf);
312 // TODO: Figure out a way to compare PDFs.
313 } else if (readPath && gRec.fBackend != kPDF_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000314 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000315 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000316 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000317 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000318}
319
320static SkPicture* generate_new_picture(GM* gm) {
321 // Pictures are refcounted so must be on heap
322 SkPicture* pict = new SkPicture;
323 SkCanvas* cv = pict->beginRecording(1000, 1000);
324 gm->draw(cv);
325 pict->endRecording();
326
327 return pict;
328}
329
330static SkPicture* stream_to_new_picture(const SkPicture& src) {
331
332 // To do in-memory commiunications with a stream, we need to:
333 // * create a dynamic memory stream
334 // * copy it into a buffer
335 // * create a read stream from it
336 // ?!?!
337
338 SkDynamicMemoryWStream storage;
339 src.serialize(&storage);
340
341 int streamSize = storage.getOffset();
342 SkAutoMalloc dstStorage(streamSize);
343 void* dst = dstStorage.get();
344 //char* dst = new char [streamSize];
345 //@todo thudson 22 April 2011 when can we safely delete [] dst?
346 storage.copyTo(dst);
347 SkMemoryStream pictReadback(dst, streamSize);
348 SkPicture* retval = new SkPicture (&pictReadback);
349 return retval;
350}
351
352// Test: draw into a bitmap or pdf.
353// Depending on flags, possibly compare to an expected image
354// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000355static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000356 const ConfigData& gRec,
357 const char writePath [],
358 const char readPath [],
359 const char diffPath [],
360 GrContext* context) {
361 SkBitmap bitmap;
362 SkDynamicMemoryWStream pdf;
363
364 if (gRec.fBackend == kRaster_Backend ||
365 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000366 // Early exit if we can't generate the image, but this is
367 // expected in some cases, so don't report a test failure.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000368 if (!generate_image(gm, gRec, context, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000369 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000370 }
371 }
372 // TODO: Figure out a way to compare PDFs.
373 if (gRec.fBackend == kPDF_Backend && writePath) {
374 generate_pdf(gm, pdf);
375 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000376 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000377 "", bitmap, &pdf);
378}
379
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000380static bool test_picture_playback(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000381 const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000382 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.comea325432011-06-09 20:30:03 +0000390 return handle_test_results(gm, gRec, NULL, 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,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000398 const char readPath [],
399 const char diffPath []) {
400 SkPicture* pict = generate_new_picture(gm);
401 SkAutoUnref aurp(pict);
402 SkPicture* repict = stream_to_new_picture(*pict);
403 SkAutoUnref aurr(repict);
404
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000405 if (kRaster_Backend == gRec.fBackend) {
406 SkBitmap bitmap;
407 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000408 return handle_test_results(gm, gRec, NULL, readPath, diffPath,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000409 "-serialize", bitmap, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000410 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000411 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000412}
413
414static void usage(const char * argv0) {
415 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000416 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000417 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000418 SkDebugf(
419" readPath: directory to read reference images from;\n"
420" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000421 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000422 SkDebugf(" --replay: exercise SkPicture replay.\n");
423 SkDebugf(
424" --serialize: exercise SkPicture serialization & deserialization.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000425}
426
427static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000428 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
429 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
430 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
431 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
432#ifdef SK_SUPPORT_PDF
433 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
434#endif
reed@android.com00dae862009-06-10 15:38:48 +0000435};
436
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000437int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000438 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000439
reed@android.com8015dd82009-06-21 00:49:18 +0000440 const char* writePath = NULL; // if non-null, where we write the originals
441 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000442 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000443
reed@google.comb8b09832011-05-26 15:57:56 +0000444 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000445 bool doSerialize = false;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000446 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000447 char* const* stop = argv + argc;
448 for (++argv; argv < stop; ++argv) {
449 if (strcmp(*argv, "-w") == 0) {
450 argv++;
451 if (argv < stop && **argv) {
452 writePath = *argv;
453 }
454 } else if (strcmp(*argv, "-r") == 0) {
455 argv++;
456 if (argv < stop && **argv) {
457 readPath = *argv;
458 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000459 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000460 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000461 if (argv < stop && **argv) {
462 diffPath = *argv;
463 }
reed@google.comb8b09832011-05-26 15:57:56 +0000464 } else if (strcmp(*argv, "--noreplay") == 0) {
465 doReplay = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000466 } else if (strcmp(*argv, "--serialize") == 0) {
467 doSerialize = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000468 } else {
469 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000470 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000471 }
472 }
473 if (argv != stop) {
474 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000475 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000476 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000477
478 // setup a GL context for drawing offscreen
reed@google.com37df17d2010-12-23 20:20:51 +0000479 GrContext* context = NULL;
reed@google.com873cb1e2010-12-23 15:00:45 +0000480 SkEGLContext eglContext;
reed@google.com37df17d2010-12-23 20:20:51 +0000481 if (eglContext.init(1024, 1024)) {
482 context = GrContext::CreateGLShaderContext();
483 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000484
reed@android.com00dae862009-06-10 15:38:48 +0000485 Iter iter;
486 GM* gm;
reed@android.com00f883e2010-12-14 17:46:14 +0000487
488 if (readPath) {
489 fprintf(stderr, "reading from %s\n", readPath);
490 } else if (writePath) {
491 fprintf(stderr, "writing to %s\n", writePath);
492 }
493
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000494 // Accumulate success of all tests so we can flag error in any
495 // one with the return value.
tomhudson@google.comea325432011-06-09 20:30:03 +0000496 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000497 while ((gm = iter.next()) != NULL) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000498 SkISize size = gm->getISize();
reed@google.com3d3f0922010-12-20 21:10:29 +0000499 SkDebugf("drawing... %s [%d %d]\n", gm->shortName(),
reed@android.com8015dd82009-06-21 00:49:18 +0000500 size.width(), size.height());
501
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000502 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000503 bool testSuccess = test_drawing(gm, gRec[i],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000504 writePath, readPath, diffPath, context);
tomhudson@google.comea325432011-06-09 20:30:03 +0000505 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000506
tomhudson@google.comea325432011-06-09 20:30:03 +0000507 if (doReplay && testSuccess) {
508 testSuccess = test_picture_playback(gm, gRec[i],
509 readPath, diffPath);
510 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000511 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000512
tomhudson@google.comea325432011-06-09 20:30:03 +0000513 if (doSerialize && testSuccess) {
514 overallSuccess &= test_picture_serialization(gm, gRec[i],
515 readPath, diffPath);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000516 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000517 }
reed@android.com00dae862009-06-10 15:38:48 +0000518 SkDELETE(gm);
519 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000520 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000521 return -1;
522 }
reed@android.com00dae862009-06-10 15:38:48 +0000523 return 0;
524}
reed@android.comdd0ac282009-06-20 02:38:16 +0000525
526///////////////////////////////////////////////////////////////////////////////
527
528using namespace skiagm;
529
530GM::GM() {}
531GM::~GM() {}
532
533void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000534 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000535}