blob: 5269b3405e33dbc91af65afe3053d96a76808538 [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"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000015#include "SkGpuCanvas.h"
16#include "SkGpuDevice.h"
reed@android.com8015dd82009-06-21 00:49:18 +000017#include "SkGraphics.h"
18#include "SkImageDecoder.h"
19#include "SkImageEncoder.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +000020#include "SkNativeGLContext.h"
21#include "SkMesaGLContext.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
bungeman@google.comb29c8832011-10-10 13:19:10 +000031#ifdef SK_SUPPORT_XPS
32 #include "SkXPSDevice.h"
33#endif
34
reed@google.com46cce912011-06-29 12:54:46 +000035#ifdef SK_BUILD_FOR_MAC
36 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000037 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000038#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000039 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000040#endif
41
reed@android.com00dae862009-06-10 15:38:48 +000042using namespace skiagm;
43
reed@android.com00dae862009-06-10 15:38:48 +000044class Iter {
45public:
46 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000047 this->reset();
48 }
49
50 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000051 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000052 }
reed@google.comd4dfd102011-01-18 21:05:42 +000053
reed@android.comdd0ac282009-06-20 02:38:16 +000054 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000055 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000056 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000057 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000058 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000059 }
60 return NULL;
61 }
reed@google.comd4dfd102011-01-18 21:05:42 +000062
reed@android.com00dae862009-06-10 15:38:48 +000063 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000064 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000065 int count = 0;
66 while (reg) {
67 count += 1;
68 reg = reg->next();
69 }
70 return count;
71 }
reed@google.comd4dfd102011-01-18 21:05:42 +000072
reed@android.com00dae862009-06-10 15:38:48 +000073private:
74 const GMRegistry* fReg;
75};
76
reed@android.com8015dd82009-06-21 00:49:18 +000077static SkString make_name(const char shortName[], const char configName[]) {
78 SkString name(shortName);
79 name.appendf("_%s", configName);
80 return name;
81}
82
tomhudson@google.com9875dd12011-04-25 15:49:53 +000083static SkString make_filename(const char path[],
84 const char pathSuffix[],
85 const SkString& name,
86 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +000087 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +000088 if (filename.endsWith("/")) {
89 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +000090 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +000091 filename.append(pathSuffix);
92 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +000093 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +000094 return filename;
95}
96
reed@android.comb9b9a182009-07-08 02:54:47 +000097/* since PNG insists on unpremultiplying our alpha, we take no precision chances
98 and force all pixels to be 100% opaque, otherwise on compare we may not get
99 a perfect match.
100 */
101static void force_all_opaque(const SkBitmap& bitmap) {
102 SkAutoLockPixels lock(bitmap);
103 for (int y = 0; y < bitmap.height(); y++) {
104 for (int x = 0; x < bitmap.width(); x++) {
105 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
106 }
107 }
108}
109
110static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
111 SkBitmap copy;
112 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
113 force_all_opaque(copy);
114 return SkImageEncoder::EncodeFile(path.c_str(), copy,
115 SkImageEncoder::kPNG_Type, 100);
116}
117
reed@google.com3d3f0922010-12-20 21:10:29 +0000118static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000119 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
120 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
121 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
122 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000123}
124
125static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000126 SkBitmap* diff) {
127 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000128
129 const int w = target.width();
130 const int h = target.height();
131 for (int y = 0; y < h; y++) {
132 for (int x = 0; x < w; x++) {
133 SkPMColor c0 = *base.getAddr32(x, y);
134 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000135 SkPMColor d = 0;
136 if (c0 != c1) {
137 d = compute_diff_pmcolor(c0, c1);
138 }
139 *diff->getAddr32(x, y) = d;
140 }
141 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000142}
143
144static bool compare(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.comea325432011-06-09 20:30:03 +0000145 const SkString& name, const char* renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000146 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000147 SkBitmap copy;
148 const SkBitmap* bm = &target;
149 if (target.config() != SkBitmap::kARGB_8888_Config) {
150 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
151 bm = &copy;
152 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000153 SkBitmap baseCopy;
154 const SkBitmap* bp = &base;
155 if (base.config() != SkBitmap::kARGB_8888_Config) {
156 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
157 bp = &baseCopy;
158 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000159
160 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000161 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000162
163 const int w = bm->width();
164 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000165 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000166 SkDebugf(
167"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
168 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000169 bp->width(), bp->height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000170 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000171 }
172
173 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000174 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000175
176 for (int y = 0; y < h; y++) {
177 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000178 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000179 SkPMColor c1 = *bm->getAddr32(x, y);
180 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000181 SkDebugf(
182"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
183 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000184
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000185 if (diff) {
186 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
187 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000188 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000189 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000190 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000191 }
192 }
193 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000194
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000195 // they're equal
196 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000197}
reed@android.com00dae862009-06-10 15:38:48 +0000198
bungeman@google.comb29c8832011-10-10 13:19:10 +0000199static bool write_document(const SkString& path,
200 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000201 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000202 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000203 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000204}
205
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000206enum Backend {
207 kRaster_Backend,
208 kGPU_Backend,
209 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000210 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000211};
212
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000213struct ConfigData {
214 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000215 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000216 const char* fName;
217};
218
219/// Returns true if processing should continue, false to skip the
220/// remainder of this config for this GM.
221//@todo thudson 22 April 2011 - could refactor this to take in
222// a factory to generate the context, always call readPixels()
223// (logically a noop for rasters, if wasted time), and thus collapse the
224// GPU special case and also let this be used for SkPicture testing.
225static void setup_bitmap(const ConfigData& gRec, SkISize& size,
226 SkBitmap* bitmap) {
227 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
228 bitmap->allocPixels();
229 bitmap->eraseColor(0);
230}
231
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000232// Returns true if the test should continue, false if the test should
233// halt.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000234static bool generate_image(GM* gm, const ConfigData& gRec,
235 GrContext* context,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000236 GrRenderTarget* rt,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000237 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000238 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000239 setup_bitmap(gRec, size, bitmap);
240 SkCanvas canvas(*bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000241
242 if (gRec.fBackend == kRaster_Backend) {
243 gm->draw(&canvas);
244 } else { // GPU
245 if (NULL == context) {
246 return false;
247 }
reed@google.comaf951c92011-06-16 19:10:39 +0000248 SkGpuCanvas gc(context, rt);
249 gc.setDevice(new SkGpuDevice(context, rt))->unref();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000250 gm->draw(&gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000251 // the device is as large as the current rendertarget, so we explicitly
252 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000253 // overwrite our previous allocation
254 gc.readPixels(SkIRect::MakeSize(size), bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000255 }
256 return true;
257}
258
259static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
260 SkPicture* pict, SkBitmap* bitmap) {
261 SkISize size = gm->getISize();
262 setup_bitmap(gRec, size, bitmap);
263 SkCanvas canvas(*bitmap);
264 canvas.drawPicture(*pict);
265}
266
267static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
268#ifdef SK_SUPPORT_PDF
269 SkISize size = gm->getISize();
270 SkMatrix identity;
271 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000272 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000273 SkAutoUnref aur(dev);
274
275 SkCanvas c(dev);
276 gm->draw(&c);
277
278 SkPDFDocument doc;
279 doc.appendPage(dev);
280 doc.emitPDF(&pdf);
281#endif
282}
283
bungeman@google.comb29c8832011-10-10 13:19:10 +0000284static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
285#ifdef SK_SUPPORT_XPS
286 SkISize size = gm->getISize();
287
288 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
289 SkIntToScalar(size.height()));
290 static const double inchesPerMeter = 10000.0 / 254.0;
291 static const double upm = 72 * inchesPerMeter;
292 SkVector unitsPerMeter = SkPoint::Make(SkDoubleToScalar(upm),
293 SkDoubleToScalar(upm));
294 static const double ppm = 200 * inchesPerMeter;
295 SkVector pixelsPerMeter = SkPoint::Make(SkDoubleToScalar(ppm),
296 SkDoubleToScalar(ppm));
297
298 SkXPSDevice* dev = new SkXPSDevice();
299 SkAutoUnref aur(dev);
300
301 SkCanvas c(dev);
302 dev->beginPortfolio(&xps);
303 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
304 gm->draw(&c);
305 dev->endSheet();
306 dev->endPortfolio();
307
308#endif
309}
310
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000311static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000312 const char writePath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000313 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000314 const SkString& name,
315 SkBitmap& bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000316 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000317 SkString path;
318 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000319 if (gRec.fBackend == kRaster_Backend ||
320 gRec.fBackend == kGPU_Backend ||
321 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
322
tomhudson@google.comea325432011-06-09 20:30:03 +0000323 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000324 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000325 }
326 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000327 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000328 success = write_document(path, *document);
329 }
330 if (kXPS_Backend == gRec.fBackend) {
331 path = make_filename(writePath, renderModeDescriptor, name, "xps");
332 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000333 }
334 if (!success) {
335 fprintf(stderr, "FAILED to write %s\n", path.c_str());
336 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000337 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000338}
339
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000340static bool compare_to_reference_image(const SkString& name,
341 SkBitmap &bitmap,
342 const SkBitmap& comparisonBitmap,
343 const char diffPath [],
344 const char renderModeDescriptor []) {
345 bool success;
346 SkBitmap diffBitmap;
347 success = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
348 diffPath ? &diffBitmap : NULL);
349 if (!success && diffPath) {
350 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
351 write_bitmap(diffName, diffBitmap);
352 }
353 return success;
354}
355
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000356static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000357 const SkString& name,
358 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000359 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000360 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000361 SkString path = make_filename(readPath, "", name, "png");
362 SkBitmap orig;
363 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
364 SkBitmap::kARGB_8888_Config,
365 SkImageDecoder::kDecodePixels_Mode, NULL);
366 if (success) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000367 success = compare_to_reference_image(name, bitmap,
368 orig, diffPath,
369 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000370 } else {
371 fprintf(stderr, "FAILED to read %s\n", path.c_str());
reed@google.comac864a92011-06-27 18:11:17 +0000372 // we lie here, and report succes, since we're just missing a master
373 // image. This way we can check in new tests, and not report failure.
374 // A real failure is to draw *differently* from the master image, but
375 // that's not the case here.
376 success = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000377 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000378 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000379}
380
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000381static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000382 const ConfigData& gRec,
383 const char writePath [],
384 const char readPath [],
385 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000386 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000387 SkBitmap& bitmap,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000388 SkDynamicMemoryWStream* pdf,
389 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000390 SkString name = make_name(gm->shortName(), gRec.fName);
391
392 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000393 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000394 name, bitmap, pdf);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000395 } else if (readPath && (
396 gRec.fBackend == kRaster_Backend ||
397 gRec.fBackend == kGPU_Backend ||
398 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000399 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000400 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000401 } else if (comparisonBitmap) {
402 return compare_to_reference_image(name, bitmap,
403 *comparisonBitmap, diffPath,
404 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000405 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000406 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000407}
408
409static SkPicture* generate_new_picture(GM* gm) {
410 // Pictures are refcounted so must be on heap
411 SkPicture* pict = new SkPicture;
412 SkCanvas* cv = pict->beginRecording(1000, 1000);
413 gm->draw(cv);
414 pict->endRecording();
415
416 return pict;
417}
418
419static SkPicture* stream_to_new_picture(const SkPicture& src) {
420
421 // To do in-memory commiunications with a stream, we need to:
422 // * create a dynamic memory stream
423 // * copy it into a buffer
424 // * create a read stream from it
425 // ?!?!
426
427 SkDynamicMemoryWStream storage;
428 src.serialize(&storage);
429
430 int streamSize = storage.getOffset();
431 SkAutoMalloc dstStorage(streamSize);
432 void* dst = dstStorage.get();
433 //char* dst = new char [streamSize];
434 //@todo thudson 22 April 2011 when can we safely delete [] dst?
435 storage.copyTo(dst);
436 SkMemoryStream pictReadback(dst, streamSize);
437 SkPicture* retval = new SkPicture (&pictReadback);
438 return retval;
439}
440
441// Test: draw into a bitmap or pdf.
442// Depending on flags, possibly compare to an expected image
443// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000444static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000445 const ConfigData& gRec,
446 const char writePath [],
447 const char readPath [],
448 const char diffPath [],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000449 GrContext* context,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000450 GrRenderTarget* rt,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000451 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000452 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000453
454 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000455 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000456 // Early exit if we can't generate the image, but this is
457 // expected in some cases, so don't report a test failure.
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000458 if (!generate_image(gm, gRec, context, rt, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000459 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000460 }
reed@google.com46cce912011-06-29 12:54:46 +0000461 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000462 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000463#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000464 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000465 SkMemoryStream stream(data.data(), data.size());
466 SkPDFDocumentToBitmap(&stream, bitmap);
467#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000468 } else if (gRec.fBackend == kXPS_Backend) {
469 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000470 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000471 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000472 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000473}
474
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000475static bool test_picture_playback(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000476 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000477 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000478 const char readPath [],
479 const char diffPath []) {
480 SkPicture* pict = generate_new_picture(gm);
481 SkAutoUnref aur(pict);
482
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000483 if (kRaster_Backend == gRec.fBackend) {
484 SkBitmap bitmap;
485 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000486 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
487 "-replay", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000488 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000489 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000490}
491
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000492static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000493 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000494 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000495 const char readPath [],
496 const char diffPath []) {
497 SkPicture* pict = generate_new_picture(gm);
498 SkAutoUnref aurp(pict);
499 SkPicture* repict = stream_to_new_picture(*pict);
500 SkAutoUnref aurr(repict);
501
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000502 if (kRaster_Backend == gRec.fBackend) {
503 SkBitmap bitmap;
504 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000505 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
506 "-serialize", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000507 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000508 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000509}
510
511static void usage(const char * argv0) {
512 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000513 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000514 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000515 SkDebugf(
516" readPath: directory to read reference images from;\n"
517" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000518 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000519 SkDebugf(" --replay: exercise SkPicture replay.\n");
520 SkDebugf(
521" --serialize: exercise SkPicture serialization & deserialization.\n");
reed@google.come6a5c4d2011-07-25 14:30:54 +0000522 SkDebugf(" --match foo will only run tests that substring match foo.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000523#if SK_MESA
524 SkDebugf(" --mesagl will run using the osmesa sw gl rasterizer.\n");
525#endif
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000526}
527
528static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000529 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
530 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
531 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000532#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000533 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000534#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000535#ifdef SK_SUPPORT_PDF
536 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
537#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000538#ifdef SK_SUPPORT_XPS
539 { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" },
540#endif
reed@android.com00dae862009-06-10 15:38:48 +0000541};
542
reed@google.comb2a51622011-10-31 16:30:04 +0000543static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
544 if (0 == array.count()) {
545 // no names, so don't skip anything
546 return false;
547 }
548 for (int i = 0; i < array.count(); ++i) {
549 if (strstr(name, array[i])) {
550 // found the name, so don't skip
551 return false;
552 }
553 }
554 return true;
555}
556
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000557namespace skiagm {
558static GrContext* gGrContext;
559GrContext* GetGr() {
560 return gGrContext;
561}
562}
563
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000564int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000565 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000566
reed@android.com8015dd82009-06-21 00:49:18 +0000567 const char* writePath = NULL; // if non-null, where we write the originals
568 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000569 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000570
reed@google.comb2a51622011-10-31 16:30:04 +0000571 SkTDArray<const char*> fMatches;
572
reed@google.comab973972011-09-19 19:01:38 +0000573 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000574 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000575 bool doSerialize = false;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000576 bool useMesa = false;
577
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000578 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000579 char* const* stop = argv + argc;
580 for (++argv; argv < stop; ++argv) {
581 if (strcmp(*argv, "-w") == 0) {
582 argv++;
583 if (argv < stop && **argv) {
584 writePath = *argv;
585 }
586 } else if (strcmp(*argv, "-r") == 0) {
587 argv++;
588 if (argv < stop && **argv) {
589 readPath = *argv;
590 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000591 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000592 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000593 if (argv < stop && **argv) {
594 diffPath = *argv;
595 }
reed@google.comb8b09832011-05-26 15:57:56 +0000596 } else if (strcmp(*argv, "--noreplay") == 0) {
597 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000598 } else if (strcmp(*argv, "--nopdf") == 0) {
599 doPDF = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000600 } else if (strcmp(*argv, "--serialize") == 0) {
601 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000602 } else if (strcmp(*argv, "--match") == 0) {
603 ++argv;
604 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000605 // just record the ptr, no need for a deep copy
606 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000607 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000608#if SK_MESA
609 } else if (strcmp(*argv, "--mesagl") == 0) {
610 useMesa = true;
611#endif
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000612 } else {
613 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000614 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000615 }
616 }
617 if (argv != stop) {
618 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000619 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000620 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000621
bsalomon@google.com39149582011-06-13 21:55:32 +0000622 int maxW = -1;
623 int maxH = -1;
624 Iter iter;
625 GM* gm;
626 while ((gm = iter.next()) != NULL) {
627 SkISize size = gm->getISize();
628 maxW = SkMax32(size.width(), maxW);
629 maxH = SkMax32(size.height(), maxH);
630 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000631 // setup a GL context for drawing offscreen
bsalomon@google.com373a6632011-10-19 20:43:20 +0000632 SkAutoTUnref<SkGLContext> glContext;
633#if SK_MESA
634 if (useMesa) {
635 glContext.reset(new SkMesaGLContext());
636 } else
637#endif
638 {
639 glContext.reset(new SkNativeGLContext());
640 }
641
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000642 GrRenderTarget* rt = NULL;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000643 if (glContext.get()->init(maxW, maxH)) {
644 GrPlatform3DContext ctx =
645 reinterpret_cast<GrPlatform3DContext>(glContext.get()->gl());
646 gGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000647 if (NULL != gGrContext) {
648 GrPlatformSurfaceDesc desc;
649 desc.reset();
650 desc.fConfig = kRGBA_8888_GrPixelConfig;
651 desc.fWidth = maxW;
652 desc.fHeight = maxH;
653 desc.fStencilBits = 8;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000654 desc.fPlatformRenderTarget = glContext.get()->getFBOID();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000655 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
656 rt = static_cast<GrRenderTarget*>(gGrContext->createPlatformSurface(desc));
657 if (NULL == rt) {
658 gGrContext->unref();
659 gGrContext = NULL;
660 }
661 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000662 } else {
663 fprintf(stderr, "could not create GL context.\n");
reed@google.com37df17d2010-12-23 20:20:51 +0000664 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000665
reed@android.com00f883e2010-12-14 17:46:14 +0000666 if (readPath) {
667 fprintf(stderr, "reading from %s\n", readPath);
668 } else if (writePath) {
669 fprintf(stderr, "writing to %s\n", writePath);
670 }
671
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000672 // Accumulate success of all tests so we can flag error in any
673 // one with the return value.
bsalomon@google.com39149582011-06-13 21:55:32 +0000674 iter.reset();
tomhudson@google.comea325432011-06-09 20:30:03 +0000675 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000676 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000677 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000678 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000679 SkDELETE(gm);
680 continue;
681 }
682
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000683 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000684 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000685 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000686 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000687
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000688 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
reed@google.comfbc21172011-09-19 19:08:33 +0000689 uint32_t gmFlags = gm->getFlags();
690
bungeman@google.com64e011a2011-09-19 19:31:04 +0000691 if ((kPDF_Backend == gRec[i].fBackend) &&
692 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
693 {
reed@google.comab973972011-09-19 19:01:38 +0000694 continue;
695 }
696
tomhudson@google.comea325432011-06-09 20:30:03 +0000697 bool testSuccess = test_drawing(gm, gRec[i],
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000698 writePath, readPath, diffPath, gGrContext,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000699 rt, &forwardRenderedBitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000700 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000701
reed@google.comfbc21172011-09-19 19:08:33 +0000702 if (doReplay && testSuccess && !(gmFlags & GM::kSkipPicture_Flag)) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000703 testSuccess = test_picture_playback(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000704 forwardRenderedBitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000705 readPath, diffPath);
706 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000707 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000708
tomhudson@google.comea325432011-06-09 20:30:03 +0000709 if (doSerialize && testSuccess) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000710 testSuccess &= test_picture_serialization(gm, gRec[i],
711 forwardRenderedBitmap,
712 readPath, diffPath);
713 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000714 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000715 }
reed@android.com00dae862009-06-10 15:38:48 +0000716 SkDELETE(gm);
717 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000718 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000719 return -1;
720 }
reed@android.com00dae862009-06-10 15:38:48 +0000721 return 0;
722}