blob: 6eb96fd227cce4496e380e5051653b94d993a5bb [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"
scroggo@google.com5af9b202012-06-04 17:17:36 +000016#include "SkGPipe.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000017#include "SkGpuCanvas.h"
18#include "SkGpuDevice.h"
reed@android.com8015dd82009-06-21 00:49:18 +000019#include "SkGraphics.h"
20#include "SkImageDecoder.h"
21#include "SkImageEncoder.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"
reed@google.com07700442010-12-20 19:46:07 +000024
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000025static bool gForceBWtext;
26
reed@google.com8923c6c2011-11-08 14:59:38 +000027extern bool gSkSuppressFontCachePurgeSpew;
28
reed@google.com07700442010-12-20 19:46:07 +000029#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000030 #include "SkPDFDevice.h"
31 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000032#endif
reed@android.com00dae862009-06-10 15:38:48 +000033
epoger@google.come3cc2eb2012-01-18 20:11:13 +000034// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
35// stop writing out XPS-format image baselines in gm.
36#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000037#ifdef SK_SUPPORT_XPS
38 #include "SkXPSDevice.h"
39#endif
40
reed@google.com46cce912011-06-29 12:54:46 +000041#ifdef SK_BUILD_FOR_MAC
42 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000043 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000044#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000045 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000046#endif
47
epoger@google.comc7cf2b32011-12-28 19:31:01 +000048typedef int ErrorBitfield;
49const static ErrorBitfield ERROR_NONE = 0x00;
50const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
51const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
52const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
53const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
54const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
55
reed@google.come8fcb502012-05-17 15:28:20 +000056// If true, emit a messange when we can't find a reference image to compare
57static bool gNotifyMissingReadReference;
58
reed@android.com00dae862009-06-10 15:38:48 +000059using namespace skiagm;
60
reed@android.com00dae862009-06-10 15:38:48 +000061class Iter {
62public:
63 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000064 this->reset();
65 }
66
67 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000068 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000069 }
reed@google.comd4dfd102011-01-18 21:05:42 +000070
reed@android.comdd0ac282009-06-20 02:38:16 +000071 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000072 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000073 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000074 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000075 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000076 }
77 return NULL;
78 }
reed@google.comd4dfd102011-01-18 21:05:42 +000079
reed@android.com00dae862009-06-10 15:38:48 +000080 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000081 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000082 int count = 0;
83 while (reg) {
84 count += 1;
85 reg = reg->next();
86 }
87 return count;
88 }
reed@google.comd4dfd102011-01-18 21:05:42 +000089
reed@android.com00dae862009-06-10 15:38:48 +000090private:
91 const GMRegistry* fReg;
92};
93
reed@android.com8015dd82009-06-21 00:49:18 +000094static SkString make_name(const char shortName[], const char configName[]) {
95 SkString name(shortName);
96 name.appendf("_%s", configName);
97 return name;
98}
99
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000100static SkString make_filename(const char path[],
101 const char pathSuffix[],
102 const SkString& name,
103 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000104 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000105 if (filename.endsWith("/")) {
106 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000107 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000108 filename.append(pathSuffix);
109 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000110 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000111 return filename;
112}
113
reed@android.comb9b9a182009-07-08 02:54:47 +0000114/* since PNG insists on unpremultiplying our alpha, we take no precision chances
115 and force all pixels to be 100% opaque, otherwise on compare we may not get
116 a perfect match.
117 */
118static void force_all_opaque(const SkBitmap& bitmap) {
119 SkAutoLockPixels lock(bitmap);
120 for (int y = 0; y < bitmap.height(); y++) {
121 for (int x = 0; x < bitmap.width(); x++) {
122 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
123 }
124 }
125}
126
127static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
128 SkBitmap copy;
129 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
130 force_all_opaque(copy);
131 return SkImageEncoder::EncodeFile(path.c_str(), copy,
132 SkImageEncoder::kPNG_Type, 100);
133}
134
reed@google.com3d3f0922010-12-20 21:10:29 +0000135static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000136 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
137 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
138 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
139 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000140}
141
142static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000143 SkBitmap* diff) {
144 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000145
146 const int w = target.width();
147 const int h = target.height();
148 for (int y = 0; y < h; y++) {
149 for (int x = 0; x < w; x++) {
150 SkPMColor c0 = *base.getAddr32(x, y);
151 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000152 SkPMColor d = 0;
153 if (c0 != c1) {
154 d = compute_diff_pmcolor(c0, c1);
155 }
156 *diff->getAddr32(x, y) = d;
157 }
158 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000159}
160
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000161static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
162 const SkString& name,
163 const char* renderModeDescriptor,
164 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000165 SkBitmap copy;
166 const SkBitmap* bm = &target;
167 if (target.config() != SkBitmap::kARGB_8888_Config) {
168 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
169 bm = &copy;
170 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000171 SkBitmap baseCopy;
172 const SkBitmap* bp = &base;
173 if (base.config() != SkBitmap::kARGB_8888_Config) {
174 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
175 bp = &baseCopy;
176 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000177
178 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000179 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000180
181 const int w = bm->width();
182 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000183 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000184 SkDebugf(
185"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
186 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000187 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000188 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000189 }
190
191 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000192 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000193
194 for (int y = 0; y < h; y++) {
195 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000196 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000197 SkPMColor c1 = *bm->getAddr32(x, y);
198 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000199 SkDebugf(
200"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
201 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000202
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000203 if (diff) {
204 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
205 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000206 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000207 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000208 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000209 }
210 }
211 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000212
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000213 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000214 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000215}
reed@android.com00dae862009-06-10 15:38:48 +0000216
bungeman@google.comb29c8832011-10-10 13:19:10 +0000217static bool write_document(const SkString& path,
218 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000219 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000220 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000221 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000222}
223
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000224enum Backend {
225 kRaster_Backend,
226 kGPU_Backend,
227 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000228 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000229};
230
bsalomon@google.com7361f542012-04-19 19:15:35 +0000231enum ConfigFlags {
232 kNone_ConfigFlag = 0x0,
233 /* Write GM images if a write path is provided. */
234 kWrite_ConfigFlag = 0x1,
235 /* Read comparison GM images if a read path is provided. */
236 kRead_ConfigFlag = 0x2,
237 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
238};
239
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000240struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000241 SkBitmap::Config fConfig;
242 Backend fBackend;
243 GrContextFactory::GLContextType fGLContextType; // GPU backend only
244 int fSampleCnt; // GPU backend only
245 ConfigFlags fFlags;
246 const char* fName;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000247};
248
249/// Returns true if processing should continue, false to skip the
250/// remainder of this config for this GM.
251//@todo thudson 22 April 2011 - could refactor this to take in
252// a factory to generate the context, always call readPixels()
253// (logically a noop for rasters, if wasted time), and thus collapse the
254// GPU special case and also let this be used for SkPicture testing.
255static void setup_bitmap(const ConfigData& gRec, SkISize& size,
256 SkBitmap* bitmap) {
257 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
258 bitmap->allocPixels();
259 bitmap->eraseColor(0);
260}
261
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000262#include "SkDrawFilter.h"
263class BWTextDrawFilter : public SkDrawFilter {
264public:
265 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
266};
267void BWTextDrawFilter::filter(SkPaint* p, Type t) {
268 if (kText_Type == t) {
269 p->setAntiAlias(false);
270 }
271}
272
273static void installFilter(SkCanvas* canvas) {
274 if (gForceBWtext) {
275 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
276 }
277}
278
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000279static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
reed@google.com778e1632012-06-04 20:00:01 +0000280 SkAutoCanvasRestore acr(canvas, true);
281
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000282 if (!isPDF) {
reed@google.com778e1632012-06-04 20:00:01 +0000283 canvas->concat(gm->getInitialTransform());
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000284 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000285 installFilter(canvas);
286 gm->draw(canvas);
287 canvas->setDrawFilter(NULL);
288}
289
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000290static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
291 GrContext* context,
292 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000293 SkBitmap* bitmap,
294 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000295 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000296 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000297
298 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000299 SkCanvas* canvas;
300 if (deferred) {
301 canvas = new SkDeferredCanvas;
302 canvas->setDevice(new SkDevice(*bitmap))->unref();
303 } else {
304 canvas = new SkCanvas(*bitmap);
305 }
306 SkAutoUnref canvasUnref(canvas);
307 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000308 canvas->flush();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000309 } else { // GPU
310 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000311 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000312 }
junov@google.com4370aed2012-01-18 16:21:08 +0000313 SkCanvas* gc;
314 if (deferred) {
315 gc = new SkDeferredCanvas;
316 } else {
317 gc = new SkGpuCanvas(context, rt);
318 }
319 SkAutoUnref gcUnref(gc);
320 gc->setDevice(new SkGpuDevice(context, rt))->unref();
321 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000322 // the device is as large as the current rendertarget, so we explicitly
323 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000324 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000325 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
326 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000327 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000328 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000329 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000330}
331
332static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
333 SkPicture* pict, SkBitmap* bitmap) {
334 SkISize size = gm->getISize();
335 setup_bitmap(gRec, size, bitmap);
336 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000337 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000338 canvas.drawPicture(*pict);
339}
340
341static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
342#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000343 SkMatrix initialTransform = gm->getInitialTransform();
344 SkISize pageSize = gm->getISize();
345 SkPDFDevice* dev = NULL;
346 if (initialTransform.isIdentity()) {
347 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
348 } else {
349 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
350 SkIntToScalar(pageSize.height()));
351 initialTransform.mapRect(&content);
352 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
353 SkIntToScalar(pageSize.height()));
354 SkISize contentSize =
355 SkISize::Make(SkScalarRoundToInt(content.width()),
356 SkScalarRoundToInt(content.height()));
357 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
358 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000359 SkAutoUnref aur(dev);
360
361 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000362 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000363
364 SkPDFDocument doc;
365 doc.appendPage(dev);
366 doc.emitPDF(&pdf);
367#endif
368}
369
bungeman@google.comb29c8832011-10-10 13:19:10 +0000370static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
371#ifdef SK_SUPPORT_XPS
372 SkISize size = gm->getISize();
373
374 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
375 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000376 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
377 static const SkScalar upm = 72 * inchesPerMeter;
378 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
379 static const SkScalar ppm = 200 * inchesPerMeter;
380 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000381
382 SkXPSDevice* dev = new SkXPSDevice();
383 SkAutoUnref aur(dev);
384
385 SkCanvas c(dev);
386 dev->beginPortfolio(&xps);
387 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000388 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000389 dev->endSheet();
390 dev->endPortfolio();
391
392#endif
393}
394
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000395static ErrorBitfield write_reference_image(const ConfigData& gRec,
396 const char writePath [],
397 const char renderModeDescriptor [],
398 const SkString& name,
399 SkBitmap& bitmap,
400 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000401 SkString path;
402 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000403 if (gRec.fBackend == kRaster_Backend ||
404 gRec.fBackend == kGPU_Backend ||
405 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
406
tomhudson@google.comea325432011-06-09 20:30:03 +0000407 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000408 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000409 }
410 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000411 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000412 success = write_document(path, *document);
413 }
414 if (kXPS_Backend == gRec.fBackend) {
415 path = make_filename(writePath, renderModeDescriptor, name, "xps");
416 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000417 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000418 if (success) {
419 return ERROR_NONE;
420 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000421 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000422 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000423 }
424}
425
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000426static ErrorBitfield compare_to_reference_image(const SkString& name,
427 SkBitmap &bitmap,
428 const SkBitmap& comparisonBitmap,
429 const char diffPath [],
430 const char renderModeDescriptor []) {
431 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000432 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000433 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
434 diffPath ? &diffBitmap : NULL);
reed@google.com8e529b72012-04-09 20:20:10 +0000435 if ((ERROR_NONE != errors) && diffPath) {
436 // write out the generated image
437 SkString genName = make_filename(diffPath, "", name, "png");
438 if (!write_bitmap(genName, bitmap)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000439 errors |= ERROR_WRITING_REFERENCE_IMAGE;
440 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000441 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000442 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000443}
444
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000445static ErrorBitfield compare_to_reference_image(const char readPath [],
446 const SkString& name,
447 SkBitmap &bitmap,
448 const char diffPath [],
449 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000450 SkString path = make_filename(readPath, "", name, "png");
451 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000452 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
453 SkBitmap::kARGB_8888_Config,
454 SkImageDecoder::kDecodePixels_Mode, NULL)) {
455 return compare_to_reference_image(name, bitmap,
456 orig, diffPath,
457 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000458 } else {
reed@google.come8fcb502012-05-17 15:28:20 +0000459 if (gNotifyMissingReadReference) {
460 fprintf(stderr, "FAILED to read %s\n", path.c_str());
461 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000462 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000463 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000464}
465
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000466static ErrorBitfield handle_test_results(GM* gm,
467 const ConfigData& gRec,
468 const char writePath [],
469 const char readPath [],
470 const char diffPath [],
471 const char renderModeDescriptor [],
472 SkBitmap& bitmap,
473 SkDynamicMemoryWStream* pdf,
474 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000475 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000476 ErrorBitfield retval = ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000477
bsalomon@google.com7361f542012-04-19 19:15:35 +0000478 if (readPath && (gRec.fFlags & kRead_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000479 retval |= compare_to_reference_image(readPath, name, bitmap,
480 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000481 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000482 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000483 retval |= write_reference_image(gRec, writePath, renderModeDescriptor,
484 name, bitmap, pdf);
485 }
486 if (comparisonBitmap) {
487 retval |= compare_to_reference_image(name, bitmap,
488 *comparisonBitmap, diffPath,
489 renderModeDescriptor);
490 }
491 return retval;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000492}
493
494static SkPicture* generate_new_picture(GM* gm) {
495 // Pictures are refcounted so must be on heap
496 SkPicture* pict = new SkPicture;
497 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000498 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000499 pict->endRecording();
500
501 return pict;
502}
503
504static SkPicture* stream_to_new_picture(const SkPicture& src) {
505
506 // To do in-memory commiunications with a stream, we need to:
507 // * create a dynamic memory stream
508 // * copy it into a buffer
509 // * create a read stream from it
510 // ?!?!
511
512 SkDynamicMemoryWStream storage;
513 src.serialize(&storage);
514
515 int streamSize = storage.getOffset();
516 SkAutoMalloc dstStorage(streamSize);
517 void* dst = dstStorage.get();
518 //char* dst = new char [streamSize];
519 //@todo thudson 22 April 2011 when can we safely delete [] dst?
520 storage.copyTo(dst);
521 SkMemoryStream pictReadback(dst, streamSize);
522 SkPicture* retval = new SkPicture (&pictReadback);
523 return retval;
524}
525
526// Test: draw into a bitmap or pdf.
527// Depending on flags, possibly compare to an expected image
528// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000529static ErrorBitfield test_drawing(GM* gm,
530 const ConfigData& gRec,
531 const char writePath [],
532 const char readPath [],
533 const char diffPath [],
534 GrContext* context,
535 GrRenderTarget* rt,
536 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000537 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000538
539 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000540 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000541 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000542 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
543 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000544 if (ERROR_NONE != errors) {
545 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000546 }
reed@google.com46cce912011-06-29 12:54:46 +0000547 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000548 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000549#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000550 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000551 SkMemoryStream stream(data.data(), data.size());
552 SkPDFDocumentToBitmap(&stream, bitmap);
553#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000554 } else if (gRec.fBackend == kXPS_Backend) {
555 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000556 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000557 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000558 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000559}
560
junov@google.com4370aed2012-01-18 16:21:08 +0000561static ErrorBitfield test_deferred_drawing(GM* gm,
562 const ConfigData& gRec,
563 const SkBitmap& comparisonBitmap,
564 const char diffPath [],
565 GrContext* context,
566 GrRenderTarget* rt) {
567 SkDynamicMemoryWStream document;
568
569 if (gRec.fBackend == kRaster_Backend ||
570 gRec.fBackend == kGPU_Backend) {
571 SkBitmap bitmap;
572 // Early exit if we can't generate the image, but this is
573 // expected in some cases, so don't report a test failure.
574 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
575 return ERROR_NONE;
576 }
577 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
578 "-deferred", bitmap, NULL, &comparisonBitmap);
579 }
580 return ERROR_NONE;
581}
582
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000583static ErrorBitfield test_picture_playback(GM* gm,
584 const ConfigData& gRec,
585 const SkBitmap& comparisonBitmap,
586 const char readPath [],
587 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000588 SkPicture* pict = generate_new_picture(gm);
589 SkAutoUnref aur(pict);
590
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000591 if (kRaster_Backend == gRec.fBackend) {
592 SkBitmap bitmap;
593 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000594 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
595 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000596 } else {
597 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000598 }
599}
600
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000601static ErrorBitfield test_picture_serialization(GM* gm,
602 const ConfigData& gRec,
603 const SkBitmap& comparisonBitmap,
604 const char readPath [],
605 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000606 SkPicture* pict = generate_new_picture(gm);
607 SkAutoUnref aurp(pict);
608 SkPicture* repict = stream_to_new_picture(*pict);
609 SkAutoUnref aurr(repict);
610
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000611 if (kRaster_Backend == gRec.fBackend) {
612 SkBitmap bitmap;
613 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000614 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
615 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000616 } else {
617 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000618 }
619}
620
scroggo@google.com5af9b202012-06-04 17:17:36 +0000621class PipeController : public SkGPipeController {
622public:
623 PipeController(SkCanvas* target);
624 ~PipeController();
625 virtual void* requestBlock(size_t minRequest, size_t* actual);
626 virtual void notifyWritten(size_t bytes);
627private:
628 SkGPipeReader fReader;
629 void* fBlock;
630 size_t fBlockSize;
631 size_t fBytesWritten;
632 SkGPipeReader::Status fStatus;
633};
634
635PipeController::PipeController(SkCanvas* target)
636:fReader(target) {
637 fBlock = NULL;
638 fBlockSize = fBytesWritten = 0;
639}
640
641PipeController::~PipeController() {
642 sk_free(fBlock);
643}
644
645void* PipeController::requestBlock(size_t minRequest, size_t *actual) {
646 sk_free(fBlock);
647 fBlockSize = minRequest * 4;
648 fBlock = sk_malloc_throw(fBlockSize);
649 fBytesWritten = 0;
650 *actual = fBlockSize;
651 return fBlock;
652}
653
654void PipeController::notifyWritten(size_t bytes) {
655 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
656 SkASSERT(SkGPipeReader::kError_Status != fStatus);
657 fBytesWritten += bytes;
658}
659
660static ErrorBitfield test_pipe_playback(GM* gm,
661 const ConfigData& gRec,
662 const SkBitmap& comparisonBitmap,
663 const char readPath [],
664 const char diffPath []) {
665 if (kRaster_Backend != gRec.fBackend) {
666 return ERROR_NONE;
667 }
668 SkBitmap bitmap;
669 SkISize size = gm->getISize();
670 setup_bitmap(gRec, size, &bitmap);
671 SkCanvas canvas(bitmap);
672 PipeController pipeController(&canvas);
673 SkGPipeWriter writer;
674 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
675 SkGPipeWriter::kCrossProcess_Flag);
676 invokeGM(gm, pipeCanvas);
677 writer.endRecording();
678 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
679 "-pipe", bitmap, NULL, &comparisonBitmap);
680}
681
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000682static void write_picture_serialization(GM* gm, const ConfigData& rec,
683 const char writePicturePath[]) {
684 // only do this once, so we pick raster
685 if (kRaster_Backend == rec.fBackend &&
686 SkBitmap::kARGB_8888_Config == rec.fConfig) {
687
688 SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
689
690 const char* pictureSuffix = "skp";
691 SkString path = make_filename(writePicturePath, "",
692 SkString(gm->shortName()), pictureSuffix);
693
694 SkFILEWStream stream(path.c_str());
695 pict->serialize(&stream);
696 }
697}
698
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000699static void usage(const char * argv0) {
junov@google.com77e498e2012-01-18 18:56:34 +0000700 SkDebugf(
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000701 "%s [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n"
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000702 " [--noreplay] [--pipe] [--serialize] [--forceBWtext] [--nopdf] \n"
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000703 " [--nodeferred] [--match substring] [--notexturecache]\n"
bsalomon@google.com7361f542012-04-19 19:15:35 +0000704 , argv0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000705 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000706 SkDebugf(
707" readPath: directory to read reference images from;\n"
708" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000709 SkDebugf(" diffPath: directory to write difference images in.\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000710 SkDebugf(" resourcePath: directory that stores image resources.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000711 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000712 SkDebugf(" --pipe: Exercise SkGPipe replay.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000713 SkDebugf(
714" --serialize: exercise SkPicture serialization & deserialization.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000715 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
716 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
717 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000718 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
twiz@google.come24a0792012-01-31 18:35:30 +0000719 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000720}
721
bsalomon@google.com7361f542012-04-19 19:15:35 +0000722static const GrContextFactory::GLContextType kDontCare_GLContextType =
723 GrContextFactory::kNative_GLContextType;
724
725// If the platform does not support writing PNGs of PDFs then there will be no
726// comparison images to read. However, we can always write the .pdf files
727static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
728 kWrite_ConfigFlag;
729
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000730static const ConfigData gRec[] = {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000731 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888" },
732 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444" },
733 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000734#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.com7361f542012-04-19 19:15:35 +0000735 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu" },
736 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16" },
737 /* The debug context does not generate images */
738 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "debug" },
739 #ifdef SK_ANGLE
740 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle" },
741 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16" },
742 #endif
743 #ifdef SK_MESA
744 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa" },
745 #endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000746#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000747#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000748 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
749 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps" },
750#endif
751#ifdef SK_SUPPORT_PDF
752 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf" },
bungeman@google.comb29c8832011-10-10 13:19:10 +0000753#endif
reed@android.com00dae862009-06-10 15:38:48 +0000754};
755
reed@google.comb2a51622011-10-31 16:30:04 +0000756static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
757 if (0 == array.count()) {
758 // no names, so don't skip anything
759 return false;
760 }
761 for (int i = 0; i < array.count(); ++i) {
762 if (strstr(name, array[i])) {
763 // found the name, so don't skip
764 return false;
765 }
766 }
767 return true;
768}
769
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000770namespace skiagm {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000771SkAutoTUnref<GrContext> gGrContext;
772/**
773 * Sets the global GrContext, accessible by indivual GMs
774 */
775void SetGr(GrContext* grContext) {
776 SkSafeRef(grContext);
777 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000778}
bsalomon@google.com7361f542012-04-19 19:15:35 +0000779
780/**
781 * Gets the global GrContext, can be called by GM tests.
782 */
783GrContext* GetGr() {
784 return gGrContext.get();
785}
786
787/**
788 * Sets the global GrContext and then resets it to its previous value at
789 * destruction.
790 */
791class AutoResetGr : SkNoncopyable {
792public:
793 AutoResetGr() : fOld(NULL) {}
794 void set(GrContext* context) {
795 SkASSERT(NULL == fOld);
796 fOld = GetGr();
797 SkSafeRef(fOld);
798 SetGr(context);
799 }
800 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
801private:
802 GrContext* fOld;
803};
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000804}
805
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000806int main(int argc, char * const argv[]) {
robertphillips@google.com25c19932012-06-05 16:24:32 +0000807 SkAutoGraphics ag;
reed@google.com8923c6c2011-11-08 14:59:38 +0000808 // we don't need to see this during a run
809 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000810
epoger@google.com7bc13a62012-02-14 14:53:59 +0000811 setSystemPreferences();
812
reed@android.com8015dd82009-06-21 00:49:18 +0000813 const char* writePath = NULL; // if non-null, where we write the originals
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000814 const char* writePicturePath = NULL; // if non-null, where we write serialized pictures
reed@android.com8015dd82009-06-21 00:49:18 +0000815 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000816 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000817 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000818
reed@google.comb2a51622011-10-31 16:30:04 +0000819 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000820
reed@google.comab973972011-09-19 19:01:38 +0000821 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000822 bool doReplay = true;
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000823 bool doPipe = false;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000824 bool doSerialize = false;
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000825 bool useDebugGL = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000826 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000827 bool disableTextureCache = false;
828
reed@google.come8fcb502012-05-17 15:28:20 +0000829 gNotifyMissingReadReference = true;
830
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000831 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000832 char* const* stop = argv + argc;
833 for (++argv; argv < stop; ++argv) {
834 if (strcmp(*argv, "-w") == 0) {
835 argv++;
836 if (argv < stop && **argv) {
837 writePath = *argv;
838 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000839 } else if (strcmp(*argv, "-wp") == 0) {
840 argv++;
841 if (argv < stop && **argv) {
842 writePicturePath = *argv;
843 }
reed@android.com8015dd82009-06-21 00:49:18 +0000844 } else if (strcmp(*argv, "-r") == 0) {
845 argv++;
846 if (argv < stop && **argv) {
847 readPath = *argv;
848 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000849 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000850 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000851 if (argv < stop && **argv) {
852 diffPath = *argv;
853 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000854 } else if (strcmp(*argv, "-i") == 0) {
855 argv++;
856 if (argv < stop && **argv) {
857 resourcePath = *argv;
858 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000859 } else if (strcmp(*argv, "--forceBWtext") == 0) {
860 gForceBWtext = true;
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000861 } else if (strcmp(*argv, "--pipe") == 0) {
862 doPipe = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000863 } else if (strcmp(*argv, "--noreplay") == 0) {
864 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000865 } else if (strcmp(*argv, "--nopdf") == 0) {
866 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000867 } else if (strcmp(*argv, "--nodeferred") == 0) {
868 doDeferred = false;
reed@google.come8fcb502012-05-17 15:28:20 +0000869 } else if (strcmp(*argv, "--disable-missing-warning") == 0) {
870 gNotifyMissingReadReference = false;
871 } else if (strcmp(*argv, "--enable-missing-warning") == 0) {
872 gNotifyMissingReadReference = true;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000873 } else if (strcmp(*argv, "--serialize") == 0) {
874 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000875 } else if (strcmp(*argv, "--match") == 0) {
876 ++argv;
877 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000878 // just record the ptr, no need for a deep copy
879 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000880 }
twiz@google.come24a0792012-01-31 18:35:30 +0000881 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000882 disableTextureCache = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000883 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000884 usage(commandName);
885 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000886 }
887 }
888 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000889 usage(commandName);
890 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000891 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000892
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000893 GM::SetResourcePath(resourcePath);
894
robertphillips@google.com25c19932012-06-05 16:24:32 +0000895 GrContextFactory grFactory;
reed@google.com873cb1e2010-12-23 15:00:45 +0000896
reed@android.com00f883e2010-12-14 17:46:14 +0000897 if (readPath) {
898 fprintf(stderr, "reading from %s\n", readPath);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000899 }
900 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +0000901 fprintf(stderr, "writing to %s\n", writePath);
902 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000903 if (writePicturePath) {
904 fprintf(stderr, "writing pictures to %s\n", writePicturePath);
905 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000906 if (resourcePath) {
907 fprintf(stderr, "reading resources from %s\n", resourcePath);
908 }
909
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000910 // Accumulate success of all tests.
911 int testsRun = 0;
912 int testsPassed = 0;
913 int testsFailed = 0;
914 int testsMissingReferenceImages = 0;
915
twiz@google.come24a0792012-01-31 18:35:30 +0000916 if (disableTextureCache) {
917 skiagm::GetGr()->setTextureCacheLimits(0, 0);
918 }
919
bsalomon@google.com7361f542012-04-19 19:15:35 +0000920 Iter iter;
921 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +0000922 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000923 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000924 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000925 SkDELETE(gm);
926 continue;
927 }
928
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000929 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000930 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000931 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000932 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000933
bsalomon@google.com29d35012011-11-30 16:57:21 +0000934 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000935 SkAutoTUnref<GrRenderTarget> rt;
936 AutoResetGr autogr;
937 if (kGPU_Backend == gRec[i].fBackend) {
robertphillips@google.com25c19932012-06-05 16:24:32 +0000938 GrContext* gr = grFactory.get(gRec[i].fGLContextType);
bsalomon@google.com7361f542012-04-19 19:15:35 +0000939 if (!gr) {
940 continue;
941 }
942
943 // create a render target to back the device
944 GrTextureDesc desc;
945 desc.fConfig = kSkia8888_PM_GrPixelConfig;
946 desc.fFlags = kRenderTarget_GrTextureFlagBit;
947 desc.fWidth = gm->getISize().width();
948 desc.fHeight = gm->getISize().height();
949 desc.fSampleCnt = gRec[i].fSampleCnt;
950 GrTexture* tex = gr->createUncachedTexture(desc, NULL, 0);
951 if (!tex) {
bsalomon@google.comdaf12bb2012-04-20 19:08:44 +0000952 continue;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000953 }
954 rt.reset(tex->asRenderTarget());
955 rt.get()->ref();
956 tex->unref();
957
958 autogr.set(gr);
959 }
960
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000961 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000962 uint32_t gmFlags = gm->getFlags();
twiz@google.come24a0792012-01-31 18:35:30 +0000963 if ((kPDF_Backend == gRec[i].fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +0000964 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
965 {
reed@google.comab973972011-09-19 19:01:38 +0000966 continue;
967 }
968
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000969 // Now we know that we want to run this test and record its
970 // success or failure.
971 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000972
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000973 if ((ERROR_NONE == testErrors) &&
974 (kGPU_Backend == gRec[i].fBackend) &&
975 (NULL == rt.get())) {
976 fprintf(stderr, "Could not create render target for gpu.\n");
977 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000978 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000979
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000980 if (ERROR_NONE == testErrors) {
981 testErrors |= test_drawing(gm, gRec[i],
982 writePath, readPath, diffPath,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000983 GetGr(),
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000984 rt.get(), &forwardRenderedBitmap);
985 }
986
junov@google.com4370aed2012-01-18 16:21:08 +0000987 if (doDeferred && !testErrors &&
twiz@google.come24a0792012-01-31 18:35:30 +0000988 (kGPU_Backend == gRec[i].fBackend ||
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000989 kRaster_Backend == gRec[i].fBackend)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000990 testErrors |= test_deferred_drawing(gm, gRec[i],
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000991 forwardRenderedBitmap,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000992 diffPath, GetGr(), rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +0000993 }
994
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000995 if ((ERROR_NONE == testErrors) && doReplay &&
996 !(gmFlags & GM::kSkipPicture_Flag)) {
997 testErrors |= test_picture_playback(gm, gRec[i],
998 forwardRenderedBitmap,
999 readPath, diffPath);
1000 }
1001
scroggo@google.com5af9b202012-06-04 17:17:36 +00001002 if ((ERROR_NONE == testErrors) && doPipe &&
1003 !(gmFlags & GM::kSkipPipe_Flag)) {
1004 testErrors |= test_pipe_playback(gm, gRec[i],
1005 forwardRenderedBitmap,
1006 readPath, diffPath);
1007 }
1008
djsollen@google.coma2ca41e2012-03-23 19:00:34 +00001009 if ((ERROR_NONE == testErrors) && doSerialize &&
1010 !(gmFlags & GM::kSkipPicture_Flag)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001011 testErrors |= test_picture_serialization(gm, gRec[i],
1012 forwardRenderedBitmap,
1013 readPath, diffPath);
1014 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001015
1016 if (!(gmFlags & GM::kSkipPicture_Flag) && writePicturePath) {
1017 write_picture_serialization(gm, gRec[i], writePicturePath);
1018 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001019
1020 // Update overall results.
1021 // We only tabulate the particular error types that we currently
1022 // care about (e.g., missing reference images). Later on, if we
1023 // want to also tabulate pixel mismatches vs dimension mistmatches
1024 // (or whatever else), we can do so.
1025 testsRun++;
1026 if (ERROR_NONE == testErrors) {
1027 testsPassed++;
1028 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
1029 testsMissingReferenceImages++;
1030 } else {
1031 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001032 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001033 }
reed@android.com00dae862009-06-10 15:38:48 +00001034 SkDELETE(gm);
1035 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001036 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
1037 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
robertphillips@google.comf6f123d2012-03-21 17:57:55 +00001038
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001039 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001040}