blob: 1e63bb6d7733271279893cf1be7260f99e4a3601 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
bsalomon@google.com971d0c82011-08-19 17:22:05 +00007
bungeman@google.comb29c8832011-10-10 13:19:10 +00008#include "gm.h"
epoger@google.com7bc13a62012-02-14 14:53:59 +00009#include "system_preferences.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000010#include "GrContext.h"
11#include "GrRenderTarget.h"
12
reed@android.comb9b9a182009-07-08 02:54:47 +000013#include "SkColorPriv.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000014#include "SkData.h"
junov@google.com4370aed2012-01-18 16:21:08 +000015#include "SkDeferredCanvas.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000016#include "SkDevice.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000017#include "SkGpuCanvas.h"
18#include "SkGpuDevice.h"
reed@android.com8015dd82009-06-21 00:49:18 +000019#include "SkGraphics.h"
20#include "SkImageDecoder.h"
21#include "SkImageEncoder.h"
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000022#include "gl/SkNativeGLContext.h"
23#include "gl/SkMesaGLContext.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000024#include "SkPicture.h"
reed@google.com07700442010-12-20 19:46:07 +000025#include "SkStream.h"
26#include "SkRefCnt.h"
27
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000028static bool gForceBWtext;
29
reed@google.com8923c6c2011-11-08 14:59:38 +000030extern bool gSkSuppressFontCachePurgeSpew;
31
reed@google.com07700442010-12-20 19:46:07 +000032#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000033 #include "SkPDFDevice.h"
34 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000035#endif
reed@android.com00dae862009-06-10 15:38:48 +000036
epoger@google.come3cc2eb2012-01-18 20:11:13 +000037// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
38// stop writing out XPS-format image baselines in gm.
39#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000040#ifdef SK_SUPPORT_XPS
41 #include "SkXPSDevice.h"
42#endif
43
reed@google.com46cce912011-06-29 12:54:46 +000044#ifdef SK_BUILD_FOR_MAC
45 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000046 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000047#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000048 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000049#endif
50
epoger@google.comc7cf2b32011-12-28 19:31:01 +000051typedef int ErrorBitfield;
52const static ErrorBitfield ERROR_NONE = 0x00;
53const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
54const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
55const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
56const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
57const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
58
reed@android.com00dae862009-06-10 15:38:48 +000059using namespace skiagm;
60
reed@android.com00dae862009-06-10 15:38:48 +000061class Iter {
62public:
63 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000064 this->reset();
65 }
66
67 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000068 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000069 }
reed@google.comd4dfd102011-01-18 21:05:42 +000070
reed@android.comdd0ac282009-06-20 02:38:16 +000071 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000072 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000073 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000074 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000075 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000076 }
77 return NULL;
78 }
reed@google.comd4dfd102011-01-18 21:05:42 +000079
reed@android.com00dae862009-06-10 15:38:48 +000080 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000081 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000082 int count = 0;
83 while (reg) {
84 count += 1;
85 reg = reg->next();
86 }
87 return count;
88 }
reed@google.comd4dfd102011-01-18 21:05:42 +000089
reed@android.com00dae862009-06-10 15:38:48 +000090private:
91 const GMRegistry* fReg;
92};
93
reed@android.com8015dd82009-06-21 00:49:18 +000094static SkString make_name(const char shortName[], const char configName[]) {
95 SkString name(shortName);
96 name.appendf("_%s", configName);
97 return name;
98}
99
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000100static SkString make_filename(const char path[],
101 const char pathSuffix[],
102 const SkString& name,
103 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +0000104 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000105 if (filename.endsWith("/")) {
106 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000107 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000108 filename.append(pathSuffix);
109 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000110 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000111 return filename;
112}
113
reed@android.comb9b9a182009-07-08 02:54:47 +0000114/* since PNG insists on unpremultiplying our alpha, we take no precision chances
115 and force all pixels to be 100% opaque, otherwise on compare we may not get
116 a perfect match.
117 */
118static void force_all_opaque(const SkBitmap& bitmap) {
119 SkAutoLockPixels lock(bitmap);
120 for (int y = 0; y < bitmap.height(); y++) {
121 for (int x = 0; x < bitmap.width(); x++) {
122 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
123 }
124 }
125}
126
127static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
128 SkBitmap copy;
129 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
130 force_all_opaque(copy);
131 return SkImageEncoder::EncodeFile(path.c_str(), copy,
132 SkImageEncoder::kPNG_Type, 100);
133}
134
reed@google.com3d3f0922010-12-20 21:10:29 +0000135static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000136 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
137 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
138 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
139 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000140}
141
142static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000143 SkBitmap* diff) {
144 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000145
146 const int w = target.width();
147 const int h = target.height();
148 for (int y = 0; y < h; y++) {
149 for (int x = 0; x < w; x++) {
150 SkPMColor c0 = *base.getAddr32(x, y);
151 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000152 SkPMColor d = 0;
153 if (c0 != c1) {
154 d = compute_diff_pmcolor(c0, c1);
155 }
156 *diff->getAddr32(x, y) = d;
157 }
158 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000159}
160
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000161static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
162 const SkString& name,
163 const char* renderModeDescriptor,
164 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000165 SkBitmap copy;
166 const SkBitmap* bm = &target;
167 if (target.config() != SkBitmap::kARGB_8888_Config) {
168 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
169 bm = &copy;
170 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000171 SkBitmap baseCopy;
172 const SkBitmap* bp = &base;
173 if (base.config() != SkBitmap::kARGB_8888_Config) {
174 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
175 bp = &baseCopy;
176 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000177
178 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000179 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000180
181 const int w = bm->width();
182 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000183 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000184 SkDebugf(
185"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
186 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000187 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000188 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000189 }
190
191 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000192 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000193
194 for (int y = 0; y < h; y++) {
195 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000196 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000197 SkPMColor c1 = *bm->getAddr32(x, y);
198 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000199 SkDebugf(
200"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
201 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000202
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000203 if (diff) {
204 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
205 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000206 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000207 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000208 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000209 }
210 }
211 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000212
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000213 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000214 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000215}
reed@android.com00dae862009-06-10 15:38:48 +0000216
bungeman@google.comb29c8832011-10-10 13:19:10 +0000217static bool write_document(const SkString& path,
218 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000219 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000220 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000221 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000222}
223
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000224enum Backend {
225 kRaster_Backend,
226 kGPU_Backend,
227 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000228 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000229};
230
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000231struct ConfigData {
232 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000233 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000234 const char* fName;
235};
236
237/// Returns true if processing should continue, false to skip the
238/// remainder of this config for this GM.
239//@todo thudson 22 April 2011 - could refactor this to take in
240// a factory to generate the context, always call readPixels()
241// (logically a noop for rasters, if wasted time), and thus collapse the
242// GPU special case and also let this be used for SkPicture testing.
243static void setup_bitmap(const ConfigData& gRec, SkISize& size,
244 SkBitmap* bitmap) {
245 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
246 bitmap->allocPixels();
247 bitmap->eraseColor(0);
248}
249
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000250#include "SkDrawFilter.h"
251class BWTextDrawFilter : public SkDrawFilter {
252public:
253 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
254};
255void BWTextDrawFilter::filter(SkPaint* p, Type t) {
256 if (kText_Type == t) {
257 p->setAntiAlias(false);
258 }
259}
260
261static void installFilter(SkCanvas* canvas) {
262 if (gForceBWtext) {
263 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
264 }
265}
266
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000267static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
268 if (!isPDF) {
269 canvas->setMatrix(gm->getInitialTransform());
270 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000271 installFilter(canvas);
272 gm->draw(canvas);
273 canvas->setDrawFilter(NULL);
274}
275
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000276static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
277 GrContext* context,
278 GrRenderTarget* rt,
junov@google.com4370aed2012-01-18 16:21:08 +0000279 SkBitmap* bitmap,
280 bool deferred) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000281 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000282 setup_bitmap(gRec, size, bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000283
284 if (gRec.fBackend == kRaster_Backend) {
junov@google.com4370aed2012-01-18 16:21:08 +0000285 SkCanvas* canvas;
286 if (deferred) {
287 canvas = new SkDeferredCanvas;
288 canvas->setDevice(new SkDevice(*bitmap))->unref();
289 } else {
290 canvas = new SkCanvas(*bitmap);
291 }
292 SkAutoUnref canvasUnref(canvas);
293 invokeGM(gm, canvas);
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000294 canvas->flush();
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000295 } else { // GPU
296 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000297 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000298 }
junov@google.com4370aed2012-01-18 16:21:08 +0000299 SkCanvas* gc;
300 if (deferred) {
301 gc = new SkDeferredCanvas;
302 } else {
303 gc = new SkGpuCanvas(context, rt);
304 }
305 SkAutoUnref gcUnref(gc);
306 gc->setDevice(new SkGpuDevice(context, rt))->unref();
307 invokeGM(gm, gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000308 // the device is as large as the current rendertarget, so we explicitly
309 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000310 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000311 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
312 size.fHeight);
junov@google.com4370aed2012-01-18 16:21:08 +0000313 gc->readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000314 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000315 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000316}
317
318static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
319 SkPicture* pict, SkBitmap* bitmap) {
320 SkISize size = gm->getISize();
321 setup_bitmap(gRec, size, bitmap);
322 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000323 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000324 canvas.drawPicture(*pict);
325}
326
327static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
328#ifdef SK_SUPPORT_PDF
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000329 SkMatrix initialTransform = gm->getInitialTransform();
330 SkISize pageSize = gm->getISize();
331 SkPDFDevice* dev = NULL;
332 if (initialTransform.isIdentity()) {
333 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
334 } else {
335 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
336 SkIntToScalar(pageSize.height()));
337 initialTransform.mapRect(&content);
338 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
339 SkIntToScalar(pageSize.height()));
340 SkISize contentSize =
341 SkISize::Make(SkScalarRoundToInt(content.width()),
342 SkScalarRoundToInt(content.height()));
343 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
344 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000345 SkAutoUnref aur(dev);
346
347 SkCanvas c(dev);
vandebo@chromium.org79d3cb42012-03-21 17:34:30 +0000348 invokeGM(gm, &c, true);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000349
350 SkPDFDocument doc;
351 doc.appendPage(dev);
352 doc.emitPDF(&pdf);
353#endif
354}
355
bungeman@google.comb29c8832011-10-10 13:19:10 +0000356static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
357#ifdef SK_SUPPORT_XPS
358 SkISize size = gm->getISize();
359
360 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
361 SkIntToScalar(size.height()));
bungeman@google.comdc9a6952012-01-05 16:56:29 +0000362 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
363 static const SkScalar upm = 72 * inchesPerMeter;
364 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
365 static const SkScalar ppm = 200 * inchesPerMeter;
366 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000367
368 SkXPSDevice* dev = new SkXPSDevice();
369 SkAutoUnref aur(dev);
370
371 SkCanvas c(dev);
372 dev->beginPortfolio(&xps);
373 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000374 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000375 dev->endSheet();
376 dev->endPortfolio();
377
378#endif
379}
380
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000381static ErrorBitfield write_reference_image(const ConfigData& gRec,
382 const char writePath [],
383 const char renderModeDescriptor [],
384 const SkString& name,
385 SkBitmap& bitmap,
386 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000387 SkString path;
388 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000389 if (gRec.fBackend == kRaster_Backend ||
390 gRec.fBackend == kGPU_Backend ||
391 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
392
tomhudson@google.comea325432011-06-09 20:30:03 +0000393 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000394 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000395 }
396 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000397 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000398 success = write_document(path, *document);
399 }
400 if (kXPS_Backend == gRec.fBackend) {
401 path = make_filename(writePath, renderModeDescriptor, name, "xps");
402 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000403 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000404 if (success) {
405 return ERROR_NONE;
406 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000407 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000408 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000409 }
410}
411
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000412static ErrorBitfield compare_to_reference_image(const SkString& name,
413 SkBitmap &bitmap,
414 const SkBitmap& comparisonBitmap,
415 const char diffPath [],
416 const char renderModeDescriptor []) {
417 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000418 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000419 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
420 diffPath ? &diffBitmap : NULL);
421 if ((ERROR_NONE == errors) && diffPath) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000422 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000423 if (!write_bitmap(diffName, diffBitmap)) {
424 errors |= ERROR_WRITING_REFERENCE_IMAGE;
425 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000426 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000427 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000428}
429
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000430static ErrorBitfield compare_to_reference_image(const char readPath [],
431 const SkString& name,
432 SkBitmap &bitmap,
433 const char diffPath [],
434 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000435 SkString path = make_filename(readPath, "", name, "png");
436 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000437 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
438 SkBitmap::kARGB_8888_Config,
439 SkImageDecoder::kDecodePixels_Mode, NULL)) {
440 return compare_to_reference_image(name, bitmap,
441 orig, diffPath,
442 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000443 } else {
444 fprintf(stderr, "FAILED to read %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000445 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000446 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000447}
448
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000449static ErrorBitfield handle_test_results(GM* gm,
450 const ConfigData& gRec,
451 const char writePath [],
452 const char readPath [],
453 const char diffPath [],
454 const char renderModeDescriptor [],
455 SkBitmap& bitmap,
456 SkDynamicMemoryWStream* pdf,
457 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000458 SkString name = make_name(gm->shortName(), gRec.fName);
459
460 if (writePath) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000461 return write_reference_image(gRec, writePath, renderModeDescriptor,
462 name, bitmap, pdf);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000463 } else if (readPath && (
464 gRec.fBackend == kRaster_Backend ||
465 gRec.fBackend == kGPU_Backend ||
466 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000467 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000468 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000469 } else if (comparisonBitmap) {
470 return compare_to_reference_image(name, bitmap,
471 *comparisonBitmap, diffPath,
472 renderModeDescriptor);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000473 } else {
474 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000475 }
476}
477
478static SkPicture* generate_new_picture(GM* gm) {
479 // Pictures are refcounted so must be on heap
480 SkPicture* pict = new SkPicture;
481 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000482 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000483 pict->endRecording();
484
485 return pict;
486}
487
488static SkPicture* stream_to_new_picture(const SkPicture& src) {
489
490 // To do in-memory commiunications with a stream, we need to:
491 // * create a dynamic memory stream
492 // * copy it into a buffer
493 // * create a read stream from it
494 // ?!?!
495
496 SkDynamicMemoryWStream storage;
497 src.serialize(&storage);
498
499 int streamSize = storage.getOffset();
500 SkAutoMalloc dstStorage(streamSize);
501 void* dst = dstStorage.get();
502 //char* dst = new char [streamSize];
503 //@todo thudson 22 April 2011 when can we safely delete [] dst?
504 storage.copyTo(dst);
505 SkMemoryStream pictReadback(dst, streamSize);
506 SkPicture* retval = new SkPicture (&pictReadback);
507 return retval;
508}
509
510// Test: draw into a bitmap or pdf.
511// Depending on flags, possibly compare to an expected image
512// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000513static ErrorBitfield test_drawing(GM* gm,
514 const ConfigData& gRec,
515 const char writePath [],
516 const char readPath [],
517 const char diffPath [],
518 GrContext* context,
519 GrRenderTarget* rt,
520 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000521 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000522
523 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000524 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000525 // Early exit if we can't generate the image.
junov@google.com4370aed2012-01-18 16:21:08 +0000526 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
527 false);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000528 if (ERROR_NONE != errors) {
529 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000530 }
reed@google.com46cce912011-06-29 12:54:46 +0000531 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000532 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000533#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000534 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000535 SkMemoryStream stream(data.data(), data.size());
536 SkPDFDocumentToBitmap(&stream, bitmap);
537#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000538 } else if (gRec.fBackend == kXPS_Backend) {
539 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000540 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000541 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000542 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000543}
544
junov@google.com4370aed2012-01-18 16:21:08 +0000545static ErrorBitfield test_deferred_drawing(GM* gm,
546 const ConfigData& gRec,
547 const SkBitmap& comparisonBitmap,
548 const char diffPath [],
549 GrContext* context,
550 GrRenderTarget* rt) {
551 SkDynamicMemoryWStream document;
552
553 if (gRec.fBackend == kRaster_Backend ||
554 gRec.fBackend == kGPU_Backend) {
555 SkBitmap bitmap;
556 // Early exit if we can't generate the image, but this is
557 // expected in some cases, so don't report a test failure.
558 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
559 return ERROR_NONE;
560 }
561 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
562 "-deferred", bitmap, NULL, &comparisonBitmap);
563 }
564 return ERROR_NONE;
565}
566
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000567static ErrorBitfield test_picture_playback(GM* gm,
568 const ConfigData& gRec,
569 const SkBitmap& comparisonBitmap,
570 const char readPath [],
571 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000572 SkPicture* pict = generate_new_picture(gm);
573 SkAutoUnref aur(pict);
574
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000575 if (kRaster_Backend == gRec.fBackend) {
576 SkBitmap bitmap;
577 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000578 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
579 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000580 } else {
581 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000582 }
583}
584
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000585static ErrorBitfield test_picture_serialization(GM* gm,
586 const ConfigData& gRec,
587 const SkBitmap& comparisonBitmap,
588 const char readPath [],
589 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000590 SkPicture* pict = generate_new_picture(gm);
591 SkAutoUnref aurp(pict);
592 SkPicture* repict = stream_to_new_picture(*pict);
593 SkAutoUnref aurr(repict);
594
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000595 if (kRaster_Backend == gRec.fBackend) {
596 SkBitmap bitmap;
597 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000598 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
599 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000600 } else {
601 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000602 }
603}
604
605static void usage(const char * argv0) {
junov@google.com77e498e2012-01-18 18:56:34 +0000606 SkDebugf(
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000607 "%s [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n"
608 " [--noreplay] [--serialize] [--forceBWtext] [--nopdf] \n"
609 " [--nodeferred] [--match substring] [--notexturecache]"
junov@google.com77e498e2012-01-18 18:56:34 +0000610#if SK_MESA
611 " [--mesagl]"
612#endif
613 "\n\n", argv0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000614 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000615 SkDebugf(
616" readPath: directory to read reference images from;\n"
617" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000618 SkDebugf(" diffPath: directory to write difference images in.\n");
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000619 SkDebugf(" resourcePath: directory that stores image resources.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000620 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000621 SkDebugf(
622" --serialize: exercise SkPicture serialization & deserialization.\n");
junov@google.com77e498e2012-01-18 18:56:34 +0000623 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
624 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
625 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
reed@google.come6a5c4d2011-07-25 14:30:54 +0000626 SkDebugf(" --match foo will only run tests that substring match foo.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000627#if SK_MESA
628 SkDebugf(" --mesagl will run using the osmesa sw gl rasterizer.\n");
629#endif
twiz@google.come24a0792012-01-31 18:35:30 +0000630 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000631}
632
633static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000634 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
635 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
636 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000637#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000638 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000639#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000640#ifdef SK_SUPPORT_PDF
641 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
642#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000643#ifdef SK_SUPPORT_XPS
644 { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" },
645#endif
reed@android.com00dae862009-06-10 15:38:48 +0000646};
647
reed@google.comb2a51622011-10-31 16:30:04 +0000648static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
649 if (0 == array.count()) {
650 // no names, so don't skip anything
651 return false;
652 }
653 for (int i = 0; i < array.count(); ++i) {
654 if (strstr(name, array[i])) {
655 // found the name, so don't skip
656 return false;
657 }
658 }
659 return true;
660}
661
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000662namespace skiagm {
663static GrContext* gGrContext;
664GrContext* GetGr() {
665 return gGrContext;
666}
667}
668
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000669int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000670 SkAutoGraphics ag;
reed@google.com8923c6c2011-11-08 14:59:38 +0000671 // we don't need to see this during a run
672 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000673
epoger@google.com7bc13a62012-02-14 14:53:59 +0000674 setSystemPreferences();
675
reed@android.com8015dd82009-06-21 00:49:18 +0000676 const char* writePath = NULL; // if non-null, where we write the originals
677 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000678 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000679 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000680
reed@google.comb2a51622011-10-31 16:30:04 +0000681 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000682
reed@google.comab973972011-09-19 19:01:38 +0000683 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000684 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000685 bool doSerialize = false;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000686 bool useMesa = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000687 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000688 bool disableTextureCache = false;
689
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000690 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000691 char* const* stop = argv + argc;
692 for (++argv; argv < stop; ++argv) {
693 if (strcmp(*argv, "-w") == 0) {
694 argv++;
695 if (argv < stop && **argv) {
696 writePath = *argv;
697 }
698 } else if (strcmp(*argv, "-r") == 0) {
699 argv++;
700 if (argv < stop && **argv) {
701 readPath = *argv;
702 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000703 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000704 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000705 if (argv < stop && **argv) {
706 diffPath = *argv;
707 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000708 } else if (strcmp(*argv, "-i") == 0) {
709 argv++;
710 if (argv < stop && **argv) {
711 resourcePath = *argv;
712 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000713 } else if (strcmp(*argv, "--forceBWtext") == 0) {
714 gForceBWtext = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000715 } else if (strcmp(*argv, "--noreplay") == 0) {
716 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000717 } else if (strcmp(*argv, "--nopdf") == 0) {
718 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000719 } else if (strcmp(*argv, "--nodeferred") == 0) {
720 doDeferred = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000721 } else if (strcmp(*argv, "--serialize") == 0) {
722 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000723 } else if (strcmp(*argv, "--match") == 0) {
724 ++argv;
725 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000726 // just record the ptr, no need for a deep copy
727 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000728 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000729#if SK_MESA
730 } else if (strcmp(*argv, "--mesagl") == 0) {
731 useMesa = true;
732#endif
twiz@google.come24a0792012-01-31 18:35:30 +0000733 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000734 disableTextureCache = true;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000735 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000736 usage(commandName);
737 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000738 }
739 }
740 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000741 usage(commandName);
742 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000743 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000744
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000745 GM::SetResourcePath(resourcePath);
746
bsalomon@google.com39149582011-06-13 21:55:32 +0000747 int maxW = -1;
748 int maxH = -1;
749 Iter iter;
750 GM* gm;
751 while ((gm = iter.next()) != NULL) {
752 SkISize size = gm->getISize();
753 maxW = SkMax32(size.width(), maxW);
754 maxH = SkMax32(size.height(), maxH);
tomhudson@google.com7816a4e2012-03-15 13:39:51 +0000755 // This fixes a memory leak, but we are churning gms; we could
756 // instead cache them if we have constructors with side-effects.
757 SkDELETE(gm);
bsalomon@google.com39149582011-06-13 21:55:32 +0000758 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000759 // setup a GL context for drawing offscreen
bsalomon@google.com373a6632011-10-19 20:43:20 +0000760 SkAutoTUnref<SkGLContext> glContext;
761#if SK_MESA
762 if (useMesa) {
763 glContext.reset(new SkMesaGLContext());
764 } else
765#endif
766 {
767 glContext.reset(new SkNativeGLContext());
768 }
769
bsalomon@google.com29d35012011-11-30 16:57:21 +0000770 GrPlatformRenderTargetDesc rtDesc;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000771 if (glContext.get()->init(maxW, maxH)) {
772 GrPlatform3DContext ctx =
773 reinterpret_cast<GrPlatform3DContext>(glContext.get()->gl());
774 gGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000775 if (NULL != gGrContext) {
bsalomon@google.com29d35012011-11-30 16:57:21 +0000776 rtDesc.fConfig = kSkia8888_PM_GrPixelConfig;
777 rtDesc.fStencilBits = 8;
778 rtDesc.fRenderTargetHandle = glContext.get()->getFBOID();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000779 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000780 } else {
781 fprintf(stderr, "could not create GL context.\n");
reed@google.com37df17d2010-12-23 20:20:51 +0000782 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000783
reed@android.com00f883e2010-12-14 17:46:14 +0000784 if (readPath) {
785 fprintf(stderr, "reading from %s\n", readPath);
786 } else if (writePath) {
787 fprintf(stderr, "writing to %s\n", writePath);
788 }
789
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000790 if (resourcePath) {
791 fprintf(stderr, "reading resources from %s\n", resourcePath);
792 }
793
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000794 // Accumulate success of all tests.
795 int testsRun = 0;
796 int testsPassed = 0;
797 int testsFailed = 0;
798 int testsMissingReferenceImages = 0;
799
twiz@google.come24a0792012-01-31 18:35:30 +0000800 if (disableTextureCache) {
801 skiagm::GetGr()->setTextureCacheLimits(0, 0);
802 }
803
bsalomon@google.com39149582011-06-13 21:55:32 +0000804 iter.reset();
reed@android.com00dae862009-06-10 15:38:48 +0000805 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000806 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000807 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000808 SkDELETE(gm);
809 continue;
810 }
811
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000812 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000813 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000814 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000815 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000816
bsalomon@google.com29d35012011-11-30 16:57:21 +0000817 // Above we created an fbo for the context at maxW x maxH size.
818 // Here we lie about the size of the rt. We claim it is the size
819 // desired by the test. The reason is that rasterization may change
820 // slightly when the viewport dimensions change. Previously, whenever
821 // a new test was checked in that bumped maxW or maxH several images
822 // would slightly change.
823 rtDesc.fWidth = size.width();
824 rtDesc.fHeight = size.height();
825 SkAutoTUnref<GrRenderTarget> rt;
826 if (gGrContext) {
827 rt.reset(gGrContext->createPlatformRenderTarget(rtDesc));
828 }
reed@google.comfbc21172011-09-19 19:08:33 +0000829
bsalomon@google.com29d35012011-11-30 16:57:21 +0000830 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000831 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000832 uint32_t gmFlags = gm->getFlags();
twiz@google.come24a0792012-01-31 18:35:30 +0000833 if ((kPDF_Backend == gRec[i].fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +0000834 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
835 {
reed@google.comab973972011-09-19 19:01:38 +0000836 continue;
837 }
838
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000839 // Now we know that we want to run this test and record its
840 // success or failure.
841 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000842
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000843 if ((ERROR_NONE == testErrors) &&
844 (kGPU_Backend == gRec[i].fBackend) &&
845 (NULL == rt.get())) {
846 fprintf(stderr, "Could not create render target for gpu.\n");
847 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000848 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000849
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000850 if (ERROR_NONE == testErrors) {
851 testErrors |= test_drawing(gm, gRec[i],
852 writePath, readPath, diffPath,
853 gGrContext,
854 rt.get(), &forwardRenderedBitmap);
855 }
856
junov@google.com4370aed2012-01-18 16:21:08 +0000857 if (doDeferred && !testErrors &&
twiz@google.come24a0792012-01-31 18:35:30 +0000858 (kGPU_Backend == gRec[i].fBackend ||
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000859 kRaster_Backend == gRec[i].fBackend)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000860 testErrors |= test_deferred_drawing(gm, gRec[i],
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000861 forwardRenderedBitmap,
862 diffPath, gGrContext, rt.get());
junov@google.com4370aed2012-01-18 16:21:08 +0000863 }
864
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000865 if ((ERROR_NONE == testErrors) && doReplay &&
866 !(gmFlags & GM::kSkipPicture_Flag)) {
867 testErrors |= test_picture_playback(gm, gRec[i],
868 forwardRenderedBitmap,
869 readPath, diffPath);
870 }
871
872 if ((ERROR_NONE == testErrors) && doSerialize) {
873 testErrors |= test_picture_serialization(gm, gRec[i],
874 forwardRenderedBitmap,
875 readPath, diffPath);
876 }
877
878 // Update overall results.
879 // We only tabulate the particular error types that we currently
880 // care about (e.g., missing reference images). Later on, if we
881 // want to also tabulate pixel mismatches vs dimension mistmatches
882 // (or whatever else), we can do so.
883 testsRun++;
884 if (ERROR_NONE == testErrors) {
885 testsPassed++;
886 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
887 testsMissingReferenceImages++;
888 } else {
889 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000890 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000891 }
reed@android.com00dae862009-06-10 15:38:48 +0000892 SkDELETE(gm);
893 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000894 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
895 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
896 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +0000897}