blob: bcceb727c2741ecf0bfe7d3c02cb2c1f4ed6137a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
bsalomon@google.com971d0c82011-08-19 17:22:05 +00007
bungeman@google.comb29c8832011-10-10 13:19:10 +00008#include "gm.h"
epoger@google.com7bc13a62012-02-14 14:53:59 +00009#include "system_preferences.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000010#include "GrContext.h"
11#include "GrRenderTarget.h"
12
reed@android.comb9b9a182009-07-08 02:54:47 +000013#include "SkColorPriv.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000014#include "SkData.h"
junov@google.com4370aed2012-01-18 16:21:08 +000015#include "SkDeferredCanvas.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000016#include "SkDevice.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000017#include "SkGpuCanvas.h"
18#include "SkGpuDevice.h"
reed@android.com8015dd82009-06-21 00:49:18 +000019#include "SkGraphics.h"
20#include "SkImageDecoder.h"
21#include "SkImageEncoder.h"
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000022#include "gl/SkNativeGLContext.h"
23#include "gl/SkMesaGLContext.h"
robertphillips@google.comf6f123d2012-03-21 17:57:55 +000024#include "gl/SkDebugGLContext.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000025#include "SkPicture.h"
reed@google.com07700442010-12-20 19:46:07 +000026#include "SkStream.h"
27#include "SkRefCnt.h"
28
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000029static bool gForceBWtext;
30
reed@google.com8923c6c2011-11-08 14:59:38 +000031extern bool gSkSuppressFontCachePurgeSpew;
32
reed@google.com07700442010-12-20 19:46:07 +000033#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000034 #include "SkPDFDevice.h"
35 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000036#endif
reed@android.com00dae862009-06-10 15:38:48 +000037
epoger@google.come3cc2eb2012-01-18 20:11:13 +000038// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
39// stop writing out XPS-format image baselines in gm.
40#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000041#ifdef SK_SUPPORT_XPS
42 #include "SkXPSDevice.h"
43#endif
44
reed@google.com46cce912011-06-29 12:54:46 +000045#ifdef SK_BUILD_FOR_MAC
46 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000047 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000048#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000049 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000050#endif
51
epoger@google.comc7cf2b32011-12-28 19:31:01 +000052typedef int ErrorBitfield;
53const static ErrorBitfield ERROR_NONE = 0x00;
54const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
55const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
56const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
57const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
58const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
59
reed@android.com00dae862009-06-10 15:38:48 +000060using namespace skiagm;
61
reed@android.com00dae862009-06-10 15:38:48 +000062class Iter {
63public:
64 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000065 this->reset();
66 }
67
68 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000069 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000070 }
reed@google.comd4dfd102011-01-18 21:05:42 +000071
reed@android.comdd0ac282009-06-20 02:38:16 +000072 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000073 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000074 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000075 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000076 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000077 }
78 return NULL;
79 }
reed@google.comd4dfd102011-01-18 21:05:42 +000080
reed@android.com00dae862009-06-10 15:38:48 +000081 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000082 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000083 int count = 0;
84 while (reg) {
85 count += 1;
86 reg = reg->next();
87 }
88 return count;
89 }
reed@google.comd4dfd102011-01-18 21:05:42 +000090
reed@android.com00dae862009-06-10 15:38:48 +000091private:
92 const GMRegistry* fReg;
93};
94
reed@android.com8015dd82009-06-21 00:49:18 +000095static SkString make_name(const char shortName[], const char configName[]) {
96 SkString name(shortName);
97 name.appendf("_%s", configName);
98 return name;
99}
100
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000101static SkString make_filename(const char path[],
102 const char pathSuffix[],
103 const SkString& name,
104 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000105 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000106 if (filename.endsWith("/")) {
107 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000108 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000109 filename.append(pathSuffix);
110 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000111 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000112 return filename;
113}
114
reed@android.comb9b9a182009-07-08 02:54:47 +0000115/* since PNG insists on unpremultiplying our alpha, we take no precision chances
116 and force all pixels to be 100% opaque, otherwise on compare we may not get
117 a perfect match.
118 */
119static void force_all_opaque(const SkBitmap& bitmap) {
120 SkAutoLockPixels lock(bitmap);
121 for (int y = 0; y < bitmap.height(); y++) {
122 for (int x = 0; x < bitmap.width(); x++) {
123 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
124 }
125 }
126}
127
128static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
129 SkBitmap copy;
130 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
131 force_all_opaque(copy);
132 return SkImageEncoder::EncodeFile(path.c_str(), copy,
133 SkImageEncoder::kPNG_Type, 100);
134}
135
reed@google.com3d3f0922010-12-20 21:10:29 +0000136static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000137 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
138 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
139 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
140 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000141}
142
143static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000144 SkBitmap* diff) {
145 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000146
147 const int w = target.width();
148 const int h = target.height();
149 for (int y = 0; y < h; y++) {
150 for (int x = 0; x < w; x++) {
151 SkPMColor c0 = *base.getAddr32(x, y);
152 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000153 SkPMColor d = 0;
154 if (c0 != c1) {
155 d = compute_diff_pmcolor(c0, c1);
156 }
157 *diff->getAddr32(x, y) = d;
158 }
159 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000160}
161
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000162static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
163 const SkString& name,
164 const char* renderModeDescriptor,
165 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000166 SkBitmap copy;
167 const SkBitmap* bm = &target;
168 if (target.config() != SkBitmap::kARGB_8888_Config) {
169 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
170 bm = &copy;
171 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000172 SkBitmap baseCopy;
173 const SkBitmap* bp = &base;
174 if (base.config() != SkBitmap::kARGB_8888_Config) {
175 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
176 bp = &baseCopy;
177 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000178
179 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000180 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000181
182 const int w = bm->width();
183 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000184 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000185 SkDebugf(
186"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
187 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000188 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000189 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000190 }
191
192 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000193 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000194
195 for (int y = 0; y < h; y++) {
196 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000197 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000198 SkPMColor c1 = *bm->getAddr32(x, y);
199 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000200 SkDebugf(
201"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
202 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000203
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000204 if (diff) {
205 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
206 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000207 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000208 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000209 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000210 }
211 }
212 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000213
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000214 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000215 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000216}
reed@android.com00dae862009-06-10 15:38:48 +0000217
bungeman@google.comb29c8832011-10-10 13:19:10 +0000218static bool write_document(const SkString& path,
219 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000220 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000221 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000222 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000223}
224
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000225enum Backend {
226 kRaster_Backend,
227 kGPU_Backend,
228 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000229 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000230};
231
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000232struct ConfigData {
233 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000234 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000235 const char* fName;
236};
237
238/// Returns true if processing should continue, false to skip the
239/// remainder of this config for this GM.
240//@todo thudson 22 April 2011 - could refactor this to take in
241// a factory to generate the context, always call readPixels()
242// (logically a noop for rasters, if wasted time), and thus collapse the
243// GPU special case and also let this be used for SkPicture testing.
244static void setup_bitmap(const ConfigData& gRec, SkISize& size,
245 SkBitmap* bitmap) {
246 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
247 bitmap->allocPixels();
248 bitmap->eraseColor(0);
249}
250
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000251#include "SkDrawFilter.h"
252class BWTextDrawFilter : public SkDrawFilter {
253public:
254 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
255};
256void BWTextDrawFilter::filter(SkPaint* p, Type t) {
257 if (kText_Type == t) {
258 p->setAntiAlias(false);
259 }
260}
261
262static void installFilter(SkCanvas* canvas) {
263 if (gForceBWtext) {
264 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
265 }
266}
267
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000268static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
269 if (!isPDF) {
270 canvas->setMatrix(gm->getInitialTransform());
271 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000272 installFilter(canvas);
273 gm->draw(canvas);
274 canvas->setDrawFilter(NULL);
275}
276
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000277static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
278 GrContext* context,
279 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000280 SkBitmap* bitmap,
281 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000282 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000283 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000284
285 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000286 SkCanvas* canvas;
287 if (deferred) {
288 canvas = new SkDeferredCanvas;
289 canvas->setDevice(new SkDevice(*bitmap))->unref();
290 } else {
291 canvas = new SkCanvas(*bitmap);
292 }
293 SkAutoUnref canvasUnref(canvas);
294 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000295 canvas->flush();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000296 } else { // GPU
297 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000298 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000299 }
junov@google.com4370aed2012-01-18 16:21:08 +0000300 SkCanvas* gc;
301 if (deferred) {
302 gc = new SkDeferredCanvas;
303 } else {
304 gc = new SkGpuCanvas(context, rt);
305 }
306 SkAutoUnref gcUnref(gc);
307 gc->setDevice(new SkGpuDevice(context, rt))->unref();
308 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000309 // the device is as large as the current rendertarget, so we explicitly
310 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000311 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000312 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
313 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000314 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000315 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000316 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000317}
318
319static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
320 SkPicture* pict, SkBitmap* bitmap) {
321 SkISize size = gm->getISize();
322 setup_bitmap(gRec, size, bitmap);
323 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000324 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000325 canvas.drawPicture(*pict);
326}
327
328static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
329#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000330 SkMatrix initialTransform = gm->getInitialTransform();
331 SkISize pageSize = gm->getISize();
332 SkPDFDevice* dev = NULL;
333 if (initialTransform.isIdentity()) {
334 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
335 } else {
336 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
337 SkIntToScalar(pageSize.height()));
338 initialTransform.mapRect(&content);
339 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
340 SkIntToScalar(pageSize.height()));
341 SkISize contentSize =
342 SkISize::Make(SkScalarRoundToInt(content.width()),
343 SkScalarRoundToInt(content.height()));
344 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
345 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000346 SkAutoUnref aur(dev);
347
348 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000349 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000350
351 SkPDFDocument doc;
352 doc.appendPage(dev);
353 doc.emitPDF(&pdf);
354#endif
355}
356
bungeman@google.comb29c8832011-10-10 13:19:10 +0000357static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
358#ifdef SK_SUPPORT_XPS
359 SkISize size = gm->getISize();
360
361 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
362 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000363 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
364 static const SkScalar upm = 72 * inchesPerMeter;
365 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
366 static const SkScalar ppm = 200 * inchesPerMeter;
367 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000368
369 SkXPSDevice* dev = new SkXPSDevice();
370 SkAutoUnref aur(dev);
371
372 SkCanvas c(dev);
373 dev->beginPortfolio(&xps);
374 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000375 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000376 dev->endSheet();
377 dev->endPortfolio();
378
379#endif
380}
381
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000382static ErrorBitfield write_reference_image(const ConfigData& gRec,
383 const char writePath [],
384 const char renderModeDescriptor [],
385 const SkString& name,
386 SkBitmap& bitmap,
387 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000388 SkString path;
389 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000390 if (gRec.fBackend == kRaster_Backend ||
391 gRec.fBackend == kGPU_Backend ||
392 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
393
tomhudson@google.comea325432011-06-09 20:30:03 +0000394 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000395 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000396 }
397 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000398 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000399 success = write_document(path, *document);
400 }
401 if (kXPS_Backend == gRec.fBackend) {
402 path = make_filename(writePath, renderModeDescriptor, name, "xps");
403 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000404 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000405 if (success) {
406 return ERROR_NONE;
407 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000408 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000409 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000410 }
411}
412
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000413static ErrorBitfield compare_to_reference_image(const SkString& name,
414 SkBitmap &bitmap,
415 const SkBitmap& comparisonBitmap,
416 const char diffPath [],
417 const char renderModeDescriptor []) {
418 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000419 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000420 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
421 diffPath ? &diffBitmap : NULL);
422 if ((ERROR_NONE == errors) && diffPath) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000423 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000424 if (!write_bitmap(diffName, diffBitmap)) {
425 errors |= ERROR_WRITING_REFERENCE_IMAGE;
426 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000427 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000428 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000429}
430
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000431static ErrorBitfield compare_to_reference_image(const char readPath [],
432 const SkString& name,
433 SkBitmap &bitmap,
434 const char diffPath [],
435 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000436 SkString path = make_filename(readPath, "", name, "png");
437 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000438 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
439 SkBitmap::kARGB_8888_Config,
440 SkImageDecoder::kDecodePixels_Mode, NULL)) {
441 return compare_to_reference_image(name, bitmap,
442 orig, diffPath,
443 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000444 } else {
445 fprintf(stderr, "FAILED to read %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000446 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000447 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000448}
449
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000450static ErrorBitfield handle_test_results(GM* gm,
451 const ConfigData& gRec,
452 const char writePath [],
453 const char readPath [],
454 const char diffPath [],
455 const char renderModeDescriptor [],
456 SkBitmap& bitmap,
457 SkDynamicMemoryWStream* pdf,
458 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000459 SkString name = make_name(gm->shortName(), gRec.fName);
460
461 if (writePath) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000462 return write_reference_image(gRec, writePath, renderModeDescriptor,
463 name, bitmap, pdf);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000464 } else if (readPath && (
465 gRec.fBackend == kRaster_Backend ||
466 gRec.fBackend == kGPU_Backend ||
467 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000468 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000469 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000470 } else if (comparisonBitmap) {
471 return compare_to_reference_image(name, bitmap,
472 *comparisonBitmap, diffPath,
473 renderModeDescriptor);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000474 } else {
475 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000476 }
477}
478
479static SkPicture* generate_new_picture(GM* gm) {
480 // Pictures are refcounted so must be on heap
481 SkPicture* pict = new SkPicture;
482 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000483 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000484 pict->endRecording();
485
486 return pict;
487}
488
489static SkPicture* stream_to_new_picture(const SkPicture& src) {
490
491 // To do in-memory commiunications with a stream, we need to:
492 // * create a dynamic memory stream
493 // * copy it into a buffer
494 // * create a read stream from it
495 // ?!?!
496
497 SkDynamicMemoryWStream storage;
498 src.serialize(&storage);
499
500 int streamSize = storage.getOffset();
501 SkAutoMalloc dstStorage(streamSize);
502 void* dst = dstStorage.get();
503 //char* dst = new char [streamSize];
504 //@todo thudson 22 April 2011 when can we safely delete [] dst?
505 storage.copyTo(dst);
506 SkMemoryStream pictReadback(dst, streamSize);
507 SkPicture* retval = new SkPicture (&pictReadback);
508 return retval;
509}
510
511// Test: draw into a bitmap or pdf.
512// Depending on flags, possibly compare to an expected image
513// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000514static ErrorBitfield test_drawing(GM* gm,
515 const ConfigData& gRec,
516 const char writePath [],
517 const char readPath [],
518 const char diffPath [],
519 GrContext* context,
520 GrRenderTarget* rt,
521 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000522 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000523
524 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000525 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000526 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000527 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
528 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000529 if (ERROR_NONE != errors) {
530 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000531 }
reed@google.com46cce912011-06-29 12:54:46 +0000532 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000533 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000534#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000535 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000536 SkMemoryStream stream(data.data(), data.size());
537 SkPDFDocumentToBitmap(&stream, bitmap);
538#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000539 } else if (gRec.fBackend == kXPS_Backend) {
540 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000541 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000542 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000543 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000544}
545
junov@google.com4370aed2012-01-18 16:21:08 +0000546static ErrorBitfield test_deferred_drawing(GM* gm,
547 const ConfigData& gRec,
548 const SkBitmap& comparisonBitmap,
549 const char diffPath [],
550 GrContext* context,
551 GrRenderTarget* rt) {
552 SkDynamicMemoryWStream document;
553
554 if (gRec.fBackend == kRaster_Backend ||
555 gRec.fBackend == kGPU_Backend) {
556 SkBitmap bitmap;
557 // Early exit if we can't generate the image, but this is
558 // expected in some cases, so don't report a test failure.
559 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
560 return ERROR_NONE;
561 }
562 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
563 "-deferred", bitmap, NULL, &comparisonBitmap);
564 }
565 return ERROR_NONE;
566}
567
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000568static ErrorBitfield test_picture_playback(GM* gm,
569 const ConfigData& gRec,
570 const SkBitmap& comparisonBitmap,
571 const char readPath [],
572 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000573 SkPicture* pict = generate_new_picture(gm);
574 SkAutoUnref aur(pict);
575
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000576 if (kRaster_Backend == gRec.fBackend) {
577 SkBitmap bitmap;
578 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000579 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
580 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000581 } else {
582 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000583 }
584}
585
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000586static ErrorBitfield test_picture_serialization(GM* gm,
587 const ConfigData& gRec,
588 const SkBitmap& comparisonBitmap,
589 const char readPath [],
590 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000591 SkPicture* pict = generate_new_picture(gm);
592 SkAutoUnref aurp(pict);
593 SkPicture* repict = stream_to_new_picture(*pict);
594 SkAutoUnref aurr(repict);
595
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000596 if (kRaster_Backend == gRec.fBackend) {
597 SkBitmap bitmap;
598 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000599 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
600 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000601 } else {
602 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000603 }
604}
605
606static void usage(const char * argv0) {
junov@google.com77e498e2012-01-18 18:56:34 +0000607 SkDebugf(
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000608 "%s [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n"
609 " [--noreplay] [--serialize] [--forceBWtext] [--nopdf] \n"
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000610 " [--nodeferred] [--match substring] [--notexturecache]\n"
611 " "
junov@google.com77e498e2012-01-18 18:56:34 +0000612#if SK_MESA
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000613 "[--mesagl]"
junov@google.com77e498e2012-01-18 18:56:34 +0000614#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000615 " [--debuggl]\n\n", argv0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000616 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000617 SkDebugf(
618" readPath: directory to read reference images from;\n"
619" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000620 SkDebugf(" diffPath: directory to write difference images in.\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000621 SkDebugf(" resourcePath: directory that stores image resources.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000622 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000623 SkDebugf(
624" --serialize: exercise SkPicture serialization & deserialization.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000625 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
626 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
627 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
reed@google.come6a5c4d2011-07-25 14:30:54 +0000628 SkDebugf(" --match foo will only run tests that substring match foo.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000629#if SK_MESA
630 SkDebugf(" --mesagl will run using the osmesa sw gl rasterizer.\n");
631#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000632 SkDebugf(" --debuggl will run using the debugging gl utility.\n");
twiz@google.come24a0792012-01-31 18:35:30 +0000633 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000634}
635
636static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000637 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
638 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
639 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000640#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000641 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000642#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000643#ifdef SK_SUPPORT_PDF
644 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
645#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000646#ifdef SK_SUPPORT_XPS
647 { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" },
648#endif
reed@android.com00dae862009-06-10 15:38:48 +0000649};
650
reed@google.comb2a51622011-10-31 16:30:04 +0000651static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
652 if (0 == array.count()) {
653 // no names, so don't skip anything
654 return false;
655 }
656 for (int i = 0; i < array.count(); ++i) {
657 if (strstr(name, array[i])) {
658 // found the name, so don't skip
659 return false;
660 }
661 }
662 return true;
663}
664
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000665namespace skiagm {
666static GrContext* gGrContext;
667GrContext* GetGr() {
668 return gGrContext;
669}
670}
671
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000672int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000673 SkAutoGraphics ag;
reed@google.com8923c6c2011-11-08 14:59:38 +0000674 // we don't need to see this during a run
675 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000676
epoger@google.com7bc13a62012-02-14 14:53:59 +0000677 setSystemPreferences();
678
reed@android.com8015dd82009-06-21 00:49:18 +0000679 const char* writePath = NULL; // if non-null, where we write the originals
680 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000681 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000682 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000683
reed@google.comb2a51622011-10-31 16:30:04 +0000684 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000685
reed@google.comab973972011-09-19 19:01:38 +0000686 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000687 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000688 bool doSerialize = false;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000689 bool useMesa = false;
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000690 bool useDebugGL = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000691 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000692 bool disableTextureCache = false;
693
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000694 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000695 char* const* stop = argv + argc;
696 for (++argv; argv < stop; ++argv) {
697 if (strcmp(*argv, "-w") == 0) {
698 argv++;
699 if (argv < stop && **argv) {
700 writePath = *argv;
701 }
702 } else if (strcmp(*argv, "-r") == 0) {
703 argv++;
704 if (argv < stop && **argv) {
705 readPath = *argv;
706 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000707 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000708 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000709 if (argv < stop && **argv) {
710 diffPath = *argv;
711 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000712 } else if (strcmp(*argv, "-i") == 0) {
713 argv++;
714 if (argv < stop && **argv) {
715 resourcePath = *argv;
716 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000717 } else if (strcmp(*argv, "--forceBWtext") == 0) {
718 gForceBWtext = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000719 } else if (strcmp(*argv, "--noreplay") == 0) {
720 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000721 } else if (strcmp(*argv, "--nopdf") == 0) {
722 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000723 } else if (strcmp(*argv, "--nodeferred") == 0) {
724 doDeferred = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000725 } else if (strcmp(*argv, "--serialize") == 0) {
726 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000727 } else if (strcmp(*argv, "--match") == 0) {
728 ++argv;
729 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000730 // just record the ptr, no need for a deep copy
731 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000732 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000733#if SK_MESA
734 } else if (strcmp(*argv, "--mesagl") == 0) {
735 useMesa = true;
736#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000737 } else if (strcmp(*argv, "--debuggl") == 0) {
738 useDebugGL = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000739 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000740 disableTextureCache = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000741 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000742 usage(commandName);
743 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000744 }
745 }
746 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000747 usage(commandName);
748 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000749 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000750
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000751 GM::SetResourcePath(resourcePath);
752
bsalomon@google.com39149582011-06-13 21:55:32 +0000753 int maxW = -1;
754 int maxH = -1;
755 Iter iter;
756 GM* gm;
757 while ((gm = iter.next()) != NULL) {
758 SkISize size = gm->getISize();
759 maxW = SkMax32(size.width(), maxW);
760 maxH = SkMax32(size.height(), maxH);
tomhudson@google.com7816a4e2012-03-15 13:39:51 +0000761 // This fixes a memory leak, but we are churning gms; we could
762 // instead cache them if we have constructors with side-effects.
763 SkDELETE(gm);
bsalomon@google.com39149582011-06-13 21:55:32 +0000764 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000765 // setup a GL context for drawing offscreen
bsalomon@google.com373a6632011-10-19 20:43:20 +0000766 SkAutoTUnref<SkGLContext> glContext;
767#if SK_MESA
768 if (useMesa) {
769 glContext.reset(new SkMesaGLContext());
770 } else
771#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000772 if (useDebugGL) {
773 glContext.reset(new SkDebugGLContext());
774 } else {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000775 glContext.reset(new SkNativeGLContext());
776 }
777
bsalomon@google.com29d35012011-11-30 16:57:21 +0000778 GrPlatformRenderTargetDesc rtDesc;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000779 if (glContext.get()->init(maxW, maxH)) {
780 GrPlatform3DContext ctx =
781 reinterpret_cast<GrPlatform3DContext>(glContext.get()->gl());
782 gGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000783 if (NULL != gGrContext) {
bsalomon@google.com29d35012011-11-30 16:57:21 +0000784 rtDesc.fConfig = kSkia8888_PM_GrPixelConfig;
785 rtDesc.fStencilBits = 8;
786 rtDesc.fRenderTargetHandle = glContext.get()->getFBOID();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000787 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000788 } else {
789 fprintf(stderr, "could not create GL context.\n");
reed@google.com37df17d2010-12-23 20:20:51 +0000790 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000791
reed@android.com00f883e2010-12-14 17:46:14 +0000792 if (readPath) {
793 fprintf(stderr, "reading from %s\n", readPath);
794 } else if (writePath) {
795 fprintf(stderr, "writing to %s\n", writePath);
796 }
797
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000798 if (resourcePath) {
799 fprintf(stderr, "reading resources from %s\n", resourcePath);
800 }
801
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000802 // Accumulate success of all tests.
803 int testsRun = 0;
804 int testsPassed = 0;
805 int testsFailed = 0;
806 int testsMissingReferenceImages = 0;
807
twiz@google.come24a0792012-01-31 18:35:30 +0000808 if (disableTextureCache) {
809 skiagm::GetGr()->setTextureCacheLimits(0, 0);
810 }
811
bsalomon@google.com39149582011-06-13 21:55:32 +0000812 iter.reset();
reed@android.com00dae862009-06-10 15:38:48 +0000813 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000814 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000815 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000816 SkDELETE(gm);
817 continue;
818 }
819
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000820 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000821 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000822 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000823 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000824
bsalomon@google.com29d35012011-11-30 16:57:21 +0000825 // Above we created an fbo for the context at maxW x maxH size.
826 // Here we lie about the size of the rt. We claim it is the size
827 // desired by the test. The reason is that rasterization may change
828 // slightly when the viewport dimensions change. Previously, whenever
829 // a new test was checked in that bumped maxW or maxH several images
830 // would slightly change.
831 rtDesc.fWidth = size.width();
832 rtDesc.fHeight = size.height();
833 SkAutoTUnref<GrRenderTarget> rt;
834 if (gGrContext) {
835 rt.reset(gGrContext->createPlatformRenderTarget(rtDesc));
836 }
reed@google.comfbc21172011-09-19 19:08:33 +0000837
bsalomon@google.com29d35012011-11-30 16:57:21 +0000838 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000839 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000840 uint32_t gmFlags = gm->getFlags();
twiz@google.come24a0792012-01-31 18:35:30 +0000841 if ((kPDF_Backend == gRec[i].fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +0000842 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
843 {
reed@google.comab973972011-09-19 19:01:38 +0000844 continue;
845 }
846
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000847 // Now we know that we want to run this test and record its
848 // success or failure.
849 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000850
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000851 if ((ERROR_NONE == testErrors) &&
852 (kGPU_Backend == gRec[i].fBackend) &&
853 (NULL == rt.get())) {
854 fprintf(stderr, "Could not create render target for gpu.\n");
855 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000856 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000857
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000858 if (ERROR_NONE == testErrors) {
859 testErrors |= test_drawing(gm, gRec[i],
860 writePath, readPath, diffPath,
861 gGrContext,
862 rt.get(), &forwardRenderedBitmap);
863 }
864
junov@google.com4370aed2012-01-18 16:21:08 +0000865 if (doDeferred && !testErrors &&
twiz@google.come24a0792012-01-31 18:35:30 +0000866 (kGPU_Backend == gRec[i].fBackend ||
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000867 kRaster_Backend == gRec[i].fBackend)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000868 testErrors |= test_deferred_drawing(gm, gRec[i],
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000869 forwardRenderedBitmap,
870 diffPath, gGrContext, rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +0000871 }
872
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000873 if ((ERROR_NONE == testErrors) && doReplay &&
874 !(gmFlags & GM::kSkipPicture_Flag)) {
875 testErrors |= test_picture_playback(gm, gRec[i],
876 forwardRenderedBitmap,
877 readPath, diffPath);
878 }
879
880 if ((ERROR_NONE == testErrors) && doSerialize) {
881 testErrors |= test_picture_serialization(gm, gRec[i],
882 forwardRenderedBitmap,
883 readPath, diffPath);
884 }
885
886 // Update overall results.
887 // We only tabulate the particular error types that we currently
888 // care about (e.g., missing reference images). Later on, if we
889 // want to also tabulate pixel mismatches vs dimension mistmatches
890 // (or whatever else), we can do so.
891 testsRun++;
892 if (ERROR_NONE == testErrors) {
893 testsPassed++;
894 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
895 testsMissingReferenceImages++;
896 } else {
897 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000898 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000899 }
reed@android.com00dae862009-06-10 15:38:48 +0000900 SkDELETE(gm);
901 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000902 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
903 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000904
905 SkDELETE(skiagm::gGrContext);
906 skiagm::gGrContext = NULL;
907
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000908 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +0000909}