blob: 5252e802cae14a58d627c6bb38904835e8da0d29 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com00dae862009-06-10 15:38:48 +00008#include "gm.h"
reed@android.comb9b9a182009-07-08 02:54:47 +00009#include "SkColorPriv.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000010#include "SkData.h"
reed@android.com8015dd82009-06-21 00:49:18 +000011#include "SkGraphics.h"
12#include "SkImageDecoder.h"
13#include "SkImageEncoder.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000014#include "SkPicture.h"
reed@google.com07700442010-12-20 19:46:07 +000015#include "SkStream.h"
16#include "SkRefCnt.h"
17
reed@google.com873cb1e2010-12-23 15:00:45 +000018#include "GrContext.h"
19#include "SkGpuCanvas.h"
reed@google.comd4dfd102011-01-18 21:05:42 +000020#include "SkGpuDevice.h"
reed@google.com873cb1e2010-12-23 15:00:45 +000021#include "SkEGLContext.h"
22#include "SkDevice.h"
23
reed@google.com07700442010-12-20 19:46:07 +000024#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000025 #include "SkPDFDevice.h"
26 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000027#endif
reed@android.com00dae862009-06-10 15:38:48 +000028
reed@google.com46cce912011-06-29 12:54:46 +000029#ifdef SK_BUILD_FOR_MAC
30 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000031 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000032#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000033 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000034#endif
35
reed@android.com00dae862009-06-10 15:38:48 +000036using namespace skiagm;
37
38// need to explicitly declare this, or we get some weird infinite loop llist
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000039template GMRegistry* SkTRegistry<GM*, void*>::gHead;
reed@android.com00dae862009-06-10 15:38:48 +000040
41class Iter {
42public:
43 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000044 this->reset();
45 }
46
47 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000048 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000049 }
reed@google.comd4dfd102011-01-18 21:05:42 +000050
reed@android.comdd0ac282009-06-20 02:38:16 +000051 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000052 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000053 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000054 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000055 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000056 }
57 return NULL;
58 }
reed@google.comd4dfd102011-01-18 21:05:42 +000059
reed@android.com00dae862009-06-10 15:38:48 +000060 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000061 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000062 int count = 0;
63 while (reg) {
64 count += 1;
65 reg = reg->next();
66 }
67 return count;
68 }
reed@google.comd4dfd102011-01-18 21:05:42 +000069
reed@android.com00dae862009-06-10 15:38:48 +000070private:
71 const GMRegistry* fReg;
72};
73
reed@android.com8015dd82009-06-21 00:49:18 +000074static SkString make_name(const char shortName[], const char configName[]) {
75 SkString name(shortName);
76 name.appendf("_%s", configName);
77 return name;
78}
79
tomhudson@google.com9875dd12011-04-25 15:49:53 +000080static SkString make_filename(const char path[],
81 const char pathSuffix[],
82 const SkString& name,
83 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +000084 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +000085 if (filename.endsWith("/")) {
86 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +000087 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +000088 filename.append(pathSuffix);
89 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +000090 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +000091 return filename;
92}
93
reed@android.comb9b9a182009-07-08 02:54:47 +000094/* since PNG insists on unpremultiplying our alpha, we take no precision chances
95 and force all pixels to be 100% opaque, otherwise on compare we may not get
96 a perfect match.
97 */
98static void force_all_opaque(const SkBitmap& bitmap) {
99 SkAutoLockPixels lock(bitmap);
100 for (int y = 0; y < bitmap.height(); y++) {
101 for (int x = 0; x < bitmap.width(); x++) {
102 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
103 }
104 }
105}
106
107static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
108 SkBitmap copy;
109 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
110 force_all_opaque(copy);
111 return SkImageEncoder::EncodeFile(path.c_str(), copy,
112 SkImageEncoder::kPNG_Type, 100);
113}
114
reed@google.com3d3f0922010-12-20 21:10:29 +0000115static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000116 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
117 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
118 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
119 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000120}
121
122static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000123 SkBitmap* diff) {
124 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000125
126 const int w = target.width();
127 const int h = target.height();
128 for (int y = 0; y < h; y++) {
129 for (int x = 0; x < w; x++) {
130 SkPMColor c0 = *base.getAddr32(x, y);
131 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000132 SkPMColor d = 0;
133 if (c0 != c1) {
134 d = compute_diff_pmcolor(c0, c1);
135 }
136 *diff->getAddr32(x, y) = d;
137 }
138 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000139}
140
141static bool compare(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.comea325432011-06-09 20:30:03 +0000142 const SkString& name, const char* renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000143 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000144 SkBitmap copy;
145 const SkBitmap* bm = &target;
146 if (target.config() != SkBitmap::kARGB_8888_Config) {
147 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
148 bm = &copy;
149 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000150 SkBitmap baseCopy;
151 const SkBitmap* bp = &base;
152 if (base.config() != SkBitmap::kARGB_8888_Config) {
153 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
154 bp = &baseCopy;
155 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000156
157 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000158 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000159
160 const int w = bm->width();
161 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000162 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000163 SkDebugf(
164"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
165 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000166 bp->width(), bp->height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000167 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000168 }
169
170 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000171 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000172
173 for (int y = 0; y < h; y++) {
174 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000175 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000176 SkPMColor c1 = *bm->getAddr32(x, y);
177 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000178 SkDebugf(
179"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
180 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000181
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000182 if (diff) {
183 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
184 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000185 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000186 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000187 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000188 }
189 }
190 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000191
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000192 // they're equal
193 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000194}
reed@android.com00dae862009-06-10 15:38:48 +0000195
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000196static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) {
197 SkFILEWStream stream(path.c_str());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000198 SkAutoDataUnref data(pdf.copyToData());
199 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000200}
201
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000202enum Backend {
203 kRaster_Backend,
204 kGPU_Backend,
205 kPDF_Backend,
206};
207
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000208struct ConfigData {
209 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000210 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000211 const char* fName;
212};
213
214/// Returns true if processing should continue, false to skip the
215/// remainder of this config for this GM.
216//@todo thudson 22 April 2011 - could refactor this to take in
217// a factory to generate the context, always call readPixels()
218// (logically a noop for rasters, if wasted time), and thus collapse the
219// GPU special case and also let this be used for SkPicture testing.
220static void setup_bitmap(const ConfigData& gRec, SkISize& size,
221 SkBitmap* bitmap) {
222 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
223 bitmap->allocPixels();
224 bitmap->eraseColor(0);
225}
226
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000227// Returns true if the test should continue, false if the test should
228// halt.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000229static bool generate_image(GM* gm, const ConfigData& gRec,
230 GrContext* context,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000231 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000232 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000233 setup_bitmap(gRec, size, bitmap);
234 SkCanvas canvas(*bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000235
236 if (gRec.fBackend == kRaster_Backend) {
237 gm->draw(&canvas);
238 } else { // GPU
239 if (NULL == context) {
240 return false;
241 }
reed@google.comaf951c92011-06-16 19:10:39 +0000242 // not a real object, so don't unref it
243 GrRenderTarget* rt = SkGpuDevice::Current3DApiRenderTarget();
244 SkGpuCanvas gc(context, rt);
245 gc.setDevice(new SkGpuDevice(context, rt))->unref();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000246 gm->draw(&gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000247 // the device is as large as the current rendertarget, so we explicitly
248 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000249 // overwrite our previous allocation
250 gc.readPixels(SkIRect::MakeSize(size), bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000251 }
252 return true;
253}
254
255static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
256 SkPicture* pict, SkBitmap* bitmap) {
257 SkISize size = gm->getISize();
258 setup_bitmap(gRec, size, bitmap);
259 SkCanvas canvas(*bitmap);
260 canvas.drawPicture(*pict);
261}
262
263static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
264#ifdef SK_SUPPORT_PDF
265 SkISize size = gm->getISize();
266 SkMatrix identity;
267 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000268 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000269 SkAutoUnref aur(dev);
270
271 SkCanvas c(dev);
272 gm->draw(&c);
273
274 SkPDFDocument doc;
275 doc.appendPage(dev);
276 doc.emitPDF(&pdf);
277#endif
278}
279
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000280static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000281 const char writePath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000282 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000283 const SkString& name,
284 SkBitmap& bitmap,
285 SkDynamicMemoryWStream* pdf) {
286 SkString path;
287 bool success = false;
reed@google.com46cce912011-06-29 12:54:46 +0000288 if (gRec.fBackend != kPDF_Backend || CAN_IMAGE_PDF) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000289 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000290 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000291 }
292 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000293 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000294 success = write_pdf(path, *pdf);
295 }
296 if (!success) {
297 fprintf(stderr, "FAILED to write %s\n", path.c_str());
298 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000299 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000300}
301
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000302static bool compare_to_reference_image(const SkString& name,
303 SkBitmap &bitmap,
304 const SkBitmap& comparisonBitmap,
305 const char diffPath [],
306 const char renderModeDescriptor []) {
307 bool success;
308 SkBitmap diffBitmap;
309 success = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
310 diffPath ? &diffBitmap : NULL);
311 if (!success && diffPath) {
312 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
313 write_bitmap(diffName, diffBitmap);
314 }
315 return success;
316}
317
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000318static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000319 const SkString& name,
320 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000321 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000322 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000323 SkString path = make_filename(readPath, "", name, "png");
324 SkBitmap orig;
325 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
326 SkBitmap::kARGB_8888_Config,
327 SkImageDecoder::kDecodePixels_Mode, NULL);
328 if (success) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000329 success = compare_to_reference_image(name, bitmap,
330 orig, diffPath,
331 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000332 } else {
333 fprintf(stderr, "FAILED to read %s\n", path.c_str());
reed@google.comac864a92011-06-27 18:11:17 +0000334 // we lie here, and report succes, since we're just missing a master
335 // image. This way we can check in new tests, and not report failure.
336 // A real failure is to draw *differently* from the master image, but
337 // that's not the case here.
338 success = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000339 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000340 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000341}
342
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000343static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000344 const ConfigData& gRec,
345 const char writePath [],
346 const char readPath [],
347 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000348 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000349 SkBitmap& bitmap,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000350 SkDynamicMemoryWStream* pdf,
351 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000352 SkString name = make_name(gm->shortName(), gRec.fName);
353
354 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000355 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000356 name, bitmap, pdf);
reed@google.com46cce912011-06-29 12:54:46 +0000357 } else if (readPath && (gRec.fBackend != kPDF_Backend || CAN_IMAGE_PDF)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000358 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000359 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000360 } else if (comparisonBitmap) {
361 return compare_to_reference_image(name, bitmap,
362 *comparisonBitmap, diffPath,
363 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000364 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000365 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000366}
367
368static SkPicture* generate_new_picture(GM* gm) {
369 // Pictures are refcounted so must be on heap
370 SkPicture* pict = new SkPicture;
371 SkCanvas* cv = pict->beginRecording(1000, 1000);
372 gm->draw(cv);
373 pict->endRecording();
374
375 return pict;
376}
377
378static SkPicture* stream_to_new_picture(const SkPicture& src) {
379
380 // To do in-memory commiunications with a stream, we need to:
381 // * create a dynamic memory stream
382 // * copy it into a buffer
383 // * create a read stream from it
384 // ?!?!
385
386 SkDynamicMemoryWStream storage;
387 src.serialize(&storage);
388
389 int streamSize = storage.getOffset();
390 SkAutoMalloc dstStorage(streamSize);
391 void* dst = dstStorage.get();
392 //char* dst = new char [streamSize];
393 //@todo thudson 22 April 2011 when can we safely delete [] dst?
394 storage.copyTo(dst);
395 SkMemoryStream pictReadback(dst, streamSize);
396 SkPicture* retval = new SkPicture (&pictReadback);
397 return retval;
398}
399
400// Test: draw into a bitmap or pdf.
401// Depending on flags, possibly compare to an expected image
402// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000403static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000404 const ConfigData& gRec,
405 const char writePath [],
406 const char readPath [],
407 const char diffPath [],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000408 GrContext* context,
409 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000410 SkDynamicMemoryWStream pdf;
411
412 if (gRec.fBackend == kRaster_Backend ||
413 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000414 // Early exit if we can't generate the image, but this is
415 // expected in some cases, so don't report a test failure.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000416 if (!generate_image(gm, gRec, context, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000417 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000418 }
reed@google.com46cce912011-06-29 12:54:46 +0000419 } else if (gRec.fBackend == kPDF_Backend) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000420 generate_pdf(gm, pdf);
reed@google.com46cce912011-06-29 12:54:46 +0000421#if CAN_IMAGE_PDF
422 SkAutoDataUnref data(pdf.copyToData());
423 SkMemoryStream stream(data.data(), data.size());
424 SkPDFDocumentToBitmap(&stream, bitmap);
425#endif
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000426 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000427 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000428 "", *bitmap, &pdf, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000429}
430
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000431static bool test_picture_playback(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 aur(pict);
438
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000439 if (kRaster_Backend == gRec.fBackend) {
440 SkBitmap bitmap;
441 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000442 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
443 "-replay", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000444 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000445 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000446}
447
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000448static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000449 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000450 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000451 const char readPath [],
452 const char diffPath []) {
453 SkPicture* pict = generate_new_picture(gm);
454 SkAutoUnref aurp(pict);
455 SkPicture* repict = stream_to_new_picture(*pict);
456 SkAutoUnref aurr(repict);
457
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000458 if (kRaster_Backend == gRec.fBackend) {
459 SkBitmap bitmap;
460 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000461 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
462 "-serialize", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000463 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000464 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000465}
466
467static void usage(const char * argv0) {
468 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000469 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000470 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000471 SkDebugf(
472" readPath: directory to read reference images from;\n"
473" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000474 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000475 SkDebugf(" --replay: exercise SkPicture replay.\n");
476 SkDebugf(
477" --serialize: exercise SkPicture serialization & deserialization.\n");
reed@google.come6a5c4d2011-07-25 14:30:54 +0000478 SkDebugf(" --match foo will only run tests that substring match foo.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000479}
480
481static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000482 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
483 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
484 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000485#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000486 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000487#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000488#ifdef SK_SUPPORT_PDF
489 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
490#endif
reed@android.com00dae862009-06-10 15:38:48 +0000491};
492
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000493namespace skiagm {
494static GrContext* gGrContext;
495GrContext* GetGr() {
496 return gGrContext;
497}
498}
499
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000500int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000501 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000502
reed@android.com8015dd82009-06-21 00:49:18 +0000503 const char* writePath = NULL; // if non-null, where we write the originals
504 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000505 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@google.comece2b022011-07-25 14:28:57 +0000506 const char* matchStr = NULL;
reed@android.com8015dd82009-06-21 00:49:18 +0000507
reed@google.comb8b09832011-05-26 15:57:56 +0000508 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000509 bool doSerialize = false;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000510 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000511 char* const* stop = argv + argc;
512 for (++argv; argv < stop; ++argv) {
513 if (strcmp(*argv, "-w") == 0) {
514 argv++;
515 if (argv < stop && **argv) {
516 writePath = *argv;
517 }
518 } else if (strcmp(*argv, "-r") == 0) {
519 argv++;
520 if (argv < stop && **argv) {
521 readPath = *argv;
522 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000523 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000524 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000525 if (argv < stop && **argv) {
526 diffPath = *argv;
527 }
reed@google.comb8b09832011-05-26 15:57:56 +0000528 } else if (strcmp(*argv, "--noreplay") == 0) {
529 doReplay = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000530 } else if (strcmp(*argv, "--serialize") == 0) {
531 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000532 } else if (strcmp(*argv, "--match") == 0) {
533 ++argv;
534 if (argv < stop && **argv) {
535 matchStr = *argv;
536 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000537 } else {
538 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000539 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000540 }
541 }
542 if (argv != stop) {
543 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000544 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000545 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000546
bsalomon@google.com39149582011-06-13 21:55:32 +0000547 int maxW = -1;
548 int maxH = -1;
549 Iter iter;
550 GM* gm;
551 while ((gm = iter.next()) != NULL) {
552 SkISize size = gm->getISize();
553 maxW = SkMax32(size.width(), maxW);
554 maxH = SkMax32(size.height(), maxH);
555 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000556 // setup a GL context for drawing offscreen
557 SkEGLContext eglContext;
bsalomon@google.com39149582011-06-13 21:55:32 +0000558 if (eglContext.init(maxW, maxH)) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000559 gGrContext = GrContext::CreateGLShaderContext();
reed@google.com37df17d2010-12-23 20:20:51 +0000560 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000561
reed@android.com00f883e2010-12-14 17:46:14 +0000562
563 if (readPath) {
564 fprintf(stderr, "reading from %s\n", readPath);
565 } else if (writePath) {
566 fprintf(stderr, "writing to %s\n", writePath);
567 }
568
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000569 // Accumulate success of all tests so we can flag error in any
570 // one with the return value.
bsalomon@google.com39149582011-06-13 21:55:32 +0000571 iter.reset();
tomhudson@google.comea325432011-06-09 20:30:03 +0000572 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000573 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000574 const char* shortName = gm->shortName();
575 if (matchStr && !strstr(shortName, matchStr)) {
576 SkDELETE(gm);
577 continue;
578 }
579
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000580 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000581 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000582 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000583 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000584
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000585 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000586 bool testSuccess = test_drawing(gm, gRec[i],
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000587 writePath, readPath, diffPath, gGrContext,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000588 &forwardRenderedBitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000589 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000590
tomhudson@google.comea325432011-06-09 20:30:03 +0000591 if (doReplay && testSuccess) {
592 testSuccess = test_picture_playback(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000593 forwardRenderedBitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000594 readPath, diffPath);
595 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000596 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000597
tomhudson@google.comea325432011-06-09 20:30:03 +0000598 if (doSerialize && testSuccess) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000599 testSuccess &= test_picture_serialization(gm, gRec[i],
600 forwardRenderedBitmap,
601 readPath, diffPath);
602 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000603 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000604 }
reed@android.com00dae862009-06-10 15:38:48 +0000605 SkDELETE(gm);
606 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000607 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000608 return -1;
609 }
reed@android.com00dae862009-06-10 15:38:48 +0000610 return 0;
611}
reed@android.comdd0ac282009-06-20 02:38:16 +0000612
613///////////////////////////////////////////////////////////////////////////////
614
615using namespace skiagm;
616
617GM::GM() {}
618GM::~GM() {}
619
620void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000621 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000622}