blob: 7150452ac46129ca41c5664849ae742e4c3beda0 [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
epoger@google.com57f7abc2012-11-13 03:41:55 +00008/*
9 * Code for the "gm" (Golden Master) rendering comparison tool.
10 *
11 * If you make changes to this, re-run the self-tests at gm/tests/run.sh
12 * to make sure they still pass... you may need to change the expected
13 * results of the self-test.
14 */
15
bungeman@google.comb29c8832011-10-10 13:19:10 +000016#include "gm.h"
epoger@google.com6f6568b2013-03-22 17:29:46 +000017#include "gm_error.h"
epoger@google.com37269602013-01-19 04:21:27 +000018#include "gm_expectations.h"
epoger@google.com7bc13a62012-02-14 14:53:59 +000019#include "system_preferences.h"
epoger@google.com5f6a0072013-01-31 16:30:55 +000020#include "SkBitmap.h"
epoger@google.comee8a8e32012-12-18 19:13:49 +000021#include "SkBitmapChecksummer.h"
reed@android.comb9b9a182009-07-08 02:54:47 +000022#include "SkColorPriv.h"
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000023#include "SkCommandLineFlags.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000024#include "SkData.h"
junov@google.com4370aed2012-01-18 16:21:08 +000025#include "SkDeferredCanvas.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000026#include "SkDevice.h"
epoger@google.comde961632012-10-26 18:56:36 +000027#include "SkDrawFilter.h"
scroggo@google.com5af9b202012-06-04 17:17:36 +000028#include "SkGPipe.h"
reed@android.com8015dd82009-06-21 00:49:18 +000029#include "SkGraphics.h"
30#include "SkImageDecoder.h"
31#include "SkImageEncoder.h"
epoger@google.come8ebeb12012-10-29 16:42:11 +000032#include "SkOSFile.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000033#include "SkPicture.h"
robertphillips@google.com977b9c82012-06-05 19:35:09 +000034#include "SkRefCnt.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000035#include "SkStream.h"
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000036#include "SkTArray.h"
junov@chromium.org3cb834b2012-12-13 16:39:53 +000037#include "SkTileGridPicture.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000038#include "SamplePipeControllers.h"
reed@google.com07700442010-12-20 19:46:07 +000039
bsalomon@google.com50c79d82013-01-08 20:31:53 +000040#ifdef SK_BUILD_FOR_WIN
41 // json includes xlocale which generates warning 4530 because we're compiling without
epoger@google.com37269602013-01-19 04:21:27 +000042 // exceptions; see https://code.google.com/p/skia/issues/detail?id=1067
bsalomon@google.com50c79d82013-01-08 20:31:53 +000043 #pragma warning(push)
44 #pragma warning(disable : 4530)
45#endif
epoger@google.comee8a8e32012-12-18 19:13:49 +000046#include "json/value.h"
bsalomon@google.com50c79d82013-01-08 20:31:53 +000047#ifdef SK_BUILD_FOR_WIN
48 #pragma warning(pop)
49#endif
epoger@google.comee8a8e32012-12-18 19:13:49 +000050
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000051#if SK_SUPPORT_GPU
52#include "GrContextFactory.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000053#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000054typedef GrContextFactory::GLContextType GLContextType;
epoger@google.com6f6568b2013-03-22 17:29:46 +000055#define DEFAULT_CACHE_VALUE -1
56static int gGpuCacheSizeBytes;
57static int gGpuCacheSizeCount;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000058#else
epoger@google.com80724df2013-03-21 13:49:54 +000059class GrContextFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000060class GrContext;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +000061class GrSurface;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000062typedef int GLContextType;
63#endif
64
reed@google.com8923c6c2011-11-08 14:59:38 +000065extern bool gSkSuppressFontCachePurgeSpew;
66
reed@google.com07700442010-12-20 19:46:07 +000067#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000068 #include "SkPDFDevice.h"
69 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000070#endif
reed@android.com00dae862009-06-10 15:38:48 +000071
epoger@google.come3cc2eb2012-01-18 20:11:13 +000072// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
73// stop writing out XPS-format image baselines in gm.
74#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000075#ifdef SK_SUPPORT_XPS
76 #include "SkXPSDevice.h"
77#endif
78
reed@google.com46cce912011-06-29 12:54:46 +000079#ifdef SK_BUILD_FOR_MAC
80 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000081 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000082#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000083 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000084#endif
85
reed@android.com00dae862009-06-10 15:38:48 +000086using namespace skiagm;
87
epoger@google.com57f7abc2012-11-13 03:41:55 +000088struct FailRec {
89 SkString fName;
epoger@google.com15655b22013-01-08 18:47:31 +000090 bool fIsPixelError;
epoger@google.com57f7abc2012-11-13 03:41:55 +000091
epoger@google.com15655b22013-01-08 18:47:31 +000092 FailRec() : fIsPixelError(false) {}
93 FailRec(const SkString& name) : fName(name), fIsPixelError(false) {}
epoger@google.com57f7abc2012-11-13 03:41:55 +000094};
95
reed@android.com00dae862009-06-10 15:38:48 +000096class Iter {
97public:
98 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000099 this->reset();
100 }
101
102 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +0000103 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +0000104 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000105
reed@android.comdd0ac282009-06-20 02:38:16 +0000106 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +0000107 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000108 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +0000109 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +0000110 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +0000111 }
112 return NULL;
113 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000114
reed@android.com00dae862009-06-10 15:38:48 +0000115 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +0000116 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +0000117 int count = 0;
118 while (reg) {
119 count += 1;
120 reg = reg->next();
121 }
122 return count;
123 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000124
reed@android.com00dae862009-06-10 15:38:48 +0000125private:
126 const GMRegistry* fReg;
127};
128
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000129enum Backend {
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000130 kRaster_Backend,
131 kGPU_Backend,
132 kPDF_Backend,
133 kXPS_Backend,
134};
135
136enum BbhType {
137 kNone_BbhType,
138 kRTree_BbhType,
139 kTileGrid_BbhType,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000140};
141
bsalomon@google.com7361f542012-04-19 19:15:35 +0000142enum ConfigFlags {
143 kNone_ConfigFlag = 0x0,
144 /* Write GM images if a write path is provided. */
145 kWrite_ConfigFlag = 0x1,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000146 /* Read reference GM images if a read path is provided. */
bsalomon@google.com7361f542012-04-19 19:15:35 +0000147 kRead_ConfigFlag = 0x2,
148 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
149};
150
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000151struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000152 SkBitmap::Config fConfig;
153 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000154 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000155 int fSampleCnt; // GPU backend only
156 ConfigFlags fFlags;
157 const char* fName;
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000158 bool fRunByDefault;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000159};
160
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000161class BWTextDrawFilter : public SkDrawFilter {
162public:
reed@google.com971aca72012-11-26 20:26:54 +0000163 virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000164};
reed@google.com971aca72012-11-26 20:26:54 +0000165bool BWTextDrawFilter::filter(SkPaint* p, Type t) {
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000166 if (kText_Type == t) {
167 p->setAntiAlias(false);
168 }
reed@google.com971aca72012-11-26 20:26:54 +0000169 return true;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000170}
171
scroggo@google.com565254b2012-06-28 15:41:32 +0000172struct PipeFlagComboData {
173 const char* name;
174 uint32_t flags;
175};
176
177static PipeFlagComboData gPipeWritingFlagCombos[] = {
178 { "", 0 },
179 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000180 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000181 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000182};
183
epoger@google.comde961632012-10-26 18:56:36 +0000184class GMMain {
185public:
epoger@google.come8ebeb12012-10-29 16:42:11 +0000186 GMMain() {
187 // Set default values of member variables, which tool_main()
188 // may override.
epoger@google.come8ebeb12012-10-29 16:42:11 +0000189 fUseFileHierarchy = false;
epoger@google.com6f6568b2013-03-22 17:29:46 +0000190 fIgnorableErrorCombination.add(kMissingExpectations_ErrorType);
junov@chromium.org95146eb2013-01-11 21:04:40 +0000191 fMismatchPath = NULL;
epoger@google.come8ebeb12012-10-29 16:42:11 +0000192 }
193
194 SkString make_name(const char shortName[], const char configName[]) {
195 SkString name;
epoger@google.com57f7abc2012-11-13 03:41:55 +0000196 if (0 == strlen(configName)) {
197 name.append(shortName);
198 } else if (fUseFileHierarchy) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000199 name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
200 } else {
201 name.appendf("%s_%s", shortName, configName);
202 }
epoger@google.comde961632012-10-26 18:56:36 +0000203 return name;
204 }
205
epoger@google.com5f6a0072013-01-31 16:30:55 +0000206 /* since PNG insists on unpremultiplying our alpha, we take no
207 precision chances and force all pixels to be 100% opaque,
208 otherwise on compare we may not get a perfect match.
209 */
210 static void force_all_opaque(const SkBitmap& bitmap) {
211 SkBitmap::Config config = bitmap.config();
212 switch (config) {
213 case SkBitmap::kARGB_8888_Config:
214 force_all_opaque_8888(bitmap);
215 break;
216 case SkBitmap::kRGB_565_Config:
217 // nothing to do here; 565 bitmaps are inherently opaque
218 break;
219 default:
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000220 gm_fprintf(stderr, "unsupported bitmap config %d\n", config);
221 DEBUGFAIL_SEE_STDERR;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000222 }
223 }
224
225 static void force_all_opaque_8888(const SkBitmap& bitmap) {
226 SkAutoLockPixels lock(bitmap);
227 for (int y = 0; y < bitmap.height(); y++) {
228 for (int x = 0; x < bitmap.width(); x++) {
229 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
230 }
231 }
232 }
233
234 static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
235 // TODO(epoger): Now that we have removed force_all_opaque()
236 // from this method, we should be able to get rid of the
237 // transformation to 8888 format also.
238 SkBitmap copy;
239 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
240 return SkImageEncoder::EncodeFile(path.c_str(), copy,
241 SkImageEncoder::kPNG_Type, 100);
242 }
243
epoger@google.com6f6568b2013-03-22 17:29:46 +0000244 /**
245 * Records the errors encountered in fFailedTests, except for any error
246 * types we want to ignore.
247 */
248 void RecordError(const ErrorCombination& errorCombination, const SkString& name,
epoger@google.com5f6a0072013-01-31 16:30:55 +0000249 const char renderModeDescriptor []) {
epoger@google.comeb066362013-03-08 09:39:36 +0000250 // The common case: no error means nothing to record.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000251 if (errorCombination.isEmpty()) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000252 return;
epoger@google.comeb066362013-03-08 09:39:36 +0000253 }
254
255 // If only certain error type(s) were reported, we know we can ignore them.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000256 if (errorCombination.minus(fIgnorableErrorCombination).isEmpty()) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000257 return;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000258 }
259
epoger@google.com6f6568b2013-03-22 17:29:46 +0000260 FailRec& rec = fFailedTests.push_back(make_name(name.c_str(), renderModeDescriptor));
261 rec.fIsPixelError = errorCombination.includes(kImageMismatch_ErrorType);
epoger@google.com5f6a0072013-01-31 16:30:55 +0000262 }
263
264 // List contents of fFailedTests via SkDebug.
265 void ListErrors() {
266 for (int i = 0; i < fFailedTests.count(); ++i) {
267 if (fFailedTests[i].fIsPixelError) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000268 gm_fprintf(stderr, "\t\t%s pixel_error\n", fFailedTests[i].fName.c_str());
epoger@google.com5f6a0072013-01-31 16:30:55 +0000269 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000270 gm_fprintf(stderr, "\t\t%s\n", fFailedTests[i].fName.c_str());
epoger@google.com5f6a0072013-01-31 16:30:55 +0000271 }
272 }
273 }
274
275 static bool write_document(const SkString& path,
276 const SkDynamicMemoryWStream& document) {
277 SkFILEWStream stream(path.c_str());
278 SkAutoDataUnref data(document.copyToData());
279 return stream.writeData(data.get());
280 }
281
epoger@google.com37269602013-01-19 04:21:27 +0000282 /**
epoger@google.com5f6a0072013-01-31 16:30:55 +0000283 * Prepare an SkBitmap to render a GM into.
284 *
285 * After you've rendered the GM into the SkBitmap, you must call
286 * complete_bitmap()!
287 *
288 * @todo thudson 22 April 2011 - could refactor this to take in
289 * a factory to generate the context, always call readPixels()
290 * (logically a noop for rasters, if wasted time), and thus collapse the
291 * GPU special case and also let this be used for SkPicture testing.
292 */
293 static void setup_bitmap(const ConfigData& gRec, SkISize& size,
294 SkBitmap* bitmap) {
295 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
296 bitmap->allocPixels();
297 bitmap->eraseColor(SK_ColorTRANSPARENT);
298 }
299
300 /**
301 * Any finalization steps we need to perform on the SkBitmap after
302 * we have rendered the GM into it.
epoger@google.com37269602013-01-19 04:21:27 +0000303 *
304 * It's too bad that we are throwing away alpha channel data
305 * we could otherwise be examining, but this had always been happening
306 * before... it was buried within the compare() method at
307 * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
308 *
309 * Apparently we need this, at least for bitmaps that are either:
310 * (a) destined to be written out as PNG files, or
311 * (b) compared against bitmaps read in from PNG files
312 * for the reasons described just above the force_all_opaque() method.
313 *
314 * Neglecting to do this led to the difficult-to-diagnose
315 * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
316 * spurious pixel_error messages as of r7258')
317 *
318 * TODO(epoger): Come up with a better solution that allows us to
319 * compare full pixel data, including alpha channel, while still being
320 * robust in the face of transformations to/from PNG files.
321 * Options include:
322 *
323 * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
324 * will be written to, or compared against, PNG files.
325 * PRO: Preserve/compare alpha channel info for the non-PNG cases
326 * (comparing different renderModes in-memory)
327 * CON: The bitmaps (and checksums) for these non-PNG cases would be
328 * different than those for the PNG-compared cases, and in the
329 * case of a failed renderMode comparison, how would we write the
330 * image to disk for examination?
331 *
332 * 2. Always compute image checksums from PNG format (either
333 * directly from the the bytes of a PNG file, or capturing the
334 * bytes we would have written to disk if we were writing the
335 * bitmap out as a PNG).
336 * PRO: I think this would allow us to never force opaque, and to
337 * the extent that alpha channel data can be preserved in a PNG
338 * file, we could observe it.
339 * CON: If we read a bitmap from disk, we need to take its checksum
340 * from the source PNG (we can't compute it from the bitmap we
341 * read out of the PNG, because we will have already premultiplied
342 * the alpha).
343 * CON: Seems wasteful to convert a bitmap to PNG format just to take
344 * its checksum. (Although we're wasting lots of effort already
345 * calling force_all_opaque().)
346 *
347 * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
348 * so we can transform images back and forth without fear of off-by-one
349 * errors.
350 * CON: Math is hard.
351 *
352 * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
353 * channel), rather than demanding absolute equality.
354 * CON: Can't do this with checksums.
355 */
epoger@google.com5f6a0072013-01-31 16:30:55 +0000356 static void complete_bitmap(SkBitmap* bitmap) {
357 force_all_opaque(*bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000358 }
359
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000360 static void installFilter(SkCanvas* canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000361
reed@google.comaef73612012-11-16 13:41:45 +0000362 static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000363 SkAutoCanvasRestore acr(canvas, true);
364
365 if (!isPDF) {
366 canvas->concat(gm->getInitialTransform());
367 }
368 installFilter(canvas);
reed@google.comaef73612012-11-16 13:41:45 +0000369 gm->setCanvasIsDeferred(isDeferred);
epoger@google.comde961632012-10-26 18:56:36 +0000370 gm->draw(canvas);
371 canvas->setDrawFilter(NULL);
372 }
373
epoger@google.com6f6568b2013-03-22 17:29:46 +0000374 static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000375 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000376 SkBitmap* bitmap,
377 bool deferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000378 SkISize size (gm->getISize());
379 setup_bitmap(gRec, size, bitmap);
380
381 SkAutoTUnref<SkCanvas> canvas;
382
383 if (gRec.fBackend == kRaster_Backend) {
384 SkAutoTUnref<SkDevice> device(new SkDevice(*bitmap));
385 if (deferred) {
386 canvas.reset(new SkDeferredCanvas(device));
387 } else {
388 canvas.reset(new SkCanvas(device));
389 }
reed@google.comaef73612012-11-16 13:41:45 +0000390 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000391 canvas->flush();
392 }
393#if SK_SUPPORT_GPU
394 else { // GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000395 SkAutoTUnref<SkDevice> device(SkGpuDevice::Create(gpuTarget));
epoger@google.comde961632012-10-26 18:56:36 +0000396 if (deferred) {
397 canvas.reset(new SkDeferredCanvas(device));
398 } else {
399 canvas.reset(new SkCanvas(device));
400 }
reed@google.comaef73612012-11-16 13:41:45 +0000401 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000402 // the device is as large as the current rendertarget, so
403 // we explicitly only readback the amount we expect (in
404 // size) overwrite our previous allocation
405 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
406 size.fHeight);
407 canvas->readPixels(bitmap, 0, 0);
408 }
409#endif
epoger@google.com5f6a0072013-01-31 16:30:55 +0000410 complete_bitmap(bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000411 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000412 }
413
414 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
junov@chromium.orgc938c482012-12-19 15:24:38 +0000415 SkPicture* pict, SkBitmap* bitmap,
416 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +0000417 SkISize size = gm->getISize();
418 setup_bitmap(gRec, size, bitmap);
419 SkCanvas canvas(*bitmap);
420 installFilter(&canvas);
junov@chromium.orgc938c482012-12-19 15:24:38 +0000421 canvas.scale(scale, scale);
epoger@google.comde961632012-10-26 18:56:36 +0000422 canvas.drawPicture(*pict);
epoger@google.com5f6a0072013-01-31 16:30:55 +0000423 complete_bitmap(bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000424 }
425
426 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
427#ifdef SK_SUPPORT_PDF
428 SkMatrix initialTransform = gm->getInitialTransform();
429 SkISize pageSize = gm->getISize();
430 SkPDFDevice* dev = NULL;
431 if (initialTransform.isIdentity()) {
432 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
433 } else {
434 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
435 SkIntToScalar(pageSize.height()));
436 initialTransform.mapRect(&content);
437 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
438 SkIntToScalar(pageSize.height()));
439 SkISize contentSize =
440 SkISize::Make(SkScalarRoundToInt(content.width()),
441 SkScalarRoundToInt(content.height()));
442 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
443 }
444 SkAutoUnref aur(dev);
445
446 SkCanvas c(dev);
reed@google.comaef73612012-11-16 13:41:45 +0000447 invokeGM(gm, &c, true, false);
epoger@google.comde961632012-10-26 18:56:36 +0000448
449 SkPDFDocument doc;
450 doc.appendPage(dev);
451 doc.emitPDF(&pdf);
452#endif
453 }
454
455 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
456#ifdef SK_SUPPORT_XPS
457 SkISize size = gm->getISize();
458
459 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
460 SkIntToScalar(size.height()));
461 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
462 static const SkScalar upm = 72 * inchesPerMeter;
463 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
464 static const SkScalar ppm = 200 * inchesPerMeter;
465 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
466
467 SkXPSDevice* dev = new SkXPSDevice();
468 SkAutoUnref aur(dev);
469
470 SkCanvas c(dev);
471 dev->beginPortfolio(&xps);
472 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000473 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000474 dev->endSheet();
475 dev->endPortfolio();
476
477#endif
478 }
479
epoger@google.com6f6568b2013-03-22 17:29:46 +0000480 ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
481 const char renderModeDescriptor [], const SkString& name,
482 SkBitmap& bitmap, SkDynamicMemoryWStream* document) {
epoger@google.comde961632012-10-26 18:56:36 +0000483 SkString path;
484 bool success = false;
485 if (gRec.fBackend == kRaster_Backend ||
486 gRec.fBackend == kGPU_Backend ||
487 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
488
epoger@google.com37269602013-01-19 04:21:27 +0000489 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
490 "png");
epoger@google.comde961632012-10-26 18:56:36 +0000491 success = write_bitmap(path, bitmap);
492 }
493 if (kPDF_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000494 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
495 "pdf");
epoger@google.comde961632012-10-26 18:56:36 +0000496 success = write_document(path, *document);
497 }
498 if (kXPS_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000499 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
500 "xps");
epoger@google.comde961632012-10-26 18:56:36 +0000501 success = write_document(path, *document);
502 }
503 if (success) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000504 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000505 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000506 gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000507 ErrorCombination errors(kWritingReferenceImage_ErrorType);
508 RecordError(errors, name, renderModeDescriptor);
509 return errors;
epoger@google.comde961632012-10-26 18:56:36 +0000510 }
511 }
512
epoger@google.com37269602013-01-19 04:21:27 +0000513 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000514 * Log more detail about the mistmatch between expectedBitmap and
515 * actualBitmap.
516 */
517 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
518 const char *testName) {
519 const int expectedWidth = expectedBitmap.width();
520 const int expectedHeight = expectedBitmap.height();
521 const int width = actualBitmap.width();
522 const int height = actualBitmap.height();
523 if ((expectedWidth != width) || (expectedHeight != height)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000524 gm_fprintf(stderr, "---- %s: dimension mismatch --"
525 " expected [%d %d], actual [%d %d]\n",
526 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000527 return;
528 }
529
530 if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
531 (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000532 gm_fprintf(stderr, "---- %s: not computing max per-channel"
533 " pixel mismatch because non-8888\n", testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000534 return;
535 }
536
537 SkAutoLockPixels alp0(expectedBitmap);
538 SkAutoLockPixels alp1(actualBitmap);
539 int errR = 0;
540 int errG = 0;
541 int errB = 0;
542 int errA = 0;
543 int differingPixels = 0;
544
545 for (int y = 0; y < height; ++y) {
546 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
547 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
548 for (int x = 0; x < width; ++x) {
549 SkPMColor expectedPixel = *expectedPixelPtr++;
550 SkPMColor actualPixel = *actualPixelPtr++;
551 if (expectedPixel != actualPixel) {
552 differingPixels++;
553 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
554 (int)SkGetPackedR32(actualPixel)));
555 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
556 (int)SkGetPackedG32(actualPixel)));
557 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
558 (int)SkGetPackedB32(actualPixel)));
559 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
560 (int)SkGetPackedA32(actualPixel)));
561 }
562 }
563 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000564 gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
565 " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
566 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000567 }
568
569 /**
epoger@google.com6f6568b2013-03-22 17:29:46 +0000570 * Compares actual checksum to expectations, returning the set of errors
571 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000572 *
573 * If fMismatchPath has been set, and there are pixel diffs, then the
574 * actual bitmap will be written out to a file within fMismatchPath.
575 *
576 * @param expectations what expectations to compare actualBitmap against
577 * @param actualBitmap the image we actually generated
578 * @param baseNameString name of test without renderModeDescriptor added
579 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
580 * @param addToJsonSummary whether to add these results (both actual and
581 * expected) to the JSON summary
582 *
583 * TODO: For now, addToJsonSummary is only set to true within
584 * compare_test_results_to_stored_expectations(), so results of our
585 * in-memory comparisons (Rtree vs regular, etc.) are not written to the
586 * JSON summary. We may wish to change that.
587 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000588 ErrorCombination compare_to_expectations(Expectations expectations,
589 const SkBitmap& actualBitmap,
590 const SkString& baseNameString,
591 const char renderModeDescriptor[],
592 bool addToJsonSummary=false) {
593 ErrorCombination errors;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000594 Checksum actualChecksum = SkBitmapChecksummer::Compute64(actualBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000595 SkString completeNameString = baseNameString;
596 completeNameString.append(renderModeDescriptor);
597 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000598
epoger@google.com37269602013-01-19 04:21:27 +0000599 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000600 errors.add(kMissingExpectations_ErrorType);
601 } else if (!expectations.match(actualChecksum)) {
602 errors.add(kImageMismatch_ErrorType);
epoger@google.com84a18022013-02-01 20:39:15 +0000603
604 // Write out the "actuals" for any mismatches, if we have
605 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000606 if (fMismatchPath) {
607 SkString path =
608 make_filename(fMismatchPath, renderModeDescriptor,
609 baseNameString.c_str(), "png");
610 write_bitmap(path, actualBitmap);
611 }
epoger@google.com84a18022013-02-01 20:39:15 +0000612
613 // If we have access to a single expected bitmap, log more
614 // detail about the mismatch.
615 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
616 if (NULL != expectedBitmapPtr) {
617 report_bitmap_diffs(*expectedBitmapPtr, actualBitmap, completeName);
618 }
epoger@google.coma243b222013-01-17 17:54:28 +0000619 }
epoger@google.com6f6568b2013-03-22 17:29:46 +0000620 RecordError(errors, baseNameString, renderModeDescriptor);
epoger@google.coma243b222013-01-17 17:54:28 +0000621
epoger@google.com37269602013-01-19 04:21:27 +0000622 if (addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000623 add_actual_results_to_json_summary(completeName, actualChecksum, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000624 expectations.ignoreFailure());
625 add_expected_results_to_json_summary(completeName, expectations);
626 }
epoger@google.coma243b222013-01-17 17:54:28 +0000627
epoger@google.com6f6568b2013-03-22 17:29:46 +0000628 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000629 }
630
epoger@google.com37269602013-01-19 04:21:27 +0000631 /**
632 * Add this result to the appropriate JSON collection of actual results,
633 * depending on status.
634 */
635 void add_actual_results_to_json_summary(const char testName[],
636 Checksum actualChecksum,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000637 ErrorCombination result,
epoger@google.com37269602013-01-19 04:21:27 +0000638 bool ignoreFailure) {
639 Json::Value actualResults;
640 actualResults[kJsonKey_ActualResults_AnyStatus_Checksum] =
641 asJsonValue(actualChecksum);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000642 if (result.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +0000643 this->fJsonActualResults_Succeeded[testName] = actualResults;
644 } else {
645 if (ignoreFailure) {
646 // TODO: Once we have added the ability to compare
647 // actual results against expectations in a JSON file
648 // (where we can set ignore-failure to either true or
epoger@google.com84a18022013-02-01 20:39:15 +0000649 // false), add test cases that exercise ignored
epoger@google.com6f6568b2013-03-22 17:29:46 +0000650 // failures (both for kMissingExpectations_ErrorType
651 // and kImageMismatch_ErrorType).
epoger@google.com37269602013-01-19 04:21:27 +0000652 this->fJsonActualResults_FailureIgnored[testName] =
653 actualResults;
654 } else {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000655 if (result.includes(kMissingExpectations_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000656 // TODO: What about the case where there IS an
657 // expected image checksum, but that gm test
658 // doesn't actually run? For now, those cases
659 // will always be ignored, because gm only looks
660 // at expectations that correspond to gm tests
661 // that were actually run.
662 //
663 // Once we have the ability to express
664 // expectations as a JSON file, we should fix this
665 // (and add a test case for which an expectation
666 // is given but the test is never run).
667 this->fJsonActualResults_NoComparison[testName] =
668 actualResults;
epoger@google.comeb066362013-03-08 09:39:36 +0000669 }
epoger@google.com6f6568b2013-03-22 17:29:46 +0000670 if (result.includes(kImageMismatch_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000671 this->fJsonActualResults_Failed[testName] = actualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000672 }
673 }
674 }
675 }
676
677 /**
678 * Add this test to the JSON collection of expected results.
679 */
680 void add_expected_results_to_json_summary(const char testName[],
681 Expectations expectations) {
682 // For now, we assume that this collection starts out empty and we
683 // just fill it in as we go; once gm accepts a JSON file as input,
684 // we'll have to change that.
685 Json::Value expectedResults;
686 expectedResults[kJsonKey_ExpectedResults_Checksums] =
687 expectations.allowedChecksumsAsJson();
688 expectedResults[kJsonKey_ExpectedResults_IgnoreFailure] =
689 expectations.ignoreFailure();
690 this->fJsonExpectedResults[testName] = expectedResults;
691 }
692
693 /**
694 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
695 *
696 * @param gm which test generated the actualBitmap
697 * @param gRec
698 * @param writePath unless this is NULL, write out actual images into this
699 * directory
700 * @param actualBitmap bitmap generated by this run
701 * @param pdf
702 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000703 ErrorCombination compare_test_results_to_stored_expectations(
epoger@google.com37269602013-01-19 04:21:27 +0000704 GM* gm, const ConfigData& gRec, const char writePath[],
705 SkBitmap& actualBitmap, SkDynamicMemoryWStream* pdf) {
706
epoger@google.coma243b222013-01-17 17:54:28 +0000707 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000708 ErrorCombination errors;
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000709
epoger@google.com6f6568b2013-03-22 17:29:46 +0000710 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
epoger@google.com37269602013-01-19 04:21:27 +0000711 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
712 /*
713 * Get the expected results for this test, as one or more allowed
714 * checksums. The current implementation of expectationsSource
715 * get this by computing the checksum of a single PNG file on disk.
716 *
717 * TODO(epoger): This relies on the fact that
718 * force_all_opaque() was called on the bitmap before it
719 * was written to disk as a PNG in the first place. If
720 * not, the checksum returned here may not match the
721 * checksum of actualBitmap, which *has* been run through
722 * force_all_opaque().
epoger@google.com5f6a0072013-01-31 16:30:55 +0000723 * See comments above complete_bitmap() for more detail.
epoger@google.com37269602013-01-19 04:21:27 +0000724 */
725 Expectations expectations = expectationsSource->get(name.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000726 errors.add(compare_to_expectations(expectations, actualBitmap,
727 name, "", true));
epoger@google.com37269602013-01-19 04:21:27 +0000728 } else {
729 // If we are running without expectations, we still want to
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000730 // record the actual results.
epoger@google.com5f6a0072013-01-31 16:30:55 +0000731 Checksum actualChecksum =
732 SkBitmapChecksummer::Compute64(actualBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000733 add_actual_results_to_json_summary(name.c_str(), actualChecksum,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000734 ErrorCombination(kMissingExpectations_ErrorType),
epoger@google.com37269602013-01-19 04:21:27 +0000735 false);
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000736 }
epoger@google.com37269602013-01-19 04:21:27 +0000737
738 // TODO: Consider moving this into compare_to_expectations(),
739 // similar to fMismatchPath... for now, we don't do that, because
740 // we don't want to write out the actual bitmaps for all
741 // renderModes of all tests! That would be a lot of files.
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000742 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000743 errors.add(write_reference_image(gRec, writePath, "",
744 name, actualBitmap, pdf));
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000745 }
epoger@google.com37269602013-01-19 04:21:27 +0000746
epoger@google.com6f6568b2013-03-22 17:29:46 +0000747 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000748 }
749
epoger@google.com37269602013-01-19 04:21:27 +0000750 /**
751 * Compare actualBitmap to referenceBitmap.
752 *
753 * @param gm which test generated the bitmap
754 * @param gRec
755 * @param renderModeDescriptor
756 * @param actualBitmap actual bitmap generated by this run
757 * @param referenceBitmap bitmap we expected to be generated
758 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000759 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.com37269602013-01-19 04:21:27 +0000760 GM* gm, const ConfigData& gRec, const char renderModeDescriptor [],
761 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
762
763 SkASSERT(referenceBitmap);
764 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com84a18022013-02-01 20:39:15 +0000765 Expectations expectations(*referenceBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000766 return compare_to_expectations(expectations, actualBitmap,
767 name, renderModeDescriptor);
768 }
769
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000770 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
771 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +0000772 // Pictures are refcounted so must be on heap
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000773 SkPicture* pict;
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000774 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
775 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +0000776
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000777 if (kTileGrid_BbhType == bbhType) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000778 SkTileGridPicture::TileGridInfo info;
779 info.fMargin.setEmpty();
780 info.fOffset.setZero();
781 info.fTileInterval.set(16, 16);
782 pict = new SkTileGridPicture(width, height, info);
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000783 } else {
784 pict = new SkPicture;
785 }
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000786 if (kNone_BbhType != bbhType) {
787 recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
788 }
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000789 SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +0000790 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +0000791 invokeGM(gm, cv, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000792 pict->endRecording();
793
794 return pict;
795 }
796
797 static SkPicture* stream_to_new_picture(const SkPicture& src) {
798
799 // To do in-memory commiunications with a stream, we need to:
800 // * create a dynamic memory stream
801 // * copy it into a buffer
802 // * create a read stream from it
803 // ?!?!
804
805 SkDynamicMemoryWStream storage;
806 src.serialize(&storage);
807
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000808 size_t streamSize = storage.getOffset();
epoger@google.comde961632012-10-26 18:56:36 +0000809 SkAutoMalloc dstStorage(streamSize);
810 void* dst = dstStorage.get();
811 //char* dst = new char [streamSize];
812 //@todo thudson 22 April 2011 when can we safely delete [] dst?
813 storage.copyTo(dst);
814 SkMemoryStream pictReadback(dst, streamSize);
815 SkPicture* retval = new SkPicture (&pictReadback);
816 return retval;
817 }
818
819 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +0000820 // Depending on flags, possibly compare to an expected image.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000821 ErrorCombination test_drawing(GM* gm,
822 const ConfigData& gRec,
823 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000824 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000825 SkBitmap* bitmap) {
epoger@google.comde961632012-10-26 18:56:36 +0000826 SkDynamicMemoryWStream document;
827
828 if (gRec.fBackend == kRaster_Backend ||
829 gRec.fBackend == kGPU_Backend) {
830 // Early exit if we can't generate the image.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000831 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000832 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +0000833 // TODO: Add a test to exercise what the stdout and
834 // JSON look like if we get an "early error" while
835 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +0000836 return errors;
837 }
838 } else if (gRec.fBackend == kPDF_Backend) {
839 generate_pdf(gm, document);
840#if CAN_IMAGE_PDF
841 SkAutoDataUnref data(document.copyToData());
842 SkMemoryStream stream(data->data(), data->size());
843 SkPDFDocumentToBitmap(&stream, bitmap);
844#endif
845 } else if (gRec.fBackend == kXPS_Backend) {
846 generate_xps(gm, document);
847 }
epoger@google.com37269602013-01-19 04:21:27 +0000848 return compare_test_results_to_stored_expectations(
849 gm, gRec, writePath, *bitmap, &document);
epoger@google.comde961632012-10-26 18:56:36 +0000850 }
851
epoger@google.com6f6568b2013-03-22 17:29:46 +0000852 ErrorCombination test_deferred_drawing(GM* gm,
853 const ConfigData& gRec,
854 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000855 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +0000856 SkDynamicMemoryWStream document;
857
858 if (gRec.fBackend == kRaster_Backend ||
859 gRec.fBackend == kGPU_Backend) {
860 SkBitmap bitmap;
861 // Early exit if we can't generate the image, but this is
862 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000863 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000864 // TODO(epoger): This logic is the opposite of what is
865 // described above... if we succeeded in generating the
866 // -deferred image, we exit early! We should fix this
867 // ASAP, because it is hiding -deferred errors... but for
868 // now, I'm leaving the logic as it is so that the
869 // refactoring change
870 // https://codereview.chromium.org/12992003/ is unblocked.
871 //
872 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
873 // ('image-surface gm test is failing in "deferred" mode,
874 // and gm is not reporting the failure')
875 if (errors.isEmpty()) {
876 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000877 }
epoger@google.com37269602013-01-19 04:21:27 +0000878 return compare_test_results_to_reference_bitmap(
879 gm, gRec, "-deferred", bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000880 }
epoger@google.com6f6568b2013-03-22 17:29:46 +0000881 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000882 }
883
epoger@google.com6f6568b2013-03-22 17:29:46 +0000884 ErrorCombination test_pipe_playback(GM* gm,
885 const ConfigData& gRec,
886 const SkBitmap& referenceBitmap) {
887 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +0000888 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
889 SkBitmap bitmap;
890 SkISize size = gm->getISize();
891 setup_bitmap(gRec, size, &bitmap);
892 SkCanvas canvas(bitmap);
scroggo@google.com0b735632013-03-19 17:38:50 +0000893 installFilter(&canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000894 PipeController pipeController(&canvas);
895 SkGPipeWriter writer;
896 SkCanvas* pipeCanvas = writer.startRecording(
897 &pipeController, gPipeWritingFlagCombos[i].flags);
reed@google.comaef73612012-11-16 13:41:45 +0000898 invokeGM(gm, pipeCanvas, false, false);
epoger@google.com5f6a0072013-01-31 16:30:55 +0000899 complete_bitmap(&bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000900 writer.endRecording();
901 SkString string("-pipe");
902 string.append(gPipeWritingFlagCombos[i].name);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000903 errors.add(compare_test_results_to_reference_bitmap(
904 gm, gRec, string.c_str(), bitmap, &referenceBitmap));
905 if (!errors.isEmpty()) {
epoger@google.comde961632012-10-26 18:56:36 +0000906 break;
907 }
908 }
909 return errors;
910 }
911
epoger@google.com6f6568b2013-03-22 17:29:46 +0000912 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
913 const SkBitmap& referenceBitmap) {
914 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +0000915 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
916 SkBitmap bitmap;
917 SkISize size = gm->getISize();
918 setup_bitmap(gRec, size, &bitmap);
919 SkCanvas canvas(bitmap);
scroggo@google.com0b735632013-03-19 17:38:50 +0000920 installFilter(&canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000921 TiledPipeController pipeController(bitmap);
922 SkGPipeWriter writer;
923 SkCanvas* pipeCanvas = writer.startRecording(
924 &pipeController, gPipeWritingFlagCombos[i].flags);
reed@google.comaef73612012-11-16 13:41:45 +0000925 invokeGM(gm, pipeCanvas, false, false);
epoger@google.com5f6a0072013-01-31 16:30:55 +0000926 complete_bitmap(&bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000927 writer.endRecording();
928 SkString string("-tiled pipe");
929 string.append(gPipeWritingFlagCombos[i].name);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000930 errors.add(compare_test_results_to_reference_bitmap(
931 gm, gRec, string.c_str(), bitmap, &referenceBitmap));
932 if (!errors.isEmpty()) {
epoger@google.comde961632012-10-26 18:56:36 +0000933 break;
934 }
935 }
936 return errors;
937 }
epoger@google.come8ebeb12012-10-29 16:42:11 +0000938
939 //
940 // member variables.
941 // They are public for now, to allow easier setting by tool_main().
942 //
943
epoger@google.come8ebeb12012-10-29 16:42:11 +0000944 bool fUseFileHierarchy;
epoger@google.com6f6568b2013-03-22 17:29:46 +0000945 ErrorCombination fIgnorableErrorCombination;
epoger@google.come8ebeb12012-10-29 16:42:11 +0000946
junov@chromium.org95146eb2013-01-11 21:04:40 +0000947 const char* fMismatchPath;
948
epoger@google.com57f7abc2012-11-13 03:41:55 +0000949 // information about all failed tests we have encountered so far
950 SkTArray<FailRec> fFailedTests;
951
epoger@google.com37269602013-01-19 04:21:27 +0000952 // Where to read expectations (expected image checksums, etc.) from.
953 // If unset, we don't do comparisons.
954 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
955
956 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +0000957 Json::Value fJsonExpectedResults;
958 Json::Value fJsonActualResults_Failed;
959 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +0000960 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +0000961 Json::Value fJsonActualResults_Succeeded;
962
epoger@google.comde961632012-10-26 18:56:36 +0000963}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +0000964
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000965#if SK_SUPPORT_GPU
966static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
967#else
968static const GLContextType kDontCare_GLContextType = 0;
969#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +0000970
971// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000972// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +0000973static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
974 kWrite_ConfigFlag;
975
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000976static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000977 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +0000978#if 0 // stop testing this (for now at least) since we want to remove support for it (soon please!!!)
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000979 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +0000980#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000981 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
982#if SK_SUPPORT_GPU
983 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
984 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", true },
985 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
bsalomon@google.com7361f542012-04-19 19:15:35 +0000986 /* The debug context does not generate images */
scroggo@google.com0f567c62013-03-20 15:35:08 +0000987 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", GR_DEBUG},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000988#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000989 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
990 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000991#endif // SK_ANGLE
992#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000993 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000994#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000995#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +0000996#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000997 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000998 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000999#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001000#ifdef SK_SUPPORT_PDF
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001001 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001002#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001003};
1004
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001005static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001006 SkString result;
1007 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001008 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1009 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001010 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001011 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001012 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001013 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001014 result.append("]\n");
1015 result.appendf("The default value is: \"");
1016 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1017 if (gRec[i].fRunByDefault) {
1018 if (i > 0) {
1019 result.append(" ");
1020 }
1021 result.appendf("%s", gRec[i].fName);
1022 }
1023 }
1024 result.appendf("\"");
1025
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001026 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001027}
scroggo@google.com7d519302013-03-19 17:28:10 +00001028
epoger@google.com6f6568b2013-03-22 17:29:46 +00001029// Macro magic to convert a numeric preprocessor token into a string.
1030// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1031// This should probably be moved into one of our common headers...
1032#define TOSTRING_INTERNAL(x) #x
1033#define TOSTRING(x) TOSTRING_INTERNAL(x)
1034
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001035// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001036DEFINE_string(config, "", configUsage().c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001037DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
1038DEFINE_bool(enableMissingWarning, true, "Print message to stderr (but don't fail) if "
1039 "unable to read a reference image for any tests.");
1040DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1041DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1042#if SK_SUPPORT_GPU
1043DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001044 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1045 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001046#endif
1047DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1048 "when reading/writing files.");
1049DEFINE_string(match, "", "Only run tests whose name includes this substring/these substrings "
1050 "(more than one can be supplied, separated by spaces).");
1051DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1052 "pixel mismatches into this directory.");
1053DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1054 "testIndex %% divisor == remainder.");
1055DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1056DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1057DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1058 "any differences between those and the newly generated ones.");
1059DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1060DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1061DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1062DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
1063DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1064DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1065DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1066 "factors to be used for tileGrid playback testing. Default value: 1.0");
1067DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
1068DEFINE_bool2(verbose, v, false, "Print diagnostics (e.g. list each config to be tested).");
1069DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
1070DEFINE_string2(writePicturePath, wp, "", "Write .skp files into this directory.");
1071
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001072static int findConfig(const char config[]) {
1073 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1074 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001075 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001076 }
1077 }
1078 return -1;
1079}
1080
reed@google.comb2a51622011-10-31 16:30:04 +00001081static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
1082 if (0 == array.count()) {
1083 // no names, so don't skip anything
1084 return false;
1085 }
1086 for (int i = 0; i < array.count(); ++i) {
1087 if (strstr(name, array[i])) {
1088 // found the name, so don't skip
1089 return false;
1090 }
1091 }
1092 return true;
1093}
1094
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001095namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001096#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +00001097SkAutoTUnref<GrContext> gGrContext;
1098/**
bsalomon@google.comc7a24d22012-11-01 18:03:48 +00001099 * Sets the global GrContext, accessible by individual GMs
bsalomon@google.com7361f542012-04-19 19:15:35 +00001100 */
caryclark@google.com13130862012-06-06 12:10:45 +00001101static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +00001102 SkSafeRef(grContext);
1103 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001104}
bsalomon@google.com7361f542012-04-19 19:15:35 +00001105
1106/**
1107 * Gets the global GrContext, can be called by GM tests.
1108 */
caryclark@google.com13130862012-06-06 12:10:45 +00001109GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +00001110GrContext* GetGr() {
1111 return gGrContext.get();
1112}
1113
1114/**
1115 * Sets the global GrContext and then resets it to its previous value at
1116 * destruction.
1117 */
1118class AutoResetGr : SkNoncopyable {
1119public:
1120 AutoResetGr() : fOld(NULL) {}
1121 void set(GrContext* context) {
1122 SkASSERT(NULL == fOld);
1123 fOld = GetGr();
1124 SkSafeRef(fOld);
1125 SetGr(context);
1126 }
1127 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1128private:
1129 GrContext* fOld;
1130};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001131#else
epoger@google.com80724df2013-03-21 13:49:54 +00001132GrContext* GetGr();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001133GrContext* GetGr() { return NULL; }
1134#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001135}
1136
reed@google.comfb2cd422013-01-04 14:43:03 +00001137template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1138 int index = array->find(value);
1139 if (index < 0) {
1140 *array->append() = value;
1141 }
1142}
1143
epoger@google.com80724df2013-03-21 13:49:54 +00001144/**
1145 * Run this test in a number of different configs (8888, 565, PDF,
1146 * etc.), confirming that the resulting bitmaps match expectations
1147 * (which may be different for each config).
1148 *
1149 * Returns all errors encountered while doing so.
1150 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001151ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1152 GrContextFactory *grFactory);
1153ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1154 GrContextFactory *grFactory) {
1155 ErrorCombination errorsForAllConfigs;
epoger@google.com80724df2013-03-21 13:49:54 +00001156 uint32_t gmFlags = gm->getFlags();
1157
epoger@google.com80724df2013-03-21 13:49:54 +00001158 for (int i = 0; i < configs.count(); i++) {
1159 ConfigData config = gRec[configs[i]];
1160
1161 // Skip any tests that we don't even need to try.
1162 if ((kPDF_Backend == config.fBackend) &&
1163 (!FLAGS_pdf|| (gmFlags & GM::kSkipPDF_Flag))) {
1164 continue;
1165 }
1166 if ((gmFlags & GM::kSkip565_Flag) &&
1167 (kRaster_Backend == config.fBackend) &&
1168 (SkBitmap::kRGB_565_Config == config.fConfig)) {
1169 continue;
1170 }
1171 if ((gmFlags & GM::kSkipGPU_Flag) &&
1172 kGPU_Backend == config.fBackend) {
1173 continue;
1174 }
1175
1176 // Now we know that we want to run this test and record its
1177 // success or failure.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001178 ErrorCombination errorsForThisConfig;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001179 GrSurface* gpuTarget = NULL;
epoger@google.com80724df2013-03-21 13:49:54 +00001180#if SK_SUPPORT_GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001181 SkAutoTUnref<GrSurface> auGpuTarget;
epoger@google.com80724df2013-03-21 13:49:54 +00001182 AutoResetGr autogr;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001183 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
epoger@google.com80724df2013-03-21 13:49:54 +00001184 GrContext* gr = grFactory->get(config.fGLContextType);
1185 bool grSuccess = false;
1186 if (gr) {
1187 // create a render target to back the device
1188 GrTextureDesc desc;
1189 desc.fConfig = kSkia8888_GrPixelConfig;
1190 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1191 desc.fWidth = gm->getISize().width();
1192 desc.fHeight = gm->getISize().height();
1193 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001194 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1195 if (NULL != auGpuTarget) {
1196 gpuTarget = auGpuTarget;
1197 grSuccess = true;
epoger@google.com80724df2013-03-21 13:49:54 +00001198 autogr.set(gr);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001199 // Set the user specified cache limits if non-default.
1200 size_t bytes;
1201 int count;
1202 gr->getTextureCacheLimits(&count, &bytes);
1203 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1204 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1205 }
1206 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1207 count = gGpuCacheSizeCount;
1208 }
1209 gr->setTextureCacheLimits(count, bytes);
epoger@google.com80724df2013-03-21 13:49:54 +00001210 }
epoger@google.com80724df2013-03-21 13:49:54 +00001211 }
1212 if (!grSuccess) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001213 errorsForThisConfig.add(kNoGpuContext_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001214 }
1215 }
1216#endif
1217
1218 SkBitmap comparisonBitmap;
1219
1220 const char* writePath;
1221 if (FLAGS_writePath.count() == 1) {
1222 writePath = FLAGS_writePath[0];
1223 } else {
1224 writePath = NULL;
1225 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001226 if (errorsForThisConfig.isEmpty()) {
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001227 errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
1228 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001229 }
1230
epoger@google.com6f6568b2013-03-22 17:29:46 +00001231 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1232 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1233 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001234 gpuTarget));
epoger@google.com80724df2013-03-21 13:49:54 +00001235 }
1236
epoger@google.com6f6568b2013-03-22 17:29:46 +00001237 errorsForAllConfigs.add(errorsForThisConfig);
epoger@google.com80724df2013-03-21 13:49:54 +00001238 }
1239 return errorsForAllConfigs;
1240}
1241
1242/**
1243 * Run this test in a number of different drawing modes (pipe,
1244 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1245 * *exactly* match comparisonBitmap.
1246 *
1247 * Returns all errors encountered while doing so.
1248 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001249ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1250 const SkBitmap &comparisonBitmap,
1251 const SkTDArray<SkScalar> &tileGridReplayScales);
1252ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1253 const SkBitmap &comparisonBitmap,
1254 const SkTDArray<SkScalar> &tileGridReplayScales) {
1255 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001256 uint32_t gmFlags = gm->getFlags();
1257
1258 // run the picture centric GM steps
1259 if (!(gmFlags & GM::kSkipPicture_Flag)) {
1260
epoger@google.com6f6568b2013-03-22 17:29:46 +00001261 ErrorCombination pictErrors;
epoger@google.com80724df2013-03-21 13:49:54 +00001262
1263 //SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
1264 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1265 SkAutoUnref aur(pict);
1266
1267 if (FLAGS_replay) {
1268 SkBitmap bitmap;
1269 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001270 pictErrors.add(gmmain.compare_test_results_to_reference_bitmap(
1271 gm, compareConfig, "-replay", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001272 }
1273
epoger@google.com6f6568b2013-03-22 17:29:46 +00001274 if ((pictErrors.isEmpty()) && FLAGS_serialize) {
epoger@google.com80724df2013-03-21 13:49:54 +00001275 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1276 SkAutoUnref aurr(repict);
1277
1278 SkBitmap bitmap;
1279 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001280 pictErrors.add(gmmain.compare_test_results_to_reference_bitmap(
1281 gm, compareConfig, "-serialize", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001282 }
1283
1284 if (FLAGS_writePicturePath.count() == 1) {
1285 const char* pictureSuffix = "skp";
1286 SkString path = make_filename(FLAGS_writePicturePath[0], "",
1287 gm->shortName(), pictureSuffix);
1288 SkFILEWStream stream(path.c_str());
1289 pict->serialize(&stream);
1290 }
1291
epoger@google.com6f6568b2013-03-22 17:29:46 +00001292 errorsForAllModes.add(pictErrors);
epoger@google.com80724df2013-03-21 13:49:54 +00001293 }
1294
1295 // TODO: add a test in which the RTree rendering results in a
1296 // different bitmap than the standard rendering. It should
1297 // show up as failed in the JSON summary, and should be listed
1298 // in the stdout also.
1299 if (!(gmFlags & GM::kSkipPicture_Flag) && FLAGS_rtree) {
1300 SkPicture* pict = gmmain.generate_new_picture(
1301 gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
1302 SkAutoUnref aur(pict);
1303 SkBitmap bitmap;
1304 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001305 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1306 gm, compareConfig, "-rtree", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001307 }
1308
1309 if (!(gmFlags & GM::kSkipPicture_Flag) && FLAGS_tileGrid) {
1310 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1311 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1312 if ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1) {
1313 continue;
1314 }
1315 // We record with the reciprocal scale to obtain a replay
1316 // result that can be validated against comparisonBitmap.
1317 SkScalar recordScale = SkScalarInvert(replayScale);
1318 SkPicture* pict = gmmain.generate_new_picture(
1319 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag, recordScale);
1320 SkAutoUnref aur(pict);
1321 SkBitmap bitmap;
1322 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap, replayScale);
1323 SkString suffix("-tilegrid");
1324 if (SK_Scalar1 != replayScale) {
1325 suffix += "-scale-";
1326 suffix.appendScalar(replayScale);
1327 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001328 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1329 gm, compareConfig, suffix.c_str(), bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001330 }
1331 }
1332
1333 // run the pipe centric GM steps
1334 if (!(gmFlags & GM::kSkipPipe_Flag)) {
1335
epoger@google.com6f6568b2013-03-22 17:29:46 +00001336 ErrorCombination pipeErrors;
epoger@google.com80724df2013-03-21 13:49:54 +00001337
1338 if (FLAGS_pipe) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001339 pipeErrors.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001340 }
1341
epoger@google.com6f6568b2013-03-22 17:29:46 +00001342 if ((pipeErrors.isEmpty()) &&
epoger@google.com80724df2013-03-21 13:49:54 +00001343 FLAGS_tiledPipe && !(gmFlags & GM::kSkipTiled_Flag)) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001344 pipeErrors.add(gmmain.test_tiled_pipe_playback(gm, compareConfig, comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001345 }
1346
epoger@google.com6f6568b2013-03-22 17:29:46 +00001347 errorsForAllModes.add(pipeErrors);
epoger@google.com80724df2013-03-21 13:49:54 +00001348 }
1349 return errorsForAllModes;
1350}
1351
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001352int tool_main(int argc, char** argv);
1353int tool_main(int argc, char** argv) {
1354
1355#if SK_ENABLE_INST_COUNT
1356 gPrintInstCount = true;
1357#endif
1358
1359 SkGraphics::Init();
1360 // we don't need to see this during a run
1361 gSkSuppressFontCachePurgeSpew = true;
1362
1363 setSystemPreferences();
1364 GMMain gmmain;
1365
1366 SkTDArray<size_t> configs;
1367 SkTDArray<size_t> excludeConfigs;
1368 bool userConfig = false;
1369
1370 SkString usage;
1371 usage.printf("Run the golden master tests.\n");
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00001372 SkCommandLineFlags::SetUsage(usage.c_str());
1373 SkCommandLineFlags::Parse(argc, argv);
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001374
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001375 gmmain.fUseFileHierarchy = FLAGS_hierarchy;
1376 if (FLAGS_mismatchPath.count() == 1) {
1377 gmmain.fMismatchPath = FLAGS_mismatchPath[0];
1378 }
1379
1380 for (int i = 0; i < FLAGS_config.count(); i++) {
1381 int index = findConfig(FLAGS_config[i]);
1382 if (index >= 0) {
1383 appendUnique<size_t>(&configs, index);
1384 userConfig = true;
scroggo@google.com0b735632013-03-19 17:38:50 +00001385 } else {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001386 gm_fprintf(stderr, "unrecognized config %s\n", FLAGS_config[i]);
scroggo@google.com7d519302013-03-19 17:28:10 +00001387 return -1;
1388 }
1389 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001390
1391 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1392 int index = findConfig(FLAGS_excludeConfig[i]);
1393 if (index >= 0) {
1394 *excludeConfigs.append() = index;
1395 } else {
1396 gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1397 return -1;
1398 }
1399 }
1400
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001401 int moduloRemainder = -1;
1402 int moduloDivisor = -1;
1403
1404 if (FLAGS_modulo.count() == 2) {
1405 moduloRemainder = atoi(FLAGS_modulo[0]);
1406 moduloDivisor = atoi(FLAGS_modulo[1]);
1407 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
1408 gm_fprintf(stderr, "invalid modulo values.");
1409 return -1;
1410 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001411 }
reed@google.com873cb1e2010-12-23 15:00:45 +00001412
epoger@google.com6f6568b2013-03-22 17:29:46 +00001413#if SK_SUPPORT_GPU
1414 if (FLAGS_gpuCacheSize.count() > 0) {
1415 if (FLAGS_gpuCacheSize.count() != 2) {
1416 gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
1417 return -1;
1418 }
1419 gGpuCacheSizeBytes = atoi(FLAGS_gpuCacheSize[0]);
1420 gGpuCacheSizeCount = atoi(FLAGS_gpuCacheSize[1]);
1421 } else {
1422 gGpuCacheSizeBytes = DEFAULT_CACHE_VALUE;
1423 gGpuCacheSizeCount = DEFAULT_CACHE_VALUE;
1424 }
1425#endif
1426
1427 SkTDArray<SkScalar> tileGridReplayScales;
1428 *tileGridReplayScales.append() = SK_Scalar1; // By default only test at scale 1.0
1429 if (FLAGS_tileGridReplayScales.count() > 0) {
1430 tileGridReplayScales.reset();
1431 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
1432 double val = atof(FLAGS_tileGridReplayScales[i]);
1433 if (0 < val) {
1434 *tileGridReplayScales.append() = SkDoubleToScalar(val);
1435 }
1436 }
1437 if (0 == tileGridReplayScales.count()) {
1438 // Should have at least one scale
1439 gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
1440 return -1;
1441 }
1442 }
1443
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001444 if (!userConfig) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001445 // if no config is specified by user, add the defaults
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001446 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001447 if (gRec[i].fRunByDefault) {
1448 *configs.append() = i;
1449 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001450 }
1451 }
reed@google.comfb2cd422013-01-04 14:43:03 +00001452 // now remove any explicitly excluded configs
1453 for (int i = 0; i < excludeConfigs.count(); ++i) {
1454 int index = configs.find(excludeConfigs[i]);
1455 if (index >= 0) {
1456 configs.remove(index);
1457 // now assert that there was only one copy in configs[]
1458 SkASSERT(configs.find(excludeConfigs[i]) < 0);
1459 }
1460 }
1461
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001462#if SK_SUPPORT_GPU
1463 GrContextFactory* grFactory = new GrContextFactory;
1464 for (int i = 0; i < configs.count(); ++i) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001465 size_t index = configs[i];
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001466 if (kGPU_Backend == gRec[index].fBackend) {
1467 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1468 if (NULL == ctx) {
1469 SkDebugf("GrContext could not be created for config %s. Config will be skipped.",
1470 gRec[index].fName);
1471 configs.remove(i);
1472 --i;
1473 }
1474 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
1475 SkDebugf("Sample count (%d) of config %s is not supported. Config will be skipped.",
1476 gRec[index].fSampleCnt, gRec[index].fName);
1477 configs.remove(i);
1478 --i;
1479 }
1480 }
1481 }
epoger@google.com80724df2013-03-21 13:49:54 +00001482#else
1483 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001484#endif
1485
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001486 if (FLAGS_verbose) {
reed@google.comfb2cd422013-01-04 14:43:03 +00001487 SkString str;
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001488 str.printf("%d configs:", configs.count());
reed@google.comfb2cd422013-01-04 14:43:03 +00001489 for (int i = 0; i < configs.count(); ++i) {
1490 str.appendf(" %s", gRec[configs[i]].fName);
1491 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001492 gm_fprintf(stderr, "%s\n", str.c_str());
reed@google.comfb2cd422013-01-04 14:43:03 +00001493 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001494
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001495 if (FLAGS_resourcePath.count() == 1) {
1496 GM::SetResourcePath(FLAGS_resourcePath[0]);
1497 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001498
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001499 if (FLAGS_readPath.count() == 1) {
1500 const char* readPath = FLAGS_readPath[0];
epoger@google.com37269602013-01-19 04:21:27 +00001501 if (!sk_exists(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001502 gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001503 return -1;
1504 }
1505 if (sk_isdir(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001506 gm_fprintf(stdout, "reading from %s\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001507 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1508 IndividualImageExpectationsSource,
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001509 (readPath, FLAGS_enableMissingWarning)));
epoger@google.com37269602013-01-19 04:21:27 +00001510 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001511 gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
epoger@google.comd271d242013-02-13 18:14:48 +00001512 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1513 JsonExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001514 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00001515 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001516 if (FLAGS_writePath.count() == 1) {
1517 gm_fprintf(stderr, "writing to %s\n", FLAGS_writePath[0]);
reed@android.com00f883e2010-12-14 17:46:14 +00001518 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001519 if (FLAGS_writePicturePath.count() == 1) {
1520 gm_fprintf(stderr, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001521 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001522 if (FLAGS_resourcePath.count() == 1) {
1523 gm_fprintf(stderr, "reading resources from %s\n", FLAGS_resourcePath[0]);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001524 }
1525
epoger@google.com82cb65b2012-10-29 18:59:17 +00001526 if (moduloDivisor <= 0) {
1527 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001528 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001529 if (moduloRemainder < 0 || moduloRemainder >= moduloDivisor) {
1530 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001531 }
1532
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001533 // Accumulate success of all tests.
1534 int testsRun = 0;
1535 int testsPassed = 0;
1536 int testsFailed = 0;
1537 int testsMissingReferenceImages = 0;
1538
reed@google.comae7b8f32012-10-18 21:30:57 +00001539 int gmIndex = -1;
1540 SkString moduloStr;
1541
epoger@google.come8ebeb12012-10-29 16:42:11 +00001542 // If we will be writing out files, prepare subdirectories.
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001543 if (FLAGS_writePath.count() == 1) {
1544 if (!sk_mkdir(FLAGS_writePath[0])) {
epoger@google.come8ebeb12012-10-29 16:42:11 +00001545 return -1;
1546 }
1547 if (gmmain.fUseFileHierarchy) {
1548 for (int i = 0; i < configs.count(); i++) {
1549 ConfigData config = gRec[configs[i]];
1550 SkString subdir;
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001551 subdir.appendf("%s%c%s", FLAGS_writePath[0], SkPATH_SEPARATOR,
epoger@google.come8ebeb12012-10-29 16:42:11 +00001552 config.fName);
1553 if (!sk_mkdir(subdir.c_str())) {
1554 return -1;
1555 }
1556 }
1557 }
1558 }
1559
bsalomon@google.com7361f542012-04-19 19:15:35 +00001560 Iter iter;
1561 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00001562 while ((gm = iter.next()) != NULL) {
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +00001563
reed@google.comae7b8f32012-10-18 21:30:57 +00001564 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00001565 if (moduloRemainder >= 0) {
1566 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00001567 continue;
1568 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001569 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00001570 }
1571
reed@google.comece2b022011-07-25 14:28:57 +00001572 const char* shortName = gm->shortName();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001573 if (skip_name(FLAGS_match, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00001574 SkDELETE(gm);
1575 continue;
1576 }
1577
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001578 SkISize size = gm->getISize();
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001579 gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
1580 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00001581
epoger@google.com6f6568b2013-03-22 17:29:46 +00001582 ErrorCombination testErrors;
1583 testErrors.add(run_multiple_configs(gmmain, gm, configs, grFactory));
djsollen@google.comebce16d2012-10-26 14:07:13 +00001584
1585 SkBitmap comparisonBitmap;
1586 const ConfigData compareConfig =
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001587 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001588 testErrors.add(gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false));
djsollen@google.comebce16d2012-10-26 14:07:13 +00001589
epoger@google.com80724df2013-03-21 13:49:54 +00001590 // TODO(epoger): only run this if gmmain.generate_image() succeeded?
1591 // Otherwise, what are we comparing against?
epoger@google.com6f6568b2013-03-22 17:29:46 +00001592 testErrors.add(run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap,
1593 tileGridReplayScales));
djsollen@google.comebce16d2012-10-26 14:07:13 +00001594
1595 // Update overall results.
1596 // We only tabulate the particular error types that we currently
1597 // care about (e.g., missing reference images). Later on, if we
epoger@google.com37269602013-01-19 04:21:27 +00001598 // want to also tabulate other error types, we can do so.
djsollen@google.comebce16d2012-10-26 14:07:13 +00001599 testsRun++;
epoger@google.com37269602013-01-19 04:21:27 +00001600 if (!gmmain.fExpectationsSource.get() ||
epoger@google.com6f6568b2013-03-22 17:29:46 +00001601 (testErrors.includes(kMissingExpectations_ErrorType))) {
djsollen@google.comebce16d2012-10-26 14:07:13 +00001602 testsMissingReferenceImages++;
borenet@google.coma904ea12013-02-21 18:34:14 +00001603 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001604 if (testErrors.minus(gmmain.fIgnorableErrorCombination).isEmpty()) {
epoger@google.com3499f3c2013-01-10 17:11:27 +00001605 testsPassed++;
djsollen@google.comebce16d2012-10-26 14:07:13 +00001606 } else {
1607 testsFailed++;
1608 }
1609
reed@android.com00dae862009-06-10 15:38:48 +00001610 SkDELETE(gm);
1611 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001612 gm_fprintf(stdout, "Ran %d tests: %d passed, %d failed, %d missing reference images\n",
1613 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
epoger@google.com57f7abc2012-11-13 03:41:55 +00001614 gmmain.ListErrors();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001615
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001616 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.comee8a8e32012-12-18 19:13:49 +00001617 Json::Value actualResults;
1618 actualResults[kJsonKey_ActualResults_Failed] =
1619 gmmain.fJsonActualResults_Failed;
1620 actualResults[kJsonKey_ActualResults_FailureIgnored] =
1621 gmmain.fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001622 actualResults[kJsonKey_ActualResults_NoComparison] =
1623 gmmain.fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001624 actualResults[kJsonKey_ActualResults_Succeeded] =
1625 gmmain.fJsonActualResults_Succeeded;
1626 Json::Value root;
1627 root[kJsonKey_ActualResults] = actualResults;
1628 root[kJsonKey_ExpectedResults] = gmmain.fJsonExpectedResults;
1629 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001630 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00001631 stream.write(jsonStdString.c_str(), jsonStdString.length());
1632 }
1633
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001634#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001635
robertphillips@google.com59552022012-08-31 13:07:37 +00001636#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001637 for (int i = 0; i < configs.count(); i++) {
1638 ConfigData config = gRec[configs[i]];
1639
1640 if (kGPU_Backend == config.fBackend) {
1641 GrContext* gr = grFactory->get(config.fGLContextType);
1642
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001643 gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001644 gr->printCacheStats();
1645 }
1646 }
1647#endif
1648
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001649 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001650#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001651 SkGraphics::Term();
1652
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001653 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001654}
caryclark@google.com5987f582012-10-02 18:33:14 +00001655
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001656void GMMain::installFilter(SkCanvas* canvas) {
1657 if (FLAGS_forceBWtext) {
1658 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
1659 }
1660}
1661
borenet@google.com7158e6a2012-11-01 17:43:44 +00001662#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00001663int main(int argc, char * const argv[]) {
1664 return tool_main(argc, (char**) argv);
1665}
1666#endif