blob: 76bd4914b136d74781af072c8643f1f7b28de0f4 [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"
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000023#if SK_MESA
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000024#include "gl/SkMesaGLContext.h"
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000025#endif
26#if SK_ANGLE
27#include "gl/SkANGLEGLContext.h"
28#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +000029#include "gl/SkDebugGLContext.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000030#include "SkPicture.h"
reed@google.com07700442010-12-20 19:46:07 +000031#include "SkStream.h"
32#include "SkRefCnt.h"
33
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000034static bool gForceBWtext;
35
reed@google.com8923c6c2011-11-08 14:59:38 +000036extern bool gSkSuppressFontCachePurgeSpew;
37
reed@google.com07700442010-12-20 19:46:07 +000038#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000039 #include "SkPDFDevice.h"
40 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000041#endif
reed@android.com00dae862009-06-10 15:38:48 +000042
epoger@google.come3cc2eb2012-01-18 20:11:13 +000043// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
44// stop writing out XPS-format image baselines in gm.
45#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000046#ifdef SK_SUPPORT_XPS
47 #include "SkXPSDevice.h"
48#endif
49
reed@google.com46cce912011-06-29 12:54:46 +000050#ifdef SK_BUILD_FOR_MAC
51 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000052 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000053#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000054 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000055#endif
56
epoger@google.comc7cf2b32011-12-28 19:31:01 +000057typedef int ErrorBitfield;
58const static ErrorBitfield ERROR_NONE = 0x00;
59const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
60const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
61const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
62const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
63const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
64
reed@android.com00dae862009-06-10 15:38:48 +000065using namespace skiagm;
66
reed@android.com00dae862009-06-10 15:38:48 +000067class Iter {
68public:
69 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000070 this->reset();
71 }
72
73 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000074 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000075 }
reed@google.comd4dfd102011-01-18 21:05:42 +000076
reed@android.comdd0ac282009-06-20 02:38:16 +000077 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000078 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000079 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000080 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000081 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000082 }
83 return NULL;
84 }
reed@google.comd4dfd102011-01-18 21:05:42 +000085
reed@android.com00dae862009-06-10 15:38:48 +000086 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000087 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000088 int count = 0;
89 while (reg) {
90 count += 1;
91 reg = reg->next();
92 }
93 return count;
94 }
reed@google.comd4dfd102011-01-18 21:05:42 +000095
reed@android.com00dae862009-06-10 15:38:48 +000096private:
97 const GMRegistry* fReg;
98};
99
reed@android.com8015dd82009-06-21 00:49:18 +0000100static SkString make_name(const char shortName[], const char configName[]) {
101 SkString name(shortName);
102 name.appendf("_%s", configName);
103 return name;
104}
105
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000106static SkString make_filename(const char path[],
107 const char pathSuffix[],
108 const SkString& name,
109 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000110 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000111 if (filename.endsWith("/")) {
112 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000113 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000114 filename.append(pathSuffix);
115 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000116 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000117 return filename;
118}
119
reed@android.comb9b9a182009-07-08 02:54:47 +0000120/* since PNG insists on unpremultiplying our alpha, we take no precision chances
121 and force all pixels to be 100% opaque, otherwise on compare we may not get
122 a perfect match.
123 */
124static void force_all_opaque(const SkBitmap& bitmap) {
125 SkAutoLockPixels lock(bitmap);
126 for (int y = 0; y < bitmap.height(); y++) {
127 for (int x = 0; x < bitmap.width(); x++) {
128 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
129 }
130 }
131}
132
133static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
134 SkBitmap copy;
135 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
136 force_all_opaque(copy);
137 return SkImageEncoder::EncodeFile(path.c_str(), copy,
138 SkImageEncoder::kPNG_Type, 100);
139}
140
reed@google.com3d3f0922010-12-20 21:10:29 +0000141static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000142 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
143 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
144 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
145 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000146}
147
148static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000149 SkBitmap* diff) {
150 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000151
152 const int w = target.width();
153 const int h = target.height();
154 for (int y = 0; y < h; y++) {
155 for (int x = 0; x < w; x++) {
156 SkPMColor c0 = *base.getAddr32(x, y);
157 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000158 SkPMColor d = 0;
159 if (c0 != c1) {
160 d = compute_diff_pmcolor(c0, c1);
161 }
162 *diff->getAddr32(x, y) = d;
163 }
164 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000165}
166
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000167static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
168 const SkString& name,
169 const char* renderModeDescriptor,
170 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000171 SkBitmap copy;
172 const SkBitmap* bm = &target;
173 if (target.config() != SkBitmap::kARGB_8888_Config) {
174 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
175 bm = &copy;
176 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000177 SkBitmap baseCopy;
178 const SkBitmap* bp = &base;
179 if (base.config() != SkBitmap::kARGB_8888_Config) {
180 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
181 bp = &baseCopy;
182 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000183
184 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000185 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000186
187 const int w = bm->width();
188 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000189 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000190 SkDebugf(
191"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
192 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000193 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000194 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000195 }
196
197 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000198 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000199
200 for (int y = 0; y < h; y++) {
201 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000202 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000203 SkPMColor c1 = *bm->getAddr32(x, y);
204 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000205 SkDebugf(
206"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
207 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000208
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000209 if (diff) {
210 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
211 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000212 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000213 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000214 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000215 }
216 }
217 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000218
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000219 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000220 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000221}
reed@android.com00dae862009-06-10 15:38:48 +0000222
bungeman@google.comb29c8832011-10-10 13:19:10 +0000223static bool write_document(const SkString& path,
224 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000225 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000226 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000227 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000228}
229
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000230enum Backend {
231 kRaster_Backend,
232 kGPU_Backend,
233 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000234 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000235};
236
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000237struct ConfigData {
238 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000239 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000240 const char* fName;
241};
242
243/// Returns true if processing should continue, false to skip the
244/// remainder of this config for this GM.
245//@todo thudson 22 April 2011 - could refactor this to take in
246// a factory to generate the context, always call readPixels()
247// (logically a noop for rasters, if wasted time), and thus collapse the
248// GPU special case and also let this be used for SkPicture testing.
249static void setup_bitmap(const ConfigData& gRec, SkISize& size,
250 SkBitmap* bitmap) {
251 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
252 bitmap->allocPixels();
253 bitmap->eraseColor(0);
254}
255
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000256#include "SkDrawFilter.h"
257class BWTextDrawFilter : public SkDrawFilter {
258public:
259 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
260};
261void BWTextDrawFilter::filter(SkPaint* p, Type t) {
262 if (kText_Type == t) {
263 p->setAntiAlias(false);
264 }
265}
266
267static void installFilter(SkCanvas* canvas) {
268 if (gForceBWtext) {
269 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
270 }
271}
272
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000273static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
274 if (!isPDF) {
275 canvas->setMatrix(gm->getInitialTransform());
276 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000277 installFilter(canvas);
278 gm->draw(canvas);
279 canvas->setDrawFilter(NULL);
280}
281
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000282static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
283 GrContext* context,
284 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000285 SkBitmap* bitmap,
286 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000287 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000288 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000289
290 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000291 SkCanvas* canvas;
292 if (deferred) {
293 canvas = new SkDeferredCanvas;
294 canvas->setDevice(new SkDevice(*bitmap))->unref();
295 } else {
296 canvas = new SkCanvas(*bitmap);
297 }
298 SkAutoUnref canvasUnref(canvas);
299 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000300 canvas->flush();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000301 } else { // GPU
302 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000303 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000304 }
junov@google.com4370aed2012-01-18 16:21:08 +0000305 SkCanvas* gc;
306 if (deferred) {
307 gc = new SkDeferredCanvas;
308 } else {
309 gc = new SkGpuCanvas(context, rt);
310 }
311 SkAutoUnref gcUnref(gc);
312 gc->setDevice(new SkGpuDevice(context, rt))->unref();
313 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000314 // the device is as large as the current rendertarget, so we explicitly
315 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000316 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000317 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
318 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000319 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000320 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000321 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000322}
323
324static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
325 SkPicture* pict, SkBitmap* bitmap) {
326 SkISize size = gm->getISize();
327 setup_bitmap(gRec, size, bitmap);
328 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000329 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000330 canvas.drawPicture(*pict);
331}
332
333static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
334#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000335 SkMatrix initialTransform = gm->getInitialTransform();
336 SkISize pageSize = gm->getISize();
337 SkPDFDevice* dev = NULL;
338 if (initialTransform.isIdentity()) {
339 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
340 } else {
341 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
342 SkIntToScalar(pageSize.height()));
343 initialTransform.mapRect(&content);
344 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
345 SkIntToScalar(pageSize.height()));
346 SkISize contentSize =
347 SkISize::Make(SkScalarRoundToInt(content.width()),
348 SkScalarRoundToInt(content.height()));
349 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
350 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000351 SkAutoUnref aur(dev);
352
353 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000354 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000355
356 SkPDFDocument doc;
357 doc.appendPage(dev);
358 doc.emitPDF(&pdf);
359#endif
360}
361
bungeman@google.comb29c8832011-10-10 13:19:10 +0000362static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
363#ifdef SK_SUPPORT_XPS
364 SkISize size = gm->getISize();
365
366 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
367 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000368 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
369 static const SkScalar upm = 72 * inchesPerMeter;
370 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
371 static const SkScalar ppm = 200 * inchesPerMeter;
372 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000373
374 SkXPSDevice* dev = new SkXPSDevice();
375 SkAutoUnref aur(dev);
376
377 SkCanvas c(dev);
378 dev->beginPortfolio(&xps);
379 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000380 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000381 dev->endSheet();
382 dev->endPortfolio();
383
384#endif
385}
386
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000387static ErrorBitfield write_reference_image(const ConfigData& gRec,
388 const char writePath [],
389 const char renderModeDescriptor [],
390 const SkString& name,
391 SkBitmap& bitmap,
392 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000393 SkString path;
394 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000395 if (gRec.fBackend == kRaster_Backend ||
396 gRec.fBackend == kGPU_Backend ||
397 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
398
tomhudson@google.comea325432011-06-09 20:30:03 +0000399 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000400 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000401 }
402 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000403 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000404 success = write_document(path, *document);
405 }
406 if (kXPS_Backend == gRec.fBackend) {
407 path = make_filename(writePath, renderModeDescriptor, name, "xps");
408 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000409 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000410 if (success) {
411 return ERROR_NONE;
412 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000413 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000414 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000415 }
416}
417
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000418static ErrorBitfield compare_to_reference_image(const SkString& name,
419 SkBitmap &bitmap,
420 const SkBitmap& comparisonBitmap,
421 const char diffPath [],
422 const char renderModeDescriptor []) {
423 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000424 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000425 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
426 diffPath ? &diffBitmap : NULL);
427 if ((ERROR_NONE == errors) && diffPath) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000428 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000429 if (!write_bitmap(diffName, diffBitmap)) {
430 errors |= ERROR_WRITING_REFERENCE_IMAGE;
431 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000432 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000433 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000434}
435
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000436static ErrorBitfield compare_to_reference_image(const char readPath [],
437 const SkString& name,
438 SkBitmap &bitmap,
439 const char diffPath [],
440 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000441 SkString path = make_filename(readPath, "", name, "png");
442 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000443 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
444 SkBitmap::kARGB_8888_Config,
445 SkImageDecoder::kDecodePixels_Mode, NULL)) {
446 return compare_to_reference_image(name, bitmap,
447 orig, diffPath,
448 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000449 } else {
450 fprintf(stderr, "FAILED to read %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000451 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000452 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000453}
454
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000455static ErrorBitfield handle_test_results(GM* gm,
456 const ConfigData& gRec,
457 const char writePath [],
458 const char readPath [],
459 const char diffPath [],
460 const char renderModeDescriptor [],
461 SkBitmap& bitmap,
462 SkDynamicMemoryWStream* pdf,
463 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000464 SkString name = make_name(gm->shortName(), gRec.fName);
465
466 if (writePath) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000467 return write_reference_image(gRec, writePath, renderModeDescriptor,
468 name, bitmap, pdf);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000469 } else if (readPath && (
470 gRec.fBackend == kRaster_Backend ||
471 gRec.fBackend == kGPU_Backend ||
472 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000473 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000474 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000475 } else if (comparisonBitmap) {
476 return compare_to_reference_image(name, bitmap,
477 *comparisonBitmap, diffPath,
478 renderModeDescriptor);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000479 } else {
480 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000481 }
482}
483
484static SkPicture* generate_new_picture(GM* gm) {
485 // Pictures are refcounted so must be on heap
486 SkPicture* pict = new SkPicture;
487 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000488 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000489 pict->endRecording();
490
491 return pict;
492}
493
494static SkPicture* stream_to_new_picture(const SkPicture& src) {
495
496 // To do in-memory commiunications with a stream, we need to:
497 // * create a dynamic memory stream
498 // * copy it into a buffer
499 // * create a read stream from it
500 // ?!?!
501
502 SkDynamicMemoryWStream storage;
503 src.serialize(&storage);
504
505 int streamSize = storage.getOffset();
506 SkAutoMalloc dstStorage(streamSize);
507 void* dst = dstStorage.get();
508 //char* dst = new char [streamSize];
509 //@todo thudson 22 April 2011 when can we safely delete [] dst?
510 storage.copyTo(dst);
511 SkMemoryStream pictReadback(dst, streamSize);
512 SkPicture* retval = new SkPicture (&pictReadback);
513 return retval;
514}
515
516// Test: draw into a bitmap or pdf.
517// Depending on flags, possibly compare to an expected image
518// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000519static ErrorBitfield test_drawing(GM* gm,
520 const ConfigData& gRec,
521 const char writePath [],
522 const char readPath [],
523 const char diffPath [],
524 GrContext* context,
525 GrRenderTarget* rt,
526 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000527 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000528
529 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000530 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000531 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000532 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
533 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000534 if (ERROR_NONE != errors) {
535 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000536 }
reed@google.com46cce912011-06-29 12:54:46 +0000537 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000538 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000539#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000540 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000541 SkMemoryStream stream(data.data(), data.size());
542 SkPDFDocumentToBitmap(&stream, bitmap);
543#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000544 } else if (gRec.fBackend == kXPS_Backend) {
545 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000546 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000547 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000548 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000549}
550
junov@google.com4370aed2012-01-18 16:21:08 +0000551static ErrorBitfield test_deferred_drawing(GM* gm,
552 const ConfigData& gRec,
553 const SkBitmap& comparisonBitmap,
554 const char diffPath [],
555 GrContext* context,
556 GrRenderTarget* rt) {
557 SkDynamicMemoryWStream document;
558
559 if (gRec.fBackend == kRaster_Backend ||
560 gRec.fBackend == kGPU_Backend) {
561 SkBitmap bitmap;
562 // Early exit if we can't generate the image, but this is
563 // expected in some cases, so don't report a test failure.
564 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
565 return ERROR_NONE;
566 }
567 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
568 "-deferred", bitmap, NULL, &comparisonBitmap);
569 }
570 return ERROR_NONE;
571}
572
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000573static ErrorBitfield test_picture_playback(GM* gm,
574 const ConfigData& gRec,
575 const SkBitmap& comparisonBitmap,
576 const char readPath [],
577 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000578 SkPicture* pict = generate_new_picture(gm);
579 SkAutoUnref aur(pict);
580
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000581 if (kRaster_Backend == gRec.fBackend) {
582 SkBitmap bitmap;
583 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000584 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
585 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000586 } else {
587 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000588 }
589}
590
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000591static ErrorBitfield test_picture_serialization(GM* gm,
592 const ConfigData& gRec,
593 const SkBitmap& comparisonBitmap,
594 const char readPath [],
595 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000596 SkPicture* pict = generate_new_picture(gm);
597 SkAutoUnref aurp(pict);
598 SkPicture* repict = stream_to_new_picture(*pict);
599 SkAutoUnref aurr(repict);
600
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000601 if (kRaster_Backend == gRec.fBackend) {
602 SkBitmap bitmap;
603 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000604 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
605 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000606 } else {
607 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000608 }
609}
610
611static void usage(const char * argv0) {
junov@google.com77e498e2012-01-18 18:56:34 +0000612 SkDebugf(
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000613 "%s [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n"
614 " [--noreplay] [--serialize] [--forceBWtext] [--nopdf] \n"
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000615 " [--nodeferred] [--match substring] [--notexturecache]\n"
616 " "
junov@google.com77e498e2012-01-18 18:56:34 +0000617#if SK_MESA
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000618 "[--mesagl]"
junov@google.com77e498e2012-01-18 18:56:34 +0000619#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000620#if SK_ANGLE
621 " [--angle]"
622#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000623 " [--debuggl]\n\n", argv0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000624 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000625 SkDebugf(
626" readPath: directory to read reference images from;\n"
627" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000628 SkDebugf(" diffPath: directory to write difference images in.\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000629 SkDebugf(" resourcePath: directory that stores image resources.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000630 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000631 SkDebugf(
632" --serialize: exercise SkPicture serialization & deserialization.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000633 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
634 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
635 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000636 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000637#if SK_MESA
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000638 SkDebugf(" --mesagl: will run using the osmesa sw gl rasterizer.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000639#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000640#if SK_ANGLE
641 SkDebugf(" --angle: use ANGLE backend on Windows.\n");
642#endif
643 SkDebugf(" --debuggl: will run using the debugging gl utility.\n");
twiz@google.come24a0792012-01-31 18:35:30 +0000644 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000645}
646
647static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000648 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
649 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
650 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000651#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000652 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000653#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000654#ifdef SK_SUPPORT_PDF
655 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
656#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000657#ifdef SK_SUPPORT_XPS
658 { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" },
659#endif
reed@android.com00dae862009-06-10 15:38:48 +0000660};
661
reed@google.comb2a51622011-10-31 16:30:04 +0000662static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
663 if (0 == array.count()) {
664 // no names, so don't skip anything
665 return false;
666 }
667 for (int i = 0; i < array.count(); ++i) {
668 if (strstr(name, array[i])) {
669 // found the name, so don't skip
670 return false;
671 }
672 }
673 return true;
674}
675
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000676namespace skiagm {
677static GrContext* gGrContext;
678GrContext* GetGr() {
679 return gGrContext;
680}
681}
682
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000683int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000684 SkAutoGraphics ag;
reed@google.com8923c6c2011-11-08 14:59:38 +0000685 // we don't need to see this during a run
686 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000687
epoger@google.com7bc13a62012-02-14 14:53:59 +0000688 setSystemPreferences();
689
reed@android.com8015dd82009-06-21 00:49:18 +0000690 const char* writePath = NULL; // if non-null, where we write the originals
691 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000692 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000693 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000694
reed@google.comb2a51622011-10-31 16:30:04 +0000695 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000696
reed@google.comab973972011-09-19 19:01:38 +0000697 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000698 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000699 bool doSerialize = false;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000700#if SK_MESA
bsalomon@google.com373a6632011-10-19 20:43:20 +0000701 bool useMesa = false;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000702#endif
703#if SK_ANGLE
704 bool useAngle = false;
705#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000706 bool useDebugGL = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000707 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000708 bool disableTextureCache = false;
709
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000710 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000711 char* const* stop = argv + argc;
712 for (++argv; argv < stop; ++argv) {
713 if (strcmp(*argv, "-w") == 0) {
714 argv++;
715 if (argv < stop && **argv) {
716 writePath = *argv;
717 }
718 } else if (strcmp(*argv, "-r") == 0) {
719 argv++;
720 if (argv < stop && **argv) {
721 readPath = *argv;
722 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000723 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000724 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000725 if (argv < stop && **argv) {
726 diffPath = *argv;
727 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000728 } else if (strcmp(*argv, "-i") == 0) {
729 argv++;
730 if (argv < stop && **argv) {
731 resourcePath = *argv;
732 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000733 } else if (strcmp(*argv, "--forceBWtext") == 0) {
734 gForceBWtext = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000735 } else if (strcmp(*argv, "--noreplay") == 0) {
736 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000737 } else if (strcmp(*argv, "--nopdf") == 0) {
738 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000739 } else if (strcmp(*argv, "--nodeferred") == 0) {
740 doDeferred = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000741 } else if (strcmp(*argv, "--serialize") == 0) {
742 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000743 } else if (strcmp(*argv, "--match") == 0) {
744 ++argv;
745 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000746 // just record the ptr, no need for a deep copy
747 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000748 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000749#if SK_MESA
750 } else if (strcmp(*argv, "--mesagl") == 0) {
751 useMesa = true;
752#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000753#if SK_ANGLE
754 } else if (strcmp(*argv, "--angle") == 0) {
755 useAngle = true;
756#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000757 } else if (strcmp(*argv, "--debuggl") == 0) {
758 useDebugGL = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000759 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000760 disableTextureCache = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000761 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000762 usage(commandName);
763 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000764 }
765 }
766 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000767 usage(commandName);
768 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000769 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000770
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000771 GM::SetResourcePath(resourcePath);
772
bsalomon@google.com39149582011-06-13 21:55:32 +0000773 int maxW = -1;
774 int maxH = -1;
775 Iter iter;
776 GM* gm;
777 while ((gm = iter.next()) != NULL) {
778 SkISize size = gm->getISize();
779 maxW = SkMax32(size.width(), maxW);
780 maxH = SkMax32(size.height(), maxH);
tomhudson@google.com7816a4e2012-03-15 13:39:51 +0000781 // This fixes a memory leak, but we are churning gms; we could
782 // instead cache them if we have constructors with side-effects.
783 SkDELETE(gm);
bsalomon@google.com39149582011-06-13 21:55:32 +0000784 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000785 // setup a GL context for drawing offscreen
bsalomon@google.com373a6632011-10-19 20:43:20 +0000786 SkAutoTUnref<SkGLContext> glContext;
787#if SK_MESA
788 if (useMesa) {
789 glContext.reset(new SkMesaGLContext());
790 } else
791#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000792#if SK_ANGLE
793 if (useAngle) {
794 glContext.reset(new SkANGLEGLContext());
795 } else
796#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000797 if (useDebugGL) {
798 glContext.reset(new SkDebugGLContext());
799 } else {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000800 glContext.reset(new SkNativeGLContext());
801 }
802
bsalomon@google.com29d35012011-11-30 16:57:21 +0000803 GrPlatformRenderTargetDesc rtDesc;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000804 if (glContext.get()->init(maxW, maxH)) {
805 GrPlatform3DContext ctx =
806 reinterpret_cast<GrPlatform3DContext>(glContext.get()->gl());
807 gGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000808 if (NULL != gGrContext) {
bsalomon@google.com29d35012011-11-30 16:57:21 +0000809 rtDesc.fConfig = kSkia8888_PM_GrPixelConfig;
810 rtDesc.fStencilBits = 8;
811 rtDesc.fRenderTargetHandle = glContext.get()->getFBOID();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000812 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000813 } else {
814 fprintf(stderr, "could not create GL context.\n");
reed@google.com37df17d2010-12-23 20:20:51 +0000815 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000816
reed@android.com00f883e2010-12-14 17:46:14 +0000817 if (readPath) {
818 fprintf(stderr, "reading from %s\n", readPath);
819 } else if (writePath) {
820 fprintf(stderr, "writing to %s\n", writePath);
821 }
822
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000823 if (resourcePath) {
824 fprintf(stderr, "reading resources from %s\n", resourcePath);
825 }
826
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000827 // Accumulate success of all tests.
828 int testsRun = 0;
829 int testsPassed = 0;
830 int testsFailed = 0;
831 int testsMissingReferenceImages = 0;
832
twiz@google.come24a0792012-01-31 18:35:30 +0000833 if (disableTextureCache) {
834 skiagm::GetGr()->setTextureCacheLimits(0, 0);
835 }
836
bsalomon@google.com39149582011-06-13 21:55:32 +0000837 iter.reset();
reed@android.com00dae862009-06-10 15:38:48 +0000838 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000839 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000840 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000841 SkDELETE(gm);
842 continue;
843 }
844
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000845 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000846 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000847 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000848 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000849
bsalomon@google.com29d35012011-11-30 16:57:21 +0000850 // Above we created an fbo for the context at maxW x maxH size.
851 // Here we lie about the size of the rt. We claim it is the size
852 // desired by the test. The reason is that rasterization may change
853 // slightly when the viewport dimensions change. Previously, whenever
854 // a new test was checked in that bumped maxW or maxH several images
855 // would slightly change.
856 rtDesc.fWidth = size.width();
857 rtDesc.fHeight = size.height();
858 SkAutoTUnref<GrRenderTarget> rt;
859 if (gGrContext) {
860 rt.reset(gGrContext->createPlatformRenderTarget(rtDesc));
861 }
reed@google.comfbc21172011-09-19 19:08:33 +0000862
bsalomon@google.com29d35012011-11-30 16:57:21 +0000863 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000864 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000865 uint32_t gmFlags = gm->getFlags();
twiz@google.come24a0792012-01-31 18:35:30 +0000866 if ((kPDF_Backend == gRec[i].fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +0000867 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
868 {
reed@google.comab973972011-09-19 19:01:38 +0000869 continue;
870 }
871
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000872 // Now we know that we want to run this test and record its
873 // success or failure.
874 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000875
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000876 if ((ERROR_NONE == testErrors) &&
877 (kGPU_Backend == gRec[i].fBackend) &&
878 (NULL == rt.get())) {
879 fprintf(stderr, "Could not create render target for gpu.\n");
880 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000881 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000882
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000883 if (ERROR_NONE == testErrors) {
884 testErrors |= test_drawing(gm, gRec[i],
885 writePath, readPath, diffPath,
886 gGrContext,
887 rt.get(), &forwardRenderedBitmap);
888 }
889
junov@google.com4370aed2012-01-18 16:21:08 +0000890 if (doDeferred && !testErrors &&
twiz@google.come24a0792012-01-31 18:35:30 +0000891 (kGPU_Backend == gRec[i].fBackend ||
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000892 kRaster_Backend == gRec[i].fBackend)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000893 testErrors |= test_deferred_drawing(gm, gRec[i],
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000894 forwardRenderedBitmap,
895 diffPath, gGrContext, rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +0000896 }
897
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000898 if ((ERROR_NONE == testErrors) && doReplay &&
899 !(gmFlags & GM::kSkipPicture_Flag)) {
900 testErrors |= test_picture_playback(gm, gRec[i],
901 forwardRenderedBitmap,
902 readPath, diffPath);
903 }
904
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000905 if ((ERROR_NONE == testErrors) && doSerialize &&
906 !(gmFlags & GM::kSkipPicture_Flag)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000907 testErrors |= test_picture_serialization(gm, gRec[i],
908 forwardRenderedBitmap,
909 readPath, diffPath);
910 }
911
912 // Update overall results.
913 // We only tabulate the particular error types that we currently
914 // care about (e.g., missing reference images). Later on, if we
915 // want to also tabulate pixel mismatches vs dimension mistmatches
916 // (or whatever else), we can do so.
917 testsRun++;
918 if (ERROR_NONE == testErrors) {
919 testsPassed++;
920 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
921 testsMissingReferenceImages++;
922 } else {
923 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000924 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000925 }
reed@android.com00dae862009-06-10 15:38:48 +0000926 SkDELETE(gm);
927 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000928 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
929 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000930
931 SkDELETE(skiagm::gGrContext);
932 skiagm::gGrContext = NULL;
933
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000934 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +0000935}