blob: 2ac4e73c04ef5cab7b4d60c7e1bbd4fc569d6e65 [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"
21#include "SamplePipeControllers.h"
reed@google.com07700442010-12-20 19:46:07 +000022
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000023#if SK_SUPPORT_GPU
24#include "GrContextFactory.h"
25#include "GrRenderTarget.h"
26#include "SkGpuDevice.h"
27#include "SkGpuCanvas.h"
28typedef GrContextFactory::GLContextType GLContextType;
29#else
30class GrContext;
31class GrRenderTarget;
32typedef int GLContextType;
33#endif
34
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000035static bool gForceBWtext;
36
reed@google.com8923c6c2011-11-08 14:59:38 +000037extern bool gSkSuppressFontCachePurgeSpew;
38
reed@google.com07700442010-12-20 19:46:07 +000039#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000040 #include "SkPDFDevice.h"
41 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000042#endif
reed@android.com00dae862009-06-10 15:38:48 +000043
epoger@google.come3cc2eb2012-01-18 20:11:13 +000044// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
45// stop writing out XPS-format image baselines in gm.
46#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000047#ifdef SK_SUPPORT_XPS
48 #include "SkXPSDevice.h"
49#endif
50
reed@google.com46cce912011-06-29 12:54:46 +000051#ifdef SK_BUILD_FOR_MAC
52 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000053 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000054#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000055 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000056#endif
57
epoger@google.comc7cf2b32011-12-28 19:31:01 +000058typedef int ErrorBitfield;
59const static ErrorBitfield ERROR_NONE = 0x00;
60const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
61const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
62const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
63const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
64const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
65
reed@google.come8fcb502012-05-17 15:28:20 +000066// If true, emit a messange when we can't find a reference image to compare
67static bool gNotifyMissingReadReference;
68
reed@android.com00dae862009-06-10 15:38:48 +000069using namespace skiagm;
70
reed@android.com00dae862009-06-10 15:38:48 +000071class Iter {
72public:
73 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000074 this->reset();
75 }
76
77 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000078 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000079 }
reed@google.comd4dfd102011-01-18 21:05:42 +000080
reed@android.comdd0ac282009-06-20 02:38:16 +000081 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000082 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000083 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000084 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000085 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000086 }
87 return NULL;
88 }
reed@google.comd4dfd102011-01-18 21:05:42 +000089
reed@android.com00dae862009-06-10 15:38:48 +000090 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000091 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000092 int count = 0;
93 while (reg) {
94 count += 1;
95 reg = reg->next();
96 }
97 return count;
98 }
reed@google.comd4dfd102011-01-18 21:05:42 +000099
reed@android.com00dae862009-06-10 15:38:48 +0000100private:
101 const GMRegistry* fReg;
102};
103
reed@android.com8015dd82009-06-21 00:49:18 +0000104static SkString make_name(const char shortName[], const char configName[]) {
105 SkString name(shortName);
106 name.appendf("_%s", configName);
107 return name;
108}
109
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000110static SkString make_filename(const char path[],
111 const char pathSuffix[],
112 const SkString& name,
113 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000114 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000115 if (filename.endsWith("/")) {
116 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000117 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000118 filename.append(pathSuffix);
119 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000120 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000121 return filename;
122}
123
reed@android.comb9b9a182009-07-08 02:54:47 +0000124/* since PNG insists on unpremultiplying our alpha, we take no precision chances
125 and force all pixels to be 100% opaque, otherwise on compare we may not get
126 a perfect match.
127 */
128static void force_all_opaque(const SkBitmap& bitmap) {
129 SkAutoLockPixels lock(bitmap);
130 for (int y = 0; y < bitmap.height(); y++) {
131 for (int x = 0; x < bitmap.width(); x++) {
132 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
133 }
134 }
135}
136
137static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
138 SkBitmap copy;
139 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
140 force_all_opaque(copy);
141 return SkImageEncoder::EncodeFile(path.c_str(), copy,
142 SkImageEncoder::kPNG_Type, 100);
143}
144
reed@google.com3d3f0922010-12-20 21:10:29 +0000145static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000146 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
147 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
148 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
149 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000150}
151
152static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000153 SkBitmap* diff) {
154 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000155
156 const int w = target.width();
157 const int h = target.height();
158 for (int y = 0; y < h; y++) {
159 for (int x = 0; x < w; x++) {
160 SkPMColor c0 = *base.getAddr32(x, y);
161 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000162 SkPMColor d = 0;
163 if (c0 != c1) {
164 d = compute_diff_pmcolor(c0, c1);
165 }
166 *diff->getAddr32(x, y) = d;
167 }
168 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000169}
170
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000171static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
172 const SkString& name,
173 const char* renderModeDescriptor,
174 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000175 SkBitmap copy;
176 const SkBitmap* bm = &target;
177 if (target.config() != SkBitmap::kARGB_8888_Config) {
178 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
179 bm = &copy;
180 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000181 SkBitmap baseCopy;
182 const SkBitmap* bp = &base;
183 if (base.config() != SkBitmap::kARGB_8888_Config) {
184 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
185 bp = &baseCopy;
186 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000187
188 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000189 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000190
191 const int w = bm->width();
192 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000193 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000194 SkDebugf(
195"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
196 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000197 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000198 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000199 }
200
201 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000202 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000203
204 for (int y = 0; y < h; y++) {
205 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000206 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000207 SkPMColor c1 = *bm->getAddr32(x, y);
208 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000209 SkDebugf(
210"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
211 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000212
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000213 if (diff) {
214 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
215 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000216 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000217 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000218 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000219 }
220 }
221 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000222
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000223 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000224 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000225}
reed@android.com00dae862009-06-10 15:38:48 +0000226
bungeman@google.comb29c8832011-10-10 13:19:10 +0000227static bool write_document(const SkString& path,
228 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000229 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000230 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000231 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000232}
233
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000234enum Backend {
235 kRaster_Backend,
236 kGPU_Backend,
237 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000238 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000239};
240
bsalomon@google.com7361f542012-04-19 19:15:35 +0000241enum ConfigFlags {
242 kNone_ConfigFlag = 0x0,
243 /* Write GM images if a write path is provided. */
244 kWrite_ConfigFlag = 0x1,
245 /* Read comparison GM images if a read path is provided. */
246 kRead_ConfigFlag = 0x2,
247 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
248};
249
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000250struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000251 SkBitmap::Config fConfig;
252 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000253 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000254 int fSampleCnt; // GPU backend only
255 ConfigFlags fFlags;
256 const char* fName;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000257};
258
259/// Returns true if processing should continue, false to skip the
260/// remainder of this config for this GM.
261//@todo thudson 22 April 2011 - could refactor this to take in
262// a factory to generate the context, always call readPixels()
263// (logically a noop for rasters, if wasted time), and thus collapse the
264// GPU special case and also let this be used for SkPicture testing.
265static void setup_bitmap(const ConfigData& gRec, SkISize& size,
266 SkBitmap* bitmap) {
267 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
268 bitmap->allocPixels();
269 bitmap->eraseColor(0);
270}
271
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000272#include "SkDrawFilter.h"
273class BWTextDrawFilter : public SkDrawFilter {
274public:
275 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
276};
277void BWTextDrawFilter::filter(SkPaint* p, Type t) {
278 if (kText_Type == t) {
279 p->setAntiAlias(false);
280 }
281}
282
283static void installFilter(SkCanvas* canvas) {
284 if (gForceBWtext) {
285 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
286 }
287}
288
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000289static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
reed@google.com778e1632012-06-04 20:00:01 +0000290 SkAutoCanvasRestore acr(canvas, true);
291
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000292 if (!isPDF) {
reed@google.com778e1632012-06-04 20:00:01 +0000293 canvas->concat(gm->getInitialTransform());
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000294 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000295 installFilter(canvas);
296 gm->draw(canvas);
297 canvas->setDrawFilter(NULL);
298}
299
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000300static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
301 GrContext* context,
302 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000303 SkBitmap* bitmap,
304 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000305 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000306 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000307
308 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000309 SkCanvas* canvas;
310 if (deferred) {
311 canvas = new SkDeferredCanvas;
312 canvas->setDevice(new SkDevice(*bitmap))->unref();
313 } else {
314 canvas = new SkCanvas(*bitmap);
315 }
316 SkAutoUnref canvasUnref(canvas);
317 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000318 canvas->flush();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000319 }
320#if SK_SUPPORT_GPU
321 else { // GPU
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000322 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000323 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000324 }
junov@google.com4370aed2012-01-18 16:21:08 +0000325 SkCanvas* gc;
326 if (deferred) {
327 gc = new SkDeferredCanvas;
328 } else {
329 gc = new SkGpuCanvas(context, rt);
330 }
331 SkAutoUnref gcUnref(gc);
332 gc->setDevice(new SkGpuDevice(context, rt))->unref();
333 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000334 // the device is as large as the current rendertarget, so we explicitly
335 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000336 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000337 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
338 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000339 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000340 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000341#endif
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000342 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000343}
344
345static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
346 SkPicture* pict, SkBitmap* bitmap) {
347 SkISize size = gm->getISize();
348 setup_bitmap(gRec, size, bitmap);
349 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000350 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000351 canvas.drawPicture(*pict);
352}
353
354static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
355#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000356 SkMatrix initialTransform = gm->getInitialTransform();
357 SkISize pageSize = gm->getISize();
358 SkPDFDevice* dev = NULL;
359 if (initialTransform.isIdentity()) {
360 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
361 } else {
362 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
363 SkIntToScalar(pageSize.height()));
364 initialTransform.mapRect(&content);
365 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
366 SkIntToScalar(pageSize.height()));
367 SkISize contentSize =
368 SkISize::Make(SkScalarRoundToInt(content.width()),
369 SkScalarRoundToInt(content.height()));
370 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
371 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000372 SkAutoUnref aur(dev);
373
374 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000375 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000376
377 SkPDFDocument doc;
378 doc.appendPage(dev);
379 doc.emitPDF(&pdf);
380#endif
381}
382
bungeman@google.comb29c8832011-10-10 13:19:10 +0000383static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
384#ifdef SK_SUPPORT_XPS
385 SkISize size = gm->getISize();
chudy@google.comf32f6e82012-07-12 15:42:37 +0000386
bungeman@google.comb29c8832011-10-10 13:19:10 +0000387 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
388 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000389 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
390 static const SkScalar upm = 72 * inchesPerMeter;
391 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
392 static const SkScalar ppm = 200 * inchesPerMeter;
393 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000394
395 SkXPSDevice* dev = new SkXPSDevice();
396 SkAutoUnref aur(dev);
397
398 SkCanvas c(dev);
399 dev->beginPortfolio(&xps);
400 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000401 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000402 dev->endSheet();
403 dev->endPortfolio();
404
405#endif
406}
407
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000408static ErrorBitfield write_reference_image(const ConfigData& gRec,
409 const char writePath [],
410 const char renderModeDescriptor [],
411 const SkString& name,
412 SkBitmap& bitmap,
413 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000414 SkString path;
415 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000416 if (gRec.fBackend == kRaster_Backend ||
417 gRec.fBackend == kGPU_Backend ||
418 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
chudy@google.comf32f6e82012-07-12 15:42:37 +0000419
tomhudson@google.comea325432011-06-09 20:30:03 +0000420 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000421 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000422 }
423 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000424 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000425 success = write_document(path, *document);
426 }
427 if (kXPS_Backend == gRec.fBackend) {
428 path = make_filename(writePath, renderModeDescriptor, name, "xps");
429 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000430 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000431 if (success) {
432 return ERROR_NONE;
433 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000434 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000435 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000436 }
437}
438
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000439static ErrorBitfield compare_to_reference_image(const SkString& name,
440 SkBitmap &bitmap,
441 const SkBitmap& comparisonBitmap,
442 const char diffPath [],
443 const char renderModeDescriptor []) {
444 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000445 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000446 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
447 diffPath ? &diffBitmap : NULL);
reed@google.com8e529b72012-04-09 20:20:10 +0000448 if ((ERROR_NONE != errors) && diffPath) {
449 // write out the generated image
450 SkString genName = make_filename(diffPath, "", name, "png");
451 if (!write_bitmap(genName, bitmap)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000452 errors |= ERROR_WRITING_REFERENCE_IMAGE;
453 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000454 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000455 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000456}
457
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000458static ErrorBitfield compare_to_reference_image(const char readPath [],
459 const SkString& name,
460 SkBitmap &bitmap,
461 const char diffPath [],
462 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000463 SkString path = make_filename(readPath, "", name, "png");
464 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000465 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
466 SkBitmap::kARGB_8888_Config,
467 SkImageDecoder::kDecodePixels_Mode, NULL)) {
468 return compare_to_reference_image(name, bitmap,
469 orig, diffPath,
470 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000471 } else {
reed@google.come8fcb502012-05-17 15:28:20 +0000472 if (gNotifyMissingReadReference) {
473 fprintf(stderr, "FAILED to read %s\n", path.c_str());
474 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000475 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000476 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000477}
478
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000479static ErrorBitfield handle_test_results(GM* gm,
480 const ConfigData& gRec,
481 const char writePath [],
482 const char readPath [],
483 const char diffPath [],
484 const char renderModeDescriptor [],
485 SkBitmap& bitmap,
486 SkDynamicMemoryWStream* pdf,
487 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000488 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000489 ErrorBitfield retval = ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000490
bsalomon@google.com7361f542012-04-19 19:15:35 +0000491 if (readPath && (gRec.fFlags & kRead_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000492 retval |= compare_to_reference_image(readPath, name, bitmap,
493 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000494 }
bsalomon@google.com7361f542012-04-19 19:15:35 +0000495 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000496 retval |= write_reference_image(gRec, writePath, renderModeDescriptor,
497 name, bitmap, pdf);
498 }
499 if (comparisonBitmap) {
500 retval |= compare_to_reference_image(name, bitmap,
501 *comparisonBitmap, diffPath,
502 renderModeDescriptor);
503 }
504 return retval;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000505}
506
507static SkPicture* generate_new_picture(GM* gm) {
508 // Pictures are refcounted so must be on heap
509 SkPicture* pict = new SkPicture;
510 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000511 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000512 pict->endRecording();
513
514 return pict;
515}
516
517static SkPicture* stream_to_new_picture(const SkPicture& src) {
518
519 // To do in-memory commiunications with a stream, we need to:
520 // * create a dynamic memory stream
521 // * copy it into a buffer
522 // * create a read stream from it
523 // ?!?!
524
525 SkDynamicMemoryWStream storage;
526 src.serialize(&storage);
527
528 int streamSize = storage.getOffset();
529 SkAutoMalloc dstStorage(streamSize);
530 void* dst = dstStorage.get();
531 //char* dst = new char [streamSize];
532 //@todo thudson 22 April 2011 when can we safely delete [] dst?
533 storage.copyTo(dst);
534 SkMemoryStream pictReadback(dst, streamSize);
535 SkPicture* retval = new SkPicture (&pictReadback);
536 return retval;
537}
538
539// Test: draw into a bitmap or pdf.
540// Depending on flags, possibly compare to an expected image
541// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000542static ErrorBitfield test_drawing(GM* gm,
543 const ConfigData& gRec,
544 const char writePath [],
545 const char readPath [],
546 const char diffPath [],
547 GrContext* context,
548 GrRenderTarget* rt,
549 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000550 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000551
552 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000553 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000554 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000555 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
556 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000557 if (ERROR_NONE != errors) {
558 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000559 }
reed@google.com46cce912011-06-29 12:54:46 +0000560 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000561 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000562#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000563 SkAutoDataUnref data(document.copyToData());
robertphillips@google.com5c0b3132012-07-10 17:50:00 +0000564 SkMemoryStream stream(data->data(), data->size());
reed@google.com46cce912011-06-29 12:54:46 +0000565 SkPDFDocumentToBitmap(&stream, bitmap);
566#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000567 } else if (gRec.fBackend == kXPS_Backend) {
568 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000569 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000570 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000571 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000572}
573
junov@google.com4370aed2012-01-18 16:21:08 +0000574static ErrorBitfield test_deferred_drawing(GM* gm,
575 const ConfigData& gRec,
576 const SkBitmap& comparisonBitmap,
577 const char diffPath [],
578 GrContext* context,
579 GrRenderTarget* rt) {
580 SkDynamicMemoryWStream document;
581
582 if (gRec.fBackend == kRaster_Backend ||
583 gRec.fBackend == kGPU_Backend) {
584 SkBitmap bitmap;
585 // Early exit if we can't generate the image, but this is
586 // expected in some cases, so don't report a test failure.
587 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
588 return ERROR_NONE;
589 }
590 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
591 "-deferred", bitmap, NULL, &comparisonBitmap);
592 }
593 return ERROR_NONE;
594}
595
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000596static ErrorBitfield test_picture_playback(GM* gm,
597 const ConfigData& gRec,
598 const SkBitmap& comparisonBitmap,
599 const char readPath [],
600 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000601 SkPicture* pict = generate_new_picture(gm);
602 SkAutoUnref aur(pict);
603
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000604 if (kRaster_Backend == gRec.fBackend) {
605 SkBitmap bitmap;
606 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000607 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
608 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000609 } else {
610 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000611 }
612}
613
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000614static ErrorBitfield test_picture_serialization(GM* gm,
615 const ConfigData& gRec,
616 const SkBitmap& comparisonBitmap,
617 const char readPath [],
618 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000619 SkPicture* pict = generate_new_picture(gm);
620 SkAutoUnref aurp(pict);
621 SkPicture* repict = stream_to_new_picture(*pict);
622 SkAutoUnref aurr(repict);
623
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000624 if (kRaster_Backend == gRec.fBackend) {
625 SkBitmap bitmap;
626 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000627 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
628 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000629 } else {
630 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000631 }
632}
633
scroggo@google.com565254b2012-06-28 15:41:32 +0000634struct PipeFlagComboData {
635 const char* name;
636 uint32_t flags;
637};
638
639static PipeFlagComboData gPipeWritingFlagCombos[] = {
640 { "", 0 },
641 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000642 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000643 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000644};
645
scroggo@google.com5af9b202012-06-04 17:17:36 +0000646static ErrorBitfield test_pipe_playback(GM* gm,
647 const ConfigData& gRec,
648 const SkBitmap& comparisonBitmap,
649 const char readPath [],
650 const char diffPath []) {
651 if (kRaster_Backend != gRec.fBackend) {
652 return ERROR_NONE;
653 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000654 ErrorBitfield errors = ERROR_NONE;
655 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
656 SkBitmap bitmap;
657 SkISize size = gm->getISize();
658 setup_bitmap(gRec, size, &bitmap);
659 SkCanvas canvas(bitmap);
660 PipeController pipeController(&canvas);
661 SkGPipeWriter writer;
662 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
663 gPipeWritingFlagCombos[i].flags);
664 invokeGM(gm, pipeCanvas);
665 writer.endRecording();
666 SkString string("-pipe");
667 string.append(gPipeWritingFlagCombos[i].name);
668 errors |= handle_test_results(gm, gRec, NULL, NULL, diffPath,
669 string.c_str(), bitmap, NULL, &comparisonBitmap);
670 if (errors != ERROR_NONE) {
671 break;
672 }
673 }
674 return errors;
scroggo@google.com5af9b202012-06-04 17:17:36 +0000675}
676
scroggo@google.com72c96722012-06-06 21:07:10 +0000677static ErrorBitfield test_tiled_pipe_playback(GM* gm,
678 const ConfigData& gRec,
679 const SkBitmap& comparisonBitmap,
680 const char readPath [],
681 const char diffPath []) {
682 if (kRaster_Backend != gRec.fBackend) {
683 return ERROR_NONE;
684 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000685 ErrorBitfield errors = ERROR_NONE;
686 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
687 SkBitmap bitmap;
688 SkISize size = gm->getISize();
689 setup_bitmap(gRec, size, &bitmap);
690 SkCanvas canvas(bitmap);
691 TiledPipeController pipeController(bitmap);
692 SkGPipeWriter writer;
693 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
694 gPipeWritingFlagCombos[i].flags);
695 invokeGM(gm, pipeCanvas);
696 writer.endRecording();
scroggo@google.comd3ba5cc2012-07-09 16:05:53 +0000697 SkString string("-tiled pipe");
scroggo@google.com565254b2012-06-28 15:41:32 +0000698 string.append(gPipeWritingFlagCombos[i].name);
699 errors |= handle_test_results(gm, gRec, NULL, NULL, diffPath,
700 string.c_str(), bitmap, NULL, &comparisonBitmap);
701 if (errors != ERROR_NONE) {
702 break;
703 }
704 }
705 return errors;
scroggo@google.com72c96722012-06-06 21:07:10 +0000706}
707
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000708static void write_picture_serialization(GM* gm, const ConfigData& rec,
709 const char writePicturePath[]) {
710 // only do this once, so we pick raster
711 if (kRaster_Backend == rec.fBackend &&
712 SkBitmap::kARGB_8888_Config == rec.fConfig) {
713
714 SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
chudy@google.comf32f6e82012-07-12 15:42:37 +0000715
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000716 const char* pictureSuffix = "skp";
717 SkString path = make_filename(writePicturePath, "",
718 SkString(gm->shortName()), pictureSuffix);
chudy@google.comf32f6e82012-07-12 15:42:37 +0000719
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000720 SkFILEWStream stream(path.c_str());
721 pict->serialize(&stream);
722 }
723}
724
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000725#if SK_SUPPORT_GPU
726static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
727#else
728static const GLContextType kDontCare_GLContextType = 0;
729#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +0000730
731// If the platform does not support writing PNGs of PDFs then there will be no
732// comparison images to read. However, we can always write the .pdf files
733static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
734 kWrite_ConfigFlag;
735
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000736static const ConfigData gRec[] = {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000737 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888" },
738 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444" },
739 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000740#if defined(SK_SCALAR_IS_FLOAT) && SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +0000741 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu" },
742 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16" },
743 /* The debug context does not generate images */
744 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "debug" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000745#if SK_ANGLE
bsalomon@google.com7361f542012-04-19 19:15:35 +0000746 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle" },
747 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000748#endif // SK_ANGLE
749#ifdef SK_MESA
bsalomon@google.com7361f542012-04-19 19:15:35 +0000750 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000751#endif // SK_MESA
752#endif //
bungeman@google.comb29c8832011-10-10 13:19:10 +0000753#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000754 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
755 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000756#endif // defined(SK_SCALAR_IS_FLOAT) && SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +0000757#ifdef SK_SUPPORT_PDF
758 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000759#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +0000760};
761
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000762static void usage(const char * argv0) {
763 SkDebugf("%s\n", argv0);
764 SkDebugf(" [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n");
chudy@google.comf32f6e82012-07-12 15:42:37 +0000765 SkDebugf(" [-wp writePicturePath]\n");
reed@google.come5f48b92012-06-22 15:27:39 +0000766 SkDebugf(" [--config ");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000767 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
768 if (i > 0) {
769 SkDebugf("|");
770 }
771 SkDebugf(gRec[i].fName);
772 }
773 SkDebugf(" ]\n");
scroggo@google.com565254b2012-06-28 15:41:32 +0000774 SkDebugf(" [--noreplay] [--nopipe] [--serialize] [--forceBWtext] [--nopdf] \n"
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000775 " [--tiledPipe] \n"
776 " [--nodeferred] [--match substring] [--notexturecache]\n"
777 " [-h|--help]\n"
778 );
779 SkDebugf(" writePath: directory to write rendered images in.\n");
chudy@google.comf32f6e82012-07-12 15:42:37 +0000780 SkDebugf(" writePicturePath: directory to write images to in .skp format.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000781 SkDebugf(
782 " readPath: directory to read reference images from;\n"
783 " reports if any pixels mismatch between reference and new images\n");
784 SkDebugf(" diffPath: directory to write difference images in.\n");
785 SkDebugf(" resourcePath: directory that stores image resources.\n");
786 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
scroggo@google.com565254b2012-06-28 15:41:32 +0000787 SkDebugf(" --nopipe: Skip SkGPipe replay.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000788 SkDebugf(" --tiledPipe: Exercise tiled SkGPipe replay.\n");
789 SkDebugf(
borenet@google.com14ca1d32012-06-15 13:46:44 +0000790 " --serialize: exercise SkPicture serialization & deserialization.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000791 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
792 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
793 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
794 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
795 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
796 SkDebugf(" -h|--help : Show this help message. \n");
797}
798
799static int findConfig(const char config[]) {
800 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
801 if (!strcmp(config, gRec[i].fName)) {
802 return i;
803 }
804 }
805 return -1;
806}
807
reed@google.comb2a51622011-10-31 16:30:04 +0000808static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
809 if (0 == array.count()) {
810 // no names, so don't skip anything
811 return false;
812 }
813 for (int i = 0; i < array.count(); ++i) {
814 if (strstr(name, array[i])) {
815 // found the name, so don't skip
816 return false;
817 }
818 }
819 return true;
820}
821
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000822namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000823#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +0000824SkAutoTUnref<GrContext> gGrContext;
825/**
826 * Sets the global GrContext, accessible by indivual GMs
827 */
caryclark@google.com13130862012-06-06 12:10:45 +0000828static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000829 SkSafeRef(grContext);
830 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000831}
bsalomon@google.com7361f542012-04-19 19:15:35 +0000832
833/**
834 * Gets the global GrContext, can be called by GM tests.
835 */
caryclark@google.com13130862012-06-06 12:10:45 +0000836GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000837GrContext* GetGr() {
838 return gGrContext.get();
839}
840
841/**
842 * Sets the global GrContext and then resets it to its previous value at
843 * destruction.
844 */
845class AutoResetGr : SkNoncopyable {
846public:
847 AutoResetGr() : fOld(NULL) {}
848 void set(GrContext* context) {
849 SkASSERT(NULL == fOld);
850 fOld = GetGr();
851 SkSafeRef(fOld);
852 SetGr(context);
853 }
854 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
855private:
856 GrContext* fOld;
857};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000858#else
859GrContext* GetGr() { return NULL; }
860#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000861}
862
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000863int main(int argc, char * const argv[]) {
robertphillips@google.comb74af872012-06-27 19:41:42 +0000864
865#ifdef SK_ENABLE_INST_COUNT
866 gPrintInstCount = true;
867#endif
868
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000869 SkGraphics::Init();
reed@google.com8923c6c2011-11-08 14:59:38 +0000870 // we don't need to see this during a run
871 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000872
epoger@google.com7bc13a62012-02-14 14:53:59 +0000873 setSystemPreferences();
874
reed@android.com8015dd82009-06-21 00:49:18 +0000875 const char* writePath = NULL; // if non-null, where we write the originals
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000876 const char* writePicturePath = NULL; // if non-null, where we write serialized pictures
reed@android.com8015dd82009-06-21 00:49:18 +0000877 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000878 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000879 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000880
reed@google.comb2a51622011-10-31 16:30:04 +0000881 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000882
reed@google.comab973972011-09-19 19:01:38 +0000883 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000884 bool doReplay = true;
scroggo@google.com565254b2012-06-28 15:41:32 +0000885 bool doPipe = true;
scroggo@google.com72c96722012-06-06 21:07:10 +0000886 bool doTiledPipe = false;
borenet@google.com14ca1d32012-06-15 13:46:44 +0000887 bool doSerialize = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000888 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000889 bool disableTextureCache = false;
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000890 SkTDArray<size_t> configs;
891 bool userConfig = false;
twiz@google.come24a0792012-01-31 18:35:30 +0000892
reed@google.come8fcb502012-05-17 15:28:20 +0000893 gNotifyMissingReadReference = true;
894
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000895 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000896 char* const* stop = argv + argc;
897 for (++argv; argv < stop; ++argv) {
898 if (strcmp(*argv, "-w") == 0) {
899 argv++;
900 if (argv < stop && **argv) {
901 writePath = *argv;
902 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000903 } else if (strcmp(*argv, "-wp") == 0) {
904 argv++;
905 if (argv < stop && **argv) {
906 writePicturePath = *argv;
907 }
reed@android.com8015dd82009-06-21 00:49:18 +0000908 } else if (strcmp(*argv, "-r") == 0) {
909 argv++;
910 if (argv < stop && **argv) {
911 readPath = *argv;
912 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000913 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000914 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000915 if (argv < stop && **argv) {
916 diffPath = *argv;
917 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000918 } else if (strcmp(*argv, "-i") == 0) {
919 argv++;
920 if (argv < stop && **argv) {
921 resourcePath = *argv;
922 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000923 } else if (strcmp(*argv, "--forceBWtext") == 0) {
924 gForceBWtext = true;
scroggo@google.com565254b2012-06-28 15:41:32 +0000925 } else if (strcmp(*argv, "--nopipe") == 0) {
926 doPipe = false;
scroggo@google.com72c96722012-06-06 21:07:10 +0000927 } else if (strcmp(*argv, "--tiledPipe") == 0) {
928 doTiledPipe = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000929 } else if (strcmp(*argv, "--noreplay") == 0) {
930 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000931 } else if (strcmp(*argv, "--nopdf") == 0) {
932 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000933 } else if (strcmp(*argv, "--nodeferred") == 0) {
934 doDeferred = false;
reed@google.come8fcb502012-05-17 15:28:20 +0000935 } else if (strcmp(*argv, "--disable-missing-warning") == 0) {
936 gNotifyMissingReadReference = false;
937 } else if (strcmp(*argv, "--enable-missing-warning") == 0) {
938 gNotifyMissingReadReference = true;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000939 } else if (strcmp(*argv, "--serialize") == 0) {
borenet@google.com14ca1d32012-06-15 13:46:44 +0000940 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000941 } else if (strcmp(*argv, "--match") == 0) {
942 ++argv;
943 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000944 // just record the ptr, no need for a deep copy
945 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000946 }
twiz@google.come24a0792012-01-31 18:35:30 +0000947 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000948 disableTextureCache = true;
reed@google.come5f48b92012-06-22 15:27:39 +0000949 } else if (strcmp(*argv, "--config") == 0) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000950 argv++;
951 if (argv < stop) {
952 int index = findConfig(*argv);
953 if (index >= 0) {
954 *configs.append() = index;
955 userConfig = true;
956 } else {
957 SkString str;
958 str.printf("unrecognized config %s\n", *argv);
959 SkDebugf(str.c_str());
960 usage(commandName);
961 return -1;
962 }
963 } else {
reed@google.come5f48b92012-06-22 15:27:39 +0000964 SkDebugf("missing arg for --config\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000965 usage(commandName);
966 return -1;
967 }
968 } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
969 usage(commandName);
970 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000971 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000972 usage(commandName);
973 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000974 }
975 }
976 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000977 usage(commandName);
978 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000979 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000980
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000981 if (!userConfig) {
982 // if no config is specified by user, we add them all.
983 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
984 *configs.append() = i;
985 }
986 }
987
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000988 GM::SetResourcePath(resourcePath);
989
reed@android.com00f883e2010-12-14 17:46:14 +0000990 if (readPath) {
991 fprintf(stderr, "reading from %s\n", readPath);
chudy@google.comf32f6e82012-07-12 15:42:37 +0000992 }
epoger@google.com9284ccd2012-04-18 13:36:54 +0000993 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +0000994 fprintf(stderr, "writing to %s\n", writePath);
995 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000996 if (writePicturePath) {
997 fprintf(stderr, "writing pictures to %s\n", writePicturePath);
998 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000999 if (resourcePath) {
1000 fprintf(stderr, "reading resources from %s\n", resourcePath);
1001 }
1002
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001003 // Accumulate success of all tests.
1004 int testsRun = 0;
1005 int testsPassed = 0;
1006 int testsFailed = 0;
1007 int testsMissingReferenceImages = 0;
1008
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001009#if SK_SUPPORT_GPU
1010 GrContextFactory* grFactory = new GrContextFactory;
twiz@google.come24a0792012-01-31 18:35:30 +00001011 if (disableTextureCache) {
1012 skiagm::GetGr()->setTextureCacheLimits(0, 0);
1013 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001014#endif
twiz@google.come24a0792012-01-31 18:35:30 +00001015
robertphillips@google.coma2f80082012-08-02 16:22:47 +00001016 SkTArray<SkString> failedTests;
1017
bsalomon@google.com7361f542012-04-19 19:15:35 +00001018 Iter iter;
1019 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00001020 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +00001021 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +00001022 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00001023 SkDELETE(gm);
1024 continue;
1025 }
1026
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001027 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +00001028 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +00001029 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +00001030 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +00001031
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001032 for (int i = 0; i < configs.count(); i++) {
1033 ConfigData config = gRec[configs[i]];
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001034 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +00001035 uint32_t gmFlags = gm->getFlags();
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001036 if ((kPDF_Backend == config.fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +00001037 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
1038 {
reed@google.comab973972011-09-19 19:01:38 +00001039 continue;
1040 }
1041
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001042 // Now we know that we want to run this test and record its
1043 // success or failure.
1044 ErrorBitfield testErrors = ERROR_NONE;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001045 GrRenderTarget* renderTarget = NULL;
1046#if SK_SUPPORT_GPU
1047 SkAutoTUnref<GrRenderTarget> rt;
1048 AutoResetGr autogr;
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001049 if ((ERROR_NONE == testErrors) &&
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001050 kGPU_Backend == config.fBackend) {
1051 GrContext* gr = grFactory->get(config.fGLContextType);
1052 bool grSuccess = false;
1053 if (gr) {
1054 // create a render target to back the device
1055 GrTextureDesc desc;
1056 desc.fConfig = kSkia8888_PM_GrPixelConfig;
1057 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1058 desc.fWidth = gm->getISize().width();
1059 desc.fHeight = gm->getISize().height();
1060 desc.fSampleCnt = config.fSampleCnt;
1061 GrTexture* tex = gr->createUncachedTexture(desc, NULL, 0);
1062 if (tex) {
1063 rt.reset(tex->asRenderTarget());
1064 rt.get()->ref();
1065 tex->unref();
1066 autogr.set(gr);
1067 renderTarget = rt.get();
1068 grSuccess = NULL != renderTarget;
1069 }
1070 }
1071 if (!grSuccess) {
1072 testErrors |= ERROR_NO_GPU_CONTEXT;
1073 }
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001074 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001075#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +00001076
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001077 if (ERROR_NONE == testErrors) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001078 testErrors |= test_drawing(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001079 writePath, readPath, diffPath,
bsalomon@google.com7361f542012-04-19 19:15:35 +00001080 GetGr(),
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001081 renderTarget, &forwardRenderedBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001082 }
1083
junov@google.com4370aed2012-01-18 16:21:08 +00001084 if (doDeferred && !testErrors &&
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001085 (kGPU_Backend == config.fBackend ||
1086 kRaster_Backend == config.fBackend)) {
1087 testErrors |= test_deferred_drawing(gm, config,
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001088 forwardRenderedBitmap,
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001089 diffPath, GetGr(), renderTarget);
junov@google.com4370aed2012-01-18 16:21:08 +00001090 }
1091
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001092 if ((ERROR_NONE == testErrors) && doReplay &&
1093 !(gmFlags & GM::kSkipPicture_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001094 testErrors |= test_picture_playback(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001095 forwardRenderedBitmap,
1096 readPath, diffPath);
1097 }
1098
scroggo@google.com5af9b202012-06-04 17:17:36 +00001099 if ((ERROR_NONE == testErrors) && doPipe &&
1100 !(gmFlags & GM::kSkipPipe_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001101 testErrors |= test_pipe_playback(gm, config,
scroggo@google.com5af9b202012-06-04 17:17:36 +00001102 forwardRenderedBitmap,
1103 readPath, diffPath);
1104 }
1105
scroggo@google.com72c96722012-06-06 21:07:10 +00001106 if ((ERROR_NONE == testErrors) && doTiledPipe &&
1107 !(gmFlags & GM::kSkipPipe_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001108 testErrors |= test_tiled_pipe_playback(gm, config,
scroggo@google.com72c96722012-06-06 21:07:10 +00001109 forwardRenderedBitmap,
1110 readPath, diffPath);
1111 }
1112
djsollen@google.coma2ca41e2012-03-23 19:00:34 +00001113 if ((ERROR_NONE == testErrors) && doSerialize &&
1114 !(gmFlags & GM::kSkipPicture_Flag)) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001115 testErrors |= test_picture_serialization(gm, config,
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001116 forwardRenderedBitmap,
1117 readPath, diffPath);
1118 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00001119
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001120 if (!(gmFlags & GM::kSkipPicture_Flag) && writePicturePath) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001121 write_picture_serialization(gm, config, writePicturePath);
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001122 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001123
1124 // Update overall results.
1125 // We only tabulate the particular error types that we currently
1126 // care about (e.g., missing reference images). Later on, if we
1127 // want to also tabulate pixel mismatches vs dimension mistmatches
1128 // (or whatever else), we can do so.
1129 testsRun++;
1130 if (ERROR_NONE == testErrors) {
1131 testsPassed++;
1132 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
1133 testsMissingReferenceImages++;
1134 } else {
1135 testsFailed++;
robertphillips@google.coma2f80082012-08-02 16:22:47 +00001136
1137 failedTests.push_back(make_name(shortName, config.fName));
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001138 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001139 }
reed@android.com00dae862009-06-10 15:38:48 +00001140 SkDELETE(gm);
1141 }
robertphillips@google.coma2f80082012-08-02 16:22:47 +00001142 SkDebugf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
1143 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
1144 for (int i = 0; i < failedTests.count(); ++i) {
1145 SkDebugf("\t\t%s\n", failedTests[i].c_str());
1146 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001147#if SK_SUPPORT_GPU
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001148 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001149#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001150 SkGraphics::Term();
1151
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001152 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001153}