blob: 35a14f4896ec9526a7192c48e9ec0a9c9ed339b2 [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
44// need to explicitly declare this, or we get some weird infinite loop llist
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000045template GMRegistry* SkTRegistry<GM*, void*>::gHead;
reed@android.com00dae862009-06-10 15:38:48 +000046
47class Iter {
48public:
49 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000050 this->reset();
51 }
52
53 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000054 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000055 }
reed@google.comd4dfd102011-01-18 21:05:42 +000056
reed@android.comdd0ac282009-06-20 02:38:16 +000057 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000058 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000059 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000060 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000061 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000062 }
63 return NULL;
64 }
reed@google.comd4dfd102011-01-18 21:05:42 +000065
reed@android.com00dae862009-06-10 15:38:48 +000066 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000067 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000068 int count = 0;
69 while (reg) {
70 count += 1;
71 reg = reg->next();
72 }
73 return count;
74 }
reed@google.comd4dfd102011-01-18 21:05:42 +000075
reed@android.com00dae862009-06-10 15:38:48 +000076private:
77 const GMRegistry* fReg;
78};
79
reed@android.com8015dd82009-06-21 00:49:18 +000080static SkString make_name(const char shortName[], const char configName[]) {
81 SkString name(shortName);
82 name.appendf("_%s", configName);
83 return name;
84}
85
tomhudson@google.com9875dd12011-04-25 15:49:53 +000086static SkString make_filename(const char path[],
87 const char pathSuffix[],
88 const SkString& name,
89 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +000090 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +000091 if (filename.endsWith("/")) {
92 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +000093 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +000094 filename.append(pathSuffix);
95 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +000096 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +000097 return filename;
98}
99
reed@android.comb9b9a182009-07-08 02:54:47 +0000100/* since PNG insists on unpremultiplying our alpha, we take no precision chances
101 and force all pixels to be 100% opaque, otherwise on compare we may not get
102 a perfect match.
103 */
104static void force_all_opaque(const SkBitmap& bitmap) {
105 SkAutoLockPixels lock(bitmap);
106 for (int y = 0; y < bitmap.height(); y++) {
107 for (int x = 0; x < bitmap.width(); x++) {
108 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
109 }
110 }
111}
112
113static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
114 SkBitmap copy;
115 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
116 force_all_opaque(copy);
117 return SkImageEncoder::EncodeFile(path.c_str(), copy,
118 SkImageEncoder::kPNG_Type, 100);
119}
120
reed@google.com3d3f0922010-12-20 21:10:29 +0000121static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000122 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
123 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
124 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
125 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000126}
127
128static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000129 SkBitmap* diff) {
130 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000131
132 const int w = target.width();
133 const int h = target.height();
134 for (int y = 0; y < h; y++) {
135 for (int x = 0; x < w; x++) {
136 SkPMColor c0 = *base.getAddr32(x, y);
137 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000138 SkPMColor d = 0;
139 if (c0 != c1) {
140 d = compute_diff_pmcolor(c0, c1);
141 }
142 *diff->getAddr32(x, y) = d;
143 }
144 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000145}
146
147static bool compare(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.comea325432011-06-09 20:30:03 +0000148 const SkString& name, const char* renderModeDescriptor,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000149 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000150 SkBitmap copy;
151 const SkBitmap* bm = &target;
152 if (target.config() != SkBitmap::kARGB_8888_Config) {
153 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
154 bm = &copy;
155 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000156 SkBitmap baseCopy;
157 const SkBitmap* bp = &base;
158 if (base.config() != SkBitmap::kARGB_8888_Config) {
159 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
160 bp = &baseCopy;
161 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000162
163 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000164 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000165
166 const int w = bm->width();
167 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000168 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000169 SkDebugf(
170"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
171 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000172 bp->width(), bp->height(), w, h);
reed@google.com3d3f0922010-12-20 21:10:29 +0000173 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000174 }
175
176 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000177 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000178
179 for (int y = 0; y < h; y++) {
180 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000181 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000182 SkPMColor c1 = *bm->getAddr32(x, y);
183 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000184 SkDebugf(
185"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
186 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000187
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000188 if (diff) {
189 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
190 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000191 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000192 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000193 return false;
reed@android.comb9b9a182009-07-08 02:54:47 +0000194 }
195 }
196 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000197
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000198 // they're equal
199 return true;
reed@android.com8015dd82009-06-21 00:49:18 +0000200}
reed@android.com00dae862009-06-10 15:38:48 +0000201
bungeman@google.comb29c8832011-10-10 13:19:10 +0000202static bool write_document(const SkString& path,
203 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000204 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000205 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000206 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000207}
208
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000209enum Backend {
210 kRaster_Backend,
211 kGPU_Backend,
212 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000213 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000214};
215
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000216struct ConfigData {
217 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000218 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000219 const char* fName;
220};
221
222/// Returns true if processing should continue, false to skip the
223/// remainder of this config for this GM.
224//@todo thudson 22 April 2011 - could refactor this to take in
225// a factory to generate the context, always call readPixels()
226// (logically a noop for rasters, if wasted time), and thus collapse the
227// GPU special case and also let this be used for SkPicture testing.
228static void setup_bitmap(const ConfigData& gRec, SkISize& size,
229 SkBitmap* bitmap) {
230 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
231 bitmap->allocPixels();
232 bitmap->eraseColor(0);
233}
234
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000235// Returns true if the test should continue, false if the test should
236// halt.
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000237static bool generate_image(GM* gm, const ConfigData& gRec,
238 GrContext* context,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000239 GrRenderTarget* rt,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000240 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000241 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000242 setup_bitmap(gRec, size, bitmap);
243 SkCanvas canvas(*bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000244
245 if (gRec.fBackend == kRaster_Backend) {
246 gm->draw(&canvas);
247 } else { // GPU
248 if (NULL == context) {
249 return false;
250 }
reed@google.comaf951c92011-06-16 19:10:39 +0000251 SkGpuCanvas gc(context, rt);
252 gc.setDevice(new SkGpuDevice(context, rt))->unref();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000253 gm->draw(&gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000254 // the device is as large as the current rendertarget, so we explicitly
255 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000256 // overwrite our previous allocation
257 gc.readPixels(SkIRect::MakeSize(size), bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000258 }
259 return true;
260}
261
262static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
263 SkPicture* pict, SkBitmap* bitmap) {
264 SkISize size = gm->getISize();
265 setup_bitmap(gRec, size, bitmap);
266 SkCanvas canvas(*bitmap);
267 canvas.drawPicture(*pict);
268}
269
270static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
271#ifdef SK_SUPPORT_PDF
272 SkISize size = gm->getISize();
273 SkMatrix identity;
274 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000275 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000276 SkAutoUnref aur(dev);
277
278 SkCanvas c(dev);
279 gm->draw(&c);
280
281 SkPDFDocument doc;
282 doc.appendPage(dev);
283 doc.emitPDF(&pdf);
284#endif
285}
286
bungeman@google.comb29c8832011-10-10 13:19:10 +0000287static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
288#ifdef SK_SUPPORT_XPS
289 SkISize size = gm->getISize();
290
291 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
292 SkIntToScalar(size.height()));
293 static const double inchesPerMeter = 10000.0 / 254.0;
294 static const double upm = 72 * inchesPerMeter;
295 SkVector unitsPerMeter = SkPoint::Make(SkDoubleToScalar(upm),
296 SkDoubleToScalar(upm));
297 static const double ppm = 200 * inchesPerMeter;
298 SkVector pixelsPerMeter = SkPoint::Make(SkDoubleToScalar(ppm),
299 SkDoubleToScalar(ppm));
300
301 SkXPSDevice* dev = new SkXPSDevice();
302 SkAutoUnref aur(dev);
303
304 SkCanvas c(dev);
305 dev->beginPortfolio(&xps);
306 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
307 gm->draw(&c);
308 dev->endSheet();
309 dev->endPortfolio();
310
311#endif
312}
313
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000314static bool write_reference_image(const ConfigData& gRec,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000315 const char writePath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000316 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000317 const SkString& name,
318 SkBitmap& bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000319 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000320 SkString path;
321 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000322 if (gRec.fBackend == kRaster_Backend ||
323 gRec.fBackend == kGPU_Backend ||
324 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
325
tomhudson@google.comea325432011-06-09 20:30:03 +0000326 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000327 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000328 }
329 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000330 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000331 success = write_document(path, *document);
332 }
333 if (kXPS_Backend == gRec.fBackend) {
334 path = make_filename(writePath, renderModeDescriptor, name, "xps");
335 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000336 }
337 if (!success) {
338 fprintf(stderr, "FAILED to write %s\n", path.c_str());
339 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000340 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000341}
342
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000343static bool compare_to_reference_image(const SkString& name,
344 SkBitmap &bitmap,
345 const SkBitmap& comparisonBitmap,
346 const char diffPath [],
347 const char renderModeDescriptor []) {
348 bool success;
349 SkBitmap diffBitmap;
350 success = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
351 diffPath ? &diffBitmap : NULL);
352 if (!success && diffPath) {
353 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
354 write_bitmap(diffName, diffBitmap);
355 }
356 return success;
357}
358
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000359static bool compare_to_reference_image(const char readPath [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000360 const SkString& name,
361 SkBitmap &bitmap,
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000362 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000363 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000364 SkString path = make_filename(readPath, "", name, "png");
365 SkBitmap orig;
366 bool success = SkImageDecoder::DecodeFile(path.c_str(), &orig,
367 SkBitmap::kARGB_8888_Config,
368 SkImageDecoder::kDecodePixels_Mode, NULL);
369 if (success) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000370 success = compare_to_reference_image(name, bitmap,
371 orig, diffPath,
372 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000373 } else {
374 fprintf(stderr, "FAILED to read %s\n", path.c_str());
reed@google.comac864a92011-06-27 18:11:17 +0000375 // we lie here, and report succes, since we're just missing a master
376 // image. This way we can check in new tests, and not report failure.
377 // A real failure is to draw *differently* from the master image, but
378 // that's not the case here.
379 success = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000380 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000381 return success;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000382}
383
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000384static bool handle_test_results(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000385 const ConfigData& gRec,
386 const char writePath [],
387 const char readPath [],
388 const char diffPath [],
tomhudson@google.comea325432011-06-09 20:30:03 +0000389 const char renderModeDescriptor [],
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000390 SkBitmap& bitmap,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000391 SkDynamicMemoryWStream* pdf,
392 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000393 SkString name = make_name(gm->shortName(), gRec.fName);
394
395 if (writePath) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000396 write_reference_image(gRec, writePath, renderModeDescriptor,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000397 name, bitmap, pdf);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000398 } else if (readPath && (
399 gRec.fBackend == kRaster_Backend ||
400 gRec.fBackend == kGPU_Backend ||
401 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000402 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000403 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000404 } else if (comparisonBitmap) {
405 return compare_to_reference_image(name, bitmap,
406 *comparisonBitmap, diffPath,
407 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000408 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000409 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000410}
411
412static SkPicture* generate_new_picture(GM* gm) {
413 // Pictures are refcounted so must be on heap
414 SkPicture* pict = new SkPicture;
415 SkCanvas* cv = pict->beginRecording(1000, 1000);
416 gm->draw(cv);
417 pict->endRecording();
418
419 return pict;
420}
421
422static SkPicture* stream_to_new_picture(const SkPicture& src) {
423
424 // To do in-memory commiunications with a stream, we need to:
425 // * create a dynamic memory stream
426 // * copy it into a buffer
427 // * create a read stream from it
428 // ?!?!
429
430 SkDynamicMemoryWStream storage;
431 src.serialize(&storage);
432
433 int streamSize = storage.getOffset();
434 SkAutoMalloc dstStorage(streamSize);
435 void* dst = dstStorage.get();
436 //char* dst = new char [streamSize];
437 //@todo thudson 22 April 2011 when can we safely delete [] dst?
438 storage.copyTo(dst);
439 SkMemoryStream pictReadback(dst, streamSize);
440 SkPicture* retval = new SkPicture (&pictReadback);
441 return retval;
442}
443
444// Test: draw into a bitmap or pdf.
445// Depending on flags, possibly compare to an expected image
446// and possibly output a diff image if it fails to match.
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000447static bool test_drawing(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000448 const ConfigData& gRec,
449 const char writePath [],
450 const char readPath [],
451 const char diffPath [],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000452 GrContext* context,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000453 GrRenderTarget* rt,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000454 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000455 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000456
457 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000458 gRec.fBackend == kGPU_Backend) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000459 // Early exit if we can't generate the image, but this is
460 // expected in some cases, so don't report a test failure.
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000461 if (!generate_image(gm, gRec, context, rt, bitmap)) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000462 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000463 }
reed@google.com46cce912011-06-29 12:54:46 +0000464 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000465 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000466#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000467 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000468 SkMemoryStream stream(data.data(), data.size());
469 SkPDFDocumentToBitmap(&stream, bitmap);
470#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000471 } else if (gRec.fBackend == kXPS_Backend) {
472 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000473 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000474 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000475 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000476}
477
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000478static bool test_picture_playback(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000479 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000480 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000481 const char readPath [],
482 const char diffPath []) {
483 SkPicture* pict = generate_new_picture(gm);
484 SkAutoUnref aur(pict);
485
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000486 if (kRaster_Backend == gRec.fBackend) {
487 SkBitmap bitmap;
488 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000489 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
490 "-replay", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000491 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000492 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000493}
494
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000495static bool test_picture_serialization(GM* gm,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000496 const ConfigData& gRec,
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000497 const SkBitmap& comparisonBitmap,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000498 const char readPath [],
499 const char diffPath []) {
500 SkPicture* pict = generate_new_picture(gm);
501 SkAutoUnref aurp(pict);
502 SkPicture* repict = stream_to_new_picture(*pict);
503 SkAutoUnref aurr(repict);
504
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000505 if (kRaster_Backend == gRec.fBackend) {
506 SkBitmap bitmap;
507 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000508 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
509 "-serialize", bitmap, NULL, &comparisonBitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000510 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000511 return true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000512}
513
514static void usage(const char * argv0) {
515 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000516 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000517 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000518 SkDebugf(
519" readPath: directory to read reference images from;\n"
520" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000521 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000522 SkDebugf(" --replay: exercise SkPicture replay.\n");
523 SkDebugf(
524" --serialize: exercise SkPicture serialization & deserialization.\n");
reed@google.come6a5c4d2011-07-25 14:30:54 +0000525 SkDebugf(" --match foo will only run tests that substring match foo.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000526#if SK_MESA
527 SkDebugf(" --mesagl will run using the osmesa sw gl rasterizer.\n");
528#endif
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000529}
530
531static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000532 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
533 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
534 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000535#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000536 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000537#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000538#ifdef SK_SUPPORT_PDF
539 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
540#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000541#ifdef SK_SUPPORT_XPS
542 { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" },
543#endif
reed@android.com00dae862009-06-10 15:38:48 +0000544};
545
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000546namespace skiagm {
547static GrContext* gGrContext;
548GrContext* GetGr() {
549 return gGrContext;
550}
551}
552
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000553int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000554 SkAutoGraphics ag;
reed@google.comd4dfd102011-01-18 21:05:42 +0000555
reed@android.com8015dd82009-06-21 00:49:18 +0000556 const char* writePath = NULL; // if non-null, where we write the originals
557 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000558 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@google.comece2b022011-07-25 14:28:57 +0000559 const char* matchStr = NULL;
reed@android.com8015dd82009-06-21 00:49:18 +0000560
reed@google.comab973972011-09-19 19:01:38 +0000561 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000562 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000563 bool doSerialize = false;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000564 bool useMesa = false;
565
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000566 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000567 char* const* stop = argv + argc;
568 for (++argv; argv < stop; ++argv) {
569 if (strcmp(*argv, "-w") == 0) {
570 argv++;
571 if (argv < stop && **argv) {
572 writePath = *argv;
573 }
574 } else if (strcmp(*argv, "-r") == 0) {
575 argv++;
576 if (argv < stop && **argv) {
577 readPath = *argv;
578 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000579 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000580 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000581 if (argv < stop && **argv) {
582 diffPath = *argv;
583 }
reed@google.comb8b09832011-05-26 15:57:56 +0000584 } else if (strcmp(*argv, "--noreplay") == 0) {
585 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000586 } else if (strcmp(*argv, "--nopdf") == 0) {
587 doPDF = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000588 } else if (strcmp(*argv, "--serialize") == 0) {
589 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000590 } else if (strcmp(*argv, "--match") == 0) {
591 ++argv;
592 if (argv < stop && **argv) {
593 matchStr = *argv;
594 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000595#if SK_MESA
596 } else if (strcmp(*argv, "--mesagl") == 0) {
597 useMesa = true;
598#endif
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000599 } else {
600 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000601 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000602 }
603 }
604 if (argv != stop) {
605 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000606 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000607 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000608
bsalomon@google.com39149582011-06-13 21:55:32 +0000609 int maxW = -1;
610 int maxH = -1;
611 Iter iter;
612 GM* gm;
613 while ((gm = iter.next()) != NULL) {
614 SkISize size = gm->getISize();
615 maxW = SkMax32(size.width(), maxW);
616 maxH = SkMax32(size.height(), maxH);
617 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000618 // setup a GL context for drawing offscreen
bsalomon@google.com373a6632011-10-19 20:43:20 +0000619 SkAutoTUnref<SkGLContext> glContext;
620#if SK_MESA
621 if (useMesa) {
622 glContext.reset(new SkMesaGLContext());
623 } else
624#endif
625 {
626 glContext.reset(new SkNativeGLContext());
627 }
628
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000629 GrRenderTarget* rt = NULL;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000630 if (glContext.get()->init(maxW, maxH)) {
631 GrPlatform3DContext ctx =
632 reinterpret_cast<GrPlatform3DContext>(glContext.get()->gl());
633 gGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000634 if (NULL != gGrContext) {
635 GrPlatformSurfaceDesc desc;
636 desc.reset();
637 desc.fConfig = kRGBA_8888_GrPixelConfig;
638 desc.fWidth = maxW;
639 desc.fHeight = maxH;
640 desc.fStencilBits = 8;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000641 desc.fPlatformRenderTarget = glContext.get()->getFBOID();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000642 desc.fSurfaceType = kRenderTarget_GrPlatformSurfaceType;
643 rt = static_cast<GrRenderTarget*>(gGrContext->createPlatformSurface(desc));
644 if (NULL == rt) {
645 gGrContext->unref();
646 gGrContext = NULL;
647 }
648 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000649 } else {
650 fprintf(stderr, "could not create GL context.\n");
reed@google.com37df17d2010-12-23 20:20:51 +0000651 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000652
reed@android.com00f883e2010-12-14 17:46:14 +0000653 if (readPath) {
654 fprintf(stderr, "reading from %s\n", readPath);
655 } else if (writePath) {
656 fprintf(stderr, "writing to %s\n", writePath);
657 }
658
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000659 // Accumulate success of all tests so we can flag error in any
660 // one with the return value.
bsalomon@google.com39149582011-06-13 21:55:32 +0000661 iter.reset();
tomhudson@google.comea325432011-06-09 20:30:03 +0000662 bool overallSuccess = true;
reed@android.com00dae862009-06-10 15:38:48 +0000663 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000664 const char* shortName = gm->shortName();
665 if (matchStr && !strstr(shortName, matchStr)) {
666 SkDELETE(gm);
667 continue;
668 }
669
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000670 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000671 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000672 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000673 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000674
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000675 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
reed@google.comfbc21172011-09-19 19:08:33 +0000676 uint32_t gmFlags = gm->getFlags();
677
bungeman@google.com64e011a2011-09-19 19:31:04 +0000678 if ((kPDF_Backend == gRec[i].fBackend) &&
679 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
680 {
reed@google.comab973972011-09-19 19:01:38 +0000681 continue;
682 }
683
tomhudson@google.comea325432011-06-09 20:30:03 +0000684 bool testSuccess = test_drawing(gm, gRec[i],
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000685 writePath, readPath, diffPath, gGrContext,
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000686 rt, &forwardRenderedBitmap);
tomhudson@google.comea325432011-06-09 20:30:03 +0000687 overallSuccess &= testSuccess;
reed@android.com00dae862009-06-10 15:38:48 +0000688
reed@google.comfbc21172011-09-19 19:08:33 +0000689 if (doReplay && testSuccess && !(gmFlags & GM::kSkipPicture_Flag)) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000690 testSuccess = test_picture_playback(gm, gRec[i],
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000691 forwardRenderedBitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000692 readPath, diffPath);
693 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000694 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000695
tomhudson@google.comea325432011-06-09 20:30:03 +0000696 if (doSerialize && testSuccess) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000697 testSuccess &= test_picture_serialization(gm, gRec[i],
698 forwardRenderedBitmap,
699 readPath, diffPath);
700 overallSuccess &= testSuccess;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000701 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000702 }
reed@android.com00dae862009-06-10 15:38:48 +0000703 SkDELETE(gm);
704 }
tomhudson@google.comea325432011-06-09 20:30:03 +0000705 if (false == overallSuccess) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000706 return -1;
707 }
reed@android.com00dae862009-06-10 15:38:48 +0000708 return 0;
709}
reed@android.comdd0ac282009-06-20 02:38:16 +0000710
711///////////////////////////////////////////////////////////////////////////////
712
713using namespace skiagm;
714
715GM::GM() {}
716GM::~GM() {}
717
718void GM::draw(SkCanvas* canvas) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000719 this->onDraw(canvas);
reed@android.comdd0ac282009-06-20 02:38:16 +0000720}