blob: e79831a0ffe4a981ee9a478184672a0dbd1e20d1 [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"
robertphillips@google.com977b9c82012-06-05 19:35:09 +000023#include "SkRefCnt.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000024#include "SkStream.h"
25#include "SamplePipeControllers.h"
reed@google.com07700442010-12-20 19:46:07 +000026
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000027static bool gForceBWtext;
28
reed@google.com8923c6c2011-11-08 14:59:38 +000029extern bool gSkSuppressFontCachePurgeSpew;
30
reed@google.com07700442010-12-20 19:46:07 +000031#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000032 #include "SkPDFDevice.h"
33 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000034#endif
reed@android.com00dae862009-06-10 15:38:48 +000035
epoger@google.come3cc2eb2012-01-18 20:11:13 +000036// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
37// stop writing out XPS-format image baselines in gm.
38#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000039#ifdef SK_SUPPORT_XPS
40 #include "SkXPSDevice.h"
41#endif
42
reed@google.com46cce912011-06-29 12:54:46 +000043#ifdef SK_BUILD_FOR_MAC
44 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000045 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000046#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000047 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000048#endif
49
epoger@google.comc7cf2b32011-12-28 19:31:01 +000050typedef int ErrorBitfield;
51const static ErrorBitfield ERROR_NONE = 0x00;
52const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
53const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
54const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
55const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
56const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
57
reed@google.come8fcb502012-05-17 15:28:20 +000058// If true, emit a messange when we can't find a reference image to compare
59static bool gNotifyMissingReadReference;
60
reed@android.com00dae862009-06-10 15:38:48 +000061using namespace skiagm;
62
reed@android.com00dae862009-06-10 15:38:48 +000063class Iter {
64public:
65 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000066 this->reset();
67 }
68
69 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000070 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000071 }
reed@google.comd4dfd102011-01-18 21:05:42 +000072
reed@android.comdd0ac282009-06-20 02:38:16 +000073 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000074 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000075 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000076 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000077 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000078 }
79 return NULL;
80 }
reed@google.comd4dfd102011-01-18 21:05:42 +000081
reed@android.com00dae862009-06-10 15:38:48 +000082 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000083 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000084 int count = 0;
85 while (reg) {
86 count += 1;
87 reg = reg->next();
88 }
89 return count;
90 }
reed@google.comd4dfd102011-01-18 21:05:42 +000091
reed@android.com00dae862009-06-10 15:38:48 +000092private:
93 const GMRegistry* fReg;
94};
95
reed@android.com8015dd82009-06-21 00:49:18 +000096static SkString make_name(const char shortName[], const char configName[]) {
97 SkString name(shortName);
98 name.appendf("_%s", configName);
99 return name;
100}
101
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000102static SkString make_filename(const char path[],
103 const char pathSuffix[],
104 const SkString& name,
105 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000106 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000107 if (filename.endsWith("/")) {
108 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000109 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000110 filename.append(pathSuffix);
111 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000112 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000113 return filename;
114}
115
reed@android.comb9b9a182009-07-08 02:54:47 +0000116/* since PNG insists on unpremultiplying our alpha, we take no precision chances
117 and force all pixels to be 100% opaque, otherwise on compare we may not get
118 a perfect match.
119 */
120static void force_all_opaque(const SkBitmap& bitmap) {
121 SkAutoLockPixels lock(bitmap);
122 for (int y = 0; y < bitmap.height(); y++) {
123 for (int x = 0; x < bitmap.width(); x++) {
124 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
125 }
126 }
127}
128
129static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
130 SkBitmap copy;
131 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
132 force_all_opaque(copy);
133 return SkImageEncoder::EncodeFile(path.c_str(), copy,
134 SkImageEncoder::kPNG_Type, 100);
135}
136
reed@google.com3d3f0922010-12-20 21:10:29 +0000137static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000138 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
139 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
140 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
141 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000142}
143
144static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000145 SkBitmap* diff) {
146 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000147
148 const int w = target.width();
149 const int h = target.height();
150 for (int y = 0; y < h; y++) {
151 for (int x = 0; x < w; x++) {
152 SkPMColor c0 = *base.getAddr32(x, y);
153 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000154 SkPMColor d = 0;
155 if (c0 != c1) {
156 d = compute_diff_pmcolor(c0, c1);
157 }
158 *diff->getAddr32(x, y) = d;
159 }
160 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000161}
162
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000163static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
164 const SkString& name,
165 const char* renderModeDescriptor,
166 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000167 SkBitmap copy;
168 const SkBitmap* bm = &target;
169 if (target.config() != SkBitmap::kARGB_8888_Config) {
170 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
171 bm = &copy;
172 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000173 SkBitmap baseCopy;
174 const SkBitmap* bp = &base;
175 if (base.config() != SkBitmap::kARGB_8888_Config) {
176 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
177 bp = &baseCopy;
178 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000179
180 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000181 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000182
183 const int w = bm->width();
184 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000185 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000186 SkDebugf(
187"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
188 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000189 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000190 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000191 }
192
193 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000194 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000195
196 for (int y = 0; y < h; y++) {
197 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000198 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000199 SkPMColor c1 = *bm->getAddr32(x, y);
200 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000201 SkDebugf(
202"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
203 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000204
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000205 if (diff) {
206 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
207 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000208 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000209 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000210 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000211 }
212 }
213 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000214
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000215 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000216 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000217}
reed@android.com00dae862009-06-10 15:38:48 +0000218
bungeman@google.comb29c8832011-10-10 13:19:10 +0000219static bool write_document(const SkString& path,
220 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000221 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000222 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000223 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000224}
225
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000226enum Backend {
227 kRaster_Backend,
228 kGPU_Backend,
229 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000230 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000231};
232
bsalomon@google.com7361f542012-04-19 19:15:35 +0000233enum ConfigFlags {
234 kNone_ConfigFlag = 0x0,
235 /* Write GM images if a write path is provided. */
236 kWrite_ConfigFlag = 0x1,
237 /* Read comparison GM images if a read path is provided. */
238 kRead_ConfigFlag = 0x2,
239 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
240};
241
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000242struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000243 SkBitmap::Config fConfig;
244 Backend fBackend;
245 GrContextFactory::GLContextType fGLContextType; // GPU backend only
246 int fSampleCnt; // GPU backend only
247 ConfigFlags fFlags;
248 const char* fName;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000249};
250
251/// Returns true if processing should continue, false to skip the
252/// remainder of this config for this GM.
253//@todo thudson 22 April 2011 - could refactor this to take in
254// a factory to generate the context, always call readPixels()
255// (logically a noop for rasters, if wasted time), and thus collapse the
256// GPU special case and also let this be used for SkPicture testing.
257static void setup_bitmap(const ConfigData& gRec, SkISize& size,
258 SkBitmap* bitmap) {
259 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
260 bitmap->allocPixels();
261 bitmap->eraseColor(0);
262}
263
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000264#include "SkDrawFilter.h"
265class BWTextDrawFilter : public SkDrawFilter {
266public:
267 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
268};
269void BWTextDrawFilter::filter(SkPaint* p, Type t) {
270 if (kText_Type == t) {
271 p->setAntiAlias(false);
272 }
273}
274
275static void installFilter(SkCanvas* canvas) {
276 if (gForceBWtext) {
277 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
278 }
279}
280
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000281static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
reed@google.com778e1632012-06-04 20:00:01 +0000282 SkAutoCanvasRestore acr(canvas, true);
283
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000284 if (!isPDF) {
reed@google.com778e1632012-06-04 20:00:01 +0000285 canvas->concat(gm->getInitialTransform());
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000286 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000287 installFilter(canvas);
288 gm->draw(canvas);
289 canvas->setDrawFilter(NULL);
290}
291
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000292static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
293 GrContext* context,
294 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000295 SkBitmap* bitmap,
296 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000297 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000298 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000299
300 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000301 SkCanvas* canvas;
302 if (deferred) {
303 canvas = new SkDeferredCanvas;
304 canvas->setDevice(new SkDevice(*bitmap))->unref();
305 } else {
306 canvas = new SkCanvas(*bitmap);
307 }
308 SkAutoUnref canvasUnref(canvas);
309 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000310 canvas->flush();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000311 } else { // GPU
312 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000313 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000314 }
junov@google.com4370aed2012-01-18 16:21:08 +0000315 SkCanvas* gc;
316 if (deferred) {
317 gc = new SkDeferredCanvas;
318 } else {
319 gc = new SkGpuCanvas(context, rt);
320 }
321 SkAutoUnref gcUnref(gc);
322 gc->setDevice(new SkGpuDevice(context, rt))->unref();
323 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000324 // the device is as large as the current rendertarget, so we explicitly
325 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000326 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000327 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
328 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000329 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000330 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000331 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000332}
333
334static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
335 SkPicture* pict, SkBitmap* bitmap) {
336 SkISize size = gm->getISize();
337 setup_bitmap(gRec, size, bitmap);
338 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000339 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000340 canvas.drawPicture(*pict);
341}
342
343static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
344#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000345 SkMatrix initialTransform = gm->getInitialTransform();
346 SkISize pageSize = gm->getISize();
347 SkPDFDevice* dev = NULL;
348 if (initialTransform.isIdentity()) {
349 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
350 } else {
351 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
352 SkIntToScalar(pageSize.height()));
353 initialTransform.mapRect(&content);
354 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
355 SkIntToScalar(pageSize.height()));
356 SkISize contentSize =
357 SkISize::Make(SkScalarRoundToInt(content.width()),
358 SkScalarRoundToInt(content.height()));
359 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
360 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000361 SkAutoUnref aur(dev);
362
363 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000364 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000365
366 SkPDFDocument doc;
367 doc.appendPage(dev);
368 doc.emitPDF(&pdf);
369#endif
370}
371
bungeman@google.comb29c8832011-10-10 13:19:10 +0000372static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
373#ifdef SK_SUPPORT_XPS
374 SkISize size = gm->getISize();
375
376 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
377 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000378 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
379 static const SkScalar upm = 72 * inchesPerMeter;
380 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
381 static const SkScalar ppm = 200 * inchesPerMeter;
382 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000383
384 SkXPSDevice* dev = new SkXPSDevice();
385 SkAutoUnref aur(dev);
386
387 SkCanvas c(dev);
388 dev->beginPortfolio(&xps);
389 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000390 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000391 dev->endSheet();
392 dev->endPortfolio();
393
394#endif
395}
396
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000397static ErrorBitfield write_reference_image(const ConfigData& gRec,
398 const char writePath [],
399 const char renderModeDescriptor [],
400 const SkString& name,
401 SkBitmap& bitmap,
402 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000403 SkString path;
404 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000405 if (gRec.fBackend == kRaster_Backend ||
406 gRec.fBackend == kGPU_Backend ||
407 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
408
tomhudson@google.comea325432011-06-09 20:30:03 +0000409 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000410 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000411 }
412 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000413 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000414 success = write_document(path, *document);
415 }
416 if (kXPS_Backend == gRec.fBackend) {
417 path = make_filename(writePath, renderModeDescriptor, name, "xps");
418 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000419 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000420 if (success) {
421 return ERROR_NONE;
422 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000423 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000424 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000425 }
426}
427
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000428static ErrorBitfield compare_to_reference_image(const SkString& name,
429 SkBitmap &bitmap,
430 const SkBitmap& comparisonBitmap,
431 const char diffPath [],
432 const char renderModeDescriptor []) {
433 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000434 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000435 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
436 diffPath ? &diffBitmap : NULL);
reed@google.com8e529b72012-04-09 20:20:10 +0000437 if ((ERROR_NONE != errors) && diffPath) {
438 // write out the generated image
439 SkString genName = make_filename(diffPath, "", name, "png");
440 if (!write_bitmap(genName, bitmap)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000441 errors |= ERROR_WRITING_REFERENCE_IMAGE;
442 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000443 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000444 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000445}
446
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000447static ErrorBitfield compare_to_reference_image(const char readPath [],
448 const SkString& name,
449 SkBitmap &bitmap,
450 const char diffPath [],
451 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000452 SkString path = make_filename(readPath, "", name, "png");
453 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000454 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
455 SkBitmap::kARGB_8888_Config,
456 SkImageDecoder::kDecodePixels_Mode, NULL)) {
457 return compare_to_reference_image(name, bitmap,
458 orig, diffPath,
459 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000460 } else {
reed@google.come8fcb502012-05-17 15:28:20 +0000461 if (gNotifyMissingReadReference) {
462 fprintf(stderr, "FAILED to read %s\n", path.c_str());
463 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000464 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000465 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000466}
467
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000468static ErrorBitfield handle_test_results(GM* gm,
469 const ConfigData& gRec,
470 const char writePath [],
471 const char readPath [],
472 const char diffPath [],
473 const char renderModeDescriptor [],
474 SkBitmap& bitmap,
475 SkDynamicMemoryWStream* pdf,
476 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000477 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000478 ErrorBitfield retval = ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000479
bsalomon@google.com7361f542012-04-19 19:15:35 +0000480 if (readPath && (gRec.fFlags & kRead_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000481 retval |= compare_to_reference_image(readPath, name, bitmap,
482 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000483 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000484 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000485 retval |= write_reference_image(gRec, writePath, renderModeDescriptor,
486 name, bitmap, pdf);
487 }
488 if (comparisonBitmap) {
489 retval |= compare_to_reference_image(name, bitmap,
490 *comparisonBitmap, diffPath,
491 renderModeDescriptor);
492 }
493 return retval;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000494}
495
496static SkPicture* generate_new_picture(GM* gm) {
497 // Pictures are refcounted so must be on heap
498 SkPicture* pict = new SkPicture;
499 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000500 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000501 pict->endRecording();
502
503 return pict;
504}
505
506static SkPicture* stream_to_new_picture(const SkPicture& src) {
507
508 // To do in-memory commiunications with a stream, we need to:
509 // * create a dynamic memory stream
510 // * copy it into a buffer
511 // * create a read stream from it
512 // ?!?!
513
514 SkDynamicMemoryWStream storage;
515 src.serialize(&storage);
516
517 int streamSize = storage.getOffset();
518 SkAutoMalloc dstStorage(streamSize);
519 void* dst = dstStorage.get();
520 //char* dst = new char [streamSize];
521 //@todo thudson 22 April 2011 when can we safely delete [] dst?
522 storage.copyTo(dst);
523 SkMemoryStream pictReadback(dst, streamSize);
524 SkPicture* retval = new SkPicture (&pictReadback);
525 return retval;
526}
527
528// Test: draw into a bitmap or pdf.
529// Depending on flags, possibly compare to an expected image
530// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000531static ErrorBitfield test_drawing(GM* gm,
532 const ConfigData& gRec,
533 const char writePath [],
534 const char readPath [],
535 const char diffPath [],
536 GrContext* context,
537 GrRenderTarget* rt,
538 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000539 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000540
541 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000542 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000543 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000544 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
545 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000546 if (ERROR_NONE != errors) {
547 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000548 }
reed@google.com46cce912011-06-29 12:54:46 +0000549 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000550 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000551#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000552 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000553 SkMemoryStream stream(data.data(), data.size());
554 SkPDFDocumentToBitmap(&stream, bitmap);
555#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000556 } else if (gRec.fBackend == kXPS_Backend) {
557 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000558 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000559 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000560 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000561}
562
junov@google.com4370aed2012-01-18 16:21:08 +0000563static ErrorBitfield test_deferred_drawing(GM* gm,
564 const ConfigData& gRec,
565 const SkBitmap& comparisonBitmap,
566 const char diffPath [],
567 GrContext* context,
568 GrRenderTarget* rt) {
569 SkDynamicMemoryWStream document;
570
571 if (gRec.fBackend == kRaster_Backend ||
572 gRec.fBackend == kGPU_Backend) {
573 SkBitmap bitmap;
574 // Early exit if we can't generate the image, but this is
575 // expected in some cases, so don't report a test failure.
576 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
577 return ERROR_NONE;
578 }
579 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
580 "-deferred", bitmap, NULL, &comparisonBitmap);
581 }
582 return ERROR_NONE;
583}
584
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000585static ErrorBitfield test_picture_playback(GM* gm,
586 const ConfigData& gRec,
587 const SkBitmap& comparisonBitmap,
588 const char readPath [],
589 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000590 SkPicture* pict = generate_new_picture(gm);
591 SkAutoUnref aur(pict);
592
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000593 if (kRaster_Backend == gRec.fBackend) {
594 SkBitmap bitmap;
595 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000596 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
597 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000598 } else {
599 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000600 }
601}
602
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000603static ErrorBitfield test_picture_serialization(GM* gm,
604 const ConfigData& gRec,
605 const SkBitmap& comparisonBitmap,
606 const char readPath [],
607 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000608 SkPicture* pict = generate_new_picture(gm);
609 SkAutoUnref aurp(pict);
610 SkPicture* repict = stream_to_new_picture(*pict);
611 SkAutoUnref aurr(repict);
612
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000613 if (kRaster_Backend == gRec.fBackend) {
614 SkBitmap bitmap;
615 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000616 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
617 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000618 } else {
619 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000620 }
621}
622
scroggo@google.com565254b2012-06-28 15:41:32 +0000623struct PipeFlagComboData {
624 const char* name;
625 uint32_t flags;
626};
627
628static PipeFlagComboData gPipeWritingFlagCombos[] = {
629 { "", 0 },
630 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
631 { " cross-process, shared adress", SkGPipeWriter::kCrossProcess_Flag
632 | SkGPipeWriter::kSharedAddressSpace_SkGPipeFlag }
633};
634
scroggo@google.com5af9b202012-06-04 17:17:36 +0000635static ErrorBitfield test_pipe_playback(GM* gm,
636 const ConfigData& gRec,
637 const SkBitmap& comparisonBitmap,
638 const char readPath [],
639 const char diffPath []) {
640 if (kRaster_Backend != gRec.fBackend) {
641 return ERROR_NONE;
642 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000643 ErrorBitfield errors = ERROR_NONE;
644 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
645 SkBitmap bitmap;
646 SkISize size = gm->getISize();
647 setup_bitmap(gRec, size, &bitmap);
648 SkCanvas canvas(bitmap);
649 PipeController pipeController(&canvas);
650 SkGPipeWriter writer;
651 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
652 gPipeWritingFlagCombos[i].flags);
653 invokeGM(gm, pipeCanvas);
654 writer.endRecording();
655 SkString string("-pipe");
656 string.append(gPipeWritingFlagCombos[i].name);
657 errors |= handle_test_results(gm, gRec, NULL, NULL, diffPath,
658 string.c_str(), bitmap, NULL, &comparisonBitmap);
659 if (errors != ERROR_NONE) {
660 break;
661 }
662 }
663 return errors;
scroggo@google.com5af9b202012-06-04 17:17:36 +0000664}
665
scroggo@google.com72c96722012-06-06 21:07:10 +0000666static ErrorBitfield test_tiled_pipe_playback(GM* gm,
667 const ConfigData& gRec,
668 const SkBitmap& comparisonBitmap,
669 const char readPath [],
670 const char diffPath []) {
671 if (kRaster_Backend != gRec.fBackend) {
672 return ERROR_NONE;
673 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000674 ErrorBitfield errors = ERROR_NONE;
675 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
676 SkBitmap bitmap;
677 SkISize size = gm->getISize();
678 setup_bitmap(gRec, size, &bitmap);
679 SkCanvas canvas(bitmap);
680 TiledPipeController pipeController(bitmap);
681 SkGPipeWriter writer;
682 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
683 gPipeWritingFlagCombos[i].flags);
684 invokeGM(gm, pipeCanvas);
685 writer.endRecording();
686 SkString string("-pipe");
687 string.append(gPipeWritingFlagCombos[i].name);
688 errors |= handle_test_results(gm, gRec, NULL, NULL, diffPath,
689 string.c_str(), bitmap, NULL, &comparisonBitmap);
690 if (errors != ERROR_NONE) {
691 break;
692 }
693 }
694 return errors;
scroggo@google.com72c96722012-06-06 21:07:10 +0000695}
696
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000697static void write_picture_serialization(GM* gm, const ConfigData& rec,
698 const char writePicturePath[]) {
699 // only do this once, so we pick raster
700 if (kRaster_Backend == rec.fBackend &&
701 SkBitmap::kARGB_8888_Config == rec.fConfig) {
702
703 SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
704
705 const char* pictureSuffix = "skp";
706 SkString path = make_filename(writePicturePath, "",
707 SkString(gm->shortName()), pictureSuffix);
708
709 SkFILEWStream stream(path.c_str());
710 pict->serialize(&stream);
711 }
712}
713
bsalomon@google.com7361f542012-04-19 19:15:35 +0000714static const GrContextFactory::GLContextType kDontCare_GLContextType =
715 GrContextFactory::kNative_GLContextType;
716
717// If the platform does not support writing PNGs of PDFs then there will be no
718// comparison images to read. However, we can always write the .pdf files
719static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
720 kWrite_ConfigFlag;
721
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000722static const ConfigData gRec[] = {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000723 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888" },
724 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444" },
725 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000726#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.com7361f542012-04-19 19:15:35 +0000727 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu" },
728 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16" },
729 /* The debug context does not generate images */
730 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "debug" },
731 #ifdef SK_ANGLE
732 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle" },
733 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16" },
734 #endif
735 #ifdef SK_MESA
736 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa" },
737 #endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000738#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000739#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000740 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
741 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps" },
742#endif
743#ifdef SK_SUPPORT_PDF
744 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf" },
bungeman@google.comb29c8832011-10-10 13:19:10 +0000745#endif
reed@android.com00dae862009-06-10 15:38:48 +0000746};
747
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000748static void usage(const char * argv0) {
749 SkDebugf("%s\n", argv0);
750 SkDebugf(" [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n");
reed@google.come5f48b92012-06-22 15:27:39 +0000751 SkDebugf(" [--config ");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000752 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
753 if (i > 0) {
754 SkDebugf("|");
755 }
756 SkDebugf(gRec[i].fName);
757 }
758 SkDebugf(" ]\n");
scroggo@google.com565254b2012-06-28 15:41:32 +0000759 SkDebugf(" [--noreplay] [--nopipe] [--serialize] [--forceBWtext] [--nopdf] \n"
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000760 " [--tiledPipe] \n"
761 " [--nodeferred] [--match substring] [--notexturecache]\n"
762 " [-h|--help]\n"
763 );
764 SkDebugf(" writePath: directory to write rendered images in.\n");
765 SkDebugf(
766 " readPath: directory to read reference images from;\n"
767 " reports if any pixels mismatch between reference and new images\n");
768 SkDebugf(" diffPath: directory to write difference images in.\n");
769 SkDebugf(" resourcePath: directory that stores image resources.\n");
770 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
scroggo@google.com565254b2012-06-28 15:41:32 +0000771 SkDebugf(" --nopipe: Skip SkGPipe replay.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000772 SkDebugf(" --tiledPipe: Exercise tiled SkGPipe replay.\n");
773 SkDebugf(
borenet@google.com14ca1d32012-06-15 13:46:44 +0000774 " --serialize: exercise SkPicture serialization & deserialization.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000775 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
776 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
777 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
778 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
779 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
780 SkDebugf(" -h|--help : Show this help message. \n");
781}
782
783static int findConfig(const char config[]) {
784 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
785 if (!strcmp(config, gRec[i].fName)) {
786 return i;
787 }
788 }
789 return -1;
790}
791
reed@google.comb2a51622011-10-31 16:30:04 +0000792static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
793 if (0 == array.count()) {
794 // no names, so don't skip anything
795 return false;
796 }
797 for (int i = 0; i < array.count(); ++i) {
798 if (strstr(name, array[i])) {
799 // found the name, so don't skip
800 return false;
801 }
802 }
803 return true;
804}
805
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000806namespace skiagm {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000807SkAutoTUnref<GrContext> gGrContext;
808/**
809 * Sets the global GrContext, accessible by indivual GMs
810 */
caryclark@google.com13130862012-06-06 12:10:45 +0000811static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000812 SkSafeRef(grContext);
813 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000814}
bsalomon@google.com7361f542012-04-19 19:15:35 +0000815
816/**
817 * Gets the global GrContext, can be called by GM tests.
818 */
caryclark@google.com13130862012-06-06 12:10:45 +0000819GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000820GrContext* GetGr() {
821 return gGrContext.get();
822}
823
824/**
825 * Sets the global GrContext and then resets it to its previous value at
826 * destruction.
827 */
828class AutoResetGr : SkNoncopyable {
829public:
830 AutoResetGr() : fOld(NULL) {}
831 void set(GrContext* context) {
832 SkASSERT(NULL == fOld);
833 fOld = GetGr();
834 SkSafeRef(fOld);
835 SetGr(context);
836 }
837 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
838private:
839 GrContext* fOld;
840};
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000841}
842
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000843int main(int argc, char * const argv[]) {
robertphillips@google.comb74af872012-06-27 19:41:42 +0000844
845#ifdef SK_ENABLE_INST_COUNT
846 gPrintInstCount = true;
847#endif
848
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000849 SkGraphics::Init();
reed@google.com8923c6c2011-11-08 14:59:38 +0000850 // we don't need to see this during a run
851 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000852
epoger@google.com7bc13a62012-02-14 14:53:59 +0000853 setSystemPreferences();
854
reed@android.com8015dd82009-06-21 00:49:18 +0000855 const char* writePath = NULL; // if non-null, where we write the originals
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000856 const char* writePicturePath = NULL; // if non-null, where we write serialized pictures
reed@android.com8015dd82009-06-21 00:49:18 +0000857 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000858 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000859 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000860
reed@google.comb2a51622011-10-31 16:30:04 +0000861 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000862
reed@google.comab973972011-09-19 19:01:38 +0000863 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000864 bool doReplay = true;
scroggo@google.com565254b2012-06-28 15:41:32 +0000865 bool doPipe = true;
scroggo@google.com72c96722012-06-06 21:07:10 +0000866 bool doTiledPipe = false;
borenet@google.com14ca1d32012-06-15 13:46:44 +0000867 bool doSerialize = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000868 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000869 bool disableTextureCache = false;
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000870 SkTDArray<size_t> configs;
871 bool userConfig = false;
twiz@google.come24a0792012-01-31 18:35:30 +0000872
reed@google.come8fcb502012-05-17 15:28:20 +0000873 gNotifyMissingReadReference = true;
874
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000875 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000876 char* const* stop = argv + argc;
877 for (++argv; argv < stop; ++argv) {
878 if (strcmp(*argv, "-w") == 0) {
879 argv++;
880 if (argv < stop && **argv) {
881 writePath = *argv;
882 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000883 } else if (strcmp(*argv, "-wp") == 0) {
884 argv++;
885 if (argv < stop && **argv) {
886 writePicturePath = *argv;
887 }
reed@android.com8015dd82009-06-21 00:49:18 +0000888 } else if (strcmp(*argv, "-r") == 0) {
889 argv++;
890 if (argv < stop && **argv) {
891 readPath = *argv;
892 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000893 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000894 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000895 if (argv < stop && **argv) {
896 diffPath = *argv;
897 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000898 } else if (strcmp(*argv, "-i") == 0) {
899 argv++;
900 if (argv < stop && **argv) {
901 resourcePath = *argv;
902 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000903 } else if (strcmp(*argv, "--forceBWtext") == 0) {
904 gForceBWtext = true;
scroggo@google.com565254b2012-06-28 15:41:32 +0000905 } else if (strcmp(*argv, "--nopipe") == 0) {
906 doPipe = false;
scroggo@google.com72c96722012-06-06 21:07:10 +0000907 } else if (strcmp(*argv, "--tiledPipe") == 0) {
908 doTiledPipe = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000909 } else if (strcmp(*argv, "--noreplay") == 0) {
910 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000911 } else if (strcmp(*argv, "--nopdf") == 0) {
912 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000913 } else if (strcmp(*argv, "--nodeferred") == 0) {
914 doDeferred = false;
reed@google.come8fcb502012-05-17 15:28:20 +0000915 } else if (strcmp(*argv, "--disable-missing-warning") == 0) {
916 gNotifyMissingReadReference = false;
917 } else if (strcmp(*argv, "--enable-missing-warning") == 0) {
918 gNotifyMissingReadReference = true;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000919 } else if (strcmp(*argv, "--serialize") == 0) {
borenet@google.com14ca1d32012-06-15 13:46:44 +0000920 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000921 } else if (strcmp(*argv, "--match") == 0) {
922 ++argv;
923 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000924 // just record the ptr, no need for a deep copy
925 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000926 }
twiz@google.come24a0792012-01-31 18:35:30 +0000927 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000928 disableTextureCache = true;
reed@google.come5f48b92012-06-22 15:27:39 +0000929 } else if (strcmp(*argv, "--config") == 0) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000930 argv++;
931 if (argv < stop) {
932 int index = findConfig(*argv);
933 if (index >= 0) {
934 *configs.append() = index;
935 userConfig = true;
936 } else {
937 SkString str;
938 str.printf("unrecognized config %s\n", *argv);
939 SkDebugf(str.c_str());
940 usage(commandName);
941 return -1;
942 }
943 } else {
reed@google.come5f48b92012-06-22 15:27:39 +0000944 SkDebugf("missing arg for --config\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000945 usage(commandName);
946 return -1;
947 }
948 } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
949 usage(commandName);
950 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000951 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000952 usage(commandName);
953 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000954 }
955 }
956 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000957 usage(commandName);
958 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000959 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000960
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000961 if (!userConfig) {
962 // if no config is specified by user, we add them all.
963 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
964 *configs.append() = i;
965 }
966 }
967
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000968 GM::SetResourcePath(resourcePath);
969
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000970 GrContextFactory* grFactory = new GrContextFactory;
reed@google.com873cb1e2010-12-23 15:00:45 +0000971
reed@android.com00f883e2010-12-14 17:46:14 +0000972 if (readPath) {
973 fprintf(stderr, "reading from %s\n", readPath);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000974 }
975 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +0000976 fprintf(stderr, "writing to %s\n", writePath);
977 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000978 if (writePicturePath) {
979 fprintf(stderr, "writing pictures to %s\n", writePicturePath);
980 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000981 if (resourcePath) {
982 fprintf(stderr, "reading resources from %s\n", resourcePath);
983 }
984
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000985 // Accumulate success of all tests.
986 int testsRun = 0;
987 int testsPassed = 0;
988 int testsFailed = 0;
989 int testsMissingReferenceImages = 0;
990
twiz@google.come24a0792012-01-31 18:35:30 +0000991 if (disableTextureCache) {
992 skiagm::GetGr()->setTextureCacheLimits(0, 0);
993 }
994
bsalomon@google.com7361f542012-04-19 19:15:35 +0000995 Iter iter;
996 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +0000997 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000998 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000999 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00001000 SkDELETE(gm);
1001 continue;
1002 }
1003
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001004 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +00001005 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +00001006 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +00001007 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +00001008
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001009 for (int i = 0; i < configs.count(); i++) {
1010 ConfigData config = gRec[configs[i]];
bsalomon@google.com7361f542012-04-19 19:15:35 +00001011 SkAutoTUnref<GrRenderTarget> rt;
1012 AutoResetGr autogr;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001013 if (kGPU_Backend == config.fBackend) {
1014 GrContext* gr = grFactory->get(config.fGLContextType);
bsalomon@google.com7361f542012-04-19 19:15:35 +00001015 if (!gr) {
1016 continue;
1017 }
1018
1019 // create a render target to back the device
1020 GrTextureDesc desc;
1021 desc.fConfig = kSkia8888_PM_GrPixelConfig;
1022 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1023 desc.fWidth = gm->getISize().width();
1024 desc.fHeight = gm->getISize().height();
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001025 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com7361f542012-04-19 19:15:35 +00001026 GrTexture* tex = gr->createUncachedTexture(desc, NULL, 0);
1027 if (!tex) {
bsalomon@google.comdaf12bb2012-04-20 19:08:44 +00001028 continue;
bsalomon@google.com7361f542012-04-19 19:15:35 +00001029 }
1030 rt.reset(tex->asRenderTarget());
1031 rt.get()->ref();
1032 tex->unref();
1033
1034 autogr.set(gr);
1035 }
1036
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001037 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +00001038 uint32_t gmFlags = gm->getFlags();
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001039 if ((kPDF_Backend == config.fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +00001040 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
1041 {
reed@google.comab973972011-09-19 19:01:38 +00001042 continue;
1043 }
1044
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001045 // Now we know that we want to run this test and record its
1046 // success or failure.
1047 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +00001048
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001049 if ((ERROR_NONE == testErrors) &&
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001050 (kGPU_Backend == config.fBackend) &&
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001051 (NULL == rt.get())) {
1052 fprintf(stderr, "Could not create render target for gpu.\n");
1053 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001054 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +00001055
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001056 if (ERROR_NONE == testErrors) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001057 testErrors |= test_drawing(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001058 writePath, readPath, diffPath,
bsalomon@google.com7361f542012-04-19 19:15:35 +00001059 GetGr(),
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001060 rt.get(), &forwardRenderedBitmap);
1061 }
1062
junov@google.com4370aed2012-01-18 16:21:08 +00001063 if (doDeferred && !testErrors &&
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001064 (kGPU_Backend == config.fBackend ||
1065 kRaster_Backend == config.fBackend)) {
1066 testErrors |= test_deferred_drawing(gm, config,
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001067 forwardRenderedBitmap,
bsalomon@google.com7361f542012-04-19 19:15:35 +00001068 diffPath, GetGr(), rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +00001069 }
1070
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001071 if ((ERROR_NONE == testErrors) && doReplay &&
1072 !(gmFlags & GM::kSkipPicture_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001073 testErrors |= test_picture_playback(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001074 forwardRenderedBitmap,
1075 readPath, diffPath);
1076 }
1077
scroggo@google.com5af9b202012-06-04 17:17:36 +00001078 if ((ERROR_NONE == testErrors) && doPipe &&
1079 !(gmFlags & GM::kSkipPipe_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001080 testErrors |= test_pipe_playback(gm, config,
scroggo@google.com5af9b202012-06-04 17:17:36 +00001081 forwardRenderedBitmap,
1082 readPath, diffPath);
1083 }
1084
scroggo@google.com72c96722012-06-06 21:07:10 +00001085 if ((ERROR_NONE == testErrors) && doTiledPipe &&
1086 !(gmFlags & GM::kSkipPipe_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001087 testErrors |= test_tiled_pipe_playback(gm, config,
scroggo@google.com72c96722012-06-06 21:07:10 +00001088 forwardRenderedBitmap,
1089 readPath, diffPath);
1090 }
1091
djsollen@google.coma2ca41e2012-03-23 19:00:34 +00001092 if ((ERROR_NONE == testErrors) && doSerialize &&
1093 !(gmFlags & GM::kSkipPicture_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001094 testErrors |= test_picture_serialization(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001095 forwardRenderedBitmap,
1096 readPath, diffPath);
1097 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001098
1099 if (!(gmFlags & GM::kSkipPicture_Flag) && writePicturePath) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001100 write_picture_serialization(gm, config, writePicturePath);
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001101 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001102
1103 // Update overall results.
1104 // We only tabulate the particular error types that we currently
1105 // care about (e.g., missing reference images). Later on, if we
1106 // want to also tabulate pixel mismatches vs dimension mistmatches
1107 // (or whatever else), we can do so.
1108 testsRun++;
1109 if (ERROR_NONE == testErrors) {
1110 testsPassed++;
1111 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
1112 testsMissingReferenceImages++;
1113 } else {
1114 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001115 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001116 }
reed@android.com00dae862009-06-10 15:38:48 +00001117 SkDELETE(gm);
1118 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001119 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
1120 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
robertphillips@google.comf6f123d2012-03-21 17:57:55 +00001121
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001122 delete grFactory;
1123 SkGraphics::Term();
1124
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001125 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001126}