blob: 61a085938d7c3f9abc35d278cad9d60d5eb413b4 [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@google.come8fcb502012-05-17 15:28:20 +000055// If true, emit a messange when we can't find a reference image to compare
56static bool gNotifyMissingReadReference;
57
reed@android.com00dae862009-06-10 15:38:48 +000058using namespace skiagm;
59
reed@android.com00dae862009-06-10 15:38:48 +000060class Iter {
61public:
62 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000063 this->reset();
64 }
65
66 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000067 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000068 }
reed@google.comd4dfd102011-01-18 21:05:42 +000069
reed@android.comdd0ac282009-06-20 02:38:16 +000070 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000071 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000072 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000073 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000074 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000075 }
76 return NULL;
77 }
reed@google.comd4dfd102011-01-18 21:05:42 +000078
reed@android.com00dae862009-06-10 15:38:48 +000079 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000080 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000081 int count = 0;
82 while (reg) {
83 count += 1;
84 reg = reg->next();
85 }
86 return count;
87 }
reed@google.comd4dfd102011-01-18 21:05:42 +000088
reed@android.com00dae862009-06-10 15:38:48 +000089private:
90 const GMRegistry* fReg;
91};
92
reed@android.com8015dd82009-06-21 00:49:18 +000093static SkString make_name(const char shortName[], const char configName[]) {
94 SkString name(shortName);
95 name.appendf("_%s", configName);
96 return name;
97}
98
tomhudson@google.com9875dd12011-04-25 15:49:53 +000099static SkString make_filename(const char path[],
100 const char pathSuffix[],
101 const SkString& name,
102 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000103 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000104 if (filename.endsWith("/")) {
105 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000106 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000107 filename.append(pathSuffix);
108 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000109 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000110 return filename;
111}
112
reed@android.comb9b9a182009-07-08 02:54:47 +0000113/* since PNG insists on unpremultiplying our alpha, we take no precision chances
114 and force all pixels to be 100% opaque, otherwise on compare we may not get
115 a perfect match.
116 */
117static void force_all_opaque(const SkBitmap& bitmap) {
118 SkAutoLockPixels lock(bitmap);
119 for (int y = 0; y < bitmap.height(); y++) {
120 for (int x = 0; x < bitmap.width(); x++) {
121 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
122 }
123 }
124}
125
126static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
127 SkBitmap copy;
128 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
129 force_all_opaque(copy);
130 return SkImageEncoder::EncodeFile(path.c_str(), copy,
131 SkImageEncoder::kPNG_Type, 100);
132}
133
reed@google.com3d3f0922010-12-20 21:10:29 +0000134static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000135 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
136 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
137 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
138 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000139}
140
141static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000142 SkBitmap* diff) {
143 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000144
145 const int w = target.width();
146 const int h = target.height();
147 for (int y = 0; y < h; y++) {
148 for (int x = 0; x < w; x++) {
149 SkPMColor c0 = *base.getAddr32(x, y);
150 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000151 SkPMColor d = 0;
152 if (c0 != c1) {
153 d = compute_diff_pmcolor(c0, c1);
154 }
155 *diff->getAddr32(x, y) = d;
156 }
157 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000158}
159
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000160static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
161 const SkString& name,
162 const char* renderModeDescriptor,
163 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000164 SkBitmap copy;
165 const SkBitmap* bm = &target;
166 if (target.config() != SkBitmap::kARGB_8888_Config) {
167 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
168 bm = &copy;
169 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000170 SkBitmap baseCopy;
171 const SkBitmap* bp = &base;
172 if (base.config() != SkBitmap::kARGB_8888_Config) {
173 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
174 bp = &baseCopy;
175 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000176
177 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000178 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000179
180 const int w = bm->width();
181 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000182 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000183 SkDebugf(
184"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
185 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000186 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000187 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000188 }
189
190 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000191 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000192
193 for (int y = 0; y < h; y++) {
194 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000195 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000196 SkPMColor c1 = *bm->getAddr32(x, y);
197 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000198 SkDebugf(
199"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
200 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000201
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000202 if (diff) {
203 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
204 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000205 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000206 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000207 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000208 }
209 }
210 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000211
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000212 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000213 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000214}
reed@android.com00dae862009-06-10 15:38:48 +0000215
bungeman@google.comb29c8832011-10-10 13:19:10 +0000216static bool write_document(const SkString& path,
217 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000218 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000219 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000220 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000221}
222
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000223enum Backend {
224 kRaster_Backend,
225 kGPU_Backend,
226 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000227 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000228};
229
bsalomon@google.com7361f542012-04-19 19:15:35 +0000230enum ConfigFlags {
231 kNone_ConfigFlag = 0x0,
232 /* Write GM images if a write path is provided. */
233 kWrite_ConfigFlag = 0x1,
234 /* Read comparison GM images if a read path is provided. */
235 kRead_ConfigFlag = 0x2,
236 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
237};
238
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000239struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000240 SkBitmap::Config fConfig;
241 Backend fBackend;
242 GrContextFactory::GLContextType fGLContextType; // GPU backend only
243 int fSampleCnt; // GPU backend only
244 ConfigFlags fFlags;
245 const char* fName;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000246};
247
248/// Returns true if processing should continue, false to skip the
249/// remainder of this config for this GM.
250//@todo thudson 22 April 2011 - could refactor this to take in
251// a factory to generate the context, always call readPixels()
252// (logically a noop for rasters, if wasted time), and thus collapse the
253// GPU special case and also let this be used for SkPicture testing.
254static void setup_bitmap(const ConfigData& gRec, SkISize& size,
255 SkBitmap* bitmap) {
256 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
257 bitmap->allocPixels();
258 bitmap->eraseColor(0);
259}
260
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000261#include "SkDrawFilter.h"
262class BWTextDrawFilter : public SkDrawFilter {
263public:
264 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
265};
266void BWTextDrawFilter::filter(SkPaint* p, Type t) {
267 if (kText_Type == t) {
268 p->setAntiAlias(false);
269 }
270}
271
272static void installFilter(SkCanvas* canvas) {
273 if (gForceBWtext) {
274 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
275 }
276}
277
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000278static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
279 if (!isPDF) {
280 canvas->setMatrix(gm->getInitialTransform());
281 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000282 installFilter(canvas);
283 gm->draw(canvas);
284 canvas->setDrawFilter(NULL);
285}
286
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000287static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
288 GrContext* context,
289 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000290 SkBitmap* bitmap,
291 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000292 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000293 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000294
295 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000296 SkCanvas* canvas;
297 if (deferred) {
298 canvas = new SkDeferredCanvas;
299 canvas->setDevice(new SkDevice(*bitmap))->unref();
300 } else {
301 canvas = new SkCanvas(*bitmap);
302 }
303 SkAutoUnref canvasUnref(canvas);
304 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000305 canvas->flush();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000306 } else { // GPU
307 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000308 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000309 }
junov@google.com4370aed2012-01-18 16:21:08 +0000310 SkCanvas* gc;
311 if (deferred) {
312 gc = new SkDeferredCanvas;
313 } else {
314 gc = new SkGpuCanvas(context, rt);
315 }
316 SkAutoUnref gcUnref(gc);
317 gc->setDevice(new SkGpuDevice(context, rt))->unref();
318 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000319 // the device is as large as the current rendertarget, so we explicitly
320 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000321 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000322 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
323 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000324 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000325 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000326 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000327}
328
329static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
330 SkPicture* pict, SkBitmap* bitmap) {
331 SkISize size = gm->getISize();
332 setup_bitmap(gRec, size, bitmap);
333 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000334 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000335 canvas.drawPicture(*pict);
336}
337
338static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
339#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000340 SkMatrix initialTransform = gm->getInitialTransform();
341 SkISize pageSize = gm->getISize();
342 SkPDFDevice* dev = NULL;
343 if (initialTransform.isIdentity()) {
344 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
345 } else {
346 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
347 SkIntToScalar(pageSize.height()));
348 initialTransform.mapRect(&content);
349 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
350 SkIntToScalar(pageSize.height()));
351 SkISize contentSize =
352 SkISize::Make(SkScalarRoundToInt(content.width()),
353 SkScalarRoundToInt(content.height()));
354 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
355 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000356 SkAutoUnref aur(dev);
357
358 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000359 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000360
361 SkPDFDocument doc;
362 doc.appendPage(dev);
363 doc.emitPDF(&pdf);
364#endif
365}
366
bungeman@google.comb29c8832011-10-10 13:19:10 +0000367static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
368#ifdef SK_SUPPORT_XPS
369 SkISize size = gm->getISize();
370
371 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
372 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000373 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
374 static const SkScalar upm = 72 * inchesPerMeter;
375 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
376 static const SkScalar ppm = 200 * inchesPerMeter;
377 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000378
379 SkXPSDevice* dev = new SkXPSDevice();
380 SkAutoUnref aur(dev);
381
382 SkCanvas c(dev);
383 dev->beginPortfolio(&xps);
384 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000385 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000386 dev->endSheet();
387 dev->endPortfolio();
388
389#endif
390}
391
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000392static ErrorBitfield write_reference_image(const ConfigData& gRec,
393 const char writePath [],
394 const char renderModeDescriptor [],
395 const SkString& name,
396 SkBitmap& bitmap,
397 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000398 SkString path;
399 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000400 if (gRec.fBackend == kRaster_Backend ||
401 gRec.fBackend == kGPU_Backend ||
402 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
403
tomhudson@google.comea325432011-06-09 20:30:03 +0000404 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000405 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000406 }
407 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000408 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000409 success = write_document(path, *document);
410 }
411 if (kXPS_Backend == gRec.fBackend) {
412 path = make_filename(writePath, renderModeDescriptor, name, "xps");
413 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000414 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000415 if (success) {
416 return ERROR_NONE;
417 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000418 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000419 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000420 }
421}
422
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000423static ErrorBitfield compare_to_reference_image(const SkString& name,
424 SkBitmap &bitmap,
425 const SkBitmap& comparisonBitmap,
426 const char diffPath [],
427 const char renderModeDescriptor []) {
428 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000429 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000430 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
431 diffPath ? &diffBitmap : NULL);
reed@google.com8e529b72012-04-09 20:20:10 +0000432 if ((ERROR_NONE != errors) && diffPath) {
433 // write out the generated image
434 SkString genName = make_filename(diffPath, "", name, "png");
435 if (!write_bitmap(genName, bitmap)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000436 errors |= ERROR_WRITING_REFERENCE_IMAGE;
437 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000438 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000439 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000440}
441
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000442static ErrorBitfield compare_to_reference_image(const char readPath [],
443 const SkString& name,
444 SkBitmap &bitmap,
445 const char diffPath [],
446 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000447 SkString path = make_filename(readPath, "", name, "png");
448 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000449 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
450 SkBitmap::kARGB_8888_Config,
451 SkImageDecoder::kDecodePixels_Mode, NULL)) {
452 return compare_to_reference_image(name, bitmap,
453 orig, diffPath,
454 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000455 } else {
reed@google.come8fcb502012-05-17 15:28:20 +0000456 if (gNotifyMissingReadReference) {
457 fprintf(stderr, "FAILED to read %s\n", path.c_str());
458 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000459 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000460 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000461}
462
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000463static ErrorBitfield handle_test_results(GM* gm,
464 const ConfigData& gRec,
465 const char writePath [],
466 const char readPath [],
467 const char diffPath [],
468 const char renderModeDescriptor [],
469 SkBitmap& bitmap,
470 SkDynamicMemoryWStream* pdf,
471 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000472 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000473 ErrorBitfield retval = ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000474
bsalomon@google.com7361f542012-04-19 19:15:35 +0000475 if (readPath && (gRec.fFlags & kRead_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000476 retval |= compare_to_reference_image(readPath, name, bitmap,
477 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000478 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000479 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000480 retval |= write_reference_image(gRec, writePath, renderModeDescriptor,
481 name, bitmap, pdf);
482 }
483 if (comparisonBitmap) {
484 retval |= compare_to_reference_image(name, bitmap,
485 *comparisonBitmap, diffPath,
486 renderModeDescriptor);
487 }
488 return retval;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000489}
490
491static SkPicture* generate_new_picture(GM* gm) {
492 // Pictures are refcounted so must be on heap
493 SkPicture* pict = new SkPicture;
494 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000495 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000496 pict->endRecording();
497
498 return pict;
499}
500
501static SkPicture* stream_to_new_picture(const SkPicture& src) {
502
503 // To do in-memory commiunications with a stream, we need to:
504 // * create a dynamic memory stream
505 // * copy it into a buffer
506 // * create a read stream from it
507 // ?!?!
508
509 SkDynamicMemoryWStream storage;
510 src.serialize(&storage);
511
512 int streamSize = storage.getOffset();
513 SkAutoMalloc dstStorage(streamSize);
514 void* dst = dstStorage.get();
515 //char* dst = new char [streamSize];
516 //@todo thudson 22 April 2011 when can we safely delete [] dst?
517 storage.copyTo(dst);
518 SkMemoryStream pictReadback(dst, streamSize);
519 SkPicture* retval = new SkPicture (&pictReadback);
520 return retval;
521}
522
523// Test: draw into a bitmap or pdf.
524// Depending on flags, possibly compare to an expected image
525// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000526static ErrorBitfield test_drawing(GM* gm,
527 const ConfigData& gRec,
528 const char writePath [],
529 const char readPath [],
530 const char diffPath [],
531 GrContext* context,
532 GrRenderTarget* rt,
533 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000534 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000535
536 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000537 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000538 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000539 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
540 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000541 if (ERROR_NONE != errors) {
542 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000543 }
reed@google.com46cce912011-06-29 12:54:46 +0000544 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000545 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000546#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000547 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000548 SkMemoryStream stream(data.data(), data.size());
549 SkPDFDocumentToBitmap(&stream, bitmap);
550#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000551 } else if (gRec.fBackend == kXPS_Backend) {
552 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000553 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000554 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000555 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000556}
557
junov@google.com4370aed2012-01-18 16:21:08 +0000558static ErrorBitfield test_deferred_drawing(GM* gm,
559 const ConfigData& gRec,
560 const SkBitmap& comparisonBitmap,
561 const char diffPath [],
562 GrContext* context,
563 GrRenderTarget* rt) {
564 SkDynamicMemoryWStream document;
565
566 if (gRec.fBackend == kRaster_Backend ||
567 gRec.fBackend == kGPU_Backend) {
568 SkBitmap bitmap;
569 // Early exit if we can't generate the image, but this is
570 // expected in some cases, so don't report a test failure.
571 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
572 return ERROR_NONE;
573 }
574 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
575 "-deferred", bitmap, NULL, &comparisonBitmap);
576 }
577 return ERROR_NONE;
578}
579
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000580static ErrorBitfield test_picture_playback(GM* gm,
581 const ConfigData& gRec,
582 const SkBitmap& comparisonBitmap,
583 const char readPath [],
584 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000585 SkPicture* pict = generate_new_picture(gm);
586 SkAutoUnref aur(pict);
587
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000588 if (kRaster_Backend == gRec.fBackend) {
589 SkBitmap bitmap;
590 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000591 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
592 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000593 } else {
594 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000595 }
596}
597
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000598static ErrorBitfield test_picture_serialization(GM* gm,
599 const ConfigData& gRec,
600 const SkBitmap& comparisonBitmap,
601 const char readPath [],
602 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000603 SkPicture* pict = generate_new_picture(gm);
604 SkAutoUnref aurp(pict);
605 SkPicture* repict = stream_to_new_picture(*pict);
606 SkAutoUnref aurr(repict);
607
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000608 if (kRaster_Backend == gRec.fBackend) {
609 SkBitmap bitmap;
610 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000611 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
612 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000613 } else {
614 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000615 }
616}
617
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000618static void write_picture_serialization(GM* gm, const ConfigData& rec,
619 const char writePicturePath[]) {
620 // only do this once, so we pick raster
621 if (kRaster_Backend == rec.fBackend &&
622 SkBitmap::kARGB_8888_Config == rec.fConfig) {
623
624 SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
625
626 const char* pictureSuffix = "skp";
627 SkString path = make_filename(writePicturePath, "",
628 SkString(gm->shortName()), pictureSuffix);
629
630 SkFILEWStream stream(path.c_str());
631 pict->serialize(&stream);
632 }
633}
634
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000635static void usage(const char * argv0) {
junov@google.com77e498e2012-01-18 18:56:34 +0000636 SkDebugf(
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000637 "%s [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n"
638 " [--noreplay] [--serialize] [--forceBWtext] [--nopdf] \n"
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000639 " [--nodeferred] [--match substring] [--notexturecache]\n"
bsalomon@google.com7361f542012-04-19 19:15:35 +0000640 , argv0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000641 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000642 SkDebugf(
643" readPath: directory to read reference images from;\n"
644" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000645 SkDebugf(" diffPath: directory to write difference images in.\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000646 SkDebugf(" resourcePath: directory that stores image resources.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000647 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000648 SkDebugf(
649" --serialize: exercise SkPicture serialization & deserialization.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000650 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
651 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
652 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000653 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
twiz@google.come24a0792012-01-31 18:35:30 +0000654 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000655}
656
bsalomon@google.com7361f542012-04-19 19:15:35 +0000657static const GrContextFactory::GLContextType kDontCare_GLContextType =
658 GrContextFactory::kNative_GLContextType;
659
660// If the platform does not support writing PNGs of PDFs then there will be no
661// comparison images to read. However, we can always write the .pdf files
662static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
663 kWrite_ConfigFlag;
664
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000665static const ConfigData gRec[] = {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000666 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888" },
667 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444" },
668 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000669#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.com7361f542012-04-19 19:15:35 +0000670 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu" },
671 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16" },
672 /* The debug context does not generate images */
673 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "debug" },
674 #ifdef SK_ANGLE
675 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle" },
676 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16" },
677 #endif
678 #ifdef SK_MESA
679 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa" },
680 #endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000681#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000682#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000683 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
684 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps" },
685#endif
686#ifdef SK_SUPPORT_PDF
687 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf" },
bungeman@google.comb29c8832011-10-10 13:19:10 +0000688#endif
reed@android.com00dae862009-06-10 15:38:48 +0000689};
690
reed@google.comb2a51622011-10-31 16:30:04 +0000691static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
692 if (0 == array.count()) {
693 // no names, so don't skip anything
694 return false;
695 }
696 for (int i = 0; i < array.count(); ++i) {
697 if (strstr(name, array[i])) {
698 // found the name, so don't skip
699 return false;
700 }
701 }
702 return true;
703}
704
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000705namespace skiagm {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000706SkAutoTUnref<GrContext> gGrContext;
707/**
708 * Sets the global GrContext, accessible by indivual GMs
709 */
710void SetGr(GrContext* grContext) {
711 SkSafeRef(grContext);
712 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000713}
bsalomon@google.com7361f542012-04-19 19:15:35 +0000714
715/**
716 * Gets the global GrContext, can be called by GM tests.
717 */
718GrContext* GetGr() {
719 return gGrContext.get();
720}
721
722/**
723 * Sets the global GrContext and then resets it to its previous value at
724 * destruction.
725 */
726class AutoResetGr : SkNoncopyable {
727public:
728 AutoResetGr() : fOld(NULL) {}
729 void set(GrContext* context) {
730 SkASSERT(NULL == fOld);
731 fOld = GetGr();
732 SkSafeRef(fOld);
733 SetGr(context);
734 }
735 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
736private:
737 GrContext* fOld;
738};
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000739}
740
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000741int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000742 SkAutoGraphics ag;
reed@google.com8923c6c2011-11-08 14:59:38 +0000743 // we don't need to see this during a run
744 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000745
epoger@google.com7bc13a62012-02-14 14:53:59 +0000746 setSystemPreferences();
747
reed@android.com8015dd82009-06-21 00:49:18 +0000748 const char* writePath = NULL; // if non-null, where we write the originals
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000749 const char* writePicturePath = NULL; // if non-null, where we write serialized pictures
reed@android.com8015dd82009-06-21 00:49:18 +0000750 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000751 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000752 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000753
reed@google.comb2a51622011-10-31 16:30:04 +0000754 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000755
reed@google.comab973972011-09-19 19:01:38 +0000756 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000757 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000758 bool doSerialize = false;
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000759 bool useDebugGL = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000760 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000761 bool disableTextureCache = false;
762
reed@google.come8fcb502012-05-17 15:28:20 +0000763 gNotifyMissingReadReference = true;
764
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000765 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000766 char* const* stop = argv + argc;
767 for (++argv; argv < stop; ++argv) {
768 if (strcmp(*argv, "-w") == 0) {
769 argv++;
770 if (argv < stop && **argv) {
771 writePath = *argv;
772 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000773 } else if (strcmp(*argv, "-wp") == 0) {
774 argv++;
775 if (argv < stop && **argv) {
776 writePicturePath = *argv;
777 }
reed@android.com8015dd82009-06-21 00:49:18 +0000778 } else if (strcmp(*argv, "-r") == 0) {
779 argv++;
780 if (argv < stop && **argv) {
781 readPath = *argv;
782 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000783 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000784 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000785 if (argv < stop && **argv) {
786 diffPath = *argv;
787 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000788 } else if (strcmp(*argv, "-i") == 0) {
789 argv++;
790 if (argv < stop && **argv) {
791 resourcePath = *argv;
792 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000793 } else if (strcmp(*argv, "--forceBWtext") == 0) {
794 gForceBWtext = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000795 } else if (strcmp(*argv, "--noreplay") == 0) {
796 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000797 } else if (strcmp(*argv, "--nopdf") == 0) {
798 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000799 } else if (strcmp(*argv, "--nodeferred") == 0) {
800 doDeferred = false;
reed@google.come8fcb502012-05-17 15:28:20 +0000801 } else if (strcmp(*argv, "--disable-missing-warning") == 0) {
802 gNotifyMissingReadReference = false;
803 } else if (strcmp(*argv, "--enable-missing-warning") == 0) {
804 gNotifyMissingReadReference = true;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000805 } else if (strcmp(*argv, "--serialize") == 0) {
806 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000807 } else if (strcmp(*argv, "--match") == 0) {
808 ++argv;
809 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000810 // just record the ptr, no need for a deep copy
811 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000812 }
twiz@google.come24a0792012-01-31 18:35:30 +0000813 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000814 disableTextureCache = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000815 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000816 usage(commandName);
817 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000818 }
819 }
820 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000821 usage(commandName);
822 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000823 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000824
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000825 GM::SetResourcePath(resourcePath);
826
bsalomon@google.com7361f542012-04-19 19:15:35 +0000827 GrContextFactory grFactory;
reed@google.com873cb1e2010-12-23 15:00:45 +0000828
reed@android.com00f883e2010-12-14 17:46:14 +0000829 if (readPath) {
830 fprintf(stderr, "reading from %s\n", readPath);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000831 }
832 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +0000833 fprintf(stderr, "writing to %s\n", writePath);
834 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000835 if (writePicturePath) {
836 fprintf(stderr, "writing pictures to %s\n", writePicturePath);
837 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000838 if (resourcePath) {
839 fprintf(stderr, "reading resources from %s\n", resourcePath);
840 }
841
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000842 // Accumulate success of all tests.
843 int testsRun = 0;
844 int testsPassed = 0;
845 int testsFailed = 0;
846 int testsMissingReferenceImages = 0;
847
twiz@google.come24a0792012-01-31 18:35:30 +0000848 if (disableTextureCache) {
849 skiagm::GetGr()->setTextureCacheLimits(0, 0);
850 }
851
bsalomon@google.com7361f542012-04-19 19:15:35 +0000852 Iter iter;
853 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +0000854 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000855 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000856 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000857 SkDELETE(gm);
858 continue;
859 }
860
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000861 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000862 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000863 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000864 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000865
bsalomon@google.com29d35012011-11-30 16:57:21 +0000866 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000867 SkAutoTUnref<GrRenderTarget> rt;
868 AutoResetGr autogr;
869 if (kGPU_Backend == gRec[i].fBackend) {
870 GrContext* gr = grFactory.get(gRec[i].fGLContextType);
871 if (!gr) {
872 continue;
873 }
874
875 // create a render target to back the device
876 GrTextureDesc desc;
877 desc.fConfig = kSkia8888_PM_GrPixelConfig;
878 desc.fFlags = kRenderTarget_GrTextureFlagBit;
879 desc.fWidth = gm->getISize().width();
880 desc.fHeight = gm->getISize().height();
881 desc.fSampleCnt = gRec[i].fSampleCnt;
882 GrTexture* tex = gr->createUncachedTexture(desc, NULL, 0);
883 if (!tex) {
bsalomon@google.comdaf12bb2012-04-20 19:08:44 +0000884 continue;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000885 }
886 rt.reset(tex->asRenderTarget());
887 rt.get()->ref();
888 tex->unref();
889
890 autogr.set(gr);
891 }
892
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000893 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000894 uint32_t gmFlags = gm->getFlags();
twiz@google.come24a0792012-01-31 18:35:30 +0000895 if ((kPDF_Backend == gRec[i].fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +0000896 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
897 {
reed@google.comab973972011-09-19 19:01:38 +0000898 continue;
899 }
900
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000901 // Now we know that we want to run this test and record its
902 // success or failure.
903 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000904
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000905 if ((ERROR_NONE == testErrors) &&
906 (kGPU_Backend == gRec[i].fBackend) &&
907 (NULL == rt.get())) {
908 fprintf(stderr, "Could not create render target for gpu.\n");
909 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000910 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000911
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000912 if (ERROR_NONE == testErrors) {
913 testErrors |= test_drawing(gm, gRec[i],
914 writePath, readPath, diffPath,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000915 GetGr(),
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000916 rt.get(), &forwardRenderedBitmap);
917 }
918
junov@google.com4370aed2012-01-18 16:21:08 +0000919 if (doDeferred && !testErrors &&
twiz@google.come24a0792012-01-31 18:35:30 +0000920 (kGPU_Backend == gRec[i].fBackend ||
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000921 kRaster_Backend == gRec[i].fBackend)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000922 testErrors |= test_deferred_drawing(gm, gRec[i],
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000923 forwardRenderedBitmap,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000924 diffPath, GetGr(), rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +0000925 }
926
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000927 if ((ERROR_NONE == testErrors) && doReplay &&
928 !(gmFlags & GM::kSkipPicture_Flag)) {
929 testErrors |= test_picture_playback(gm, gRec[i],
930 forwardRenderedBitmap,
931 readPath, diffPath);
932 }
933
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000934 if ((ERROR_NONE == testErrors) && doSerialize &&
935 !(gmFlags & GM::kSkipPicture_Flag)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000936 testErrors |= test_picture_serialization(gm, gRec[i],
937 forwardRenderedBitmap,
938 readPath, diffPath);
939 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000940
941 if (!(gmFlags & GM::kSkipPicture_Flag) && writePicturePath) {
942 write_picture_serialization(gm, gRec[i], writePicturePath);
943 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000944
945 // Update overall results.
946 // We only tabulate the particular error types that we currently
947 // care about (e.g., missing reference images). Later on, if we
948 // want to also tabulate pixel mismatches vs dimension mistmatches
949 // (or whatever else), we can do so.
950 testsRun++;
951 if (ERROR_NONE == testErrors) {
952 testsPassed++;
953 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
954 testsMissingReferenceImages++;
955 } else {
956 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000957 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000958 }
reed@android.com00dae862009-06-10 15:38:48 +0000959 SkDELETE(gm);
960 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000961 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
962 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000963
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000964 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +0000965}