blob: a90e0a88f5d2746e36d0aee7bc0a5e68359357dd [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
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000024template GMRegistry* SkTRegistry<GM*, void*>::gHead;
reed@android.com00dae862009-06-10 15:38:48 +000025
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 }
reed@google.comaf951c92011-06-16 19:10:39 +0000219
220 // not a real object, so don't unref it
221 GrRenderTarget* rt = SkGpuDevice::Current3DApiRenderTarget();
222 SkGpuCanvas gc(context, rt);
223 gc.setDevice(new SkGpuDevice(context, rt))->unref();
224
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000225 gm->draw(&gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000226 // the device is as large as the current rendertarget, so we explicitly
227 // only readback the amount we expect (in size)
228 gc.readPixels(SkIRect::MakeSize(size), &bitmap); // overwrite our previous allocation
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000229 }
230 return true;
231}
232
233static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
234 SkPicture* pict, SkBitmap* bitmap) {
235 SkISize size = gm->getISize();
236 setup_bitmap(gRec, size, bitmap);
237 SkCanvas canvas(*bitmap);
238 canvas.drawPicture(*pict);
239}
240
241static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
242#ifdef SK_SUPPORT_PDF
243 SkISize size = gm->getISize();
244 SkMatrix identity;
245 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000246 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000247 SkAutoUnref aur(dev);
248
249 SkCanvas c(dev);
250 gm->draw(&c);
251
252 SkPDFDocument doc;
253 doc.appendPage(dev);
254 doc.emitPDF(&pdf);
255#endif
256}
257
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000258static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000259 const char writePath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000260 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000261 const SkString& name,
262 SkBitmap& bitmap,
263 SkDynamicMemoryWStream* pdf) {
264 SkString path;
265 bool success = false;
266 if (gRec.fBackend != kPDF_Backend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000267 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000268 success = write_bitmap(path, bitmap);
269 } else if (pdf) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000270 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000271 success = write_pdf(path, *pdf);
272 }
273 if (!success) {
274 fprintf(stderr, "FAILED to write %s\n", path.c_str());
275 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000276 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000277}
278
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000279static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000280 const SkString& name,
281 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000282 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000283 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000284 SkString path = make_filename(readPath, "", name, "png");
285 SkBitmap orig;
286 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
287 SkBitmap::kARGB_8888_Config,
288 SkImageDecoder::kDecodePixels_Mode, NULL);
289 if (success) {
290 SkBitmap diffBitmap;
tomhudson@google.comea325432011-06-09 20:30:03 +0000291 success = compare(bitmap, orig, name, renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000292 diffPath ? &diffBitmap : NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000293 if (!success && diffPath) {
294 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
295 fprintf(stderr, "Writing %s\n", diffName.c_str());
296 write_bitmap(diffName, diffBitmap);
297 }
298 } else {
299 fprintf(stderr, "FAILED to read %s\n", path.c_str());
300 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000301 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000302}
303
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000304static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000305 const ConfigData& gRec,
306 const char writePath [],
307 const char readPath [],
308 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000309 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000310 SkBitmap& bitmap,
311 SkDynamicMemoryWStream* pdf) {
312 SkString name = make_name(gm->shortName(), gRec.fName);
313
314 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000315 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000316 name, bitmap, pdf);
317 // TODO: Figure out a way to compare PDFs.
318 } else if (readPath && gRec.fBackend != kPDF_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000319 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000320 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000321 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000322 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000323}
324
325static SkPicture* generate_new_picture(GM* gm) {
326 // Pictures are refcounted so must be on heap
327 SkPicture* pict = new SkPicture;
328 SkCanvas* cv = pict->beginRecording(1000, 1000);
329 gm->draw(cv);
330 pict->endRecording();
331
332 return pict;
333}
334
335static SkPicture* stream_to_new_picture(const SkPicture& src) {
336
337 // To do in-memory commiunications with a stream, we need to:
338 // * create a dynamic memory stream
339 // * copy it into a buffer
340 // * create a read stream from it
341 // ?!?!
342
343 SkDynamicMemoryWStream storage;
344 src.serialize(&storage);
345
346 int streamSize = storage.getOffset();
347 SkAutoMalloc dstStorage(streamSize);
348 void* dst = dstStorage.get();
349 //char* dst = new char [streamSize];
350 //@todo thudson 22 April 2011 when can we safely delete [] dst?
351 storage.copyTo(dst);
352 SkMemoryStream pictReadback(dst, streamSize);
353 SkPicture* retval = new SkPicture (&pictReadback);
354 return retval;
355}
356
357// Test: draw into a bitmap or pdf.
358// Depending on flags, possibly compare to an expected image
359// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000360static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000361 const ConfigData& gRec,
362 const char writePath [],
363 const char readPath [],
364 const char diffPath [],
365 GrContext* context) {
366 SkBitmap bitmap;
367 SkDynamicMemoryWStream pdf;
368
369 if (gRec.fBackend == kRaster_Backend ||
370 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000371 // Early exit if we can't generate the image, but this is
372 // expected in some cases, so don't report a test failure.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000373 if (!generate_image(gm, gRec, context, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000374 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000375 }
376 }
377 // TODO: Figure out a way to compare PDFs.
378 if (gRec.fBackend == kPDF_Backend && writePath) {
379 generate_pdf(gm, pdf);
380 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000381 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000382 "", bitmap, &pdf);
383}
384
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000385static bool test_picture_playback(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000386 const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000387 const char readPath [],
388 const char diffPath []) {
389 SkPicture* pict = generate_new_picture(gm);
390 SkAutoUnref aur(pict);
391
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000392 if (kRaster_Backend == gRec.fBackend) {
393 SkBitmap bitmap;
394 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000395 return handle_test_results(gm, gRec, NULL, readPath, diffPath,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000396 "-replay", bitmap, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000397 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000398 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000399}
400
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000401static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000402 const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000403 const char readPath [],
404 const char diffPath []) {
405 SkPicture* pict = generate_new_picture(gm);
406 SkAutoUnref aurp(pict);
407 SkPicture* repict = stream_to_new_picture(*pict);
408 SkAutoUnref aurr(repict);
409
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000410 if (kRaster_Backend == gRec.fBackend) {
411 SkBitmap bitmap;
412 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000413 return handle_test_results(gm, gRec, NULL, readPath, diffPath,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000414 "-serialize", bitmap, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000415 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000416 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000417}
418
419static void usage(const char * argv0) {
420 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000421 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000422 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000423 SkDebugf(
424" readPath: directory to read reference images from;\n"
425" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000426 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000427 SkDebugf(" --replay: exercise SkPicture replay.\n");
428 SkDebugf(
429" --serialize: exercise SkPicture serialization & deserialization.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000430}
431
432static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000433 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
434 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
435 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
436 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
437#ifdef SK_SUPPORT_PDF
438 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
439#endif
reed@android.com00dae862009-06-10 15:38:48 +0000440};
441
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000442int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000443 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000444
reed@android.com8015dd82009-06-21 00:49:18 +0000445 const char* writePath = NULL; // if non-null, where we write the originals
446 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000447 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000448
reed@google.comb8b09832011-05-26 15:57:56 +0000449 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000450 bool doSerialize = false;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000451 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000452 char* const* stop = argv + argc;
453 for (++argv; argv < stop; ++argv) {
454 if (strcmp(*argv, "-w") == 0) {
455 argv++;
456 if (argv < stop && **argv) {
457 writePath = *argv;
458 }
459 } else if (strcmp(*argv, "-r") == 0) {
460 argv++;
461 if (argv < stop && **argv) {
462 readPath = *argv;
463 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000464 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000465 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000466 if (argv < stop && **argv) {
467 diffPath = *argv;
468 }
reed@google.comb8b09832011-05-26 15:57:56 +0000469 } else if (strcmp(*argv, "--noreplay") == 0) {
470 doReplay = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000471 } else if (strcmp(*argv, "--serialize") == 0) {
472 doSerialize = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000473 } else {
474 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000475 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000476 }
477 }
478 if (argv != stop) {
479 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000480 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000481 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000482
bsalomon@google.com39149582011-06-13 21:55:32 +0000483 int maxW = -1;
484 int maxH = -1;
485 Iter iter;
486 GM* gm;
487 while ((gm = iter.next()) != NULL) {
488 SkISize size = gm->getISize();
489 maxW = SkMax32(size.width(), maxW);
490 maxH = SkMax32(size.height(), maxH);
491 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000492 // setup a GL context for drawing offscreen
reed@google.com37df17d2010-12-23 20:20:51 +0000493 GrContext* context = NULL;
reed@google.com873cb1e2010-12-23 15:00:45 +0000494 SkEGLContext eglContext;
bsalomon@google.com39149582011-06-13 21:55:32 +0000495 if (eglContext.init(maxW, maxH)) {
reed@google.com37df17d2010-12-23 20:20:51 +0000496 context = GrContext::CreateGLShaderContext();
497 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000498
reed@android.com00f883e2010-12-14 17:46:14 +0000499
500 if (readPath) {
501 fprintf(stderr, "reading from %s\n", readPath);
502 } else if (writePath) {
503 fprintf(stderr, "writing to %s\n", writePath);
504 }
505
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000506 // Accumulate success of all tests so we can flag error in any
507 // one with the return value.
bsalomon@google.com39149582011-06-13 21:55:32 +0000508 iter.reset();
tomhudson@google.comea325432011-06-09 20:30:03 +0000509 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000510 while ((gm = iter.next()) != NULL) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000511 SkISize size = gm->getISize();
reed@google.com3d3f0922010-12-20 21:10:29 +0000512 SkDebugf("drawing... %s [%d %d]\n", gm->shortName(),
reed@android.com8015dd82009-06-21 00:49:18 +0000513 size.width(), size.height());
514
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000515 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000516 bool testSuccess = test_drawing(gm, gRec[i],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000517 writePath, readPath, diffPath, context);
tomhudson@google.comea325432011-06-09 20:30:03 +0000518 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000519
tomhudson@google.comea325432011-06-09 20:30:03 +0000520 if (doReplay && testSuccess) {
521 testSuccess = test_picture_playback(gm, gRec[i],
522 readPath, diffPath);
523 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000524 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000525
tomhudson@google.comea325432011-06-09 20:30:03 +0000526 if (doSerialize && testSuccess) {
527 overallSuccess &= test_picture_serialization(gm, gRec[i],
528 readPath, diffPath);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000529 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000530 }
reed@android.com00dae862009-06-10 15:38:48 +0000531 SkDELETE(gm);
532 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000533 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000534 return -1;
535 }
reed@android.com00dae862009-06-10 15:38:48 +0000536 return 0;
537}
reed@android.comdd0ac282009-06-20 02:38:16 +0000538
539///////////////////////////////////////////////////////////////////////////////
540
541using namespace skiagm;
542
543GM::GM() {}
544GM::~GM() {}
545
546void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000547 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000548}