blob: c6159e39445982b50b087c16d16c877c5dbc33e6 [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"
robertphillips@google.com977b9c82012-06-05 19:35:09 +000024#include "SkRefCnt.h"
reed@google.com07700442010-12-20 19:46:07 +000025
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000026static bool gForceBWtext;
27
reed@google.com8923c6c2011-11-08 14:59:38 +000028extern bool gSkSuppressFontCachePurgeSpew;
29
reed@google.com07700442010-12-20 19:46:07 +000030#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000031 #include "SkPDFDevice.h"
32 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000033#endif
reed@android.com00dae862009-06-10 15:38:48 +000034
epoger@google.come3cc2eb2012-01-18 20:11:13 +000035// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
36// stop writing out XPS-format image baselines in gm.
37#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000038#ifdef SK_SUPPORT_XPS
39 #include "SkXPSDevice.h"
40#endif
41
reed@google.com46cce912011-06-29 12:54:46 +000042#ifdef SK_BUILD_FOR_MAC
43 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000044 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000045#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000046 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000047#endif
48
epoger@google.comc7cf2b32011-12-28 19:31:01 +000049typedef int ErrorBitfield;
50const static ErrorBitfield ERROR_NONE = 0x00;
51const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
52const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
53const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
54const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
55const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
56
reed@google.come8fcb502012-05-17 15:28:20 +000057// If true, emit a messange when we can't find a reference image to compare
58static bool gNotifyMissingReadReference;
59
reed@android.com00dae862009-06-10 15:38:48 +000060using namespace skiagm;
61
reed@android.com00dae862009-06-10 15:38:48 +000062class Iter {
63public:
64 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000065 this->reset();
66 }
67
68 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000069 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000070 }
reed@google.comd4dfd102011-01-18 21:05:42 +000071
reed@android.comdd0ac282009-06-20 02:38:16 +000072 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000073 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000074 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000075 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000076 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000077 }
78 return NULL;
79 }
reed@google.comd4dfd102011-01-18 21:05:42 +000080
reed@android.com00dae862009-06-10 15:38:48 +000081 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000082 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000083 int count = 0;
84 while (reg) {
85 count += 1;
86 reg = reg->next();
87 }
88 return count;
89 }
reed@google.comd4dfd102011-01-18 21:05:42 +000090
reed@android.com00dae862009-06-10 15:38:48 +000091private:
92 const GMRegistry* fReg;
93};
94
reed@android.com8015dd82009-06-21 00:49:18 +000095static SkString make_name(const char shortName[], const char configName[]) {
96 SkString name(shortName);
97 name.appendf("_%s", configName);
98 return name;
99}
100
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000101static SkString make_filename(const char path[],
102 const char pathSuffix[],
103 const SkString& name,
104 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000105 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000106 if (filename.endsWith("/")) {
107 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000108 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000109 filename.append(pathSuffix);
110 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000111 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000112 return filename;
113}
114
reed@android.comb9b9a182009-07-08 02:54:47 +0000115/* since PNG insists on unpremultiplying our alpha, we take no precision chances
116 and force all pixels to be 100% opaque, otherwise on compare we may not get
117 a perfect match.
118 */
119static void force_all_opaque(const SkBitmap& bitmap) {
120 SkAutoLockPixels lock(bitmap);
121 for (int y = 0; y < bitmap.height(); y++) {
122 for (int x = 0; x < bitmap.width(); x++) {
123 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
124 }
125 }
126}
127
128static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
129 SkBitmap copy;
130 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
131 force_all_opaque(copy);
132 return SkImageEncoder::EncodeFile(path.c_str(), copy,
133 SkImageEncoder::kPNG_Type, 100);
134}
135
reed@google.com3d3f0922010-12-20 21:10:29 +0000136static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000137 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
138 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
139 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
140 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000141}
142
143static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000144 SkBitmap* diff) {
145 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000146
147 const int w = target.width();
148 const int h = target.height();
149 for (int y = 0; y < h; y++) {
150 for (int x = 0; x < w; x++) {
151 SkPMColor c0 = *base.getAddr32(x, y);
152 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000153 SkPMColor d = 0;
154 if (c0 != c1) {
155 d = compute_diff_pmcolor(c0, c1);
156 }
157 *diff->getAddr32(x, y) = d;
158 }
159 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000160}
161
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000162static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
163 const SkString& name,
164 const char* renderModeDescriptor,
165 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000166 SkBitmap copy;
167 const SkBitmap* bm = &target;
168 if (target.config() != SkBitmap::kARGB_8888_Config) {
169 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
170 bm = &copy;
171 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000172 SkBitmap baseCopy;
173 const SkBitmap* bp = &base;
174 if (base.config() != SkBitmap::kARGB_8888_Config) {
175 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
176 bp = &baseCopy;
177 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000178
179 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000180 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000181
182 const int w = bm->width();
183 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000184 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000185 SkDebugf(
186"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
187 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000188 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000189 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000190 }
191
192 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000193 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000194
195 for (int y = 0; y < h; y++) {
196 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000197 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000198 SkPMColor c1 = *bm->getAddr32(x, y);
199 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000200 SkDebugf(
201"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
202 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000203
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000204 if (diff) {
205 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
206 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000207 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000208 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000209 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000210 }
211 }
212 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000213
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000214 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000215 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000216}
reed@android.com00dae862009-06-10 15:38:48 +0000217
bungeman@google.comb29c8832011-10-10 13:19:10 +0000218static bool write_document(const SkString& path,
219 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000220 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000221 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000222 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000223}
224
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000225enum Backend {
226 kRaster_Backend,
227 kGPU_Backend,
228 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000229 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000230};
231
bsalomon@google.com7361f542012-04-19 19:15:35 +0000232enum ConfigFlags {
233 kNone_ConfigFlag = 0x0,
234 /* Write GM images if a write path is provided. */
235 kWrite_ConfigFlag = 0x1,
236 /* Read comparison GM images if a read path is provided. */
237 kRead_ConfigFlag = 0x2,
238 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
239};
240
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000241struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000242 SkBitmap::Config fConfig;
243 Backend fBackend;
244 GrContextFactory::GLContextType fGLContextType; // GPU backend only
245 int fSampleCnt; // GPU backend only
246 ConfigFlags fFlags;
247 const char* fName;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000248};
249
250/// Returns true if processing should continue, false to skip the
251/// remainder of this config for this GM.
252//@todo thudson 22 April 2011 - could refactor this to take in
253// a factory to generate the context, always call readPixels()
254// (logically a noop for rasters, if wasted time), and thus collapse the
255// GPU special case and also let this be used for SkPicture testing.
256static void setup_bitmap(const ConfigData& gRec, SkISize& size,
257 SkBitmap* bitmap) {
258 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
259 bitmap->allocPixels();
260 bitmap->eraseColor(0);
261}
262
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000263#include "SkDrawFilter.h"
264class BWTextDrawFilter : public SkDrawFilter {
265public:
266 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
267};
268void BWTextDrawFilter::filter(SkPaint* p, Type t) {
269 if (kText_Type == t) {
270 p->setAntiAlias(false);
271 }
272}
273
274static void installFilter(SkCanvas* canvas) {
275 if (gForceBWtext) {
276 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
277 }
278}
279
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000280static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
reed@google.com778e1632012-06-04 20:00:01 +0000281 SkAutoCanvasRestore acr(canvas, true);
282
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000283 if (!isPDF) {
reed@google.com778e1632012-06-04 20:00:01 +0000284 canvas->concat(gm->getInitialTransform());
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000285 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000286 installFilter(canvas);
287 gm->draw(canvas);
288 canvas->setDrawFilter(NULL);
289}
290
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000291static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
292 GrContext* context,
293 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000294 SkBitmap* bitmap,
295 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000296 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000297 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000298
299 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000300 SkCanvas* canvas;
301 if (deferred) {
302 canvas = new SkDeferredCanvas;
303 canvas->setDevice(new SkDevice(*bitmap))->unref();
304 } else {
305 canvas = new SkCanvas(*bitmap);
306 }
307 SkAutoUnref canvasUnref(canvas);
308 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000309 canvas->flush();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000310 } else { // GPU
311 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000312 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000313 }
junov@google.com4370aed2012-01-18 16:21:08 +0000314 SkCanvas* gc;
315 if (deferred) {
316 gc = new SkDeferredCanvas;
317 } else {
318 gc = new SkGpuCanvas(context, rt);
319 }
320 SkAutoUnref gcUnref(gc);
321 gc->setDevice(new SkGpuDevice(context, rt))->unref();
322 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000323 // the device is as large as the current rendertarget, so we explicitly
324 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000325 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000326 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
327 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000328 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000329 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000330 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000331}
332
333static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
334 SkPicture* pict, SkBitmap* bitmap) {
335 SkISize size = gm->getISize();
336 setup_bitmap(gRec, size, bitmap);
337 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000338 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000339 canvas.drawPicture(*pict);
340}
341
342static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
343#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000344 SkMatrix initialTransform = gm->getInitialTransform();
345 SkISize pageSize = gm->getISize();
346 SkPDFDevice* dev = NULL;
347 if (initialTransform.isIdentity()) {
348 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
349 } else {
350 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
351 SkIntToScalar(pageSize.height()));
352 initialTransform.mapRect(&content);
353 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
354 SkIntToScalar(pageSize.height()));
355 SkISize contentSize =
356 SkISize::Make(SkScalarRoundToInt(content.width()),
357 SkScalarRoundToInt(content.height()));
358 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
359 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000360 SkAutoUnref aur(dev);
361
362 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000363 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000364
365 SkPDFDocument doc;
366 doc.appendPage(dev);
367 doc.emitPDF(&pdf);
368#endif
369}
370
bungeman@google.comb29c8832011-10-10 13:19:10 +0000371static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
372#ifdef SK_SUPPORT_XPS
373 SkISize size = gm->getISize();
374
375 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
376 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000377 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
378 static const SkScalar upm = 72 * inchesPerMeter;
379 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
380 static const SkScalar ppm = 200 * inchesPerMeter;
381 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000382
383 SkXPSDevice* dev = new SkXPSDevice();
384 SkAutoUnref aur(dev);
385
386 SkCanvas c(dev);
387 dev->beginPortfolio(&xps);
388 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000389 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000390 dev->endSheet();
391 dev->endPortfolio();
392
393#endif
394}
395
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000396static ErrorBitfield write_reference_image(const ConfigData& gRec,
397 const char writePath [],
398 const char renderModeDescriptor [],
399 const SkString& name,
400 SkBitmap& bitmap,
401 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000402 SkString path;
403 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000404 if (gRec.fBackend == kRaster_Backend ||
405 gRec.fBackend == kGPU_Backend ||
406 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
407
tomhudson@google.comea325432011-06-09 20:30:03 +0000408 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000409 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000410 }
411 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000412 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000413 success = write_document(path, *document);
414 }
415 if (kXPS_Backend == gRec.fBackend) {
416 path = make_filename(writePath, renderModeDescriptor, name, "xps");
417 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000418 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000419 if (success) {
420 return ERROR_NONE;
421 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000422 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000423 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000424 }
425}
426
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000427static ErrorBitfield compare_to_reference_image(const SkString& name,
428 SkBitmap &bitmap,
429 const SkBitmap& comparisonBitmap,
430 const char diffPath [],
431 const char renderModeDescriptor []) {
432 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000433 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000434 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
435 diffPath ? &diffBitmap : NULL);
reed@google.com8e529b72012-04-09 20:20:10 +0000436 if ((ERROR_NONE != errors) && diffPath) {
437 // write out the generated image
438 SkString genName = make_filename(diffPath, "", name, "png");
439 if (!write_bitmap(genName, bitmap)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000440 errors |= ERROR_WRITING_REFERENCE_IMAGE;
441 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000442 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000443 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000444}
445
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000446static ErrorBitfield compare_to_reference_image(const char readPath [],
447 const SkString& name,
448 SkBitmap &bitmap,
449 const char diffPath [],
450 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000451 SkString path = make_filename(readPath, "", name, "png");
452 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000453 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
454 SkBitmap::kARGB_8888_Config,
455 SkImageDecoder::kDecodePixels_Mode, NULL)) {
456 return compare_to_reference_image(name, bitmap,
457 orig, diffPath,
458 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000459 } else {
reed@google.come8fcb502012-05-17 15:28:20 +0000460 if (gNotifyMissingReadReference) {
461 fprintf(stderr, "FAILED to read %s\n", path.c_str());
462 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000463 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000464 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000465}
466
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000467static ErrorBitfield handle_test_results(GM* gm,
468 const ConfigData& gRec,
469 const char writePath [],
470 const char readPath [],
471 const char diffPath [],
472 const char renderModeDescriptor [],
473 SkBitmap& bitmap,
474 SkDynamicMemoryWStream* pdf,
475 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000476 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000477 ErrorBitfield retval = ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000478
bsalomon@google.com7361f542012-04-19 19:15:35 +0000479 if (readPath && (gRec.fFlags & kRead_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000480 retval |= compare_to_reference_image(readPath, name, bitmap,
481 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000482 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000483 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000484 retval |= write_reference_image(gRec, writePath, renderModeDescriptor,
485 name, bitmap, pdf);
486 }
487 if (comparisonBitmap) {
488 retval |= compare_to_reference_image(name, bitmap,
489 *comparisonBitmap, diffPath,
490 renderModeDescriptor);
491 }
492 return retval;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000493}
494
495static SkPicture* generate_new_picture(GM* gm) {
496 // Pictures are refcounted so must be on heap
497 SkPicture* pict = new SkPicture;
498 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000499 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000500 pict->endRecording();
501
502 return pict;
503}
504
505static SkPicture* stream_to_new_picture(const SkPicture& src) {
506
507 // To do in-memory commiunications with a stream, we need to:
508 // * create a dynamic memory stream
509 // * copy it into a buffer
510 // * create a read stream from it
511 // ?!?!
512
513 SkDynamicMemoryWStream storage;
514 src.serialize(&storage);
515
516 int streamSize = storage.getOffset();
517 SkAutoMalloc dstStorage(streamSize);
518 void* dst = dstStorage.get();
519 //char* dst = new char [streamSize];
520 //@todo thudson 22 April 2011 when can we safely delete [] dst?
521 storage.copyTo(dst);
522 SkMemoryStream pictReadback(dst, streamSize);
523 SkPicture* retval = new SkPicture (&pictReadback);
524 return retval;
525}
526
527// Test: draw into a bitmap or pdf.
528// Depending on flags, possibly compare to an expected image
529// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000530static ErrorBitfield test_drawing(GM* gm,
531 const ConfigData& gRec,
532 const char writePath [],
533 const char readPath [],
534 const char diffPath [],
535 GrContext* context,
536 GrRenderTarget* rt,
537 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000538 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000539
540 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000541 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000542 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000543 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
544 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000545 if (ERROR_NONE != errors) {
546 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000547 }
reed@google.com46cce912011-06-29 12:54:46 +0000548 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000549 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000550#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000551 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000552 SkMemoryStream stream(data.data(), data.size());
553 SkPDFDocumentToBitmap(&stream, bitmap);
554#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000555 } else if (gRec.fBackend == kXPS_Backend) {
556 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000557 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000558 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000559 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000560}
561
junov@google.com4370aed2012-01-18 16:21:08 +0000562static ErrorBitfield test_deferred_drawing(GM* gm,
563 const ConfigData& gRec,
564 const SkBitmap& comparisonBitmap,
565 const char diffPath [],
566 GrContext* context,
567 GrRenderTarget* rt) {
568 SkDynamicMemoryWStream document;
569
570 if (gRec.fBackend == kRaster_Backend ||
571 gRec.fBackend == kGPU_Backend) {
572 SkBitmap bitmap;
573 // Early exit if we can't generate the image, but this is
574 // expected in some cases, so don't report a test failure.
575 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
576 return ERROR_NONE;
577 }
578 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
579 "-deferred", bitmap, NULL, &comparisonBitmap);
580 }
581 return ERROR_NONE;
582}
583
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000584static ErrorBitfield test_picture_playback(GM* gm,
585 const ConfigData& gRec,
586 const SkBitmap& comparisonBitmap,
587 const char readPath [],
588 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000589 SkPicture* pict = generate_new_picture(gm);
590 SkAutoUnref aur(pict);
591
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000592 if (kRaster_Backend == gRec.fBackend) {
593 SkBitmap bitmap;
594 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000595 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
596 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000597 } else {
598 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000599 }
600}
601
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000602static ErrorBitfield test_picture_serialization(GM* gm,
603 const ConfigData& gRec,
604 const SkBitmap& comparisonBitmap,
605 const char readPath [],
606 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000607 SkPicture* pict = generate_new_picture(gm);
608 SkAutoUnref aurp(pict);
609 SkPicture* repict = stream_to_new_picture(*pict);
610 SkAutoUnref aurr(repict);
611
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000612 if (kRaster_Backend == gRec.fBackend) {
613 SkBitmap bitmap;
614 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000615 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
616 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000617 } else {
618 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000619 }
620}
621
scroggo@google.com5af9b202012-06-04 17:17:36 +0000622class PipeController : public SkGPipeController {
623public:
624 PipeController(SkCanvas* target);
625 ~PipeController();
626 virtual void* requestBlock(size_t minRequest, size_t* actual);
627 virtual void notifyWritten(size_t bytes);
628private:
629 SkGPipeReader fReader;
630 void* fBlock;
631 size_t fBlockSize;
632 size_t fBytesWritten;
633 SkGPipeReader::Status fStatus;
634};
635
636PipeController::PipeController(SkCanvas* target)
637:fReader(target) {
638 fBlock = NULL;
639 fBlockSize = fBytesWritten = 0;
640}
641
642PipeController::~PipeController() {
643 sk_free(fBlock);
644}
645
646void* PipeController::requestBlock(size_t minRequest, size_t *actual) {
647 sk_free(fBlock);
648 fBlockSize = minRequest * 4;
649 fBlock = sk_malloc_throw(fBlockSize);
650 fBytesWritten = 0;
651 *actual = fBlockSize;
652 return fBlock;
653}
654
655void PipeController::notifyWritten(size_t bytes) {
656 fStatus = fReader.playback((const char*)fBlock + fBytesWritten, bytes);
657 SkASSERT(SkGPipeReader::kError_Status != fStatus);
658 fBytesWritten += bytes;
659}
660
661static ErrorBitfield test_pipe_playback(GM* gm,
662 const ConfigData& gRec,
663 const SkBitmap& comparisonBitmap,
664 const char readPath [],
665 const char diffPath []) {
666 if (kRaster_Backend != gRec.fBackend) {
667 return ERROR_NONE;
668 }
669 SkBitmap bitmap;
670 SkISize size = gm->getISize();
671 setup_bitmap(gRec, size, &bitmap);
672 SkCanvas canvas(bitmap);
673 PipeController pipeController(&canvas);
674 SkGPipeWriter writer;
675 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
676 SkGPipeWriter::kCrossProcess_Flag);
677 invokeGM(gm, pipeCanvas);
678 writer.endRecording();
679 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
680 "-pipe", bitmap, NULL, &comparisonBitmap);
681}
682
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000683static void write_picture_serialization(GM* gm, const ConfigData& rec,
684 const char writePicturePath[]) {
685 // only do this once, so we pick raster
686 if (kRaster_Backend == rec.fBackend &&
687 SkBitmap::kARGB_8888_Config == rec.fConfig) {
688
689 SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
690
691 const char* pictureSuffix = "skp";
692 SkString path = make_filename(writePicturePath, "",
693 SkString(gm->shortName()), pictureSuffix);
694
695 SkFILEWStream stream(path.c_str());
696 pict->serialize(&stream);
697 }
698}
699
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000700static void usage(const char * argv0) {
junov@google.com77e498e2012-01-18 18:56:34 +0000701 SkDebugf(
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000702 "%s [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n"
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000703 " [--noreplay] [--pipe] [--serialize] [--forceBWtext] [--nopdf] \n"
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000704 " [--nodeferred] [--match substring] [--notexturecache]\n"
bsalomon@google.com7361f542012-04-19 19:15:35 +0000705 , argv0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000706 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000707 SkDebugf(
708" readPath: directory to read reference images from;\n"
709" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000710 SkDebugf(" diffPath: directory to write difference images in.\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000711 SkDebugf(" resourcePath: directory that stores image resources.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000712 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000713 SkDebugf(" --pipe: Exercise SkGPipe replay.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000714 SkDebugf(
715" --serialize: exercise SkPicture serialization & deserialization.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000716 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
717 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
718 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000719 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
twiz@google.come24a0792012-01-31 18:35:30 +0000720 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000721}
722
bsalomon@google.com7361f542012-04-19 19:15:35 +0000723static const GrContextFactory::GLContextType kDontCare_GLContextType =
724 GrContextFactory::kNative_GLContextType;
725
726// If the platform does not support writing PNGs of PDFs then there will be no
727// comparison images to read. However, we can always write the .pdf files
728static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
729 kWrite_ConfigFlag;
730
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000731static const ConfigData gRec[] = {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000732 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888" },
733 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444" },
734 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000735#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.com7361f542012-04-19 19:15:35 +0000736 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu" },
737 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16" },
738 /* The debug context does not generate images */
739 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "debug" },
740 #ifdef SK_ANGLE
741 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle" },
742 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16" },
743 #endif
744 #ifdef SK_MESA
745 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa" },
746 #endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000747#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000748#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000749 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
750 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps" },
751#endif
752#ifdef SK_SUPPORT_PDF
753 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf" },
bungeman@google.comb29c8832011-10-10 13:19:10 +0000754#endif
reed@android.com00dae862009-06-10 15:38:48 +0000755};
756
reed@google.comb2a51622011-10-31 16:30:04 +0000757static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
758 if (0 == array.count()) {
759 // no names, so don't skip anything
760 return false;
761 }
762 for (int i = 0; i < array.count(); ++i) {
763 if (strstr(name, array[i])) {
764 // found the name, so don't skip
765 return false;
766 }
767 }
768 return true;
769}
770
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000771namespace skiagm {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000772SkAutoTUnref<GrContext> gGrContext;
773/**
774 * Sets the global GrContext, accessible by indivual GMs
775 */
776void SetGr(GrContext* grContext) {
777 SkSafeRef(grContext);
778 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000779}
bsalomon@google.com7361f542012-04-19 19:15:35 +0000780
781/**
782 * Gets the global GrContext, can be called by GM tests.
783 */
784GrContext* GetGr() {
785 return gGrContext.get();
786}
787
788/**
789 * Sets the global GrContext and then resets it to its previous value at
790 * destruction.
791 */
792class AutoResetGr : SkNoncopyable {
793public:
794 AutoResetGr() : fOld(NULL) {}
795 void set(GrContext* context) {
796 SkASSERT(NULL == fOld);
797 fOld = GetGr();
798 SkSafeRef(fOld);
799 SetGr(context);
800 }
801 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
802private:
803 GrContext* fOld;
804};
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000805}
806
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000807int main(int argc, char * const argv[]) {
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000808 SkGraphics::Init();
reed@google.com8923c6c2011-11-08 14:59:38 +0000809 // we don't need to see this during a run
810 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000811
epoger@google.com7bc13a62012-02-14 14:53:59 +0000812 setSystemPreferences();
813
reed@android.com8015dd82009-06-21 00:49:18 +0000814 const char* writePath = NULL; // if non-null, where we write the originals
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000815 const char* writePicturePath = NULL; // if non-null, where we write serialized pictures
reed@android.com8015dd82009-06-21 00:49:18 +0000816 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000817 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000818 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000819
reed@google.comb2a51622011-10-31 16:30:04 +0000820 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000821
reed@google.comab973972011-09-19 19:01:38 +0000822 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000823 bool doReplay = true;
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000824 bool doPipe = false;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000825 bool doSerialize = false;
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000826 bool useDebugGL = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000827 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000828 bool disableTextureCache = false;
829
reed@google.come8fcb502012-05-17 15:28:20 +0000830 gNotifyMissingReadReference = true;
831
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000832 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000833 char* const* stop = argv + argc;
834 for (++argv; argv < stop; ++argv) {
835 if (strcmp(*argv, "-w") == 0) {
836 argv++;
837 if (argv < stop && **argv) {
838 writePath = *argv;
839 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000840 } else if (strcmp(*argv, "-wp") == 0) {
841 argv++;
842 if (argv < stop && **argv) {
843 writePicturePath = *argv;
844 }
reed@android.com8015dd82009-06-21 00:49:18 +0000845 } else if (strcmp(*argv, "-r") == 0) {
846 argv++;
847 if (argv < stop && **argv) {
848 readPath = *argv;
849 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000850 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000851 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000852 if (argv < stop && **argv) {
853 diffPath = *argv;
854 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000855 } else if (strcmp(*argv, "-i") == 0) {
856 argv++;
857 if (argv < stop && **argv) {
858 resourcePath = *argv;
859 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000860 } else if (strcmp(*argv, "--forceBWtext") == 0) {
861 gForceBWtext = true;
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000862 } else if (strcmp(*argv, "--pipe") == 0) {
863 doPipe = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000864 } else if (strcmp(*argv, "--noreplay") == 0) {
865 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000866 } else if (strcmp(*argv, "--nopdf") == 0) {
867 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000868 } else if (strcmp(*argv, "--nodeferred") == 0) {
869 doDeferred = false;
reed@google.come8fcb502012-05-17 15:28:20 +0000870 } else if (strcmp(*argv, "--disable-missing-warning") == 0) {
871 gNotifyMissingReadReference = false;
872 } else if (strcmp(*argv, "--enable-missing-warning") == 0) {
873 gNotifyMissingReadReference = true;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000874 } else if (strcmp(*argv, "--serialize") == 0) {
875 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000876 } else if (strcmp(*argv, "--match") == 0) {
877 ++argv;
878 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000879 // just record the ptr, no need for a deep copy
880 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000881 }
twiz@google.come24a0792012-01-31 18:35:30 +0000882 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000883 disableTextureCache = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000884 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000885 usage(commandName);
886 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000887 }
888 }
889 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000890 usage(commandName);
891 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000892 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000893
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000894 GM::SetResourcePath(resourcePath);
895
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000896 GrContextFactory* grFactory = new GrContextFactory;
reed@google.com873cb1e2010-12-23 15:00:45 +0000897
reed@android.com00f883e2010-12-14 17:46:14 +0000898 if (readPath) {
899 fprintf(stderr, "reading from %s\n", readPath);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000900 }
901 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +0000902 fprintf(stderr, "writing to %s\n", writePath);
903 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000904 if (writePicturePath) {
905 fprintf(stderr, "writing pictures to %s\n", writePicturePath);
906 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000907 if (resourcePath) {
908 fprintf(stderr, "reading resources from %s\n", resourcePath);
909 }
910
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000911 // Accumulate success of all tests.
912 int testsRun = 0;
913 int testsPassed = 0;
914 int testsFailed = 0;
915 int testsMissingReferenceImages = 0;
916
twiz@google.come24a0792012-01-31 18:35:30 +0000917 if (disableTextureCache) {
918 skiagm::GetGr()->setTextureCacheLimits(0, 0);
919 }
920
bsalomon@google.com7361f542012-04-19 19:15:35 +0000921 Iter iter;
922 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +0000923 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000924 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000925 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000926 SkDELETE(gm);
927 continue;
928 }
929
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000930 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000931 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000932 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000933 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000934
bsalomon@google.com29d35012011-11-30 16:57:21 +0000935 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000936 SkAutoTUnref<GrRenderTarget> rt;
937 AutoResetGr autogr;
938 if (kGPU_Backend == gRec[i].fBackend) {
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000939 GrContext* gr = grFactory->get(gRec[i].fGLContextType);
bsalomon@google.com7361f542012-04-19 19:15:35 +0000940 if (!gr) {
941 continue;
942 }
943
944 // create a render target to back the device
945 GrTextureDesc desc;
946 desc.fConfig = kSkia8888_PM_GrPixelConfig;
947 desc.fFlags = kRenderTarget_GrTextureFlagBit;
948 desc.fWidth = gm->getISize().width();
949 desc.fHeight = gm->getISize().height();
950 desc.fSampleCnt = gRec[i].fSampleCnt;
951 GrTexture* tex = gr->createUncachedTexture(desc, NULL, 0);
952 if (!tex) {
bsalomon@google.comdaf12bb2012-04-20 19:08:44 +0000953 continue;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000954 }
955 rt.reset(tex->asRenderTarget());
956 rt.get()->ref();
957 tex->unref();
958
959 autogr.set(gr);
960 }
961
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000962 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000963 uint32_t gmFlags = gm->getFlags();
twiz@google.come24a0792012-01-31 18:35:30 +0000964 if ((kPDF_Backend == gRec[i].fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +0000965 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
966 {
reed@google.comab973972011-09-19 19:01:38 +0000967 continue;
968 }
969
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000970 // Now we know that we want to run this test and record its
971 // success or failure.
972 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000973
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000974 if ((ERROR_NONE == testErrors) &&
975 (kGPU_Backend == gRec[i].fBackend) &&
976 (NULL == rt.get())) {
977 fprintf(stderr, "Could not create render target for gpu.\n");
978 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000979 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000980
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000981 if (ERROR_NONE == testErrors) {
982 testErrors |= test_drawing(gm, gRec[i],
983 writePath, readPath, diffPath,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000984 GetGr(),
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000985 rt.get(), &forwardRenderedBitmap);
986 }
987
junov@google.com4370aed2012-01-18 16:21:08 +0000988 if (doDeferred && !testErrors &&
twiz@google.come24a0792012-01-31 18:35:30 +0000989 (kGPU_Backend == gRec[i].fBackend ||
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000990 kRaster_Backend == gRec[i].fBackend)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000991 testErrors |= test_deferred_drawing(gm, gRec[i],
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000992 forwardRenderedBitmap,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000993 diffPath, GetGr(), rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +0000994 }
995
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000996 if ((ERROR_NONE == testErrors) && doReplay &&
997 !(gmFlags & GM::kSkipPicture_Flag)) {
998 testErrors |= test_picture_playback(gm, gRec[i],
999 forwardRenderedBitmap,
1000 readPath, diffPath);
1001 }
1002
scroggo@google.com5af9b202012-06-04 17:17:36 +00001003 if ((ERROR_NONE == testErrors) && doPipe &&
1004 !(gmFlags & GM::kSkipPipe_Flag)) {
1005 testErrors |= test_pipe_playback(gm, gRec[i],
1006 forwardRenderedBitmap,
1007 readPath, diffPath);
1008 }
1009
djsollen@google.coma2ca41e2012-03-23 19:00:34 +00001010 if ((ERROR_NONE == testErrors) && doSerialize &&
1011 !(gmFlags & GM::kSkipPicture_Flag)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001012 testErrors |= test_picture_serialization(gm, gRec[i],
1013 forwardRenderedBitmap,
1014 readPath, diffPath);
1015 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001016
1017 if (!(gmFlags & GM::kSkipPicture_Flag) && writePicturePath) {
1018 write_picture_serialization(gm, gRec[i], writePicturePath);
1019 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001020
1021 // Update overall results.
1022 // We only tabulate the particular error types that we currently
1023 // care about (e.g., missing reference images). Later on, if we
1024 // want to also tabulate pixel mismatches vs dimension mistmatches
1025 // (or whatever else), we can do so.
1026 testsRun++;
1027 if (ERROR_NONE == testErrors) {
1028 testsPassed++;
1029 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
1030 testsMissingReferenceImages++;
1031 } else {
1032 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001033 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001034 }
reed@android.com00dae862009-06-10 15:38:48 +00001035 SkDELETE(gm);
1036 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001037 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
1038 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
robertphillips@google.comf6f123d2012-03-21 17:57:55 +00001039
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001040 delete grFactory;
1041 SkGraphics::Term();
1042
1043 PRINT_INST_COUNT(SkRefCnt);
1044 PRINT_INST_COUNT(GrResource);
1045
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001046 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001047}