blob: 27a7bdaf9aec516e0a909de81e83c3c26b929710 [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"
epoger@google.com310478e2013-04-03 18:00:39 +000037#include "SkTDict.h"
junov@chromium.org3cb834b2012-12-13 16:39:53 +000038#include "SkTileGridPicture.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000039#include "SamplePipeControllers.h"
reed@google.com07700442010-12-20 19:46:07 +000040
bsalomon@google.com50c79d82013-01-08 20:31:53 +000041#ifdef SK_BUILD_FOR_WIN
42 // json includes xlocale which generates warning 4530 because we're compiling without
epoger@google.com37269602013-01-19 04:21:27 +000043 // exceptions; see https://code.google.com/p/skia/issues/detail?id=1067
bsalomon@google.com50c79d82013-01-08 20:31:53 +000044 #pragma warning(push)
45 #pragma warning(disable : 4530)
46#endif
epoger@google.comee8a8e32012-12-18 19:13:49 +000047#include "json/value.h"
bsalomon@google.com50c79d82013-01-08 20:31:53 +000048#ifdef SK_BUILD_FOR_WIN
49 #pragma warning(pop)
50#endif
epoger@google.comee8a8e32012-12-18 19:13:49 +000051
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000052#if SK_SUPPORT_GPU
53#include "GrContextFactory.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000054#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000055typedef GrContextFactory::GLContextType GLContextType;
epoger@google.com6f6568b2013-03-22 17:29:46 +000056#define DEFAULT_CACHE_VALUE -1
57static int gGpuCacheSizeBytes;
58static int gGpuCacheSizeCount;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000059#else
epoger@google.com80724df2013-03-21 13:49:54 +000060class GrContextFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000061class GrContext;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +000062class GrSurface;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000063typedef int GLContextType;
64#endif
65
reed@google.com8923c6c2011-11-08 14:59:38 +000066extern bool gSkSuppressFontCachePurgeSpew;
67
reed@google.com07700442010-12-20 19:46:07 +000068#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000069 #include "SkPDFDevice.h"
70 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000071#endif
reed@android.com00dae862009-06-10 15:38:48 +000072
epoger@google.come3cc2eb2012-01-18 20:11:13 +000073// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
74// stop writing out XPS-format image baselines in gm.
75#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000076#ifdef SK_SUPPORT_XPS
77 #include "SkXPSDevice.h"
78#endif
79
reed@google.com46cce912011-06-29 12:54:46 +000080#ifdef SK_BUILD_FOR_MAC
81 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000082 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000083#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000084 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000085#endif
86
reed@android.com00dae862009-06-10 15:38:48 +000087using namespace skiagm;
88
reed@android.com00dae862009-06-10 15:38:48 +000089class Iter {
90public:
91 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000092 this->reset();
93 }
94
95 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000096 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000097 }
reed@google.comd4dfd102011-01-18 21:05:42 +000098
reed@android.comdd0ac282009-06-20 02:38:16 +000099 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +0000100 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000101 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +0000102 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +0000103 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +0000104 }
105 return NULL;
106 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000107
reed@android.com00dae862009-06-10 15:38:48 +0000108 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +0000109 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +0000110 int count = 0;
111 while (reg) {
112 count += 1;
113 reg = reg->next();
114 }
115 return count;
116 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000117
reed@android.com00dae862009-06-10 15:38:48 +0000118private:
119 const GMRegistry* fReg;
120};
121
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000122enum Backend {
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000123 kRaster_Backend,
124 kGPU_Backend,
125 kPDF_Backend,
126 kXPS_Backend,
127};
128
129enum BbhType {
130 kNone_BbhType,
131 kRTree_BbhType,
132 kTileGrid_BbhType,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000133};
134
bsalomon@google.com7361f542012-04-19 19:15:35 +0000135enum ConfigFlags {
136 kNone_ConfigFlag = 0x0,
137 /* Write GM images if a write path is provided. */
138 kWrite_ConfigFlag = 0x1,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000139 /* Read reference GM images if a read path is provided. */
bsalomon@google.com7361f542012-04-19 19:15:35 +0000140 kRead_ConfigFlag = 0x2,
141 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
142};
143
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000144struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000145 SkBitmap::Config fConfig;
146 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000147 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000148 int fSampleCnt; // GPU backend only
149 ConfigFlags fFlags;
150 const char* fName;
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000151 bool fRunByDefault;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000152};
153
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000154class BWTextDrawFilter : public SkDrawFilter {
155public:
reed@google.com971aca72012-11-26 20:26:54 +0000156 virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000157};
reed@google.com971aca72012-11-26 20:26:54 +0000158bool BWTextDrawFilter::filter(SkPaint* p, Type t) {
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000159 if (kText_Type == t) {
160 p->setAntiAlias(false);
161 }
reed@google.com971aca72012-11-26 20:26:54 +0000162 return true;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000163}
164
scroggo@google.com565254b2012-06-28 15:41:32 +0000165struct PipeFlagComboData {
166 const char* name;
167 uint32_t flags;
168};
169
170static PipeFlagComboData gPipeWritingFlagCombos[] = {
171 { "", 0 },
172 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000173 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000174 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000175};
176
epoger@google.comde961632012-10-26 18:56:36 +0000177class GMMain {
178public:
epoger@google.com310478e2013-04-03 18:00:39 +0000179 GMMain() : fUseFileHierarchy(false), fMismatchPath(NULL), fTestsRun(0),
180 fRenderModesEncountered(1) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000181 fIgnorableErrorCombination.add(kMissingExpectations_ErrorType);
epoger@google.come8ebeb12012-10-29 16:42:11 +0000182 }
183
184 SkString make_name(const char shortName[], const char configName[]) {
185 SkString name;
epoger@google.com57f7abc2012-11-13 03:41:55 +0000186 if (0 == strlen(configName)) {
187 name.append(shortName);
188 } else if (fUseFileHierarchy) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000189 name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
190 } else {
191 name.appendf("%s_%s", shortName, configName);
192 }
epoger@google.comde961632012-10-26 18:56:36 +0000193 return name;
194 }
195
epoger@google.com5f6a0072013-01-31 16:30:55 +0000196 /* since PNG insists on unpremultiplying our alpha, we take no
197 precision chances and force all pixels to be 100% opaque,
198 otherwise on compare we may not get a perfect match.
199 */
200 static void force_all_opaque(const SkBitmap& bitmap) {
201 SkBitmap::Config config = bitmap.config();
202 switch (config) {
203 case SkBitmap::kARGB_8888_Config:
204 force_all_opaque_8888(bitmap);
205 break;
206 case SkBitmap::kRGB_565_Config:
207 // nothing to do here; 565 bitmaps are inherently opaque
208 break;
209 default:
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000210 gm_fprintf(stderr, "unsupported bitmap config %d\n", config);
211 DEBUGFAIL_SEE_STDERR;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000212 }
213 }
214
215 static void force_all_opaque_8888(const SkBitmap& bitmap) {
216 SkAutoLockPixels lock(bitmap);
217 for (int y = 0; y < bitmap.height(); y++) {
218 for (int x = 0; x < bitmap.width(); x++) {
219 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
220 }
221 }
222 }
223
224 static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
225 // TODO(epoger): Now that we have removed force_all_opaque()
226 // from this method, we should be able to get rid of the
227 // transformation to 8888 format also.
228 SkBitmap copy;
229 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
230 return SkImageEncoder::EncodeFile(path.c_str(), copy,
231 SkImageEncoder::kPNG_Type, 100);
232 }
233
epoger@google.com6f6568b2013-03-22 17:29:46 +0000234 /**
epoger@google.com310478e2013-04-03 18:00:39 +0000235 * Add all render modes encountered thus far to the "modes" array.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000236 */
epoger@google.com310478e2013-04-03 18:00:39 +0000237 void GetRenderModesEncountered(SkTArray<SkString> &modes) {
238 SkTDict<int>::Iter iter(this->fRenderModesEncountered);
239 const char* mode;
240 while ((mode = iter.next(NULL)) != NULL) {
241 SkString modeAsString = SkString(mode);
242 // TODO(epoger): It seems a bit silly that all of these modes were
243 // recorded with a leading "-" which we have to remove here
244 // (except for mode "", which means plain old original mode).
245 // But that's how renderModeDescriptor has been passed into
246 // compare_test_results_to_reference_bitmap() historically,
247 // and changing that now may affect other parts of our code.
248 if (modeAsString.startsWith("-")) {
249 modeAsString.remove(0, 1);
250 modes.push_back(modeAsString);
251 }
252 }
253 }
254
255 /**
256 * Records the results of this test in fTestsRun and fFailedTests.
257 *
258 * We even record successes, and errors that we regard as
259 * "ignorable"; we can filter them out later.
260 */
261 void RecordTestResults(const ErrorCombination& errorCombination, const SkString& name,
262 const char renderModeDescriptor []) {
263 // Things to do regardless of errorCombination.
264 fTestsRun++;
265 int renderModeCount = 0;
266 this->fRenderModesEncountered.find(renderModeDescriptor, &renderModeCount);
267 renderModeCount++;
268 this->fRenderModesEncountered.set(renderModeDescriptor, renderModeCount);
269
epoger@google.com6f6568b2013-03-22 17:29:46 +0000270 if (errorCombination.isEmpty()) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000271 return;
epoger@google.comeb066362013-03-08 09:39:36 +0000272 }
273
epoger@google.com310478e2013-04-03 18:00:39 +0000274 // Things to do only if there is some error condition.
epoger@google.comcaac3db2013-04-04 19:23:11 +0000275 SkString fullName = name;
276 fullName.append(renderModeDescriptor);
epoger@google.com310478e2013-04-03 18:00:39 +0000277 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
278 ErrorType type = static_cast<ErrorType>(typeInt);
279 if (errorCombination.includes(type)) {
280 fFailedTests[type].push_back(fullName);
epoger@google.comf60494b2013-04-03 17:02:53 +0000281 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000282 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000283 }
284
epoger@google.com310478e2013-04-03 18:00:39 +0000285 /**
286 * Return the number of significant (non-ignorable) errors we have
287 * encountered so far.
288 */
289 int NumSignificantErrors() {
290 int significantErrors = 0;
291 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
292 ErrorType type = static_cast<ErrorType>(typeInt);
293 if (!fIgnorableErrorCombination.includes(type)) {
294 significantErrors += fFailedTests[type].count();
295 }
296 }
297 return significantErrors;
298 }
299
300 /**
301 * List contents of fFailedTests to stdout.
302 */
303 void ListErrors() {
304 // First, print a single summary line.
305 SkString summary;
306 summary.appendf("Ran %d tests:", fTestsRun);
307 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
308 ErrorType type = static_cast<ErrorType>(typeInt);
309 summary.appendf(" %s=%d", getErrorTypeName(type), fFailedTests[type].count());
310 }
311 gm_fprintf(stdout, "%s\n", summary.c_str());
312
313 // Now, for each failure type, list the tests that failed that way.
314 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
315 SkString line;
316 ErrorType type = static_cast<ErrorType>(typeInt);
317 if (fIgnorableErrorCombination.includes(type)) {
318 line.append("[ ] ");
319 } else {
320 line.append("[*] ");
321 }
322
323 SkTArray<SkString> *failedTestsOfThisType = &fFailedTests[type];
324 int count = failedTestsOfThisType->count();
325 line.appendf("%d %s:", count, getErrorTypeName(type));
326 for (int i = 0; i < count; ++i) {
327 line.append(" ");
328 line.append((*failedTestsOfThisType)[i]);
329 }
330 gm_fprintf(stdout, "%s\n", line.c_str());
331 }
332 gm_fprintf(stdout, "(results marked with [*] will cause nonzero return value)\n");
333 }
334
epoger@google.com5f6a0072013-01-31 16:30:55 +0000335 static bool write_document(const SkString& path,
336 const SkDynamicMemoryWStream& document) {
337 SkFILEWStream stream(path.c_str());
338 SkAutoDataUnref data(document.copyToData());
339 return stream.writeData(data.get());
340 }
341
epoger@google.com37269602013-01-19 04:21:27 +0000342 /**
epoger@google.com5f6a0072013-01-31 16:30:55 +0000343 * Prepare an SkBitmap to render a GM into.
344 *
345 * After you've rendered the GM into the SkBitmap, you must call
346 * complete_bitmap()!
347 *
348 * @todo thudson 22 April 2011 - could refactor this to take in
349 * a factory to generate the context, always call readPixels()
350 * (logically a noop for rasters, if wasted time), and thus collapse the
351 * GPU special case and also let this be used for SkPicture testing.
352 */
353 static void setup_bitmap(const ConfigData& gRec, SkISize& size,
354 SkBitmap* bitmap) {
355 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
356 bitmap->allocPixels();
357 bitmap->eraseColor(SK_ColorTRANSPARENT);
358 }
359
360 /**
361 * Any finalization steps we need to perform on the SkBitmap after
362 * we have rendered the GM into it.
epoger@google.com37269602013-01-19 04:21:27 +0000363 *
364 * It's too bad that we are throwing away alpha channel data
365 * we could otherwise be examining, but this had always been happening
366 * before... it was buried within the compare() method at
367 * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
368 *
369 * Apparently we need this, at least for bitmaps that are either:
370 * (a) destined to be written out as PNG files, or
371 * (b) compared against bitmaps read in from PNG files
372 * for the reasons described just above the force_all_opaque() method.
373 *
374 * Neglecting to do this led to the difficult-to-diagnose
375 * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
376 * spurious pixel_error messages as of r7258')
377 *
378 * TODO(epoger): Come up with a better solution that allows us to
379 * compare full pixel data, including alpha channel, while still being
380 * robust in the face of transformations to/from PNG files.
381 * Options include:
382 *
383 * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
384 * will be written to, or compared against, PNG files.
385 * PRO: Preserve/compare alpha channel info for the non-PNG cases
386 * (comparing different renderModes in-memory)
387 * CON: The bitmaps (and checksums) for these non-PNG cases would be
388 * different than those for the PNG-compared cases, and in the
389 * case of a failed renderMode comparison, how would we write the
390 * image to disk for examination?
391 *
392 * 2. Always compute image checksums from PNG format (either
393 * directly from the the bytes of a PNG file, or capturing the
394 * bytes we would have written to disk if we were writing the
395 * bitmap out as a PNG).
396 * PRO: I think this would allow us to never force opaque, and to
397 * the extent that alpha channel data can be preserved in a PNG
398 * file, we could observe it.
399 * CON: If we read a bitmap from disk, we need to take its checksum
400 * from the source PNG (we can't compute it from the bitmap we
401 * read out of the PNG, because we will have already premultiplied
402 * the alpha).
403 * CON: Seems wasteful to convert a bitmap to PNG format just to take
404 * its checksum. (Although we're wasting lots of effort already
405 * calling force_all_opaque().)
406 *
407 * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
408 * so we can transform images back and forth without fear of off-by-one
409 * errors.
410 * CON: Math is hard.
411 *
412 * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
413 * channel), rather than demanding absolute equality.
414 * CON: Can't do this with checksums.
415 */
epoger@google.com5f6a0072013-01-31 16:30:55 +0000416 static void complete_bitmap(SkBitmap* bitmap) {
417 force_all_opaque(*bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000418 }
419
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000420 static void installFilter(SkCanvas* canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000421
reed@google.comaef73612012-11-16 13:41:45 +0000422 static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000423 SkAutoCanvasRestore acr(canvas, true);
424
425 if (!isPDF) {
426 canvas->concat(gm->getInitialTransform());
427 }
428 installFilter(canvas);
reed@google.comaef73612012-11-16 13:41:45 +0000429 gm->setCanvasIsDeferred(isDeferred);
epoger@google.comde961632012-10-26 18:56:36 +0000430 gm->draw(canvas);
431 canvas->setDrawFilter(NULL);
432 }
433
epoger@google.com6f6568b2013-03-22 17:29:46 +0000434 static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000435 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000436 SkBitmap* bitmap,
437 bool deferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000438 SkISize size (gm->getISize());
439 setup_bitmap(gRec, size, bitmap);
440
441 SkAutoTUnref<SkCanvas> canvas;
442
443 if (gRec.fBackend == kRaster_Backend) {
444 SkAutoTUnref<SkDevice> device(new SkDevice(*bitmap));
445 if (deferred) {
446 canvas.reset(new SkDeferredCanvas(device));
447 } else {
448 canvas.reset(new SkCanvas(device));
449 }
reed@google.comaef73612012-11-16 13:41:45 +0000450 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000451 canvas->flush();
452 }
453#if SK_SUPPORT_GPU
454 else { // GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000455 SkAutoTUnref<SkDevice> device(SkGpuDevice::Create(gpuTarget));
epoger@google.comde961632012-10-26 18:56:36 +0000456 if (deferred) {
457 canvas.reset(new SkDeferredCanvas(device));
458 } else {
459 canvas.reset(new SkCanvas(device));
460 }
reed@google.comaef73612012-11-16 13:41:45 +0000461 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000462 // the device is as large as the current rendertarget, so
463 // we explicitly only readback the amount we expect (in
464 // size) overwrite our previous allocation
465 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
466 size.fHeight);
467 canvas->readPixels(bitmap, 0, 0);
468 }
469#endif
epoger@google.com5f6a0072013-01-31 16:30:55 +0000470 complete_bitmap(bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000471 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000472 }
473
474 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
junov@chromium.orgc938c482012-12-19 15:24:38 +0000475 SkPicture* pict, SkBitmap* bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000476 SkScalar scale = SK_Scalar1,
477 bool tile = false) {
epoger@google.comde961632012-10-26 18:56:36 +0000478 SkISize size = gm->getISize();
479 setup_bitmap(gRec, size, bitmap);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000480
481 if (tile) {
482 // Generate the result image by rendering to tiles and accumulating
483 // the results in 'bitmap'
484
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000485 // This 16x16 tiling matches the settings applied to 'pict' in
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000486 // 'generate_new_picture'
487 SkISize tileSize = SkISize::Make(16, 16);
488
489 SkBitmap tileBM;
490 setup_bitmap(gRec, tileSize, &tileBM);
491 SkCanvas tileCanvas(tileBM);
492 installFilter(&tileCanvas);
493
494 SkCanvas bmpCanvas(*bitmap);
495 SkPaint bmpPaint;
496 bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
497
498 for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
499 for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
500 int saveCount = tileCanvas.save();
501 SkMatrix mat(tileCanvas.getTotalMatrix());
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000502 mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000503 SkIntToScalar(-yTile*tileSize.height()));
504 tileCanvas.setMatrix(mat);
505 pict->draw(&tileCanvas);
506 tileCanvas.flush();
507 tileCanvas.restoreToCount(saveCount);
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000508 bmpCanvas.drawBitmap(tileBM,
509 SkIntToScalar(xTile * tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000510 SkIntToScalar(yTile * tileSize.height()),
511 &bmpPaint);
512 }
513 }
514 } else {
515 SkCanvas canvas(*bitmap);
516 installFilter(&canvas);
517 canvas.scale(scale, scale);
518 canvas.drawPicture(*pict);
519 complete_bitmap(bitmap);
520 }
epoger@google.comde961632012-10-26 18:56:36 +0000521 }
522
523 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
524#ifdef SK_SUPPORT_PDF
525 SkMatrix initialTransform = gm->getInitialTransform();
526 SkISize pageSize = gm->getISize();
527 SkPDFDevice* dev = NULL;
528 if (initialTransform.isIdentity()) {
529 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
530 } else {
531 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
532 SkIntToScalar(pageSize.height()));
533 initialTransform.mapRect(&content);
534 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
535 SkIntToScalar(pageSize.height()));
536 SkISize contentSize =
537 SkISize::Make(SkScalarRoundToInt(content.width()),
538 SkScalarRoundToInt(content.height()));
539 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
540 }
541 SkAutoUnref aur(dev);
542
543 SkCanvas c(dev);
reed@google.comaef73612012-11-16 13:41:45 +0000544 invokeGM(gm, &c, true, false);
epoger@google.comde961632012-10-26 18:56:36 +0000545
546 SkPDFDocument doc;
547 doc.appendPage(dev);
548 doc.emitPDF(&pdf);
549#endif
550 }
551
552 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
553#ifdef SK_SUPPORT_XPS
554 SkISize size = gm->getISize();
555
556 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
557 SkIntToScalar(size.height()));
558 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
559 static const SkScalar upm = 72 * inchesPerMeter;
560 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
561 static const SkScalar ppm = 200 * inchesPerMeter;
562 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
563
564 SkXPSDevice* dev = new SkXPSDevice();
565 SkAutoUnref aur(dev);
566
567 SkCanvas c(dev);
568 dev->beginPortfolio(&xps);
569 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000570 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000571 dev->endSheet();
572 dev->endPortfolio();
573
574#endif
575 }
576
epoger@google.com6f6568b2013-03-22 17:29:46 +0000577 ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
578 const char renderModeDescriptor [], const SkString& name,
579 SkBitmap& bitmap, SkDynamicMemoryWStream* document) {
epoger@google.comde961632012-10-26 18:56:36 +0000580 SkString path;
581 bool success = false;
582 if (gRec.fBackend == kRaster_Backend ||
583 gRec.fBackend == kGPU_Backend ||
584 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
585
epoger@google.com37269602013-01-19 04:21:27 +0000586 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
587 "png");
epoger@google.comde961632012-10-26 18:56:36 +0000588 success = write_bitmap(path, bitmap);
589 }
590 if (kPDF_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000591 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
592 "pdf");
epoger@google.comde961632012-10-26 18:56:36 +0000593 success = write_document(path, *document);
594 }
595 if (kXPS_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000596 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
597 "xps");
epoger@google.comde961632012-10-26 18:56:36 +0000598 success = write_document(path, *document);
599 }
600 if (success) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000601 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000602 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000603 gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000604 ErrorCombination errors(kWritingReferenceImage_ErrorType);
epoger@google.com310478e2013-04-03 18:00:39 +0000605 // TODO(epoger): Don't call RecordTestResults() here...
606 // Instead, we should make sure to call RecordTestResults
607 // exactly ONCE per test. (Otherwise, gmmain.fTestsRun
608 // will be incremented twice for this test: once in
609 // compare_test_results_to_stored_expectations() before
610 // that method calls this one, and again here.)
611 //
612 // When we make that change, we should probably add a
613 // WritingReferenceImage test to the gm self-tests.)
614 RecordTestResults(errors, name, renderModeDescriptor);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000615 return errors;
epoger@google.comde961632012-10-26 18:56:36 +0000616 }
617 }
618
epoger@google.com37269602013-01-19 04:21:27 +0000619 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000620 * Log more detail about the mistmatch between expectedBitmap and
621 * actualBitmap.
622 */
623 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
624 const char *testName) {
625 const int expectedWidth = expectedBitmap.width();
626 const int expectedHeight = expectedBitmap.height();
627 const int width = actualBitmap.width();
628 const int height = actualBitmap.height();
629 if ((expectedWidth != width) || (expectedHeight != height)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000630 gm_fprintf(stderr, "---- %s: dimension mismatch --"
631 " expected [%d %d], actual [%d %d]\n",
632 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000633 return;
634 }
635
636 if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
637 (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000638 gm_fprintf(stderr, "---- %s: not computing max per-channel"
639 " pixel mismatch because non-8888\n", testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000640 return;
641 }
642
643 SkAutoLockPixels alp0(expectedBitmap);
644 SkAutoLockPixels alp1(actualBitmap);
645 int errR = 0;
646 int errG = 0;
647 int errB = 0;
648 int errA = 0;
649 int differingPixels = 0;
650
651 for (int y = 0; y < height; ++y) {
652 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
653 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
654 for (int x = 0; x < width; ++x) {
655 SkPMColor expectedPixel = *expectedPixelPtr++;
656 SkPMColor actualPixel = *actualPixelPtr++;
657 if (expectedPixel != actualPixel) {
658 differingPixels++;
659 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
660 (int)SkGetPackedR32(actualPixel)));
661 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
662 (int)SkGetPackedG32(actualPixel)));
663 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
664 (int)SkGetPackedB32(actualPixel)));
665 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
666 (int)SkGetPackedA32(actualPixel)));
667 }
668 }
669 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000670 gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
671 " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
672 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000673 }
674
675 /**
epoger@google.com6f6568b2013-03-22 17:29:46 +0000676 * Compares actual checksum to expectations, returning the set of errors
677 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000678 *
679 * If fMismatchPath has been set, and there are pixel diffs, then the
680 * actual bitmap will be written out to a file within fMismatchPath.
681 *
682 * @param expectations what expectations to compare actualBitmap against
683 * @param actualBitmap the image we actually generated
684 * @param baseNameString name of test without renderModeDescriptor added
685 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
686 * @param addToJsonSummary whether to add these results (both actual and
epoger@google.comcaac3db2013-04-04 19:23:11 +0000687 * expected) to the JSON summary. Regardless of this setting, if
688 * we find an image mismatch in this test, we will write these
689 * results to the JSON summary. (This is so that we will always
690 * report errors across rendering modes, such as pipe vs tiled.
691 * See https://codereview.chromium.org/13650002/ )
epoger@google.com37269602013-01-19 04:21:27 +0000692 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000693 ErrorCombination compare_to_expectations(Expectations expectations,
694 const SkBitmap& actualBitmap,
695 const SkString& baseNameString,
696 const char renderModeDescriptor[],
epoger@google.comcaac3db2013-04-04 19:23:11 +0000697 bool addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000698 ErrorCombination errors;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000699 Checksum actualChecksum = SkBitmapChecksummer::Compute64(actualBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000700 SkString completeNameString = baseNameString;
701 completeNameString.append(renderModeDescriptor);
702 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000703
epoger@google.com37269602013-01-19 04:21:27 +0000704 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000705 errors.add(kMissingExpectations_ErrorType);
706 } else if (!expectations.match(actualChecksum)) {
epoger@google.comcaac3db2013-04-04 19:23:11 +0000707 addToJsonSummary = true;
708 // The error mode we record depends on whether this was running
709 // in a non-standard renderMode.
710 if ('\0' == *renderModeDescriptor) {
711 errors.add(kExpectationsMismatch_ErrorType);
712 } else {
713 errors.add(kRenderModeMismatch_ErrorType);
714 }
epoger@google.com84a18022013-02-01 20:39:15 +0000715
716 // Write out the "actuals" for any mismatches, if we have
717 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000718 if (fMismatchPath) {
719 SkString path =
720 make_filename(fMismatchPath, renderModeDescriptor,
721 baseNameString.c_str(), "png");
722 write_bitmap(path, actualBitmap);
723 }
epoger@google.com84a18022013-02-01 20:39:15 +0000724
725 // If we have access to a single expected bitmap, log more
726 // detail about the mismatch.
727 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
728 if (NULL != expectedBitmapPtr) {
729 report_bitmap_diffs(*expectedBitmapPtr, actualBitmap, completeName);
730 }
epoger@google.coma243b222013-01-17 17:54:28 +0000731 }
epoger@google.com310478e2013-04-03 18:00:39 +0000732 RecordTestResults(errors, baseNameString, renderModeDescriptor);
epoger@google.coma243b222013-01-17 17:54:28 +0000733
epoger@google.com37269602013-01-19 04:21:27 +0000734 if (addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000735 add_actual_results_to_json_summary(completeName, actualChecksum, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000736 expectations.ignoreFailure());
737 add_expected_results_to_json_summary(completeName, expectations);
738 }
epoger@google.coma243b222013-01-17 17:54:28 +0000739
epoger@google.com6f6568b2013-03-22 17:29:46 +0000740 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000741 }
742
epoger@google.com37269602013-01-19 04:21:27 +0000743 /**
744 * Add this result to the appropriate JSON collection of actual results,
745 * depending on status.
746 */
747 void add_actual_results_to_json_summary(const char testName[],
748 Checksum actualChecksum,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000749 ErrorCombination result,
epoger@google.com37269602013-01-19 04:21:27 +0000750 bool ignoreFailure) {
751 Json::Value actualResults;
752 actualResults[kJsonKey_ActualResults_AnyStatus_Checksum] =
753 asJsonValue(actualChecksum);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000754 if (result.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +0000755 this->fJsonActualResults_Succeeded[testName] = actualResults;
756 } else {
757 if (ignoreFailure) {
758 // TODO: Once we have added the ability to compare
759 // actual results against expectations in a JSON file
760 // (where we can set ignore-failure to either true or
epoger@google.com84a18022013-02-01 20:39:15 +0000761 // false), add test cases that exercise ignored
epoger@google.com6f6568b2013-03-22 17:29:46 +0000762 // failures (both for kMissingExpectations_ErrorType
epoger@google.comcaac3db2013-04-04 19:23:11 +0000763 // and kExpectationsMismatch_ErrorType).
epoger@google.com37269602013-01-19 04:21:27 +0000764 this->fJsonActualResults_FailureIgnored[testName] =
765 actualResults;
766 } else {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000767 if (result.includes(kMissingExpectations_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000768 // TODO: What about the case where there IS an
769 // expected image checksum, but that gm test
770 // doesn't actually run? For now, those cases
771 // will always be ignored, because gm only looks
772 // at expectations that correspond to gm tests
773 // that were actually run.
774 //
775 // Once we have the ability to express
776 // expectations as a JSON file, we should fix this
777 // (and add a test case for which an expectation
778 // is given but the test is never run).
779 this->fJsonActualResults_NoComparison[testName] =
780 actualResults;
epoger@google.comeb066362013-03-08 09:39:36 +0000781 }
epoger@google.comcaac3db2013-04-04 19:23:11 +0000782 if (result.includes(kExpectationsMismatch_ErrorType) ||
783 result.includes(kRenderModeMismatch_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000784 this->fJsonActualResults_Failed[testName] = actualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000785 }
786 }
787 }
788 }
789
790 /**
791 * Add this test to the JSON collection of expected results.
792 */
793 void add_expected_results_to_json_summary(const char testName[],
794 Expectations expectations) {
795 // For now, we assume that this collection starts out empty and we
796 // just fill it in as we go; once gm accepts a JSON file as input,
797 // we'll have to change that.
798 Json::Value expectedResults;
799 expectedResults[kJsonKey_ExpectedResults_Checksums] =
800 expectations.allowedChecksumsAsJson();
801 expectedResults[kJsonKey_ExpectedResults_IgnoreFailure] =
802 expectations.ignoreFailure();
803 this->fJsonExpectedResults[testName] = expectedResults;
804 }
805
806 /**
807 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
808 *
809 * @param gm which test generated the actualBitmap
810 * @param gRec
811 * @param writePath unless this is NULL, write out actual images into this
812 * directory
813 * @param actualBitmap bitmap generated by this run
814 * @param pdf
815 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000816 ErrorCombination compare_test_results_to_stored_expectations(
epoger@google.com37269602013-01-19 04:21:27 +0000817 GM* gm, const ConfigData& gRec, const char writePath[],
818 SkBitmap& actualBitmap, SkDynamicMemoryWStream* pdf) {
819
epoger@google.coma243b222013-01-17 17:54:28 +0000820 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000821 ErrorCombination errors;
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000822
epoger@google.com6f6568b2013-03-22 17:29:46 +0000823 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
epoger@google.com37269602013-01-19 04:21:27 +0000824 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
825 /*
826 * Get the expected results for this test, as one or more allowed
827 * checksums. The current implementation of expectationsSource
828 * get this by computing the checksum of a single PNG file on disk.
829 *
830 * TODO(epoger): This relies on the fact that
831 * force_all_opaque() was called on the bitmap before it
832 * was written to disk as a PNG in the first place. If
833 * not, the checksum returned here may not match the
834 * checksum of actualBitmap, which *has* been run through
835 * force_all_opaque().
epoger@google.com5f6a0072013-01-31 16:30:55 +0000836 * See comments above complete_bitmap() for more detail.
epoger@google.com37269602013-01-19 04:21:27 +0000837 */
838 Expectations expectations = expectationsSource->get(name.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000839 errors.add(compare_to_expectations(expectations, actualBitmap,
840 name, "", true));
epoger@google.com37269602013-01-19 04:21:27 +0000841 } else {
842 // If we are running without expectations, we still want to
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000843 // record the actual results.
epoger@google.com5f6a0072013-01-31 16:30:55 +0000844 Checksum actualChecksum =
845 SkBitmapChecksummer::Compute64(actualBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000846 add_actual_results_to_json_summary(name.c_str(), actualChecksum,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000847 ErrorCombination(kMissingExpectations_ErrorType),
epoger@google.com37269602013-01-19 04:21:27 +0000848 false);
epoger@google.com310478e2013-04-03 18:00:39 +0000849 RecordTestResults(ErrorCombination(kMissingExpectations_ErrorType), name, "");
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000850 }
epoger@google.com37269602013-01-19 04:21:27 +0000851
852 // TODO: Consider moving this into compare_to_expectations(),
853 // similar to fMismatchPath... for now, we don't do that, because
854 // we don't want to write out the actual bitmaps for all
855 // renderModes of all tests! That would be a lot of files.
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000856 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000857 errors.add(write_reference_image(gRec, writePath, "",
858 name, actualBitmap, pdf));
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000859 }
epoger@google.com37269602013-01-19 04:21:27 +0000860
epoger@google.com6f6568b2013-03-22 17:29:46 +0000861 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000862 }
863
epoger@google.com37269602013-01-19 04:21:27 +0000864 /**
865 * Compare actualBitmap to referenceBitmap.
866 *
867 * @param gm which test generated the bitmap
868 * @param gRec
869 * @param renderModeDescriptor
870 * @param actualBitmap actual bitmap generated by this run
871 * @param referenceBitmap bitmap we expected to be generated
872 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000873 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.com37269602013-01-19 04:21:27 +0000874 GM* gm, const ConfigData& gRec, const char renderModeDescriptor [],
875 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
876
877 SkASSERT(referenceBitmap);
878 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com84a18022013-02-01 20:39:15 +0000879 Expectations expectations(*referenceBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000880 return compare_to_expectations(expectations, actualBitmap,
epoger@google.comcaac3db2013-04-04 19:23:11 +0000881 name, renderModeDescriptor, false);
epoger@google.com37269602013-01-19 04:21:27 +0000882 }
883
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000884 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
885 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +0000886 // Pictures are refcounted so must be on heap
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000887 SkPicture* pict;
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000888 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
889 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +0000890
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000891 if (kTileGrid_BbhType == bbhType) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000892 SkTileGridPicture::TileGridInfo info;
893 info.fMargin.setEmpty();
894 info.fOffset.setZero();
895 info.fTileInterval.set(16, 16);
896 pict = new SkTileGridPicture(width, height, info);
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000897 } else {
898 pict = new SkPicture;
899 }
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000900 if (kNone_BbhType != bbhType) {
901 recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
902 }
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000903 SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +0000904 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +0000905 invokeGM(gm, cv, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000906 pict->endRecording();
907
908 return pict;
909 }
910
911 static SkPicture* stream_to_new_picture(const SkPicture& src) {
912
913 // To do in-memory commiunications with a stream, we need to:
914 // * create a dynamic memory stream
915 // * copy it into a buffer
916 // * create a read stream from it
917 // ?!?!
918
919 SkDynamicMemoryWStream storage;
920 src.serialize(&storage);
921
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000922 size_t streamSize = storage.getOffset();
epoger@google.comde961632012-10-26 18:56:36 +0000923 SkAutoMalloc dstStorage(streamSize);
924 void* dst = dstStorage.get();
925 //char* dst = new char [streamSize];
926 //@todo thudson 22 April 2011 when can we safely delete [] dst?
927 storage.copyTo(dst);
928 SkMemoryStream pictReadback(dst, streamSize);
929 SkPicture* retval = new SkPicture (&pictReadback);
930 return retval;
931 }
932
933 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +0000934 // Depending on flags, possibly compare to an expected image.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000935 ErrorCombination test_drawing(GM* gm,
936 const ConfigData& gRec,
937 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000938 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000939 SkBitmap* bitmap) {
epoger@google.comde961632012-10-26 18:56:36 +0000940 SkDynamicMemoryWStream document;
941
942 if (gRec.fBackend == kRaster_Backend ||
943 gRec.fBackend == kGPU_Backend) {
944 // Early exit if we can't generate the image.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000945 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000946 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +0000947 // TODO: Add a test to exercise what the stdout and
948 // JSON look like if we get an "early error" while
949 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +0000950 return errors;
951 }
952 } else if (gRec.fBackend == kPDF_Backend) {
953 generate_pdf(gm, document);
954#if CAN_IMAGE_PDF
955 SkAutoDataUnref data(document.copyToData());
956 SkMemoryStream stream(data->data(), data->size());
957 SkPDFDocumentToBitmap(&stream, bitmap);
958#endif
959 } else if (gRec.fBackend == kXPS_Backend) {
960 generate_xps(gm, document);
961 }
epoger@google.com37269602013-01-19 04:21:27 +0000962 return compare_test_results_to_stored_expectations(
963 gm, gRec, writePath, *bitmap, &document);
epoger@google.comde961632012-10-26 18:56:36 +0000964 }
965
epoger@google.com6f6568b2013-03-22 17:29:46 +0000966 ErrorCombination test_deferred_drawing(GM* gm,
967 const ConfigData& gRec,
968 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000969 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +0000970 SkDynamicMemoryWStream document;
971
972 if (gRec.fBackend == kRaster_Backend ||
973 gRec.fBackend == kGPU_Backend) {
974 SkBitmap bitmap;
975 // Early exit if we can't generate the image, but this is
976 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000977 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000978 // TODO(epoger): This logic is the opposite of what is
979 // described above... if we succeeded in generating the
980 // -deferred image, we exit early! We should fix this
981 // ASAP, because it is hiding -deferred errors... but for
982 // now, I'm leaving the logic as it is so that the
983 // refactoring change
984 // https://codereview.chromium.org/12992003/ is unblocked.
985 //
986 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
987 // ('image-surface gm test is failing in "deferred" mode,
988 // and gm is not reporting the failure')
989 if (errors.isEmpty()) {
epoger@google.com310478e2013-04-03 18:00:39 +0000990 // TODO(epoger): Report this as a new ErrorType,
991 // something like kImageGeneration_ErrorType?
epoger@google.com6f6568b2013-03-22 17:29:46 +0000992 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000993 }
epoger@google.com37269602013-01-19 04:21:27 +0000994 return compare_test_results_to_reference_bitmap(
995 gm, gRec, "-deferred", bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000996 }
epoger@google.com6f6568b2013-03-22 17:29:46 +0000997 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000998 }
999
epoger@google.comcaac3db2013-04-04 19:23:11 +00001000 ErrorCombination test_pipe_playback(GM* gm, const ConfigData& gRec,
1001 const SkBitmap& referenceBitmap, bool simulateFailure) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001002 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001003 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
1004 SkBitmap bitmap;
1005 SkISize size = gm->getISize();
1006 setup_bitmap(gRec, size, &bitmap);
1007 SkCanvas canvas(bitmap);
scroggo@google.com0b735632013-03-19 17:38:50 +00001008 installFilter(&canvas);
epoger@google.comde961632012-10-26 18:56:36 +00001009 PipeController pipeController(&canvas);
1010 SkGPipeWriter writer;
1011 SkCanvas* pipeCanvas = writer.startRecording(
1012 &pipeController, gPipeWritingFlagCombos[i].flags);
epoger@google.comcaac3db2013-04-04 19:23:11 +00001013 if (!simulateFailure) {
1014 invokeGM(gm, pipeCanvas, false, false);
1015 }
epoger@google.com5f6a0072013-01-31 16:30:55 +00001016 complete_bitmap(&bitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001017 writer.endRecording();
1018 SkString string("-pipe");
1019 string.append(gPipeWritingFlagCombos[i].name);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001020 errors.add(compare_test_results_to_reference_bitmap(
1021 gm, gRec, string.c_str(), bitmap, &referenceBitmap));
1022 if (!errors.isEmpty()) {
epoger@google.comde961632012-10-26 18:56:36 +00001023 break;
1024 }
1025 }
1026 return errors;
1027 }
1028
epoger@google.com6f6568b2013-03-22 17:29:46 +00001029 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
1030 const SkBitmap& referenceBitmap) {
1031 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001032 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
1033 SkBitmap bitmap;
1034 SkISize size = gm->getISize();
1035 setup_bitmap(gRec, size, &bitmap);
1036 SkCanvas canvas(bitmap);
scroggo@google.com0b735632013-03-19 17:38:50 +00001037 installFilter(&canvas);
epoger@google.comde961632012-10-26 18:56:36 +00001038 TiledPipeController pipeController(bitmap);
1039 SkGPipeWriter writer;
1040 SkCanvas* pipeCanvas = writer.startRecording(
1041 &pipeController, gPipeWritingFlagCombos[i].flags);
reed@google.comaef73612012-11-16 13:41:45 +00001042 invokeGM(gm, pipeCanvas, false, false);
epoger@google.com5f6a0072013-01-31 16:30:55 +00001043 complete_bitmap(&bitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001044 writer.endRecording();
1045 SkString string("-tiled pipe");
1046 string.append(gPipeWritingFlagCombos[i].name);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001047 errors.add(compare_test_results_to_reference_bitmap(
1048 gm, gRec, string.c_str(), bitmap, &referenceBitmap));
1049 if (!errors.isEmpty()) {
epoger@google.comde961632012-10-26 18:56:36 +00001050 break;
1051 }
1052 }
1053 return errors;
1054 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00001055
1056 //
1057 // member variables.
1058 // They are public for now, to allow easier setting by tool_main().
1059 //
1060
epoger@google.come8ebeb12012-10-29 16:42:11 +00001061 bool fUseFileHierarchy;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001062 ErrorCombination fIgnorableErrorCombination;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001063
junov@chromium.org95146eb2013-01-11 21:04:40 +00001064 const char* fMismatchPath;
1065
epoger@google.com310478e2013-04-03 18:00:39 +00001066 // collection of tests that have failed with each ErrorType
1067 SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
1068 int fTestsRun;
1069 SkTDict<int> fRenderModesEncountered;
epoger@google.com57f7abc2012-11-13 03:41:55 +00001070
epoger@google.com37269602013-01-19 04:21:27 +00001071 // Where to read expectations (expected image checksums, etc.) from.
1072 // If unset, we don't do comparisons.
1073 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
1074
1075 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +00001076 Json::Value fJsonExpectedResults;
1077 Json::Value fJsonActualResults_Failed;
1078 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001079 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001080 Json::Value fJsonActualResults_Succeeded;
1081
epoger@google.comde961632012-10-26 18:56:36 +00001082}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +00001083
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001084#if SK_SUPPORT_GPU
1085static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1086#else
1087static const GLContextType kDontCare_GLContextType = 0;
1088#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +00001089
1090// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +00001091// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +00001092static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
1093 kWrite_ConfigFlag;
1094
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001095static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001096 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001097#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 +00001098 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001099#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001100 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
1101#if SK_SUPPORT_GPU
1102 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
1103 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", true },
1104 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
bsalomon@google.com7361f542012-04-19 19:15:35 +00001105 /* The debug context does not generate images */
scroggo@google.com0f567c62013-03-20 15:35:08 +00001106 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", GR_DEBUG},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001107#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001108 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
1109 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001110#endif // SK_ANGLE
1111#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001112 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001113#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001114#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +00001115#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001116 /* 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 +00001117 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +00001118#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001119#ifdef SK_SUPPORT_PDF
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001120 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001121#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001122};
1123
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001124static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001125 SkString result;
1126 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001127 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1128 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001129 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001130 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001131 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001132 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001133 result.append("]\n");
1134 result.appendf("The default value is: \"");
1135 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1136 if (gRec[i].fRunByDefault) {
1137 if (i > 0) {
1138 result.append(" ");
1139 }
1140 result.appendf("%s", gRec[i].fName);
1141 }
1142 }
1143 result.appendf("\"");
1144
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001145 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001146}
scroggo@google.com7d519302013-03-19 17:28:10 +00001147
epoger@google.com6f6568b2013-03-22 17:29:46 +00001148// Macro magic to convert a numeric preprocessor token into a string.
1149// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1150// This should probably be moved into one of our common headers...
1151#define TOSTRING_INTERNAL(x) #x
1152#define TOSTRING(x) TOSTRING_INTERNAL(x)
1153
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001154// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001155DEFINE_string(config, "", configUsage().c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001156DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
1157DEFINE_bool(enableMissingWarning, true, "Print message to stderr (but don't fail) if "
1158 "unable to read a reference image for any tests.");
1159DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1160DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1161#if SK_SUPPORT_GPU
1162DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001163 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1164 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001165#endif
1166DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1167 "when reading/writing files.");
1168DEFINE_string(match, "", "Only run tests whose name includes this substring/these substrings "
1169 "(more than one can be supplied, separated by spaces).");
1170DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1171 "pixel mismatches into this directory.");
1172DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1173 "testIndex %% divisor == remainder.");
1174DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1175DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1176DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1177 "any differences between those and the newly generated ones.");
1178DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1179DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1180DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1181DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
epoger@google.comcaac3db2013-04-04 19:23:11 +00001182DEFINE_bool(simulatePipePlaybackFailure, false, "Simulate a rendering failure in pipe mode only.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001183DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1184DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1185DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1186 "factors to be used for tileGrid playback testing. Default value: 1.0");
1187DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
1188DEFINE_bool2(verbose, v, false, "Print diagnostics (e.g. list each config to be tested).");
1189DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
scroggo@google.com604e0c22013-04-09 21:25:46 +00001190DEFINE_string2(writePicturePath, p, "", "Write .skp files into this directory.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001191
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001192static int findConfig(const char config[]) {
1193 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1194 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001195 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001196 }
1197 }
1198 return -1;
1199}
1200
reed@google.comb2a51622011-10-31 16:30:04 +00001201static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
1202 if (0 == array.count()) {
1203 // no names, so don't skip anything
1204 return false;
1205 }
1206 for (int i = 0; i < array.count(); ++i) {
1207 if (strstr(name, array[i])) {
1208 // found the name, so don't skip
1209 return false;
1210 }
1211 }
1212 return true;
1213}
1214
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001215namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001216#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +00001217SkAutoTUnref<GrContext> gGrContext;
1218/**
bsalomon@google.comc7a24d22012-11-01 18:03:48 +00001219 * Sets the global GrContext, accessible by individual GMs
bsalomon@google.com7361f542012-04-19 19:15:35 +00001220 */
caryclark@google.com13130862012-06-06 12:10:45 +00001221static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +00001222 SkSafeRef(grContext);
1223 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001224}
bsalomon@google.com7361f542012-04-19 19:15:35 +00001225
1226/**
1227 * Gets the global GrContext, can be called by GM tests.
1228 */
caryclark@google.com13130862012-06-06 12:10:45 +00001229GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +00001230GrContext* GetGr() {
1231 return gGrContext.get();
1232}
1233
1234/**
1235 * Sets the global GrContext and then resets it to its previous value at
1236 * destruction.
1237 */
1238class AutoResetGr : SkNoncopyable {
1239public:
1240 AutoResetGr() : fOld(NULL) {}
1241 void set(GrContext* context) {
1242 SkASSERT(NULL == fOld);
1243 fOld = GetGr();
1244 SkSafeRef(fOld);
1245 SetGr(context);
1246 }
1247 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1248private:
1249 GrContext* fOld;
1250};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001251#else
epoger@google.com80724df2013-03-21 13:49:54 +00001252GrContext* GetGr();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001253GrContext* GetGr() { return NULL; }
1254#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001255}
1256
reed@google.comfb2cd422013-01-04 14:43:03 +00001257template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1258 int index = array->find(value);
1259 if (index < 0) {
1260 *array->append() = value;
1261 }
1262}
1263
epoger@google.com80724df2013-03-21 13:49:54 +00001264/**
1265 * Run this test in a number of different configs (8888, 565, PDF,
1266 * etc.), confirming that the resulting bitmaps match expectations
1267 * (which may be different for each config).
1268 *
1269 * Returns all errors encountered while doing so.
1270 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001271ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1272 GrContextFactory *grFactory);
1273ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1274 GrContextFactory *grFactory) {
1275 ErrorCombination errorsForAllConfigs;
epoger@google.com80724df2013-03-21 13:49:54 +00001276 uint32_t gmFlags = gm->getFlags();
1277
epoger@google.com80724df2013-03-21 13:49:54 +00001278 for (int i = 0; i < configs.count(); i++) {
1279 ConfigData config = gRec[configs[i]];
1280
1281 // Skip any tests that we don't even need to try.
1282 if ((kPDF_Backend == config.fBackend) &&
1283 (!FLAGS_pdf|| (gmFlags & GM::kSkipPDF_Flag))) {
1284 continue;
1285 }
1286 if ((gmFlags & GM::kSkip565_Flag) &&
1287 (kRaster_Backend == config.fBackend) &&
1288 (SkBitmap::kRGB_565_Config == config.fConfig)) {
1289 continue;
1290 }
1291 if ((gmFlags & GM::kSkipGPU_Flag) &&
1292 kGPU_Backend == config.fBackend) {
1293 continue;
1294 }
1295
1296 // Now we know that we want to run this test and record its
1297 // success or failure.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001298 ErrorCombination errorsForThisConfig;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001299 GrSurface* gpuTarget = NULL;
epoger@google.com80724df2013-03-21 13:49:54 +00001300#if SK_SUPPORT_GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001301 SkAutoTUnref<GrSurface> auGpuTarget;
epoger@google.com80724df2013-03-21 13:49:54 +00001302 AutoResetGr autogr;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001303 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
epoger@google.com80724df2013-03-21 13:49:54 +00001304 GrContext* gr = grFactory->get(config.fGLContextType);
1305 bool grSuccess = false;
1306 if (gr) {
1307 // create a render target to back the device
1308 GrTextureDesc desc;
1309 desc.fConfig = kSkia8888_GrPixelConfig;
1310 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1311 desc.fWidth = gm->getISize().width();
1312 desc.fHeight = gm->getISize().height();
1313 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001314 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1315 if (NULL != auGpuTarget) {
1316 gpuTarget = auGpuTarget;
1317 grSuccess = true;
epoger@google.com80724df2013-03-21 13:49:54 +00001318 autogr.set(gr);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001319 // Set the user specified cache limits if non-default.
1320 size_t bytes;
1321 int count;
1322 gr->getTextureCacheLimits(&count, &bytes);
1323 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1324 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1325 }
1326 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1327 count = gGpuCacheSizeCount;
1328 }
1329 gr->setTextureCacheLimits(count, bytes);
epoger@google.com80724df2013-03-21 13:49:54 +00001330 }
epoger@google.com80724df2013-03-21 13:49:54 +00001331 }
1332 if (!grSuccess) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001333 errorsForThisConfig.add(kNoGpuContext_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001334 }
1335 }
1336#endif
1337
1338 SkBitmap comparisonBitmap;
1339
1340 const char* writePath;
1341 if (FLAGS_writePath.count() == 1) {
1342 writePath = FLAGS_writePath[0];
1343 } else {
1344 writePath = NULL;
1345 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001346 if (errorsForThisConfig.isEmpty()) {
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001347 errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
1348 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001349 }
1350
epoger@google.com6f6568b2013-03-22 17:29:46 +00001351 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1352 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1353 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001354 gpuTarget));
epoger@google.com80724df2013-03-21 13:49:54 +00001355 }
1356
epoger@google.com6f6568b2013-03-22 17:29:46 +00001357 errorsForAllConfigs.add(errorsForThisConfig);
epoger@google.com80724df2013-03-21 13:49:54 +00001358 }
1359 return errorsForAllConfigs;
1360}
1361
1362/**
1363 * Run this test in a number of different drawing modes (pipe,
1364 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1365 * *exactly* match comparisonBitmap.
1366 *
1367 * Returns all errors encountered while doing so.
1368 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001369ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1370 const SkBitmap &comparisonBitmap,
1371 const SkTDArray<SkScalar> &tileGridReplayScales);
1372ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1373 const SkBitmap &comparisonBitmap,
1374 const SkTDArray<SkScalar> &tileGridReplayScales) {
1375 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001376 uint32_t gmFlags = gm->getFlags();
1377
epoger@google.com310478e2013-04-03 18:00:39 +00001378 // TODO(epoger): We should start recording any per-GM skipped
1379 // modes (i.e. those we skipped due to gmFlags) with a new
1380 // ErrorType, perhaps named kIntentionallySkipped_ErrorType.
epoger@google.com80724df2013-03-21 13:49:54 +00001381 if (!(gmFlags & GM::kSkipPicture_Flag)) {
1382
epoger@google.com6f6568b2013-03-22 17:29:46 +00001383 ErrorCombination pictErrors;
epoger@google.com80724df2013-03-21 13:49:54 +00001384
1385 //SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
1386 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1387 SkAutoUnref aur(pict);
1388
1389 if (FLAGS_replay) {
1390 SkBitmap bitmap;
1391 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001392 pictErrors.add(gmmain.compare_test_results_to_reference_bitmap(
1393 gm, compareConfig, "-replay", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001394 }
1395
epoger@google.com6f6568b2013-03-22 17:29:46 +00001396 if ((pictErrors.isEmpty()) && FLAGS_serialize) {
epoger@google.com80724df2013-03-21 13:49:54 +00001397 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1398 SkAutoUnref aurr(repict);
1399
1400 SkBitmap bitmap;
1401 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001402 pictErrors.add(gmmain.compare_test_results_to_reference_bitmap(
1403 gm, compareConfig, "-serialize", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001404 }
1405
1406 if (FLAGS_writePicturePath.count() == 1) {
1407 const char* pictureSuffix = "skp";
1408 SkString path = make_filename(FLAGS_writePicturePath[0], "",
1409 gm->shortName(), pictureSuffix);
1410 SkFILEWStream stream(path.c_str());
1411 pict->serialize(&stream);
1412 }
1413
epoger@google.com6f6568b2013-03-22 17:29:46 +00001414 errorsForAllModes.add(pictErrors);
epoger@google.com80724df2013-03-21 13:49:54 +00001415 }
1416
epoger@google.com80724df2013-03-21 13:49:54 +00001417 if (!(gmFlags & GM::kSkipPicture_Flag) && FLAGS_rtree) {
1418 SkPicture* pict = gmmain.generate_new_picture(
1419 gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
1420 SkAutoUnref aur(pict);
1421 SkBitmap bitmap;
1422 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001423 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1424 gm, compareConfig, "-rtree", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001425 }
1426
1427 if (!(gmFlags & GM::kSkipPicture_Flag) && FLAGS_tileGrid) {
1428 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1429 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1430 if ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1) {
1431 continue;
1432 }
1433 // We record with the reciprocal scale to obtain a replay
1434 // result that can be validated against comparisonBitmap.
1435 SkScalar recordScale = SkScalarInvert(replayScale);
1436 SkPicture* pict = gmmain.generate_new_picture(
1437 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag, recordScale);
1438 SkAutoUnref aur(pict);
1439 SkBitmap bitmap;
robertphillips@google.com5a7d0292013-04-02 15:18:41 +00001440 // We cannot yet pass 'true' to generate_image_from_picture to
1441 // perform actual tiled rendering (see Issue 1198 -
1442 // https://code.google.com/p/skia/issues/detail?id=1198)
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +00001443 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +00001444 replayScale /*, true */);
epoger@google.com80724df2013-03-21 13:49:54 +00001445 SkString suffix("-tilegrid");
1446 if (SK_Scalar1 != replayScale) {
1447 suffix += "-scale-";
1448 suffix.appendScalar(replayScale);
1449 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001450 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1451 gm, compareConfig, suffix.c_str(), bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001452 }
1453 }
1454
1455 // run the pipe centric GM steps
1456 if (!(gmFlags & GM::kSkipPipe_Flag)) {
1457
epoger@google.com6f6568b2013-03-22 17:29:46 +00001458 ErrorCombination pipeErrors;
epoger@google.com80724df2013-03-21 13:49:54 +00001459
1460 if (FLAGS_pipe) {
epoger@google.comcaac3db2013-04-04 19:23:11 +00001461 pipeErrors.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap,
1462 FLAGS_simulatePipePlaybackFailure));
epoger@google.com80724df2013-03-21 13:49:54 +00001463 }
1464
epoger@google.com6f6568b2013-03-22 17:29:46 +00001465 if ((pipeErrors.isEmpty()) &&
epoger@google.com80724df2013-03-21 13:49:54 +00001466 FLAGS_tiledPipe && !(gmFlags & GM::kSkipTiled_Flag)) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001467 pipeErrors.add(gmmain.test_tiled_pipe_playback(gm, compareConfig, comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001468 }
1469
epoger@google.com6f6568b2013-03-22 17:29:46 +00001470 errorsForAllModes.add(pipeErrors);
epoger@google.com80724df2013-03-21 13:49:54 +00001471 }
1472 return errorsForAllModes;
1473}
1474
epoger@google.com310478e2013-04-03 18:00:39 +00001475/**
1476 * Return a list of all entries in an array of strings as a single string
1477 * of this form:
1478 * "item1", "item2", "item3"
1479 */
1480SkString list_all(const SkTArray<SkString> &stringArray);
1481SkString list_all(const SkTArray<SkString> &stringArray) {
1482 SkString total;
1483 for (int i = 0; i < stringArray.count(); i++) {
1484 if (i > 0) {
1485 total.append(", ");
1486 }
1487 total.append("\"");
1488 total.append(stringArray[i]);
1489 total.append("\"");
1490 }
1491 return total;
1492}
1493
1494/**
1495 * Return a list of configuration names, as a single string of this form:
1496 * "item1", "item2", "item3"
1497 *
1498 * @param configs configurations, as a list of indices into gRec
1499 */
1500SkString list_all_config_names(const SkTDArray<size_t> &configs);
1501SkString list_all_config_names(const SkTDArray<size_t> &configs) {
1502 SkString total;
1503 for (int i = 0; i < configs.count(); i++) {
1504 if (i > 0) {
1505 total.append(", ");
1506 }
1507 total.append("\"");
1508 total.append(gRec[configs[i]].fName);
1509 total.append("\"");
1510 }
1511 return total;
1512}
1513
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001514int tool_main(int argc, char** argv);
1515int tool_main(int argc, char** argv) {
1516
1517#if SK_ENABLE_INST_COUNT
1518 gPrintInstCount = true;
1519#endif
1520
1521 SkGraphics::Init();
1522 // we don't need to see this during a run
1523 gSkSuppressFontCachePurgeSpew = true;
1524
1525 setSystemPreferences();
1526 GMMain gmmain;
1527
1528 SkTDArray<size_t> configs;
1529 SkTDArray<size_t> excludeConfigs;
1530 bool userConfig = false;
1531
1532 SkString usage;
1533 usage.printf("Run the golden master tests.\n");
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00001534 SkCommandLineFlags::SetUsage(usage.c_str());
1535 SkCommandLineFlags::Parse(argc, argv);
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001536
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001537 gmmain.fUseFileHierarchy = FLAGS_hierarchy;
1538 if (FLAGS_mismatchPath.count() == 1) {
1539 gmmain.fMismatchPath = FLAGS_mismatchPath[0];
1540 }
1541
1542 for (int i = 0; i < FLAGS_config.count(); i++) {
1543 int index = findConfig(FLAGS_config[i]);
1544 if (index >= 0) {
1545 appendUnique<size_t>(&configs, index);
1546 userConfig = true;
scroggo@google.com0b735632013-03-19 17:38:50 +00001547 } else {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001548 gm_fprintf(stderr, "unrecognized config %s\n", FLAGS_config[i]);
scroggo@google.com7d519302013-03-19 17:28:10 +00001549 return -1;
1550 }
1551 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001552
1553 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1554 int index = findConfig(FLAGS_excludeConfig[i]);
1555 if (index >= 0) {
1556 *excludeConfigs.append() = index;
1557 } else {
1558 gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1559 return -1;
1560 }
1561 }
1562
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001563 int moduloRemainder = -1;
1564 int moduloDivisor = -1;
1565
1566 if (FLAGS_modulo.count() == 2) {
1567 moduloRemainder = atoi(FLAGS_modulo[0]);
1568 moduloDivisor = atoi(FLAGS_modulo[1]);
1569 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
1570 gm_fprintf(stderr, "invalid modulo values.");
1571 return -1;
1572 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001573 }
reed@google.com873cb1e2010-12-23 15:00:45 +00001574
epoger@google.com6f6568b2013-03-22 17:29:46 +00001575#if SK_SUPPORT_GPU
1576 if (FLAGS_gpuCacheSize.count() > 0) {
1577 if (FLAGS_gpuCacheSize.count() != 2) {
1578 gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
1579 return -1;
1580 }
1581 gGpuCacheSizeBytes = atoi(FLAGS_gpuCacheSize[0]);
1582 gGpuCacheSizeCount = atoi(FLAGS_gpuCacheSize[1]);
1583 } else {
1584 gGpuCacheSizeBytes = DEFAULT_CACHE_VALUE;
1585 gGpuCacheSizeCount = DEFAULT_CACHE_VALUE;
1586 }
1587#endif
1588
1589 SkTDArray<SkScalar> tileGridReplayScales;
1590 *tileGridReplayScales.append() = SK_Scalar1; // By default only test at scale 1.0
1591 if (FLAGS_tileGridReplayScales.count() > 0) {
1592 tileGridReplayScales.reset();
1593 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
1594 double val = atof(FLAGS_tileGridReplayScales[i]);
1595 if (0 < val) {
1596 *tileGridReplayScales.append() = SkDoubleToScalar(val);
1597 }
1598 }
1599 if (0 == tileGridReplayScales.count()) {
1600 // Should have at least one scale
1601 gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
1602 return -1;
1603 }
1604 }
1605
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001606 if (!userConfig) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001607 // if no config is specified by user, add the defaults
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001608 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001609 if (gRec[i].fRunByDefault) {
1610 *configs.append() = i;
1611 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001612 }
1613 }
reed@google.comfb2cd422013-01-04 14:43:03 +00001614 // now remove any explicitly excluded configs
1615 for (int i = 0; i < excludeConfigs.count(); ++i) {
1616 int index = configs.find(excludeConfigs[i]);
1617 if (index >= 0) {
1618 configs.remove(index);
1619 // now assert that there was only one copy in configs[]
1620 SkASSERT(configs.find(excludeConfigs[i]) < 0);
1621 }
1622 }
1623
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001624#if SK_SUPPORT_GPU
1625 GrContextFactory* grFactory = new GrContextFactory;
1626 for (int i = 0; i < configs.count(); ++i) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001627 size_t index = configs[i];
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001628 if (kGPU_Backend == gRec[index].fBackend) {
1629 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1630 if (NULL == ctx) {
1631 SkDebugf("GrContext could not be created for config %s. Config will be skipped.",
1632 gRec[index].fName);
1633 configs.remove(i);
1634 --i;
1635 }
1636 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
1637 SkDebugf("Sample count (%d) of config %s is not supported. Config will be skipped.",
1638 gRec[index].fSampleCnt, gRec[index].fName);
1639 configs.remove(i);
1640 --i;
1641 }
1642 }
1643 }
epoger@google.com80724df2013-03-21 13:49:54 +00001644#else
1645 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001646#endif
1647
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001648 if (FLAGS_verbose) {
reed@google.comfb2cd422013-01-04 14:43:03 +00001649 SkString str;
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001650 str.printf("%d configs:", configs.count());
reed@google.comfb2cd422013-01-04 14:43:03 +00001651 for (int i = 0; i < configs.count(); ++i) {
1652 str.appendf(" %s", gRec[configs[i]].fName);
1653 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001654 gm_fprintf(stderr, "%s\n", str.c_str());
reed@google.comfb2cd422013-01-04 14:43:03 +00001655 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001656
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001657 if (FLAGS_resourcePath.count() == 1) {
1658 GM::SetResourcePath(FLAGS_resourcePath[0]);
1659 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001660
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001661 if (FLAGS_readPath.count() == 1) {
1662 const char* readPath = FLAGS_readPath[0];
epoger@google.com37269602013-01-19 04:21:27 +00001663 if (!sk_exists(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001664 gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001665 return -1;
1666 }
1667 if (sk_isdir(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001668 gm_fprintf(stdout, "reading from %s\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001669 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1670 IndividualImageExpectationsSource,
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001671 (readPath, FLAGS_enableMissingWarning)));
epoger@google.com37269602013-01-19 04:21:27 +00001672 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001673 gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
epoger@google.comd271d242013-02-13 18:14:48 +00001674 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1675 JsonExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001676 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00001677 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001678 if (FLAGS_writePath.count() == 1) {
1679 gm_fprintf(stderr, "writing to %s\n", FLAGS_writePath[0]);
reed@android.com00f883e2010-12-14 17:46:14 +00001680 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001681 if (FLAGS_writePicturePath.count() == 1) {
1682 gm_fprintf(stderr, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001683 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001684 if (FLAGS_resourcePath.count() == 1) {
1685 gm_fprintf(stderr, "reading resources from %s\n", FLAGS_resourcePath[0]);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001686 }
1687
epoger@google.com82cb65b2012-10-29 18:59:17 +00001688 if (moduloDivisor <= 0) {
1689 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001690 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001691 if (moduloRemainder < 0 || moduloRemainder >= moduloDivisor) {
1692 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001693 }
1694
epoger@google.com310478e2013-04-03 18:00:39 +00001695 int gmsRun = 0;
reed@google.comae7b8f32012-10-18 21:30:57 +00001696 int gmIndex = -1;
1697 SkString moduloStr;
1698
epoger@google.come8ebeb12012-10-29 16:42:11 +00001699 // If we will be writing out files, prepare subdirectories.
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001700 if (FLAGS_writePath.count() == 1) {
1701 if (!sk_mkdir(FLAGS_writePath[0])) {
epoger@google.come8ebeb12012-10-29 16:42:11 +00001702 return -1;
1703 }
1704 if (gmmain.fUseFileHierarchy) {
1705 for (int i = 0; i < configs.count(); i++) {
1706 ConfigData config = gRec[configs[i]];
1707 SkString subdir;
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001708 subdir.appendf("%s%c%s", FLAGS_writePath[0], SkPATH_SEPARATOR,
epoger@google.come8ebeb12012-10-29 16:42:11 +00001709 config.fName);
1710 if (!sk_mkdir(subdir.c_str())) {
1711 return -1;
1712 }
1713 }
1714 }
1715 }
1716
bsalomon@google.com7361f542012-04-19 19:15:35 +00001717 Iter iter;
1718 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00001719 while ((gm = iter.next()) != NULL) {
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +00001720
reed@google.comae7b8f32012-10-18 21:30:57 +00001721 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00001722 if (moduloRemainder >= 0) {
1723 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00001724 continue;
1725 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001726 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00001727 }
1728
reed@google.comece2b022011-07-25 14:28:57 +00001729 const char* shortName = gm->shortName();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001730 if (skip_name(FLAGS_match, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00001731 SkDELETE(gm);
1732 continue;
1733 }
1734
epoger@google.com310478e2013-04-03 18:00:39 +00001735 gmsRun++;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001736 SkISize size = gm->getISize();
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001737 gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
1738 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00001739
epoger@google.com310478e2013-04-03 18:00:39 +00001740 run_multiple_configs(gmmain, gm, configs, grFactory);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001741
1742 SkBitmap comparisonBitmap;
1743 const ConfigData compareConfig =
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001744 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
epoger@google.com310478e2013-04-03 18:00:39 +00001745 gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001746
epoger@google.com80724df2013-03-21 13:49:54 +00001747 // TODO(epoger): only run this if gmmain.generate_image() succeeded?
1748 // Otherwise, what are we comparing against?
epoger@google.com310478e2013-04-03 18:00:39 +00001749 run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap, tileGridReplayScales);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001750
reed@android.com00dae862009-06-10 15:38:48 +00001751 SkDELETE(gm);
1752 }
epoger@google.com310478e2013-04-03 18:00:39 +00001753
1754 SkTArray<SkString> modes;
1755 gmmain.GetRenderModesEncountered(modes);
1756
1757 // Output summary to stdout.
1758 gm_fprintf(stdout, "Ran %d GMs\n", gmsRun);
1759 gm_fprintf(stdout, "... over %2d configs [%s]\n", configs.count(),
1760 list_all_config_names(configs).c_str());
1761 gm_fprintf(stdout, "... and %2d modes [%s]\n", modes.count(), list_all(modes).c_str());
1762 gm_fprintf(stdout, "... so there should be a total of %d tests.\n",
1763 gmsRun * (configs.count() + modes.count()));
1764
1765 // TODO(epoger): Ultimately, we should signal an error if the
1766 // expected total number of tests (displayed above) does not match
1767 // gmmain.fTestsRun. But for now, there are cases where those
1768 // numbers won't match: specifically, if some configs/modes are
1769 // skipped on a per-GM basis (due to gm->getFlags() for a specific
1770 // GM). Later on, we should record tests like that using some new
1771 // ErrorType, like kIntentionallySkipped_ErrorType. Then we could
1772 // signal an error if the totals didn't match up.
epoger@google.com57f7abc2012-11-13 03:41:55 +00001773 gmmain.ListErrors();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001774
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001775 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.comee8a8e32012-12-18 19:13:49 +00001776 Json::Value actualResults;
1777 actualResults[kJsonKey_ActualResults_Failed] =
1778 gmmain.fJsonActualResults_Failed;
1779 actualResults[kJsonKey_ActualResults_FailureIgnored] =
1780 gmmain.fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001781 actualResults[kJsonKey_ActualResults_NoComparison] =
1782 gmmain.fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001783 actualResults[kJsonKey_ActualResults_Succeeded] =
1784 gmmain.fJsonActualResults_Succeeded;
1785 Json::Value root;
1786 root[kJsonKey_ActualResults] = actualResults;
1787 root[kJsonKey_ExpectedResults] = gmmain.fJsonExpectedResults;
1788 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001789 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00001790 stream.write(jsonStdString.c_str(), jsonStdString.length());
1791 }
1792
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001793#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001794
robertphillips@google.com59552022012-08-31 13:07:37 +00001795#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001796 for (int i = 0; i < configs.count(); i++) {
1797 ConfigData config = gRec[configs[i]];
1798
1799 if (kGPU_Backend == config.fBackend) {
1800 GrContext* gr = grFactory->get(config.fGLContextType);
1801
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001802 gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001803 gr->printCacheStats();
1804 }
1805 }
1806#endif
1807
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001808 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001809#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001810 SkGraphics::Term();
1811
epoger@google.com310478e2013-04-03 18:00:39 +00001812 return (0 == gmmain.NumSignificantErrors()) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001813}
caryclark@google.com5987f582012-10-02 18:33:14 +00001814
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001815void GMMain::installFilter(SkCanvas* canvas) {
1816 if (FLAGS_forceBWtext) {
1817 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
1818 }
1819}
1820
borenet@google.com7158e6a2012-11-01 17:43:44 +00001821#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00001822int main(int argc, char * const argv[]) {
1823 return tool_main(argc, (char**) argv);
1824}
1825#endif