blob: 6f7f67be00b9e1365035f92b165cf7b2b06cba32 [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"
bsalomon@google.com971d0c82011-08-19 17:22:05 +00009
10#include "GrContext.h"
11#include "GrRenderTarget.h"
12
reed@android.comb9b9a182009-07-08 02:54:47 +000013#include "SkColorPriv.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000014#include "SkData.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000015#include "SkDevice.h"
16#include "SkEGLContext.h"
17#include "SkGpuCanvas.h"
18#include "SkGpuDevice.h"
reed@android.com8015dd82009-06-21 00:49:18 +000019#include "SkGraphics.h"
20#include "SkImageDecoder.h"
21#include "SkImageEncoder.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000022#include "SkPicture.h"
reed@google.com07700442010-12-20 19:46:07 +000023#include "SkStream.h"
24#include "SkRefCnt.h"
25
26#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000027 #include "SkPDFDevice.h"
28 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000029#endif
reed@android.com00dae862009-06-10 15:38:48 +000030
reed@google.com46cce912011-06-29 12:54:46 +000031#ifdef SK_BUILD_FOR_MAC
32 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000033 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000034#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000035 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000036#endif
37
reed@android.com00dae862009-06-10 15:38:48 +000038using namespace skiagm;
39
40// need to explicitly declare this, or we get some weird infinite loop llist
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000041template GMRegistry* SkTRegistry<GM*, void*>::gHead;
reed@android.com00dae862009-06-10 15:38:48 +000042
43class Iter {
44public:
45 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000046 this->reset();
47 }
48
49 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000050 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000051 }
reed@google.comd4dfd102011-01-18 21:05:42 +000052
reed@android.comdd0ac282009-06-20 02:38:16 +000053 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000054 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000055 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000056 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000057 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000058 }
59 return NULL;
60 }
reed@google.comd4dfd102011-01-18 21:05:42 +000061
reed@android.com00dae862009-06-10 15:38:48 +000062 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000063 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000064 int count = 0;
65 while (reg) {
66 count += 1;
67 reg = reg->next();
68 }
69 return count;
70 }
reed@google.comd4dfd102011-01-18 21:05:42 +000071
reed@android.com00dae862009-06-10 15:38:48 +000072private:
73 const GMRegistry* fReg;
74};
75
reed@android.com8015dd82009-06-21 00:49:18 +000076static SkString make_name(const char shortName[], const char configName[]) {
77 SkString name(shortName);
78 name.appendf("_%s", configName);
79 return name;
80}
81
tomhudson@google.com9875dd12011-04-25 15:49:53 +000082static SkString make_filename(const char path[],
83 const char pathSuffix[],
84 const SkString& name,
85 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +000086 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +000087 if (filename.endsWith("/")) {
88 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +000089 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +000090 filename.append(pathSuffix);
91 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +000092 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +000093 return filename;
94}
95
reed@android.comb9b9a182009-07-08 02:54:47 +000096/* since PNG insists on unpremultiplying our alpha, we take no precision chances
97 and force all pixels to be 100% opaque, otherwise on compare we may not get
98 a perfect match.
99 */
100static void force_all_opaque(const SkBitmap& bitmap) {
101 SkAutoLockPixels lock(bitmap);
102 for (int y = 0; y < bitmap.height(); y++) {
103 for (int x = 0; x < bitmap.width(); x++) {
104 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
105 }
106 }
107}
108
109static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
110 SkBitmap copy;
111 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
112 force_all_opaque(copy);
113 return SkImageEncoder::EncodeFile(path.c_str(), copy,
114 SkImageEncoder::kPNG_Type, 100);
115}
116
reed@google.com3d3f0922010-12-20 21:10:29 +0000117static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000118 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
119 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
120 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
121 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000122}
123
124static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000125 SkBitmap* diff) {
126 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000127
128 const int w = target.width();
129 const int h = target.height();
130 for (int y = 0; y < h; y++) {
131 for (int x = 0; x < w; x++) {
132 SkPMColor c0 = *base.getAddr32(x, y);
133 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000134 SkPMColor d = 0;
135 if (c0 != c1) {
136 d = compute_diff_pmcolor(c0, c1);
137 }
138 *diff->getAddr32(x, y) = d;
139 }
140 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000141}
142
143static bool compare(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.comea325432011-06-09 20:30:03 +0000144 const SkString& name, const char* renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000145 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000146 SkBitmap copy;
147 const SkBitmap* bm = &target;
148 if (target.config() != SkBitmap::kARGB_8888_Config) {
149 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
150 bm = &copy;
151 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000152 SkBitmap baseCopy;
153 const SkBitmap* bp = &base;
154 if (base.config() != SkBitmap::kARGB_8888_Config) {
155 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
156 bp = &baseCopy;
157 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000158
159 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000160 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000161
162 const int w = bm->width();
163 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000164 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000165 SkDebugf(
166"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
167 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000168 bp->width(), bp->height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000169 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000170 }
171
172 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000173 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000174
175 for (int y = 0; y < h; y++) {
176 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000177 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000178 SkPMColor c1 = *bm->getAddr32(x, y);
179 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000180 SkDebugf(
181"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
182 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000183
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000184 if (diff) {
185 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
186 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000187 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000188 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000189 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000190 }
191 }
192 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000193
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000194 // they're equal
195 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000196}
reed@android.com00dae862009-06-10 15:38:48 +0000197
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000198static bool write_pdf(const SkString& path, const SkDynamicMemoryWStream& pdf) {
199 SkFILEWStream stream(path.c_str());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000200 SkAutoDataUnref data(pdf.copyToData());
201 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000202}
203
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000204enum Backend {
205 kRaster_Backend,
206 kGPU_Backend,
207 kPDF_Backend,
208};
209
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000210struct ConfigData {
211 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000212 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000213 const char* fName;
214};
215
216/// Returns true if processing should continue, false to skip the
217/// remainder of this config for this GM.
218//@todo thudson 22 April 2011 - could refactor this to take in
219// a factory to generate the context, always call readPixels()
220// (logically a noop for rasters, if wasted time), and thus collapse the
221// GPU special case and also let this be used for SkPicture testing.
222static void setup_bitmap(const ConfigData& gRec, SkISize& size,
223 SkBitmap* bitmap) {
224 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
225 bitmap->allocPixels();
226 bitmap->eraseColor(0);
227}
228
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000229// Returns true if the test should continue, false if the test should
230// halt.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000231static bool generate_image(GM* gm, const ConfigData& gRec,
232 GrContext* context,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000233 GrRenderTarget* rt,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000234 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000235 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000236 setup_bitmap(gRec, size, bitmap);
237 SkCanvas canvas(*bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000238
239 if (gRec.fBackend == kRaster_Backend) {
240 gm->draw(&canvas);
241 } else { // GPU
242 if (NULL == context) {
243 return false;
244 }
reed@google.comaf951c92011-06-16 19:10:39 +0000245 SkGpuCanvas gc(context, rt);
246 gc.setDevice(new SkGpuDevice(context, rt))->unref();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000247 gm->draw(&gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000248 // the device is as large as the current rendertarget, so we explicitly
249 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000250 // overwrite our previous allocation
251 gc.readPixels(SkIRect::MakeSize(size), bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000252 }
253 return true;
254}
255
256static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
257 SkPicture* pict, SkBitmap* bitmap) {
258 SkISize size = gm->getISize();
259 setup_bitmap(gRec, size, bitmap);
260 SkCanvas canvas(*bitmap);
261 canvas.drawPicture(*pict);
262}
263
264static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
265#ifdef SK_SUPPORT_PDF
266 SkISize size = gm->getISize();
267 SkMatrix identity;
268 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000269 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000270 SkAutoUnref aur(dev);
271
272 SkCanvas c(dev);
273 gm->draw(&c);
274
275 SkPDFDocument doc;
276 doc.appendPage(dev);
277 doc.emitPDF(&pdf);
278#endif
279}
280
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000281static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000282 const char writePath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000283 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000284 const SkString& name,
285 SkBitmap& bitmap,
286 SkDynamicMemoryWStream* pdf) {
287 SkString path;
288 bool success = false;
reed@google.com46cce912011-06-29 12:54:46 +0000289 if (gRec.fBackend != kPDF_Backend || CAN_IMAGE_PDF) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000290 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000291 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000292 }
293 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000294 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000295 success = write_pdf(path, *pdf);
296 }
297 if (!success) {
298 fprintf(stderr, "FAILED to write %s\n", path.c_str());
299 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000300 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000301}
302
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000303static bool compare_to_reference_image(const SkString& name,
304 SkBitmap &bitmap,
305 const SkBitmap& comparisonBitmap,
306 const char diffPath [],
307 const char renderModeDescriptor []) {
308 bool success;
309 SkBitmap diffBitmap;
310 success = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
311 diffPath ? &diffBitmap : NULL);
312 if (!success && diffPath) {
313 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
314 write_bitmap(diffName, diffBitmap);
315 }
316 return success;
317}
318
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000319static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000320 const SkString& name,
321 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000322 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000323 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000324 SkString path = make_filename(readPath, "", name, "png");
325 SkBitmap orig;
326 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
327 SkBitmap::kARGB_8888_Config,
328 SkImageDecoder::kDecodePixels_Mode, NULL);
329 if (success) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000330 success = compare_to_reference_image(name, bitmap,
331 orig, diffPath,
332 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000333 } else {
334 fprintf(stderr, "FAILED to read %s\n", path.c_str());
reed@google.comac864a92011-06-27 18:11:17 +0000335 // we lie here, and report succes, since we're just missing a master
336 // image. This way we can check in new tests, and not report failure.
337 // A real failure is to draw *differently* from the master image, but
338 // that's not the case here.
339 success = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000340 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000341 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000342}
343
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000344static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000345 const ConfigData& gRec,
346 const char writePath [],
347 const char readPath [],
348 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000349 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000350 SkBitmap& bitmap,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000351 SkDynamicMemoryWStream* pdf,
352 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000353 SkString name = make_name(gm->shortName(), gRec.fName);
354
355 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000356 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000357 name, bitmap, pdf);
reed@google.com46cce912011-06-29 12:54:46 +0000358 } else if (readPath && (gRec.fBackend != kPDF_Backend || CAN_IMAGE_PDF)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000359 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000360 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000361 } else if (comparisonBitmap) {
362 return compare_to_reference_image(name, bitmap,
363 *comparisonBitmap, diffPath,
364 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000365 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000366 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000367}
368
369static SkPicture* generate_new_picture(GM* gm) {
370 // Pictures are refcounted so must be on heap
371 SkPicture* pict = new SkPicture;
372 SkCanvas* cv = pict->beginRecording(1000, 1000);
373 gm->draw(cv);
374 pict->endRecording();
375
376 return pict;
377}
378
379static SkPicture* stream_to_new_picture(const SkPicture& src) {
380
381 // To do in-memory commiunications with a stream, we need to:
382 // * create a dynamic memory stream
383 // * copy it into a buffer
384 // * create a read stream from it
385 // ?!?!
386
387 SkDynamicMemoryWStream storage;
388 src.serialize(&storage);
389
390 int streamSize = storage.getOffset();
391 SkAutoMalloc dstStorage(streamSize);
392 void* dst = dstStorage.get();
393 //char* dst = new char [streamSize];
394 //@todo thudson 22 April 2011 when can we safely delete [] dst?
395 storage.copyTo(dst);
396 SkMemoryStream pictReadback(dst, streamSize);
397 SkPicture* retval = new SkPicture (&pictReadback);
398 return retval;
399}
400
401// Test: draw into a bitmap or pdf.
402// Depending on flags, possibly compare to an expected image
403// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000404static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000405 const ConfigData& gRec,
406 const char writePath [],
407 const char readPath [],
408 const char diffPath [],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000409 GrContext* context,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000410 GrRenderTarget* rt,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000411 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000412 SkDynamicMemoryWStream pdf;
413
414 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000415 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000416 // Early exit if we can't generate the image, but this is
417 // expected in some cases, so don't report a test failure.
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000418 if (!generate_image(gm, gRec, context, rt, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000419 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000420 }
reed@google.com46cce912011-06-29 12:54:46 +0000421 } else if (gRec.fBackend == kPDF_Backend) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000422 generate_pdf(gm, pdf);
reed@google.com46cce912011-06-29 12:54:46 +0000423#if CAN_IMAGE_PDF
424 SkAutoDataUnref data(pdf.copyToData());
425 SkMemoryStream stream(data.data(), data.size());
426 SkPDFDocumentToBitmap(&stream, bitmap);
427#endif
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000428 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000429 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000430 "", *bitmap, &pdf, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000431}
432
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000433static bool test_picture_playback(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000434 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000435 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000436 const char readPath [],
437 const char diffPath []) {
438 SkPicture* pict = generate_new_picture(gm);
439 SkAutoUnref aur(pict);
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, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000444 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
445 "-replay", 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
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000450static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000451 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000452 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000453 const char readPath [],
454 const char diffPath []) {
455 SkPicture* pict = generate_new_picture(gm);
456 SkAutoUnref aurp(pict);
457 SkPicture* repict = stream_to_new_picture(*pict);
458 SkAutoUnref aurr(repict);
459
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000460 if (kRaster_Backend == gRec.fBackend) {
461 SkBitmap bitmap;
462 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000463 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
464 "-serialize", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000465 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000466 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000467}
468
469static void usage(const char * argv0) {
470 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000471 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000472 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000473 SkDebugf(
474" readPath: directory to read reference images from;\n"
475" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000476 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000477 SkDebugf(" --replay: exercise SkPicture replay.\n");
478 SkDebugf(
479" --serialize: exercise SkPicture serialization & deserialization.\n");
reed@google.come6a5c4d2011-07-25 14:30:54 +0000480 SkDebugf(" --match foo will only run tests that substring match foo.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000481}
482
483static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000484 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
485 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
486 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000487#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000488 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000489#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000490#ifdef SK_SUPPORT_PDF
491 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
492#endif
reed@android.com00dae862009-06-10 15:38:48 +0000493};
494
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000495namespace skiagm {
496static GrContext* gGrContext;
497GrContext* GetGr() {
498 return gGrContext;
499}
500}
501
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000502int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000503 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000504
reed@android.com8015dd82009-06-21 00:49:18 +0000505 const char* writePath = NULL; // if non-null, where we write the originals
506 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000507 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@google.comece2b022011-07-25 14:28:57 +0000508 const char* matchStr = NULL;
reed@android.com8015dd82009-06-21 00:49:18 +0000509
reed@google.comb8b09832011-05-26 15:57:56 +0000510 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000511 bool doSerialize = false;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000512 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000513 char* const* stop = argv + argc;
514 for (++argv; argv < stop; ++argv) {
515 if (strcmp(*argv, "-w") == 0) {
516 argv++;
517 if (argv < stop && **argv) {
518 writePath = *argv;
519 }
520 } else if (strcmp(*argv, "-r") == 0) {
521 argv++;
522 if (argv < stop && **argv) {
523 readPath = *argv;
524 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000525 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000526 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000527 if (argv < stop && **argv) {
528 diffPath = *argv;
529 }
reed@google.comb8b09832011-05-26 15:57:56 +0000530 } else if (strcmp(*argv, "--noreplay") == 0) {
531 doReplay = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000532 } else if (strcmp(*argv, "--serialize") == 0) {
533 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000534 } else if (strcmp(*argv, "--match") == 0) {
535 ++argv;
536 if (argv < stop && **argv) {
537 matchStr = *argv;
538 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000539 } else {
540 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000541 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000542 }
543 }
544 if (argv != stop) {
545 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000546 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000547 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000548
bsalomon@google.com39149582011-06-13 21:55:32 +0000549 int maxW = -1;
550 int maxH = -1;
551 Iter iter;
552 GM* gm;
553 while ((gm = iter.next()) != NULL) {
554 SkISize size = gm->getISize();
555 maxW = SkMax32(size.width(), maxW);
556 maxH = SkMax32(size.height(), maxH);
557 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000558 // setup a GL context for drawing offscreen
559 SkEGLContext eglContext;
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000560 GrRenderTarget* rt = NULL;
bsalomon@google.com39149582011-06-13 21:55:32 +0000561 if (eglContext.init(maxW, maxH)) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000562 gGrContext = GrContext::CreateGLShaderContext();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000563 if (NULL != gGrContext) {
564 GrPlatformSurfaceDesc desc;
565 desc.reset();
566 desc.fConfig = kRGBA_8888_GrPixelConfig;
567 desc.fWidth = maxW;
568 desc.fHeight = maxH;
569 desc.fStencilBits = 8;
570 desc.fPlatformRenderTarget = eglContext.getFBOID();
571 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
572 rt = static_cast<GrRenderTarget*>(gGrContext->createPlatformSurface(desc));
573 if (NULL == rt) {
574 gGrContext->unref();
575 gGrContext = NULL;
576 }
577 }
reed@google.com37df17d2010-12-23 20:20:51 +0000578 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000579
reed@android.com00f883e2010-12-14 17:46:14 +0000580 if (readPath) {
581 fprintf(stderr, "reading from %s\n", readPath);
582 } else if (writePath) {
583 fprintf(stderr, "writing to %s\n", writePath);
584 }
585
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000586 // Accumulate success of all tests so we can flag error in any
587 // one with the return value.
bsalomon@google.com39149582011-06-13 21:55:32 +0000588 iter.reset();
tomhudson@google.comea325432011-06-09 20:30:03 +0000589 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000590 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000591 const char* shortName = gm->shortName();
592 if (matchStr && !strstr(shortName, matchStr)) {
593 SkDELETE(gm);
594 continue;
595 }
596
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000597 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000598 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000599 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000600 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000601
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000602 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000603 bool testSuccess = test_drawing(gm, gRec[i],
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000604 writePath, readPath, diffPath, gGrContext,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000605 rt, &forwardRenderedBitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000606 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000607
tomhudson@google.comea325432011-06-09 20:30:03 +0000608 if (doReplay && testSuccess) {
609 testSuccess = test_picture_playback(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000610 forwardRenderedBitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000611 readPath, diffPath);
612 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000613 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000614
tomhudson@google.comea325432011-06-09 20:30:03 +0000615 if (doSerialize && testSuccess) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000616 testSuccess &= test_picture_serialization(gm, gRec[i],
617 forwardRenderedBitmap,
618 readPath, diffPath);
619 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000620 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000621 }
reed@android.com00dae862009-06-10 15:38:48 +0000622 SkDELETE(gm);
623 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000624 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000625 return -1;
626 }
reed@android.com00dae862009-06-10 15:38:48 +0000627 return 0;
628}
reed@android.comdd0ac282009-06-20 02:38:16 +0000629
630///////////////////////////////////////////////////////////////////////////////
631
632using namespace skiagm;
633
634GM::GM() {}
635GM::~GM() {}
636
637void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000638 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000639}