blob: c4ed298efa8d70a2255db76e5508f89ece7a80c2 [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"
reed@android.comb9b9a182009-07-08 02:54:47 +000010#include "SkColorPriv.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000011#include "SkData.h"
junov@google.com4370aed2012-01-18 16:21:08 +000012#include "SkDeferredCanvas.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000013#include "SkDevice.h"
scroggo@google.com5af9b202012-06-04 17:17:36 +000014#include "SkGPipe.h"
reed@android.com8015dd82009-06-21 00:49:18 +000015#include "SkGraphics.h"
16#include "SkImageDecoder.h"
17#include "SkImageEncoder.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000018#include "SkPicture.h"
robertphillips@google.com977b9c82012-06-05 19:35:09 +000019#include "SkRefCnt.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000020#include "SkStream.h"
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000021#include "SkTArray.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000022#include "SamplePipeControllers.h"
reed@google.com07700442010-12-20 19:46:07 +000023
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000024#if SK_SUPPORT_GPU
25#include "GrContextFactory.h"
26#include "GrRenderTarget.h"
27#include "SkGpuDevice.h"
28#include "SkGpuCanvas.h"
29typedef GrContextFactory::GLContextType GLContextType;
30#else
31class GrContext;
32class GrRenderTarget;
33typedef int GLContextType;
34#endif
35
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000036static bool gForceBWtext;
37
reed@google.com8923c6c2011-11-08 14:59:38 +000038extern bool gSkSuppressFontCachePurgeSpew;
39
reed@google.com07700442010-12-20 19:46:07 +000040#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000041 #include "SkPDFDevice.h"
42 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000043#endif
reed@android.com00dae862009-06-10 15:38:48 +000044
epoger@google.come3cc2eb2012-01-18 20:11:13 +000045// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
46// stop writing out XPS-format image baselines in gm.
47#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000048#ifdef SK_SUPPORT_XPS
49 #include "SkXPSDevice.h"
50#endif
51
reed@google.com46cce912011-06-29 12:54:46 +000052#ifdef SK_BUILD_FOR_MAC
53 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000054 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000055#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000056 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000057#endif
58
epoger@google.comc7cf2b32011-12-28 19:31:01 +000059typedef int ErrorBitfield;
60const static ErrorBitfield ERROR_NONE = 0x00;
61const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
62const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
63const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
64const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
65const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
66
reed@google.come8fcb502012-05-17 15:28:20 +000067// If true, emit a messange when we can't find a reference image to compare
68static bool gNotifyMissingReadReference;
69
reed@android.com00dae862009-06-10 15:38:48 +000070using namespace skiagm;
71
reed@android.com00dae862009-06-10 15:38:48 +000072class Iter {
73public:
74 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000075 this->reset();
76 }
77
78 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000079 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000080 }
reed@google.comd4dfd102011-01-18 21:05:42 +000081
reed@android.comdd0ac282009-06-20 02:38:16 +000082 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000083 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000084 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000085 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000086 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000087 }
88 return NULL;
89 }
reed@google.comd4dfd102011-01-18 21:05:42 +000090
reed@android.com00dae862009-06-10 15:38:48 +000091 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000092 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000093 int count = 0;
94 while (reg) {
95 count += 1;
96 reg = reg->next();
97 }
98 return count;
99 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000100
reed@android.com00dae862009-06-10 15:38:48 +0000101private:
102 const GMRegistry* fReg;
103};
104
reed@android.com8015dd82009-06-21 00:49:18 +0000105static SkString make_name(const char shortName[], const char configName[]) {
106 SkString name(shortName);
107 name.appendf("_%s", configName);
108 return name;
109}
110
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000111static SkString make_filename(const char path[],
112 const char pathSuffix[],
113 const SkString& name,
114 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000115 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000116 if (filename.endsWith("/")) {
117 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000118 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000119 filename.append(pathSuffix);
120 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000121 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000122 return filename;
123}
124
reed@android.comb9b9a182009-07-08 02:54:47 +0000125/* since PNG insists on unpremultiplying our alpha, we take no precision chances
126 and force all pixels to be 100% opaque, otherwise on compare we may not get
127 a perfect match.
128 */
129static void force_all_opaque(const SkBitmap& bitmap) {
130 SkAutoLockPixels lock(bitmap);
131 for (int y = 0; y < bitmap.height(); y++) {
132 for (int x = 0; x < bitmap.width(); x++) {
133 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
134 }
135 }
136}
137
138static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
139 SkBitmap copy;
140 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
141 force_all_opaque(copy);
142 return SkImageEncoder::EncodeFile(path.c_str(), copy,
143 SkImageEncoder::kPNG_Type, 100);
144}
145
reed@google.com3d3f0922010-12-20 21:10:29 +0000146static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000147 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
148 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
149 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
150 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000151}
152
153static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000154 SkBitmap* diff) {
155 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000156
157 const int w = target.width();
158 const int h = target.height();
159 for (int y = 0; y < h; y++) {
160 for (int x = 0; x < w; x++) {
161 SkPMColor c0 = *base.getAddr32(x, y);
162 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000163 SkPMColor d = 0;
164 if (c0 != c1) {
165 d = compute_diff_pmcolor(c0, c1);
166 }
167 *diff->getAddr32(x, y) = d;
168 }
169 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000170}
171
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000172static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
173 const SkString& name,
174 const char* renderModeDescriptor,
175 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000176 SkBitmap copy;
177 const SkBitmap* bm = &target;
178 if (target.config() != SkBitmap::kARGB_8888_Config) {
179 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
180 bm = &copy;
181 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000182 SkBitmap baseCopy;
183 const SkBitmap* bp = &base;
184 if (base.config() != SkBitmap::kARGB_8888_Config) {
185 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
186 bp = &baseCopy;
187 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000188
189 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000190 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000191
192 const int w = bm->width();
193 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000194 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000195 SkDebugf(
196"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
197 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000198 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000199 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000200 }
201
202 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000203 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000204
205 for (int y = 0; y < h; y++) {
206 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000207 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000208 SkPMColor c1 = *bm->getAddr32(x, y);
209 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000210 SkDebugf(
211"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
212 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000213
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000214 if (diff) {
215 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
216 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000217 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000218 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000219 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000220 }
221 }
222 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000223
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000224 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000225 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000226}
reed@android.com00dae862009-06-10 15:38:48 +0000227
bungeman@google.comb29c8832011-10-10 13:19:10 +0000228static bool write_document(const SkString& path,
229 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000230 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000231 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000232 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000233}
234
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000235enum Backend {
236 kRaster_Backend,
237 kGPU_Backend,
238 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000239 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000240};
241
bsalomon@google.com7361f542012-04-19 19:15:35 +0000242enum ConfigFlags {
243 kNone_ConfigFlag = 0x0,
244 /* Write GM images if a write path is provided. */
245 kWrite_ConfigFlag = 0x1,
246 /* Read comparison GM images if a read path is provided. */
247 kRead_ConfigFlag = 0x2,
248 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
249};
250
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000251struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000252 SkBitmap::Config fConfig;
253 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000254 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000255 int fSampleCnt; // GPU backend only
256 ConfigFlags fFlags;
257 const char* fName;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000258};
259
260/// Returns true if processing should continue, false to skip the
261/// remainder of this config for this GM.
262//@todo thudson 22 April 2011 - could refactor this to take in
263// a factory to generate the context, always call readPixels()
264// (logically a noop for rasters, if wasted time), and thus collapse the
265// GPU special case and also let this be used for SkPicture testing.
266static void setup_bitmap(const ConfigData& gRec, SkISize& size,
267 SkBitmap* bitmap) {
268 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
269 bitmap->allocPixels();
270 bitmap->eraseColor(0);
271}
272
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000273#include "SkDrawFilter.h"
274class BWTextDrawFilter : public SkDrawFilter {
275public:
276 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
277};
278void BWTextDrawFilter::filter(SkPaint* p, Type t) {
279 if (kText_Type == t) {
280 p->setAntiAlias(false);
281 }
282}
283
284static void installFilter(SkCanvas* canvas) {
285 if (gForceBWtext) {
286 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
287 }
288}
289
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000290static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
reed@google.com778e1632012-06-04 20:00:01 +0000291 SkAutoCanvasRestore acr(canvas, true);
292
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000293 if (!isPDF) {
reed@google.com778e1632012-06-04 20:00:01 +0000294 canvas->concat(gm->getInitialTransform());
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000295 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000296 installFilter(canvas);
297 gm->draw(canvas);
298 canvas->setDrawFilter(NULL);
299}
300
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000301static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
302 GrContext* context,
303 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000304 SkBitmap* bitmap,
305 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000306 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000307 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000308
309 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000310 SkCanvas* canvas;
311 if (deferred) {
312 canvas = new SkDeferredCanvas;
313 canvas->setDevice(new SkDevice(*bitmap))->unref();
314 } else {
315 canvas = new SkCanvas(*bitmap);
316 }
317 SkAutoUnref canvasUnref(canvas);
318 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000319 canvas->flush();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000320 }
321#if SK_SUPPORT_GPU
322 else { // GPU
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000323 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000324 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000325 }
junov@google.com4370aed2012-01-18 16:21:08 +0000326 SkCanvas* gc;
327 if (deferred) {
328 gc = new SkDeferredCanvas;
329 } else {
330 gc = new SkGpuCanvas(context, rt);
331 }
332 SkAutoUnref gcUnref(gc);
333 gc->setDevice(new SkGpuDevice(context, rt))->unref();
334 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000335 // the device is as large as the current rendertarget, so we explicitly
336 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000337 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000338 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
339 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000340 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000341 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000342#endif
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000343 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000344}
345
346static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
347 SkPicture* pict, SkBitmap* bitmap) {
348 SkISize size = gm->getISize();
349 setup_bitmap(gRec, size, bitmap);
350 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000351 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000352 canvas.drawPicture(*pict);
353}
354
355static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
356#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000357 SkMatrix initialTransform = gm->getInitialTransform();
358 SkISize pageSize = gm->getISize();
359 SkPDFDevice* dev = NULL;
360 if (initialTransform.isIdentity()) {
361 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
362 } else {
363 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
364 SkIntToScalar(pageSize.height()));
365 initialTransform.mapRect(&content);
366 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
367 SkIntToScalar(pageSize.height()));
368 SkISize contentSize =
369 SkISize::Make(SkScalarRoundToInt(content.width()),
370 SkScalarRoundToInt(content.height()));
371 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
372 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000373 SkAutoUnref aur(dev);
374
375 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000376 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000377
378 SkPDFDocument doc;
379 doc.appendPage(dev);
380 doc.emitPDF(&pdf);
381#endif
382}
383
bungeman@google.comb29c8832011-10-10 13:19:10 +0000384static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
385#ifdef SK_SUPPORT_XPS
386 SkISize size = gm->getISize();
chudy@google.comf32f6e82012-07-12 15:42:37 +0000387
bungeman@google.comb29c8832011-10-10 13:19:10 +0000388 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
389 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000390 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
391 static const SkScalar upm = 72 * inchesPerMeter;
392 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
393 static const SkScalar ppm = 200 * inchesPerMeter;
394 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000395
396 SkXPSDevice* dev = new SkXPSDevice();
397 SkAutoUnref aur(dev);
398
399 SkCanvas c(dev);
400 dev->beginPortfolio(&xps);
401 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000402 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000403 dev->endSheet();
404 dev->endPortfolio();
405
406#endif
407}
408
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000409static ErrorBitfield write_reference_image(const ConfigData& gRec,
410 const char writePath [],
411 const char renderModeDescriptor [],
412 const SkString& name,
413 SkBitmap& bitmap,
414 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000415 SkString path;
416 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000417 if (gRec.fBackend == kRaster_Backend ||
418 gRec.fBackend == kGPU_Backend ||
419 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
chudy@google.comf32f6e82012-07-12 15:42:37 +0000420
tomhudson@google.comea325432011-06-09 20:30:03 +0000421 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000422 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000423 }
424 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000425 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000426 success = write_document(path, *document);
427 }
428 if (kXPS_Backend == gRec.fBackend) {
429 path = make_filename(writePath, renderModeDescriptor, name, "xps");
430 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000431 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000432 if (success) {
433 return ERROR_NONE;
434 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000435 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000436 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000437 }
438}
439
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000440static ErrorBitfield compare_to_reference_image(const SkString& name,
441 SkBitmap &bitmap,
442 const SkBitmap& comparisonBitmap,
443 const char diffPath [],
444 const char renderModeDescriptor []) {
445 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000446 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000447 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
448 diffPath ? &diffBitmap : NULL);
reed@google.com8e529b72012-04-09 20:20:10 +0000449 if ((ERROR_NONE != errors) && diffPath) {
450 // write out the generated image
451 SkString genName = make_filename(diffPath, "", name, "png");
452 if (!write_bitmap(genName, bitmap)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000453 errors |= ERROR_WRITING_REFERENCE_IMAGE;
454 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000455 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000456 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000457}
458
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000459static ErrorBitfield compare_to_reference_image(const char readPath [],
460 const SkString& name,
461 SkBitmap &bitmap,
462 const char diffPath [],
463 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000464 SkString path = make_filename(readPath, "", name, "png");
465 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000466 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
467 SkBitmap::kARGB_8888_Config,
468 SkImageDecoder::kDecodePixels_Mode, NULL)) {
469 return compare_to_reference_image(name, bitmap,
470 orig, diffPath,
471 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000472 } else {
reed@google.come8fcb502012-05-17 15:28:20 +0000473 if (gNotifyMissingReadReference) {
474 fprintf(stderr, "FAILED to read %s\n", path.c_str());
475 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000476 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000477 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000478}
479
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000480static ErrorBitfield handle_test_results(GM* gm,
481 const ConfigData& gRec,
482 const char writePath [],
483 const char readPath [],
484 const char diffPath [],
485 const char renderModeDescriptor [],
486 SkBitmap& bitmap,
487 SkDynamicMemoryWStream* pdf,
488 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000489 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000490 ErrorBitfield retval = ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000491
bsalomon@google.com7361f542012-04-19 19:15:35 +0000492 if (readPath && (gRec.fFlags & kRead_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000493 retval |= compare_to_reference_image(readPath, name, bitmap,
494 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000495 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000496 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000497 retval |= write_reference_image(gRec, writePath, renderModeDescriptor,
498 name, bitmap, pdf);
499 }
500 if (comparisonBitmap) {
501 retval |= compare_to_reference_image(name, bitmap,
502 *comparisonBitmap, diffPath,
503 renderModeDescriptor);
504 }
505 return retval;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000506}
507
508static SkPicture* generate_new_picture(GM* gm) {
509 // Pictures are refcounted so must be on heap
510 SkPicture* pict = new SkPicture;
511 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000512 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000513 pict->endRecording();
514
515 return pict;
516}
517
518static SkPicture* stream_to_new_picture(const SkPicture& src) {
519
520 // To do in-memory commiunications with a stream, we need to:
521 // * create a dynamic memory stream
522 // * copy it into a buffer
523 // * create a read stream from it
524 // ?!?!
525
526 SkDynamicMemoryWStream storage;
527 src.serialize(&storage);
528
529 int streamSize = storage.getOffset();
530 SkAutoMalloc dstStorage(streamSize);
531 void* dst = dstStorage.get();
532 //char* dst = new char [streamSize];
533 //@todo thudson 22 April 2011 when can we safely delete [] dst?
534 storage.copyTo(dst);
535 SkMemoryStream pictReadback(dst, streamSize);
536 SkPicture* retval = new SkPicture (&pictReadback);
537 return retval;
538}
539
540// Test: draw into a bitmap or pdf.
541// Depending on flags, possibly compare to an expected image
542// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000543static ErrorBitfield test_drawing(GM* gm,
544 const ConfigData& gRec,
545 const char writePath [],
546 const char readPath [],
547 const char diffPath [],
548 GrContext* context,
549 GrRenderTarget* rt,
550 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000551 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000552
553 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000554 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000555 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000556 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
557 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000558 if (ERROR_NONE != errors) {
559 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000560 }
reed@google.com46cce912011-06-29 12:54:46 +0000561 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000562 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000563#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000564 SkAutoDataUnref data(document.copyToData());
robertphillips@google.com5c0b3132012-07-10 17:50:00 +0000565 SkMemoryStream stream(data->data(), data->size());
reed@google.com46cce912011-06-29 12:54:46 +0000566 SkPDFDocumentToBitmap(&stream, bitmap);
567#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000568 } else if (gRec.fBackend == kXPS_Backend) {
569 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000570 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000571 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000572 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000573}
574
junov@google.com4370aed2012-01-18 16:21:08 +0000575static ErrorBitfield test_deferred_drawing(GM* gm,
576 const ConfigData& gRec,
577 const SkBitmap& comparisonBitmap,
578 const char diffPath [],
579 GrContext* context,
580 GrRenderTarget* rt) {
581 SkDynamicMemoryWStream document;
582
583 if (gRec.fBackend == kRaster_Backend ||
584 gRec.fBackend == kGPU_Backend) {
585 SkBitmap bitmap;
586 // Early exit if we can't generate the image, but this is
587 // expected in some cases, so don't report a test failure.
588 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
589 return ERROR_NONE;
590 }
591 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
592 "-deferred", bitmap, NULL, &comparisonBitmap);
593 }
594 return ERROR_NONE;
595}
596
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000597static ErrorBitfield test_picture_playback(GM* gm,
598 const ConfigData& gRec,
599 const SkBitmap& comparisonBitmap,
600 const char readPath [],
601 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000602 SkPicture* pict = generate_new_picture(gm);
603 SkAutoUnref aur(pict);
604
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000605 if (kRaster_Backend == gRec.fBackend) {
606 SkBitmap bitmap;
607 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000608 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
609 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000610 } else {
611 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000612 }
613}
614
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000615static ErrorBitfield test_picture_serialization(GM* gm,
616 const ConfigData& gRec,
617 const SkBitmap& comparisonBitmap,
618 const char readPath [],
619 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000620 SkPicture* pict = generate_new_picture(gm);
621 SkAutoUnref aurp(pict);
622 SkPicture* repict = stream_to_new_picture(*pict);
623 SkAutoUnref aurr(repict);
624
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000625 if (kRaster_Backend == gRec.fBackend) {
626 SkBitmap bitmap;
627 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000628 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
629 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000630 } else {
631 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000632 }
633}
634
scroggo@google.com565254b2012-06-28 15:41:32 +0000635struct PipeFlagComboData {
636 const char* name;
637 uint32_t flags;
638};
639
640static PipeFlagComboData gPipeWritingFlagCombos[] = {
641 { "", 0 },
642 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000643 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000644 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000645};
646
scroggo@google.com5af9b202012-06-04 17:17:36 +0000647static ErrorBitfield test_pipe_playback(GM* gm,
648 const ConfigData& gRec,
649 const SkBitmap& comparisonBitmap,
650 const char readPath [],
651 const char diffPath []) {
652 if (kRaster_Backend != gRec.fBackend) {
653 return ERROR_NONE;
654 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000655 ErrorBitfield errors = ERROR_NONE;
656 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
657 SkBitmap bitmap;
658 SkISize size = gm->getISize();
659 setup_bitmap(gRec, size, &bitmap);
660 SkCanvas canvas(bitmap);
661 PipeController pipeController(&canvas);
662 SkGPipeWriter writer;
663 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
664 gPipeWritingFlagCombos[i].flags);
665 invokeGM(gm, pipeCanvas);
666 writer.endRecording();
667 SkString string("-pipe");
668 string.append(gPipeWritingFlagCombos[i].name);
669 errors |= handle_test_results(gm, gRec, NULL, NULL, diffPath,
670 string.c_str(), bitmap, NULL, &comparisonBitmap);
671 if (errors != ERROR_NONE) {
672 break;
673 }
674 }
675 return errors;
scroggo@google.com5af9b202012-06-04 17:17:36 +0000676}
677
scroggo@google.com72c96722012-06-06 21:07:10 +0000678static ErrorBitfield test_tiled_pipe_playback(GM* gm,
679 const ConfigData& gRec,
680 const SkBitmap& comparisonBitmap,
681 const char readPath [],
682 const char diffPath []) {
683 if (kRaster_Backend != gRec.fBackend) {
684 return ERROR_NONE;
685 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000686 ErrorBitfield errors = ERROR_NONE;
687 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
688 SkBitmap bitmap;
689 SkISize size = gm->getISize();
690 setup_bitmap(gRec, size, &bitmap);
691 SkCanvas canvas(bitmap);
692 TiledPipeController pipeController(bitmap);
693 SkGPipeWriter writer;
694 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
695 gPipeWritingFlagCombos[i].flags);
696 invokeGM(gm, pipeCanvas);
697 writer.endRecording();
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000698 SkString string("-tiled pipe");
scroggo@google.com565254b2012-06-28 15:41:32 +0000699 string.append(gPipeWritingFlagCombos[i].name);
700 errors |= handle_test_results(gm, gRec, NULL, NULL, diffPath,
701 string.c_str(), bitmap, NULL, &comparisonBitmap);
702 if (errors != ERROR_NONE) {
703 break;
704 }
705 }
706 return errors;
scroggo@google.com72c96722012-06-06 21:07:10 +0000707}
708
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000709static void write_picture_serialization(GM* gm, const ConfigData& rec,
710 const char writePicturePath[]) {
711 // only do this once, so we pick raster
712 if (kRaster_Backend == rec.fBackend &&
713 SkBitmap::kARGB_8888_Config == rec.fConfig) {
714
715 SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
chudy@google.comf32f6e82012-07-12 15:42:37 +0000716
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000717 const char* pictureSuffix = "skp";
718 SkString path = make_filename(writePicturePath, "",
719 SkString(gm->shortName()), pictureSuffix);
chudy@google.comf32f6e82012-07-12 15:42:37 +0000720
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000721 SkFILEWStream stream(path.c_str());
722 pict->serialize(&stream);
723 }
724}
725
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000726#if SK_SUPPORT_GPU
727static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
728#else
729static const GLContextType kDontCare_GLContextType = 0;
730#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +0000731
732// If the platform does not support writing PNGs of PDFs then there will be no
733// comparison images to read. However, we can always write the .pdf files
734static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
735 kWrite_ConfigFlag;
736
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000737static const ConfigData gRec[] = {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000738 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888" },
739 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444" },
740 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000741#if defined(SK_SCALAR_IS_FLOAT) && SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +0000742 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu" },
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000743#ifndef SK_BUILD_FOR_ANDROID
744 // currently we don't want to run MSAA tests on Android
bsalomon@google.com7361f542012-04-19 19:15:35 +0000745 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16" },
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000746#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +0000747 /* The debug context does not generate images */
748 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "debug" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000749#if SK_ANGLE
bsalomon@google.com7361f542012-04-19 19:15:35 +0000750 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle" },
751 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000752#endif // SK_ANGLE
753#ifdef SK_MESA
bsalomon@google.com7361f542012-04-19 19:15:35 +0000754 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000755#endif // SK_MESA
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000756#endif // defined(SK_SCALAR_IS_FLOAT) && SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +0000757#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000758 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
759 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps" },
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000760#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000761#ifdef SK_SUPPORT_PDF
762 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000763#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +0000764};
765
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000766static void usage(const char * argv0) {
767 SkDebugf("%s\n", argv0);
768 SkDebugf(" [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n");
chudy@google.comf32f6e82012-07-12 15:42:37 +0000769 SkDebugf(" [-wp writePicturePath]\n");
reed@google.come5f48b92012-06-22 15:27:39 +0000770 SkDebugf(" [--config ");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000771 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
772 if (i > 0) {
773 SkDebugf("|");
774 }
775 SkDebugf(gRec[i].fName);
776 }
777 SkDebugf(" ]\n");
scroggo@google.com39362522012-09-14 17:37:46 +0000778 SkDebugf(" [--noreplay] [--nopipe] [--noserialize] [--forceBWtext] [--nopdf] \n"
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000779 " [--tiledPipe] \n"
780 " [--nodeferred] [--match substring] [--notexturecache]\n"
781 " [-h|--help]\n"
782 );
783 SkDebugf(" writePath: directory to write rendered images in.\n");
chudy@google.comf32f6e82012-07-12 15:42:37 +0000784 SkDebugf(" writePicturePath: directory to write images to in .skp format.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000785 SkDebugf(
786 " readPath: directory to read reference images from;\n"
787 " reports if any pixels mismatch between reference and new images\n");
788 SkDebugf(" diffPath: directory to write difference images in.\n");
789 SkDebugf(" resourcePath: directory that stores image resources.\n");
790 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
scroggo@google.com565254b2012-06-28 15:41:32 +0000791 SkDebugf(" --nopipe: Skip SkGPipe replay.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000792 SkDebugf(" --tiledPipe: Exercise tiled SkGPipe replay.\n");
793 SkDebugf(
scroggo@google.com39362522012-09-14 17:37:46 +0000794 " --noserialize: do not exercise SkPicture serialization & deserialization.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000795 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
796 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
797 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
798 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
799 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
800 SkDebugf(" -h|--help : Show this help message. \n");
801}
802
803static int findConfig(const char config[]) {
804 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
805 if (!strcmp(config, gRec[i].fName)) {
806 return i;
807 }
808 }
809 return -1;
810}
811
reed@google.comb2a51622011-10-31 16:30:04 +0000812static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
813 if (0 == array.count()) {
814 // no names, so don't skip anything
815 return false;
816 }
817 for (int i = 0; i < array.count(); ++i) {
818 if (strstr(name, array[i])) {
819 // found the name, so don't skip
820 return false;
821 }
822 }
823 return true;
824}
825
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000826namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000827#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +0000828SkAutoTUnref<GrContext> gGrContext;
829/**
830 * Sets the global GrContext, accessible by indivual GMs
831 */
caryclark@google.com13130862012-06-06 12:10:45 +0000832static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000833 SkSafeRef(grContext);
834 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000835}
bsalomon@google.com7361f542012-04-19 19:15:35 +0000836
837/**
838 * Gets the global GrContext, can be called by GM tests.
839 */
caryclark@google.com13130862012-06-06 12:10:45 +0000840GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000841GrContext* GetGr() {
842 return gGrContext.get();
843}
844
845/**
846 * Sets the global GrContext and then resets it to its previous value at
847 * destruction.
848 */
849class AutoResetGr : SkNoncopyable {
850public:
851 AutoResetGr() : fOld(NULL) {}
852 void set(GrContext* context) {
853 SkASSERT(NULL == fOld);
854 fOld = GetGr();
855 SkSafeRef(fOld);
856 SetGr(context);
857 }
858 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
859private:
860 GrContext* fOld;
861};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000862#else
863GrContext* GetGr() { return NULL; }
864#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000865}
866
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000867int main(int argc, char * const argv[]) {
robertphillips@google.comb74af872012-06-27 19:41:42 +0000868
869#ifdef SK_ENABLE_INST_COUNT
870 gPrintInstCount = true;
871#endif
872
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000873 SkGraphics::Init();
reed@google.com8923c6c2011-11-08 14:59:38 +0000874 // we don't need to see this during a run
875 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000876
epoger@google.com7bc13a62012-02-14 14:53:59 +0000877 setSystemPreferences();
878
reed@android.com8015dd82009-06-21 00:49:18 +0000879 const char* writePath = NULL; // if non-null, where we write the originals
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000880 const char* writePicturePath = NULL; // if non-null, where we write serialized pictures
reed@android.com8015dd82009-06-21 00:49:18 +0000881 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000882 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000883 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000884
reed@google.comb2a51622011-10-31 16:30:04 +0000885 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000886
reed@google.comab973972011-09-19 19:01:38 +0000887 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000888 bool doReplay = true;
scroggo@google.com565254b2012-06-28 15:41:32 +0000889 bool doPipe = true;
scroggo@google.com72c96722012-06-06 21:07:10 +0000890 bool doTiledPipe = false;
scroggo@google.com39362522012-09-14 17:37:46 +0000891 bool doSerialize = true;
junov@google.com4370aed2012-01-18 16:21:08 +0000892 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000893 bool disableTextureCache = false;
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000894 SkTDArray<size_t> configs;
895 bool userConfig = false;
twiz@google.come24a0792012-01-31 18:35:30 +0000896
reed@google.come8fcb502012-05-17 15:28:20 +0000897 gNotifyMissingReadReference = true;
898
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000899 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000900 char* const* stop = argv + argc;
901 for (++argv; argv < stop; ++argv) {
902 if (strcmp(*argv, "-w") == 0) {
903 argv++;
904 if (argv < stop && **argv) {
905 writePath = *argv;
906 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000907 } else if (strcmp(*argv, "-wp") == 0) {
908 argv++;
909 if (argv < stop && **argv) {
910 writePicturePath = *argv;
911 }
reed@android.com8015dd82009-06-21 00:49:18 +0000912 } else if (strcmp(*argv, "-r") == 0) {
913 argv++;
914 if (argv < stop && **argv) {
915 readPath = *argv;
916 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000917 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000918 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000919 if (argv < stop && **argv) {
920 diffPath = *argv;
921 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000922 } else if (strcmp(*argv, "-i") == 0) {
923 argv++;
924 if (argv < stop && **argv) {
925 resourcePath = *argv;
926 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000927 } else if (strcmp(*argv, "--forceBWtext") == 0) {
928 gForceBWtext = true;
scroggo@google.com565254b2012-06-28 15:41:32 +0000929 } else if (strcmp(*argv, "--nopipe") == 0) {
930 doPipe = false;
scroggo@google.com72c96722012-06-06 21:07:10 +0000931 } else if (strcmp(*argv, "--tiledPipe") == 0) {
932 doTiledPipe = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000933 } else if (strcmp(*argv, "--noreplay") == 0) {
934 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000935 } else if (strcmp(*argv, "--nopdf") == 0) {
936 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000937 } else if (strcmp(*argv, "--nodeferred") == 0) {
938 doDeferred = false;
reed@google.come8fcb502012-05-17 15:28:20 +0000939 } else if (strcmp(*argv, "--disable-missing-warning") == 0) {
940 gNotifyMissingReadReference = false;
941 } else if (strcmp(*argv, "--enable-missing-warning") == 0) {
942 gNotifyMissingReadReference = true;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000943 } else if (strcmp(*argv, "--serialize") == 0) {
scroggo@google.com39362522012-09-14 17:37:46 +0000944 // Leaving in this option so that a user need not modify their command line arguments
945 // to still run.
borenet@google.com14ca1d32012-06-15 13:46:44 +0000946 doSerialize = true;
scroggo@google.com39362522012-09-14 17:37:46 +0000947 } else if (strcmp(*argv, "--noserialize") == 0) {
948 doSerialize = false;
reed@google.comece2b022011-07-25 14:28:57 +0000949 } else if (strcmp(*argv, "--match") == 0) {
950 ++argv;
951 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000952 // just record the ptr, no need for a deep copy
953 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000954 }
twiz@google.come24a0792012-01-31 18:35:30 +0000955 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000956 disableTextureCache = true;
reed@google.come5f48b92012-06-22 15:27:39 +0000957 } else if (strcmp(*argv, "--config") == 0) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000958 argv++;
959 if (argv < stop) {
960 int index = findConfig(*argv);
961 if (index >= 0) {
962 *configs.append() = index;
963 userConfig = true;
964 } else {
965 SkString str;
966 str.printf("unrecognized config %s\n", *argv);
967 SkDebugf(str.c_str());
968 usage(commandName);
969 return -1;
970 }
971 } else {
reed@google.come5f48b92012-06-22 15:27:39 +0000972 SkDebugf("missing arg for --config\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000973 usage(commandName);
974 return -1;
975 }
976 } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
977 usage(commandName);
978 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000979 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000980 usage(commandName);
981 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000982 }
983 }
984 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000985 usage(commandName);
986 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000987 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000988
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000989 if (!userConfig) {
990 // if no config is specified by user, we add them all.
991 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
992 *configs.append() = i;
993 }
994 }
995
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000996 GM::SetResourcePath(resourcePath);
997
reed@android.com00f883e2010-12-14 17:46:14 +0000998 if (readPath) {
999 fprintf(stderr, "reading from %s\n", readPath);
chudy@google.comf32f6e82012-07-12 15:42:37 +00001000 }
epoger@google.com9284ccd2012-04-18 13:36:54 +00001001 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +00001002 fprintf(stderr, "writing to %s\n", writePath);
1003 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001004 if (writePicturePath) {
1005 fprintf(stderr, "writing pictures to %s\n", writePicturePath);
1006 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001007 if (resourcePath) {
1008 fprintf(stderr, "reading resources from %s\n", resourcePath);
1009 }
1010
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001011 // Accumulate success of all tests.
1012 int testsRun = 0;
1013 int testsPassed = 0;
1014 int testsFailed = 0;
1015 int testsMissingReferenceImages = 0;
1016
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001017#if SK_SUPPORT_GPU
1018 GrContextFactory* grFactory = new GrContextFactory;
twiz@google.come24a0792012-01-31 18:35:30 +00001019 if (disableTextureCache) {
1020 skiagm::GetGr()->setTextureCacheLimits(0, 0);
1021 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001022#endif
twiz@google.come24a0792012-01-31 18:35:30 +00001023
robertphillips@google.coma2f80082012-08-02 16:22:47 +00001024 SkTArray<SkString> failedTests;
1025
bsalomon@google.com7361f542012-04-19 19:15:35 +00001026 Iter iter;
1027 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00001028 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +00001029 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +00001030 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00001031 SkDELETE(gm);
1032 continue;
1033 }
1034
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001035 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +00001036 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +00001037 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +00001038 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +00001039
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001040 for (int i = 0; i < configs.count(); i++) {
1041 ConfigData config = gRec[configs[i]];
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001042 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +00001043 uint32_t gmFlags = gm->getFlags();
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001044 if ((kPDF_Backend == config.fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +00001045 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
1046 {
reed@google.comab973972011-09-19 19:01:38 +00001047 continue;
1048 }
1049
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001050 // Now we know that we want to run this test and record its
1051 // success or failure.
1052 ErrorBitfield testErrors = ERROR_NONE;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001053 GrRenderTarget* renderTarget = NULL;
1054#if SK_SUPPORT_GPU
1055 SkAutoTUnref<GrRenderTarget> rt;
1056 AutoResetGr autogr;
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001057 if ((ERROR_NONE == testErrors) &&
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001058 kGPU_Backend == config.fBackend) {
1059 GrContext* gr = grFactory->get(config.fGLContextType);
1060 bool grSuccess = false;
1061 if (gr) {
1062 // create a render target to back the device
1063 GrTextureDesc desc;
1064 desc.fConfig = kSkia8888_PM_GrPixelConfig;
1065 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1066 desc.fWidth = gm->getISize().width();
1067 desc.fHeight = gm->getISize().height();
1068 desc.fSampleCnt = config.fSampleCnt;
1069 GrTexture* tex = gr->createUncachedTexture(desc, NULL, 0);
1070 if (tex) {
1071 rt.reset(tex->asRenderTarget());
1072 rt.get()->ref();
1073 tex->unref();
1074 autogr.set(gr);
1075 renderTarget = rt.get();
1076 grSuccess = NULL != renderTarget;
1077 }
1078 }
1079 if (!grSuccess) {
1080 testErrors |= ERROR_NO_GPU_CONTEXT;
1081 }
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001082 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001083#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +00001084
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001085 if (ERROR_NONE == testErrors) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001086 testErrors |= test_drawing(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001087 writePath, readPath, diffPath,
bsalomon@google.com7361f542012-04-19 19:15:35 +00001088 GetGr(),
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001089 renderTarget, &forwardRenderedBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001090 }
1091
junov@google.com4370aed2012-01-18 16:21:08 +00001092 if (doDeferred && !testErrors &&
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001093 (kGPU_Backend == config.fBackend ||
1094 kRaster_Backend == config.fBackend)) {
1095 testErrors |= test_deferred_drawing(gm, config,
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001096 forwardRenderedBitmap,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001097 diffPath, GetGr(), renderTarget);
junov@google.com4370aed2012-01-18 16:21:08 +00001098 }
1099
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001100 if ((ERROR_NONE == testErrors) && doReplay &&
1101 !(gmFlags & GM::kSkipPicture_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001102 testErrors |= test_picture_playback(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001103 forwardRenderedBitmap,
1104 readPath, diffPath);
1105 }
1106
scroggo@google.com5af9b202012-06-04 17:17:36 +00001107 if ((ERROR_NONE == testErrors) && doPipe &&
1108 !(gmFlags & GM::kSkipPipe_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001109 testErrors |= test_pipe_playback(gm, config,
scroggo@google.com5af9b202012-06-04 17:17:36 +00001110 forwardRenderedBitmap,
1111 readPath, diffPath);
1112 }
1113
scroggo@google.com72c96722012-06-06 21:07:10 +00001114 if ((ERROR_NONE == testErrors) && doTiledPipe &&
scroggo@google.com63258862012-08-15 16:32:19 +00001115 !SkToBool(gmFlags & (GM::kSkipPipe_Flag | GM::kSkipTiled_Flag))) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001116 testErrors |= test_tiled_pipe_playback(gm, config,
scroggo@google.com72c96722012-06-06 21:07:10 +00001117 forwardRenderedBitmap,
1118 readPath, diffPath);
1119 }
1120
djsollen@google.coma2ca41e2012-03-23 19:00:34 +00001121 if ((ERROR_NONE == testErrors) && doSerialize &&
1122 !(gmFlags & GM::kSkipPicture_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001123 testErrors |= test_picture_serialization(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001124 forwardRenderedBitmap,
1125 readPath, diffPath);
1126 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00001127
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001128 if (!(gmFlags & GM::kSkipPicture_Flag) && writePicturePath) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001129 write_picture_serialization(gm, config, writePicturePath);
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001130 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001131
1132 // Update overall results.
1133 // We only tabulate the particular error types that we currently
1134 // care about (e.g., missing reference images). Later on, if we
1135 // want to also tabulate pixel mismatches vs dimension mistmatches
1136 // (or whatever else), we can do so.
1137 testsRun++;
1138 if (ERROR_NONE == testErrors) {
1139 testsPassed++;
1140 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
1141 testsMissingReferenceImages++;
1142 } else {
1143 testsFailed++;
robertphillips@google.coma2f80082012-08-02 16:22:47 +00001144
1145 failedTests.push_back(make_name(shortName, config.fName));
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001146 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001147 }
reed@android.com00dae862009-06-10 15:38:48 +00001148 SkDELETE(gm);
1149 }
robertphillips@google.coma2f80082012-08-02 16:22:47 +00001150 SkDebugf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
1151 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
1152 for (int i = 0; i < failedTests.count(); ++i) {
1153 SkDebugf("\t\t%s\n", failedTests[i].c_str());
1154 }
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001155
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001156#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001157
robertphillips@google.com59552022012-08-31 13:07:37 +00001158#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001159 for (int i = 0; i < configs.count(); i++) {
1160 ConfigData config = gRec[configs[i]];
1161
1162 if (kGPU_Backend == config.fBackend) {
1163 GrContext* gr = grFactory->get(config.fGLContextType);
1164
1165 SkDebugf("config: %s %x\n", config.fName, gr);
1166 gr->printCacheStats();
1167 }
1168 }
1169#endif
1170
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001171 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001172#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001173 SkGraphics::Term();
1174
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001175 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001176}