blob: 254a6247f65e60b7efa25c61f6a86339792b7eb1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
bsalomon@google.com971d0c82011-08-19 17:22:05 +00007
bungeman@google.comb29c8832011-10-10 13:19:10 +00008#include "gm.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +00009#include "GrContext.h"
10#include "GrRenderTarget.h"
11
reed@android.comb9b9a182009-07-08 02:54:47 +000012#include "SkColorPriv.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000013#include "SkData.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000014#include "SkDevice.h"
15#include "SkEGLContext.h"
16#include "SkGpuCanvas.h"
17#include "SkGpuDevice.h"
reed@android.com8015dd82009-06-21 00:49:18 +000018#include "SkGraphics.h"
19#include "SkImageDecoder.h"
20#include "SkImageEncoder.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000021#include "SkPicture.h"
reed@google.com07700442010-12-20 19:46:07 +000022#include "SkStream.h"
23#include "SkRefCnt.h"
24
25#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000026 #include "SkPDFDevice.h"
27 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000028#endif
reed@android.com00dae862009-06-10 15:38:48 +000029
bungeman@google.comb29c8832011-10-10 13:19:10 +000030#ifdef SK_SUPPORT_XPS
31 #include "SkXPSDevice.h"
32#endif
33
reed@google.com46cce912011-06-29 12:54:46 +000034#ifdef SK_BUILD_FOR_MAC
35 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000036 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000037#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000038 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000039#endif
40
reed@android.com00dae862009-06-10 15:38:48 +000041using namespace skiagm;
42
43// need to explicitly declare this, or we get some weird infinite loop llist
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000044template GMRegistry* SkTRegistry<GM*, void*>::gHead;
reed@android.com00dae862009-06-10 15:38:48 +000045
46class Iter {
47public:
48 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000049 this->reset();
50 }
51
52 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000053 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000054 }
reed@google.comd4dfd102011-01-18 21:05:42 +000055
reed@android.comdd0ac282009-06-20 02:38:16 +000056 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000057 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000058 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000059 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000060 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000061 }
62 return NULL;
63 }
reed@google.comd4dfd102011-01-18 21:05:42 +000064
reed@android.com00dae862009-06-10 15:38:48 +000065 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000066 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000067 int count = 0;
68 while (reg) {
69 count += 1;
70 reg = reg->next();
71 }
72 return count;
73 }
reed@google.comd4dfd102011-01-18 21:05:42 +000074
reed@android.com00dae862009-06-10 15:38:48 +000075private:
76 const GMRegistry* fReg;
77};
78
reed@android.com8015dd82009-06-21 00:49:18 +000079static SkString make_name(const char shortName[], const char configName[]) {
80 SkString name(shortName);
81 name.appendf("_%s", configName);
82 return name;
83}
84
tomhudson@google.com9875dd12011-04-25 15:49:53 +000085static SkString make_filename(const char path[],
86 const char pathSuffix[],
87 const SkString& name,
88 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +000089 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +000090 if (filename.endsWith("/")) {
91 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +000092 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +000093 filename.append(pathSuffix);
94 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +000095 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +000096 return filename;
97}
98
reed@android.comb9b9a182009-07-08 02:54:47 +000099/* since PNG insists on unpremultiplying our alpha, we take no precision chances
100 and force all pixels to be 100% opaque, otherwise on compare we may not get
101 a perfect match.
102 */
103static void force_all_opaque(const SkBitmap& bitmap) {
104 SkAutoLockPixels lock(bitmap);
105 for (int y = 0; y < bitmap.height(); y++) {
106 for (int x = 0; x < bitmap.width(); x++) {
107 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
108 }
109 }
110}
111
112static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
113 SkBitmap copy;
114 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
115 force_all_opaque(copy);
116 return SkImageEncoder::EncodeFile(path.c_str(), copy,
117 SkImageEncoder::kPNG_Type, 100);
118}
119
reed@google.com3d3f0922010-12-20 21:10:29 +0000120static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000121 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
122 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
123 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
124 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000125}
126
127static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000128 SkBitmap* diff) {
129 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000130
131 const int w = target.width();
132 const int h = target.height();
133 for (int y = 0; y < h; y++) {
134 for (int x = 0; x < w; x++) {
135 SkPMColor c0 = *base.getAddr32(x, y);
136 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000137 SkPMColor d = 0;
138 if (c0 != c1) {
139 d = compute_diff_pmcolor(c0, c1);
140 }
141 *diff->getAddr32(x, y) = d;
142 }
143 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000144}
145
146static bool compare(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.comea325432011-06-09 20:30:03 +0000147 const SkString& name, const char* renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000148 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000149 SkBitmap copy;
150 const SkBitmap* bm = &target;
151 if (target.config() != SkBitmap::kARGB_8888_Config) {
152 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
153 bm = &copy;
154 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000155 SkBitmap baseCopy;
156 const SkBitmap* bp = &base;
157 if (base.config() != SkBitmap::kARGB_8888_Config) {
158 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
159 bp = &baseCopy;
160 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000161
162 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000163 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000164
165 const int w = bm->width();
166 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000167 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000168 SkDebugf(
169"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
170 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000171 bp->width(), bp->height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000172 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000173 }
174
175 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000176 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000177
178 for (int y = 0; y < h; y++) {
179 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000180 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000181 SkPMColor c1 = *bm->getAddr32(x, y);
182 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000183 SkDebugf(
184"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
185 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000186
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000187 if (diff) {
188 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
189 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000190 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000191 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000192 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000193 }
194 }
195 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000196
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000197 // they're equal
198 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000199}
reed@android.com00dae862009-06-10 15:38:48 +0000200
bungeman@google.comb29c8832011-10-10 13:19:10 +0000201static bool write_document(const SkString& path,
202 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000203 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000204 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000205 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000206}
207
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000208enum Backend {
209 kRaster_Backend,
210 kGPU_Backend,
211 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000212 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000213};
214
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000215struct ConfigData {
216 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000217 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000218 const char* fName;
219};
220
221/// Returns true if processing should continue, false to skip the
222/// remainder of this config for this GM.
223//@todo thudson 22 April 2011 - could refactor this to take in
224// a factory to generate the context, always call readPixels()
225// (logically a noop for rasters, if wasted time), and thus collapse the
226// GPU special case and also let this be used for SkPicture testing.
227static void setup_bitmap(const ConfigData& gRec, SkISize& size,
228 SkBitmap* bitmap) {
229 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
230 bitmap->allocPixels();
231 bitmap->eraseColor(0);
232}
233
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000234// Returns true if the test should continue, false if the test should
235// halt.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000236static bool generate_image(GM* gm, const ConfigData& gRec,
237 GrContext* context,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000238 GrRenderTarget* rt,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000239 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000240 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000241 setup_bitmap(gRec, size, bitmap);
242 SkCanvas canvas(*bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000243
244 if (gRec.fBackend == kRaster_Backend) {
245 gm->draw(&canvas);
246 } else { // GPU
247 if (NULL == context) {
248 return false;
249 }
reed@google.comaf951c92011-06-16 19:10:39 +0000250 SkGpuCanvas gc(context, rt);
251 gc.setDevice(new SkGpuDevice(context, rt))->unref();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000252 gm->draw(&gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000253 // the device is as large as the current rendertarget, so we explicitly
254 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000255 // overwrite our previous allocation
256 gc.readPixels(SkIRect::MakeSize(size), bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000257 }
258 return true;
259}
260
261static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
262 SkPicture* pict, SkBitmap* bitmap) {
263 SkISize size = gm->getISize();
264 setup_bitmap(gRec, size, bitmap);
265 SkCanvas canvas(*bitmap);
266 canvas.drawPicture(*pict);
267}
268
269static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
270#ifdef SK_SUPPORT_PDF
271 SkISize size = gm->getISize();
272 SkMatrix identity;
273 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000274 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000275 SkAutoUnref aur(dev);
276
277 SkCanvas c(dev);
278 gm->draw(&c);
279
280 SkPDFDocument doc;
281 doc.appendPage(dev);
282 doc.emitPDF(&pdf);
283#endif
284}
285
bungeman@google.comb29c8832011-10-10 13:19:10 +0000286static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
287#ifdef SK_SUPPORT_XPS
288 SkISize size = gm->getISize();
289
290 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
291 SkIntToScalar(size.height()));
292 static const double inchesPerMeter = 10000.0 / 254.0;
293 static const double upm = 72 * inchesPerMeter;
294 SkVector unitsPerMeter = SkPoint::Make(SkDoubleToScalar(upm),
295 SkDoubleToScalar(upm));
296 static const double ppm = 200 * inchesPerMeter;
297 SkVector pixelsPerMeter = SkPoint::Make(SkDoubleToScalar(ppm),
298 SkDoubleToScalar(ppm));
299
300 SkXPSDevice* dev = new SkXPSDevice();
301 SkAutoUnref aur(dev);
302
303 SkCanvas c(dev);
304 dev->beginPortfolio(&xps);
305 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
306 gm->draw(&c);
307 dev->endSheet();
308 dev->endPortfolio();
309
310#endif
311}
312
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000313static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000314 const char writePath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000315 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000316 const SkString& name,
317 SkBitmap& bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000318 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000319 SkString path;
320 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000321 if (gRec.fBackend == kRaster_Backend ||
322 gRec.fBackend == kGPU_Backend ||
323 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
324
tomhudson@google.comea325432011-06-09 20:30:03 +0000325 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000326 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000327 }
328 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000329 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000330 success = write_document(path, *document);
331 }
332 if (kXPS_Backend == gRec.fBackend) {
333 path = make_filename(writePath, renderModeDescriptor, name, "xps");
334 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000335 }
336 if (!success) {
337 fprintf(stderr, "FAILED to write %s\n", path.c_str());
338 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000339 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000340}
341
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000342static bool compare_to_reference_image(const SkString& name,
343 SkBitmap &bitmap,
344 const SkBitmap& comparisonBitmap,
345 const char diffPath [],
346 const char renderModeDescriptor []) {
347 bool success;
348 SkBitmap diffBitmap;
349 success = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
350 diffPath ? &diffBitmap : NULL);
351 if (!success && diffPath) {
352 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
353 write_bitmap(diffName, diffBitmap);
354 }
355 return success;
356}
357
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000358static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000359 const SkString& name,
360 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000361 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000362 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000363 SkString path = make_filename(readPath, "", name, "png");
364 SkBitmap orig;
365 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
366 SkBitmap::kARGB_8888_Config,
367 SkImageDecoder::kDecodePixels_Mode, NULL);
368 if (success) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000369 success = compare_to_reference_image(name, bitmap,
370 orig, diffPath,
371 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000372 } else {
373 fprintf(stderr, "FAILED to read %s\n", path.c_str());
reed@google.comac864a92011-06-27 18:11:17 +0000374 // we lie here, and report succes, since we're just missing a master
375 // image. This way we can check in new tests, and not report failure.
376 // A real failure is to draw *differently* from the master image, but
377 // that's not the case here.
378 success = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000379 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000380 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000381}
382
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000383static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000384 const ConfigData& gRec,
385 const char writePath [],
386 const char readPath [],
387 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000388 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000389 SkBitmap& bitmap,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000390 SkDynamicMemoryWStream* pdf,
391 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000392 SkString name = make_name(gm->shortName(), gRec.fName);
393
394 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000395 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000396 name, bitmap, pdf);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000397 } else if (readPath && (
398 gRec.fBackend == kRaster_Backend ||
399 gRec.fBackend == kGPU_Backend ||
400 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000401 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000402 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000403 } else if (comparisonBitmap) {
404 return compare_to_reference_image(name, bitmap,
405 *comparisonBitmap, diffPath,
406 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000407 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000408 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000409}
410
411static SkPicture* generate_new_picture(GM* gm) {
412 // Pictures are refcounted so must be on heap
413 SkPicture* pict = new SkPicture;
414 SkCanvas* cv = pict->beginRecording(1000, 1000);
415 gm->draw(cv);
416 pict->endRecording();
417
418 return pict;
419}
420
421static SkPicture* stream_to_new_picture(const SkPicture& src) {
422
423 // To do in-memory commiunications with a stream, we need to:
424 // * create a dynamic memory stream
425 // * copy it into a buffer
426 // * create a read stream from it
427 // ?!?!
428
429 SkDynamicMemoryWStream storage;
430 src.serialize(&storage);
431
432 int streamSize = storage.getOffset();
433 SkAutoMalloc dstStorage(streamSize);
434 void* dst = dstStorage.get();
435 //char* dst = new char [streamSize];
436 //@todo thudson 22 April 2011 when can we safely delete [] dst?
437 storage.copyTo(dst);
438 SkMemoryStream pictReadback(dst, streamSize);
439 SkPicture* retval = new SkPicture (&pictReadback);
440 return retval;
441}
442
443// Test: draw into a bitmap or pdf.
444// Depending on flags, possibly compare to an expected image
445// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000446static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000447 const ConfigData& gRec,
448 const char writePath [],
449 const char readPath [],
450 const char diffPath [],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000451 GrContext* context,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000452 GrRenderTarget* rt,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000453 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000454 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000455
456 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000457 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000458 // Early exit if we can't generate the image, but this is
459 // expected in some cases, so don't report a test failure.
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000460 if (!generate_image(gm, gRec, context, rt, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000461 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000462 }
reed@google.com46cce912011-06-29 12:54:46 +0000463 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000464 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000465#if CAN_IMAGE_PDF
466 SkAutoDataUnref data(pdf.copyToData());
467 SkMemoryStream stream(data.data(), data.size());
468 SkPDFDocumentToBitmap(&stream, bitmap);
469#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000470 } else if (gRec.fBackend == kXPS_Backend) {
471 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000472 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000473 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000474 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000475}
476
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000477static bool test_picture_playback(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000478 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000479 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000480 const char readPath [],
481 const char diffPath []) {
482 SkPicture* pict = generate_new_picture(gm);
483 SkAutoUnref aur(pict);
484
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000485 if (kRaster_Backend == gRec.fBackend) {
486 SkBitmap bitmap;
487 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000488 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
489 "-replay", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000490 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000491 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000492}
493
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000494static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000495 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000496 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000497 const char readPath [],
498 const char diffPath []) {
499 SkPicture* pict = generate_new_picture(gm);
500 SkAutoUnref aurp(pict);
501 SkPicture* repict = stream_to_new_picture(*pict);
502 SkAutoUnref aurr(repict);
503
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000504 if (kRaster_Backend == gRec.fBackend) {
505 SkBitmap bitmap;
506 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000507 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
508 "-serialize", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000509 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000510 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000511}
512
513static void usage(const char * argv0) {
514 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000515 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000516 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000517 SkDebugf(
518" readPath: directory to read reference images from;\n"
519" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000520 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000521 SkDebugf(" --replay: exercise SkPicture replay.\n");
522 SkDebugf(
523" --serialize: exercise SkPicture serialization & deserialization.\n");
reed@google.come6a5c4d2011-07-25 14:30:54 +0000524 SkDebugf(" --match foo will only run tests that substring match foo.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000525}
526
527static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000528 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
529 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
530 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000531#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000532 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000533#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000534#ifdef SK_SUPPORT_PDF
535 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
536#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000537#ifdef SK_SUPPORT_XPS
538 { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" },
539#endif
reed@android.com00dae862009-06-10 15:38:48 +0000540};
541
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000542namespace skiagm {
543static GrContext* gGrContext;
544GrContext* GetGr() {
545 return gGrContext;
546}
547}
548
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000549int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000550 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000551
reed@android.com8015dd82009-06-21 00:49:18 +0000552 const char* writePath = NULL; // if non-null, where we write the originals
553 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000554 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@google.comece2b022011-07-25 14:28:57 +0000555 const char* matchStr = NULL;
reed@android.com8015dd82009-06-21 00:49:18 +0000556
reed@google.comab973972011-09-19 19:01:38 +0000557 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000558 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000559 bool doSerialize = false;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000560 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000561 char* const* stop = argv + argc;
562 for (++argv; argv < stop; ++argv) {
563 if (strcmp(*argv, "-w") == 0) {
564 argv++;
565 if (argv < stop && **argv) {
566 writePath = *argv;
567 }
568 } else if (strcmp(*argv, "-r") == 0) {
569 argv++;
570 if (argv < stop && **argv) {
571 readPath = *argv;
572 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000573 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000574 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000575 if (argv < stop && **argv) {
576 diffPath = *argv;
577 }
reed@google.comb8b09832011-05-26 15:57:56 +0000578 } else if (strcmp(*argv, "--noreplay") == 0) {
579 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000580 } else if (strcmp(*argv, "--nopdf") == 0) {
581 doPDF = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000582 } else if (strcmp(*argv, "--serialize") == 0) {
583 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000584 } else if (strcmp(*argv, "--match") == 0) {
585 ++argv;
586 if (argv < stop && **argv) {
587 matchStr = *argv;
588 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000589 } else {
590 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000591 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000592 }
593 }
594 if (argv != stop) {
595 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000596 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000597 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000598
bsalomon@google.com39149582011-06-13 21:55:32 +0000599 int maxW = -1;
600 int maxH = -1;
601 Iter iter;
602 GM* gm;
603 while ((gm = iter.next()) != NULL) {
604 SkISize size = gm->getISize();
605 maxW = SkMax32(size.width(), maxW);
606 maxH = SkMax32(size.height(), maxH);
607 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000608 // setup a GL context for drawing offscreen
609 SkEGLContext eglContext;
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000610 GrRenderTarget* rt = NULL;
bsalomon@google.com39149582011-06-13 21:55:32 +0000611 if (eglContext.init(maxW, maxH)) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000612 gGrContext = GrContext::CreateGLShaderContext();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000613 if (NULL != gGrContext) {
614 GrPlatformSurfaceDesc desc;
615 desc.reset();
616 desc.fConfig = kRGBA_8888_GrPixelConfig;
617 desc.fWidth = maxW;
618 desc.fHeight = maxH;
619 desc.fStencilBits = 8;
620 desc.fPlatformRenderTarget = eglContext.getFBOID();
621 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
622 rt = static_cast<GrRenderTarget*>(gGrContext->createPlatformSurface(desc));
623 if (NULL == rt) {
624 gGrContext->unref();
625 gGrContext = NULL;
626 }
627 }
reed@google.com37df17d2010-12-23 20:20:51 +0000628 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000629
reed@android.com00f883e2010-12-14 17:46:14 +0000630 if (readPath) {
631 fprintf(stderr, "reading from %s\n", readPath);
632 } else if (writePath) {
633 fprintf(stderr, "writing to %s\n", writePath);
634 }
635
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000636 // Accumulate success of all tests so we can flag error in any
637 // one with the return value.
bsalomon@google.com39149582011-06-13 21:55:32 +0000638 iter.reset();
tomhudson@google.comea325432011-06-09 20:30:03 +0000639 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000640 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000641 const char* shortName = gm->shortName();
642 if (matchStr && !strstr(shortName, matchStr)) {
643 SkDELETE(gm);
644 continue;
645 }
646
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000647 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000648 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000649 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000650 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000651
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000652 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
reed@google.comfbc21172011-09-19 19:08:33 +0000653 uint32_t gmFlags = gm->getFlags();
654
bungeman@google.com64e011a2011-09-19 19:31:04 +0000655 if ((kPDF_Backend == gRec[i].fBackend) &&
656 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
657 {
reed@google.comab973972011-09-19 19:01:38 +0000658 continue;
659 }
660
tomhudson@google.comea325432011-06-09 20:30:03 +0000661 bool testSuccess = test_drawing(gm, gRec[i],
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000662 writePath, readPath, diffPath, gGrContext,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000663 rt, &forwardRenderedBitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000664 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000665
reed@google.comfbc21172011-09-19 19:08:33 +0000666 if (doReplay && testSuccess && !(gmFlags & GM::kSkipPicture_Flag)) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000667 testSuccess = test_picture_playback(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000668 forwardRenderedBitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000669 readPath, diffPath);
670 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000671 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000672
tomhudson@google.comea325432011-06-09 20:30:03 +0000673 if (doSerialize && testSuccess) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000674 testSuccess &= test_picture_serialization(gm, gRec[i],
675 forwardRenderedBitmap,
676 readPath, diffPath);
677 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000678 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000679 }
reed@android.com00dae862009-06-10 15:38:48 +0000680 SkDELETE(gm);
681 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000682 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000683 return -1;
684 }
reed@android.com00dae862009-06-10 15:38:48 +0000685 return 0;
686}
reed@android.comdd0ac282009-06-20 02:38:16 +0000687
688///////////////////////////////////////////////////////////////////////////////
689
690using namespace skiagm;
691
692GM::GM() {}
693GM::~GM() {}
694
695void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000696 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000697}