blob: 34ded4af9b95c7251a7f98f3d8c33b3933db611c [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.com5af9b202012-06-04 17:17:36 +0000623static ErrorBitfield test_pipe_playback(GM* gm,
624 const ConfigData& gRec,
625 const SkBitmap& comparisonBitmap,
626 const char readPath [],
627 const char diffPath []) {
628 if (kRaster_Backend != gRec.fBackend) {
629 return ERROR_NONE;
630 }
631 SkBitmap bitmap;
632 SkISize size = gm->getISize();
633 setup_bitmap(gRec, size, &bitmap);
634 SkCanvas canvas(bitmap);
635 PipeController pipeController(&canvas);
636 SkGPipeWriter writer;
637 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
638 SkGPipeWriter::kCrossProcess_Flag);
639 invokeGM(gm, pipeCanvas);
640 writer.endRecording();
641 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
642 "-pipe", bitmap, NULL, &comparisonBitmap);
643}
644
scroggo@google.com72c96722012-06-06 21:07:10 +0000645static ErrorBitfield test_tiled_pipe_playback(GM* gm,
646 const ConfigData& gRec,
647 const SkBitmap& comparisonBitmap,
648 const char readPath [],
649 const char diffPath []) {
650 if (kRaster_Backend != gRec.fBackend) {
651 return ERROR_NONE;
652 }
653 SkBitmap bitmap;
654 SkISize size = gm->getISize();
655 setup_bitmap(gRec, size, &bitmap);
656 TiledPipeController pipeController(bitmap);
657 SkGPipeWriter writer;
658 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
659 SkGPipeWriter::kCrossProcess_Flag);
660 invokeGM(gm, pipeCanvas);
661 writer.endRecording();
662 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
663 "-tiled pipe", bitmap, NULL, &comparisonBitmap);
664}
665
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000666static void write_picture_serialization(GM* gm, const ConfigData& rec,
667 const char writePicturePath[]) {
668 // only do this once, so we pick raster
669 if (kRaster_Backend == rec.fBackend &&
670 SkBitmap::kARGB_8888_Config == rec.fConfig) {
671
672 SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
673
674 const char* pictureSuffix = "skp";
675 SkString path = make_filename(writePicturePath, "",
676 SkString(gm->shortName()), pictureSuffix);
677
678 SkFILEWStream stream(path.c_str());
679 pict->serialize(&stream);
680 }
681}
682
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000683static void usage(const char * argv0) {
junov@google.com77e498e2012-01-18 18:56:34 +0000684 SkDebugf(
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000685 "%s [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n"
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000686 " [--noreplay] [--pipe] [--serialize] [--forceBWtext] [--nopdf] \n"
scroggo@google.com72c96722012-06-06 21:07:10 +0000687 " [--tiledPipe] \n"
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000688 " [--nodeferred] [--match substring] [--notexturecache]\n"
bsalomon@google.com7361f542012-04-19 19:15:35 +0000689 , argv0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000690 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000691 SkDebugf(
692" readPath: directory to read reference images from;\n"
693" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000694 SkDebugf(" diffPath: directory to write difference images in.\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000695 SkDebugf(" resourcePath: directory that stores image resources.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000696 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000697 SkDebugf(" --pipe: Exercise SkGPipe replay.\n");
scroggo@google.com72c96722012-06-06 21:07:10 +0000698 SkDebugf(" --tiledPipe: Exercise tiled SkGPipe replay.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000699 SkDebugf(
700" --serialize: exercise SkPicture serialization & deserialization.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000701 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
702 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
703 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000704 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
twiz@google.come24a0792012-01-31 18:35:30 +0000705 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000706}
707
bsalomon@google.com7361f542012-04-19 19:15:35 +0000708static const GrContextFactory::GLContextType kDontCare_GLContextType =
709 GrContextFactory::kNative_GLContextType;
710
711// If the platform does not support writing PNGs of PDFs then there will be no
712// comparison images to read. However, we can always write the .pdf files
713static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
714 kWrite_ConfigFlag;
715
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000716static const ConfigData gRec[] = {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000717 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888" },
718 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444" },
719 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000720#ifdef SK_SCALAR_IS_FLOAT
bsalomon@google.com7361f542012-04-19 19:15:35 +0000721 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu" },
722 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16" },
723 /* The debug context does not generate images */
724 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "debug" },
725 #ifdef SK_ANGLE
726 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle" },
727 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16" },
728 #endif
729 #ifdef SK_MESA
730 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa" },
731 #endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000732#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000733#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000734 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
735 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps" },
736#endif
737#ifdef SK_SUPPORT_PDF
738 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf" },
bungeman@google.comb29c8832011-10-10 13:19:10 +0000739#endif
reed@android.com00dae862009-06-10 15:38:48 +0000740};
741
reed@google.comb2a51622011-10-31 16:30:04 +0000742static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
743 if (0 == array.count()) {
744 // no names, so don't skip anything
745 return false;
746 }
747 for (int i = 0; i < array.count(); ++i) {
748 if (strstr(name, array[i])) {
749 // found the name, so don't skip
750 return false;
751 }
752 }
753 return true;
754}
755
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000756namespace skiagm {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000757SkAutoTUnref<GrContext> gGrContext;
758/**
759 * Sets the global GrContext, accessible by indivual GMs
760 */
caryclark@google.com13130862012-06-06 12:10:45 +0000761static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000762 SkSafeRef(grContext);
763 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000764}
bsalomon@google.com7361f542012-04-19 19:15:35 +0000765
766/**
767 * Gets the global GrContext, can be called by GM tests.
768 */
caryclark@google.com13130862012-06-06 12:10:45 +0000769GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000770GrContext* GetGr() {
771 return gGrContext.get();
772}
773
774/**
775 * Sets the global GrContext and then resets it to its previous value at
776 * destruction.
777 */
778class AutoResetGr : SkNoncopyable {
779public:
780 AutoResetGr() : fOld(NULL) {}
781 void set(GrContext* context) {
782 SkASSERT(NULL == fOld);
783 fOld = GetGr();
784 SkSafeRef(fOld);
785 SetGr(context);
786 }
787 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
788private:
789 GrContext* fOld;
790};
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000791}
792
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000793int main(int argc, char * const argv[]) {
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000794 SkGraphics::Init();
reed@google.com8923c6c2011-11-08 14:59:38 +0000795 // we don't need to see this during a run
796 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000797
epoger@google.com7bc13a62012-02-14 14:53:59 +0000798 setSystemPreferences();
799
reed@android.com8015dd82009-06-21 00:49:18 +0000800 const char* writePath = NULL; // if non-null, where we write the originals
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000801 const char* writePicturePath = NULL; // if non-null, where we write serialized pictures
reed@android.com8015dd82009-06-21 00:49:18 +0000802 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000803 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000804 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000805
reed@google.comb2a51622011-10-31 16:30:04 +0000806 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000807
reed@google.comab973972011-09-19 19:01:38 +0000808 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000809 bool doReplay = true;
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000810 bool doPipe = false;
scroggo@google.com72c96722012-06-06 21:07:10 +0000811 bool doTiledPipe = false;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000812 bool doSerialize = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000813 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000814 bool disableTextureCache = false;
815
reed@google.come8fcb502012-05-17 15:28:20 +0000816 gNotifyMissingReadReference = true;
817
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000818 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000819 char* const* stop = argv + argc;
820 for (++argv; argv < stop; ++argv) {
821 if (strcmp(*argv, "-w") == 0) {
822 argv++;
823 if (argv < stop && **argv) {
824 writePath = *argv;
825 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000826 } else if (strcmp(*argv, "-wp") == 0) {
827 argv++;
828 if (argv < stop && **argv) {
829 writePicturePath = *argv;
830 }
reed@android.com8015dd82009-06-21 00:49:18 +0000831 } else if (strcmp(*argv, "-r") == 0) {
832 argv++;
833 if (argv < stop && **argv) {
834 readPath = *argv;
835 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000836 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000837 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000838 if (argv < stop && **argv) {
839 diffPath = *argv;
840 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000841 } else if (strcmp(*argv, "-i") == 0) {
842 argv++;
843 if (argv < stop && **argv) {
844 resourcePath = *argv;
845 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000846 } else if (strcmp(*argv, "--forceBWtext") == 0) {
847 gForceBWtext = true;
scroggo@google.comd13bdfb2012-06-04 17:45:54 +0000848 } else if (strcmp(*argv, "--pipe") == 0) {
849 doPipe = true;
scroggo@google.com72c96722012-06-06 21:07:10 +0000850 } else if (strcmp(*argv, "--tiledPipe") == 0) {
851 doTiledPipe = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000852 } else if (strcmp(*argv, "--noreplay") == 0) {
853 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000854 } else if (strcmp(*argv, "--nopdf") == 0) {
855 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000856 } else if (strcmp(*argv, "--nodeferred") == 0) {
857 doDeferred = false;
reed@google.come8fcb502012-05-17 15:28:20 +0000858 } else if (strcmp(*argv, "--disable-missing-warning") == 0) {
859 gNotifyMissingReadReference = false;
860 } else if (strcmp(*argv, "--enable-missing-warning") == 0) {
861 gNotifyMissingReadReference = true;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000862 } else if (strcmp(*argv, "--serialize") == 0) {
863 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000864 } else if (strcmp(*argv, "--match") == 0) {
865 ++argv;
866 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000867 // just record the ptr, no need for a deep copy
868 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000869 }
twiz@google.come24a0792012-01-31 18:35:30 +0000870 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000871 disableTextureCache = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000872 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000873 usage(commandName);
874 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000875 }
876 }
877 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000878 usage(commandName);
879 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000880 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000881
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000882 GM::SetResourcePath(resourcePath);
883
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000884 GrContextFactory* grFactory = new GrContextFactory;
reed@google.com873cb1e2010-12-23 15:00:45 +0000885
reed@android.com00f883e2010-12-14 17:46:14 +0000886 if (readPath) {
887 fprintf(stderr, "reading from %s\n", readPath);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000888 }
889 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +0000890 fprintf(stderr, "writing to %s\n", writePath);
891 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000892 if (writePicturePath) {
893 fprintf(stderr, "writing pictures to %s\n", writePicturePath);
894 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000895 if (resourcePath) {
896 fprintf(stderr, "reading resources from %s\n", resourcePath);
897 }
898
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000899 // Accumulate success of all tests.
900 int testsRun = 0;
901 int testsPassed = 0;
902 int testsFailed = 0;
903 int testsMissingReferenceImages = 0;
904
twiz@google.come24a0792012-01-31 18:35:30 +0000905 if (disableTextureCache) {
906 skiagm::GetGr()->setTextureCacheLimits(0, 0);
907 }
908
bsalomon@google.com7361f542012-04-19 19:15:35 +0000909 Iter iter;
910 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +0000911 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000912 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000913 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000914 SkDELETE(gm);
915 continue;
916 }
917
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000918 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000919 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000920 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000921 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000922
bsalomon@google.com29d35012011-11-30 16:57:21 +0000923 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000924 SkAutoTUnref<GrRenderTarget> rt;
925 AutoResetGr autogr;
926 if (kGPU_Backend == gRec[i].fBackend) {
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000927 GrContext* gr = grFactory->get(gRec[i].fGLContextType);
bsalomon@google.com7361f542012-04-19 19:15:35 +0000928 if (!gr) {
929 continue;
930 }
931
932 // create a render target to back the device
933 GrTextureDesc desc;
934 desc.fConfig = kSkia8888_PM_GrPixelConfig;
935 desc.fFlags = kRenderTarget_GrTextureFlagBit;
936 desc.fWidth = gm->getISize().width();
937 desc.fHeight = gm->getISize().height();
938 desc.fSampleCnt = gRec[i].fSampleCnt;
939 GrTexture* tex = gr->createUncachedTexture(desc, NULL, 0);
940 if (!tex) {
bsalomon@google.comdaf12bb2012-04-20 19:08:44 +0000941 continue;
bsalomon@google.com7361f542012-04-19 19:15:35 +0000942 }
943 rt.reset(tex->asRenderTarget());
944 rt.get()->ref();
945 tex->unref();
946
947 autogr.set(gr);
948 }
949
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000950 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000951 uint32_t gmFlags = gm->getFlags();
twiz@google.come24a0792012-01-31 18:35:30 +0000952 if ((kPDF_Backend == gRec[i].fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +0000953 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
954 {
reed@google.comab973972011-09-19 19:01:38 +0000955 continue;
956 }
957
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000958 // Now we know that we want to run this test and record its
959 // success or failure.
960 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000961
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000962 if ((ERROR_NONE == testErrors) &&
963 (kGPU_Backend == gRec[i].fBackend) &&
964 (NULL == rt.get())) {
965 fprintf(stderr, "Could not create render target for gpu.\n");
966 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000967 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000968
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000969 if (ERROR_NONE == testErrors) {
970 testErrors |= test_drawing(gm, gRec[i],
971 writePath, readPath, diffPath,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000972 GetGr(),
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000973 rt.get(), &forwardRenderedBitmap);
974 }
975
junov@google.com4370aed2012-01-18 16:21:08 +0000976 if (doDeferred && !testErrors &&
twiz@google.come24a0792012-01-31 18:35:30 +0000977 (kGPU_Backend == gRec[i].fBackend ||
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000978 kRaster_Backend == gRec[i].fBackend)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000979 testErrors |= test_deferred_drawing(gm, gRec[i],
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000980 forwardRenderedBitmap,
bsalomon@google.com7361f542012-04-19 19:15:35 +0000981 diffPath, GetGr(), rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +0000982 }
983
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000984 if ((ERROR_NONE == testErrors) && doReplay &&
985 !(gmFlags & GM::kSkipPicture_Flag)) {
986 testErrors |= test_picture_playback(gm, gRec[i],
987 forwardRenderedBitmap,
988 readPath, diffPath);
989 }
990
scroggo@google.com5af9b202012-06-04 17:17:36 +0000991 if ((ERROR_NONE == testErrors) && doPipe &&
992 !(gmFlags & GM::kSkipPipe_Flag)) {
993 testErrors |= test_pipe_playback(gm, gRec[i],
994 forwardRenderedBitmap,
995 readPath, diffPath);
996 }
997
scroggo@google.com72c96722012-06-06 21:07:10 +0000998 if ((ERROR_NONE == testErrors) && doTiledPipe &&
999 !(gmFlags & GM::kSkipPipe_Flag)) {
1000 testErrors |= test_tiled_pipe_playback(gm, gRec[i],
1001 forwardRenderedBitmap,
1002 readPath, diffPath);
1003 }
1004
djsollen@google.coma2ca41e2012-03-23 19:00:34 +00001005 if ((ERROR_NONE == testErrors) && doSerialize &&
1006 !(gmFlags & GM::kSkipPicture_Flag)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001007 testErrors |= test_picture_serialization(gm, gRec[i],
1008 forwardRenderedBitmap,
1009 readPath, diffPath);
1010 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001011
1012 if (!(gmFlags & GM::kSkipPicture_Flag) && writePicturePath) {
1013 write_picture_serialization(gm, gRec[i], writePicturePath);
1014 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001015
1016 // Update overall results.
1017 // We only tabulate the particular error types that we currently
1018 // care about (e.g., missing reference images). Later on, if we
1019 // want to also tabulate pixel mismatches vs dimension mistmatches
1020 // (or whatever else), we can do so.
1021 testsRun++;
1022 if (ERROR_NONE == testErrors) {
1023 testsPassed++;
1024 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
1025 testsMissingReferenceImages++;
1026 } else {
1027 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001028 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001029 }
reed@android.com00dae862009-06-10 15:38:48 +00001030 SkDELETE(gm);
1031 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001032 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
1033 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
robertphillips@google.comf6f123d2012-03-21 17:57:55 +00001034
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001035 delete grFactory;
1036 SkGraphics::Term();
1037
1038 PRINT_INST_COUNT(SkRefCnt);
1039 PRINT_INST_COUNT(GrResource);
1040
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001041 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001042}