blob: 373356e11775b38189d0d597fa1f69b38da949cf [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
reed@google.com46cce912011-06-29 12:54:46 +000022#ifdef SK_BUILD_FOR_MAC
23 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000024 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000025#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000026 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000027#endif
28
reed@android.com00dae862009-06-10 15:38:48 +000029using namespace skiagm;
30
31// need to explicitly declare this, or we get some weird infinite loop llist
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000032template GMRegistry* SkTRegistry<GM*, void*>::gHead;
reed@android.com00dae862009-06-10 15:38:48 +000033
34class Iter {
35public:
36 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000037 this->reset();
38 }
39
40 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000041 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000042 }
reed@google.comd4dfd102011-01-18 21:05:42 +000043
reed@android.comdd0ac282009-06-20 02:38:16 +000044 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000045 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000046 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000047 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000048 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000049 }
50 return NULL;
51 }
reed@google.comd4dfd102011-01-18 21:05:42 +000052
reed@android.com00dae862009-06-10 15:38:48 +000053 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000054 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000055 int count = 0;
56 while (reg) {
57 count += 1;
58 reg = reg->next();
59 }
60 return count;
61 }
reed@google.comd4dfd102011-01-18 21:05:42 +000062
reed@android.com00dae862009-06-10 15:38:48 +000063private:
64 const GMRegistry* fReg;
65};
66
reed@android.com8015dd82009-06-21 00:49:18 +000067static SkString make_name(const char shortName[], const char configName[]) {
68 SkString name(shortName);
69 name.appendf("_%s", configName);
70 return name;
71}
72
tomhudson@google.com9875dd12011-04-25 15:49:53 +000073static SkString make_filename(const char path[],
74 const char pathSuffix[],
75 const SkString& name,
76 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +000077 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +000078 if (filename.endsWith("/")) {
79 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +000080 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +000081 filename.append(pathSuffix);
82 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +000083 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +000084 return filename;
85}
86
reed@android.comb9b9a182009-07-08 02:54:47 +000087/* since PNG insists on unpremultiplying our alpha, we take no precision chances
88 and force all pixels to be 100% opaque, otherwise on compare we may not get
89 a perfect match.
90 */
91static void force_all_opaque(const SkBitmap& bitmap) {
92 SkAutoLockPixels lock(bitmap);
93 for (int y = 0; y < bitmap.height(); y++) {
94 for (int x = 0; x < bitmap.width(); x++) {
95 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
96 }
97 }
98}
99
100static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
101 SkBitmap copy;
102 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
103 force_all_opaque(copy);
104 return SkImageEncoder::EncodeFile(path.c_str(), copy,
105 SkImageEncoder::kPNG_Type, 100);
106}
107
reed@google.com3d3f0922010-12-20 21:10:29 +0000108static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000109 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
110 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
111 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
112 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000113}
114
115static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000116 SkBitmap* diff) {
117 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000118
119 const int w = target.width();
120 const int h = target.height();
121 for (int y = 0; y < h; y++) {
122 for (int x = 0; x < w; x++) {
123 SkPMColor c0 = *base.getAddr32(x, y);
124 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000125 SkPMColor d = 0;
126 if (c0 != c1) {
127 d = compute_diff_pmcolor(c0, c1);
128 }
129 *diff->getAddr32(x, y) = d;
130 }
131 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000132}
133
134static bool compare(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.comea325432011-06-09 20:30:03 +0000135 const SkString& name, const char* renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000136 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000137 SkBitmap copy;
138 const SkBitmap* bm = &target;
139 if (target.config() != SkBitmap::kARGB_8888_Config) {
140 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
141 bm = &copy;
142 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000143 SkBitmap baseCopy;
144 const SkBitmap* bp = &base;
145 if (base.config() != SkBitmap::kARGB_8888_Config) {
146 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
147 bp = &baseCopy;
148 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000149
150 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000151 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000152
153 const int w = bm->width();
154 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000155 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000156 SkDebugf(
157"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
158 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000159 bp->width(), bp->height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000160 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000161 }
162
163 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000164 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000165
166 for (int y = 0; y < h; y++) {
167 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000168 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000169 SkPMColor c1 = *bm->getAddr32(x, y);
170 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000171 SkDebugf(
172"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
173 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000174
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000175 if (diff) {
176 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
177 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000178 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000179 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000180 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000181 }
182 }
183 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000184
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000185 // they're equal
186 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000187}
reed@android.com00dae862009-06-10 15:38:48 +0000188
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000189static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) {
190 SkFILEWStream stream(path.c_str());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000191 SkAutoDataUnref data(pdf.copyToData());
192 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000193}
194
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000195enum Backend {
196 kRaster_Backend,
197 kGPU_Backend,
198 kPDF_Backend,
199};
200
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000201struct ConfigData {
202 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000203 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000204 const char* fName;
205};
206
207/// Returns true if processing should continue, false to skip the
208/// remainder of this config for this GM.
209//@todo thudson 22 April 2011 - could refactor this to take in
210// a factory to generate the context, always call readPixels()
211// (logically a noop for rasters, if wasted time), and thus collapse the
212// GPU special case and also let this be used for SkPicture testing.
213static void setup_bitmap(const ConfigData& gRec, SkISize& size,
214 SkBitmap* bitmap) {
215 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
216 bitmap->allocPixels();
217 bitmap->eraseColor(0);
218}
219
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000220// Returns true if the test should continue, false if the test should
221// halt.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000222static bool generate_image(GM* gm, const ConfigData& gRec,
223 GrContext* context,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000224 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000225 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000226 setup_bitmap(gRec, size, bitmap);
227 SkCanvas canvas(*bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000228
229 if (gRec.fBackend == kRaster_Backend) {
230 gm->draw(&canvas);
231 } else { // GPU
232 if (NULL == context) {
233 return false;
234 }
reed@google.comaf951c92011-06-16 19:10:39 +0000235 // not a real object, so don't unref it
236 GrRenderTarget* rt = SkGpuDevice::Current3DApiRenderTarget();
237 SkGpuCanvas gc(context, rt);
238 gc.setDevice(new SkGpuDevice(context, rt))->unref();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000239 gm->draw(&gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000240 // the device is as large as the current rendertarget, so we explicitly
241 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000242 // overwrite our previous allocation
243 gc.readPixels(SkIRect::MakeSize(size), bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000244 }
245 return true;
246}
247
248static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
249 SkPicture* pict, SkBitmap* bitmap) {
250 SkISize size = gm->getISize();
251 setup_bitmap(gRec, size, bitmap);
252 SkCanvas canvas(*bitmap);
253 canvas.drawPicture(*pict);
254}
255
256static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
257#ifdef SK_SUPPORT_PDF
258 SkISize size = gm->getISize();
259 SkMatrix identity;
260 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000261 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000262 SkAutoUnref aur(dev);
263
264 SkCanvas c(dev);
265 gm->draw(&c);
266
267 SkPDFDocument doc;
268 doc.appendPage(dev);
269 doc.emitPDF(&pdf);
270#endif
271}
272
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000273static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000274 const char writePath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000275 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000276 const SkString& name,
277 SkBitmap& bitmap,
278 SkDynamicMemoryWStream* pdf) {
279 SkString path;
280 bool success = false;
reed@google.com46cce912011-06-29 12:54:46 +0000281 if (gRec.fBackend != kPDF_Backend || CAN_IMAGE_PDF) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000282 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000283 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000284 }
285 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000286 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000287 success = write_pdf(path, *pdf);
288 }
289 if (!success) {
290 fprintf(stderr, "FAILED to write %s\n", path.c_str());
291 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000292 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000293}
294
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000295static bool compare_to_reference_image(const SkString& name,
296 SkBitmap &bitmap,
297 const SkBitmap& comparisonBitmap,
298 const char diffPath [],
299 const char renderModeDescriptor []) {
300 bool success;
301 SkBitmap diffBitmap;
302 success = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
303 diffPath ? &diffBitmap : NULL);
304 if (!success && diffPath) {
305 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
306 write_bitmap(diffName, diffBitmap);
307 }
308 return success;
309}
310
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000311static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000312 const SkString& name,
313 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000314 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000315 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000316 SkString path = make_filename(readPath, "", name, "png");
317 SkBitmap orig;
318 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
319 SkBitmap::kARGB_8888_Config,
320 SkImageDecoder::kDecodePixels_Mode, NULL);
321 if (success) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000322 success = compare_to_reference_image(name, bitmap,
323 orig, diffPath,
324 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000325 } else {
326 fprintf(stderr, "FAILED to read %s\n", path.c_str());
reed@google.comac864a92011-06-27 18:11:17 +0000327 // we lie here, and report succes, since we're just missing a master
328 // image. This way we can check in new tests, and not report failure.
329 // A real failure is to draw *differently* from the master image, but
330 // that's not the case here.
331 success = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000332 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000333 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000334}
335
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000336static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000337 const ConfigData& gRec,
338 const char writePath [],
339 const char readPath [],
340 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000341 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000342 SkBitmap& bitmap,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000343 SkDynamicMemoryWStream* pdf,
344 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000345 SkString name = make_name(gm->shortName(), gRec.fName);
346
347 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000348 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000349 name, bitmap, pdf);
reed@google.com46cce912011-06-29 12:54:46 +0000350 } else if (readPath && (gRec.fBackend != kPDF_Backend || CAN_IMAGE_PDF)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000351 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000352 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000353 } else if (comparisonBitmap) {
354 return compare_to_reference_image(name, bitmap,
355 *comparisonBitmap, diffPath,
356 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000357 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000358 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000359}
360
361static SkPicture* generate_new_picture(GM* gm) {
362 // Pictures are refcounted so must be on heap
363 SkPicture* pict = new SkPicture;
364 SkCanvas* cv = pict->beginRecording(1000, 1000);
365 gm->draw(cv);
366 pict->endRecording();
367
368 return pict;
369}
370
371static SkPicture* stream_to_new_picture(const SkPicture& src) {
372
373 // To do in-memory commiunications with a stream, we need to:
374 // * create a dynamic memory stream
375 // * copy it into a buffer
376 // * create a read stream from it
377 // ?!?!
378
379 SkDynamicMemoryWStream storage;
380 src.serialize(&storage);
381
382 int streamSize = storage.getOffset();
383 SkAutoMalloc dstStorage(streamSize);
384 void* dst = dstStorage.get();
385 //char* dst = new char [streamSize];
386 //@todo thudson 22 April 2011 when can we safely delete [] dst?
387 storage.copyTo(dst);
388 SkMemoryStream pictReadback(dst, streamSize);
389 SkPicture* retval = new SkPicture (&pictReadback);
390 return retval;
391}
392
393// Test: draw into a bitmap or pdf.
394// Depending on flags, possibly compare to an expected image
395// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000396static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000397 const ConfigData& gRec,
398 const char writePath [],
399 const char readPath [],
400 const char diffPath [],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000401 GrContext* context,
402 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000403 SkDynamicMemoryWStream pdf;
404
405 if (gRec.fBackend == kRaster_Backend ||
406 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000407 // Early exit if we can't generate the image, but this is
408 // expected in some cases, so don't report a test failure.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000409 if (!generate_image(gm, gRec, context, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000410 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000411 }
reed@google.com46cce912011-06-29 12:54:46 +0000412 } else if (gRec.fBackend == kPDF_Backend) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000413 generate_pdf(gm, pdf);
reed@google.com46cce912011-06-29 12:54:46 +0000414#if CAN_IMAGE_PDF
415 SkAutoDataUnref data(pdf.copyToData());
416 SkMemoryStream stream(data.data(), data.size());
417 SkPDFDocumentToBitmap(&stream, bitmap);
418#endif
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000419 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000420 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000421 "", *bitmap, &pdf, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000422}
423
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000424static bool test_picture_playback(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 aur(pict);
431
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000432 if (kRaster_Backend == gRec.fBackend) {
433 SkBitmap bitmap;
434 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000435 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
436 "-replay", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000437 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000438 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000439}
440
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000441static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000442 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000443 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000444 const char readPath [],
445 const char diffPath []) {
446 SkPicture* pict = generate_new_picture(gm);
447 SkAutoUnref aurp(pict);
448 SkPicture* repict = stream_to_new_picture(*pict);
449 SkAutoUnref aurr(repict);
450
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000451 if (kRaster_Backend == gRec.fBackend) {
452 SkBitmap bitmap;
453 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000454 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
455 "-serialize", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000456 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000457 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000458}
459
460static void usage(const char * argv0) {
461 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000462 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000463 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000464 SkDebugf(
465" readPath: directory to read reference images from;\n"
466" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000467 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000468 SkDebugf(" --replay: exercise SkPicture replay.\n");
469 SkDebugf(
470" --serialize: exercise SkPicture serialization & deserialization.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000471}
472
473static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000474 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
475 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
476 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000477#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000478 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000479#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000480#ifdef SK_SUPPORT_PDF
481 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
482#endif
reed@android.com00dae862009-06-10 15:38:48 +0000483};
484
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000485namespace skiagm {
486static GrContext* gGrContext;
487GrContext* GetGr() {
488 return gGrContext;
489}
490}
491
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000492int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000493 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000494
reed@android.com8015dd82009-06-21 00:49:18 +0000495 const char* writePath = NULL; // if non-null, where we write the originals
496 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000497 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000498
reed@google.comb8b09832011-05-26 15:57:56 +0000499 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000500 bool doSerialize = false;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000501 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000502 char* const* stop = argv + argc;
503 for (++argv; argv < stop; ++argv) {
504 if (strcmp(*argv, "-w") == 0) {
505 argv++;
506 if (argv < stop && **argv) {
507 writePath = *argv;
508 }
509 } else if (strcmp(*argv, "-r") == 0) {
510 argv++;
511 if (argv < stop && **argv) {
512 readPath = *argv;
513 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000514 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000515 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000516 if (argv < stop && **argv) {
517 diffPath = *argv;
518 }
reed@google.comb8b09832011-05-26 15:57:56 +0000519 } else if (strcmp(*argv, "--noreplay") == 0) {
520 doReplay = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000521 } else if (strcmp(*argv, "--serialize") == 0) {
522 doSerialize = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000523 } else {
524 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000525 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000526 }
527 }
528 if (argv != stop) {
529 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000530 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000531 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000532
bsalomon@google.com39149582011-06-13 21:55:32 +0000533 int maxW = -1;
534 int maxH = -1;
535 Iter iter;
536 GM* gm;
537 while ((gm = iter.next()) != NULL) {
538 SkISize size = gm->getISize();
539 maxW = SkMax32(size.width(), maxW);
540 maxH = SkMax32(size.height(), maxH);
541 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000542 // setup a GL context for drawing offscreen
543 SkEGLContext eglContext;
bsalomon@google.com39149582011-06-13 21:55:32 +0000544 if (eglContext.init(maxW, maxH)) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000545 gGrContext = GrContext::CreateGLShaderContext();
reed@google.com37df17d2010-12-23 20:20:51 +0000546 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000547
reed@android.com00f883e2010-12-14 17:46:14 +0000548
549 if (readPath) {
550 fprintf(stderr, "reading from %s\n", readPath);
551 } else if (writePath) {
552 fprintf(stderr, "writing to %s\n", writePath);
553 }
554
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000555 // Accumulate success of all tests so we can flag error in any
556 // one with the return value.
bsalomon@google.com39149582011-06-13 21:55:32 +0000557 iter.reset();
tomhudson@google.comea325432011-06-09 20:30:03 +0000558 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000559 while ((gm = iter.next()) != NULL) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000560 SkISize size = gm->getISize();
reed@google.com3d3f0922010-12-20 21:10:29 +0000561 SkDebugf("drawing... %s [%d %d]\n", gm->shortName(),
reed@android.com8015dd82009-06-21 00:49:18 +0000562 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000563 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000564
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000565 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000566 bool testSuccess = test_drawing(gm, gRec[i],
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000567 writePath, readPath, diffPath, gGrContext,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000568 &forwardRenderedBitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000569 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000570
tomhudson@google.comea325432011-06-09 20:30:03 +0000571 if (doReplay && testSuccess) {
572 testSuccess = test_picture_playback(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000573 forwardRenderedBitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000574 readPath, diffPath);
575 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000576 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000577
tomhudson@google.comea325432011-06-09 20:30:03 +0000578 if (doSerialize && testSuccess) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000579 testSuccess &= test_picture_serialization(gm, gRec[i],
580 forwardRenderedBitmap,
581 readPath, diffPath);
582 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000583 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000584 }
reed@android.com00dae862009-06-10 15:38:48 +0000585 SkDELETE(gm);
586 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000587 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000588 return -1;
589 }
reed@android.com00dae862009-06-10 15:38:48 +0000590 return 0;
591}
reed@android.comdd0ac282009-06-20 02:38:16 +0000592
593///////////////////////////////////////////////////////////////////////////////
594
595using namespace skiagm;
596
597GM::GM() {}
598GM::~GM() {}
599
600void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000601 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000602}