blob: 0827ae6f5cb5d1b71c3c04a3a03af8bdbb2611bb [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());
319 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000320 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000321}
322
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000323static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000324 const ConfigData& gRec,
325 const char writePath [],
326 const char readPath [],
327 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000328 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000329 SkBitmap& bitmap,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000330 SkDynamicMemoryWStream* pdf,
331 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000332 SkString name = make_name(gm->shortName(), gRec.fName);
333
334 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000335 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000336 name, bitmap, pdf);
337 // TODO: Figure out a way to compare PDFs.
338 } else if (readPath && gRec.fBackend != kPDF_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000339 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000340 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000341 } else if (comparisonBitmap) {
342 return compare_to_reference_image(name, bitmap,
343 *comparisonBitmap, diffPath,
344 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000345 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000346 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000347}
348
349static SkPicture* generate_new_picture(GM* gm) {
350 // Pictures are refcounted so must be on heap
351 SkPicture* pict = new SkPicture;
352 SkCanvas* cv = pict->beginRecording(1000, 1000);
353 gm->draw(cv);
354 pict->endRecording();
355
356 return pict;
357}
358
359static SkPicture* stream_to_new_picture(const SkPicture& src) {
360
361 // To do in-memory commiunications with a stream, we need to:
362 // * create a dynamic memory stream
363 // * copy it into a buffer
364 // * create a read stream from it
365 // ?!?!
366
367 SkDynamicMemoryWStream storage;
368 src.serialize(&storage);
369
370 int streamSize = storage.getOffset();
371 SkAutoMalloc dstStorage(streamSize);
372 void* dst = dstStorage.get();
373 //char* dst = new char [streamSize];
374 //@todo thudson 22 April 2011 when can we safely delete [] dst?
375 storage.copyTo(dst);
376 SkMemoryStream pictReadback(dst, streamSize);
377 SkPicture* retval = new SkPicture (&pictReadback);
378 return retval;
379}
380
381// Test: draw into a bitmap or pdf.
382// Depending on flags, possibly compare to an expected image
383// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000384static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000385 const ConfigData& gRec,
386 const char writePath [],
387 const char readPath [],
388 const char diffPath [],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000389 GrContext* context,
390 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000391 SkDynamicMemoryWStream pdf;
392
393 if (gRec.fBackend == kRaster_Backend ||
394 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000395 // Early exit if we can't generate the image, but this is
396 // expected in some cases, so don't report a test failure.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000397 if (!generate_image(gm, gRec, context, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000398 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000399 }
400 }
401 // TODO: Figure out a way to compare PDFs.
402 if (gRec.fBackend == kPDF_Backend && writePath) {
403 generate_pdf(gm, pdf);
404 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000405 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000406 "", *bitmap, &pdf, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000407}
408
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000409static bool test_picture_playback(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000410 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000411 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000412 const char readPath [],
413 const char diffPath []) {
414 SkPicture* pict = generate_new_picture(gm);
415 SkAutoUnref aur(pict);
416
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000417 if (kRaster_Backend == gRec.fBackend) {
418 SkBitmap bitmap;
419 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000420 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
421 "-replay", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000422 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000423 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000424}
425
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000426static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000427 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000428 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000429 const char readPath [],
430 const char diffPath []) {
431 SkPicture* pict = generate_new_picture(gm);
432 SkAutoUnref aurp(pict);
433 SkPicture* repict = stream_to_new_picture(*pict);
434 SkAutoUnref aurr(repict);
435
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000436 if (kRaster_Backend == gRec.fBackend) {
437 SkBitmap bitmap;
438 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000439 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
440 "-serialize", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000441 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000442 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000443}
444
445static void usage(const char * argv0) {
446 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000447 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000448 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000449 SkDebugf(
450" readPath: directory to read reference images from;\n"
451" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000452 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000453 SkDebugf(" --replay: exercise SkPicture replay.\n");
454 SkDebugf(
455" --serialize: exercise SkPicture serialization & deserialization.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000456}
457
458static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000459 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
460 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
461 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000462#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000463 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000464#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000465#ifdef SK_SUPPORT_PDF
466 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
467#endif
reed@android.com00dae862009-06-10 15:38:48 +0000468};
469
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000470int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000471 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000472
reed@android.com8015dd82009-06-21 00:49:18 +0000473 const char* writePath = NULL; // if non-null, where we write the originals
474 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000475 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000476
reed@google.comb8b09832011-05-26 15:57:56 +0000477 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000478 bool doSerialize = false;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000479 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000480 char* const* stop = argv + argc;
481 for (++argv; argv < stop; ++argv) {
482 if (strcmp(*argv, "-w") == 0) {
483 argv++;
484 if (argv < stop && **argv) {
485 writePath = *argv;
486 }
487 } else if (strcmp(*argv, "-r") == 0) {
488 argv++;
489 if (argv < stop && **argv) {
490 readPath = *argv;
491 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000492 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000493 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000494 if (argv < stop && **argv) {
495 diffPath = *argv;
496 }
reed@google.comb8b09832011-05-26 15:57:56 +0000497 } else if (strcmp(*argv, "--noreplay") == 0) {
498 doReplay = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000499 } else if (strcmp(*argv, "--serialize") == 0) {
500 doSerialize = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000501 } else {
502 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000503 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000504 }
505 }
506 if (argv != stop) {
507 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000508 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000509 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000510
bsalomon@google.com39149582011-06-13 21:55:32 +0000511 int maxW = -1;
512 int maxH = -1;
513 Iter iter;
514 GM* gm;
515 while ((gm = iter.next()) != NULL) {
516 SkISize size = gm->getISize();
517 maxW = SkMax32(size.width(), maxW);
518 maxH = SkMax32(size.height(), maxH);
519 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000520 // setup a GL context for drawing offscreen
reed@google.com37df17d2010-12-23 20:20:51 +0000521 GrContext* context = NULL;
reed@google.com873cb1e2010-12-23 15:00:45 +0000522 SkEGLContext eglContext;
bsalomon@google.com39149582011-06-13 21:55:32 +0000523 if (eglContext.init(maxW, maxH)) {
reed@google.com37df17d2010-12-23 20:20:51 +0000524 context = GrContext::CreateGLShaderContext();
525 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000526
reed@android.com00f883e2010-12-14 17:46:14 +0000527
528 if (readPath) {
529 fprintf(stderr, "reading from %s\n", readPath);
530 } else if (writePath) {
531 fprintf(stderr, "writing to %s\n", writePath);
532 }
533
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000534 // Accumulate success of all tests so we can flag error in any
535 // one with the return value.
bsalomon@google.com39149582011-06-13 21:55:32 +0000536 iter.reset();
tomhudson@google.comea325432011-06-09 20:30:03 +0000537 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000538 while ((gm = iter.next()) != NULL) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000539 SkISize size = gm->getISize();
reed@google.com3d3f0922010-12-20 21:10:29 +0000540 SkDebugf("drawing... %s [%d %d]\n", gm->shortName(),
reed@android.com8015dd82009-06-21 00:49:18 +0000541 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000542 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000543
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000544 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000545 bool testSuccess = test_drawing(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000546 writePath, readPath, diffPath, context,
547 &forwardRenderedBitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000548 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000549
tomhudson@google.comea325432011-06-09 20:30:03 +0000550 if (doReplay && testSuccess) {
551 testSuccess = test_picture_playback(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000552 forwardRenderedBitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000553 readPath, diffPath);
554 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000555 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000556
tomhudson@google.comea325432011-06-09 20:30:03 +0000557 if (doSerialize && testSuccess) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000558 testSuccess &= test_picture_serialization(gm, gRec[i],
559 forwardRenderedBitmap,
560 readPath, diffPath);
561 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000562 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000563 }
reed@android.com00dae862009-06-10 15:38:48 +0000564 SkDELETE(gm);
565 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000566 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000567 return -1;
568 }
reed@android.com00dae862009-06-10 15:38:48 +0000569 return 0;
570}
reed@android.comdd0ac282009-06-20 02:38:16 +0000571
572///////////////////////////////////////////////////////////////////////////////
573
574using namespace skiagm;
575
576GM::GM() {}
577GM::~GM() {}
578
579void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000580 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000581}