blob: 8f78c41d2485db173e8b20638e281676672dd086 [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
reed@google.com8923c6c2011-11-08 14:59:38 +000026extern bool gSkSuppressFontCachePurgeSpew;
27
reed@google.com07700442010-12-20 19:46:07 +000028#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000029 #include "SkPDFDevice.h"
30 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000031#endif
reed@android.com00dae862009-06-10 15:38:48 +000032
bungeman@google.comb29c8832011-10-10 13:19:10 +000033#ifdef SK_SUPPORT_XPS
34 #include "SkXPSDevice.h"
35#endif
36
reed@google.com46cce912011-06-29 12:54:46 +000037#ifdef SK_BUILD_FOR_MAC
38 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000039 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000040#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000041 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000042#endif
43
epoger@google.comc7cf2b32011-12-28 19:31:01 +000044typedef int ErrorBitfield;
45const static ErrorBitfield ERROR_NONE = 0x00;
46const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
47const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
48const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
49const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
50const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
51
reed@android.com00dae862009-06-10 15:38:48 +000052using namespace skiagm;
53
reed@android.com00dae862009-06-10 15:38:48 +000054class Iter {
55public:
56 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000057 this->reset();
58 }
59
60 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000061 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000062 }
reed@google.comd4dfd102011-01-18 21:05:42 +000063
reed@android.comdd0ac282009-06-20 02:38:16 +000064 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000065 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000066 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000067 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000068 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000069 }
70 return NULL;
71 }
reed@google.comd4dfd102011-01-18 21:05:42 +000072
reed@android.com00dae862009-06-10 15:38:48 +000073 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000074 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000075 int count = 0;
76 while (reg) {
77 count += 1;
78 reg = reg->next();
79 }
80 return count;
81 }
reed@google.comd4dfd102011-01-18 21:05:42 +000082
reed@android.com00dae862009-06-10 15:38:48 +000083private:
84 const GMRegistry* fReg;
85};
86
reed@android.com8015dd82009-06-21 00:49:18 +000087static SkString make_name(const char shortName[], const char configName[]) {
88 SkString name(shortName);
89 name.appendf("_%s", configName);
90 return name;
91}
92
tomhudson@google.com9875dd12011-04-25 15:49:53 +000093static SkString make_filename(const char path[],
94 const char pathSuffix[],
95 const SkString& name,
96 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +000097 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +000098 if (filename.endsWith("/")) {
99 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000100 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000101 filename.append(pathSuffix);
102 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000103 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000104 return filename;
105}
106
reed@android.comb9b9a182009-07-08 02:54:47 +0000107/* since PNG insists on unpremultiplying our alpha, we take no precision chances
108 and force all pixels to be 100% opaque, otherwise on compare we may not get
109 a perfect match.
110 */
111static void force_all_opaque(const SkBitmap& bitmap) {
112 SkAutoLockPixels lock(bitmap);
113 for (int y = 0; y < bitmap.height(); y++) {
114 for (int x = 0; x < bitmap.width(); x++) {
115 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
116 }
117 }
118}
119
120static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
121 SkBitmap copy;
122 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
123 force_all_opaque(copy);
124 return SkImageEncoder::EncodeFile(path.c_str(), copy,
125 SkImageEncoder::kPNG_Type, 100);
126}
127
reed@google.com3d3f0922010-12-20 21:10:29 +0000128static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000129 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
130 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
131 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
132 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000133}
134
135static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000136 SkBitmap* diff) {
137 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000138
139 const int w = target.width();
140 const int h = target.height();
141 for (int y = 0; y < h; y++) {
142 for (int x = 0; x < w; x++) {
143 SkPMColor c0 = *base.getAddr32(x, y);
144 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000145 SkPMColor d = 0;
146 if (c0 != c1) {
147 d = compute_diff_pmcolor(c0, c1);
148 }
149 *diff->getAddr32(x, y) = d;
150 }
151 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000152}
153
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000154static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
155 const SkString& name,
156 const char* renderModeDescriptor,
157 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000158 SkBitmap copy;
159 const SkBitmap* bm = &target;
160 if (target.config() != SkBitmap::kARGB_8888_Config) {
161 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
162 bm = &copy;
163 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000164 SkBitmap baseCopy;
165 const SkBitmap* bp = &base;
166 if (base.config() != SkBitmap::kARGB_8888_Config) {
167 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
168 bp = &baseCopy;
169 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000170
171 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000172 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000173
174 const int w = bm->width();
175 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000176 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000177 SkDebugf(
178"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
179 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000180 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000181 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000182 }
183
184 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000185 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000186
187 for (int y = 0; y < h; y++) {
188 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000189 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000190 SkPMColor c1 = *bm->getAddr32(x, y);
191 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000192 SkDebugf(
193"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
194 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000195
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000196 if (diff) {
197 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
198 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000199 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000200 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000201 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000202 }
203 }
204 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000205
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000206 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000207 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000208}
reed@android.com00dae862009-06-10 15:38:48 +0000209
bungeman@google.comb29c8832011-10-10 13:19:10 +0000210static bool write_document(const SkString& path,
211 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000212 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000213 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000214 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000215}
216
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000217enum Backend {
218 kRaster_Backend,
219 kGPU_Backend,
220 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000221 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000222};
223
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000224struct ConfigData {
225 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000226 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000227 const char* fName;
228};
229
230/// Returns true if processing should continue, false to skip the
231/// remainder of this config for this GM.
232//@todo thudson 22 April 2011 - could refactor this to take in
233// a factory to generate the context, always call readPixels()
234// (logically a noop for rasters, if wasted time), and thus collapse the
235// GPU special case and also let this be used for SkPicture testing.
236static void setup_bitmap(const ConfigData& gRec, SkISize& size,
237 SkBitmap* bitmap) {
238 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
239 bitmap->allocPixels();
240 bitmap->eraseColor(0);
241}
242
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000243static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
244 GrContext* context,
245 GrRenderTarget* rt,
246 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000247 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000248 setup_bitmap(gRec, size, bitmap);
249 SkCanvas canvas(*bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000250
251 if (gRec.fBackend == kRaster_Backend) {
252 gm->draw(&canvas);
253 } else { // GPU
254 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000255 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000256 }
reed@google.comaf951c92011-06-16 19:10:39 +0000257 SkGpuCanvas gc(context, rt);
258 gc.setDevice(new SkGpuDevice(context, rt))->unref();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000259 gm->draw(&gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000260 // the device is as large as the current rendertarget, so we explicitly
261 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000262 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000263 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
264 size.fHeight);
265 gc.readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000266 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000267 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000268}
269
270static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
271 SkPicture* pict, SkBitmap* bitmap) {
272 SkISize size = gm->getISize();
273 setup_bitmap(gRec, size, bitmap);
274 SkCanvas canvas(*bitmap);
275 canvas.drawPicture(*pict);
276}
277
278static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
279#ifdef SK_SUPPORT_PDF
280 SkISize size = gm->getISize();
281 SkMatrix identity;
282 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000283 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000284 SkAutoUnref aur(dev);
285
286 SkCanvas c(dev);
287 gm->draw(&c);
288
289 SkPDFDocument doc;
290 doc.appendPage(dev);
291 doc.emitPDF(&pdf);
292#endif
293}
294
bungeman@google.comb29c8832011-10-10 13:19:10 +0000295static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
296#ifdef SK_SUPPORT_XPS
297 SkISize size = gm->getISize();
298
299 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
300 SkIntToScalar(size.height()));
301 static const double inchesPerMeter = 10000.0 / 254.0;
302 static const double upm = 72 * inchesPerMeter;
303 SkVector unitsPerMeter = SkPoint::Make(SkDoubleToScalar(upm),
304 SkDoubleToScalar(upm));
305 static const double ppm = 200 * inchesPerMeter;
306 SkVector pixelsPerMeter = SkPoint::Make(SkDoubleToScalar(ppm),
307 SkDoubleToScalar(ppm));
308
309 SkXPSDevice* dev = new SkXPSDevice();
310 SkAutoUnref aur(dev);
311
312 SkCanvas c(dev);
313 dev->beginPortfolio(&xps);
314 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
315 gm->draw(&c);
316 dev->endSheet();
317 dev->endPortfolio();
318
319#endif
320}
321
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000322static ErrorBitfield write_reference_image(const ConfigData& gRec,
323 const char writePath [],
324 const char renderModeDescriptor [],
325 const SkString& name,
326 SkBitmap& bitmap,
327 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000328 SkString path;
329 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000330 if (gRec.fBackend == kRaster_Backend ||
331 gRec.fBackend == kGPU_Backend ||
332 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
333
tomhudson@google.comea325432011-06-09 20:30:03 +0000334 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000335 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000336 }
337 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000338 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000339 success = write_document(path, *document);
340 }
341 if (kXPS_Backend == gRec.fBackend) {
342 path = make_filename(writePath, renderModeDescriptor, name, "xps");
343 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000344 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000345 if (success) {
346 return ERROR_NONE;
347 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000348 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000349 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000350 }
351}
352
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000353static ErrorBitfield compare_to_reference_image(const SkString& name,
354 SkBitmap &bitmap,
355 const SkBitmap& comparisonBitmap,
356 const char diffPath [],
357 const char renderModeDescriptor []) {
358 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000359 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000360 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
361 diffPath ? &diffBitmap : NULL);
362 if ((ERROR_NONE == errors) && diffPath) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000363 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000364 if (!write_bitmap(diffName, diffBitmap)) {
365 errors |= ERROR_WRITING_REFERENCE_IMAGE;
366 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000367 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000368 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000369}
370
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000371static ErrorBitfield compare_to_reference_image(const char readPath [],
372 const SkString& name,
373 SkBitmap &bitmap,
374 const char diffPath [],
375 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000376 SkString path = make_filename(readPath, "", name, "png");
377 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000378 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
379 SkBitmap::kARGB_8888_Config,
380 SkImageDecoder::kDecodePixels_Mode, NULL)) {
381 return compare_to_reference_image(name, bitmap,
382 orig, diffPath,
383 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000384 } else {
385 fprintf(stderr, "FAILED to read %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000386 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000387 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000388}
389
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000390static ErrorBitfield handle_test_results(GM* gm,
391 const ConfigData& gRec,
392 const char writePath [],
393 const char readPath [],
394 const char diffPath [],
395 const char renderModeDescriptor [],
396 SkBitmap& bitmap,
397 SkDynamicMemoryWStream* pdf,
398 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000399 SkString name = make_name(gm->shortName(), gRec.fName);
400
401 if (writePath) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000402 return write_reference_image(gRec, writePath, renderModeDescriptor,
403 name, bitmap, pdf);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000404 } else if (readPath && (
405 gRec.fBackend == kRaster_Backend ||
406 gRec.fBackend == kGPU_Backend ||
407 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000408 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000409 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000410 } else if (comparisonBitmap) {
411 return compare_to_reference_image(name, bitmap,
412 *comparisonBitmap, diffPath,
413 renderModeDescriptor);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000414 } else {
415 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000416 }
417}
418
419static SkPicture* generate_new_picture(GM* gm) {
420 // Pictures are refcounted so must be on heap
421 SkPicture* pict = new SkPicture;
422 SkCanvas* cv = pict->beginRecording(1000, 1000);
423 gm->draw(cv);
424 pict->endRecording();
425
426 return pict;
427}
428
429static SkPicture* stream_to_new_picture(const SkPicture& src) {
430
431 // To do in-memory commiunications with a stream, we need to:
432 // * create a dynamic memory stream
433 // * copy it into a buffer
434 // * create a read stream from it
435 // ?!?!
436
437 SkDynamicMemoryWStream storage;
438 src.serialize(&storage);
439
440 int streamSize = storage.getOffset();
441 SkAutoMalloc dstStorage(streamSize);
442 void* dst = dstStorage.get();
443 //char* dst = new char [streamSize];
444 //@todo thudson 22 April 2011 when can we safely delete [] dst?
445 storage.copyTo(dst);
446 SkMemoryStream pictReadback(dst, streamSize);
447 SkPicture* retval = new SkPicture (&pictReadback);
448 return retval;
449}
450
451// Test: draw into a bitmap or pdf.
452// Depending on flags, possibly compare to an expected image
453// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000454static ErrorBitfield test_drawing(GM* gm,
455 const ConfigData& gRec,
456 const char writePath [],
457 const char readPath [],
458 const char diffPath [],
459 GrContext* context,
460 GrRenderTarget* rt,
461 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000462 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000463
464 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000465 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000466 // Early exit if we can't generate the image.
467 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap);
468 if (ERROR_NONE != errors) {
469 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000470 }
reed@google.com46cce912011-06-29 12:54:46 +0000471 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000472 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000473#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000474 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000475 SkMemoryStream stream(data.data(), data.size());
476 SkPDFDocumentToBitmap(&stream, bitmap);
477#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000478 } else if (gRec.fBackend == kXPS_Backend) {
479 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000480 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000481 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000482 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000483}
484
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000485static ErrorBitfield test_picture_playback(GM* gm,
486 const ConfigData& gRec,
487 const SkBitmap& comparisonBitmap,
488 const char readPath [],
489 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000490 SkPicture* pict = generate_new_picture(gm);
491 SkAutoUnref aur(pict);
492
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000493 if (kRaster_Backend == gRec.fBackend) {
494 SkBitmap bitmap;
495 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000496 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
497 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000498 } else {
499 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000500 }
501}
502
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000503static ErrorBitfield test_picture_serialization(GM* gm,
504 const ConfigData& gRec,
505 const SkBitmap& comparisonBitmap,
506 const char readPath [],
507 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000508 SkPicture* pict = generate_new_picture(gm);
509 SkAutoUnref aurp(pict);
510 SkPicture* repict = stream_to_new_picture(*pict);
511 SkAutoUnref aurr(repict);
512
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000513 if (kRaster_Backend == gRec.fBackend) {
514 SkBitmap bitmap;
515 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000516 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
517 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000518 } else {
519 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000520 }
521}
522
523static void usage(const char * argv0) {
524 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000525 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000526 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000527 SkDebugf(
528" readPath: directory to read reference images from;\n"
529" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000530 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000531 SkDebugf(" --replay: exercise SkPicture replay.\n");
532 SkDebugf(
533" --serialize: exercise SkPicture serialization & deserialization.\n");
reed@google.come6a5c4d2011-07-25 14:30:54 +0000534 SkDebugf(" --match foo will only run tests that substring match foo.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000535#if SK_MESA
536 SkDebugf(" --mesagl will run using the osmesa sw gl rasterizer.\n");
537#endif
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000538}
539
540static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000541 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
542 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
543 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000544#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000545 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000546#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000547#ifdef SK_SUPPORT_PDF
548 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
549#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000550#ifdef SK_SUPPORT_XPS
551 { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" },
552#endif
reed@android.com00dae862009-06-10 15:38:48 +0000553};
554
reed@google.comb2a51622011-10-31 16:30:04 +0000555static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
556 if (0 == array.count()) {
557 // no names, so don't skip anything
558 return false;
559 }
560 for (int i = 0; i < array.count(); ++i) {
561 if (strstr(name, array[i])) {
562 // found the name, so don't skip
563 return false;
564 }
565 }
566 return true;
567}
568
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000569namespace skiagm {
570static GrContext* gGrContext;
571GrContext* GetGr() {
572 return gGrContext;
573}
574}
575
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000576int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000577 SkAutoGraphics ag;
reed@google.com8923c6c2011-11-08 14:59:38 +0000578 // we don't need to see this during a run
579 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000580
reed@android.com8015dd82009-06-21 00:49:18 +0000581 const char* writePath = NULL; // if non-null, where we write the originals
582 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000583 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000584
reed@google.comb2a51622011-10-31 16:30:04 +0000585 SkTDArray<const char*> fMatches;
586
reed@google.comab973972011-09-19 19:01:38 +0000587 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000588 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000589 bool doSerialize = false;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000590 bool useMesa = false;
591
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000592 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000593 char* const* stop = argv + argc;
594 for (++argv; argv < stop; ++argv) {
595 if (strcmp(*argv, "-w") == 0) {
596 argv++;
597 if (argv < stop && **argv) {
598 writePath = *argv;
599 }
600 } else if (strcmp(*argv, "-r") == 0) {
601 argv++;
602 if (argv < stop && **argv) {
603 readPath = *argv;
604 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000605 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000606 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000607 if (argv < stop && **argv) {
608 diffPath = *argv;
609 }
reed@google.comb8b09832011-05-26 15:57:56 +0000610 } else if (strcmp(*argv, "--noreplay") == 0) {
611 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000612 } else if (strcmp(*argv, "--nopdf") == 0) {
613 doPDF = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000614 } else if (strcmp(*argv, "--serialize") == 0) {
615 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000616 } else if (strcmp(*argv, "--match") == 0) {
617 ++argv;
618 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000619 // just record the ptr, no need for a deep copy
620 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000621 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000622#if SK_MESA
623 } else if (strcmp(*argv, "--mesagl") == 0) {
624 useMesa = true;
625#endif
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000626 } else {
627 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000628 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000629 }
630 }
631 if (argv != stop) {
632 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000633 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000634 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000635
bsalomon@google.com39149582011-06-13 21:55:32 +0000636 int maxW = -1;
637 int maxH = -1;
638 Iter iter;
639 GM* gm;
640 while ((gm = iter.next()) != NULL) {
641 SkISize size = gm->getISize();
642 maxW = SkMax32(size.width(), maxW);
643 maxH = SkMax32(size.height(), maxH);
644 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000645 // setup a GL context for drawing offscreen
bsalomon@google.com373a6632011-10-19 20:43:20 +0000646 SkAutoTUnref<SkGLContext> glContext;
647#if SK_MESA
648 if (useMesa) {
649 glContext.reset(new SkMesaGLContext());
650 } else
651#endif
652 {
653 glContext.reset(new SkNativeGLContext());
654 }
655
bsalomon@google.com29d35012011-11-30 16:57:21 +0000656 GrPlatformRenderTargetDesc rtDesc;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000657 if (glContext.get()->init(maxW, maxH)) {
658 GrPlatform3DContext ctx =
659 reinterpret_cast<GrPlatform3DContext>(glContext.get()->gl());
660 gGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000661 if (NULL != gGrContext) {
bsalomon@google.com29d35012011-11-30 16:57:21 +0000662 rtDesc.fConfig = kSkia8888_PM_GrPixelConfig;
663 rtDesc.fStencilBits = 8;
664 rtDesc.fRenderTargetHandle = glContext.get()->getFBOID();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000665 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000666 } else {
667 fprintf(stderr, "could not create GL context.\n");
reed@google.com37df17d2010-12-23 20:20:51 +0000668 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000669
reed@android.com00f883e2010-12-14 17:46:14 +0000670 if (readPath) {
671 fprintf(stderr, "reading from %s\n", readPath);
672 } else if (writePath) {
673 fprintf(stderr, "writing to %s\n", writePath);
674 }
675
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000676 // Accumulate success of all tests.
677 int testsRun = 0;
678 int testsPassed = 0;
679 int testsFailed = 0;
680 int testsMissingReferenceImages = 0;
681
bsalomon@google.com39149582011-06-13 21:55:32 +0000682 iter.reset();
reed@android.com00dae862009-06-10 15:38:48 +0000683 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000684 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000685 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000686 SkDELETE(gm);
687 continue;
688 }
689
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000690 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000691 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000692 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000693 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000694
bsalomon@google.com29d35012011-11-30 16:57:21 +0000695 // Above we created an fbo for the context at maxW x maxH size.
696 // Here we lie about the size of the rt. We claim it is the size
697 // desired by the test. The reason is that rasterization may change
698 // slightly when the viewport dimensions change. Previously, whenever
699 // a new test was checked in that bumped maxW or maxH several images
700 // would slightly change.
701 rtDesc.fWidth = size.width();
702 rtDesc.fHeight = size.height();
703 SkAutoTUnref<GrRenderTarget> rt;
704 if (gGrContext) {
705 rt.reset(gGrContext->createPlatformRenderTarget(rtDesc));
706 }
reed@google.comfbc21172011-09-19 19:08:33 +0000707
bsalomon@google.com29d35012011-11-30 16:57:21 +0000708 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000709 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000710 uint32_t gmFlags = gm->getFlags();
bungeman@google.com64e011a2011-09-19 19:31:04 +0000711 if ((kPDF_Backend == gRec[i].fBackend) &&
712 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
713 {
reed@google.comab973972011-09-19 19:01:38 +0000714 continue;
715 }
716
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000717 // Now we know that we want to run this test and record its
718 // success or failure.
719 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000720
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000721 if ((ERROR_NONE == testErrors) &&
722 (kGPU_Backend == gRec[i].fBackend) &&
723 (NULL == rt.get())) {
724 fprintf(stderr, "Could not create render target for gpu.\n");
725 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000726 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000727
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000728 if (ERROR_NONE == testErrors) {
729 testErrors |= test_drawing(gm, gRec[i],
730 writePath, readPath, diffPath,
731 gGrContext,
732 rt.get(), &forwardRenderedBitmap);
733 }
734
735 if ((ERROR_NONE == testErrors) && doReplay &&
736 !(gmFlags & GM::kSkipPicture_Flag)) {
737 testErrors |= test_picture_playback(gm, gRec[i],
738 forwardRenderedBitmap,
739 readPath, diffPath);
740 }
741
742 if ((ERROR_NONE == testErrors) && doSerialize) {
743 testErrors |= test_picture_serialization(gm, gRec[i],
744 forwardRenderedBitmap,
745 readPath, diffPath);
746 }
747
748 // Update overall results.
749 // We only tabulate the particular error types that we currently
750 // care about (e.g., missing reference images). Later on, if we
751 // want to also tabulate pixel mismatches vs dimension mistmatches
752 // (or whatever else), we can do so.
753 testsRun++;
754 if (ERROR_NONE == testErrors) {
755 testsPassed++;
756 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
757 testsMissingReferenceImages++;
758 } else {
759 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000760 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000761 }
reed@android.com00dae862009-06-10 15:38:48 +0000762 SkDELETE(gm);
763 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000764 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
765 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
766 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +0000767}