blob: 0c41418c93b1e3408fda6b0b2de7842afd11ffbb [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"
bsalomon@google.com971d0c82011-08-19 17:22:05 +00009#include "GrContext.h"
10#include "GrRenderTarget.h"
11
reed@android.comb9b9a182009-07-08 02:54:47 +000012#include "SkColorPriv.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000013#include "SkData.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000014#include "SkDevice.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000015#include "SkGpuCanvas.h"
16#include "SkGpuDevice.h"
reed@android.com8015dd82009-06-21 00:49:18 +000017#include "SkGraphics.h"
18#include "SkImageDecoder.h"
19#include "SkImageEncoder.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +000020#include "SkNativeGLContext.h"
21#include "SkMesaGLContext.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000022#include "SkPicture.h"
reed@google.com07700442010-12-20 19:46:07 +000023#include "SkStream.h"
24#include "SkRefCnt.h"
25
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000026static bool gForceBWtext;
27
reed@google.com8923c6c2011-11-08 14:59:38 +000028extern bool gSkSuppressFontCachePurgeSpew;
29
reed@google.com07700442010-12-20 19:46:07 +000030#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000031 #include "SkPDFDevice.h"
32 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000033#endif
reed@android.com00dae862009-06-10 15:38:48 +000034
bungeman@google.comb29c8832011-10-10 13:19:10 +000035#ifdef SK_SUPPORT_XPS
36 #include "SkXPSDevice.h"
37#endif
38
reed@google.com46cce912011-06-29 12:54:46 +000039#ifdef SK_BUILD_FOR_MAC
40 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000041 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000042#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000043 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000044#endif
45
epoger@google.comc7cf2b32011-12-28 19:31:01 +000046typedef int ErrorBitfield;
47const static ErrorBitfield ERROR_NONE = 0x00;
48const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
49const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
50const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
51const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
52const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
53
reed@android.com00dae862009-06-10 15:38:48 +000054using namespace skiagm;
55
reed@android.com00dae862009-06-10 15:38:48 +000056class Iter {
57public:
58 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000059 this->reset();
60 }
61
62 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000063 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000064 }
reed@google.comd4dfd102011-01-18 21:05:42 +000065
reed@android.comdd0ac282009-06-20 02:38:16 +000066 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000067 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000068 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000069 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000070 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000071 }
72 return NULL;
73 }
reed@google.comd4dfd102011-01-18 21:05:42 +000074
reed@android.com00dae862009-06-10 15:38:48 +000075 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000076 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000077 int count = 0;
78 while (reg) {
79 count += 1;
80 reg = reg->next();
81 }
82 return count;
83 }
reed@google.comd4dfd102011-01-18 21:05:42 +000084
reed@android.com00dae862009-06-10 15:38:48 +000085private:
86 const GMRegistry* fReg;
87};
88
reed@android.com8015dd82009-06-21 00:49:18 +000089static SkString make_name(const char shortName[], const char configName[]) {
90 SkString name(shortName);
91 name.appendf("_%s", configName);
92 return name;
93}
94
tomhudson@google.com9875dd12011-04-25 15:49:53 +000095static SkString make_filename(const char path[],
96 const char pathSuffix[],
97 const SkString& name,
98 const char suffix[]) {
reed@android.com8015dd82009-06-21 00:49:18 +000099 SkString filename(path);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000100 if (filename.endsWith("/")) {
101 filename.remove(filename.size() - 1, 1);
reed@android.com00dae862009-06-10 15:38:48 +0000102 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000103 filename.append(pathSuffix);
104 filename.append("/");
reed@google.com07700442010-12-20 19:46:07 +0000105 filename.appendf("%s.%s", name.c_str(), suffix);
reed@android.com8015dd82009-06-21 00:49:18 +0000106 return filename;
107}
108
reed@android.comb9b9a182009-07-08 02:54:47 +0000109/* since PNG insists on unpremultiplying our alpha, we take no precision chances
110 and force all pixels to be 100% opaque, otherwise on compare we may not get
111 a perfect match.
112 */
113static void force_all_opaque(const SkBitmap& bitmap) {
114 SkAutoLockPixels lock(bitmap);
115 for (int y = 0; y < bitmap.height(); y++) {
116 for (int x = 0; x < bitmap.width(); x++) {
117 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
118 }
119 }
120}
121
122static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
123 SkBitmap copy;
124 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
125 force_all_opaque(copy);
126 return SkImageEncoder::EncodeFile(path.c_str(), copy,
127 SkImageEncoder::kPNG_Type, 100);
128}
129
reed@google.com3d3f0922010-12-20 21:10:29 +0000130static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000131 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
132 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
133 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
134 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
reed@google.com3d3f0922010-12-20 21:10:29 +0000135}
136
137static void compute_diff(const SkBitmap& target, const SkBitmap& base,
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000138 SkBitmap* diff) {
139 SkAutoLockPixels alp(*diff);
reed@google.com3d3f0922010-12-20 21:10:29 +0000140
141 const int w = target.width();
142 const int h = target.height();
143 for (int y = 0; y < h; y++) {
144 for (int x = 0; x < w; x++) {
145 SkPMColor c0 = *base.getAddr32(x, y);
146 SkPMColor c1 = *target.getAddr32(x, y);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000147 SkPMColor d = 0;
148 if (c0 != c1) {
149 d = compute_diff_pmcolor(c0, c1);
150 }
151 *diff->getAddr32(x, y) = d;
152 }
153 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000154}
155
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000156static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
157 const SkString& name,
158 const char* renderModeDescriptor,
159 SkBitmap* diff) {
reed@android.comb9b9a182009-07-08 02:54:47 +0000160 SkBitmap copy;
161 const SkBitmap* bm = &target;
162 if (target.config() != SkBitmap::kARGB_8888_Config) {
163 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
164 bm = &copy;
165 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000166 SkBitmap baseCopy;
167 const SkBitmap* bp = &base;
168 if (base.config() != SkBitmap::kARGB_8888_Config) {
169 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
170 bp = &baseCopy;
171 }
reed@android.comb9b9a182009-07-08 02:54:47 +0000172
173 force_all_opaque(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000174 force_all_opaque(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000175
176 const int w = bm->width();
177 const int h = bm->height();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000178 if (w != bp->width() || h != bp->height()) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000179 SkDebugf(
180"---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
181 renderModeDescriptor, name.c_str(),
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000182 bp->width(), bp->height(), w, h);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000183 return ERROR_DIMENSION_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000184 }
185
186 SkAutoLockPixels bmLock(*bm);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000187 SkAutoLockPixels baseLock(*bp);
reed@android.comb9b9a182009-07-08 02:54:47 +0000188
189 for (int y = 0; y < h; y++) {
190 for (int x = 0; x < w; x++) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000191 SkPMColor c0 = *bp->getAddr32(x, y);
reed@android.comb9b9a182009-07-08 02:54:47 +0000192 SkPMColor c1 = *bm->getAddr32(x, y);
193 if (c0 != c1) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000194 SkDebugf(
195"----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
196 renderModeDescriptor, name.c_str(), x, y, c0, c1);
reed@google.com3d3f0922010-12-20 21:10:29 +0000197
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000198 if (diff) {
199 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
200 diff->allocPixels();
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000201 compute_diff(*bm, *bp, diff);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000202 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000203 return ERROR_PIXEL_MISMATCH;
reed@android.comb9b9a182009-07-08 02:54:47 +0000204 }
205 }
206 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000207
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000208 // they're equal
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000209 return ERROR_NONE;
reed@android.com8015dd82009-06-21 00:49:18 +0000210}
reed@android.com00dae862009-06-10 15:38:48 +0000211
bungeman@google.comb29c8832011-10-10 13:19:10 +0000212static bool write_document(const SkString& path,
213 const SkDynamicMemoryWStream& document) {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000214 SkFILEWStream stream(path.c_str());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000215 SkAutoDataUnref data(document.copyToData());
reed@google.com8a85d0c2011-06-24 19:12:12 +0000216 return stream.writeData(data.get());
reed@google.com07700442010-12-20 19:46:07 +0000217}
218
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000219enum Backend {
220 kRaster_Backend,
221 kGPU_Backend,
222 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000223 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000224};
225
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000226struct ConfigData {
227 SkBitmap::Config fConfig;
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000228 Backend fBackend;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000229 const char* fName;
230};
231
232/// Returns true if processing should continue, false to skip the
233/// remainder of this config for this GM.
234//@todo thudson 22 April 2011 - could refactor this to take in
235// a factory to generate the context, always call readPixels()
236// (logically a noop for rasters, if wasted time), and thus collapse the
237// GPU special case and also let this be used for SkPicture testing.
238static void setup_bitmap(const ConfigData& gRec, SkISize& size,
239 SkBitmap* bitmap) {
240 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
241 bitmap->allocPixels();
242 bitmap->eraseColor(0);
243}
244
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000245#include "SkDrawFilter.h"
246class BWTextDrawFilter : public SkDrawFilter {
247public:
248 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
249};
250void BWTextDrawFilter::filter(SkPaint* p, Type t) {
251 if (kText_Type == t) {
252 p->setAntiAlias(false);
253 }
254}
255
256static void installFilter(SkCanvas* canvas) {
257 if (gForceBWtext) {
258 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
259 }
260}
261
262static void invokeGM(GM* gm, SkCanvas* canvas) {
263 installFilter(canvas);
264 gm->draw(canvas);
265 canvas->setDrawFilter(NULL);
266}
267
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000268static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
269 GrContext* context,
270 GrRenderTarget* rt,
271 SkBitmap* bitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000272 SkISize size (gm->getISize());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000273 setup_bitmap(gRec, size, bitmap);
274 SkCanvas canvas(*bitmap);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000275
276 if (gRec.fBackend == kRaster_Backend) {
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000277 invokeGM(gm, &canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000278 } else { // GPU
279 if (NULL == context) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000280 return ERROR_NO_GPU_CONTEXT;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000281 }
reed@google.comaf951c92011-06-16 19:10:39 +0000282 SkGpuCanvas gc(context, rt);
283 gc.setDevice(new SkGpuDevice(context, rt))->unref();
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000284 invokeGM(gm, &gc);
reed@google.comaf951c92011-06-16 19:10:39 +0000285 // the device is as large as the current rendertarget, so we explicitly
286 // only readback the amount we expect (in size)
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000287 // overwrite our previous allocation
bsalomon@google.comc6980972011-11-02 19:57:21 +0000288 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
289 size.fHeight);
290 gc.readPixels(bitmap, 0, 0);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000291 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000292 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000293}
294
295static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
296 SkPicture* pict, SkBitmap* bitmap) {
297 SkISize size = gm->getISize();
298 setup_bitmap(gRec, size, bitmap);
299 SkCanvas canvas(*bitmap);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000300 installFilter(&canvas);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000301 canvas.drawPicture(*pict);
302}
303
304static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
305#ifdef SK_SUPPORT_PDF
306 SkISize size = gm->getISize();
307 SkMatrix identity;
308 identity.reset();
ctguil@chromium.org15261292011-04-29 17:54:16 +0000309 SkPDFDevice* dev = new SkPDFDevice(size, size, identity);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000310 SkAutoUnref aur(dev);
311
312 SkCanvas c(dev);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000313 invokeGM(gm, &c);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000314
315 SkPDFDocument doc;
316 doc.appendPage(dev);
317 doc.emitPDF(&pdf);
318#endif
319}
320
bungeman@google.comb29c8832011-10-10 13:19:10 +0000321static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
322#ifdef SK_SUPPORT_XPS
323 SkISize size = gm->getISize();
324
325 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
326 SkIntToScalar(size.height()));
327 static const double inchesPerMeter = 10000.0 / 254.0;
328 static const double upm = 72 * inchesPerMeter;
329 SkVector unitsPerMeter = SkPoint::Make(SkDoubleToScalar(upm),
330 SkDoubleToScalar(upm));
331 static const double ppm = 200 * inchesPerMeter;
332 SkVector pixelsPerMeter = SkPoint::Make(SkDoubleToScalar(ppm),
333 SkDoubleToScalar(ppm));
334
335 SkXPSDevice* dev = new SkXPSDevice();
336 SkAutoUnref aur(dev);
337
338 SkCanvas c(dev);
339 dev->beginPortfolio(&xps);
340 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000341 invokeGM(gm, &c);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000342 dev->endSheet();
343 dev->endPortfolio();
344
345#endif
346}
347
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000348static ErrorBitfield write_reference_image(const ConfigData& gRec,
349 const char writePath [],
350 const char renderModeDescriptor [],
351 const SkString& name,
352 SkBitmap& bitmap,
353 SkDynamicMemoryWStream* document) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000354 SkString path;
355 bool success = false;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000356 if (gRec.fBackend == kRaster_Backend ||
357 gRec.fBackend == kGPU_Backend ||
358 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
359
tomhudson@google.comea325432011-06-09 20:30:03 +0000360 path = make_filename(writePath, renderModeDescriptor, name, "png");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000361 success = write_bitmap(path, bitmap);
reed@google.com46cce912011-06-29 12:54:46 +0000362 }
363 if (kPDF_Backend == gRec.fBackend) {
tomhudson@google.comea325432011-06-09 20:30:03 +0000364 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000365 success = write_document(path, *document);
366 }
367 if (kXPS_Backend == gRec.fBackend) {
368 path = make_filename(writePath, renderModeDescriptor, name, "xps");
369 success = write_document(path, *document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000370 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000371 if (success) {
372 return ERROR_NONE;
373 } else {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000374 fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000375 return ERROR_WRITING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000376 }
377}
378
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000379static ErrorBitfield compare_to_reference_image(const SkString& name,
380 SkBitmap &bitmap,
381 const SkBitmap& comparisonBitmap,
382 const char diffPath [],
383 const char renderModeDescriptor []) {
384 ErrorBitfield errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000385 SkBitmap diffBitmap;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000386 errors = compare(bitmap, comparisonBitmap, name, renderModeDescriptor,
387 diffPath ? &diffBitmap : NULL);
388 if ((ERROR_NONE == errors) && diffPath) {
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000389 SkString diffName = make_filename(diffPath, "", name, ".diff.png");
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000390 if (!write_bitmap(diffName, diffBitmap)) {
391 errors |= ERROR_WRITING_REFERENCE_IMAGE;
392 }
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000393 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000394 return errors;
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000395}
396
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000397static ErrorBitfield compare_to_reference_image(const char readPath [],
398 const SkString& name,
399 SkBitmap &bitmap,
400 const char diffPath [],
401 const char renderModeDescriptor []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000402 SkString path = make_filename(readPath, "", name, "png");
403 SkBitmap orig;
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000404 if (SkImageDecoder::DecodeFile(path.c_str(), &orig,
405 SkBitmap::kARGB_8888_Config,
406 SkImageDecoder::kDecodePixels_Mode, NULL)) {
407 return compare_to_reference_image(name, bitmap,
408 orig, diffPath,
409 renderModeDescriptor);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000410 } else {
411 fprintf(stderr, "FAILED to read %s\n", path.c_str());
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000412 return ERROR_READING_REFERENCE_IMAGE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000413 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000414}
415
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000416static ErrorBitfield handle_test_results(GM* gm,
417 const ConfigData& gRec,
418 const char writePath [],
419 const char readPath [],
420 const char diffPath [],
421 const char renderModeDescriptor [],
422 SkBitmap& bitmap,
423 SkDynamicMemoryWStream* pdf,
424 const SkBitmap* comparisonBitmap) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000425 SkString name = make_name(gm->shortName(), gRec.fName);
426
427 if (writePath) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000428 return write_reference_image(gRec, writePath, renderModeDescriptor,
429 name, bitmap, pdf);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000430 } else if (readPath && (
431 gRec.fBackend == kRaster_Backend ||
432 gRec.fBackend == kGPU_Backend ||
433 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF))) {
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000434 return compare_to_reference_image(readPath, name, bitmap,
tomhudson@google.comea325432011-06-09 20:30:03 +0000435 diffPath, renderModeDescriptor);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000436 } else if (comparisonBitmap) {
437 return compare_to_reference_image(name, bitmap,
438 *comparisonBitmap, diffPath,
439 renderModeDescriptor);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000440 } else {
441 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000442 }
443}
444
445static SkPicture* generate_new_picture(GM* gm) {
446 // Pictures are refcounted so must be on heap
447 SkPicture* pict = new SkPicture;
448 SkCanvas* cv = pict->beginRecording(1000, 1000);
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000449 invokeGM(gm, cv);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000450 pict->endRecording();
451
452 return pict;
453}
454
455static SkPicture* stream_to_new_picture(const SkPicture& src) {
456
457 // To do in-memory commiunications with a stream, we need to:
458 // * create a dynamic memory stream
459 // * copy it into a buffer
460 // * create a read stream from it
461 // ?!?!
462
463 SkDynamicMemoryWStream storage;
464 src.serialize(&storage);
465
466 int streamSize = storage.getOffset();
467 SkAutoMalloc dstStorage(streamSize);
468 void* dst = dstStorage.get();
469 //char* dst = new char [streamSize];
470 //@todo thudson 22 April 2011 when can we safely delete [] dst?
471 storage.copyTo(dst);
472 SkMemoryStream pictReadback(dst, streamSize);
473 SkPicture* retval = new SkPicture (&pictReadback);
474 return retval;
475}
476
477// Test: draw into a bitmap or pdf.
478// Depending on flags, possibly compare to an expected image
479// and possibly output a diff image if it fails to match.
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000480static ErrorBitfield test_drawing(GM* gm,
481 const ConfigData& gRec,
482 const char writePath [],
483 const char readPath [],
484 const char diffPath [],
485 GrContext* context,
486 GrRenderTarget* rt,
487 SkBitmap* bitmap) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000488 SkDynamicMemoryWStream document;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000489
490 if (gRec.fBackend == kRaster_Backend ||
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000491 gRec.fBackend == kGPU_Backend) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000492 // Early exit if we can't generate the image.
493 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap);
494 if (ERROR_NONE != errors) {
495 return errors;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000496 }
reed@google.com46cce912011-06-29 12:54:46 +0000497 } else if (gRec.fBackend == kPDF_Backend) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000498 generate_pdf(gm, document);
reed@google.com46cce912011-06-29 12:54:46 +0000499#if CAN_IMAGE_PDF
bungeman@google.com0f1541f2011-10-10 13:47:06 +0000500 SkAutoDataUnref data(document.copyToData());
reed@google.com46cce912011-06-29 12:54:46 +0000501 SkMemoryStream stream(data.data(), data.size());
502 SkPDFDocumentToBitmap(&stream, bitmap);
503#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000504 } else if (gRec.fBackend == kXPS_Backend) {
505 generate_xps(gm, document);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000506 }
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000507 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000508 "", *bitmap, &document, NULL);
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000509}
510
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000511static ErrorBitfield test_picture_playback(GM* gm,
512 const ConfigData& gRec,
513 const SkBitmap& comparisonBitmap,
514 const char readPath [],
515 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000516 SkPicture* pict = generate_new_picture(gm);
517 SkAutoUnref aur(pict);
518
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000519 if (kRaster_Backend == gRec.fBackend) {
520 SkBitmap bitmap;
521 generate_image_from_picture(gm, gRec, pict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000522 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
523 "-replay", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000524 } else {
525 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000526 }
527}
528
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000529static ErrorBitfield test_picture_serialization(GM* gm,
530 const ConfigData& gRec,
531 const SkBitmap& comparisonBitmap,
532 const char readPath [],
533 const char diffPath []) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000534 SkPicture* pict = generate_new_picture(gm);
535 SkAutoUnref aurp(pict);
536 SkPicture* repict = stream_to_new_picture(*pict);
537 SkAutoUnref aurr(repict);
538
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000539 if (kRaster_Backend == gRec.fBackend) {
540 SkBitmap bitmap;
541 generate_image_from_picture(gm, gRec, repict, &bitmap);
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000542 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
543 "-serialize", bitmap, NULL, &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000544 } else {
545 return ERROR_NONE;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000546 }
547}
548
549static void usage(const char * argv0) {
550 SkDebugf("%s [-w writePath] [-r readPath] [-d diffPath]\n", argv0);
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000551 SkDebugf(" [--replay] [--serialize]\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000552 SkDebugf(" writePath: directory to write rendered images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000553 SkDebugf(
554" readPath: directory to read reference images from;\n"
555" reports if any pixels mismatch between reference and new images\n");
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000556 SkDebugf(" diffPath: directory to write difference images in.\n");
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000557 SkDebugf(" --replay: exercise SkPicture replay.\n");
558 SkDebugf(
559" --serialize: exercise SkPicture serialization & deserialization.\n");
reed@google.come6a5c4d2011-07-25 14:30:54 +0000560 SkDebugf(" --match foo will only run tests that substring match foo.\n");
bsalomon@google.com373a6632011-10-19 20:43:20 +0000561#if SK_MESA
562 SkDebugf(" --mesagl will run using the osmesa sw gl rasterizer.\n");
563#endif
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000564}
565
566static const ConfigData gRec[] = {
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000567 { SkBitmap::kARGB_8888_Config, kRaster_Backend, "8888" },
568 { SkBitmap::kARGB_4444_Config, kRaster_Backend, "4444" },
569 { SkBitmap::kRGB_565_Config, kRaster_Backend, "565" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000570#ifdef SK_SCALAR_IS_FLOAT
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000571 { SkBitmap::kARGB_8888_Config, kGPU_Backend, "gpu" },
reed@google.com1a7e9462011-06-20 13:21:24 +0000572#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000573#ifdef SK_SUPPORT_PDF
574 { SkBitmap::kARGB_8888_Config, kPDF_Backend, "pdf" },
575#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +0000576#ifdef SK_SUPPORT_XPS
577 { SkBitmap::kARGB_8888_Config, kXPS_Backend, "xps" },
578#endif
reed@android.com00dae862009-06-10 15:38:48 +0000579};
580
reed@google.comb2a51622011-10-31 16:30:04 +0000581static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
582 if (0 == array.count()) {
583 // no names, so don't skip anything
584 return false;
585 }
586 for (int i = 0; i < array.count(); ++i) {
587 if (strstr(name, array[i])) {
588 // found the name, so don't skip
589 return false;
590 }
591 }
592 return true;
593}
594
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000595namespace skiagm {
596static GrContext* gGrContext;
597GrContext* GetGr() {
598 return gGrContext;
599}
600}
601
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000602int main(int argc, char * const argv[]) {
reed@android.com00dae862009-06-10 15:38:48 +0000603 SkAutoGraphics ag;
reed@google.com8923c6c2011-11-08 14:59:38 +0000604 // we don't need to see this during a run
605 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000606
reed@android.com8015dd82009-06-21 00:49:18 +0000607 const char* writePath = NULL; // if non-null, where we write the originals
608 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000609 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
reed@android.com8015dd82009-06-21 00:49:18 +0000610
reed@google.comb2a51622011-10-31 16:30:04 +0000611 SkTDArray<const char*> fMatches;
612
reed@google.comab973972011-09-19 19:01:38 +0000613 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000614 bool doReplay = true;
tomhudson@google.com6abfa492011-04-26 14:59:32 +0000615 bool doSerialize = false;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000616 bool useMesa = false;
617
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000618 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000619 char* const* stop = argv + argc;
620 for (++argv; argv < stop; ++argv) {
621 if (strcmp(*argv, "-w") == 0) {
622 argv++;
623 if (argv < stop && **argv) {
624 writePath = *argv;
625 }
626 } else if (strcmp(*argv, "-r") == 0) {
627 argv++;
628 if (argv < stop && **argv) {
629 readPath = *argv;
630 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000631 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000632 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000633 if (argv < stop && **argv) {
634 diffPath = *argv;
635 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000636 } else if (strcmp(*argv, "--forceBWtext") == 0) {
637 gForceBWtext = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000638 } else if (strcmp(*argv, "--noreplay") == 0) {
639 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000640 } else if (strcmp(*argv, "--nopdf") == 0) {
641 doPDF = false;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000642 } else if (strcmp(*argv, "--serialize") == 0) {
643 doSerialize = true;
reed@google.comece2b022011-07-25 14:28:57 +0000644 } else if (strcmp(*argv, "--match") == 0) {
645 ++argv;
646 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000647 // just record the ptr, no need for a deep copy
648 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000649 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000650#if SK_MESA
651 } else if (strcmp(*argv, "--mesagl") == 0) {
652 useMesa = true;
653#endif
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000654 } else {
655 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000656 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000657 }
658 }
659 if (argv != stop) {
660 usage(commandName);
tomhudson@google.com8e728d72011-04-26 20:22:57 +0000661 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000662 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000663
bsalomon@google.com39149582011-06-13 21:55:32 +0000664 int maxW = -1;
665 int maxH = -1;
666 Iter iter;
667 GM* gm;
668 while ((gm = iter.next()) != NULL) {
669 SkISize size = gm->getISize();
670 maxW = SkMax32(size.width(), maxW);
671 maxH = SkMax32(size.height(), maxH);
672 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000673 // setup a GL context for drawing offscreen
bsalomon@google.com373a6632011-10-19 20:43:20 +0000674 SkAutoTUnref<SkGLContext> glContext;
675#if SK_MESA
676 if (useMesa) {
677 glContext.reset(new SkMesaGLContext());
678 } else
679#endif
680 {
681 glContext.reset(new SkNativeGLContext());
682 }
683
bsalomon@google.com29d35012011-11-30 16:57:21 +0000684 GrPlatformRenderTargetDesc rtDesc;
bsalomon@google.com373a6632011-10-19 20:43:20 +0000685 if (glContext.get()->init(maxW, maxH)) {
686 GrPlatform3DContext ctx =
687 reinterpret_cast<GrPlatform3DContext>(glContext.get()->gl());
688 gGrContext = GrContext::Create(kOpenGL_Shaders_GrEngine, ctx);
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000689 if (NULL != gGrContext) {
bsalomon@google.com29d35012011-11-30 16:57:21 +0000690 rtDesc.fConfig = kSkia8888_PM_GrPixelConfig;
691 rtDesc.fStencilBits = 8;
692 rtDesc.fRenderTargetHandle = glContext.get()->getFBOID();
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000693 }
bsalomon@google.com373a6632011-10-19 20:43:20 +0000694 } else {
695 fprintf(stderr, "could not create GL context.\n");
reed@google.com37df17d2010-12-23 20:20:51 +0000696 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000697
reed@android.com00f883e2010-12-14 17:46:14 +0000698 if (readPath) {
699 fprintf(stderr, "reading from %s\n", readPath);
700 } else if (writePath) {
701 fprintf(stderr, "writing to %s\n", writePath);
702 }
703
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000704 // Accumulate success of all tests.
705 int testsRun = 0;
706 int testsPassed = 0;
707 int testsFailed = 0;
708 int testsMissingReferenceImages = 0;
709
bsalomon@google.com39149582011-06-13 21:55:32 +0000710 iter.reset();
reed@android.com00dae862009-06-10 15:38:48 +0000711 while ((gm = iter.next()) != NULL) {
reed@google.comece2b022011-07-25 14:28:57 +0000712 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +0000713 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +0000714 SkDELETE(gm);
715 continue;
716 }
717
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000718 SkISize size = gm->getISize();
reed@google.comece2b022011-07-25 14:28:57 +0000719 SkDebugf("drawing... %s [%d %d]\n", shortName,
reed@android.com8015dd82009-06-21 00:49:18 +0000720 size.width(), size.height());
tomhudson@google.comcae6b3f2011-06-17 13:11:45 +0000721 SkBitmap forwardRenderedBitmap;
reed@android.com8015dd82009-06-21 00:49:18 +0000722
bsalomon@google.com29d35012011-11-30 16:57:21 +0000723 // Above we created an fbo for the context at maxW x maxH size.
724 // Here we lie about the size of the rt. We claim it is the size
725 // desired by the test. The reason is that rasterization may change
726 // slightly when the viewport dimensions change. Previously, whenever
727 // a new test was checked in that bumped maxW or maxH several images
728 // would slightly change.
729 rtDesc.fWidth = size.width();
730 rtDesc.fHeight = size.height();
731 SkAutoTUnref<GrRenderTarget> rt;
732 if (gGrContext) {
733 rt.reset(gGrContext->createPlatformRenderTarget(rtDesc));
734 }
reed@google.comfbc21172011-09-19 19:08:33 +0000735
bsalomon@google.com29d35012011-11-30 16:57:21 +0000736 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000737 // Skip any tests that we don't even need to try.
bsalomon@google.com29d35012011-11-30 16:57:21 +0000738 uint32_t gmFlags = gm->getFlags();
bungeman@google.com64e011a2011-09-19 19:31:04 +0000739 if ((kPDF_Backend == gRec[i].fBackend) &&
740 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
741 {
reed@google.comab973972011-09-19 19:01:38 +0000742 continue;
743 }
744
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000745 // Now we know that we want to run this test and record its
746 // success or failure.
747 ErrorBitfield testErrors = ERROR_NONE;
reed@android.com00dae862009-06-10 15:38:48 +0000748
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000749 if ((ERROR_NONE == testErrors) &&
750 (kGPU_Backend == gRec[i].fBackend) &&
751 (NULL == rt.get())) {
752 fprintf(stderr, "Could not create render target for gpu.\n");
753 testErrors |= ERROR_NO_GPU_CONTEXT;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000754 }
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000755
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000756 if (ERROR_NONE == testErrors) {
757 testErrors |= test_drawing(gm, gRec[i],
758 writePath, readPath, diffPath,
759 gGrContext,
760 rt.get(), &forwardRenderedBitmap);
761 }
762
763 if ((ERROR_NONE == testErrors) && doReplay &&
764 !(gmFlags & GM::kSkipPicture_Flag)) {
765 testErrors |= test_picture_playback(gm, gRec[i],
766 forwardRenderedBitmap,
767 readPath, diffPath);
768 }
769
770 if ((ERROR_NONE == testErrors) && doSerialize) {
771 testErrors |= test_picture_serialization(gm, gRec[i],
772 forwardRenderedBitmap,
773 readPath, diffPath);
774 }
775
776 // Update overall results.
777 // We only tabulate the particular error types that we currently
778 // care about (e.g., missing reference images). Later on, if we
779 // want to also tabulate pixel mismatches vs dimension mistmatches
780 // (or whatever else), we can do so.
781 testsRun++;
782 if (ERROR_NONE == testErrors) {
783 testsPassed++;
784 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
785 testsMissingReferenceImages++;
786 } else {
787 testsFailed++;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000788 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000789 }
reed@android.com00dae862009-06-10 15:38:48 +0000790 SkDELETE(gm);
791 }
epoger@google.comc7cf2b32011-12-28 19:31:01 +0000792 printf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
793 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
794 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +0000795}