blob: 04087813587e68803119f302f108bf6783de0357 [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 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000135 SkBitmap baseCopy;
136 const SkBitmap* bp = &base;
137 if (base.config() != SkBitmap::kARGB_8888_Config) {
138 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
139 bp = &baseCopy;
140 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000141
142 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000143 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000144
145 const int w = bm->width();
146 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000147 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000148 SkDebugf(
149"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
150 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000151 bp->width(), bp->height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000152 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000153 }
154
155 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000156 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000157
158 for (int y = 0; y < h; y++) {
159 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000160 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000161 SkPMColor c1 = *bm->getAddr32(x, y);
162 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000163 SkDebugf(
164"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
165 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000166
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000167 if (diff) {
168 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
169 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000170 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000171 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000172 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000173 }
174 }
175 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000176
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000177 // they're equal
178 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000179}
reed@android.com00dae862009-06-10 15:38:48 +0000180
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000181static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) {
182 SkFILEWStream stream(path.c_str());
183 return stream.write(pdf.getStream(), pdf.getOffset());
reed@google.com07700442010-12-20 19:46:07 +0000184}
185
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000186enum Backend {
187 kRaster_Backend,
188 kGPU_Backend,
189 kPDF_Backend,
190};
191
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000192struct ConfigData {
193 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000194 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000195 const char* fName;
196};
197
198/// Returns true if processing should continue, false to skip the
199/// remainder of this config for this GM.
200//@todo thudson 22 April 2011 - could refactor this to take in
201// a factory to generate the context, always call readPixels()
202// (logically a noop for rasters, if wasted time), and thus collapse the
203// GPU special case and also let this be used for SkPicture testing.
204static void setup_bitmap(const ConfigData& gRec, SkISize& size,
205 SkBitmap* bitmap) {
206 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
207 bitmap->allocPixels();
208 bitmap->eraseColor(0);
209}
210
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000211// Returns true if the test should continue, false if the test should
212// halt.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000213static bool generate_image(GM* gm, const ConfigData& gRec,
214 GrContext* context,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000215 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000216 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000217 setup_bitmap(gRec, size, bitmap);
218 SkCanvas canvas(*bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000219
220 if (gRec.fBackend == kRaster_Backend) {
221 gm->draw(&canvas);
222 } else { // GPU
223 if (NULL == context) {
224 return false;
225 }
reed@google.comaf951c92011-06-16 19:10:39 +0000226 // not a real object, so don't unref it
227 GrRenderTarget* rt = SkGpuDevice::Current3DApiRenderTarget();
228 SkGpuCanvas gc(context, rt);
229 gc.setDevice(new SkGpuDevice(context, rt))->unref();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000230 gm->draw(&gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000231 // the device is as large as the current rendertarget, so we explicitly
232 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000233 // overwrite our previous allocation
234 gc.readPixels(SkIRect::MakeSize(size), bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000235 }
236 return true;
237}
238
239static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
240 SkPicture* pict, SkBitmap* bitmap) {
241 SkISize size = gm->getISize();
242 setup_bitmap(gRec, size, bitmap);
243 SkCanvas canvas(*bitmap);
244 canvas.drawPicture(*pict);
245}
246
247static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
248#ifdef SK_SUPPORT_PDF
249 SkISize size = gm->getISize();
250 SkMatrix identity;
251 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000252 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000253 SkAutoUnref aur(dev);
254
255 SkCanvas c(dev);
256 gm->draw(&c);
257
258 SkPDFDocument doc;
259 doc.appendPage(dev);
260 doc.emitPDF(&pdf);
261#endif
262}
263
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000264static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000265 const char writePath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000266 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000267 const SkString& name,
268 SkBitmap& bitmap,
269 SkDynamicMemoryWStream* pdf) {
270 SkString path;
271 bool success = false;
272 if (gRec.fBackend != kPDF_Backend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000273 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000274 success = write_bitmap(path, bitmap);
275 } else if (pdf) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000276 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000277 success = write_pdf(path, *pdf);
278 }
279 if (!success) {
280 fprintf(stderr, "FAILED to write %s\n", path.c_str());
281 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000282 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000283}
284
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000285static bool compare_to_reference_image(const SkString& name,
286 SkBitmap &bitmap,
287 const SkBitmap& comparisonBitmap,
288 const char diffPath [],
289 const char renderModeDescriptor []) {
290 bool success;
291 SkBitmap diffBitmap;
292 success = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
293 diffPath ? &diffBitmap : NULL);
294 if (!success && diffPath) {
295 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
296 write_bitmap(diffName, diffBitmap);
297 }
298 return success;
299}
300
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000301static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000302 const SkString& name,
303 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000304 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000305 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000306 SkString path = make_filename(readPath, "", name, "png");
307 SkBitmap orig;
308 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
309 SkBitmap::kARGB_8888_Config,
310 SkImageDecoder::kDecodePixels_Mode, NULL);
311 if (success) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000312 success = compare_to_reference_image(name, bitmap,
313 orig, diffPath,
314 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000315 } else {
316 fprintf(stderr, "FAILED to read %s\n", path.c_str());
317 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000318 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000319}
320
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000321static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000322 const ConfigData& gRec,
323 const char writePath [],
324 const char readPath [],
325 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000326 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000327 SkBitmap& bitmap,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000328 SkDynamicMemoryWStream* pdf,
329 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000330 SkString name = make_name(gm->shortName(), gRec.fName);
331
332 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000333 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000334 name, bitmap, pdf);
335 // TODO: Figure out a way to compare PDFs.
336 } else if (readPath && gRec.fBackend != kPDF_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000337 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000338 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000339 } else if (comparisonBitmap) {
340 return compare_to_reference_image(name, bitmap,
341 *comparisonBitmap, diffPath,
342 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000343 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000344 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000345}
346
347static SkPicture* generate_new_picture(GM* gm) {
348 // Pictures are refcounted so must be on heap
349 SkPicture* pict = new SkPicture;
350 SkCanvas* cv = pict->beginRecording(1000, 1000);
351 gm->draw(cv);
352 pict->endRecording();
353
354 return pict;
355}
356
357static SkPicture* stream_to_new_picture(const SkPicture& src) {
358
359 // To do in-memory commiunications with a stream, we need to:
360 // * create a dynamic memory stream
361 // * copy it into a buffer
362 // * create a read stream from it
363 // ?!?!
364
365 SkDynamicMemoryWStream storage;
366 src.serialize(&storage);
367
368 int streamSize = storage.getOffset();
369 SkAutoMalloc dstStorage(streamSize);
370 void* dst = dstStorage.get();
371 //char* dst = new char [streamSize];
372 //@todo thudson 22 April 2011 when can we safely delete [] dst?
373 storage.copyTo(dst);
374 SkMemoryStream pictReadback(dst, streamSize);
375 SkPicture* retval = new SkPicture (&pictReadback);
376 return retval;
377}
378
379// Test: draw into a bitmap or pdf.
380// Depending on flags, possibly compare to an expected image
381// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000382static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000383 const ConfigData& gRec,
384 const char writePath [],
385 const char readPath [],
386 const char diffPath [],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000387 GrContext* context,
388 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000389 SkDynamicMemoryWStream pdf;
390
391 if (gRec.fBackend == kRaster_Backend ||
392 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000393 // Early exit if we can't generate the image, but this is
394 // expected in some cases, so don't report a test failure.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000395 if (!generate_image(gm, gRec, context, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000396 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000397 }
398 }
399 // TODO: Figure out a way to compare PDFs.
400 if (gRec.fBackend == kPDF_Backend && writePath) {
401 generate_pdf(gm, pdf);
402 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000403 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000404 "", *bitmap, &pdf, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000405}
406
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000407static bool test_picture_playback(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000408 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000409 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000410 const char readPath [],
411 const char diffPath []) {
412 SkPicture* pict = generate_new_picture(gm);
413 SkAutoUnref aur(pict);
414
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000415 if (kRaster_Backend == gRec.fBackend) {
416 SkBitmap bitmap;
417 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000418 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
419 "-replay", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000420 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000421 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000422}
423
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000424static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000425 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000426 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000427 const char readPath [],
428 const char diffPath []) {
429 SkPicture* pict = generate_new_picture(gm);
430 SkAutoUnref aurp(pict);
431 SkPicture* repict = stream_to_new_picture(*pict);
432 SkAutoUnref aurr(repict);
433
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000434 if (kRaster_Backend == gRec.fBackend) {
435 SkBitmap bitmap;
436 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000437 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
438 "-serialize", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000439 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000440 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000441}
442
443static void usage(const char * argv0) {
444 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000445 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000446 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000447 SkDebugf(
448" readPath: directory to read reference images from;\n"
449" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000450 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000451 SkDebugf(" --replay: exercise SkPicture replay.\n");
452 SkDebugf(
453" --serialize: exercise SkPicture serialization & deserialization.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000454}
455
456static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000457 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
458 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
459 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000460#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000461 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000462#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000463#ifdef SK_SUPPORT_PDF
464 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
465#endif
reed@android.com00dae862009-06-10 15:38:48 +0000466};
467
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000468int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000469 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000470
reed@android.com8015dd82009-06-21 00:49:18 +0000471 const char* writePath = NULL; // if non-null, where we write the originals
472 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000473 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000474
reed@google.comb8b09832011-05-26 15:57:56 +0000475 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000476 bool doSerialize = false;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000477 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000478 char* const* stop = argv + argc;
479 for (++argv; argv < stop; ++argv) {
480 if (strcmp(*argv, "-w") == 0) {
481 argv++;
482 if (argv < stop && **argv) {
483 writePath = *argv;
484 }
485 } else if (strcmp(*argv, "-r") == 0) {
486 argv++;
487 if (argv < stop && **argv) {
488 readPath = *argv;
489 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000490 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000491 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000492 if (argv < stop && **argv) {
493 diffPath = *argv;
494 }
reed@google.comb8b09832011-05-26 15:57:56 +0000495 } else if (strcmp(*argv, "--noreplay") == 0) {
496 doReplay = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000497 } else if (strcmp(*argv, "--serialize") == 0) {
498 doSerialize = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000499 } else {
500 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000501 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000502 }
503 }
504 if (argv != stop) {
505 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000506 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000507 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000508
bsalomon@google.com39149582011-06-13 21:55:32 +0000509 int maxW = -1;
510 int maxH = -1;
511 Iter iter;
512 GM* gm;
513 while ((gm = iter.next()) != NULL) {
514 SkISize size = gm->getISize();
515 maxW = SkMax32(size.width(), maxW);
516 maxH = SkMax32(size.height(), maxH);
517 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000518 // setup a GL context for drawing offscreen
reed@google.com37df17d2010-12-23 20:20:51 +0000519 GrContext* context = NULL;
reed@google.com873cb1e2010-12-23 15:00:45 +0000520 SkEGLContext eglContext;
bsalomon@google.com39149582011-06-13 21:55:32 +0000521 if (eglContext.init(maxW, maxH)) {
reed@google.com37df17d2010-12-23 20:20:51 +0000522 context = GrContext::CreateGLShaderContext();
523 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000524
reed@android.com00f883e2010-12-14 17:46:14 +0000525
526 if (readPath) {
527 fprintf(stderr, "reading from %s\n", readPath);
528 } else if (writePath) {
529 fprintf(stderr, "writing to %s\n", writePath);
530 }
531
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000532 // Accumulate success of all tests so we can flag error in any
533 // one with the return value.
bsalomon@google.com39149582011-06-13 21:55:32 +0000534 iter.reset();
tomhudson@google.comea325432011-06-09 20:30:03 +0000535 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000536 while ((gm = iter.next()) != NULL) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000537 SkISize size = gm->getISize();
reed@google.com3d3f0922010-12-20 21:10:29 +0000538 SkDebugf("drawing... %s [%d %d]\n", gm->shortName(),
reed@android.com8015dd82009-06-21 00:49:18 +0000539 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000540 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000541
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000542 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000543 bool testSuccess = test_drawing(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000544 writePath, readPath, diffPath, context,
545 &forwardRenderedBitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000546 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000547
tomhudson@google.comea325432011-06-09 20:30:03 +0000548 if (doReplay && testSuccess) {
549 testSuccess = test_picture_playback(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000550 forwardRenderedBitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000551 readPath, diffPath);
552 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000553 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000554
tomhudson@google.comea325432011-06-09 20:30:03 +0000555 if (doSerialize && testSuccess) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000556 testSuccess &= test_picture_serialization(gm, gRec[i],
557 forwardRenderedBitmap,
558 readPath, diffPath);
559 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000560 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000561 }
reed@android.com00dae862009-06-10 15:38:48 +0000562 SkDELETE(gm);
563 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000564 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000565 return -1;
566 }
reed@android.com00dae862009-06-10 15:38:48 +0000567 return 0;
568}
reed@android.comdd0ac282009-06-20 02:38:16 +0000569
570///////////////////////////////////////////////////////////////////////////////
571
572using namespace skiagm;
573
574GM::GM() {}
575GM::~GM() {}
576
577void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000578 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000579}