blob: 7d47bbe26dde41dac9eba8339cb8339a8ebdee30 [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);
reed@google.com8e529b72012-04-09 20:20:10 +0000426 if ((ERROR_NONE != errors) && diffPath) {
427 // write out the generated image
428 SkString genName = make_filename(diffPath, "", name, "png");
429 if (!write_bitmap(genName, bitmap)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000430 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);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000465 ErrorBitfield retval = ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000466
epoger@google.com9284ccd2012-04-18 13:36:54 +0000467 if (readPath && (
bungeman@google.comb29c8832011-10-10 13:19:10 +0000468 gRec.fBackend == kRaster_Backend ||
469 gRec.fBackend == kGPU_Backend ||
470 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) {
epoger@google.com9284ccd2012-04-18 13:36:54 +0000471 retval |= compare_to_reference_image(readPath, name, bitmap,
472 diffPath, renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000473 }
epoger@google.com9284ccd2012-04-18 13:36:54 +0000474 if (writePath) {
475 retval |= write_reference_image(gRec, writePath, renderModeDescriptor,
476 name, bitmap, pdf);
477 }
478 if (comparisonBitmap) {
479 retval |= compare_to_reference_image(name, bitmap,
480 *comparisonBitmap, diffPath,
481 renderModeDescriptor);
482 }
483 return retval;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000484}
485
486static SkPicture* generate_new_picture(GM* gm) {
487 // Pictures are refcounted so must be on heap
488 SkPicture* pict = new SkPicture;
489 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000490 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000491 pict->endRecording();
492
493 return pict;
494}
495
496static SkPicture* stream_to_new_picture(const SkPicture& src) {
497
498 // To do in-memory commiunications with a stream, we need to:
499 // * create a dynamic memory stream
500 // * copy it into a buffer
501 // * create a read stream from it
502 // ?!?!
503
504 SkDynamicMemoryWStream storage;
505 src.serialize(&storage);
506
507 int streamSize = storage.getOffset();
508 SkAutoMalloc dstStorage(streamSize);
509 void* dst = dstStorage.get();
510 //char* dst = new char [streamSize];
511 //@todo thudson 22 April 2011 when can we safely delete [] dst?
512 storage.copyTo(dst);
513 SkMemoryStream pictReadback(dst, streamSize);
514 SkPicture* retval = new SkPicture (&pictReadback);
515 return retval;
516}
517
518// Test: draw into a bitmap or pdf.
519// Depending on flags, possibly compare to an expected image
520// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000521static ErrorBitfield test_drawing(GM* gm,
522 const ConfigData& gRec,
523 const char writePath [],
524 const char readPath [],
525 const char diffPath [],
526 GrContext* context,
527 GrRenderTarget* rt,
528 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000529 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000530
531 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000532 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000533 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000534 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
535 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000536 if (ERROR_NONE != errors) {
537 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000538 }
reed@google.com46cce912011-06-29 12:54:46 +0000539 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000540 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000541#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000542 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000543 SkMemoryStream stream(data.data(), data.size());
544 SkPDFDocumentToBitmap(&stream, bitmap);
545#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000546 } else if (gRec.fBackend == kXPS_Backend) {
547 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000548 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000549 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000550 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000551}
552
junov@google.com4370aed2012-01-18 16:21:08 +0000553static ErrorBitfield test_deferred_drawing(GM* gm,
554 const ConfigData& gRec,
555 const SkBitmap& comparisonBitmap,
556 const char diffPath [],
557 GrContext* context,
558 GrRenderTarget* rt) {
559 SkDynamicMemoryWStream document;
560
561 if (gRec.fBackend == kRaster_Backend ||
562 gRec.fBackend == kGPU_Backend) {
563 SkBitmap bitmap;
564 // Early exit if we can't generate the image, but this is
565 // expected in some cases, so don't report a test failure.
566 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
567 return ERROR_NONE;
568 }
569 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
570 "-deferred", bitmap, NULL, &comparisonBitmap);
571 }
572 return ERROR_NONE;
573}
574
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000575static ErrorBitfield test_picture_playback(GM* gm,
576 const ConfigData& gRec,
577 const SkBitmap& comparisonBitmap,
578 const char readPath [],
579 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000580 SkPicture* pict = generate_new_picture(gm);
581 SkAutoUnref aur(pict);
582
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000583 if (kRaster_Backend == gRec.fBackend) {
584 SkBitmap bitmap;
585 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000586 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
587 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000588 } else {
589 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000590 }
591}
592
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000593static ErrorBitfield test_picture_serialization(GM* gm,
594 const ConfigData& gRec,
595 const SkBitmap& comparisonBitmap,
596 const char readPath [],
597 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000598 SkPicture* pict = generate_new_picture(gm);
599 SkAutoUnref aurp(pict);
600 SkPicture* repict = stream_to_new_picture(*pict);
601 SkAutoUnref aurr(repict);
602
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000603 if (kRaster_Backend == gRec.fBackend) {
604 SkBitmap bitmap;
605 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000606 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
607 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000608 } else {
609 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000610 }
611}
612
613static void usage(const char * argv0) {
junov@google.com77e498e2012-01-18 18:56:34 +0000614 SkDebugf(
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000615 "%s [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n"
616 " [--noreplay] [--serialize] [--forceBWtext] [--nopdf] \n"
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000617 " [--nodeferred] [--match substring] [--notexturecache]\n"
618 " "
junov@google.com77e498e2012-01-18 18:56:34 +0000619#if SK_MESA
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000620 "[--mesagl]"
junov@google.com77e498e2012-01-18 18:56:34 +0000621#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000622#if SK_ANGLE
623 " [--angle]"
624#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000625 " [--debuggl]\n\n", argv0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000626 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000627 SkDebugf(
628" readPath: directory to read reference images from;\n"
629" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000630 SkDebugf(" diffPath: directory to write difference images in.\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000631 SkDebugf(" resourcePath: directory that stores image resources.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000632 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000633 SkDebugf(
634" --serialize: exercise SkPicture serialization & deserialization.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000635 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
636 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
637 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000638 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000639#if SK_MESA
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000640 SkDebugf(" --mesagl: will run using the osmesa sw gl rasterizer.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000641#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000642#if SK_ANGLE
643 SkDebugf(" --angle: use ANGLE backend on Windows.\n");
644#endif
645 SkDebugf(" --debuggl: will run using the debugging gl utility.\n");
twiz@google.come24a0792012-01-31 18:35:30 +0000646 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000647}
648
649static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000650 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
651 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
652 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000653#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000654 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000655#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000656#ifdef SK_SUPPORT_PDF
657 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
658#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000659#ifdef SK_SUPPORT_XPS
660 { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" },
661#endif
reed@android.com00dae862009-06-10 15:38:48 +0000662};
663
reed@google.comb2a51622011-10-31 16:30:04 +0000664static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
665 if (0 == array.count()) {
666 // no names, so don't skip anything
667 return false;
668 }
669 for (int i = 0; i < array.count(); ++i) {
670 if (strstr(name, array[i])) {
671 // found the name, so don't skip
672 return false;
673 }
674 }
675 return true;
676}
677
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000678namespace skiagm {
679static GrContext* gGrContext;
680GrContext* GetGr() {
681 return gGrContext;
682}
683}
684
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000685int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000686 SkAutoGraphics ag;
reed@google.com8923c6c2011-11-08 14:59:38 +0000687 // we don't need to see this during a run
688 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000689
epoger@google.com7bc13a62012-02-14 14:53:59 +0000690 setSystemPreferences();
691
reed@android.com8015dd82009-06-21 00:49:18 +0000692 const char* writePath = NULL; // if non-null, where we write the originals
693 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000694 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000695 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000696
reed@google.comb2a51622011-10-31 16:30:04 +0000697 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000698
reed@google.comab973972011-09-19 19:01:38 +0000699 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000700 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000701 bool doSerialize = false;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000702#if SK_MESA
bsalomon@google.com373a6632011-10-19 20:43:20 +0000703 bool useMesa = false;
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000704#endif
705#if SK_ANGLE
706 bool useAngle = false;
707#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000708 bool useDebugGL = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000709 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000710 bool disableTextureCache = false;
711
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000712 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000713 char* const* stop = argv + argc;
714 for (++argv; argv < stop; ++argv) {
715 if (strcmp(*argv, "-w") == 0) {
716 argv++;
717 if (argv < stop && **argv) {
718 writePath = *argv;
719 }
720 } else if (strcmp(*argv, "-r") == 0) {
721 argv++;
722 if (argv < stop && **argv) {
723 readPath = *argv;
724 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000725 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000726 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000727 if (argv < stop && **argv) {
728 diffPath = *argv;
729 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000730 } else if (strcmp(*argv, "-i") == 0) {
731 argv++;
732 if (argv < stop && **argv) {
733 resourcePath = *argv;
734 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000735 } else if (strcmp(*argv, "--forceBWtext") == 0) {
736 gForceBWtext = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000737 } else if (strcmp(*argv, "--noreplay") == 0) {
738 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000739 } else if (strcmp(*argv, "--nopdf") == 0) {
740 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000741 } else if (strcmp(*argv, "--nodeferred") == 0) {
742 doDeferred = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000743 } else if (strcmp(*argv, "--serialize") == 0) {
744 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000745 } else if (strcmp(*argv, "--match") == 0) {
746 ++argv;
747 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000748 // just record the ptr, no need for a deep copy
749 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000750 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000751#if SK_MESA
752 } else if (strcmp(*argv, "--mesagl") == 0) {
753 useMesa = true;
754#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000755#if SK_ANGLE
756 } else if (strcmp(*argv, "--angle") == 0) {
757 useAngle = true;
758#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000759 } else if (strcmp(*argv, "--debuggl") == 0) {
760 useDebugGL = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000761 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000762 disableTextureCache = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000763 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000764 usage(commandName);
765 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000766 }
767 }
768 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000769 usage(commandName);
770 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000771 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000772
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000773 GM::SetResourcePath(resourcePath);
774
bsalomon@google.com39149582011-06-13 21:55:32 +0000775 int maxW = -1;
776 int maxH = -1;
777 Iter iter;
778 GM* gm;
779 while ((gm = iter.next()) != NULL) {
780 SkISize size = gm->getISize();
781 maxW = SkMax32(size.width(), maxW);
782 maxH = SkMax32(size.height(), maxH);
tomhudson@google.com7816a4e2012-03-15 13:39:51 +0000783 // This fixes a memory leak, but we are churning gms; we could
784 // instead cache them if we have constructors with side-effects.
785 SkDELETE(gm);
bsalomon@google.com39149582011-06-13 21:55:32 +0000786 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000787 // setup a GL context for drawing offscreen
bsalomon@google.com373a6632011-10-19 20:43:20 +0000788 SkAutoTUnref<SkGLContext> glContext;
789#if SK_MESA
790 if (useMesa) {
791 glContext.reset(new SkMesaGLContext());
792 } else
793#endif
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000794#if SK_ANGLE
795 if (useAngle) {
796 glContext.reset(new SkANGLEGLContext());
797 } else
798#endif
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000799 if (useDebugGL) {
800 glContext.reset(new SkDebugGLContext());
801 } else {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000802 glContext.reset(new SkNativeGLContext());
803 }
804
bsalomon@google.com29d35012011-11-30 16:57:21 +0000805 GrPlatformRenderTargetDesc rtDesc;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000806 if (glContext.get()->init(maxW, maxH)) {
807 GrPlatform3DContext ctx =
808 reinterpret_cast<GrPlatform3DContext>(glContext.get()->gl());
809 gGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000810 if (NULL != gGrContext) {
bsalomon@google.com29d35012011-11-30 16:57:21 +0000811 rtDesc.fConfig = kSkia8888_PM_GrPixelConfig;
812 rtDesc.fStencilBits = 8;
813 rtDesc.fRenderTargetHandle = glContext.get()->getFBOID();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000814 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000815 } else {
816 fprintf(stderr, "could not create GL context.\n");
reed@google.com37df17d2010-12-23 20:20:51 +0000817 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000818
reed@android.com00f883e2010-12-14 17:46:14 +0000819 if (readPath) {
820 fprintf(stderr, "reading from %s\n", readPath);
epoger@google.com9284ccd2012-04-18 13:36:54 +0000821 }
822 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +0000823 fprintf(stderr, "writing to %s\n", writePath);
824 }
825
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000826 if (resourcePath) {
827 fprintf(stderr, "reading resources from %s\n", resourcePath);
828 }
829
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000830 // Accumulate success of all tests.
831 int testsRun = 0;
832 int testsPassed = 0;
833 int testsFailed = 0;
834 int testsMissingReferenceImages = 0;
835
twiz@google.come24a0792012-01-31 18:35:30 +0000836 if (disableTextureCache) {
837 skiagm::GetGr()->setTextureCacheLimits(0, 0);
838 }
839
bsalomon@google.com39149582011-06-13 21:55:32 +0000840 iter.reset();
reed@android.com00dae862009-06-10 15:38:48 +0000841 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000842 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000843 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000844 SkDELETE(gm);
845 continue;
846 }
847
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000848 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000849 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000850 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000851 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000852
bsalomon@google.com29d35012011-11-30 16:57:21 +0000853 // Above we created an fbo for the context at maxW x maxH size.
854 // Here we lie about the size of the rt. We claim it is the size
855 // desired by the test. The reason is that rasterization may change
856 // slightly when the viewport dimensions change. Previously, whenever
857 // a new test was checked in that bumped maxW or maxH several images
858 // would slightly change.
859 rtDesc.fWidth = size.width();
860 rtDesc.fHeight = size.height();
861 SkAutoTUnref<GrRenderTarget> rt;
862 if (gGrContext) {
863 rt.reset(gGrContext->createPlatformRenderTarget(rtDesc));
864 }
reed@google.comfbc21172011-09-19 19:08:33 +0000865
bsalomon@google.com29d35012011-11-30 16:57:21 +0000866 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000867 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000868 uint32_t gmFlags = gm->getFlags();
twiz@google.come24a0792012-01-31 18:35:30 +0000869 if ((kPDF_Backend == gRec[i].fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +0000870 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
871 {
reed@google.comab973972011-09-19 19:01:38 +0000872 continue;
873 }
874
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000875 // Now we know that we want to run this test and record its
876 // success or failure.
877 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000878
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000879 if ((ERROR_NONE == testErrors) &&
880 (kGPU_Backend == gRec[i].fBackend) &&
881 (NULL == rt.get())) {
882 fprintf(stderr, "Could not create render target for gpu.\n");
883 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000884 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000885
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000886 if (ERROR_NONE == testErrors) {
887 testErrors |= test_drawing(gm, gRec[i],
888 writePath, readPath, diffPath,
889 gGrContext,
890 rt.get(), &forwardRenderedBitmap);
891 }
892
junov@google.com4370aed2012-01-18 16:21:08 +0000893 if (doDeferred && !testErrors &&
twiz@google.come24a0792012-01-31 18:35:30 +0000894 (kGPU_Backend == gRec[i].fBackend ||
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000895 kRaster_Backend == gRec[i].fBackend)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000896 testErrors |= test_deferred_drawing(gm, gRec[i],
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000897 forwardRenderedBitmap,
898 diffPath, gGrContext, rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +0000899 }
900
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000901 if ((ERROR_NONE == testErrors) && doReplay &&
902 !(gmFlags & GM::kSkipPicture_Flag)) {
903 testErrors |= test_picture_playback(gm, gRec[i],
904 forwardRenderedBitmap,
905 readPath, diffPath);
906 }
907
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000908 if ((ERROR_NONE == testErrors) && doSerialize &&
909 !(gmFlags & GM::kSkipPicture_Flag)) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000910 testErrors |= test_picture_serialization(gm, gRec[i],
911 forwardRenderedBitmap,
912 readPath, diffPath);
913 }
914
915 // Update overall results.
916 // We only tabulate the particular error types that we currently
917 // care about (e.g., missing reference images). Later on, if we
918 // want to also tabulate pixel mismatches vs dimension mistmatches
919 // (or whatever else), we can do so.
920 testsRun++;
921 if (ERROR_NONE == testErrors) {
922 testsPassed++;
923 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
924 testsMissingReferenceImages++;
925 } else {
926 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000927 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000928 }
reed@android.com00dae862009-06-10 15:38:48 +0000929 SkDELETE(gm);
930 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000931 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
932 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
robertphillips@google.comf6f123d2012-03-21 17:57:55 +0000933
934 SkDELETE(skiagm::gGrContext);
935 skiagm::gGrContext = NULL;
936
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000937 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +0000938}