blob: def00948f909768bf96c1da2911d9ff1358c794b [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"
reed@google.com07700442010-12-20 19:46:07 +000032
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000033static bool gForceBWtext;
34
reed@google.com8923c6c2011-11-08 14:59:38 +000035extern bool gSkSuppressFontCachePurgeSpew;
36
reed@google.com07700442010-12-20 19:46:07 +000037#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000038 #include "SkPDFDevice.h"
39 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000040#endif
reed@android.com00dae862009-06-10 15:38:48 +000041
epoger@google.come3cc2eb2012-01-18 20:11:13 +000042// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
43// stop writing out XPS-format image baselines in gm.
44#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000045#ifdef SK_SUPPORT_XPS
46 #include "SkXPSDevice.h"
47#endif
48
reed@google.com46cce912011-06-29 12:54:46 +000049#ifdef SK_BUILD_FOR_MAC
50 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000051 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000052#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000053 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000054#endif
55
epoger@google.comc7cf2b32011-12-28 19:31:01 +000056typedef int ErrorBitfield;
57const static ErrorBitfield ERROR_NONE = 0x00;
58const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
59const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
60const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
61const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
62const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
63
reed@android.com00dae862009-06-10 15:38:48 +000064using namespace skiagm;
65
reed@android.com00dae862009-06-10 15:38:48 +000066class Iter {
67public:
68 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000069 this->reset();
70 }
71
72 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000073 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000074 }
reed@google.comd4dfd102011-01-18 21:05:42 +000075
reed@android.comdd0ac282009-06-20 02:38:16 +000076 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000077 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000078 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000079 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000080 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000081 }
82 return NULL;
83 }
reed@google.comd4dfd102011-01-18 21:05:42 +000084
reed@android.com00dae862009-06-10 15:38:48 +000085 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000086 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000087 int count = 0;
88 while (reg) {
89 count += 1;
90 reg = reg->next();
91 }
92 return count;
93 }
reed@google.comd4dfd102011-01-18 21:05:42 +000094
reed@android.com00dae862009-06-10 15:38:48 +000095private:
96 const GMRegistry* fReg;
97};
98
reed@android.com8015dd82009-06-21 00:49:18 +000099static SkString make_name(const char shortName[], const char configName[]) {
100 SkString name(shortName);
101 name.appendf("_%s", configName);
102 return name;
103}
104
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000105static SkString make_filename(const char path[],
106 const char pathSuffix[],
107 const SkString& name,
108 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000109 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000110 if (filename.endsWith("/")) {
111 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000112 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000113 filename.append(pathSuffix);
114 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000115 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000116 return filename;
117}
118
reed@android.comb9b9a182009-07-08 02:54:47 +0000119/* since PNG insists on unpremultiplying our alpha, we take no precision chances
120 and force all pixels to be 100% opaque, otherwise on compare we may not get
121 a perfect match.
122 */
123static void force_all_opaque(const SkBitmap& bitmap) {
124 SkAutoLockPixels lock(bitmap);
125 for (int y = 0; y < bitmap.height(); y++) {
126 for (int x = 0; x < bitmap.width(); x++) {
127 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
128 }
129 }
130}
131
132static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
133 SkBitmap copy;
134 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
135 force_all_opaque(copy);
136 return SkImageEncoder::EncodeFile(path.c_str(), copy,
137 SkImageEncoder::kPNG_Type, 100);
138}
139
reed@google.com3d3f0922010-12-20 21:10:29 +0000140static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000141 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
142 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
143 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
144 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000145}
146
147static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000148 SkBitmap* diff) {
149 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000150
151 const int w = target.width();
152 const int h = target.height();
153 for (int y = 0; y < h; y++) {
154 for (int x = 0; x < w; x++) {
155 SkPMColor c0 = *base.getAddr32(x, y);
156 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000157 SkPMColor d = 0;
158 if (c0 != c1) {
159 d = compute_diff_pmcolor(c0, c1);
160 }
161 *diff->getAddr32(x, y) = d;
162 }
163 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000164}
165
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000166static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
167 const SkString& name,
168 const char* renderModeDescriptor,
169 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000170 SkBitmap copy;
171 const SkBitmap* bm = &target;
172 if (target.config() != SkBitmap::kARGB_8888_Config) {
173 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
174 bm = &copy;
175 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000176 SkBitmap baseCopy;
177 const SkBitmap* bp = &base;
178 if (base.config() != SkBitmap::kARGB_8888_Config) {
179 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
180 bp = &baseCopy;
181 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000182
183 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000184 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000185
186 const int w = bm->width();
187 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000188 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000189 SkDebugf(
190"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
191 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000192 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000193 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000194 }
195
196 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000197 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000198
199 for (int y = 0; y < h; y++) {
200 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000201 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000202 SkPMColor c1 = *bm->getAddr32(x, y);
203 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000204 SkDebugf(
205"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
206 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000207
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000208 if (diff) {
209 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
210 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000211 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000212 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000213 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000214 }
215 }
216 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000217
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000218 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000219 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000220}
reed@android.com00dae862009-06-10 15:38:48 +0000221
bungeman@google.comb29c8832011-10-10 13:19:10 +0000222static bool write_document(const SkString& path,
223 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000224 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000225 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000226 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000227}
228
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000229enum Backend {
230 kRaster_Backend,
231 kGPU_Backend,
232 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000233 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000234};
235
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000236struct ConfigData {
237 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000238 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000239 const char* fName;
240};
241
242/// Returns true if processing should continue, false to skip the
243/// remainder of this config for this GM.
244//@todo thudson 22 April 2011 - could refactor this to take in
245// a factory to generate the context, always call readPixels()
246// (logically a noop for rasters, if wasted time), and thus collapse the
247// GPU special case and also let this be used for SkPicture testing.
248static void setup_bitmap(const ConfigData& gRec, SkISize& size,
249 SkBitmap* bitmap) {
250 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
251 bitmap->allocPixels();
252 bitmap->eraseColor(0);
253}
254
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000255#include "SkDrawFilter.h"
256class BWTextDrawFilter : public SkDrawFilter {
257public:
258 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
259};
260void BWTextDrawFilter::filter(SkPaint* p, Type t) {
261 if (kText_Type == t) {
262 p->setAntiAlias(false);
263 }
264}
265
266static void installFilter(SkCanvas* canvas) {
267 if (gForceBWtext) {
268 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
269 }
270}
271
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000272static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
273 if (!isPDF) {
274 canvas->setMatrix(gm->getInitialTransform());
275 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000276 installFilter(canvas);
277 gm->draw(canvas);
278 canvas->setDrawFilter(NULL);
279}
280
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000281static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
282 GrContext* context,
283 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000284 SkBitmap* bitmap,
285 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000286 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000287 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000288
289 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000290 SkCanvas* canvas;
291 if (deferred) {
292 canvas = new SkDeferredCanvas;
293 canvas->setDevice(new SkDevice(*bitmap))->unref();
294 } else {
295 canvas = new SkCanvas(*bitmap);
296 }
297 SkAutoUnref canvasUnref(canvas);
298 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000299 canvas->flush();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000300 } else { // GPU
301 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000302 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000303 }
junov@google.com4370aed2012-01-18 16:21:08 +0000304 SkCanvas* gc;
305 if (deferred) {
306 gc = new SkDeferredCanvas;
307 } else {
308 gc = new SkGpuCanvas(context, rt);
309 }
310 SkAutoUnref gcUnref(gc);
311 gc->setDevice(new SkGpuDevice(context, rt))->unref();
312 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000313 // the device is as large as the current rendertarget, so we explicitly
314 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000315 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000316 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
317 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000318 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000319 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000320 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000321}
322
323static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
324 SkPicture* pict, SkBitmap* bitmap) {
325 SkISize size = gm->getISize();
326 setup_bitmap(gRec, size, bitmap);
327 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000328 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000329 canvas.drawPicture(*pict);
330}
331
332static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
333#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000334 SkMatrix initialTransform = gm->getInitialTransform();
335 SkISize pageSize = gm->getISize();
336 SkPDFDevice* dev = NULL;
337 if (initialTransform.isIdentity()) {
338 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
339 } else {
340 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
341 SkIntToScalar(pageSize.height()));
342 initialTransform.mapRect(&content);
343 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
344 SkIntToScalar(pageSize.height()));
345 SkISize contentSize =
346 SkISize::Make(SkScalarRoundToInt(content.width()),
347 SkScalarRoundToInt(content.height()));
348 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
349 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000350 SkAutoUnref aur(dev);
351
352 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000353 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000354
355 SkPDFDocument doc;
356 doc.appendPage(dev);
357 doc.emitPDF(&pdf);
358#endif
359}
360
bungeman@google.comb29c8832011-10-10 13:19:10 +0000361static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
362#ifdef SK_SUPPORT_XPS
363 SkISize size = gm->getISize();
364
365 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
366 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000367 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
368 static const SkScalar upm = 72 * inchesPerMeter;
369 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
370 static const SkScalar ppm = 200 * inchesPerMeter;
371 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000372
373 SkXPSDevice* dev = new SkXPSDevice();
374 SkAutoUnref aur(dev);
375
376 SkCanvas c(dev);
377 dev->beginPortfolio(&xps);
378 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000379 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000380 dev->endSheet();
381 dev->endPortfolio();
382
383#endif
384}
385
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000386static ErrorBitfield write_reference_image(const ConfigData& gRec,
387 const char writePath [],
388 const char renderModeDescriptor [],
389 const SkString& name,
390 SkBitmap& bitmap,
391 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000392 SkString path;
393 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000394 if (gRec.fBackend == kRaster_Backend ||
395 gRec.fBackend == kGPU_Backend ||
396 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
397
tomhudson@google.comea325432011-06-09 20:30:03 +0000398 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000399 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000400 }
401 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000402 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000403 success = write_document(path, *document);
404 }
405 if (kXPS_Backend == gRec.fBackend) {
406 path = make_filename(writePath, renderModeDescriptor, name, "xps");
407 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000408 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000409 if (success) {
410 return ERROR_NONE;
411 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000412 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000413 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000414 }
415}
416
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000417static ErrorBitfield compare_to_reference_image(const SkString& name,
418 SkBitmap &bitmap,
419 const SkBitmap& comparisonBitmap,
420 const char diffPath [],
421 const char renderModeDescriptor []) {
422 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000423 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000424 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
425 diffPath ? &diffBitmap : NULL);
426 if ((ERROR_NONE == errors) && diffPath) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000427 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000428 if (!write_bitmap(diffName, diffBitmap)) {
429 errors |= ERROR_WRITING_REFERENCE_IMAGE;
430 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000431 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000432 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000433}
434
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000435static ErrorBitfield compare_to_reference_image(const char readPath [],
436 const SkString& name,
437 SkBitmap &bitmap,
438 const char diffPath [],
439 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000440 SkString path = make_filename(readPath, "", name, "png");
441 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000442 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
443 SkBitmap::kARGB_8888_Config,
444 SkImageDecoder::kDecodePixels_Mode, NULL)) {
445 return compare_to_reference_image(name, bitmap,
446 orig, diffPath,
447 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000448 } else {
449 fprintf(stderr, "FAILED to read %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000450 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000451 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000452}
453
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000454static ErrorBitfield handle_test_results(GM* gm,
455 const ConfigData& gRec,
456 const char writePath [],
457 const char readPath [],
458 const char diffPath [],
459 const char renderModeDescriptor [],
460 SkBitmap& bitmap,
461 SkDynamicMemoryWStream* pdf,
462 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000463 SkString name = make_name(gm->shortName(), gRec.fName);
464
465 if (writePath) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000466 return write_reference_image(gRec, writePath, renderModeDescriptor,
467 name, bitmap, pdf);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000468 } else if (readPath && (
469 gRec.fBackend == kRaster_Backend ||
470 gRec.fBackend == kGPU_Backend ||
471 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000472 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000473 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000474 } else if (comparisonBitmap) {
475 return compare_to_reference_image(name, bitmap,
476 *comparisonBitmap, diffPath,
477 renderModeDescriptor);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000478 } else {
479 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000480 }
481}
482
483static SkPicture* generate_new_picture(GM* gm) {
484 // Pictures are refcounted so must be on heap
485 SkPicture* pict = new SkPicture;
486 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000487 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000488 pict->endRecording();
489
490 return pict;
491}
492
493static SkPicture* stream_to_new_picture(const SkPicture& src) {
494
495 // To do in-memory commiunications with a stream, we need to:
496 // * create a dynamic memory stream
497 // * copy it into a buffer
498 // * create a read stream from it
499 // ?!?!
500
501 SkDynamicMemoryWStream storage;
502 src.serialize(&storage);
503
504 int streamSize = storage.getOffset();
505 SkAutoMalloc dstStorage(streamSize);
506 void* dst = dstStorage.get();
507 //char* dst = new char [streamSize];
508 //@todo thudson 22 April 2011 when can we safely delete [] dst?
509 storage.copyTo(dst);
510 SkMemoryStream pictReadback(dst, streamSize);
511 SkPicture* retval = new SkPicture (&pictReadback);
512 return retval;
513}
514
515// Test: draw into a bitmap or pdf.
516// Depending on flags, possibly compare to an expected image
517// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000518static ErrorBitfield test_drawing(GM* gm,
519 const ConfigData& gRec,
520 const char writePath [],
521 const char readPath [],
522 const char diffPath [],
523 GrContext* context,
524 GrRenderTarget* rt,
525 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000526 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000527
528 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000529 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000530 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000531 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
532 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000533 if (ERROR_NONE != errors) {
534 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000535 }
reed@google.com46cce912011-06-29 12:54:46 +0000536 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000537 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000538#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000539 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000540 SkMemoryStream stream(data.data(), data.size());
541 SkPDFDocumentToBitmap(&stream, bitmap);
542#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000543 } else if (gRec.fBackend == kXPS_Backend) {
544 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000545 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000546 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000547 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000548}
549
junov@google.com4370aed2012-01-18 16:21:08 +0000550static ErrorBitfield test_deferred_drawing(GM* gm,
551 const ConfigData& gRec,
552 const SkBitmap& comparisonBitmap,
553 const char diffPath [],
554 GrContext* context,
555 GrRenderTarget* rt) {
556 SkDynamicMemoryWStream document;
557
558 if (gRec.fBackend == kRaster_Backend ||
559 gRec.fBackend == kGPU_Backend) {
560 SkBitmap bitmap;
561 // Early exit if we can't generate the image, but this is
562 // expected in some cases, so don't report a test failure.
563 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
564 return ERROR_NONE;
565 }
566 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
567 "-deferred", bitmap, NULL, &comparisonBitmap);
568 }
569 return ERROR_NONE;
570}
571
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000572static ErrorBitfield test_picture_playback(GM* gm,
573 const ConfigData& gRec,
574 const SkBitmap& comparisonBitmap,
575 const char readPath [],
576 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000577 SkPicture* pict = generate_new_picture(gm);
578 SkAutoUnref aur(pict);
579
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000580 if (kRaster_Backend == gRec.fBackend) {
581 SkBitmap bitmap;
582 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000583 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
584 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000585 } else {
586 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000587 }
588}
589
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000590static ErrorBitfield test_picture_serialization(GM* gm,
591 const ConfigData& gRec,
592 const SkBitmap& comparisonBitmap,
593 const char readPath [],
594 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000595 SkPicture* pict = generate_new_picture(gm);
596 SkAutoUnref aurp(pict);
597 SkPicture* repict = stream_to_new_picture(*pict);
598 SkAutoUnref aurr(repict);
599
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000600 if (kRaster_Backend == gRec.fBackend) {
601 SkBitmap bitmap;
602 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000603 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
604 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000605 } else {
606 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000607 }
608}
609
610static void usage(const char * argv0) {
junov@google.com77e498e2012-01-18 18:56:34 +0000611 SkDebugf(
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000612 "%s [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n"
613 " [--noreplay] [--serialize] [--forceBWtext] [--nopdf] \n"
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000614 " [--nodeferred] [--match substring] [--notexturecache]\n"
615 " "
junov@google.com77e498e2012-01-18 18:56:34 +0000616#if SK_MESA
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000617 "[--mesagl]"
junov@google.com77e498e2012-01-18 18:56:34 +0000618#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000619#if SK_ANGLE
620 " [--angle]"
621#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000622 " [--debuggl]\n\n", argv0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000623 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000624 SkDebugf(
625" readPath: directory to read reference images from;\n"
626" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000627 SkDebugf(" diffPath: directory to write difference images in.\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000628 SkDebugf(" resourcePath: directory that stores image resources.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000629 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000630 SkDebugf(
631" --serialize: exercise SkPicture serialization & deserialization.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000632 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
633 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
634 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000635 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000636#if SK_MESA
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000637 SkDebugf(" --mesagl: will run using the osmesa sw gl rasterizer.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000638#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000639#if SK_ANGLE
640 SkDebugf(" --angle: use ANGLE backend on Windows.\n");
641#endif
642 SkDebugf(" --debuggl: will run using the debugging gl utility.\n");
twiz@google.come24a0792012-01-31 18:35:30 +0000643 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000644}
645
646static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000647 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
648 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
649 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000650#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000651 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000652#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000653#ifdef SK_SUPPORT_PDF
654 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
655#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000656#ifdef SK_SUPPORT_XPS
657 { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" },
658#endif
reed@android.com00dae862009-06-10 15:38:48 +0000659};
660
reed@google.comb2a51622011-10-31 16:30:04 +0000661static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
662 if (0 == array.count()) {
663 // no names, so don't skip anything
664 return false;
665 }
666 for (int i = 0; i < array.count(); ++i) {
667 if (strstr(name, array[i])) {
668 // found the name, so don't skip
669 return false;
670 }
671 }
672 return true;
673}
674
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000675namespace skiagm {
676static GrContext* gGrContext;
677GrContext* GetGr() {
678 return gGrContext;
679}
680}
681
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000682int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000683 SkAutoGraphics ag;
reed@google.com8923c6c2011-11-08 14:59:38 +0000684 // we don't need to see this during a run
685 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000686
epoger@google.com7bc13a62012-02-14 14:53:59 +0000687 setSystemPreferences();
688
reed@android.com8015dd82009-06-21 00:49:18 +0000689 const char* writePath = NULL; // if non-null, where we write the originals
690 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000691 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000692 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000693
reed@google.comb2a51622011-10-31 16:30:04 +0000694 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000695
reed@google.comab973972011-09-19 19:01:38 +0000696 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000697 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000698 bool doSerialize = false;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000699#if SK_MESA
bsalomon@google.com373a6632011-10-19 20:43:20 +0000700 bool useMesa = false;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000701#endif
702#if SK_ANGLE
703 bool useAngle = false;
704#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000705 bool useDebugGL = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000706 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000707 bool disableTextureCache = false;
708
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000709 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000710 char* const* stop = argv + argc;
711 for (++argv; argv < stop; ++argv) {
712 if (strcmp(*argv, "-w") == 0) {
713 argv++;
714 if (argv < stop && **argv) {
715 writePath = *argv;
716 }
717 } else if (strcmp(*argv, "-r") == 0) {
718 argv++;
719 if (argv < stop && **argv) {
720 readPath = *argv;
721 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000722 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000723 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000724 if (argv < stop && **argv) {
725 diffPath = *argv;
726 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000727 } else if (strcmp(*argv, "-i") == 0) {
728 argv++;
729 if (argv < stop && **argv) {
730 resourcePath = *argv;
731 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000732 } else if (strcmp(*argv, "--forceBWtext") == 0) {
733 gForceBWtext = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000734 } else if (strcmp(*argv, "--noreplay") == 0) {
735 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000736 } else if (strcmp(*argv, "--nopdf") == 0) {
737 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000738 } else if (strcmp(*argv, "--nodeferred") == 0) {
739 doDeferred = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000740 } else if (strcmp(*argv, "--serialize") == 0) {
741 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000742 } else if (strcmp(*argv, "--match") == 0) {
743 ++argv;
744 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000745 // just record the ptr, no need for a deep copy
746 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000747 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000748#if SK_MESA
749 } else if (strcmp(*argv, "--mesagl") == 0) {
750 useMesa = true;
751#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000752#if SK_ANGLE
753 } else if (strcmp(*argv, "--angle") == 0) {
754 useAngle = true;
755#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000756 } else if (strcmp(*argv, "--debuggl") == 0) {
757 useDebugGL = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000758 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000759 disableTextureCache = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000760 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000761 usage(commandName);
762 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000763 }
764 }
765 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000766 usage(commandName);
767 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000768 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000769
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000770 GM::SetResourcePath(resourcePath);
771
bsalomon@google.com39149582011-06-13 21:55:32 +0000772 int maxW = -1;
773 int maxH = -1;
774 Iter iter;
775 GM* gm;
776 while ((gm = iter.next()) != NULL) {
777 SkISize size = gm->getISize();
778 maxW = SkMax32(size.width(), maxW);
779 maxH = SkMax32(size.height(), maxH);
tomhudson@google.com7816a4e2012-03-15 13:39:51 +0000780 // This fixes a memory leak, but we are churning gms; we could
781 // instead cache them if we have constructors with side-effects.
782 SkDELETE(gm);
bsalomon@google.com39149582011-06-13 21:55:32 +0000783 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000784 // setup a GL context for drawing offscreen
bsalomon@google.com373a6632011-10-19 20:43:20 +0000785 SkAutoTUnref<SkGLContext> glContext;
786#if SK_MESA
787 if (useMesa) {
788 glContext.reset(new SkMesaGLContext());
789 } else
790#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000791#if SK_ANGLE
792 if (useAngle) {
793 glContext.reset(new SkANGLEGLContext());
794 } else
795#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000796 if (useDebugGL) {
797 glContext.reset(new SkDebugGLContext());
798 } else {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000799 glContext.reset(new SkNativeGLContext());
800 }
801
bsalomon@google.com29d35012011-11-30 16:57:21 +0000802 GrPlatformRenderTargetDesc rtDesc;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000803 if (glContext.get()->init(maxW, maxH)) {
804 GrPlatform3DContext ctx =
805 reinterpret_cast<GrPlatform3DContext>(glContext.get()->gl());
806 gGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000807 if (NULL != gGrContext) {
bsalomon@google.com29d35012011-11-30 16:57:21 +0000808 rtDesc.fConfig = kSkia8888_PM_GrPixelConfig;
809 rtDesc.fStencilBits = 8;
810 rtDesc.fRenderTargetHandle = glContext.get()->getFBOID();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000811 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000812 } else {
813 fprintf(stderr, "could not create GL context.\n");
reed@google.com37df17d2010-12-23 20:20:51 +0000814 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000815
reed@android.com00f883e2010-12-14 17:46:14 +0000816 if (readPath) {
817 fprintf(stderr, "reading from %s\n", readPath);
818 } else if (writePath) {
819 fprintf(stderr, "writing to %s\n", writePath);
820 }
821
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000822 if (resourcePath) {
823 fprintf(stderr, "reading resources from %s\n", resourcePath);
824 }
825
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000826 // Accumulate success of all tests.
827 int testsRun = 0;
828 int testsPassed = 0;
829 int testsFailed = 0;
830 int testsMissingReferenceImages = 0;
831
twiz@google.come24a0792012-01-31 18:35:30 +0000832 if (disableTextureCache) {
833 skiagm::GetGr()->setTextureCacheLimits(0, 0);
834 }
835
bsalomon@google.com39149582011-06-13 21:55:32 +0000836 iter.reset();
reed@android.com00dae862009-06-10 15:38:48 +0000837 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000838 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000839 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000840 SkDELETE(gm);
841 continue;
842 }
843
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000844 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000845 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000846 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000847 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000848
bsalomon@google.com29d35012011-11-30 16:57:21 +0000849 // Above we created an fbo for the context at maxW x maxH size.
850 // Here we lie about the size of the rt. We claim it is the size
851 // desired by the test. The reason is that rasterization may change
852 // slightly when the viewport dimensions change. Previously, whenever
853 // a new test was checked in that bumped maxW or maxH several images
854 // would slightly change.
855 rtDesc.fWidth = size.width();
856 rtDesc.fHeight = size.height();
857 SkAutoTUnref<GrRenderTarget> rt;
858 if (gGrContext) {
859 rt.reset(gGrContext->createPlatformRenderTarget(rtDesc));
860 }
reed@google.comfbc21172011-09-19 19:08:33 +0000861
bsalomon@google.com29d35012011-11-30 16:57:21 +0000862 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000863 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000864 uint32_t gmFlags = gm->getFlags();
twiz@google.come24a0792012-01-31 18:35:30 +0000865 if ((kPDF_Backend == gRec[i].fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +0000866 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
867 {
reed@google.comab973972011-09-19 19:01:38 +0000868 continue;
869 }
870
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000871 // Now we know that we want to run this test and record its
872 // success or failure.
873 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000874
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000875 if ((ERROR_NONE == testErrors) &&
876 (kGPU_Backend == gRec[i].fBackend) &&
877 (NULL == rt.get())) {
878 fprintf(stderr, "Could not create render target for gpu.\n");
879 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000880 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000881
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000882 if (ERROR_NONE == testErrors) {
883 testErrors |= test_drawing(gm, gRec[i],
884 writePath, readPath, diffPath,
885 gGrContext,
886 rt.get(), &forwardRenderedBitmap);
887 }
888
junov@google.com4370aed2012-01-18 16:21:08 +0000889 if (doDeferred && !testErrors &&
twiz@google.come24a0792012-01-31 18:35:30 +0000890 (kGPU_Backend == gRec[i].fBackend ||
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000891 kRaster_Backend == gRec[i].fBackend)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000892 testErrors |= test_deferred_drawing(gm, gRec[i],
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000893 forwardRenderedBitmap,
894 diffPath, gGrContext, rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +0000895 }
896
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000897 if ((ERROR_NONE == testErrors) && doReplay &&
898 !(gmFlags & GM::kSkipPicture_Flag)) {
899 testErrors |= test_picture_playback(gm, gRec[i],
900 forwardRenderedBitmap,
901 readPath, diffPath);
902 }
903
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000904 if ((ERROR_NONE == testErrors) && doSerialize &&
905 !(gmFlags & GM::kSkipPicture_Flag)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000906 testErrors |= test_picture_serialization(gm, gRec[i],
907 forwardRenderedBitmap,
908 readPath, diffPath);
909 }
910
911 // Update overall results.
912 // We only tabulate the particular error types that we currently
913 // care about (e.g., missing reference images). Later on, if we
914 // want to also tabulate pixel mismatches vs dimension mistmatches
915 // (or whatever else), we can do so.
916 testsRun++;
917 if (ERROR_NONE == testErrors) {
918 testsPassed++;
919 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
920 testsMissingReferenceImages++;
921 } else {
922 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000923 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000924 }
reed@android.com00dae862009-06-10 15:38:48 +0000925 SkDELETE(gm);
926 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000927 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
928 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000929
930 SkDELETE(skiagm::gGrContext);
931 skiagm::gGrContext = NULL;
932
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000933 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +0000934}