blob: bc53cc52377b7422148a4d1b6dd2e849c7f2d6eb [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,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000416 SkScalar scale = SK_Scalar1,
417 bool tile = false) {
epoger@google.comde961632012-10-26 18:56:36 +0000418 SkISize size = gm->getISize();
419 setup_bitmap(gRec, size, bitmap);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000420
421 if (tile) {
422 // Generate the result image by rendering to tiles and accumulating
423 // the results in 'bitmap'
424
425 // This 16x16 tiling matches the settings applied to 'pict' in
426 // 'generate_new_picture'
427 SkISize tileSize = SkISize::Make(16, 16);
428
429 SkBitmap tileBM;
430 setup_bitmap(gRec, tileSize, &tileBM);
431 SkCanvas tileCanvas(tileBM);
432 installFilter(&tileCanvas);
433
434 SkCanvas bmpCanvas(*bitmap);
435 SkPaint bmpPaint;
436 bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
437
438 for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
439 for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
440 int saveCount = tileCanvas.save();
441 SkMatrix mat(tileCanvas.getTotalMatrix());
442 mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
443 SkIntToScalar(-yTile*tileSize.height()));
444 tileCanvas.setMatrix(mat);
445 pict->draw(&tileCanvas);
446 tileCanvas.flush();
447 tileCanvas.restoreToCount(saveCount);
448 bmpCanvas.drawBitmap(tileBM,
449 SkIntToScalar(xTile * tileSize.width()),
450 SkIntToScalar(yTile * tileSize.height()),
451 &bmpPaint);
452 }
453 }
454 } else {
455 SkCanvas canvas(*bitmap);
456 installFilter(&canvas);
457 canvas.scale(scale, scale);
458 canvas.drawPicture(*pict);
459 complete_bitmap(bitmap);
460 }
epoger@google.comde961632012-10-26 18:56:36 +0000461 }
462
463 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
464#ifdef SK_SUPPORT_PDF
465 SkMatrix initialTransform = gm->getInitialTransform();
466 SkISize pageSize = gm->getISize();
467 SkPDFDevice* dev = NULL;
468 if (initialTransform.isIdentity()) {
469 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
470 } else {
471 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
472 SkIntToScalar(pageSize.height()));
473 initialTransform.mapRect(&content);
474 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
475 SkIntToScalar(pageSize.height()));
476 SkISize contentSize =
477 SkISize::Make(SkScalarRoundToInt(content.width()),
478 SkScalarRoundToInt(content.height()));
479 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
480 }
481 SkAutoUnref aur(dev);
482
483 SkCanvas c(dev);
reed@google.comaef73612012-11-16 13:41:45 +0000484 invokeGM(gm, &c, true, false);
epoger@google.comde961632012-10-26 18:56:36 +0000485
486 SkPDFDocument doc;
487 doc.appendPage(dev);
488 doc.emitPDF(&pdf);
489#endif
490 }
491
492 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
493#ifdef SK_SUPPORT_XPS
494 SkISize size = gm->getISize();
495
496 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
497 SkIntToScalar(size.height()));
498 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
499 static const SkScalar upm = 72 * inchesPerMeter;
500 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
501 static const SkScalar ppm = 200 * inchesPerMeter;
502 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
503
504 SkXPSDevice* dev = new SkXPSDevice();
505 SkAutoUnref aur(dev);
506
507 SkCanvas c(dev);
508 dev->beginPortfolio(&xps);
509 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000510 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000511 dev->endSheet();
512 dev->endPortfolio();
513
514#endif
515 }
516
epoger@google.com6f6568b2013-03-22 17:29:46 +0000517 ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
518 const char renderModeDescriptor [], const SkString& name,
519 SkBitmap& bitmap, SkDynamicMemoryWStream* document) {
epoger@google.comde961632012-10-26 18:56:36 +0000520 SkString path;
521 bool success = false;
522 if (gRec.fBackend == kRaster_Backend ||
523 gRec.fBackend == kGPU_Backend ||
524 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
525
epoger@google.com37269602013-01-19 04:21:27 +0000526 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
527 "png");
epoger@google.comde961632012-10-26 18:56:36 +0000528 success = write_bitmap(path, bitmap);
529 }
530 if (kPDF_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000531 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
532 "pdf");
epoger@google.comde961632012-10-26 18:56:36 +0000533 success = write_document(path, *document);
534 }
535 if (kXPS_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000536 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
537 "xps");
epoger@google.comde961632012-10-26 18:56:36 +0000538 success = write_document(path, *document);
539 }
540 if (success) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000541 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000542 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000543 gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000544 ErrorCombination errors(kWritingReferenceImage_ErrorType);
545 RecordError(errors, name, renderModeDescriptor);
546 return errors;
epoger@google.comde961632012-10-26 18:56:36 +0000547 }
548 }
549
epoger@google.com37269602013-01-19 04:21:27 +0000550 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000551 * Log more detail about the mistmatch between expectedBitmap and
552 * actualBitmap.
553 */
554 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
555 const char *testName) {
556 const int expectedWidth = expectedBitmap.width();
557 const int expectedHeight = expectedBitmap.height();
558 const int width = actualBitmap.width();
559 const int height = actualBitmap.height();
560 if ((expectedWidth != width) || (expectedHeight != height)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000561 gm_fprintf(stderr, "---- %s: dimension mismatch --"
562 " expected [%d %d], actual [%d %d]\n",
563 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000564 return;
565 }
566
567 if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
568 (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000569 gm_fprintf(stderr, "---- %s: not computing max per-channel"
570 " pixel mismatch because non-8888\n", testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000571 return;
572 }
573
574 SkAutoLockPixels alp0(expectedBitmap);
575 SkAutoLockPixels alp1(actualBitmap);
576 int errR = 0;
577 int errG = 0;
578 int errB = 0;
579 int errA = 0;
580 int differingPixels = 0;
581
582 for (int y = 0; y < height; ++y) {
583 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
584 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
585 for (int x = 0; x < width; ++x) {
586 SkPMColor expectedPixel = *expectedPixelPtr++;
587 SkPMColor actualPixel = *actualPixelPtr++;
588 if (expectedPixel != actualPixel) {
589 differingPixels++;
590 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
591 (int)SkGetPackedR32(actualPixel)));
592 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
593 (int)SkGetPackedG32(actualPixel)));
594 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
595 (int)SkGetPackedB32(actualPixel)));
596 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
597 (int)SkGetPackedA32(actualPixel)));
598 }
599 }
600 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000601 gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
602 " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
603 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000604 }
605
606 /**
epoger@google.com6f6568b2013-03-22 17:29:46 +0000607 * Compares actual checksum to expectations, returning the set of errors
608 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000609 *
610 * If fMismatchPath has been set, and there are pixel diffs, then the
611 * actual bitmap will be written out to a file within fMismatchPath.
612 *
613 * @param expectations what expectations to compare actualBitmap against
614 * @param actualBitmap the image we actually generated
615 * @param baseNameString name of test without renderModeDescriptor added
616 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
617 * @param addToJsonSummary whether to add these results (both actual and
618 * expected) to the JSON summary
619 *
620 * TODO: For now, addToJsonSummary is only set to true within
621 * compare_test_results_to_stored_expectations(), so results of our
622 * in-memory comparisons (Rtree vs regular, etc.) are not written to the
623 * JSON summary. We may wish to change that.
624 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000625 ErrorCombination compare_to_expectations(Expectations expectations,
626 const SkBitmap& actualBitmap,
627 const SkString& baseNameString,
628 const char renderModeDescriptor[],
629 bool addToJsonSummary=false) {
630 ErrorCombination errors;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000631 Checksum actualChecksum = SkBitmapChecksummer::Compute64(actualBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000632 SkString completeNameString = baseNameString;
633 completeNameString.append(renderModeDescriptor);
634 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000635
epoger@google.com37269602013-01-19 04:21:27 +0000636 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000637 errors.add(kMissingExpectations_ErrorType);
638 } else if (!expectations.match(actualChecksum)) {
639 errors.add(kImageMismatch_ErrorType);
epoger@google.com84a18022013-02-01 20:39:15 +0000640
641 // Write out the "actuals" for any mismatches, if we have
642 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000643 if (fMismatchPath) {
644 SkString path =
645 make_filename(fMismatchPath, renderModeDescriptor,
646 baseNameString.c_str(), "png");
647 write_bitmap(path, actualBitmap);
648 }
epoger@google.com84a18022013-02-01 20:39:15 +0000649
650 // If we have access to a single expected bitmap, log more
651 // detail about the mismatch.
652 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
653 if (NULL != expectedBitmapPtr) {
654 report_bitmap_diffs(*expectedBitmapPtr, actualBitmap, completeName);
655 }
epoger@google.coma243b222013-01-17 17:54:28 +0000656 }
epoger@google.com6f6568b2013-03-22 17:29:46 +0000657 RecordError(errors, baseNameString, renderModeDescriptor);
epoger@google.coma243b222013-01-17 17:54:28 +0000658
epoger@google.com37269602013-01-19 04:21:27 +0000659 if (addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000660 add_actual_results_to_json_summary(completeName, actualChecksum, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000661 expectations.ignoreFailure());
662 add_expected_results_to_json_summary(completeName, expectations);
663 }
epoger@google.coma243b222013-01-17 17:54:28 +0000664
epoger@google.com6f6568b2013-03-22 17:29:46 +0000665 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000666 }
667
epoger@google.com37269602013-01-19 04:21:27 +0000668 /**
669 * Add this result to the appropriate JSON collection of actual results,
670 * depending on status.
671 */
672 void add_actual_results_to_json_summary(const char testName[],
673 Checksum actualChecksum,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000674 ErrorCombination result,
epoger@google.com37269602013-01-19 04:21:27 +0000675 bool ignoreFailure) {
676 Json::Value actualResults;
677 actualResults[kJsonKey_ActualResults_AnyStatus_Checksum] =
678 asJsonValue(actualChecksum);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000679 if (result.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +0000680 this->fJsonActualResults_Succeeded[testName] = actualResults;
681 } else {
682 if (ignoreFailure) {
683 // TODO: Once we have added the ability to compare
684 // actual results against expectations in a JSON file
685 // (where we can set ignore-failure to either true or
epoger@google.com84a18022013-02-01 20:39:15 +0000686 // false), add test cases that exercise ignored
epoger@google.com6f6568b2013-03-22 17:29:46 +0000687 // failures (both for kMissingExpectations_ErrorType
688 // and kImageMismatch_ErrorType).
epoger@google.com37269602013-01-19 04:21:27 +0000689 this->fJsonActualResults_FailureIgnored[testName] =
690 actualResults;
691 } else {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000692 if (result.includes(kMissingExpectations_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000693 // TODO: What about the case where there IS an
694 // expected image checksum, but that gm test
695 // doesn't actually run? For now, those cases
696 // will always be ignored, because gm only looks
697 // at expectations that correspond to gm tests
698 // that were actually run.
699 //
700 // Once we have the ability to express
701 // expectations as a JSON file, we should fix this
702 // (and add a test case for which an expectation
703 // is given but the test is never run).
704 this->fJsonActualResults_NoComparison[testName] =
705 actualResults;
epoger@google.comeb066362013-03-08 09:39:36 +0000706 }
epoger@google.com6f6568b2013-03-22 17:29:46 +0000707 if (result.includes(kImageMismatch_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000708 this->fJsonActualResults_Failed[testName] = actualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000709 }
710 }
711 }
712 }
713
714 /**
715 * Add this test to the JSON collection of expected results.
716 */
717 void add_expected_results_to_json_summary(const char testName[],
718 Expectations expectations) {
719 // For now, we assume that this collection starts out empty and we
720 // just fill it in as we go; once gm accepts a JSON file as input,
721 // we'll have to change that.
722 Json::Value expectedResults;
723 expectedResults[kJsonKey_ExpectedResults_Checksums] =
724 expectations.allowedChecksumsAsJson();
725 expectedResults[kJsonKey_ExpectedResults_IgnoreFailure] =
726 expectations.ignoreFailure();
727 this->fJsonExpectedResults[testName] = expectedResults;
728 }
729
730 /**
731 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
732 *
733 * @param gm which test generated the actualBitmap
734 * @param gRec
735 * @param writePath unless this is NULL, write out actual images into this
736 * directory
737 * @param actualBitmap bitmap generated by this run
738 * @param pdf
739 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000740 ErrorCombination compare_test_results_to_stored_expectations(
epoger@google.com37269602013-01-19 04:21:27 +0000741 GM* gm, const ConfigData& gRec, const char writePath[],
742 SkBitmap& actualBitmap, SkDynamicMemoryWStream* pdf) {
743
epoger@google.coma243b222013-01-17 17:54:28 +0000744 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000745 ErrorCombination errors;
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000746
epoger@google.com6f6568b2013-03-22 17:29:46 +0000747 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
epoger@google.com37269602013-01-19 04:21:27 +0000748 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
749 /*
750 * Get the expected results for this test, as one or more allowed
751 * checksums. The current implementation of expectationsSource
752 * get this by computing the checksum of a single PNG file on disk.
753 *
754 * TODO(epoger): This relies on the fact that
755 * force_all_opaque() was called on the bitmap before it
756 * was written to disk as a PNG in the first place. If
757 * not, the checksum returned here may not match the
758 * checksum of actualBitmap, which *has* been run through
759 * force_all_opaque().
epoger@google.com5f6a0072013-01-31 16:30:55 +0000760 * See comments above complete_bitmap() for more detail.
epoger@google.com37269602013-01-19 04:21:27 +0000761 */
762 Expectations expectations = expectationsSource->get(name.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000763 errors.add(compare_to_expectations(expectations, actualBitmap,
764 name, "", true));
epoger@google.com37269602013-01-19 04:21:27 +0000765 } else {
766 // If we are running without expectations, we still want to
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000767 // record the actual results.
epoger@google.com5f6a0072013-01-31 16:30:55 +0000768 Checksum actualChecksum =
769 SkBitmapChecksummer::Compute64(actualBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000770 add_actual_results_to_json_summary(name.c_str(), actualChecksum,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000771 ErrorCombination(kMissingExpectations_ErrorType),
epoger@google.com37269602013-01-19 04:21:27 +0000772 false);
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000773 }
epoger@google.com37269602013-01-19 04:21:27 +0000774
775 // TODO: Consider moving this into compare_to_expectations(),
776 // similar to fMismatchPath... for now, we don't do that, because
777 // we don't want to write out the actual bitmaps for all
778 // renderModes of all tests! That would be a lot of files.
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000779 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000780 errors.add(write_reference_image(gRec, writePath, "",
781 name, actualBitmap, pdf));
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000782 }
epoger@google.com37269602013-01-19 04:21:27 +0000783
epoger@google.com6f6568b2013-03-22 17:29:46 +0000784 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000785 }
786
epoger@google.com37269602013-01-19 04:21:27 +0000787 /**
788 * Compare actualBitmap to referenceBitmap.
789 *
790 * @param gm which test generated the bitmap
791 * @param gRec
792 * @param renderModeDescriptor
793 * @param actualBitmap actual bitmap generated by this run
794 * @param referenceBitmap bitmap we expected to be generated
795 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000796 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.com37269602013-01-19 04:21:27 +0000797 GM* gm, const ConfigData& gRec, const char renderModeDescriptor [],
798 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
799
800 SkASSERT(referenceBitmap);
801 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com84a18022013-02-01 20:39:15 +0000802 Expectations expectations(*referenceBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000803 return compare_to_expectations(expectations, actualBitmap,
804 name, renderModeDescriptor);
805 }
806
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000807 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
808 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +0000809 // Pictures are refcounted so must be on heap
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000810 SkPicture* pict;
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000811 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
812 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +0000813
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000814 if (kTileGrid_BbhType == bbhType) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000815 SkTileGridPicture::TileGridInfo info;
816 info.fMargin.setEmpty();
817 info.fOffset.setZero();
818 info.fTileInterval.set(16, 16);
819 pict = new SkTileGridPicture(width, height, info);
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000820 } else {
821 pict = new SkPicture;
822 }
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000823 if (kNone_BbhType != bbhType) {
824 recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
825 }
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000826 SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +0000827 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +0000828 invokeGM(gm, cv, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000829 pict->endRecording();
830
831 return pict;
832 }
833
834 static SkPicture* stream_to_new_picture(const SkPicture& src) {
835
836 // To do in-memory commiunications with a stream, we need to:
837 // * create a dynamic memory stream
838 // * copy it into a buffer
839 // * create a read stream from it
840 // ?!?!
841
842 SkDynamicMemoryWStream storage;
843 src.serialize(&storage);
844
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000845 size_t streamSize = storage.getOffset();
epoger@google.comde961632012-10-26 18:56:36 +0000846 SkAutoMalloc dstStorage(streamSize);
847 void* dst = dstStorage.get();
848 //char* dst = new char [streamSize];
849 //@todo thudson 22 April 2011 when can we safely delete [] dst?
850 storage.copyTo(dst);
851 SkMemoryStream pictReadback(dst, streamSize);
852 SkPicture* retval = new SkPicture (&pictReadback);
853 return retval;
854 }
855
856 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +0000857 // Depending on flags, possibly compare to an expected image.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000858 ErrorCombination test_drawing(GM* gm,
859 const ConfigData& gRec,
860 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000861 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000862 SkBitmap* bitmap) {
epoger@google.comde961632012-10-26 18:56:36 +0000863 SkDynamicMemoryWStream document;
864
865 if (gRec.fBackend == kRaster_Backend ||
866 gRec.fBackend == kGPU_Backend) {
867 // Early exit if we can't generate the image.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000868 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000869 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +0000870 // TODO: Add a test to exercise what the stdout and
871 // JSON look like if we get an "early error" while
872 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +0000873 return errors;
874 }
875 } else if (gRec.fBackend == kPDF_Backend) {
876 generate_pdf(gm, document);
877#if CAN_IMAGE_PDF
878 SkAutoDataUnref data(document.copyToData());
879 SkMemoryStream stream(data->data(), data->size());
880 SkPDFDocumentToBitmap(&stream, bitmap);
881#endif
882 } else if (gRec.fBackend == kXPS_Backend) {
883 generate_xps(gm, document);
884 }
epoger@google.com37269602013-01-19 04:21:27 +0000885 return compare_test_results_to_stored_expectations(
886 gm, gRec, writePath, *bitmap, &document);
epoger@google.comde961632012-10-26 18:56:36 +0000887 }
888
epoger@google.com6f6568b2013-03-22 17:29:46 +0000889 ErrorCombination test_deferred_drawing(GM* gm,
890 const ConfigData& gRec,
891 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000892 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +0000893 SkDynamicMemoryWStream document;
894
895 if (gRec.fBackend == kRaster_Backend ||
896 gRec.fBackend == kGPU_Backend) {
897 SkBitmap bitmap;
898 // Early exit if we can't generate the image, but this is
899 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000900 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000901 // TODO(epoger): This logic is the opposite of what is
902 // described above... if we succeeded in generating the
903 // -deferred image, we exit early! We should fix this
904 // ASAP, because it is hiding -deferred errors... but for
905 // now, I'm leaving the logic as it is so that the
906 // refactoring change
907 // https://codereview.chromium.org/12992003/ is unblocked.
908 //
909 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
910 // ('image-surface gm test is failing in "deferred" mode,
911 // and gm is not reporting the failure')
912 if (errors.isEmpty()) {
913 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000914 }
epoger@google.com37269602013-01-19 04:21:27 +0000915 return compare_test_results_to_reference_bitmap(
916 gm, gRec, "-deferred", bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000917 }
epoger@google.com6f6568b2013-03-22 17:29:46 +0000918 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000919 }
920
epoger@google.com6f6568b2013-03-22 17:29:46 +0000921 ErrorCombination test_pipe_playback(GM* gm,
922 const ConfigData& gRec,
923 const SkBitmap& referenceBitmap) {
924 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +0000925 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
926 SkBitmap bitmap;
927 SkISize size = gm->getISize();
928 setup_bitmap(gRec, size, &bitmap);
929 SkCanvas canvas(bitmap);
scroggo@google.com0b735632013-03-19 17:38:50 +0000930 installFilter(&canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000931 PipeController pipeController(&canvas);
932 SkGPipeWriter writer;
933 SkCanvas* pipeCanvas = writer.startRecording(
934 &pipeController, gPipeWritingFlagCombos[i].flags);
reed@google.comaef73612012-11-16 13:41:45 +0000935 invokeGM(gm, pipeCanvas, false, false);
epoger@google.com5f6a0072013-01-31 16:30:55 +0000936 complete_bitmap(&bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000937 writer.endRecording();
938 SkString string("-pipe");
939 string.append(gPipeWritingFlagCombos[i].name);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000940 errors.add(compare_test_results_to_reference_bitmap(
941 gm, gRec, string.c_str(), bitmap, &referenceBitmap));
942 if (!errors.isEmpty()) {
epoger@google.comde961632012-10-26 18:56:36 +0000943 break;
944 }
945 }
946 return errors;
947 }
948
epoger@google.com6f6568b2013-03-22 17:29:46 +0000949 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
950 const SkBitmap& referenceBitmap) {
951 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +0000952 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
953 SkBitmap bitmap;
954 SkISize size = gm->getISize();
955 setup_bitmap(gRec, size, &bitmap);
956 SkCanvas canvas(bitmap);
scroggo@google.com0b735632013-03-19 17:38:50 +0000957 installFilter(&canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000958 TiledPipeController pipeController(bitmap);
959 SkGPipeWriter writer;
960 SkCanvas* pipeCanvas = writer.startRecording(
961 &pipeController, gPipeWritingFlagCombos[i].flags);
reed@google.comaef73612012-11-16 13:41:45 +0000962 invokeGM(gm, pipeCanvas, false, false);
epoger@google.com5f6a0072013-01-31 16:30:55 +0000963 complete_bitmap(&bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000964 writer.endRecording();
965 SkString string("-tiled pipe");
966 string.append(gPipeWritingFlagCombos[i].name);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000967 errors.add(compare_test_results_to_reference_bitmap(
968 gm, gRec, string.c_str(), bitmap, &referenceBitmap));
969 if (!errors.isEmpty()) {
epoger@google.comde961632012-10-26 18:56:36 +0000970 break;
971 }
972 }
973 return errors;
974 }
epoger@google.come8ebeb12012-10-29 16:42:11 +0000975
976 //
977 // member variables.
978 // They are public for now, to allow easier setting by tool_main().
979 //
980
epoger@google.come8ebeb12012-10-29 16:42:11 +0000981 bool fUseFileHierarchy;
epoger@google.com6f6568b2013-03-22 17:29:46 +0000982 ErrorCombination fIgnorableErrorCombination;
epoger@google.come8ebeb12012-10-29 16:42:11 +0000983
junov@chromium.org95146eb2013-01-11 21:04:40 +0000984 const char* fMismatchPath;
985
epoger@google.com57f7abc2012-11-13 03:41:55 +0000986 // information about all failed tests we have encountered so far
987 SkTArray<FailRec> fFailedTests;
988
epoger@google.com37269602013-01-19 04:21:27 +0000989 // Where to read expectations (expected image checksums, etc.) from.
990 // If unset, we don't do comparisons.
991 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
992
993 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +0000994 Json::Value fJsonExpectedResults;
995 Json::Value fJsonActualResults_Failed;
996 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +0000997 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +0000998 Json::Value fJsonActualResults_Succeeded;
999
epoger@google.comde961632012-10-26 18:56:36 +00001000}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +00001001
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001002#if SK_SUPPORT_GPU
1003static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1004#else
1005static const GLContextType kDontCare_GLContextType = 0;
1006#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +00001007
1008// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +00001009// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +00001010static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
1011 kWrite_ConfigFlag;
1012
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001013static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001014 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001015#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 +00001016 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001017#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001018 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
1019#if SK_SUPPORT_GPU
1020 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
1021 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", true },
1022 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
bsalomon@google.com7361f542012-04-19 19:15:35 +00001023 /* The debug context does not generate images */
scroggo@google.com0f567c62013-03-20 15:35:08 +00001024 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", GR_DEBUG},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001025#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001026 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
1027 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001028#endif // SK_ANGLE
1029#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001030 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001031#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001032#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +00001033#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001034 /* 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 +00001035 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +00001036#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001037#ifdef SK_SUPPORT_PDF
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001038 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001039#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001040};
1041
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001042static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001043 SkString result;
1044 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001045 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1046 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001047 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001048 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001049 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001050 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001051 result.append("]\n");
1052 result.appendf("The default value is: \"");
1053 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1054 if (gRec[i].fRunByDefault) {
1055 if (i > 0) {
1056 result.append(" ");
1057 }
1058 result.appendf("%s", gRec[i].fName);
1059 }
1060 }
1061 result.appendf("\"");
1062
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001063 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001064}
scroggo@google.com7d519302013-03-19 17:28:10 +00001065
epoger@google.com6f6568b2013-03-22 17:29:46 +00001066// Macro magic to convert a numeric preprocessor token into a string.
1067// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1068// This should probably be moved into one of our common headers...
1069#define TOSTRING_INTERNAL(x) #x
1070#define TOSTRING(x) TOSTRING_INTERNAL(x)
1071
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001072// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001073DEFINE_string(config, "", configUsage().c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001074DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
1075DEFINE_bool(enableMissingWarning, true, "Print message to stderr (but don't fail) if "
1076 "unable to read a reference image for any tests.");
1077DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1078DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1079#if SK_SUPPORT_GPU
1080DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001081 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1082 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001083#endif
1084DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1085 "when reading/writing files.");
1086DEFINE_string(match, "", "Only run tests whose name includes this substring/these substrings "
1087 "(more than one can be supplied, separated by spaces).");
1088DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1089 "pixel mismatches into this directory.");
1090DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1091 "testIndex %% divisor == remainder.");
1092DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1093DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1094DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1095 "any differences between those and the newly generated ones.");
1096DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1097DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1098DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1099DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
1100DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1101DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1102DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1103 "factors to be used for tileGrid playback testing. Default value: 1.0");
1104DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
1105DEFINE_bool2(verbose, v, false, "Print diagnostics (e.g. list each config to be tested).");
1106DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
1107DEFINE_string2(writePicturePath, wp, "", "Write .skp files into this directory.");
1108
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001109static int findConfig(const char config[]) {
1110 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1111 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001112 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001113 }
1114 }
1115 return -1;
1116}
1117
reed@google.comb2a51622011-10-31 16:30:04 +00001118static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
1119 if (0 == array.count()) {
1120 // no names, so don't skip anything
1121 return false;
1122 }
1123 for (int i = 0; i < array.count(); ++i) {
1124 if (strstr(name, array[i])) {
1125 // found the name, so don't skip
1126 return false;
1127 }
1128 }
1129 return true;
1130}
1131
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001132namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001133#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +00001134SkAutoTUnref<GrContext> gGrContext;
1135/**
bsalomon@google.comc7a24d22012-11-01 18:03:48 +00001136 * Sets the global GrContext, accessible by individual GMs
bsalomon@google.com7361f542012-04-19 19:15:35 +00001137 */
caryclark@google.com13130862012-06-06 12:10:45 +00001138static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +00001139 SkSafeRef(grContext);
1140 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001141}
bsalomon@google.com7361f542012-04-19 19:15:35 +00001142
1143/**
1144 * Gets the global GrContext, can be called by GM tests.
1145 */
caryclark@google.com13130862012-06-06 12:10:45 +00001146GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +00001147GrContext* GetGr() {
1148 return gGrContext.get();
1149}
1150
1151/**
1152 * Sets the global GrContext and then resets it to its previous value at
1153 * destruction.
1154 */
1155class AutoResetGr : SkNoncopyable {
1156public:
1157 AutoResetGr() : fOld(NULL) {}
1158 void set(GrContext* context) {
1159 SkASSERT(NULL == fOld);
1160 fOld = GetGr();
1161 SkSafeRef(fOld);
1162 SetGr(context);
1163 }
1164 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1165private:
1166 GrContext* fOld;
1167};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001168#else
epoger@google.com80724df2013-03-21 13:49:54 +00001169GrContext* GetGr();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001170GrContext* GetGr() { return NULL; }
1171#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001172}
1173
reed@google.comfb2cd422013-01-04 14:43:03 +00001174template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1175 int index = array->find(value);
1176 if (index < 0) {
1177 *array->append() = value;
1178 }
1179}
1180
epoger@google.com80724df2013-03-21 13:49:54 +00001181/**
1182 * Run this test in a number of different configs (8888, 565, PDF,
1183 * etc.), confirming that the resulting bitmaps match expectations
1184 * (which may be different for each config).
1185 *
1186 * Returns all errors encountered while doing so.
1187 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001188ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1189 GrContextFactory *grFactory);
1190ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1191 GrContextFactory *grFactory) {
1192 ErrorCombination errorsForAllConfigs;
epoger@google.com80724df2013-03-21 13:49:54 +00001193 uint32_t gmFlags = gm->getFlags();
1194
epoger@google.com80724df2013-03-21 13:49:54 +00001195 for (int i = 0; i < configs.count(); i++) {
1196 ConfigData config = gRec[configs[i]];
1197
1198 // Skip any tests that we don't even need to try.
1199 if ((kPDF_Backend == config.fBackend) &&
1200 (!FLAGS_pdf|| (gmFlags & GM::kSkipPDF_Flag))) {
1201 continue;
1202 }
1203 if ((gmFlags & GM::kSkip565_Flag) &&
1204 (kRaster_Backend == config.fBackend) &&
1205 (SkBitmap::kRGB_565_Config == config.fConfig)) {
1206 continue;
1207 }
1208 if ((gmFlags & GM::kSkipGPU_Flag) &&
1209 kGPU_Backend == config.fBackend) {
1210 continue;
1211 }
1212
1213 // Now we know that we want to run this test and record its
1214 // success or failure.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001215 ErrorCombination errorsForThisConfig;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001216 GrSurface* gpuTarget = NULL;
epoger@google.com80724df2013-03-21 13:49:54 +00001217#if SK_SUPPORT_GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001218 SkAutoTUnref<GrSurface> auGpuTarget;
epoger@google.com80724df2013-03-21 13:49:54 +00001219 AutoResetGr autogr;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001220 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
epoger@google.com80724df2013-03-21 13:49:54 +00001221 GrContext* gr = grFactory->get(config.fGLContextType);
1222 bool grSuccess = false;
1223 if (gr) {
1224 // create a render target to back the device
1225 GrTextureDesc desc;
1226 desc.fConfig = kSkia8888_GrPixelConfig;
1227 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1228 desc.fWidth = gm->getISize().width();
1229 desc.fHeight = gm->getISize().height();
1230 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001231 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1232 if (NULL != auGpuTarget) {
1233 gpuTarget = auGpuTarget;
1234 grSuccess = true;
epoger@google.com80724df2013-03-21 13:49:54 +00001235 autogr.set(gr);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001236 // Set the user specified cache limits if non-default.
1237 size_t bytes;
1238 int count;
1239 gr->getTextureCacheLimits(&count, &bytes);
1240 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1241 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1242 }
1243 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1244 count = gGpuCacheSizeCount;
1245 }
1246 gr->setTextureCacheLimits(count, bytes);
epoger@google.com80724df2013-03-21 13:49:54 +00001247 }
epoger@google.com80724df2013-03-21 13:49:54 +00001248 }
1249 if (!grSuccess) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001250 errorsForThisConfig.add(kNoGpuContext_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001251 }
1252 }
1253#endif
1254
1255 SkBitmap comparisonBitmap;
1256
1257 const char* writePath;
1258 if (FLAGS_writePath.count() == 1) {
1259 writePath = FLAGS_writePath[0];
1260 } else {
1261 writePath = NULL;
1262 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001263 if (errorsForThisConfig.isEmpty()) {
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001264 errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
1265 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001266 }
1267
epoger@google.com6f6568b2013-03-22 17:29:46 +00001268 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1269 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1270 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001271 gpuTarget));
epoger@google.com80724df2013-03-21 13:49:54 +00001272 }
1273
epoger@google.com6f6568b2013-03-22 17:29:46 +00001274 errorsForAllConfigs.add(errorsForThisConfig);
epoger@google.com80724df2013-03-21 13:49:54 +00001275 }
1276 return errorsForAllConfigs;
1277}
1278
1279/**
1280 * Run this test in a number of different drawing modes (pipe,
1281 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1282 * *exactly* match comparisonBitmap.
1283 *
1284 * Returns all errors encountered while doing so.
1285 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001286ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1287 const SkBitmap &comparisonBitmap,
1288 const SkTDArray<SkScalar> &tileGridReplayScales);
1289ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1290 const SkBitmap &comparisonBitmap,
1291 const SkTDArray<SkScalar> &tileGridReplayScales) {
1292 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001293 uint32_t gmFlags = gm->getFlags();
1294
1295 // run the picture centric GM steps
1296 if (!(gmFlags & GM::kSkipPicture_Flag)) {
1297
epoger@google.com6f6568b2013-03-22 17:29:46 +00001298 ErrorCombination pictErrors;
epoger@google.com80724df2013-03-21 13:49:54 +00001299
1300 //SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
1301 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1302 SkAutoUnref aur(pict);
1303
1304 if (FLAGS_replay) {
1305 SkBitmap bitmap;
1306 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001307 pictErrors.add(gmmain.compare_test_results_to_reference_bitmap(
1308 gm, compareConfig, "-replay", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001309 }
1310
epoger@google.com6f6568b2013-03-22 17:29:46 +00001311 if ((pictErrors.isEmpty()) && FLAGS_serialize) {
epoger@google.com80724df2013-03-21 13:49:54 +00001312 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1313 SkAutoUnref aurr(repict);
1314
1315 SkBitmap bitmap;
1316 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001317 pictErrors.add(gmmain.compare_test_results_to_reference_bitmap(
1318 gm, compareConfig, "-serialize", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001319 }
1320
1321 if (FLAGS_writePicturePath.count() == 1) {
1322 const char* pictureSuffix = "skp";
1323 SkString path = make_filename(FLAGS_writePicturePath[0], "",
1324 gm->shortName(), pictureSuffix);
1325 SkFILEWStream stream(path.c_str());
1326 pict->serialize(&stream);
1327 }
1328
epoger@google.com6f6568b2013-03-22 17:29:46 +00001329 errorsForAllModes.add(pictErrors);
epoger@google.com80724df2013-03-21 13:49:54 +00001330 }
1331
1332 // TODO: add a test in which the RTree rendering results in a
1333 // different bitmap than the standard rendering. It should
1334 // show up as failed in the JSON summary, and should be listed
1335 // in the stdout also.
1336 if (!(gmFlags & GM::kSkipPicture_Flag) && FLAGS_rtree) {
1337 SkPicture* pict = gmmain.generate_new_picture(
1338 gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
1339 SkAutoUnref aur(pict);
1340 SkBitmap bitmap;
1341 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001342 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1343 gm, compareConfig, "-rtree", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001344 }
1345
1346 if (!(gmFlags & GM::kSkipPicture_Flag) && FLAGS_tileGrid) {
1347 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1348 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1349 if ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1) {
1350 continue;
1351 }
1352 // We record with the reciprocal scale to obtain a replay
1353 // result that can be validated against comparisonBitmap.
1354 SkScalar recordScale = SkScalarInvert(replayScale);
1355 SkPicture* pict = gmmain.generate_new_picture(
1356 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag, recordScale);
1357 SkAutoUnref aur(pict);
1358 SkBitmap bitmap;
robertphillips@google.com5a7d0292013-04-02 15:18:41 +00001359 // We cannot yet pass 'true' to generate_image_from_picture to
1360 // perform actual tiled rendering (see Issue 1198 -
1361 // https://code.google.com/p/skia/issues/detail?id=1198)
1362 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
1363 replayScale /*, true */);
epoger@google.com80724df2013-03-21 13:49:54 +00001364 SkString suffix("-tilegrid");
1365 if (SK_Scalar1 != replayScale) {
1366 suffix += "-scale-";
1367 suffix.appendScalar(replayScale);
1368 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001369 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1370 gm, compareConfig, suffix.c_str(), bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001371 }
1372 }
1373
1374 // run the pipe centric GM steps
1375 if (!(gmFlags & GM::kSkipPipe_Flag)) {
1376
epoger@google.com6f6568b2013-03-22 17:29:46 +00001377 ErrorCombination pipeErrors;
epoger@google.com80724df2013-03-21 13:49:54 +00001378
1379 if (FLAGS_pipe) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001380 pipeErrors.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001381 }
1382
epoger@google.com6f6568b2013-03-22 17:29:46 +00001383 if ((pipeErrors.isEmpty()) &&
epoger@google.com80724df2013-03-21 13:49:54 +00001384 FLAGS_tiledPipe && !(gmFlags & GM::kSkipTiled_Flag)) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001385 pipeErrors.add(gmmain.test_tiled_pipe_playback(gm, compareConfig, comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001386 }
1387
epoger@google.com6f6568b2013-03-22 17:29:46 +00001388 errorsForAllModes.add(pipeErrors);
epoger@google.com80724df2013-03-21 13:49:54 +00001389 }
1390 return errorsForAllModes;
1391}
1392
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001393int tool_main(int argc, char** argv);
1394int tool_main(int argc, char** argv) {
1395
1396#if SK_ENABLE_INST_COUNT
1397 gPrintInstCount = true;
1398#endif
1399
1400 SkGraphics::Init();
1401 // we don't need to see this during a run
1402 gSkSuppressFontCachePurgeSpew = true;
1403
1404 setSystemPreferences();
1405 GMMain gmmain;
1406
1407 SkTDArray<size_t> configs;
1408 SkTDArray<size_t> excludeConfigs;
1409 bool userConfig = false;
1410
1411 SkString usage;
1412 usage.printf("Run the golden master tests.\n");
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00001413 SkCommandLineFlags::SetUsage(usage.c_str());
1414 SkCommandLineFlags::Parse(argc, argv);
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001415
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001416 gmmain.fUseFileHierarchy = FLAGS_hierarchy;
1417 if (FLAGS_mismatchPath.count() == 1) {
1418 gmmain.fMismatchPath = FLAGS_mismatchPath[0];
1419 }
1420
1421 for (int i = 0; i < FLAGS_config.count(); i++) {
1422 int index = findConfig(FLAGS_config[i]);
1423 if (index >= 0) {
1424 appendUnique<size_t>(&configs, index);
1425 userConfig = true;
scroggo@google.com0b735632013-03-19 17:38:50 +00001426 } else {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001427 gm_fprintf(stderr, "unrecognized config %s\n", FLAGS_config[i]);
scroggo@google.com7d519302013-03-19 17:28:10 +00001428 return -1;
1429 }
1430 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001431
1432 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1433 int index = findConfig(FLAGS_excludeConfig[i]);
1434 if (index >= 0) {
1435 *excludeConfigs.append() = index;
1436 } else {
1437 gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1438 return -1;
1439 }
1440 }
1441
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001442 int moduloRemainder = -1;
1443 int moduloDivisor = -1;
1444
1445 if (FLAGS_modulo.count() == 2) {
1446 moduloRemainder = atoi(FLAGS_modulo[0]);
1447 moduloDivisor = atoi(FLAGS_modulo[1]);
1448 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
1449 gm_fprintf(stderr, "invalid modulo values.");
1450 return -1;
1451 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001452 }
reed@google.com873cb1e2010-12-23 15:00:45 +00001453
epoger@google.com6f6568b2013-03-22 17:29:46 +00001454#if SK_SUPPORT_GPU
1455 if (FLAGS_gpuCacheSize.count() > 0) {
1456 if (FLAGS_gpuCacheSize.count() != 2) {
1457 gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
1458 return -1;
1459 }
1460 gGpuCacheSizeBytes = atoi(FLAGS_gpuCacheSize[0]);
1461 gGpuCacheSizeCount = atoi(FLAGS_gpuCacheSize[1]);
1462 } else {
1463 gGpuCacheSizeBytes = DEFAULT_CACHE_VALUE;
1464 gGpuCacheSizeCount = DEFAULT_CACHE_VALUE;
1465 }
1466#endif
1467
1468 SkTDArray<SkScalar> tileGridReplayScales;
1469 *tileGridReplayScales.append() = SK_Scalar1; // By default only test at scale 1.0
1470 if (FLAGS_tileGridReplayScales.count() > 0) {
1471 tileGridReplayScales.reset();
1472 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
1473 double val = atof(FLAGS_tileGridReplayScales[i]);
1474 if (0 < val) {
1475 *tileGridReplayScales.append() = SkDoubleToScalar(val);
1476 }
1477 }
1478 if (0 == tileGridReplayScales.count()) {
1479 // Should have at least one scale
1480 gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
1481 return -1;
1482 }
1483 }
1484
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001485 if (!userConfig) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001486 // if no config is specified by user, add the defaults
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001487 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001488 if (gRec[i].fRunByDefault) {
1489 *configs.append() = i;
1490 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001491 }
1492 }
reed@google.comfb2cd422013-01-04 14:43:03 +00001493 // now remove any explicitly excluded configs
1494 for (int i = 0; i < excludeConfigs.count(); ++i) {
1495 int index = configs.find(excludeConfigs[i]);
1496 if (index >= 0) {
1497 configs.remove(index);
1498 // now assert that there was only one copy in configs[]
1499 SkASSERT(configs.find(excludeConfigs[i]) < 0);
1500 }
1501 }
1502
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001503#if SK_SUPPORT_GPU
1504 GrContextFactory* grFactory = new GrContextFactory;
1505 for (int i = 0; i < configs.count(); ++i) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001506 size_t index = configs[i];
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001507 if (kGPU_Backend == gRec[index].fBackend) {
1508 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1509 if (NULL == ctx) {
1510 SkDebugf("GrContext could not be created for config %s. Config will be skipped.",
1511 gRec[index].fName);
1512 configs.remove(i);
1513 --i;
1514 }
1515 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
1516 SkDebugf("Sample count (%d) of config %s is not supported. Config will be skipped.",
1517 gRec[index].fSampleCnt, gRec[index].fName);
1518 configs.remove(i);
1519 --i;
1520 }
1521 }
1522 }
epoger@google.com80724df2013-03-21 13:49:54 +00001523#else
1524 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001525#endif
1526
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001527 if (FLAGS_verbose) {
reed@google.comfb2cd422013-01-04 14:43:03 +00001528 SkString str;
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001529 str.printf("%d configs:", configs.count());
reed@google.comfb2cd422013-01-04 14:43:03 +00001530 for (int i = 0; i < configs.count(); ++i) {
1531 str.appendf(" %s", gRec[configs[i]].fName);
1532 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001533 gm_fprintf(stderr, "%s\n", str.c_str());
reed@google.comfb2cd422013-01-04 14:43:03 +00001534 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001535
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001536 if (FLAGS_resourcePath.count() == 1) {
1537 GM::SetResourcePath(FLAGS_resourcePath[0]);
1538 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001539
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001540 if (FLAGS_readPath.count() == 1) {
1541 const char* readPath = FLAGS_readPath[0];
epoger@google.com37269602013-01-19 04:21:27 +00001542 if (!sk_exists(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001543 gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001544 return -1;
1545 }
1546 if (sk_isdir(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001547 gm_fprintf(stdout, "reading from %s\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001548 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1549 IndividualImageExpectationsSource,
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001550 (readPath, FLAGS_enableMissingWarning)));
epoger@google.com37269602013-01-19 04:21:27 +00001551 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001552 gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
epoger@google.comd271d242013-02-13 18:14:48 +00001553 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1554 JsonExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001555 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00001556 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001557 if (FLAGS_writePath.count() == 1) {
1558 gm_fprintf(stderr, "writing to %s\n", FLAGS_writePath[0]);
reed@android.com00f883e2010-12-14 17:46:14 +00001559 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001560 if (FLAGS_writePicturePath.count() == 1) {
1561 gm_fprintf(stderr, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001562 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001563 if (FLAGS_resourcePath.count() == 1) {
1564 gm_fprintf(stderr, "reading resources from %s\n", FLAGS_resourcePath[0]);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001565 }
1566
epoger@google.com82cb65b2012-10-29 18:59:17 +00001567 if (moduloDivisor <= 0) {
1568 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001569 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001570 if (moduloRemainder < 0 || moduloRemainder >= moduloDivisor) {
1571 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001572 }
1573
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001574 // Accumulate success of all tests.
1575 int testsRun = 0;
1576 int testsPassed = 0;
1577 int testsFailed = 0;
1578 int testsMissingReferenceImages = 0;
1579
reed@google.comae7b8f32012-10-18 21:30:57 +00001580 int gmIndex = -1;
1581 SkString moduloStr;
1582
epoger@google.come8ebeb12012-10-29 16:42:11 +00001583 // If we will be writing out files, prepare subdirectories.
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001584 if (FLAGS_writePath.count() == 1) {
1585 if (!sk_mkdir(FLAGS_writePath[0])) {
epoger@google.come8ebeb12012-10-29 16:42:11 +00001586 return -1;
1587 }
1588 if (gmmain.fUseFileHierarchy) {
1589 for (int i = 0; i < configs.count(); i++) {
1590 ConfigData config = gRec[configs[i]];
1591 SkString subdir;
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001592 subdir.appendf("%s%c%s", FLAGS_writePath[0], SkPATH_SEPARATOR,
epoger@google.come8ebeb12012-10-29 16:42:11 +00001593 config.fName);
1594 if (!sk_mkdir(subdir.c_str())) {
1595 return -1;
1596 }
1597 }
1598 }
1599 }
1600
bsalomon@google.com7361f542012-04-19 19:15:35 +00001601 Iter iter;
1602 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00001603 while ((gm = iter.next()) != NULL) {
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +00001604
reed@google.comae7b8f32012-10-18 21:30:57 +00001605 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00001606 if (moduloRemainder >= 0) {
1607 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00001608 continue;
1609 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001610 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00001611 }
1612
reed@google.comece2b022011-07-25 14:28:57 +00001613 const char* shortName = gm->shortName();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001614 if (skip_name(FLAGS_match, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00001615 SkDELETE(gm);
1616 continue;
1617 }
1618
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001619 SkISize size = gm->getISize();
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001620 gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
1621 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00001622
epoger@google.com6f6568b2013-03-22 17:29:46 +00001623 ErrorCombination testErrors;
1624 testErrors.add(run_multiple_configs(gmmain, gm, configs, grFactory));
djsollen@google.comebce16d2012-10-26 14:07:13 +00001625
1626 SkBitmap comparisonBitmap;
1627 const ConfigData compareConfig =
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001628 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001629 testErrors.add(gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false));
djsollen@google.comebce16d2012-10-26 14:07:13 +00001630
epoger@google.com80724df2013-03-21 13:49:54 +00001631 // TODO(epoger): only run this if gmmain.generate_image() succeeded?
1632 // Otherwise, what are we comparing against?
epoger@google.com6f6568b2013-03-22 17:29:46 +00001633 testErrors.add(run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap,
1634 tileGridReplayScales));
djsollen@google.comebce16d2012-10-26 14:07:13 +00001635
1636 // Update overall results.
1637 // We only tabulate the particular error types that we currently
1638 // care about (e.g., missing reference images). Later on, if we
epoger@google.com37269602013-01-19 04:21:27 +00001639 // want to also tabulate other error types, we can do so.
djsollen@google.comebce16d2012-10-26 14:07:13 +00001640 testsRun++;
epoger@google.com37269602013-01-19 04:21:27 +00001641 if (!gmmain.fExpectationsSource.get() ||
epoger@google.com6f6568b2013-03-22 17:29:46 +00001642 (testErrors.includes(kMissingExpectations_ErrorType))) {
djsollen@google.comebce16d2012-10-26 14:07:13 +00001643 testsMissingReferenceImages++;
borenet@google.coma904ea12013-02-21 18:34:14 +00001644 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001645 if (testErrors.minus(gmmain.fIgnorableErrorCombination).isEmpty()) {
epoger@google.com3499f3c2013-01-10 17:11:27 +00001646 testsPassed++;
djsollen@google.comebce16d2012-10-26 14:07:13 +00001647 } else {
1648 testsFailed++;
1649 }
1650
reed@android.com00dae862009-06-10 15:38:48 +00001651 SkDELETE(gm);
1652 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001653 gm_fprintf(stdout, "Ran %d tests: %d passed, %d failed, %d missing reference images\n",
1654 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
epoger@google.com57f7abc2012-11-13 03:41:55 +00001655 gmmain.ListErrors();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001656
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001657 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.comee8a8e32012-12-18 19:13:49 +00001658 Json::Value actualResults;
1659 actualResults[kJsonKey_ActualResults_Failed] =
1660 gmmain.fJsonActualResults_Failed;
1661 actualResults[kJsonKey_ActualResults_FailureIgnored] =
1662 gmmain.fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001663 actualResults[kJsonKey_ActualResults_NoComparison] =
1664 gmmain.fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001665 actualResults[kJsonKey_ActualResults_Succeeded] =
1666 gmmain.fJsonActualResults_Succeeded;
1667 Json::Value root;
1668 root[kJsonKey_ActualResults] = actualResults;
1669 root[kJsonKey_ExpectedResults] = gmmain.fJsonExpectedResults;
1670 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001671 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00001672 stream.write(jsonStdString.c_str(), jsonStdString.length());
1673 }
1674
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001675#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001676
robertphillips@google.com59552022012-08-31 13:07:37 +00001677#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001678 for (int i = 0; i < configs.count(); i++) {
1679 ConfigData config = gRec[configs[i]];
1680
1681 if (kGPU_Backend == config.fBackend) {
1682 GrContext* gr = grFactory->get(config.fGLContextType);
1683
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001684 gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001685 gr->printCacheStats();
1686 }
1687 }
1688#endif
1689
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001690 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001691#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001692 SkGraphics::Term();
1693
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001694 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001695}
caryclark@google.com5987f582012-10-02 18:33:14 +00001696
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001697void GMMain::installFilter(SkCanvas* canvas) {
1698 if (FLAGS_forceBWtext) {
1699 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
1700 }
1701}
1702
borenet@google.com7158e6a2012-11-01 17:43:44 +00001703#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00001704int main(int argc, char * const argv[]) {
1705 return tool_main(argc, (char**) argv);
1706}
1707#endif