blob: ca49d1911f886bfa144a11a0d307a8c9c5ef1e7f [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@google.com8a85d0c2011-06-24 19:12:12 +00003#include "SkData.h"
reed@android.com8015dd82009-06-21 00:49:18 +00004#include "SkGraphics.h"
5#include "SkImageDecoder.h"
6#include "SkImageEncoder.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +00007#include "SkPicture.h"
reed@google.com07700442010-12-20 19:46:07 +00008#include "SkStream.h"
9#include "SkRefCnt.h"
10
reed@google.com873cb1e2010-12-23 15:00:45 +000011#include "GrContext.h"
12#include "SkGpuCanvas.h"
reed@google.comd4dfd102011-01-18 21:05:42 +000013#include "SkGpuDevice.h"
reed@google.com873cb1e2010-12-23 15:00:45 +000014#include "SkEGLContext.h"
15#include "SkDevice.h"
16
reed@google.com07700442010-12-20 19:46:07 +000017#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000018 #include "SkPDFDevice.h"
19 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000020#endif
reed@android.com00dae862009-06-10 15:38:48 +000021
22using namespace skiagm;
23
24// need to explicitly declare this, or we get some weird infinite loop llist
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000025template GMRegistry* SkTRegistry<GM*, void*>::gHead;
reed@android.com00dae862009-06-10 15:38:48 +000026
27class Iter {
28public:
29 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000030 this->reset();
31 }
32
33 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000034 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000035 }
reed@google.comd4dfd102011-01-18 21:05:42 +000036
reed@android.comdd0ac282009-06-20 02:38:16 +000037 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000038 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000039 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000040 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000041 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000042 }
43 return NULL;
44 }
reed@google.comd4dfd102011-01-18 21:05:42 +000045
reed@android.com00dae862009-06-10 15:38:48 +000046 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000047 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000048 int count = 0;
49 while (reg) {
50 count += 1;
51 reg = reg->next();
52 }
53 return count;
54 }
reed@google.comd4dfd102011-01-18 21:05:42 +000055
reed@android.com00dae862009-06-10 15:38:48 +000056private:
57 const GMRegistry* fReg;
58};
59
reed@android.com8015dd82009-06-21 00:49:18 +000060static SkString make_name(const char shortName[], const char configName[]) {
61 SkString name(shortName);
62 name.appendf("_%s", configName);
63 return name;
64}
65
tomhudson@google.com9875dd12011-04-25 15:49:53 +000066static SkString make_filename(const char path[],
67 const char pathSuffix[],
68 const SkString& name,
69 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +000070 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +000071 if (filename.endsWith("/")) {
72 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +000073 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +000074 filename.append(pathSuffix);
75 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +000076 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +000077 return filename;
78}
79
reed@android.comb9b9a182009-07-08 02:54:47 +000080/* since PNG insists on unpremultiplying our alpha, we take no precision chances
81 and force all pixels to be 100% opaque, otherwise on compare we may not get
82 a perfect match.
83 */
84static void force_all_opaque(const SkBitmap& bitmap) {
85 SkAutoLockPixels lock(bitmap);
86 for (int y = 0; y < bitmap.height(); y++) {
87 for (int x = 0; x < bitmap.width(); x++) {
88 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
89 }
90 }
91}
92
93static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
94 SkBitmap copy;
95 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
96 force_all_opaque(copy);
97 return SkImageEncoder::EncodeFile(path.c_str(), copy,
98 SkImageEncoder::kPNG_Type, 100);
99}
100
reed@google.com3d3f0922010-12-20 21:10:29 +0000101static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000102 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
103 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
104 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
105 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000106}
107
108static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000109 SkBitmap* diff) {
110 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000111
112 const int w = target.width();
113 const int h = target.height();
114 for (int y = 0; y < h; y++) {
115 for (int x = 0; x < w; x++) {
116 SkPMColor c0 = *base.getAddr32(x, y);
117 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000118 SkPMColor d = 0;
119 if (c0 != c1) {
120 d = compute_diff_pmcolor(c0, c1);
121 }
122 *diff->getAddr32(x, y) = d;
123 }
124 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000125}
126
127static bool compare(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.comea325432011-06-09 20:30:03 +0000128 const SkString& name, const char* renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000129 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000130 SkBitmap copy;
131 const SkBitmap* bm = &target;
132 if (target.config() != SkBitmap::kARGB_8888_Config) {
133 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
134 bm = &copy;
135 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000136 SkBitmap baseCopy;
137 const SkBitmap* bp = &base;
138 if (base.config() != SkBitmap::kARGB_8888_Config) {
139 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
140 bp = &baseCopy;
141 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000142
143 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000144 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000145
146 const int w = bm->width();
147 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000148 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000149 SkDebugf(
150"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
151 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000152 bp->width(), bp->height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000153 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000154 }
155
156 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000157 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000158
159 for (int y = 0; y < h; y++) {
160 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000161 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000162 SkPMColor c1 = *bm->getAddr32(x, y);
163 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000164 SkDebugf(
165"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
166 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000167
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000168 if (diff) {
169 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
170 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000171 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000172 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000173 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000174 }
175 }
176 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000177
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000178 // they're equal
179 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000180}
reed@android.com00dae862009-06-10 15:38:48 +0000181
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000182static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) {
183 SkFILEWStream stream(path.c_str());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000184 SkAutoDataUnref data(pdf.copyToData());
185 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000186}
187
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000188enum Backend {
189 kRaster_Backend,
190 kGPU_Backend,
191 kPDF_Backend,
192};
193
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000194struct ConfigData {
195 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000196 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000197 const char* fName;
198};
199
200/// Returns true if processing should continue, false to skip the
201/// remainder of this config for this GM.
202//@todo thudson 22 April 2011 - could refactor this to take in
203// a factory to generate the context, always call readPixels()
204// (logically a noop for rasters, if wasted time), and thus collapse the
205// GPU special case and also let this be used for SkPicture testing.
206static void setup_bitmap(const ConfigData& gRec, SkISize& size,
207 SkBitmap* bitmap) {
208 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
209 bitmap->allocPixels();
210 bitmap->eraseColor(0);
211}
212
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000213// Returns true if the test should continue, false if the test should
214// halt.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000215static bool generate_image(GM* gm, const ConfigData& gRec,
216 GrContext* context,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000217 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000218 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000219 setup_bitmap(gRec, size, bitmap);
220 SkCanvas canvas(*bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000221
222 if (gRec.fBackend == kRaster_Backend) {
223 gm->draw(&canvas);
224 } else { // GPU
225 if (NULL == context) {
226 return false;
227 }
reed@google.comaf951c92011-06-16 19:10:39 +0000228 // not a real object, so don't unref it
229 GrRenderTarget* rt = SkGpuDevice::Current3DApiRenderTarget();
230 SkGpuCanvas gc(context, rt);
231 gc.setDevice(new SkGpuDevice(context, rt))->unref();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000232 gm->draw(&gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000233 // the device is as large as the current rendertarget, so we explicitly
234 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000235 // overwrite our previous allocation
236 gc.readPixels(SkIRect::MakeSize(size), bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000237 }
238 return true;
239}
240
241static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
242 SkPicture* pict, SkBitmap* bitmap) {
243 SkISize size = gm->getISize();
244 setup_bitmap(gRec, size, bitmap);
245 SkCanvas canvas(*bitmap);
246 canvas.drawPicture(*pict);
247}
248
249static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
250#ifdef SK_SUPPORT_PDF
251 SkISize size = gm->getISize();
252 SkMatrix identity;
253 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000254 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000255 SkAutoUnref aur(dev);
256
257 SkCanvas c(dev);
258 gm->draw(&c);
259
260 SkPDFDocument doc;
261 doc.appendPage(dev);
262 doc.emitPDF(&pdf);
263#endif
264}
265
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000266static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000267 const char writePath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000268 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000269 const SkString& name,
270 SkBitmap& bitmap,
271 SkDynamicMemoryWStream* pdf) {
272 SkString path;
273 bool success = false;
274 if (gRec.fBackend != kPDF_Backend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000275 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000276 success = write_bitmap(path, bitmap);
277 } else if (pdf) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000278 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000279 success = write_pdf(path, *pdf);
280 }
281 if (!success) {
282 fprintf(stderr, "FAILED to write %s\n", path.c_str());
283 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000284 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000285}
286
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000287static bool compare_to_reference_image(const SkString& name,
288 SkBitmap &bitmap,
289 const SkBitmap& comparisonBitmap,
290 const char diffPath [],
291 const char renderModeDescriptor []) {
292 bool success;
293 SkBitmap diffBitmap;
294 success = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
295 diffPath ? &diffBitmap : NULL);
296 if (!success && diffPath) {
297 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
298 write_bitmap(diffName, diffBitmap);
299 }
300 return success;
301}
302
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000303static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000304 const SkString& name,
305 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000306 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000307 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000308 SkString path = make_filename(readPath, "", name, "png");
309 SkBitmap orig;
310 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
311 SkBitmap::kARGB_8888_Config,
312 SkImageDecoder::kDecodePixels_Mode, NULL);
313 if (success) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000314 success = compare_to_reference_image(name, bitmap,
315 orig, diffPath,
316 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000317 } else {
318 fprintf(stderr, "FAILED to read %s\n", path.c_str());
reed@google.comac864a92011-06-27 18:11:17 +0000319 // we lie here, and report succes, since we're just missing a master
320 // image. This way we can check in new tests, and not report failure.
321 // A real failure is to draw *differently* from the master image, but
322 // that's not the case here.
323 success = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000324 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000325 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000326}
327
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000328static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000329 const ConfigData& gRec,
330 const char writePath [],
331 const char readPath [],
332 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000333 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000334 SkBitmap& bitmap,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000335 SkDynamicMemoryWStream* pdf,
336 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000337 SkString name = make_name(gm->shortName(), gRec.fName);
338
339 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000340 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000341 name, bitmap, pdf);
342 // TODO: Figure out a way to compare PDFs.
343 } else if (readPath && gRec.fBackend != kPDF_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000344 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000345 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000346 } else if (comparisonBitmap) {
347 return compare_to_reference_image(name, bitmap,
348 *comparisonBitmap, diffPath,
349 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000350 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000351 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000352}
353
354static SkPicture* generate_new_picture(GM* gm) {
355 // Pictures are refcounted so must be on heap
356 SkPicture* pict = new SkPicture;
357 SkCanvas* cv = pict->beginRecording(1000, 1000);
358 gm->draw(cv);
359 pict->endRecording();
360
361 return pict;
362}
363
364static SkPicture* stream_to_new_picture(const SkPicture& src) {
365
366 // To do in-memory commiunications with a stream, we need to:
367 // * create a dynamic memory stream
368 // * copy it into a buffer
369 // * create a read stream from it
370 // ?!?!
371
372 SkDynamicMemoryWStream storage;
373 src.serialize(&storage);
374
375 int streamSize = storage.getOffset();
376 SkAutoMalloc dstStorage(streamSize);
377 void* dst = dstStorage.get();
378 //char* dst = new char [streamSize];
379 //@todo thudson 22 April 2011 when can we safely delete [] dst?
380 storage.copyTo(dst);
381 SkMemoryStream pictReadback(dst, streamSize);
382 SkPicture* retval = new SkPicture (&pictReadback);
383 return retval;
384}
385
386// Test: draw into a bitmap or pdf.
387// Depending on flags, possibly compare to an expected image
388// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000389static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000390 const ConfigData& gRec,
391 const char writePath [],
392 const char readPath [],
393 const char diffPath [],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000394 GrContext* context,
395 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000396 SkDynamicMemoryWStream pdf;
397
398 if (gRec.fBackend == kRaster_Backend ||
399 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000400 // Early exit if we can't generate the image, but this is
401 // expected in some cases, so don't report a test failure.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000402 if (!generate_image(gm, gRec, context, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000403 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000404 }
405 }
406 // TODO: Figure out a way to compare PDFs.
407 if (gRec.fBackend == kPDF_Backend && writePath) {
408 generate_pdf(gm, pdf);
409 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000410 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000411 "", *bitmap, &pdf, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000412}
413
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000414static bool test_picture_playback(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000415 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000416 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000417 const char readPath [],
418 const char diffPath []) {
419 SkPicture* pict = generate_new_picture(gm);
420 SkAutoUnref aur(pict);
421
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000422 if (kRaster_Backend == gRec.fBackend) {
423 SkBitmap bitmap;
424 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000425 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
426 "-replay", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000427 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000428 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000429}
430
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000431static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000432 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000433 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000434 const char readPath [],
435 const char diffPath []) {
436 SkPicture* pict = generate_new_picture(gm);
437 SkAutoUnref aurp(pict);
438 SkPicture* repict = stream_to_new_picture(*pict);
439 SkAutoUnref aurr(repict);
440
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000441 if (kRaster_Backend == gRec.fBackend) {
442 SkBitmap bitmap;
443 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000444 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
445 "-serialize", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000446 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000447 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000448}
449
450static void usage(const char * argv0) {
451 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000452 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000453 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000454 SkDebugf(
455" readPath: directory to read reference images from;\n"
456" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000457 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000458 SkDebugf(" --replay: exercise SkPicture replay.\n");
459 SkDebugf(
460" --serialize: exercise SkPicture serialization & deserialization.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000461}
462
463static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000464 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
465 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
466 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000467#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000468 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000469#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000470#ifdef SK_SUPPORT_PDF
471 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
472#endif
reed@android.com00dae862009-06-10 15:38:48 +0000473};
474
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000475int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000476 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000477
reed@android.com8015dd82009-06-21 00:49:18 +0000478 const char* writePath = NULL; // if non-null, where we write the originals
479 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000480 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000481
reed@google.comb8b09832011-05-26 15:57:56 +0000482 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000483 bool doSerialize = false;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000484 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000485 char* const* stop = argv + argc;
486 for (++argv; argv < stop; ++argv) {
487 if (strcmp(*argv, "-w") == 0) {
488 argv++;
489 if (argv < stop && **argv) {
490 writePath = *argv;
491 }
492 } else if (strcmp(*argv, "-r") == 0) {
493 argv++;
494 if (argv < stop && **argv) {
495 readPath = *argv;
496 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000497 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000498 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000499 if (argv < stop && **argv) {
500 diffPath = *argv;
501 }
reed@google.comb8b09832011-05-26 15:57:56 +0000502 } else if (strcmp(*argv, "--noreplay") == 0) {
503 doReplay = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000504 } else if (strcmp(*argv, "--serialize") == 0) {
505 doSerialize = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000506 } else {
507 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000508 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000509 }
510 }
511 if (argv != stop) {
512 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000513 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000514 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000515
bsalomon@google.com39149582011-06-13 21:55:32 +0000516 int maxW = -1;
517 int maxH = -1;
518 Iter iter;
519 GM* gm;
520 while ((gm = iter.next()) != NULL) {
521 SkISize size = gm->getISize();
522 maxW = SkMax32(size.width(), maxW);
523 maxH = SkMax32(size.height(), maxH);
524 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000525 // setup a GL context for drawing offscreen
reed@google.com37df17d2010-12-23 20:20:51 +0000526 GrContext* context = NULL;
reed@google.com873cb1e2010-12-23 15:00:45 +0000527 SkEGLContext eglContext;
bsalomon@google.com39149582011-06-13 21:55:32 +0000528 if (eglContext.init(maxW, maxH)) {
reed@google.com37df17d2010-12-23 20:20:51 +0000529 context = GrContext::CreateGLShaderContext();
530 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000531
reed@android.com00f883e2010-12-14 17:46:14 +0000532
533 if (readPath) {
534 fprintf(stderr, "reading from %s\n", readPath);
535 } else if (writePath) {
536 fprintf(stderr, "writing to %s\n", writePath);
537 }
538
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000539 // Accumulate success of all tests so we can flag error in any
540 // one with the return value.
bsalomon@google.com39149582011-06-13 21:55:32 +0000541 iter.reset();
tomhudson@google.comea325432011-06-09 20:30:03 +0000542 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000543 while ((gm = iter.next()) != NULL) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000544 SkISize size = gm->getISize();
reed@google.com3d3f0922010-12-20 21:10:29 +0000545 SkDebugf("drawing... %s [%d %d]\n", gm->shortName(),
reed@android.com8015dd82009-06-21 00:49:18 +0000546 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000547 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000548
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000549 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000550 bool testSuccess = test_drawing(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000551 writePath, readPath, diffPath, context,
552 &forwardRenderedBitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000553 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000554
tomhudson@google.comea325432011-06-09 20:30:03 +0000555 if (doReplay && testSuccess) {
556 testSuccess = test_picture_playback(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000557 forwardRenderedBitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000558 readPath, diffPath);
559 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000560 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000561
tomhudson@google.comea325432011-06-09 20:30:03 +0000562 if (doSerialize && testSuccess) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000563 testSuccess &= test_picture_serialization(gm, gRec[i],
564 forwardRenderedBitmap,
565 readPath, diffPath);
566 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000567 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000568 }
reed@android.com00dae862009-06-10 15:38:48 +0000569 SkDELETE(gm);
570 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000571 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000572 return -1;
573 }
reed@android.com00dae862009-06-10 15:38:48 +0000574 return 0;
575}
reed@android.comdd0ac282009-06-20 02:38:16 +0000576
577///////////////////////////////////////////////////////////////////////////////
578
579using namespace skiagm;
580
581GM::GM() {}
582GM::~GM() {}
583
584void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000585 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000586}