blob: 760262cb406b82662bb58b5da7b7f1c0f577aabf [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) {
280 if (!isPDF) {
281 canvas->setMatrix(gm->getInitialTransform());
282 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000283 installFilter(canvas);
284 gm->draw(canvas);
285 canvas->setDrawFilter(NULL);
286}
287
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000288static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
289 GrContext* context,
290 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000291 SkBitmap* bitmap,
292 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000293 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000294 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000295
296 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000297 SkCanvas* canvas;
298 if (deferred) {
299 canvas = new SkDeferredCanvas;
300 canvas->setDevice(new SkDevice(*bitmap))->unref();
301 } else {
302 canvas = new SkCanvas(*bitmap);
303 }
304 SkAutoUnref canvasUnref(canvas);
305 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000306 canvas->flush();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000307 } else { // GPU
308 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000309 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000310 }
junov@google.com4370aed2012-01-18 16:21:08 +0000311 SkCanvas* gc;
312 if (deferred) {
313 gc = new SkDeferredCanvas;
314 } else {
315 gc = new SkGpuCanvas(context, rt);
316 }
317 SkAutoUnref gcUnref(gc);
318 gc->setDevice(new SkGpuDevice(context, rt))->unref();
319 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000320 // the device is as large as the current rendertarget, so we explicitly
321 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000322 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000323 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
324 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000325 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000326 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000327 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000328}
329
330static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
331 SkPicture* pict, SkBitmap* bitmap) {
332 SkISize size = gm->getISize();
333 setup_bitmap(gRec, size, bitmap);
334 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000335 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000336 canvas.drawPicture(*pict);
337}
338
339static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
340#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000341 SkMatrix initialTransform = gm->getInitialTransform();
342 SkISize pageSize = gm->getISize();
343 SkPDFDevice* dev = NULL;
344 if (initialTransform.isIdentity()) {
345 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
346 } else {
347 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
348 SkIntToScalar(pageSize.height()));
349 initialTransform.mapRect(&content);
350 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
351 SkIntToScalar(pageSize.height()));
352 SkISize contentSize =
353 SkISize::Make(SkScalarRoundToInt(content.width()),
354 SkScalarRoundToInt(content.height()));
355 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
356 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000357 SkAutoUnref aur(dev);
358
359 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000360 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000361
362 SkPDFDocument doc;
363 doc.appendPage(dev);
364 doc.emitPDF(&pdf);
365#endif
366}
367
bungeman@google.comb29c8832011-10-10 13:19:10 +0000368static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
369#ifdef SK_SUPPORT_XPS
370 SkISize size = gm->getISize();
371
372 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
373 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000374 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
375 static const SkScalar upm = 72 * inchesPerMeter;
376 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
377 static const SkScalar ppm = 200 * inchesPerMeter;
378 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000379
380 SkXPSDevice* dev = new SkXPSDevice();
381 SkAutoUnref aur(dev);
382
383 SkCanvas c(dev);
384 dev->beginPortfolio(&xps);
385 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000386 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000387 dev->endSheet();
388 dev->endPortfolio();
389
390#endif
391}
392
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000393static ErrorBitfield write_reference_image(const ConfigData& gRec,
394 const char writePath [],
395 const char renderModeDescriptor [],
396 const SkString& name,
397 SkBitmap& bitmap,
398 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000399 SkString path;
400 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000401 if (gRec.fBackend == kRaster_Backend ||
402 gRec.fBackend == kGPU_Backend ||
403 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
404
tomhudson@google.comea325432011-06-09 20:30:03 +0000405 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000406 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000407 }
408 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000409 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000410 success = write_document(path, *document);
411 }
412 if (kXPS_Backend == gRec.fBackend) {
413 path = make_filename(writePath, renderModeDescriptor, name, "xps");
414 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000415 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000416 if (success) {
417 return ERROR_NONE;
418 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000419 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000420 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000421 }
422}
423
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000424static ErrorBitfield compare_to_reference_image(const SkString& name,
425 SkBitmap &bitmap,
426 const SkBitmap& comparisonBitmap,
427 const char diffPath [],
428 const char renderModeDescriptor []) {
429 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000430 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000431 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
432 diffPath ? &diffBitmap : NULL);
reed@google.com8e529b72012-04-09 20:20:10 +0000433 if ((ERROR_NONE != errors) && diffPath) {
434 // write out the generated image
435 SkString genName = make_filename(diffPath, "", name, "png");
436 if (!write_bitmap(genName, bitmap)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000437 errors |= ERROR_WRITING_REFERENCE_IMAGE;
438 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000439 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000440 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000441}
442
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000443static ErrorBitfield compare_to_reference_image(const char readPath [],
444 const SkString& name,
445 SkBitmap &bitmap,
446 const char diffPath [],
447 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000448 SkString path = make_filename(readPath, "", name, "png");
449 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000450 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
451 SkBitmap::kARGB_8888_Config,
452 SkImageDecoder::kDecodePixels_Mode, NULL)) {
453 return compare_to_reference_image(name, bitmap,
454 orig, diffPath,
455 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000456 } else {
reed@google.come8fcb502012-05-17 15:28:20 +0000457 if (gNotifyMissingReadReference) {
458 fprintf(stderr, "FAILED to read %s\n", path.c_str());
459 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000460 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000461 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000462}
463
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000464static ErrorBitfield handle_test_results(GM* gm,
465 const ConfigData& gRec,
466 const char writePath [],
467 const char readPath [],
468 const char diffPath [],
469 const char renderModeDescriptor [],
470 SkBitmap& bitmap,
471 SkDynamicMemoryWStream* pdf,
472 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000473 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000474 ErrorBitfield retval = ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000475
bsalomon@google.com7361f542012-04-19 19:15:35 +0000476 if (readPath && (gRec.fFlags & kRead_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000477 retval |= compare_to_reference_image(readPath, name, bitmap,
478 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000479 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000480 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000481 retval |= write_reference_image(gRec, writePath, renderModeDescriptor,
482 name, bitmap, pdf);
483 }
484 if (comparisonBitmap) {
485 retval |= compare_to_reference_image(name, bitmap,
486 *comparisonBitmap, diffPath,
487 renderModeDescriptor);
488 }
489 return retval;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000490}
491
492static SkPicture* generate_new_picture(GM* gm) {
493 // Pictures are refcounted so must be on heap
494 SkPicture* pict = new SkPicture;
495 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000496 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000497 pict->endRecording();
498
499 return pict;
500}
501
502static SkPicture* stream_to_new_picture(const SkPicture& src) {
503
504 // To do in-memory commiunications with a stream, we need to:
505 // * create a dynamic memory stream
506 // * copy it into a buffer
507 // * create a read stream from it
508 // ?!?!
509
510 SkDynamicMemoryWStream storage;
511 src.serialize(&storage);
512
513 int streamSize = storage.getOffset();
514 SkAutoMalloc dstStorage(streamSize);
515 void* dst = dstStorage.get();
516 //char* dst = new char [streamSize];
517 //@todo thudson 22 April 2011 when can we safely delete [] dst?
518 storage.copyTo(dst);
519 SkMemoryStream pictReadback(dst, streamSize);
520 SkPicture* retval = new SkPicture (&pictReadback);
521 return retval;
522}
523
524// Test: draw into a bitmap or pdf.
525// Depending on flags, possibly compare to an expected image
526// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000527static ErrorBitfield test_drawing(GM* gm,
528 const ConfigData& gRec,
529 const char writePath [],
530 const char readPath [],
531 const char diffPath [],
532 GrContext* context,
533 GrRenderTarget* rt,
534 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000535 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000536
537 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000538 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000539 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000540 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
541 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000542 if (ERROR_NONE != errors) {
543 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000544 }
reed@google.com46cce912011-06-29 12:54:46 +0000545 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000546 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000547#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000548 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000549 SkMemoryStream stream(data.data(), data.size());
550 SkPDFDocumentToBitmap(&stream, bitmap);
551#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000552 } else if (gRec.fBackend == kXPS_Backend) {
553 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000554 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000555 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000556 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000557}
558
junov@google.com4370aed2012-01-18 16:21:08 +0000559static ErrorBitfield test_deferred_drawing(GM* gm,
560 const ConfigData& gRec,
561 const SkBitmap& comparisonBitmap,
562 const char diffPath [],
563 GrContext* context,
564 GrRenderTarget* rt) {
565 SkDynamicMemoryWStream document;
566
567 if (gRec.fBackend == kRaster_Backend ||
568 gRec.fBackend == kGPU_Backend) {
569 SkBitmap bitmap;
570 // Early exit if we can't generate the image, but this is
571 // expected in some cases, so don't report a test failure.
572 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
573 return ERROR_NONE;
574 }
575 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
576 "-deferred", bitmap, NULL, &comparisonBitmap);
577 }
578 return ERROR_NONE;
579}
580
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000581static ErrorBitfield test_picture_playback(GM* gm,
582 const ConfigData& gRec,
583 const SkBitmap& comparisonBitmap,
584 const char readPath [],
585 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000586 SkPicture* pict = generate_new_picture(gm);
587 SkAutoUnref aur(pict);
588
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000589 if (kRaster_Backend == gRec.fBackend) {
590 SkBitmap bitmap;
591 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000592 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
593 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000594 } else {
595 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000596 }
597}
598
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000599static ErrorBitfield test_picture_serialization(GM* gm,
600 const ConfigData& gRec,
601 const SkBitmap& comparisonBitmap,
602 const char readPath [],
603 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000604 SkPicture* pict = generate_new_picture(gm);
605 SkAutoUnref aurp(pict);
606 SkPicture* repict = stream_to_new_picture(*pict);
607 SkAutoUnref aurr(repict);
608
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000609 if (kRaster_Backend == gRec.fBackend) {
610 SkBitmap bitmap;
611 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000612 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
613 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000614 } else {
615 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000616 }
617}
618
scroggo@google.com5af9b202012-06-04 17:17:36 +0000619class PipeController : public SkGPipeController {
620public:
621 PipeController(SkCanvas* target);
622 ~PipeController();
623 virtual void* requestBlock(size_t minRequest, size_t* actual);
624 virtual void notifyWritten(size_t bytes);
625private:
626 SkGPipeReader fReader;
627 void* fBlock;
628 size_t fBlockSize;
629 size_t fBytesWritten;
630 SkGPipeReader::Status fStatus;
631};
632
633PipeController::PipeController(SkCanvas* target)
634:fReader(target) {
635 fBlock = NULL;
636 fBlockSize = fBytesWritten = 0;
637}
638
639PipeController::~PipeController() {
640 sk_free(fBlock);
641}
642
643void* PipeController::requestBlock(size_t minRequest, size_t *actual) {
644 sk_free(fBlock);
645 fBlockSize = minRequest * 4;
646 fBlock = sk_malloc_throw(fBlockSize);
647 fBytesWritten = 0;
648 *actual = fBlockSize;
649 return fBlock;
650}
651
652void PipeController::notifyWritten(size_t bytes) {
653 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
654 SkASSERT(SkGPipeReader::kError_Status != fStatus);
655 fBytesWritten += bytes;
656}
657
658static ErrorBitfield test_pipe_playback(GM* gm,
659 const ConfigData& gRec,
660 const SkBitmap& comparisonBitmap,
661 const char readPath [],
662 const char diffPath []) {
663 if (kRaster_Backend != gRec.fBackend) {
664 return ERROR_NONE;
665 }
666 SkBitmap bitmap;
667 SkISize size = gm->getISize();
668 setup_bitmap(gRec, size, &bitmap);
669 SkCanvas canvas(bitmap);
670 PipeController pipeController(&canvas);
671 SkGPipeWriter writer;
672 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
673 SkGPipeWriter::kCrossProcess_Flag);
674 invokeGM(gm, pipeCanvas);
675 writer.endRecording();
676 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
677 "-pipe", bitmap, NULL, &comparisonBitmap);
678}
679
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000680static void write_picture_serialization(GM* gm, const ConfigData& rec,
681 const char writePicturePath[]) {
682 // only do this once, so we pick raster
683 if (kRaster_Backend == rec.fBackend &&
684 SkBitmap::kARGB_8888_Config == rec.fConfig) {
685
686 SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
687
688 const char* pictureSuffix = "skp";
689 SkString path = make_filename(writePicturePath, "",
690 SkString(gm->shortName()), pictureSuffix);
691
692 SkFILEWStream stream(path.c_str());
693 pict->serialize(&stream);
694 }
695}
696
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000697static void usage(const char * argv0) {
junov@google.com77e498e2012-01-18 18:56:34 +0000698 SkDebugf(
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000699 "%s [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n"
scroggo@google.com5af9b202012-06-04 17:17:36 +0000700 " [--noreplay] [--nopipe] [--serialize] [--forceBWtext] [--nopdf] \n"
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000701 " [--nodeferred] [--match substring] [--notexturecache]\n"
bsalomon@google.com7361f542012-04-19 19:15:35 +0000702 , argv0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000703 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000704 SkDebugf(
705" readPath: directory to read reference images from;\n"
706" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000707 SkDebugf(" diffPath: directory to write difference images in.\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000708 SkDebugf(" resourcePath: directory that stores image resources.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000709 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
scroggo@google.com5af9b202012-06-04 17:17:36 +0000710 SkDebugf(" --nopipe: Skip SkGPipe replay.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000711 SkDebugf(
712" --serialize: exercise SkPicture serialization & deserialization.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000713 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
714 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
715 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000716 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
twiz@google.come24a0792012-01-31 18:35:30 +0000717 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000718}
719
bsalomon@google.com7361f542012-04-19 19:15:35 +0000720static const GrContextFactory::GLContextType kDontCare_GLContextType =
721 GrContextFactory::kNative_GLContextType;
722
723// If the platform does not support writing PNGs of PDFs then there will be no
724// comparison images to read. However, we can always write the .pdf files
725static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
726 kWrite_ConfigFlag;
727
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000728static const ConfigData gRec[] = {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000729 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888" },
730 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444" },
731 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000732#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.com7361f542012-04-19 19:15:35 +0000733 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu" },
734 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16" },
735 /* The debug context does not generate images */
736 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "debug" },
737 #ifdef SK_ANGLE
738 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle" },
739 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16" },
740 #endif
741 #ifdef SK_MESA
742 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa" },
743 #endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000744#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000745#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000746 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
747 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps" },
748#endif
749#ifdef SK_SUPPORT_PDF
750 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf" },
bungeman@google.comb29c8832011-10-10 13:19:10 +0000751#endif
reed@android.com00dae862009-06-10 15:38:48 +0000752};
753
reed@google.comb2a51622011-10-31 16:30:04 +0000754static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
755 if (0 == array.count()) {
756 // no names, so don't skip anything
757 return false;
758 }
759 for (int i = 0; i < array.count(); ++i) {
760 if (strstr(name, array[i])) {
761 // found the name, so don't skip
762 return false;
763 }
764 }
765 return true;
766}
767
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000768namespace skiagm {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000769SkAutoTUnref<GrContext> gGrContext;
770/**
771 * Sets the global GrContext, accessible by indivual GMs
772 */
773void SetGr(GrContext* grContext) {
774 SkSafeRef(grContext);
775 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000776}
bsalomon@google.com7361f542012-04-19 19:15:35 +0000777
778/**
779 * Gets the global GrContext, can be called by GM tests.
780 */
781GrContext* GetGr() {
782 return gGrContext.get();
783}
784
785/**
786 * Sets the global GrContext and then resets it to its previous value at
787 * destruction.
788 */
789class AutoResetGr : SkNoncopyable {
790public:
791 AutoResetGr() : fOld(NULL) {}
792 void set(GrContext* context) {
793 SkASSERT(NULL == fOld);
794 fOld = GetGr();
795 SkSafeRef(fOld);
796 SetGr(context);
797 }
798 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
799private:
800 GrContext* fOld;
801};
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000802}
803
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000804int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000805 SkAutoGraphics ag;
reed@google.com8923c6c2011-11-08 14:59:38 +0000806 // we don't need to see this during a run
807 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000808
epoger@google.com7bc13a62012-02-14 14:53:59 +0000809 setSystemPreferences();
810
reed@android.com8015dd82009-06-21 00:49:18 +0000811 const char* writePath = NULL; // if non-null, where we write the originals
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000812 const char* writePicturePath = NULL; // if non-null, where we write serialized pictures
reed@android.com8015dd82009-06-21 00:49:18 +0000813 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000814 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000815 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000816
reed@google.comb2a51622011-10-31 16:30:04 +0000817 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000818
reed@google.comab973972011-09-19 19:01:38 +0000819 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000820 bool doReplay = true;
scroggo@google.com5af9b202012-06-04 17:17:36 +0000821 bool doPipe = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000822 bool doSerialize = false;
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000823 bool useDebugGL = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000824 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000825 bool disableTextureCache = false;
826
reed@google.come8fcb502012-05-17 15:28:20 +0000827 gNotifyMissingReadReference = true;
828
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000829 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000830 char* const* stop = argv + argc;
831 for (++argv; argv < stop; ++argv) {
832 if (strcmp(*argv, "-w") == 0) {
833 argv++;
834 if (argv < stop && **argv) {
835 writePath = *argv;
836 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000837 } else if (strcmp(*argv, "-wp") == 0) {
838 argv++;
839 if (argv < stop && **argv) {
840 writePicturePath = *argv;
841 }
reed@android.com8015dd82009-06-21 00:49:18 +0000842 } else if (strcmp(*argv, "-r") == 0) {
843 argv++;
844 if (argv < stop && **argv) {
845 readPath = *argv;
846 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000847 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000848 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000849 if (argv < stop && **argv) {
850 diffPath = *argv;
851 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000852 } else if (strcmp(*argv, "-i") == 0) {
853 argv++;
854 if (argv < stop && **argv) {
855 resourcePath = *argv;
856 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000857 } else if (strcmp(*argv, "--forceBWtext") == 0) {
858 gForceBWtext = true;
scroggo@google.com5af9b202012-06-04 17:17:36 +0000859 } else if (strcmp(*argv, "--nopipe") == 0) {
860 doPipe = false;
reed@google.comb8b09832011-05-26 15:57:56 +0000861 } else if (strcmp(*argv, "--noreplay") == 0) {
862 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000863 } else if (strcmp(*argv, "--nopdf") == 0) {
864 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000865 } else if (strcmp(*argv, "--nodeferred") == 0) {
866 doDeferred = false;
reed@google.come8fcb502012-05-17 15:28:20 +0000867 } else if (strcmp(*argv, "--disable-missing-warning") == 0) {
868 gNotifyMissingReadReference = false;
869 } else if (strcmp(*argv, "--enable-missing-warning") == 0) {
870 gNotifyMissingReadReference = true;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000871 } else if (strcmp(*argv, "--serialize") == 0) {
872 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000873 } else if (strcmp(*argv, "--match") == 0) {
874 ++argv;
875 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000876 // just record the ptr, no need for a deep copy
877 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000878 }
twiz@google.come24a0792012-01-31 18:35:30 +0000879 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000880 disableTextureCache = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000881 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000882 usage(commandName);
883 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000884 }
885 }
886 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000887 usage(commandName);
888 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000889 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000890
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000891 GM::SetResourcePath(resourcePath);
892
bsalomon@google.com7361f542012-04-19 19:15:35 +0000893 GrContextFactory grFactory;
reed@google.com873cb1e2010-12-23 15:00:45 +0000894
reed@android.com00f883e2010-12-14 17:46:14 +0000895 if (readPath) {
896 fprintf(stderr, "reading from %s\n", readPath);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000897 }
898 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +0000899 fprintf(stderr, "writing to %s\n", writePath);
900 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000901 if (writePicturePath) {
902 fprintf(stderr, "writing pictures to %s\n", writePicturePath);
903 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000904 if (resourcePath) {
905 fprintf(stderr, "reading resources from %s\n", resourcePath);
906 }
907
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000908 // Accumulate success of all tests.
909 int testsRun = 0;
910 int testsPassed = 0;
911 int testsFailed = 0;
912 int testsMissingReferenceImages = 0;
913
twiz@google.come24a0792012-01-31 18:35:30 +0000914 if (disableTextureCache) {
915 skiagm::GetGr()->setTextureCacheLimits(0, 0);
916 }
917
bsalomon@google.com7361f542012-04-19 19:15:35 +0000918 Iter iter;
919 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +0000920 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000921 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000922 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000923 SkDELETE(gm);
924 continue;
925 }
926
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000927 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000928 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000929 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000930 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000931
bsalomon@google.com29d35012011-11-30 16:57:21 +0000932 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000933 SkAutoTUnref<GrRenderTarget> rt;
934 AutoResetGr autogr;
935 if (kGPU_Backend == gRec[i].fBackend) {
936 GrContext* gr = grFactory.get(gRec[i].fGLContextType);
937 if (!gr) {
938 continue;
939 }
940
941 // create a render target to back the device
942 GrTextureDesc desc;
943 desc.fConfig = kSkia8888_PM_GrPixelConfig;
944 desc.fFlags = kRenderTarget_GrTextureFlagBit;
945 desc.fWidth = gm->getISize().width();
946 desc.fHeight = gm->getISize().height();
947 desc.fSampleCnt = gRec[i].fSampleCnt;
948 GrTexture* tex = gr->createUncachedTexture(desc, NULL, 0);
949 if (!tex) {
bsalomon@google.comdaf12bb2012-04-20 19:08:44 +0000950 continue;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000951 }
952 rt.reset(tex->asRenderTarget());
953 rt.get()->ref();
954 tex->unref();
955
956 autogr.set(gr);
957 }
958
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000959 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000960 uint32_t gmFlags = gm->getFlags();
twiz@google.come24a0792012-01-31 18:35:30 +0000961 if ((kPDF_Backend == gRec[i].fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +0000962 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
963 {
reed@google.comab973972011-09-19 19:01:38 +0000964 continue;
965 }
966
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000967 // Now we know that we want to run this test and record its
968 // success or failure.
969 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000970
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000971 if ((ERROR_NONE == testErrors) &&
972 (kGPU_Backend == gRec[i].fBackend) &&
973 (NULL == rt.get())) {
974 fprintf(stderr, "Could not create render target for gpu.\n");
975 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000976 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000977
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000978 if (ERROR_NONE == testErrors) {
979 testErrors |= test_drawing(gm, gRec[i],
980 writePath, readPath, diffPath,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000981 GetGr(),
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000982 rt.get(), &forwardRenderedBitmap);
983 }
984
junov@google.com4370aed2012-01-18 16:21:08 +0000985 if (doDeferred && !testErrors &&
twiz@google.come24a0792012-01-31 18:35:30 +0000986 (kGPU_Backend == gRec[i].fBackend ||
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000987 kRaster_Backend == gRec[i].fBackend)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000988 testErrors |= test_deferred_drawing(gm, gRec[i],
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000989 forwardRenderedBitmap,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000990 diffPath, GetGr(), rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +0000991 }
992
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000993 if ((ERROR_NONE == testErrors) && doReplay &&
994 !(gmFlags & GM::kSkipPicture_Flag)) {
995 testErrors |= test_picture_playback(gm, gRec[i],
996 forwardRenderedBitmap,
997 readPath, diffPath);
998 }
999
scroggo@google.com5af9b202012-06-04 17:17:36 +00001000 if ((ERROR_NONE == testErrors) && doPipe &&
1001 !(gmFlags & GM::kSkipPipe_Flag)) {
1002 testErrors |= test_pipe_playback(gm, gRec[i],
1003 forwardRenderedBitmap,
1004 readPath, diffPath);
1005 }
1006
djsollen@google.coma2ca41e2012-03-23 19:00:34 +00001007 if ((ERROR_NONE == testErrors) && doSerialize &&
1008 !(gmFlags & GM::kSkipPicture_Flag)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001009 testErrors |= test_picture_serialization(gm, gRec[i],
1010 forwardRenderedBitmap,
1011 readPath, diffPath);
1012 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001013
1014 if (!(gmFlags & GM::kSkipPicture_Flag) && writePicturePath) {
1015 write_picture_serialization(gm, gRec[i], writePicturePath);
1016 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001017
1018 // Update overall results.
1019 // We only tabulate the particular error types that we currently
1020 // care about (e.g., missing reference images). Later on, if we
1021 // want to also tabulate pixel mismatches vs dimension mistmatches
1022 // (or whatever else), we can do so.
1023 testsRun++;
1024 if (ERROR_NONE == testErrors) {
1025 testsPassed++;
1026 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
1027 testsMissingReferenceImages++;
1028 } else {
1029 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001030 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001031 }
reed@android.com00dae862009-06-10 15:38:48 +00001032 SkDELETE(gm);
1033 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001034 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
1035 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
robertphillips@google.comf6f123d2012-03-21 17:57:55 +00001036
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001037 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001038}