blob: 3855f2f2dce5c0a36432aeadb71007c97a285d37 [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"
epoger@google.com7bc13a62012-02-14 14:53:59 +00009#include "system_preferences.h"
bsalomon@google.com7361f542012-04-19 19:15:35 +000010#include "GrContextFactory.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000011#include "GrRenderTarget.h"
reed@android.comb9b9a182009-07-08 02:54:47 +000012#include "SkColorPriv.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000013#include "SkData.h"
junov@google.com4370aed2012-01-18 16:21:08 +000014#include "SkDeferredCanvas.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000015#include "SkDevice.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000016#include "SkGpuCanvas.h"
17#include "SkGpuDevice.h"
reed@android.com8015dd82009-06-21 00:49:18 +000018#include "SkGraphics.h"
19#include "SkImageDecoder.h"
20#include "SkImageEncoder.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000021#include "SkPicture.h"
reed@google.com07700442010-12-20 19:46:07 +000022#include "SkStream.h"
reed@google.com07700442010-12-20 19:46:07 +000023
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000024static bool gForceBWtext;
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
epoger@google.come3cc2eb2012-01-18 20:11:13 +000033// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
34// stop writing out XPS-format image baselines in gm.
35#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000036#ifdef SK_SUPPORT_XPS
37 #include "SkXPSDevice.h"
38#endif
39
reed@google.com46cce912011-06-29 12:54:46 +000040#ifdef SK_BUILD_FOR_MAC
41 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000042 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000043#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000044 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000045#endif
46
epoger@google.comc7cf2b32011-12-28 19:31:01 +000047typedef int ErrorBitfield;
48const static ErrorBitfield ERROR_NONE = 0x00;
49const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
50const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
51const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
52const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
53const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
54
reed@android.com00dae862009-06-10 15:38:48 +000055using namespace skiagm;
56
reed@android.com00dae862009-06-10 15:38:48 +000057class Iter {
58public:
59 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000060 this->reset();
61 }
62
63 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000064 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000065 }
reed@google.comd4dfd102011-01-18 21:05:42 +000066
reed@android.comdd0ac282009-06-20 02:38:16 +000067 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000068 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000069 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000070 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000071 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000072 }
73 return NULL;
74 }
reed@google.comd4dfd102011-01-18 21:05:42 +000075
reed@android.com00dae862009-06-10 15:38:48 +000076 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000077 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000078 int count = 0;
79 while (reg) {
80 count += 1;
81 reg = reg->next();
82 }
83 return count;
84 }
reed@google.comd4dfd102011-01-18 21:05:42 +000085
reed@android.com00dae862009-06-10 15:38:48 +000086private:
87 const GMRegistry* fReg;
88};
89
reed@android.com8015dd82009-06-21 00:49:18 +000090static SkString make_name(const char shortName[], const char configName[]) {
91 SkString name(shortName);
92 name.appendf("_%s", configName);
93 return name;
94}
95
tomhudson@google.com9875dd12011-04-25 15:49:53 +000096static SkString make_filename(const char path[],
97 const char pathSuffix[],
98 const SkString& name,
99 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000100 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000101 if (filename.endsWith("/")) {
102 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000103 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000104 filename.append(pathSuffix);
105 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000106 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000107 return filename;
108}
109
reed@android.comb9b9a182009-07-08 02:54:47 +0000110/* since PNG insists on unpremultiplying our alpha, we take no precision chances
111 and force all pixels to be 100% opaque, otherwise on compare we may not get
112 a perfect match.
113 */
114static void force_all_opaque(const SkBitmap& bitmap) {
115 SkAutoLockPixels lock(bitmap);
116 for (int y = 0; y < bitmap.height(); y++) {
117 for (int x = 0; x < bitmap.width(); x++) {
118 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
119 }
120 }
121}
122
123static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
124 SkBitmap copy;
125 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
126 force_all_opaque(copy);
127 return SkImageEncoder::EncodeFile(path.c_str(), copy,
128 SkImageEncoder::kPNG_Type, 100);
129}
130
reed@google.com3d3f0922010-12-20 21:10:29 +0000131static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000132 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
133 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
134 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
135 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000136}
137
138static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000139 SkBitmap* diff) {
140 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000141
142 const int w = target.width();
143 const int h = target.height();
144 for (int y = 0; y < h; y++) {
145 for (int x = 0; x < w; x++) {
146 SkPMColor c0 = *base.getAddr32(x, y);
147 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000148 SkPMColor d = 0;
149 if (c0 != c1) {
150 d = compute_diff_pmcolor(c0, c1);
151 }
152 *diff->getAddr32(x, y) = d;
153 }
154 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000155}
156
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000157static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
158 const SkString& name,
159 const char* renderModeDescriptor,
160 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000161 SkBitmap copy;
162 const SkBitmap* bm = &target;
163 if (target.config() != SkBitmap::kARGB_8888_Config) {
164 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
165 bm = &copy;
166 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000167 SkBitmap baseCopy;
168 const SkBitmap* bp = &base;
169 if (base.config() != SkBitmap::kARGB_8888_Config) {
170 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
171 bp = &baseCopy;
172 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000173
174 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000175 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000176
177 const int w = bm->width();
178 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000179 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000180 SkDebugf(
181"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
182 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000183 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000184 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000185 }
186
187 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000188 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000189
190 for (int y = 0; y < h; y++) {
191 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000192 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000193 SkPMColor c1 = *bm->getAddr32(x, y);
194 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000195 SkDebugf(
196"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
197 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000198
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000199 if (diff) {
200 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
201 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000202 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000203 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000204 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000205 }
206 }
207 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000208
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000209 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000210 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000211}
reed@android.com00dae862009-06-10 15:38:48 +0000212
bungeman@google.comb29c8832011-10-10 13:19:10 +0000213static bool write_document(const SkString& path,
214 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000215 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000216 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000217 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000218}
219
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000220enum Backend {
221 kRaster_Backend,
222 kGPU_Backend,
223 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000224 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000225};
226
bsalomon@google.com7361f542012-04-19 19:15:35 +0000227enum ConfigFlags {
228 kNone_ConfigFlag = 0x0,
229 /* Write GM images if a write path is provided. */
230 kWrite_ConfigFlag = 0x1,
231 /* Read comparison GM images if a read path is provided. */
232 kRead_ConfigFlag = 0x2,
233 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
234};
235
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000236struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000237 SkBitmap::Config fConfig;
238 Backend fBackend;
239 GrContextFactory::GLContextType fGLContextType; // GPU backend only
240 int fSampleCnt; // GPU backend only
241 ConfigFlags fFlags;
242 const char* fName;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000243};
244
245/// Returns true if processing should continue, false to skip the
246/// remainder of this config for this GM.
247//@todo thudson 22 April 2011 - could refactor this to take in
248// a factory to generate the context, always call readPixels()
249// (logically a noop for rasters, if wasted time), and thus collapse the
250// GPU special case and also let this be used for SkPicture testing.
251static void setup_bitmap(const ConfigData& gRec, SkISize& size,
252 SkBitmap* bitmap) {
253 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
254 bitmap->allocPixels();
255 bitmap->eraseColor(0);
256}
257
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000258#include "SkDrawFilter.h"
259class BWTextDrawFilter : public SkDrawFilter {
260public:
261 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
262};
263void BWTextDrawFilter::filter(SkPaint* p, Type t) {
264 if (kText_Type == t) {
265 p->setAntiAlias(false);
266 }
267}
268
269static void installFilter(SkCanvas* canvas) {
270 if (gForceBWtext) {
271 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
272 }
273}
274
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000275static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
276 if (!isPDF) {
277 canvas->setMatrix(gm->getInitialTransform());
278 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000279 installFilter(canvas);
280 gm->draw(canvas);
281 canvas->setDrawFilter(NULL);
282}
283
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000284static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
285 GrContext* context,
286 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000287 SkBitmap* bitmap,
288 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000289 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000290 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000291
292 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000293 SkCanvas* canvas;
294 if (deferred) {
295 canvas = new SkDeferredCanvas;
296 canvas->setDevice(new SkDevice(*bitmap))->unref();
297 } else {
298 canvas = new SkCanvas(*bitmap);
299 }
300 SkAutoUnref canvasUnref(canvas);
301 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000302 canvas->flush();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000303 } else { // GPU
304 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000305 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000306 }
junov@google.com4370aed2012-01-18 16:21:08 +0000307 SkCanvas* gc;
308 if (deferred) {
309 gc = new SkDeferredCanvas;
310 } else {
311 gc = new SkGpuCanvas(context, rt);
312 }
313 SkAutoUnref gcUnref(gc);
314 gc->setDevice(new SkGpuDevice(context, rt))->unref();
315 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000316 // the device is as large as the current rendertarget, so we explicitly
317 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000318 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000319 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
320 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000321 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000322 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000323 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000324}
325
326static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
327 SkPicture* pict, SkBitmap* bitmap) {
328 SkISize size = gm->getISize();
329 setup_bitmap(gRec, size, bitmap);
330 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000331 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000332 canvas.drawPicture(*pict);
333}
334
335static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
336#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000337 SkMatrix initialTransform = gm->getInitialTransform();
338 SkISize pageSize = gm->getISize();
339 SkPDFDevice* dev = NULL;
340 if (initialTransform.isIdentity()) {
341 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
342 } else {
343 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
344 SkIntToScalar(pageSize.height()));
345 initialTransform.mapRect(&content);
346 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
347 SkIntToScalar(pageSize.height()));
348 SkISize contentSize =
349 SkISize::Make(SkScalarRoundToInt(content.width()),
350 SkScalarRoundToInt(content.height()));
351 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
352 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000353 SkAutoUnref aur(dev);
354
355 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000356 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000357
358 SkPDFDocument doc;
359 doc.appendPage(dev);
360 doc.emitPDF(&pdf);
361#endif
362}
363
bungeman@google.comb29c8832011-10-10 13:19:10 +0000364static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
365#ifdef SK_SUPPORT_XPS
366 SkISize size = gm->getISize();
367
368 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
369 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000370 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
371 static const SkScalar upm = 72 * inchesPerMeter;
372 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
373 static const SkScalar ppm = 200 * inchesPerMeter;
374 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000375
376 SkXPSDevice* dev = new SkXPSDevice();
377 SkAutoUnref aur(dev);
378
379 SkCanvas c(dev);
380 dev->beginPortfolio(&xps);
381 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000382 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000383 dev->endSheet();
384 dev->endPortfolio();
385
386#endif
387}
388
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000389static ErrorBitfield write_reference_image(const ConfigData& gRec,
390 const char writePath [],
391 const char renderModeDescriptor [],
392 const SkString& name,
393 SkBitmap& bitmap,
394 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000395 SkString path;
396 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000397 if (gRec.fBackend == kRaster_Backend ||
398 gRec.fBackend == kGPU_Backend ||
399 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
400
tomhudson@google.comea325432011-06-09 20:30:03 +0000401 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000402 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000403 }
404 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000405 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000406 success = write_document(path, *document);
407 }
408 if (kXPS_Backend == gRec.fBackend) {
409 path = make_filename(writePath, renderModeDescriptor, name, "xps");
410 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000411 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000412 if (success) {
413 return ERROR_NONE;
414 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000415 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000416 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000417 }
418}
419
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000420static ErrorBitfield compare_to_reference_image(const SkString& name,
421 SkBitmap &bitmap,
422 const SkBitmap& comparisonBitmap,
423 const char diffPath [],
424 const char renderModeDescriptor []) {
425 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000426 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000427 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
428 diffPath ? &diffBitmap : NULL);
reed@google.com8e529b72012-04-09 20:20:10 +0000429 if ((ERROR_NONE != errors) && diffPath) {
430 // write out the generated image
431 SkString genName = make_filename(diffPath, "", name, "png");
432 if (!write_bitmap(genName, bitmap)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000433 errors |= ERROR_WRITING_REFERENCE_IMAGE;
434 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000435 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000436 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000437}
438
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000439static ErrorBitfield compare_to_reference_image(const char readPath [],
440 const SkString& name,
441 SkBitmap &bitmap,
442 const char diffPath [],
443 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000444 SkString path = make_filename(readPath, "", name, "png");
445 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000446 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
447 SkBitmap::kARGB_8888_Config,
448 SkImageDecoder::kDecodePixels_Mode, NULL)) {
449 return compare_to_reference_image(name, bitmap,
450 orig, diffPath,
451 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000452 } else {
453 fprintf(stderr, "FAILED to read %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000454 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000455 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000456}
457
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000458static ErrorBitfield handle_test_results(GM* gm,
459 const ConfigData& gRec,
460 const char writePath [],
461 const char readPath [],
462 const char diffPath [],
463 const char renderModeDescriptor [],
464 SkBitmap& bitmap,
465 SkDynamicMemoryWStream* pdf,
466 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000467 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000468 ErrorBitfield retval = ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000469
bsalomon@google.com7361f542012-04-19 19:15:35 +0000470 if (readPath && (gRec.fFlags & kRead_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000471 retval |= compare_to_reference_image(readPath, name, bitmap,
472 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000473 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000474 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000475 retval |= write_reference_image(gRec, writePath, renderModeDescriptor,
476 name, bitmap, pdf);
477 }
478 if (comparisonBitmap) {
479 retval |= compare_to_reference_image(name, bitmap,
480 *comparisonBitmap, diffPath,
481 renderModeDescriptor);
482 }
483 return retval;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000484}
485
486static SkPicture* generate_new_picture(GM* gm) {
487 // Pictures are refcounted so must be on heap
488 SkPicture* pict = new SkPicture;
489 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000490 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000491 pict->endRecording();
492
493 return pict;
494}
495
496static SkPicture* stream_to_new_picture(const SkPicture& src) {
497
498 // To do in-memory commiunications with a stream, we need to:
499 // * create a dynamic memory stream
500 // * copy it into a buffer
501 // * create a read stream from it
502 // ?!?!
503
504 SkDynamicMemoryWStream storage;
505 src.serialize(&storage);
506
507 int streamSize = storage.getOffset();
508 SkAutoMalloc dstStorage(streamSize);
509 void* dst = dstStorage.get();
510 //char* dst = new char [streamSize];
511 //@todo thudson 22 April 2011 when can we safely delete [] dst?
512 storage.copyTo(dst);
513 SkMemoryStream pictReadback(dst, streamSize);
514 SkPicture* retval = new SkPicture (&pictReadback);
515 return retval;
516}
517
518// Test: draw into a bitmap or pdf.
519// Depending on flags, possibly compare to an expected image
520// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000521static ErrorBitfield test_drawing(GM* gm,
522 const ConfigData& gRec,
523 const char writePath [],
524 const char readPath [],
525 const char diffPath [],
526 GrContext* context,
527 GrRenderTarget* rt,
528 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000529 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000530
531 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000532 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000533 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000534 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
535 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000536 if (ERROR_NONE != errors) {
537 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000538 }
reed@google.com46cce912011-06-29 12:54:46 +0000539 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000540 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000541#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000542 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000543 SkMemoryStream stream(data.data(), data.size());
544 SkPDFDocumentToBitmap(&stream, bitmap);
545#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000546 } else if (gRec.fBackend == kXPS_Backend) {
547 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000548 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000549 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000550 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000551}
552
junov@google.com4370aed2012-01-18 16:21:08 +0000553static ErrorBitfield test_deferred_drawing(GM* gm,
554 const ConfigData& gRec,
555 const SkBitmap& comparisonBitmap,
556 const char diffPath [],
557 GrContext* context,
558 GrRenderTarget* rt) {
559 SkDynamicMemoryWStream document;
560
561 if (gRec.fBackend == kRaster_Backend ||
562 gRec.fBackend == kGPU_Backend) {
563 SkBitmap bitmap;
564 // Early exit if we can't generate the image, but this is
565 // expected in some cases, so don't report a test failure.
566 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
567 return ERROR_NONE;
568 }
569 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
570 "-deferred", bitmap, NULL, &comparisonBitmap);
571 }
572 return ERROR_NONE;
573}
574
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000575static ErrorBitfield test_picture_playback(GM* gm,
576 const ConfigData& gRec,
577 const SkBitmap& comparisonBitmap,
578 const char readPath [],
579 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000580 SkPicture* pict = generate_new_picture(gm);
581 SkAutoUnref aur(pict);
582
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000583 if (kRaster_Backend == gRec.fBackend) {
584 SkBitmap bitmap;
585 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000586 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
587 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000588 } else {
589 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000590 }
591}
592
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000593static ErrorBitfield test_picture_serialization(GM* gm,
594 const ConfigData& gRec,
595 const SkBitmap& comparisonBitmap,
596 const char readPath [],
597 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000598 SkPicture* pict = generate_new_picture(gm);
599 SkAutoUnref aurp(pict);
600 SkPicture* repict = stream_to_new_picture(*pict);
601 SkAutoUnref aurr(repict);
602
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000603 if (kRaster_Backend == gRec.fBackend) {
604 SkBitmap bitmap;
605 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000606 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
607 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000608 } else {
609 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000610 }
611}
612
613static void usage(const char * argv0) {
junov@google.com77e498e2012-01-18 18:56:34 +0000614 SkDebugf(
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000615 "%s [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n"
616 " [--noreplay] [--serialize] [--forceBWtext] [--nopdf] \n"
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000617 " [--nodeferred] [--match substring] [--notexturecache]\n"
bsalomon@google.com7361f542012-04-19 19:15:35 +0000618 , argv0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000619 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000620 SkDebugf(
621" readPath: directory to read reference images from;\n"
622" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000623 SkDebugf(" diffPath: directory to write difference images in.\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000624 SkDebugf(" resourcePath: directory that stores image resources.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000625 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000626 SkDebugf(
627" --serialize: exercise SkPicture serialization & deserialization.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000628 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
629 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
630 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000631 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
twiz@google.come24a0792012-01-31 18:35:30 +0000632 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000633}
634
bsalomon@google.com7361f542012-04-19 19:15:35 +0000635static const GrContextFactory::GLContextType kDontCare_GLContextType =
636 GrContextFactory::kNative_GLContextType;
637
638// If the platform does not support writing PNGs of PDFs then there will be no
639// comparison images to read. However, we can always write the .pdf files
640static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
641 kWrite_ConfigFlag;
642
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000643static const ConfigData gRec[] = {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000644 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888" },
645 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444" },
646 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000647#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.com7361f542012-04-19 19:15:35 +0000648 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu" },
649 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16" },
650 /* The debug context does not generate images */
651 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "debug" },
652 #ifdef SK_ANGLE
653 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle" },
654 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16" },
655 #endif
656 #ifdef SK_MESA
657 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa" },
658 #endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000659#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000660#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000661 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
662 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps" },
663#endif
664#ifdef SK_SUPPORT_PDF
665 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf" },
bungeman@google.comb29c8832011-10-10 13:19:10 +0000666#endif
reed@android.com00dae862009-06-10 15:38:48 +0000667};
668
reed@google.comb2a51622011-10-31 16:30:04 +0000669static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
670 if (0 == array.count()) {
671 // no names, so don't skip anything
672 return false;
673 }
674 for (int i = 0; i < array.count(); ++i) {
675 if (strstr(name, array[i])) {
676 // found the name, so don't skip
677 return false;
678 }
679 }
680 return true;
681}
682
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000683namespace skiagm {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000684SkAutoTUnref<GrContext> gGrContext;
685/**
686 * Sets the global GrContext, accessible by indivual GMs
687 */
688void SetGr(GrContext* grContext) {
689 SkSafeRef(grContext);
690 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000691}
bsalomon@google.com7361f542012-04-19 19:15:35 +0000692
693/**
694 * Gets the global GrContext, can be called by GM tests.
695 */
696GrContext* GetGr() {
697 return gGrContext.get();
698}
699
700/**
701 * Sets the global GrContext and then resets it to its previous value at
702 * destruction.
703 */
704class AutoResetGr : SkNoncopyable {
705public:
706 AutoResetGr() : fOld(NULL) {}
707 void set(GrContext* context) {
708 SkASSERT(NULL == fOld);
709 fOld = GetGr();
710 SkSafeRef(fOld);
711 SetGr(context);
712 }
713 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
714private:
715 GrContext* fOld;
716};
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000717}
718
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000719int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000720 SkAutoGraphics ag;
reed@google.com8923c6c2011-11-08 14:59:38 +0000721 // we don't need to see this during a run
722 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000723
epoger@google.com7bc13a62012-02-14 14:53:59 +0000724 setSystemPreferences();
725
reed@android.com8015dd82009-06-21 00:49:18 +0000726 const char* writePath = NULL; // if non-null, where we write the originals
727 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000728 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000729 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000730
reed@google.comb2a51622011-10-31 16:30:04 +0000731 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000732
reed@google.comab973972011-09-19 19:01:38 +0000733 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000734 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000735 bool doSerialize = false;
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000736 bool useDebugGL = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000737 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000738 bool disableTextureCache = false;
739
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000740 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000741 char* const* stop = argv + argc;
742 for (++argv; argv < stop; ++argv) {
743 if (strcmp(*argv, "-w") == 0) {
744 argv++;
745 if (argv < stop && **argv) {
746 writePath = *argv;
747 }
748 } else if (strcmp(*argv, "-r") == 0) {
749 argv++;
750 if (argv < stop && **argv) {
751 readPath = *argv;
752 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000753 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000754 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000755 if (argv < stop && **argv) {
756 diffPath = *argv;
757 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000758 } else if (strcmp(*argv, "-i") == 0) {
759 argv++;
760 if (argv < stop && **argv) {
761 resourcePath = *argv;
762 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000763 } else if (strcmp(*argv, "--forceBWtext") == 0) {
764 gForceBWtext = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000765 } else if (strcmp(*argv, "--noreplay") == 0) {
766 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000767 } else if (strcmp(*argv, "--nopdf") == 0) {
768 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000769 } else if (strcmp(*argv, "--nodeferred") == 0) {
770 doDeferred = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000771 } else if (strcmp(*argv, "--serialize") == 0) {
772 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000773 } else if (strcmp(*argv, "--match") == 0) {
774 ++argv;
775 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000776 // just record the ptr, no need for a deep copy
777 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000778 }
twiz@google.come24a0792012-01-31 18:35:30 +0000779 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000780 disableTextureCache = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000781 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000782 usage(commandName);
783 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000784 }
785 }
786 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000787 usage(commandName);
788 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000789 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000790
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000791 GM::SetResourcePath(resourcePath);
792
bsalomon@google.com7361f542012-04-19 19:15:35 +0000793 GrContextFactory grFactory;
reed@google.com873cb1e2010-12-23 15:00:45 +0000794
reed@android.com00f883e2010-12-14 17:46:14 +0000795 if (readPath) {
796 fprintf(stderr, "reading from %s\n", readPath);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000797 }
798 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +0000799 fprintf(stderr, "writing to %s\n", writePath);
800 }
801
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000802 if (resourcePath) {
803 fprintf(stderr, "reading resources from %s\n", resourcePath);
804 }
805
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000806 // Accumulate success of all tests.
807 int testsRun = 0;
808 int testsPassed = 0;
809 int testsFailed = 0;
810 int testsMissingReferenceImages = 0;
811
twiz@google.come24a0792012-01-31 18:35:30 +0000812 if (disableTextureCache) {
813 skiagm::GetGr()->setTextureCacheLimits(0, 0);
814 }
815
bsalomon@google.com7361f542012-04-19 19:15:35 +0000816 Iter iter;
817 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +0000818 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000819 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000820 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000821 SkDELETE(gm);
822 continue;
823 }
824
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000825 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000826 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000827 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000828 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000829
bsalomon@google.com29d35012011-11-30 16:57:21 +0000830 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000831 SkAutoTUnref<GrRenderTarget> rt;
832 AutoResetGr autogr;
833 if (kGPU_Backend == gRec[i].fBackend) {
834 GrContext* gr = grFactory.get(gRec[i].fGLContextType);
835 if (!gr) {
836 continue;
837 }
838
839 // create a render target to back the device
840 GrTextureDesc desc;
841 desc.fConfig = kSkia8888_PM_GrPixelConfig;
842 desc.fFlags = kRenderTarget_GrTextureFlagBit;
843 desc.fWidth = gm->getISize().width();
844 desc.fHeight = gm->getISize().height();
845 desc.fSampleCnt = gRec[i].fSampleCnt;
846 GrTexture* tex = gr->createUncachedTexture(desc, NULL, 0);
847 if (!tex) {
848 return false;
849 }
850 rt.reset(tex->asRenderTarget());
851 rt.get()->ref();
852 tex->unref();
853
854 autogr.set(gr);
855 }
856
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000857 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000858 uint32_t gmFlags = gm->getFlags();
twiz@google.come24a0792012-01-31 18:35:30 +0000859 if ((kPDF_Backend == gRec[i].fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +0000860 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
861 {
reed@google.comab973972011-09-19 19:01:38 +0000862 continue;
863 }
864
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000865 // Now we know that we want to run this test and record its
866 // success or failure.
867 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000868
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000869 if ((ERROR_NONE == testErrors) &&
870 (kGPU_Backend == gRec[i].fBackend) &&
871 (NULL == rt.get())) {
872 fprintf(stderr, "Could not create render target for gpu.\n");
873 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000874 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000875
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000876 if (ERROR_NONE == testErrors) {
877 testErrors |= test_drawing(gm, gRec[i],
878 writePath, readPath, diffPath,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000879 GetGr(),
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000880 rt.get(), &forwardRenderedBitmap);
881 }
882
junov@google.com4370aed2012-01-18 16:21:08 +0000883 if (doDeferred && !testErrors &&
twiz@google.come24a0792012-01-31 18:35:30 +0000884 (kGPU_Backend == gRec[i].fBackend ||
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000885 kRaster_Backend == gRec[i].fBackend)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000886 testErrors |= test_deferred_drawing(gm, gRec[i],
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000887 forwardRenderedBitmap,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000888 diffPath, GetGr(), rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +0000889 }
890
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000891 if ((ERROR_NONE == testErrors) && doReplay &&
892 !(gmFlags & GM::kSkipPicture_Flag)) {
893 testErrors |= test_picture_playback(gm, gRec[i],
894 forwardRenderedBitmap,
895 readPath, diffPath);
896 }
897
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000898 if ((ERROR_NONE == testErrors) && doSerialize &&
899 !(gmFlags & GM::kSkipPicture_Flag)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000900 testErrors |= test_picture_serialization(gm, gRec[i],
901 forwardRenderedBitmap,
902 readPath, diffPath);
903 }
904
905 // Update overall results.
906 // We only tabulate the particular error types that we currently
907 // care about (e.g., missing reference images). Later on, if we
908 // want to also tabulate pixel mismatches vs dimension mistmatches
909 // (or whatever else), we can do so.
910 testsRun++;
911 if (ERROR_NONE == testErrors) {
912 testsPassed++;
913 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
914 testsMissingReferenceImages++;
915 } else {
916 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000917 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000918 }
reed@android.com00dae862009-06-10 15:38:48 +0000919 SkDELETE(gm);
920 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000921 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
922 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000923
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000924 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +0000925}