blob: 01eaaebffd8a6a9ff4cb1f87ae6adfcca5745ebe [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"
reed@android.comb9b9a182009-07-08 02:54:47 +000021#include "SkColorPriv.h"
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000022#include "SkCommandLineFlags.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000023#include "SkData.h"
junov@google.com4370aed2012-01-18 16:21:08 +000024#include "SkDeferredCanvas.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000025#include "SkDevice.h"
epoger@google.comde961632012-10-26 18:56:36 +000026#include "SkDrawFilter.h"
commit-bot@chromium.orgc41295d2013-06-18 20:06:36 +000027#include "SkForceLinking.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
commit-bot@chromium.orgc41295d2013-06-18 20:06:36 +000041__SK_FORCE_IMAGE_DECODER_LINKING;
42
bsalomon@google.com50c79d82013-01-08 20:31:53 +000043#ifdef SK_BUILD_FOR_WIN
44 // json includes xlocale which generates warning 4530 because we're compiling without
epoger@google.com37269602013-01-19 04:21:27 +000045 // exceptions; see https://code.google.com/p/skia/issues/detail?id=1067
bsalomon@google.com50c79d82013-01-08 20:31:53 +000046 #pragma warning(push)
47 #pragma warning(disable : 4530)
48#endif
epoger@google.comee8a8e32012-12-18 19:13:49 +000049#include "json/value.h"
bsalomon@google.com50c79d82013-01-08 20:31:53 +000050#ifdef SK_BUILD_FOR_WIN
51 #pragma warning(pop)
52#endif
epoger@google.comee8a8e32012-12-18 19:13:49 +000053
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000054#if SK_SUPPORT_GPU
55#include "GrContextFactory.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000056#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000057typedef GrContextFactory::GLContextType GLContextType;
epoger@google.com6f6568b2013-03-22 17:29:46 +000058#define DEFAULT_CACHE_VALUE -1
59static int gGpuCacheSizeBytes;
60static int gGpuCacheSizeCount;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000061#else
epoger@google.com80724df2013-03-21 13:49:54 +000062class GrContextFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000063class GrContext;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +000064class GrSurface;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000065typedef int GLContextType;
66#endif
67
epoger@google.com76c913d2013-04-26 15:06:44 +000068#define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message")
69
reed@google.com8923c6c2011-11-08 14:59:38 +000070extern bool gSkSuppressFontCachePurgeSpew;
71
reed@google.com07700442010-12-20 19:46:07 +000072#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000073 #include "SkPDFDevice.h"
74 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000075#endif
reed@android.com00dae862009-06-10 15:38:48 +000076
epoger@google.come3cc2eb2012-01-18 20:11:13 +000077// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
78// stop writing out XPS-format image baselines in gm.
79#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000080#ifdef SK_SUPPORT_XPS
81 #include "SkXPSDevice.h"
82#endif
83
reed@google.com46cce912011-06-29 12:54:46 +000084#ifdef SK_BUILD_FOR_MAC
85 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000086 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000087#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000088 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000089#endif
90
reed@android.com00dae862009-06-10 15:38:48 +000091using namespace skiagm;
92
reed@android.com00dae862009-06-10 15:38:48 +000093class Iter {
94public:
95 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000096 this->reset();
97 }
98
99 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +0000100 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +0000101 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000102
reed@android.comdd0ac282009-06-20 02:38:16 +0000103 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +0000104 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000105 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +0000106 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +0000107 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +0000108 }
109 return NULL;
110 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000111
reed@android.com00dae862009-06-10 15:38:48 +0000112 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +0000113 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +0000114 int count = 0;
115 while (reg) {
116 count += 1;
117 reg = reg->next();
118 }
119 return count;
120 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000121
reed@android.com00dae862009-06-10 15:38:48 +0000122private:
123 const GMRegistry* fReg;
124};
125
epoger@google.comce057fe2013-05-14 15:17:46 +0000126// TODO(epoger): Right now, various places in this code assume that all the
127// image files read/written by GM use this file extension.
128// Search for references to this constant to find these assumptions.
129const static char kPNG_FileExtension[] = "png";
130
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000131enum Backend {
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000132 kRaster_Backend,
133 kGPU_Backend,
134 kPDF_Backend,
135 kXPS_Backend,
136};
137
138enum BbhType {
139 kNone_BbhType,
140 kRTree_BbhType,
141 kTileGrid_BbhType,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000142};
143
bsalomon@google.com7361f542012-04-19 19:15:35 +0000144enum ConfigFlags {
145 kNone_ConfigFlag = 0x0,
146 /* Write GM images if a write path is provided. */
147 kWrite_ConfigFlag = 0x1,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000148 /* Read reference GM images if a read path is provided. */
bsalomon@google.com7361f542012-04-19 19:15:35 +0000149 kRead_ConfigFlag = 0x2,
150 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
151};
152
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000153struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000154 SkBitmap::Config fConfig;
155 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000156 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000157 int fSampleCnt; // GPU backend only
158 ConfigFlags fFlags;
159 const char* fName;
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000160 bool fRunByDefault;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000161};
162
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000163class BWTextDrawFilter : public SkDrawFilter {
164public:
reed@google.com971aca72012-11-26 20:26:54 +0000165 virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000166};
reed@google.com971aca72012-11-26 20:26:54 +0000167bool BWTextDrawFilter::filter(SkPaint* p, Type t) {
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000168 if (kText_Type == t) {
169 p->setAntiAlias(false);
170 }
reed@google.com971aca72012-11-26 20:26:54 +0000171 return true;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000172}
173
scroggo@google.com565254b2012-06-28 15:41:32 +0000174struct PipeFlagComboData {
175 const char* name;
176 uint32_t flags;
177};
178
179static PipeFlagComboData gPipeWritingFlagCombos[] = {
180 { "", 0 },
181 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000182 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000183 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000184};
185
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000186static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect);
187
epoger@google.com5079d2c2013-04-12 14:11:21 +0000188const static ErrorCombination kDefaultIgnorableErrorTypes = ErrorCombination()
189 .plus(kMissingExpectations_ErrorType)
190 .plus(kIntentionallySkipped_ErrorType);
191
epoger@google.comde961632012-10-26 18:56:36 +0000192class GMMain {
193public:
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000194 GMMain() : fUseFileHierarchy(false), fWriteChecksumBasedFilenames(false),
195 fIgnorableErrorTypes(kDefaultIgnorableErrorTypes),
epoger@google.com5f995452013-06-21 18:16:47 +0000196 fMismatchPath(NULL), fMissingExpectationsPath(NULL), fTestsRun(0),
197 fRenderModesEncountered(1) {}
epoger@google.come8ebeb12012-10-29 16:42:11 +0000198
epoger@google.com659c8c02013-05-21 15:45:45 +0000199 /**
200 * Assemble shortNamePlusConfig from (surprise!) shortName and configName.
201 *
202 * The method for doing so depends on whether we are using hierarchical naming.
203 * For example, shortName "selftest1" and configName "8888" could be assembled into
204 * either "selftest1_8888" or "8888/selftest1".
205 */
206 SkString make_shortname_plus_config(const char *shortName, const char *configName) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000207 SkString name;
epoger@google.com57f7abc2012-11-13 03:41:55 +0000208 if (0 == strlen(configName)) {
209 name.append(shortName);
210 } else if (fUseFileHierarchy) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000211 name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
212 } else {
213 name.appendf("%s_%s", shortName, configName);
214 }
epoger@google.comde961632012-10-26 18:56:36 +0000215 return name;
216 }
217
epoger@google.com659c8c02013-05-21 15:45:45 +0000218 /**
219 * Assemble filename, suitable for writing out the results of a particular test.
220 */
221 SkString make_filename(const char *path,
222 const char *shortName,
223 const char *configName,
224 const char *renderModeDescriptor,
225 const char *suffix) {
226 SkString filename = make_shortname_plus_config(shortName, configName);
227 filename.append(renderModeDescriptor);
228 filename.appendUnichar('.');
229 filename.append(suffix);
scroggo@google.comccd7afb2013-05-28 16:45:07 +0000230 return SkOSPath::SkPathJoin(path, filename.c_str());
epoger@google.com659c8c02013-05-21 15:45:45 +0000231 }
232
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000233 /**
234 * Assemble filename suitable for writing out an SkBitmap.
235 */
236 SkString make_bitmap_filename(const char *path,
237 const char *shortName,
238 const char *configName,
239 const char *renderModeDescriptor,
240 const GmResultDigest &bitmapDigest) {
241 if (fWriteChecksumBasedFilenames) {
242 SkString filename;
243 filename.append(bitmapDigest.getHashType());
244 filename.appendUnichar('_');
245 filename.append(shortName);
246 filename.appendUnichar('_');
247 filename.append(bitmapDigest.getDigestValue());
248 filename.appendUnichar('.');
249 filename.append(kPNG_FileExtension);
250 return SkOSPath::SkPathJoin(path, filename.c_str());
251 } else {
252 return make_filename(path, shortName, configName, renderModeDescriptor,
253 kPNG_FileExtension);
254 }
255 }
256
epoger@google.com5f6a0072013-01-31 16:30:55 +0000257 /* since PNG insists on unpremultiplying our alpha, we take no
258 precision chances and force all pixels to be 100% opaque,
259 otherwise on compare we may not get a perfect match.
260 */
261 static void force_all_opaque(const SkBitmap& bitmap) {
262 SkBitmap::Config config = bitmap.config();
263 switch (config) {
264 case SkBitmap::kARGB_8888_Config:
265 force_all_opaque_8888(bitmap);
266 break;
267 case SkBitmap::kRGB_565_Config:
268 // nothing to do here; 565 bitmaps are inherently opaque
269 break;
270 default:
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000271 gm_fprintf(stderr, "unsupported bitmap config %d\n", config);
272 DEBUGFAIL_SEE_STDERR;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000273 }
274 }
275
276 static void force_all_opaque_8888(const SkBitmap& bitmap) {
277 SkAutoLockPixels lock(bitmap);
278 for (int y = 0; y < bitmap.height(); y++) {
279 for (int x = 0; x < bitmap.width(); x++) {
280 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
281 }
282 }
283 }
284
285 static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
286 // TODO(epoger): Now that we have removed force_all_opaque()
287 // from this method, we should be able to get rid of the
288 // transformation to 8888 format also.
289 SkBitmap copy;
290 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
291 return SkImageEncoder::EncodeFile(path.c_str(), copy,
292 SkImageEncoder::kPNG_Type, 100);
293 }
294
epoger@google.com6f6568b2013-03-22 17:29:46 +0000295 /**
epoger@google.com310478e2013-04-03 18:00:39 +0000296 * Add all render modes encountered thus far to the "modes" array.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000297 */
epoger@google.com310478e2013-04-03 18:00:39 +0000298 void GetRenderModesEncountered(SkTArray<SkString> &modes) {
299 SkTDict<int>::Iter iter(this->fRenderModesEncountered);
300 const char* mode;
301 while ((mode = iter.next(NULL)) != NULL) {
302 SkString modeAsString = SkString(mode);
303 // TODO(epoger): It seems a bit silly that all of these modes were
304 // recorded with a leading "-" which we have to remove here
305 // (except for mode "", which means plain old original mode).
306 // But that's how renderModeDescriptor has been passed into
307 // compare_test_results_to_reference_bitmap() historically,
308 // and changing that now may affect other parts of our code.
309 if (modeAsString.startsWith("-")) {
310 modeAsString.remove(0, 1);
311 modes.push_back(modeAsString);
312 }
313 }
314 }
315
316 /**
317 * Records the results of this test in fTestsRun and fFailedTests.
318 *
319 * We even record successes, and errors that we regard as
320 * "ignorable"; we can filter them out later.
321 */
epoger@google.com659c8c02013-05-21 15:45:45 +0000322 void RecordTestResults(const ErrorCombination& errorCombination,
323 const SkString& shortNamePlusConfig,
epoger@google.com310478e2013-04-03 18:00:39 +0000324 const char renderModeDescriptor []) {
325 // Things to do regardless of errorCombination.
326 fTestsRun++;
327 int renderModeCount = 0;
328 this->fRenderModesEncountered.find(renderModeDescriptor, &renderModeCount);
329 renderModeCount++;
330 this->fRenderModesEncountered.set(renderModeDescriptor, renderModeCount);
331
epoger@google.com6f6568b2013-03-22 17:29:46 +0000332 if (errorCombination.isEmpty()) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000333 return;
epoger@google.comeb066362013-03-08 09:39:36 +0000334 }
335
epoger@google.com310478e2013-04-03 18:00:39 +0000336 // Things to do only if there is some error condition.
epoger@google.com659c8c02013-05-21 15:45:45 +0000337 SkString fullName = shortNamePlusConfig;
epoger@google.comcaac3db2013-04-04 19:23:11 +0000338 fullName.append(renderModeDescriptor);
epoger@google.com310478e2013-04-03 18:00:39 +0000339 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
340 ErrorType type = static_cast<ErrorType>(typeInt);
341 if (errorCombination.includes(type)) {
342 fFailedTests[type].push_back(fullName);
epoger@google.comf60494b2013-04-03 17:02:53 +0000343 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000344 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000345 }
346
epoger@google.com310478e2013-04-03 18:00:39 +0000347 /**
348 * Return the number of significant (non-ignorable) errors we have
349 * encountered so far.
350 */
351 int NumSignificantErrors() {
352 int significantErrors = 0;
353 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
354 ErrorType type = static_cast<ErrorType>(typeInt);
epoger@google.com5079d2c2013-04-12 14:11:21 +0000355 if (!fIgnorableErrorTypes.includes(type)) {
epoger@google.com310478e2013-04-03 18:00:39 +0000356 significantErrors += fFailedTests[type].count();
357 }
358 }
359 return significantErrors;
360 }
361
362 /**
epoger@google.com51dbabe2013-04-10 15:24:53 +0000363 * Display the summary of results with this ErrorType.
364 *
365 * @param type which ErrorType
366 * @param verbose whether to be all verbose about it
epoger@google.com310478e2013-04-03 18:00:39 +0000367 */
epoger@google.com51dbabe2013-04-10 15:24:53 +0000368 void DisplayResultTypeSummary(ErrorType type, bool verbose) {
epoger@google.com5079d2c2013-04-12 14:11:21 +0000369 bool isIgnorableType = fIgnorableErrorTypes.includes(type);
epoger@google.com51dbabe2013-04-10 15:24:53 +0000370
371 SkString line;
372 if (isIgnorableType) {
373 line.append("[ ] ");
374 } else {
375 line.append("[*] ");
376 }
377
378 SkTArray<SkString> *failedTestsOfThisType = &fFailedTests[type];
379 int count = failedTestsOfThisType->count();
380 line.appendf("%d %s", count, getErrorTypeName(type));
381 if (!isIgnorableType || verbose) {
382 line.append(":");
383 for (int i = 0; i < count; ++i) {
384 line.append(" ");
385 line.append((*failedTestsOfThisType)[i]);
386 }
387 }
388 gm_fprintf(stdout, "%s\n", line.c_str());
389 }
390
391 /**
392 * List contents of fFailedTests to stdout.
393 *
394 * @param verbose whether to be all verbose about it
395 */
396 void ListErrors(bool verbose) {
epoger@google.com310478e2013-04-03 18:00:39 +0000397 // First, print a single summary line.
398 SkString summary;
399 summary.appendf("Ran %d tests:", fTestsRun);
400 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
401 ErrorType type = static_cast<ErrorType>(typeInt);
402 summary.appendf(" %s=%d", getErrorTypeName(type), fFailedTests[type].count());
403 }
404 gm_fprintf(stdout, "%s\n", summary.c_str());
405
406 // Now, for each failure type, list the tests that failed that way.
407 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
epoger@google.com51dbabe2013-04-10 15:24:53 +0000408 this->DisplayResultTypeSummary(static_cast<ErrorType>(typeInt), verbose);
epoger@google.com310478e2013-04-03 18:00:39 +0000409 }
410 gm_fprintf(stdout, "(results marked with [*] will cause nonzero return value)\n");
411 }
412
bungeman@google.com88682b72013-07-19 13:55:41 +0000413 static bool write_document(const SkString& path, SkStreamAsset* asset) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000414 SkFILEWStream stream(path.c_str());
bungeman@google.com88682b72013-07-19 13:55:41 +0000415 return stream.writeStream(asset, asset->getLength());
epoger@google.com5f6a0072013-01-31 16:30:55 +0000416 }
417
epoger@google.com37269602013-01-19 04:21:27 +0000418 /**
epoger@google.com5f6a0072013-01-31 16:30:55 +0000419 * Prepare an SkBitmap to render a GM into.
420 *
421 * After you've rendered the GM into the SkBitmap, you must call
422 * complete_bitmap()!
423 *
424 * @todo thudson 22 April 2011 - could refactor this to take in
425 * a factory to generate the context, always call readPixels()
426 * (logically a noop for rasters, if wasted time), and thus collapse the
427 * GPU special case and also let this be used for SkPicture testing.
428 */
429 static void setup_bitmap(const ConfigData& gRec, SkISize& size,
430 SkBitmap* bitmap) {
431 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
432 bitmap->allocPixels();
433 bitmap->eraseColor(SK_ColorTRANSPARENT);
434 }
435
436 /**
437 * Any finalization steps we need to perform on the SkBitmap after
438 * we have rendered the GM into it.
epoger@google.com37269602013-01-19 04:21:27 +0000439 *
440 * It's too bad that we are throwing away alpha channel data
441 * we could otherwise be examining, but this had always been happening
442 * before... it was buried within the compare() method at
443 * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
444 *
445 * Apparently we need this, at least for bitmaps that are either:
446 * (a) destined to be written out as PNG files, or
447 * (b) compared against bitmaps read in from PNG files
448 * for the reasons described just above the force_all_opaque() method.
449 *
450 * Neglecting to do this led to the difficult-to-diagnose
451 * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
452 * spurious pixel_error messages as of r7258')
453 *
454 * TODO(epoger): Come up with a better solution that allows us to
455 * compare full pixel data, including alpha channel, while still being
456 * robust in the face of transformations to/from PNG files.
457 * Options include:
458 *
459 * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
460 * will be written to, or compared against, PNG files.
461 * PRO: Preserve/compare alpha channel info for the non-PNG cases
462 * (comparing different renderModes in-memory)
epoger@google.com908f5832013-04-12 02:23:55 +0000463 * CON: The bitmaps (and hash digests) for these non-PNG cases would be
epoger@google.com37269602013-01-19 04:21:27 +0000464 * different than those for the PNG-compared cases, and in the
465 * case of a failed renderMode comparison, how would we write the
466 * image to disk for examination?
467 *
epoger@google.com908f5832013-04-12 02:23:55 +0000468 * 2. Always compute image hash digests from PNG format (either
epoger@google.com37269602013-01-19 04:21:27 +0000469 * directly from the the bytes of a PNG file, or capturing the
470 * bytes we would have written to disk if we were writing the
471 * bitmap out as a PNG).
472 * PRO: I think this would allow us to never force opaque, and to
473 * the extent that alpha channel data can be preserved in a PNG
474 * file, we could observe it.
epoger@google.com908f5832013-04-12 02:23:55 +0000475 * CON: If we read a bitmap from disk, we need to take its hash digest
epoger@google.com37269602013-01-19 04:21:27 +0000476 * from the source PNG (we can't compute it from the bitmap we
477 * read out of the PNG, because we will have already premultiplied
478 * the alpha).
479 * CON: Seems wasteful to convert a bitmap to PNG format just to take
epoger@google.com908f5832013-04-12 02:23:55 +0000480 * its hash digest. (Although we're wasting lots of effort already
epoger@google.com37269602013-01-19 04:21:27 +0000481 * calling force_all_opaque().)
482 *
483 * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
484 * so we can transform images back and forth without fear of off-by-one
485 * errors.
486 * CON: Math is hard.
487 *
488 * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
489 * channel), rather than demanding absolute equality.
epoger@google.com908f5832013-04-12 02:23:55 +0000490 * CON: Can't do this with hash digests.
epoger@google.com37269602013-01-19 04:21:27 +0000491 */
epoger@google.com5f6a0072013-01-31 16:30:55 +0000492 static void complete_bitmap(SkBitmap* bitmap) {
493 force_all_opaque(*bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000494 }
495
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000496 static void installFilter(SkCanvas* canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000497
reed@google.comaef73612012-11-16 13:41:45 +0000498 static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000499 SkAutoCanvasRestore acr(canvas, true);
500
501 if (!isPDF) {
502 canvas->concat(gm->getInitialTransform());
503 }
504 installFilter(canvas);
reed@google.comaef73612012-11-16 13:41:45 +0000505 gm->setCanvasIsDeferred(isDeferred);
epoger@google.comde961632012-10-26 18:56:36 +0000506 gm->draw(canvas);
507 canvas->setDrawFilter(NULL);
508 }
509
epoger@google.com6f6568b2013-03-22 17:29:46 +0000510 static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000511 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000512 SkBitmap* bitmap,
513 bool deferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000514 SkISize size (gm->getISize());
515 setup_bitmap(gRec, size, bitmap);
516
517 SkAutoTUnref<SkCanvas> canvas;
518
519 if (gRec.fBackend == kRaster_Backend) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000520 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*bitmap)));
epoger@google.comde961632012-10-26 18:56:36 +0000521 if (deferred) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000522#if SK_DEFERRED_CANVAS_USES_FACTORIES
523 canvas.reset(SkDeferredCanvas::Create(device));
524#else
525 canvas.reset(SkNEW_ARGS(SkDeferredCanvas, (device)));
526#endif
epoger@google.comde961632012-10-26 18:56:36 +0000527 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000528 canvas.reset(SkNEW_ARGS(SkCanvas, (device)));
epoger@google.comde961632012-10-26 18:56:36 +0000529 }
reed@google.comaef73612012-11-16 13:41:45 +0000530 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000531 canvas->flush();
532 }
533#if SK_SUPPORT_GPU
534 else { // GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000535 SkAutoTUnref<SkDevice> device(SkGpuDevice::Create(gpuTarget));
epoger@google.comde961632012-10-26 18:56:36 +0000536 if (deferred) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000537#if SK_DEFERRED_CANVAS_USES_FACTORIES
538 canvas.reset(SkDeferredCanvas::Create(device));
539#else
540 canvas.reset(SkNEW_ARGS(SkDeferredCanvas, (device)));
541#endif
epoger@google.comde961632012-10-26 18:56:36 +0000542 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000543 canvas.reset(SkNEW_ARGS(SkCanvas, (device)));
epoger@google.comde961632012-10-26 18:56:36 +0000544 }
reed@google.comaef73612012-11-16 13:41:45 +0000545 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000546 // the device is as large as the current rendertarget, so
547 // we explicitly only readback the amount we expect (in
548 // size) overwrite our previous allocation
549 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
550 size.fHeight);
551 canvas->readPixels(bitmap, 0, 0);
552 }
553#endif
epoger@google.com5f6a0072013-01-31 16:30:55 +0000554 complete_bitmap(bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000555 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000556 }
557
558 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
junov@chromium.orgc938c482012-12-19 15:24:38 +0000559 SkPicture* pict, SkBitmap* bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000560 SkScalar scale = SK_Scalar1,
561 bool tile = false) {
epoger@google.comde961632012-10-26 18:56:36 +0000562 SkISize size = gm->getISize();
563 setup_bitmap(gRec, size, bitmap);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000564
565 if (tile) {
566 // Generate the result image by rendering to tiles and accumulating
567 // the results in 'bitmap'
568
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000569 // This 16x16 tiling matches the settings applied to 'pict' in
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000570 // 'generate_new_picture'
571 SkISize tileSize = SkISize::Make(16, 16);
572
573 SkBitmap tileBM;
574 setup_bitmap(gRec, tileSize, &tileBM);
575 SkCanvas tileCanvas(tileBM);
576 installFilter(&tileCanvas);
577
578 SkCanvas bmpCanvas(*bitmap);
579 SkPaint bmpPaint;
580 bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
581
582 for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
583 for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
584 int saveCount = tileCanvas.save();
585 SkMatrix mat(tileCanvas.getTotalMatrix());
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000586 mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000587 SkIntToScalar(-yTile*tileSize.height()));
588 tileCanvas.setMatrix(mat);
589 pict->draw(&tileCanvas);
590 tileCanvas.flush();
591 tileCanvas.restoreToCount(saveCount);
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000592 bmpCanvas.drawBitmap(tileBM,
593 SkIntToScalar(xTile * tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000594 SkIntToScalar(yTile * tileSize.height()),
595 &bmpPaint);
596 }
597 }
598 } else {
599 SkCanvas canvas(*bitmap);
600 installFilter(&canvas);
601 canvas.scale(scale, scale);
602 canvas.drawPicture(*pict);
603 complete_bitmap(bitmap);
604 }
epoger@google.comde961632012-10-26 18:56:36 +0000605 }
606
607 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
608#ifdef SK_SUPPORT_PDF
609 SkMatrix initialTransform = gm->getInitialTransform();
610 SkISize pageSize = gm->getISize();
611 SkPDFDevice* dev = NULL;
612 if (initialTransform.isIdentity()) {
613 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
614 } else {
615 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
616 SkIntToScalar(pageSize.height()));
617 initialTransform.mapRect(&content);
618 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
619 SkIntToScalar(pageSize.height()));
620 SkISize contentSize =
621 SkISize::Make(SkScalarRoundToInt(content.width()),
622 SkScalarRoundToInt(content.height()));
623 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
624 }
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000625 dev->setDCTEncoder(encode_to_dct_stream);
epoger@google.comde961632012-10-26 18:56:36 +0000626 SkAutoUnref aur(dev);
627
628 SkCanvas c(dev);
reed@google.comaef73612012-11-16 13:41:45 +0000629 invokeGM(gm, &c, true, false);
epoger@google.comde961632012-10-26 18:56:36 +0000630
631 SkPDFDocument doc;
632 doc.appendPage(dev);
633 doc.emitPDF(&pdf);
634#endif
635 }
636
637 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
638#ifdef SK_SUPPORT_XPS
639 SkISize size = gm->getISize();
640
641 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
642 SkIntToScalar(size.height()));
643 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
644 static const SkScalar upm = 72 * inchesPerMeter;
645 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
646 static const SkScalar ppm = 200 * inchesPerMeter;
647 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
648
649 SkXPSDevice* dev = new SkXPSDevice();
650 SkAutoUnref aur(dev);
651
652 SkCanvas c(dev);
653 dev->beginPortfolio(&xps);
654 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000655 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000656 dev->endSheet();
657 dev->endPortfolio();
658
659#endif
660 }
661
epoger@google.com6f6568b2013-03-22 17:29:46 +0000662 ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
epoger@google.com659c8c02013-05-21 15:45:45 +0000663 const char renderModeDescriptor [],
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000664 const char *shortName,
epoger@google.come33e1372013-07-08 19:13:33 +0000665 const BitmapAndDigest* bitmapAndDigest,
bungeman@google.com88682b72013-07-19 13:55:41 +0000666 SkStreamAsset* document) {
epoger@google.comde961632012-10-26 18:56:36 +0000667 SkString path;
668 bool success = false;
669 if (gRec.fBackend == kRaster_Backend ||
670 gRec.fBackend == kGPU_Backend ||
671 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
672
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000673 path = make_bitmap_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.come33e1372013-07-08 19:13:33 +0000674 bitmapAndDigest->fDigest);
675 success = write_bitmap(path, bitmapAndDigest->fBitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000676 }
677 if (kPDF_Backend == gRec.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000678 path = make_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000679 "pdf");
bungeman@google.com88682b72013-07-19 13:55:41 +0000680 success = write_document(path, document);
epoger@google.comde961632012-10-26 18:56:36 +0000681 }
682 if (kXPS_Backend == gRec.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000683 path = make_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000684 "xps");
bungeman@google.com88682b72013-07-19 13:55:41 +0000685 success = write_document(path, document);
epoger@google.comde961632012-10-26 18:56:36 +0000686 }
687 if (success) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000688 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000689 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000690 gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000691 ErrorCombination errors(kWritingReferenceImage_ErrorType);
epoger@google.com310478e2013-04-03 18:00:39 +0000692 // TODO(epoger): Don't call RecordTestResults() here...
693 // Instead, we should make sure to call RecordTestResults
694 // exactly ONCE per test. (Otherwise, gmmain.fTestsRun
695 // will be incremented twice for this test: once in
696 // compare_test_results_to_stored_expectations() before
697 // that method calls this one, and again here.)
698 //
699 // When we make that change, we should probably add a
700 // WritingReferenceImage test to the gm self-tests.)
epoger@google.com659c8c02013-05-21 15:45:45 +0000701 RecordTestResults(errors, make_shortname_plus_config(shortName, gRec.fName),
702 renderModeDescriptor);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000703 return errors;
epoger@google.comde961632012-10-26 18:56:36 +0000704 }
705 }
706
epoger@google.com37269602013-01-19 04:21:27 +0000707 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000708 * Log more detail about the mistmatch between expectedBitmap and
709 * actualBitmap.
710 */
711 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
712 const char *testName) {
713 const int expectedWidth = expectedBitmap.width();
714 const int expectedHeight = expectedBitmap.height();
715 const int width = actualBitmap.width();
716 const int height = actualBitmap.height();
717 if ((expectedWidth != width) || (expectedHeight != height)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000718 gm_fprintf(stderr, "---- %s: dimension mismatch --"
719 " expected [%d %d], actual [%d %d]\n",
720 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000721 return;
722 }
723
724 if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
725 (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000726 gm_fprintf(stderr, "---- %s: not computing max per-channel"
727 " pixel mismatch because non-8888\n", testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000728 return;
729 }
730
731 SkAutoLockPixels alp0(expectedBitmap);
732 SkAutoLockPixels alp1(actualBitmap);
733 int errR = 0;
734 int errG = 0;
735 int errB = 0;
736 int errA = 0;
737 int differingPixels = 0;
738
739 for (int y = 0; y < height; ++y) {
740 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
741 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
742 for (int x = 0; x < width; ++x) {
743 SkPMColor expectedPixel = *expectedPixelPtr++;
744 SkPMColor actualPixel = *actualPixelPtr++;
745 if (expectedPixel != actualPixel) {
746 differingPixels++;
747 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
748 (int)SkGetPackedR32(actualPixel)));
749 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
750 (int)SkGetPackedG32(actualPixel)));
751 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
752 (int)SkGetPackedB32(actualPixel)));
753 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
754 (int)SkGetPackedA32(actualPixel)));
755 }
756 }
757 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000758 gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
759 " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
760 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000761 }
762
763 /**
epoger@google.com908f5832013-04-12 02:23:55 +0000764 * Compares actual hash digest to expectations, returning the set of errors
epoger@google.com6f6568b2013-03-22 17:29:46 +0000765 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000766 *
767 * If fMismatchPath has been set, and there are pixel diffs, then the
768 * actual bitmap will be written out to a file within fMismatchPath.
epoger@google.com5f995452013-06-21 18:16:47 +0000769 * And similarly for fMissingExpectationsPath...
epoger@google.com37269602013-01-19 04:21:27 +0000770 *
771 * @param expectations what expectations to compare actualBitmap against
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000772 * @param actualBitmapAndDigest the SkBitmap we actually generated, and its GmResultDigest
epoger@google.com659c8c02013-05-21 15:45:45 +0000773 * @param shortName name of test, e.g. "selftest1"
774 * @param configName name of config, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000775 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
776 * @param addToJsonSummary whether to add these results (both actual and
epoger@google.comcaac3db2013-04-04 19:23:11 +0000777 * expected) to the JSON summary. Regardless of this setting, if
778 * we find an image mismatch in this test, we will write these
779 * results to the JSON summary. (This is so that we will always
780 * report errors across rendering modes, such as pipe vs tiled.
781 * See https://codereview.chromium.org/13650002/ )
epoger@google.com37269602013-01-19 04:21:27 +0000782 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000783 ErrorCombination compare_to_expectations(Expectations expectations,
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000784 const BitmapAndDigest& actualBitmapAndDigest,
epoger@google.com659c8c02013-05-21 15:45:45 +0000785 const char *shortName, const char *configName,
786 const char *renderModeDescriptor,
epoger@google.comcaac3db2013-04-04 19:23:11 +0000787 bool addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000788 ErrorCombination errors;
epoger@google.com659c8c02013-05-21 15:45:45 +0000789 SkString shortNamePlusConfig = make_shortname_plus_config(shortName, configName);
790 SkString completeNameString(shortNamePlusConfig);
epoger@google.com37269602013-01-19 04:21:27 +0000791 completeNameString.append(renderModeDescriptor);
epoger@google.comce057fe2013-05-14 15:17:46 +0000792 completeNameString.append(".");
793 completeNameString.append(kPNG_FileExtension);
epoger@google.com37269602013-01-19 04:21:27 +0000794 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000795
epoger@google.com37269602013-01-19 04:21:27 +0000796 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000797 errors.add(kMissingExpectations_ErrorType);
epoger@google.com5f995452013-06-21 18:16:47 +0000798
799 // Write out the "actuals" for any tests without expectations, if we have
800 // been directed to do so.
801 if (fMissingExpectationsPath) {
802 SkString path = make_bitmap_filename(fMissingExpectationsPath, shortName,
803 configName, renderModeDescriptor,
804 actualBitmapAndDigest.fDigest);
805 write_bitmap(path, actualBitmapAndDigest.fBitmap);
806 }
807
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000808 } else if (!expectations.match(actualBitmapAndDigest.fDigest)) {
epoger@google.comcaac3db2013-04-04 19:23:11 +0000809 addToJsonSummary = true;
810 // The error mode we record depends on whether this was running
811 // in a non-standard renderMode.
812 if ('\0' == *renderModeDescriptor) {
813 errors.add(kExpectationsMismatch_ErrorType);
814 } else {
815 errors.add(kRenderModeMismatch_ErrorType);
816 }
epoger@google.com84a18022013-02-01 20:39:15 +0000817
818 // Write out the "actuals" for any mismatches, if we have
819 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000820 if (fMismatchPath) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000821 SkString path = make_bitmap_filename(fMismatchPath, shortName, configName,
822 renderModeDescriptor,
823 actualBitmapAndDigest.fDigest);
824 write_bitmap(path, actualBitmapAndDigest.fBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000825 }
epoger@google.com84a18022013-02-01 20:39:15 +0000826
827 // If we have access to a single expected bitmap, log more
828 // detail about the mismatch.
829 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
830 if (NULL != expectedBitmapPtr) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000831 report_bitmap_diffs(*expectedBitmapPtr, actualBitmapAndDigest.fBitmap,
832 completeName);
epoger@google.com84a18022013-02-01 20:39:15 +0000833 }
epoger@google.coma243b222013-01-17 17:54:28 +0000834 }
epoger@google.com659c8c02013-05-21 15:45:45 +0000835 RecordTestResults(errors, shortNamePlusConfig, renderModeDescriptor);
epoger@google.coma243b222013-01-17 17:54:28 +0000836
epoger@google.com37269602013-01-19 04:21:27 +0000837 if (addToJsonSummary) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000838 add_actual_results_to_json_summary(completeName, actualBitmapAndDigest.fDigest, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000839 expectations.ignoreFailure());
840 add_expected_results_to_json_summary(completeName, expectations);
841 }
epoger@google.coma243b222013-01-17 17:54:28 +0000842
epoger@google.com6f6568b2013-03-22 17:29:46 +0000843 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000844 }
845
epoger@google.com37269602013-01-19 04:21:27 +0000846 /**
847 * Add this result to the appropriate JSON collection of actual results,
epoger@google.com76c913d2013-04-26 15:06:44 +0000848 * depending on errors encountered.
epoger@google.com37269602013-01-19 04:21:27 +0000849 */
850 void add_actual_results_to_json_summary(const char testName[],
epoger@google.comd4993ff2013-05-24 14:33:28 +0000851 const GmResultDigest &actualResultDigest,
epoger@google.com76c913d2013-04-26 15:06:44 +0000852 ErrorCombination errors,
epoger@google.com37269602013-01-19 04:21:27 +0000853 bool ignoreFailure) {
epoger@google.comd4993ff2013-05-24 14:33:28 +0000854 Json::Value jsonActualResults = actualResultDigest.asJsonTypeValuePair();
epoger@google.com76c913d2013-04-26 15:06:44 +0000855 if (errors.isEmpty()) {
856 this->fJsonActualResults_Succeeded[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000857 } else {
858 if (ignoreFailure) {
859 // TODO: Once we have added the ability to compare
860 // actual results against expectations in a JSON file
861 // (where we can set ignore-failure to either true or
epoger@google.com84a18022013-02-01 20:39:15 +0000862 // false), add test cases that exercise ignored
epoger@google.com6f6568b2013-03-22 17:29:46 +0000863 // failures (both for kMissingExpectations_ErrorType
epoger@google.comcaac3db2013-04-04 19:23:11 +0000864 // and kExpectationsMismatch_ErrorType).
epoger@google.com37269602013-01-19 04:21:27 +0000865 this->fJsonActualResults_FailureIgnored[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000866 jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000867 } else {
epoger@google.com76c913d2013-04-26 15:06:44 +0000868 if (errors.includes(kMissingExpectations_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000869 // TODO: What about the case where there IS an
epoger@google.com908f5832013-04-12 02:23:55 +0000870 // expected image hash digest, but that gm test
epoger@google.com37269602013-01-19 04:21:27 +0000871 // doesn't actually run? For now, those cases
872 // will always be ignored, because gm only looks
873 // at expectations that correspond to gm tests
874 // that were actually run.
875 //
876 // Once we have the ability to express
877 // expectations as a JSON file, we should fix this
878 // (and add a test case for which an expectation
879 // is given but the test is never run).
880 this->fJsonActualResults_NoComparison[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000881 jsonActualResults;
epoger@google.comeb066362013-03-08 09:39:36 +0000882 }
epoger@google.com76c913d2013-04-26 15:06:44 +0000883 if (errors.includes(kExpectationsMismatch_ErrorType) ||
884 errors.includes(kRenderModeMismatch_ErrorType)) {
885 this->fJsonActualResults_Failed[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000886 }
887 }
888 }
889 }
890
891 /**
892 * Add this test to the JSON collection of expected results.
893 */
894 void add_expected_results_to_json_summary(const char testName[],
895 Expectations expectations) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000896 this->fJsonExpectedResults[testName] = expectations.asJsonValue();
epoger@google.com37269602013-01-19 04:21:27 +0000897 }
898
899 /**
900 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
901 *
902 * @param gm which test generated the actualBitmap
903 * @param gRec
904 * @param writePath unless this is NULL, write out actual images into this
905 * directory
epoger@google.come33e1372013-07-08 19:13:33 +0000906 * @param actualBitmapAndDigest ptr to bitmap generated by this run, or NULL
907 * if we don't have a usable bitmap representation
908 * @param document pdf or xps representation, if appropriate
epoger@google.com37269602013-01-19 04:21:27 +0000909 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000910 ErrorCombination compare_test_results_to_stored_expectations(
epoger@google.com37269602013-01-19 04:21:27 +0000911 GM* gm, const ConfigData& gRec, const char writePath[],
bungeman@google.com88682b72013-07-19 13:55:41 +0000912 const BitmapAndDigest* actualBitmapAndDigest, SkStreamAsset* document) {
epoger@google.com37269602013-01-19 04:21:27 +0000913
epoger@google.com659c8c02013-05-21 15:45:45 +0000914 SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(), gRec.fName);
915 SkString nameWithExtension(shortNamePlusConfig);
916 nameWithExtension.append(".");
917 nameWithExtension.append(kPNG_FileExtension);
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000918
epoger@google.com659c8c02013-05-21 15:45:45 +0000919 ErrorCombination errors;
epoger@google.come33e1372013-07-08 19:13:33 +0000920
921 if (NULL == actualBitmapAndDigest) {
922 // Note that we intentionally skipped validating the results for
923 // this test, because we don't know how to generate an SkBitmap
924 // version of the output.
925 RecordTestResults(ErrorCombination(kIntentionallySkipped_ErrorType),
epoger@google.com659c8c02013-05-21 15:45:45 +0000926 shortNamePlusConfig, "");
epoger@google.comc824c832013-07-12 15:52:59 +0000927 } else if (!(gRec.fFlags & kWrite_ConfigFlag)) {
928 // We don't record the results for this test or compare them
929 // against any expectations, because the output image isn't
930 // meaningful.
931 // See https://code.google.com/p/skia/issues/detail?id=1410 ('some
932 // GM result images not available for download from Google Storage')
933 RecordTestResults(ErrorCombination(kIntentionallySkipped_ErrorType),
934 shortNamePlusConfig, "");
epoger@google.come33e1372013-07-08 19:13:33 +0000935 } else {
936 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
937 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
938 /*
939 * Get the expected results for this test, as one or more allowed
940 * hash digests. The current implementation of expectationsSource
941 * get this by computing the hash digest of a single PNG file on disk.
942 *
943 * TODO(epoger): This relies on the fact that
944 * force_all_opaque() was called on the bitmap before it
945 * was written to disk as a PNG in the first place. If
946 * not, the hash digest returned here may not match the
947 * hash digest of actualBitmap, which *has* been run through
948 * force_all_opaque().
949 * See comments above complete_bitmap() for more detail.
950 */
951 Expectations expectations = expectationsSource->get(nameWithExtension.c_str());
952 errors.add(compare_to_expectations(expectations, *actualBitmapAndDigest,
953 gm->shortName(), gRec.fName, "", true));
954 } else {
955 // If we are running without expectations, we still want to
956 // record the actual results.
957 add_actual_results_to_json_summary(nameWithExtension.c_str(),
958 actualBitmapAndDigest->fDigest,
959 ErrorCombination(kMissingExpectations_ErrorType),
960 false);
961 RecordTestResults(ErrorCombination(kMissingExpectations_ErrorType),
962 shortNamePlusConfig, "");
963 }
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000964 }
epoger@google.com37269602013-01-19 04:21:27 +0000965
966 // TODO: Consider moving this into compare_to_expectations(),
967 // similar to fMismatchPath... for now, we don't do that, because
968 // we don't want to write out the actual bitmaps for all
969 // renderModes of all tests! That would be a lot of files.
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000970 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000971 errors.add(write_reference_image(gRec, writePath, "", gm->shortName(),
epoger@google.come33e1372013-07-08 19:13:33 +0000972 actualBitmapAndDigest, document));
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000973 }
epoger@google.com37269602013-01-19 04:21:27 +0000974
epoger@google.com6f6568b2013-03-22 17:29:46 +0000975 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000976 }
977
epoger@google.com37269602013-01-19 04:21:27 +0000978 /**
979 * Compare actualBitmap to referenceBitmap.
980 *
epoger@google.com659c8c02013-05-21 15:45:45 +0000981 * @param shortName test name, e.g. "selftest1"
982 * @param configName configuration name, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000983 * @param renderModeDescriptor
984 * @param actualBitmap actual bitmap generated by this run
985 * @param referenceBitmap bitmap we expected to be generated
986 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000987 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +0000988 const char *shortName, const char *configName, const char *renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000989 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
990
991 SkASSERT(referenceBitmap);
epoger@google.com84a18022013-02-01 20:39:15 +0000992 Expectations expectations(*referenceBitmap);
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000993 BitmapAndDigest actualBitmapAndDigest(actualBitmap);
994 return compare_to_expectations(expectations, actualBitmapAndDigest, shortName,
epoger@google.com659c8c02013-05-21 15:45:45 +0000995 configName, renderModeDescriptor, false);
epoger@google.com37269602013-01-19 04:21:27 +0000996 }
997
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000998 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
999 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +00001000 // Pictures are refcounted so must be on heap
junov@chromium.org3cb834b2012-12-13 16:39:53 +00001001 SkPicture* pict;
junov@chromium.org706ff2f2012-12-19 15:55:40 +00001002 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
1003 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +00001004
junov@chromium.org3cb834b2012-12-13 16:39:53 +00001005 if (kTileGrid_BbhType == bbhType) {
junov@chromium.org29b19e52013-02-27 18:35:16 +00001006 SkTileGridPicture::TileGridInfo info;
1007 info.fMargin.setEmpty();
1008 info.fOffset.setZero();
1009 info.fTileInterval.set(16, 16);
1010 pict = new SkTileGridPicture(width, height, info);
junov@chromium.org3cb834b2012-12-13 16:39:53 +00001011 } else {
1012 pict = new SkPicture;
1013 }
junov@chromium.org20bd04e2013-01-16 18:43:36 +00001014 if (kNone_BbhType != bbhType) {
1015 recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
1016 }
junov@chromium.org706ff2f2012-12-19 15:55:40 +00001017 SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +00001018 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +00001019 invokeGM(gm, cv, false, false);
epoger@google.comde961632012-10-26 18:56:36 +00001020 pict->endRecording();
1021
1022 return pict;
1023 }
1024
1025 static SkPicture* stream_to_new_picture(const SkPicture& src) {
epoger@google.comde961632012-10-26 18:56:36 +00001026 SkDynamicMemoryWStream storage;
1027 src.serialize(&storage);
bungeman@google.comc29f3d82013-07-19 22:32:11 +00001028 SkAutoTUnref<SkStreamAsset> pictReadback(storage.detachAsStream());
bungeman@google.com88682b72013-07-19 13:55:41 +00001029 SkPicture* retval = SkPicture::CreateFromStream(pictReadback);
epoger@google.comde961632012-10-26 18:56:36 +00001030 return retval;
1031 }
1032
1033 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +00001034 // Depending on flags, possibly compare to an expected image.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001035 ErrorCombination test_drawing(GM* gm,
1036 const ConfigData& gRec,
1037 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001038 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +00001039 SkBitmap* bitmap) {
epoger@google.comde961632012-10-26 18:56:36 +00001040 SkDynamicMemoryWStream document;
1041
1042 if (gRec.fBackend == kRaster_Backend ||
1043 gRec.fBackend == kGPU_Backend) {
1044 // Early exit if we can't generate the image.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001045 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001046 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +00001047 // TODO: Add a test to exercise what the stdout and
1048 // JSON look like if we get an "early error" while
1049 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +00001050 return errors;
1051 }
1052 } else if (gRec.fBackend == kPDF_Backend) {
1053 generate_pdf(gm, document);
1054#if CAN_IMAGE_PDF
1055 SkAutoDataUnref data(document.copyToData());
1056 SkMemoryStream stream(data->data(), data->size());
1057 SkPDFDocumentToBitmap(&stream, bitmap);
epoger@google.come33e1372013-07-08 19:13:33 +00001058#else
1059 bitmap = NULL; // we don't generate a bitmap rendering of the PDF file
epoger@google.comde961632012-10-26 18:56:36 +00001060#endif
1061 } else if (gRec.fBackend == kXPS_Backend) {
1062 generate_xps(gm, document);
epoger@google.come33e1372013-07-08 19:13:33 +00001063 bitmap = NULL; // we don't generate a bitmap rendering of the XPS file
epoger@google.comde961632012-10-26 18:56:36 +00001064 }
epoger@google.come33e1372013-07-08 19:13:33 +00001065
bungeman@google.comc29f3d82013-07-19 22:32:11 +00001066 SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream());
epoger@google.come33e1372013-07-08 19:13:33 +00001067 if (NULL == bitmap) {
1068 return compare_test_results_to_stored_expectations(
bungeman@google.com88682b72013-07-19 13:55:41 +00001069 gm, gRec, writePath, NULL, documentStream);
epoger@google.come33e1372013-07-08 19:13:33 +00001070 } else {
1071 BitmapAndDigest bitmapAndDigest(*bitmap);
1072 return compare_test_results_to_stored_expectations(
bungeman@google.com88682b72013-07-19 13:55:41 +00001073 gm, gRec, writePath, &bitmapAndDigest, documentStream);
epoger@google.come33e1372013-07-08 19:13:33 +00001074 }
epoger@google.comde961632012-10-26 18:56:36 +00001075 }
1076
epoger@google.com6f6568b2013-03-22 17:29:46 +00001077 ErrorCombination test_deferred_drawing(GM* gm,
1078 const ConfigData& gRec,
1079 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001080 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +00001081 if (gRec.fBackend == kRaster_Backend ||
1082 gRec.fBackend == kGPU_Backend) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001083 const char renderModeDescriptor[] = "-deferred";
epoger@google.comde961632012-10-26 18:56:36 +00001084 SkBitmap bitmap;
1085 // Early exit if we can't generate the image, but this is
1086 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001087 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001088 // TODO(epoger): This logic is the opposite of what is
1089 // described above... if we succeeded in generating the
1090 // -deferred image, we exit early! We should fix this
1091 // ASAP, because it is hiding -deferred errors... but for
1092 // now, I'm leaving the logic as it is so that the
1093 // refactoring change
1094 // https://codereview.chromium.org/12992003/ is unblocked.
1095 //
1096 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
1097 // ('image-surface gm test is failing in "deferred" mode,
1098 // and gm is not reporting the failure')
1099 if (errors.isEmpty()) {
epoger@google.com310478e2013-04-03 18:00:39 +00001100 // TODO(epoger): Report this as a new ErrorType,
1101 // something like kImageGeneration_ErrorType?
epoger@google.com6f6568b2013-03-22 17:29:46 +00001102 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001103 }
epoger@google.com37269602013-01-19 04:21:27 +00001104 return compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001105 gm->shortName(), gRec.fName, renderModeDescriptor, bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001106 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001107 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001108 }
1109
epoger@google.comcaac3db2013-04-04 19:23:11 +00001110 ErrorCombination test_pipe_playback(GM* gm, const ConfigData& gRec,
1111 const SkBitmap& referenceBitmap, bool simulateFailure) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001112 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(),
1113 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001114 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001115 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001116 SkString renderModeDescriptor("-pipe");
1117 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1118
1119 if (gm->getFlags() & GM::kSkipPipe_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001120 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001121 renderModeDescriptor.c_str());
1122 errors.add(kIntentionallySkipped_ErrorType);
1123 } else {
1124 SkBitmap bitmap;
1125 SkISize size = gm->getISize();
1126 setup_bitmap(gRec, size, &bitmap);
1127 SkCanvas canvas(bitmap);
1128 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001129 // Pass a decoding function so the factory GM (which has an SkBitmap
1130 // with encoded data) will not fail playback.
1131 PipeController pipeController(&canvas, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001132 SkGPipeWriter writer;
1133 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001134 gPipeWritingFlagCombos[i].flags,
1135 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001136 if (!simulateFailure) {
1137 invokeGM(gm, pipeCanvas, false, false);
1138 }
1139 complete_bitmap(&bitmap);
1140 writer.endRecording();
1141 errors.add(compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001142 gm->shortName(), gRec.fName, renderModeDescriptor.c_str(), bitmap,
1143 &referenceBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001144 if (!errors.isEmpty()) {
1145 break;
1146 }
epoger@google.comde961632012-10-26 18:56:36 +00001147 }
1148 }
1149 return errors;
1150 }
1151
epoger@google.com6f6568b2013-03-22 17:29:46 +00001152 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
1153 const SkBitmap& referenceBitmap) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001154 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(),
1155 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001156 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001157 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001158 SkString renderModeDescriptor("-tiled pipe");
1159 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1160
1161 if ((gm->getFlags() & GM::kSkipPipe_Flag) ||
1162 (gm->getFlags() & GM::kSkipTiled_Flag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001163 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001164 renderModeDescriptor.c_str());
1165 errors.add(kIntentionallySkipped_ErrorType);
1166 } else {
1167 SkBitmap bitmap;
1168 SkISize size = gm->getISize();
1169 setup_bitmap(gRec, size, &bitmap);
1170 SkCanvas canvas(bitmap);
1171 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001172 TiledPipeController pipeController(bitmap, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001173 SkGPipeWriter writer;
1174 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001175 gPipeWritingFlagCombos[i].flags,
1176 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001177 invokeGM(gm, pipeCanvas, false, false);
1178 complete_bitmap(&bitmap);
1179 writer.endRecording();
epoger@google.com659c8c02013-05-21 15:45:45 +00001180 errors.add(compare_test_results_to_reference_bitmap(gm->shortName(), gRec.fName,
epoger@google.comc8263e72013-04-10 12:17:34 +00001181 renderModeDescriptor.c_str(),
1182 bitmap, &referenceBitmap));
1183 if (!errors.isEmpty()) {
1184 break;
1185 }
epoger@google.comde961632012-10-26 18:56:36 +00001186 }
1187 }
1188 return errors;
1189 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00001190
1191 //
1192 // member variables.
1193 // They are public for now, to allow easier setting by tool_main().
1194 //
1195
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001196 bool fUseFileHierarchy, fWriteChecksumBasedFilenames;
epoger@google.com5079d2c2013-04-12 14:11:21 +00001197 ErrorCombination fIgnorableErrorTypes;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001198
junov@chromium.org95146eb2013-01-11 21:04:40 +00001199 const char* fMismatchPath;
epoger@google.com5f995452013-06-21 18:16:47 +00001200 const char* fMissingExpectationsPath;
junov@chromium.org95146eb2013-01-11 21:04:40 +00001201
epoger@google.com310478e2013-04-03 18:00:39 +00001202 // collection of tests that have failed with each ErrorType
1203 SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
1204 int fTestsRun;
1205 SkTDict<int> fRenderModesEncountered;
epoger@google.com57f7abc2012-11-13 03:41:55 +00001206
epoger@google.com908f5832013-04-12 02:23:55 +00001207 // Where to read expectations (expected image hash digests, etc.) from.
epoger@google.com37269602013-01-19 04:21:27 +00001208 // If unset, we don't do comparisons.
1209 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
1210
1211 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +00001212 Json::Value fJsonExpectedResults;
1213 Json::Value fJsonActualResults_Failed;
1214 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001215 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001216 Json::Value fJsonActualResults_Succeeded;
1217
epoger@google.comde961632012-10-26 18:56:36 +00001218}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +00001219
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001220#if SK_SUPPORT_GPU
1221static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1222#else
1223static const GLContextType kDontCare_GLContextType = 0;
1224#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +00001225
1226// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +00001227// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +00001228static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
1229 kWrite_ConfigFlag;
1230
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001231static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001232 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001233#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 +00001234 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001235#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001236 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
1237#if SK_SUPPORT_GPU
1238 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
robertphillips@google.com7bb28a12013-06-06 21:29:39 +00001239 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", false},
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001240 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
epoger@google.comc824c832013-07-12 15:52:59 +00001241 /* The gpudebug context does not generate meaningful images, so don't record
1242 * the images it generates! We only run it to look for asserts. */
scroggo@google.com0f567c62013-03-20 15:35:08 +00001243 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", GR_DEBUG},
robertphillips@google.comd6543e52013-07-18 17:39:14 +00001244 /* The gpunull context does the least amount of work possible and doesn't
1245 generate meaninful images, so don't record them!. It can be run to
1246 isolate the CPU-side processing expense from the GPU-side.
1247 */
1248 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNull_GLContextType, 0, kNone_ConfigFlag, "gpunull", GR_DEBUG},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001249#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001250 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
1251 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001252#endif // SK_ANGLE
1253#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001254 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001255#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001256#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +00001257#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001258 /* 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 +00001259 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +00001260#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001261#ifdef SK_SUPPORT_PDF
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001262 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001263#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001264};
1265
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001266static const char kDefaultsConfigStr[] = "defaults";
1267static const char kExcludeConfigChar = '~';
1268
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001269static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001270 SkString result;
1271 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001272 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001273 SkASSERT(gRec[i].fName != kDefaultsConfigStr);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001274 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001275 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001276 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001277 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001278 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001279 result.append("]\n");
1280 result.appendf("The default value is: \"");
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001281 SkString firstDefault;
1282 SkString allButFirstDefaults;
1283 SkString nonDefault;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001284 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1285 if (gRec[i].fRunByDefault) {
1286 if (i > 0) {
1287 result.append(" ");
1288 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001289 result.append(gRec[i].fName);
1290 if (firstDefault.isEmpty()) {
1291 firstDefault = gRec[i].fName;
1292 } else {
1293 if (!allButFirstDefaults.isEmpty()) {
1294 allButFirstDefaults.append(" ");
1295 }
1296 allButFirstDefaults.append(gRec[i].fName);
1297 }
1298 } else {
1299 nonDefault = gRec[i].fName;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001300 }
1301 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001302 result.append("\"\n");
1303 result.appendf("\"%s\" evaluates to the default set of configs.\n", kDefaultsConfigStr);
1304 result.appendf("Prepending \"%c\" on a config name excludes it from the set of configs to run.\n"
1305 "Exclusions always override inclusions regardless of order.\n",
1306 kExcludeConfigChar);
1307 result.appendf("E.g. \"--config %s %c%s %s\" will run these configs:\n\t%s %s",
1308 kDefaultsConfigStr,
1309 kExcludeConfigChar,
1310 firstDefault.c_str(),
1311 nonDefault.c_str(),
1312 allButFirstDefaults.c_str(),
1313 nonDefault.c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001314 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001315}
scroggo@google.com7d519302013-03-19 17:28:10 +00001316
epoger@google.com6f6568b2013-03-22 17:29:46 +00001317// Macro magic to convert a numeric preprocessor token into a string.
1318// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1319// This should probably be moved into one of our common headers...
1320#define TOSTRING_INTERNAL(x) #x
1321#define TOSTRING(x) TOSTRING_INTERNAL(x)
1322
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001323// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001324DEFINE_string(config, "", configUsage().c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001325DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001326DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1327DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1328#if SK_SUPPORT_GPU
1329DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001330 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1331 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001332#endif
1333DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1334 "when reading/writing files.");
epoger@google.com5079d2c2013-04-12 14:11:21 +00001335DEFINE_string(ignoreErrorTypes, kDefaultIgnorableErrorTypes.asString(" ").c_str(),
1336 "Space-separated list of ErrorTypes that should be ignored. If any *other* error "
1337 "types are encountered, the tool will exit with a nonzero return value.");
caryclark@google.com512c9b62013-05-10 15:16:13 +00001338DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
1339 "Multiple matches may be separated by spaces.\n"
1340 "~ causes a matching test to always be skipped\n"
1341 "^ requires the start of the test to match\n"
1342 "$ requires the end of the test to match\n"
1343 "^ and $ requires an exact match\n"
1344 "If a test does not match any list entry,\n"
1345 "it is skipped unless some list entry starts with ~");
epoger@google.com5f995452013-06-21 18:16:47 +00001346DEFINE_string(missingExpectationsPath, "", "Write images for tests without expectations "
1347 "into this directory.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001348DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1349 "pixel mismatches into this directory.");
1350DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1351 "testIndex %% divisor == remainder.");
1352DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1353DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1354DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1355 "any differences between those and the newly generated ones.");
1356DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1357DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1358DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1359DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
epoger@google.comcaac3db2013-04-04 19:23:11 +00001360DEFINE_bool(simulatePipePlaybackFailure, false, "Simulate a rendering failure in pipe mode only.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001361DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1362DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1363DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1364 "factors to be used for tileGrid playback testing. Default value: 1.0");
epoger@google.comb0f8b432013-04-10 18:46:25 +00001365DEFINE_bool2(verbose, v, false, "Give more detail (e.g. list all GMs run, more info about "
1366 "each test).");
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001367DEFINE_bool(writeChecksumBasedFilenames, false, "When writing out actual images, use checksum-"
1368 "based filenames, as rebaseline.py will use when downloading them from Google Storage");
1369DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001370DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
scroggo@google.com604e0c22013-04-09 21:25:46 +00001371DEFINE_string2(writePicturePath, p, "", "Write .skp files into this directory.");
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001372DEFINE_int32(pdfJpegQuality, -1, "Encodes images in JPEG at quality level N, "
1373 "which can be in range 0-100). N = -1 will disable JPEG compression. "
1374 "Default is N = 100, maximum quality.");
1375
1376static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect) {
1377 // Filter output of warnings that JPEG is not available for the image.
1378 if (bitmap.width() >= 65500 || bitmap.height() >= 65500) return false;
1379 if (FLAGS_pdfJpegQuality == -1) return false;
1380
1381 SkIRect bitmapBounds;
1382 SkBitmap subset;
1383 const SkBitmap* bitmapToUse = &bitmap;
1384 bitmap.getBounds(&bitmapBounds);
1385 if (rect != bitmapBounds) {
1386 SkAssertResult(bitmap.extractSubset(&subset, rect));
1387 bitmapToUse = &subset;
1388 }
1389
1390#if defined(SK_BUILD_FOR_MAC)
1391 // Workaround bug #1043 where bitmaps with referenced pixels cause
1392 // CGImageDestinationFinalize to crash
1393 SkBitmap copy;
1394 bitmapToUse->deepCopyTo(&copy, bitmapToUse->config());
1395 bitmapToUse = &copy;
1396#endif
1397
1398 return SkImageEncoder::EncodeStream(stream,
1399 *bitmapToUse,
1400 SkImageEncoder::kJPEG_Type,
1401 FLAGS_pdfJpegQuality);
1402}
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001403
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001404static int findConfig(const char config[]) {
1405 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1406 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001407 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001408 }
1409 }
1410 return -1;
1411}
1412
scroggo@google.comb7dbf632013-04-23 15:38:09 +00001413static bool skip_name(SkCommandLineFlags::StringArray array, const char name[]) {
caryclark@google.com512c9b62013-05-10 15:16:13 +00001414 // FIXME: this duplicates the logic in test/skia_test.cpp -- consolidate
1415 int count = array.count();
1416 size_t testLen = strlen(name);
1417 bool anyExclude = count == 0;
reed@google.comb2a51622011-10-31 16:30:04 +00001418 for (int i = 0; i < array.count(); ++i) {
caryclark@google.com512c9b62013-05-10 15:16:13 +00001419 const char* matchName = array[i];
1420 size_t matchLen = strlen(matchName);
1421 bool matchExclude, matchStart, matchEnd;
1422 if ((matchExclude = matchName[0] == '~')) {
1423 anyExclude = true;
1424 matchName++;
1425 matchLen--;
1426 }
1427 if ((matchStart = matchName[0] == '^')) {
1428 matchName++;
1429 matchLen--;
1430 }
1431 if ((matchEnd = matchName[matchLen - 1] == '$')) {
1432 matchLen--;
1433 }
1434 if (matchStart ? (!matchEnd || matchLen == testLen)
1435 && strncmp(name, matchName, matchLen) == 0
1436 : matchEnd ? matchLen <= testLen
1437 && strncmp(name + testLen - matchLen, matchName, matchLen) == 0
1438 : strstr(name, matchName) != 0) {
1439 return matchExclude;
reed@google.comb2a51622011-10-31 16:30:04 +00001440 }
1441 }
caryclark@google.com512c9b62013-05-10 15:16:13 +00001442 return !anyExclude;
reed@google.comb2a51622011-10-31 16:30:04 +00001443}
1444
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001445namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001446#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +00001447SkAutoTUnref<GrContext> gGrContext;
1448/**
bsalomon@google.comc7a24d22012-11-01 18:03:48 +00001449 * Sets the global GrContext, accessible by individual GMs
bsalomon@google.com7361f542012-04-19 19:15:35 +00001450 */
caryclark@google.com13130862012-06-06 12:10:45 +00001451static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +00001452 SkSafeRef(grContext);
1453 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001454}
bsalomon@google.com7361f542012-04-19 19:15:35 +00001455
1456/**
1457 * Gets the global GrContext, can be called by GM tests.
1458 */
caryclark@google.com13130862012-06-06 12:10:45 +00001459GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +00001460GrContext* GetGr() {
1461 return gGrContext.get();
1462}
1463
1464/**
1465 * Sets the global GrContext and then resets it to its previous value at
1466 * destruction.
1467 */
1468class AutoResetGr : SkNoncopyable {
1469public:
1470 AutoResetGr() : fOld(NULL) {}
1471 void set(GrContext* context) {
1472 SkASSERT(NULL == fOld);
1473 fOld = GetGr();
1474 SkSafeRef(fOld);
1475 SetGr(context);
1476 }
1477 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1478private:
1479 GrContext* fOld;
1480};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001481#else
epoger@google.com80724df2013-03-21 13:49:54 +00001482GrContext* GetGr();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001483GrContext* GetGr() { return NULL; }
1484#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001485}
1486
reed@google.comfb2cd422013-01-04 14:43:03 +00001487template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1488 int index = array->find(value);
1489 if (index < 0) {
1490 *array->append() = value;
1491 }
1492}
1493
epoger@google.com80724df2013-03-21 13:49:54 +00001494/**
1495 * Run this test in a number of different configs (8888, 565, PDF,
1496 * etc.), confirming that the resulting bitmaps match expectations
1497 * (which may be different for each config).
1498 *
1499 * Returns all errors encountered while doing so.
1500 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001501ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1502 GrContextFactory *grFactory);
1503ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1504 GrContextFactory *grFactory) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001505 const char renderModeDescriptor[] = "";
epoger@google.com6f6568b2013-03-22 17:29:46 +00001506 ErrorCombination errorsForAllConfigs;
epoger@google.com80724df2013-03-21 13:49:54 +00001507 uint32_t gmFlags = gm->getFlags();
1508
epoger@google.com80724df2013-03-21 13:49:54 +00001509 for (int i = 0; i < configs.count(); i++) {
1510 ConfigData config = gRec[configs[i]];
epoger@google.com659c8c02013-05-21 15:45:45 +00001511 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->shortName(),
1512 config.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001513
1514 // Skip any tests that we don't even need to try.
epoger@google.comc8263e72013-04-10 12:17:34 +00001515 // If any of these were skipped on a per-GM basis, record them as
1516 // kIntentionallySkipped.
1517 if (kPDF_Backend == config.fBackend) {
1518 if (!FLAGS_pdf) {
epoger@google.com80724df2013-03-21 13:49:54 +00001519 continue;
1520 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001521 if (gmFlags & GM::kSkipPDF_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001522 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001523 renderModeDescriptor);
1524 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1525 continue;
1526 }
1527 }
epoger@google.com80724df2013-03-21 13:49:54 +00001528 if ((gmFlags & GM::kSkip565_Flag) &&
1529 (kRaster_Backend == config.fBackend) &&
1530 (SkBitmap::kRGB_565_Config == config.fConfig)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001531 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001532 renderModeDescriptor);
1533 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001534 continue;
1535 }
1536 if ((gmFlags & GM::kSkipGPU_Flag) &&
1537 kGPU_Backend == config.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001538 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001539 renderModeDescriptor);
1540 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001541 continue;
1542 }
1543
1544 // Now we know that we want to run this test and record its
1545 // success or failure.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001546 ErrorCombination errorsForThisConfig;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001547 GrSurface* gpuTarget = NULL;
epoger@google.com80724df2013-03-21 13:49:54 +00001548#if SK_SUPPORT_GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001549 SkAutoTUnref<GrSurface> auGpuTarget;
epoger@google.com80724df2013-03-21 13:49:54 +00001550 AutoResetGr autogr;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001551 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
epoger@google.com80724df2013-03-21 13:49:54 +00001552 GrContext* gr = grFactory->get(config.fGLContextType);
1553 bool grSuccess = false;
1554 if (gr) {
1555 // create a render target to back the device
1556 GrTextureDesc desc;
1557 desc.fConfig = kSkia8888_GrPixelConfig;
1558 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1559 desc.fWidth = gm->getISize().width();
1560 desc.fHeight = gm->getISize().height();
1561 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001562 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1563 if (NULL != auGpuTarget) {
1564 gpuTarget = auGpuTarget;
1565 grSuccess = true;
epoger@google.com80724df2013-03-21 13:49:54 +00001566 autogr.set(gr);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001567 // Set the user specified cache limits if non-default.
1568 size_t bytes;
1569 int count;
1570 gr->getTextureCacheLimits(&count, &bytes);
1571 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1572 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1573 }
1574 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1575 count = gGpuCacheSizeCount;
1576 }
1577 gr->setTextureCacheLimits(count, bytes);
epoger@google.com80724df2013-03-21 13:49:54 +00001578 }
epoger@google.com80724df2013-03-21 13:49:54 +00001579 }
1580 if (!grSuccess) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001581 errorsForThisConfig.add(kNoGpuContext_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001582 }
1583 }
1584#endif
1585
1586 SkBitmap comparisonBitmap;
1587
1588 const char* writePath;
1589 if (FLAGS_writePath.count() == 1) {
1590 writePath = FLAGS_writePath[0];
1591 } else {
1592 writePath = NULL;
1593 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001594 if (errorsForThisConfig.isEmpty()) {
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001595 errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
1596 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001597 }
1598
epoger@google.com6f6568b2013-03-22 17:29:46 +00001599 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1600 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1601 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001602 gpuTarget));
epoger@google.com80724df2013-03-21 13:49:54 +00001603 }
1604
epoger@google.com6f6568b2013-03-22 17:29:46 +00001605 errorsForAllConfigs.add(errorsForThisConfig);
epoger@google.com80724df2013-03-21 13:49:54 +00001606 }
1607 return errorsForAllConfigs;
1608}
1609
1610/**
1611 * Run this test in a number of different drawing modes (pipe,
1612 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1613 * *exactly* match comparisonBitmap.
1614 *
1615 * Returns all errors encountered while doing so.
1616 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001617ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1618 const SkBitmap &comparisonBitmap,
1619 const SkTDArray<SkScalar> &tileGridReplayScales);
1620ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1621 const SkBitmap &comparisonBitmap,
1622 const SkTDArray<SkScalar> &tileGridReplayScales) {
1623 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001624 uint32_t gmFlags = gm->getFlags();
epoger@google.com659c8c02013-05-21 15:45:45 +00001625 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->shortName(),
1626 compareConfig.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001627
epoger@google.comc8263e72013-04-10 12:17:34 +00001628 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1629 SkAutoUnref aur(pict);
1630 if (FLAGS_replay) {
1631 const char renderModeDescriptor[] = "-replay";
1632 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001633 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1634 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001635 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1636 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001637 SkBitmap bitmap;
1638 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001639 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001640 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1641 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001642 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001643 }
epoger@google.com80724df2013-03-21 13:49:54 +00001644
epoger@google.comc8263e72013-04-10 12:17:34 +00001645 if (FLAGS_serialize) {
1646 const char renderModeDescriptor[] = "-serialize";
1647 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001648 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1649 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001650 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1651 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001652 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1653 SkAutoUnref aurr(repict);
epoger@google.com80724df2013-03-21 13:49:54 +00001654 SkBitmap bitmap;
1655 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001656 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001657 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1658 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001659 }
epoger@google.com80724df2013-03-21 13:49:54 +00001660 }
1661
epoger@google.comc8263e72013-04-10 12:17:34 +00001662 if ((1 == FLAGS_writePicturePath.count()) &&
1663 !(gmFlags & GM::kSkipPicture_Flag)) {
1664 const char* pictureSuffix = "skp";
epoger@google.com659c8c02013-05-21 15:45:45 +00001665 // TODO(epoger): Make sure this still works even though the
1666 // filename now contains the config name (it used to contain
1667 // just the shortName). I think this is actually an
1668 // *improvement*, because now runs with different configs will
1669 // write out their SkPictures to separate files rather than
1670 // overwriting each other. But we should make sure it doesn't
1671 // break anybody.
1672 SkString path = gmmain.make_filename(FLAGS_writePicturePath[0], gm->shortName(),
1673 compareConfig.fName, "", pictureSuffix);
epoger@google.comc8263e72013-04-10 12:17:34 +00001674 SkFILEWStream stream(path.c_str());
1675 pict->serialize(&stream);
epoger@google.com80724df2013-03-21 13:49:54 +00001676 }
1677
epoger@google.comc8263e72013-04-10 12:17:34 +00001678 if (FLAGS_rtree) {
1679 const char renderModeDescriptor[] = "-rtree";
1680 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001681 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1682 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001683 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1684 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001685 SkPicture* pict = gmmain.generate_new_picture(
epoger@google.comc8263e72013-04-10 12:17:34 +00001686 gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
epoger@google.com80724df2013-03-21 13:49:54 +00001687 SkAutoUnref aur(pict);
1688 SkBitmap bitmap;
epoger@google.comc8263e72013-04-10 12:17:34 +00001689 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001690 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001691 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1692 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001693 }
1694 }
1695
1696 if (FLAGS_tileGrid) {
1697 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1698 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1699 SkString renderModeDescriptor("-tilegrid");
1700 if (SK_Scalar1 != replayScale) {
1701 renderModeDescriptor += "-scale-";
1702 renderModeDescriptor.appendScalar(replayScale);
1703 }
1704
1705 if ((gmFlags & GM::kSkipPicture_Flag) ||
1706 ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001707 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001708 renderModeDescriptor.c_str());
1709 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1710 } else {
1711 // We record with the reciprocal scale to obtain a replay
1712 // result that can be validated against comparisonBitmap.
1713 SkScalar recordScale = SkScalarInvert(replayScale);
1714 SkPicture* pict = gmmain.generate_new_picture(
1715 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag,
1716 recordScale);
1717 SkAutoUnref aur(pict);
1718 SkBitmap bitmap;
1719 // We cannot yet pass 'true' to generate_image_from_picture to
1720 // perform actual tiled rendering (see Issue 1198 -
1721 // https://code.google.com/p/skia/issues/detail?id=1198)
1722 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
1723 replayScale /*, true */);
1724 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001725 gm->shortName(), compareConfig.fName, renderModeDescriptor.c_str(), bitmap,
1726 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001727 }
epoger@google.com80724df2013-03-21 13:49:54 +00001728 }
1729 }
1730
1731 // run the pipe centric GM steps
epoger@google.comc8263e72013-04-10 12:17:34 +00001732 if (FLAGS_pipe) {
1733 errorsForAllModes.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap,
1734 FLAGS_simulatePipePlaybackFailure));
1735 if (FLAGS_tiledPipe) {
1736 errorsForAllModes.add(gmmain.test_tiled_pipe_playback(gm, compareConfig,
1737 comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001738 }
epoger@google.com80724df2013-03-21 13:49:54 +00001739 }
1740 return errorsForAllModes;
1741}
1742
epoger@google.com310478e2013-04-03 18:00:39 +00001743/**
1744 * Return a list of all entries in an array of strings as a single string
1745 * of this form:
1746 * "item1", "item2", "item3"
1747 */
1748SkString list_all(const SkTArray<SkString> &stringArray);
1749SkString list_all(const SkTArray<SkString> &stringArray) {
1750 SkString total;
1751 for (int i = 0; i < stringArray.count(); i++) {
1752 if (i > 0) {
1753 total.append(", ");
1754 }
1755 total.append("\"");
1756 total.append(stringArray[i]);
1757 total.append("\"");
1758 }
1759 return total;
1760}
1761
1762/**
1763 * Return a list of configuration names, as a single string of this form:
1764 * "item1", "item2", "item3"
1765 *
1766 * @param configs configurations, as a list of indices into gRec
1767 */
1768SkString list_all_config_names(const SkTDArray<size_t> &configs);
1769SkString list_all_config_names(const SkTDArray<size_t> &configs) {
1770 SkString total;
1771 for (int i = 0; i < configs.count(); i++) {
1772 if (i > 0) {
1773 total.append(", ");
1774 }
1775 total.append("\"");
1776 total.append(gRec[configs[i]].fName);
1777 total.append("\"");
1778 }
1779 return total;
1780}
1781
epoger@google.comfdea3252013-05-02 18:24:03 +00001782bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1783 const SkTDArray<size_t> &configs);
1784bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1785 const SkTDArray<size_t> &configs) {
1786 if (!sk_mkdir(root)) {
1787 return false;
1788 }
1789 if (useFileHierarchy) {
1790 for (int i = 0; i < configs.count(); i++) {
1791 ConfigData config = gRec[configs[i]];
1792 SkString subdir;
1793 subdir.appendf("%s%c%s", root, SkPATH_SEPARATOR, config.fName);
1794 if (!sk_mkdir(subdir.c_str())) {
1795 return false;
1796 }
1797 }
1798 }
1799 return true;
1800}
1801
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001802int tool_main(int argc, char** argv);
1803int tool_main(int argc, char** argv) {
1804
1805#if SK_ENABLE_INST_COUNT
1806 gPrintInstCount = true;
1807#endif
1808
1809 SkGraphics::Init();
1810 // we don't need to see this during a run
1811 gSkSuppressFontCachePurgeSpew = true;
1812
1813 setSystemPreferences();
1814 GMMain gmmain;
1815
1816 SkTDArray<size_t> configs;
1817 SkTDArray<size_t> excludeConfigs;
1818 bool userConfig = false;
1819
1820 SkString usage;
1821 usage.printf("Run the golden master tests.\n");
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00001822 SkCommandLineFlags::SetUsage(usage.c_str());
1823 SkCommandLineFlags::Parse(argc, argv);
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001824
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001825 gmmain.fUseFileHierarchy = FLAGS_hierarchy;
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001826 gmmain.fWriteChecksumBasedFilenames = FLAGS_writeChecksumBasedFilenames;
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001827 if (FLAGS_mismatchPath.count() == 1) {
1828 gmmain.fMismatchPath = FLAGS_mismatchPath[0];
1829 }
epoger@google.com5f995452013-06-21 18:16:47 +00001830 if (FLAGS_missingExpectationsPath.count() == 1) {
1831 gmmain.fMissingExpectationsPath = FLAGS_missingExpectationsPath[0];
1832 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001833
1834 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001835 const char* config = FLAGS_config[i];
1836 userConfig = true;
1837 bool exclude = false;
1838 if (*config == kExcludeConfigChar) {
1839 exclude = true;
1840 config += 1;
1841 }
1842 int index = findConfig(config);
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001843 if (index >= 0) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001844 if (exclude) {
1845 *excludeConfigs.append() = index;
1846 } else {
1847 appendUnique<size_t>(&configs, index);
1848 }
1849 } else if (0 == strcmp(kDefaultsConfigStr, config)) {
1850 for (size_t c = 0; c < SK_ARRAY_COUNT(gRec); ++c) {
1851 if (gRec[c].fRunByDefault) {
1852 if (exclude) {
1853 gm_fprintf(stderr, "%c%s is not allowed.\n",
1854 kExcludeConfigChar, kDefaultsConfigStr);
1855 return -1;
1856 } else {
1857 appendUnique<size_t>(&configs, c);
1858 }
1859 }
1860 }
scroggo@google.com0b735632013-03-19 17:38:50 +00001861 } else {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001862 gm_fprintf(stderr, "unrecognized config %s\n", config);
scroggo@google.com7d519302013-03-19 17:28:10 +00001863 return -1;
1864 }
1865 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001866
1867 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1868 int index = findConfig(FLAGS_excludeConfig[i]);
1869 if (index >= 0) {
1870 *excludeConfigs.append() = index;
1871 } else {
1872 gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1873 return -1;
1874 }
1875 }
1876
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001877 int moduloRemainder = -1;
1878 int moduloDivisor = -1;
1879
1880 if (FLAGS_modulo.count() == 2) {
1881 moduloRemainder = atoi(FLAGS_modulo[0]);
1882 moduloDivisor = atoi(FLAGS_modulo[1]);
1883 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
1884 gm_fprintf(stderr, "invalid modulo values.");
1885 return -1;
1886 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001887 }
reed@google.com873cb1e2010-12-23 15:00:45 +00001888
epoger@google.com5079d2c2013-04-12 14:11:21 +00001889 if (FLAGS_ignoreErrorTypes.count() > 0) {
1890 gmmain.fIgnorableErrorTypes = ErrorCombination();
1891 for (int i = 0; i < FLAGS_ignoreErrorTypes.count(); i++) {
1892 ErrorType type;
1893 const char *name = FLAGS_ignoreErrorTypes[i];
1894 if (!getErrorTypeByName(name, &type)) {
1895 gm_fprintf(stderr, "cannot find ErrorType with name '%s'\n", name);
1896 return -1;
1897 } else {
1898 gmmain.fIgnorableErrorTypes.add(type);
1899 }
1900 }
1901 }
1902
epoger@google.com6f6568b2013-03-22 17:29:46 +00001903#if SK_SUPPORT_GPU
1904 if (FLAGS_gpuCacheSize.count() > 0) {
1905 if (FLAGS_gpuCacheSize.count() != 2) {
1906 gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
1907 return -1;
1908 }
1909 gGpuCacheSizeBytes = atoi(FLAGS_gpuCacheSize[0]);
1910 gGpuCacheSizeCount = atoi(FLAGS_gpuCacheSize[1]);
1911 } else {
1912 gGpuCacheSizeBytes = DEFAULT_CACHE_VALUE;
1913 gGpuCacheSizeCount = DEFAULT_CACHE_VALUE;
1914 }
1915#endif
1916
1917 SkTDArray<SkScalar> tileGridReplayScales;
1918 *tileGridReplayScales.append() = SK_Scalar1; // By default only test at scale 1.0
1919 if (FLAGS_tileGridReplayScales.count() > 0) {
1920 tileGridReplayScales.reset();
1921 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
1922 double val = atof(FLAGS_tileGridReplayScales[i]);
1923 if (0 < val) {
1924 *tileGridReplayScales.append() = SkDoubleToScalar(val);
1925 }
1926 }
1927 if (0 == tileGridReplayScales.count()) {
1928 // Should have at least one scale
1929 gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
1930 return -1;
1931 }
1932 }
1933
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001934 if (!userConfig) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001935 // if no config is specified by user, add the defaults
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001936 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001937 if (gRec[i].fRunByDefault) {
1938 *configs.append() = i;
1939 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001940 }
1941 }
reed@google.comfb2cd422013-01-04 14:43:03 +00001942 // now remove any explicitly excluded configs
1943 for (int i = 0; i < excludeConfigs.count(); ++i) {
1944 int index = configs.find(excludeConfigs[i]);
1945 if (index >= 0) {
1946 configs.remove(index);
1947 // now assert that there was only one copy in configs[]
1948 SkASSERT(configs.find(excludeConfigs[i]) < 0);
1949 }
1950 }
1951
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001952#if SK_SUPPORT_GPU
1953 GrContextFactory* grFactory = new GrContextFactory;
1954 for (int i = 0; i < configs.count(); ++i) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001955 size_t index = configs[i];
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001956 if (kGPU_Backend == gRec[index].fBackend) {
1957 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1958 if (NULL == ctx) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001959 gm_fprintf(stderr, "GrContext could not be created for config %s."
1960 " Config will be skipped.\n", gRec[index].fName);
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001961 configs.remove(i);
1962 --i;
bsalomon@google.com42db2e42013-06-11 19:22:44 +00001963 continue;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001964 }
1965 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001966 gm_fprintf(stderr, "Sample count (%d) of config %s is not supported."
1967 " Config will be skipped.\n", gRec[index].fSampleCnt, gRec[index].fName);
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001968 configs.remove(i);
1969 --i;
1970 }
1971 }
1972 }
epoger@google.com80724df2013-03-21 13:49:54 +00001973#else
1974 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001975#endif
1976
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001977 if (configs.isEmpty()) {
1978 gm_fprintf(stderr, "No configs to run.");
1979 return -1;
1980 }
1981
1982 // now show the user the set of configs that will be run.
1983 SkString configStr("These configs will be run: ");
1984 // show the user the config that will run.
1985 for (int i = 0; i < configs.count(); ++i) {
1986 configStr.appendf("%s%s", gRec[configs[i]].fName, (i == configs.count() - 1) ? "\n" : " ");
1987 }
1988 gm_fprintf(stdout, "%s", configStr.c_str());
1989
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001990 if (FLAGS_resourcePath.count() == 1) {
1991 GM::SetResourcePath(FLAGS_resourcePath[0]);
1992 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001993
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001994 if (FLAGS_readPath.count() == 1) {
1995 const char* readPath = FLAGS_readPath[0];
epoger@google.com37269602013-01-19 04:21:27 +00001996 if (!sk_exists(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001997 gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001998 return -1;
1999 }
2000 if (sk_isdir(readPath)) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00002001 if (FLAGS_verbose) {
2002 gm_fprintf(stdout, "reading from %s\n", readPath);
2003 }
epoger@google.com37269602013-01-19 04:21:27 +00002004 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
epoger@google.comb0f8b432013-04-10 18:46:25 +00002005 IndividualImageExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00002006 } else {
epoger@google.comb0f8b432013-04-10 18:46:25 +00002007 if (FLAGS_verbose) {
2008 gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
2009 }
epoger@google.comd271d242013-02-13 18:14:48 +00002010 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
2011 JsonExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00002012 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00002013 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00002014 if (FLAGS_verbose) {
2015 if (FLAGS_writePath.count() == 1) {
2016 gm_fprintf(stdout, "writing to %s\n", FLAGS_writePath[0]);
2017 }
epoger@google.comfdea3252013-05-02 18:24:03 +00002018 if (NULL != gmmain.fMismatchPath) {
2019 gm_fprintf(stdout, "writing mismatches to %s\n", gmmain.fMismatchPath);
2020 }
epoger@google.com5f995452013-06-21 18:16:47 +00002021 if (NULL != gmmain.fMissingExpectationsPath) {
2022 gm_fprintf(stdout, "writing images without expectations to %s\n",
2023 gmmain.fMissingExpectationsPath);
2024 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00002025 if (FLAGS_writePicturePath.count() == 1) {
2026 gm_fprintf(stdout, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
2027 }
2028 if (FLAGS_resourcePath.count() == 1) {
2029 gm_fprintf(stdout, "reading resources from %s\n", FLAGS_resourcePath[0]);
2030 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00002031 }
2032
epoger@google.com82cb65b2012-10-29 18:59:17 +00002033 if (moduloDivisor <= 0) {
2034 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00002035 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00002036 if (moduloRemainder < 0 || moduloRemainder >= moduloDivisor) {
2037 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00002038 }
2039
epoger@google.com310478e2013-04-03 18:00:39 +00002040 int gmsRun = 0;
reed@google.comae7b8f32012-10-18 21:30:57 +00002041 int gmIndex = -1;
2042 SkString moduloStr;
2043
epoger@google.come8ebeb12012-10-29 16:42:11 +00002044 // If we will be writing out files, prepare subdirectories.
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002045 if (FLAGS_writePath.count() == 1) {
epoger@google.comfdea3252013-05-02 18:24:03 +00002046 if (!prepare_subdirectories(FLAGS_writePath[0], gmmain.fUseFileHierarchy, configs)) {
epoger@google.come8ebeb12012-10-29 16:42:11 +00002047 return -1;
2048 }
epoger@google.comfdea3252013-05-02 18:24:03 +00002049 }
2050 if (NULL != gmmain.fMismatchPath) {
2051 if (!prepare_subdirectories(gmmain.fMismatchPath, gmmain.fUseFileHierarchy, configs)) {
2052 return -1;
epoger@google.come8ebeb12012-10-29 16:42:11 +00002053 }
2054 }
epoger@google.com5f995452013-06-21 18:16:47 +00002055 if (NULL != gmmain.fMissingExpectationsPath) {
2056 if (!prepare_subdirectories(gmmain.fMissingExpectationsPath, gmmain.fUseFileHierarchy,
2057 configs)) {
2058 return -1;
2059 }
2060 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00002061
edisonn@google.comd9dfa182013-04-24 13:01:01 +00002062 if (FLAGS_pdfJpegQuality < -1 || FLAGS_pdfJpegQuality > 100) {
2063 gm_fprintf(stderr, "%s\n", "pdfJpegQuality must be in [-1 .. 100] range.");
2064 }
2065
bsalomon@google.com7361f542012-04-19 19:15:35 +00002066 Iter iter;
2067 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00002068 while ((gm = iter.next()) != NULL) {
scroggo@google.com7fd2d702013-04-16 19:11:14 +00002069 SkAutoTDelete<GM> adgm(gm);
reed@google.comae7b8f32012-10-18 21:30:57 +00002070 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00002071 if (moduloRemainder >= 0) {
2072 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00002073 continue;
2074 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00002075 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00002076 }
2077
reed@google.comece2b022011-07-25 14:28:57 +00002078 const char* shortName = gm->shortName();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002079 if (skip_name(FLAGS_match, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00002080 continue;
2081 }
2082
epoger@google.com310478e2013-04-03 18:00:39 +00002083 gmsRun++;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00002084 SkISize size = gm->getISize();
epoger@google.combcbf5aa2013-04-12 02:11:54 +00002085 gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
2086 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00002087
epoger@google.com310478e2013-04-03 18:00:39 +00002088 run_multiple_configs(gmmain, gm, configs, grFactory);
djsollen@google.comebce16d2012-10-26 14:07:13 +00002089
2090 SkBitmap comparisonBitmap;
2091 const ConfigData compareConfig =
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002092 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
epoger@google.com310478e2013-04-03 18:00:39 +00002093 gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false);
djsollen@google.comebce16d2012-10-26 14:07:13 +00002094
epoger@google.com80724df2013-03-21 13:49:54 +00002095 // TODO(epoger): only run this if gmmain.generate_image() succeeded?
2096 // Otherwise, what are we comparing against?
epoger@google.com310478e2013-04-03 18:00:39 +00002097 run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap, tileGridReplayScales);
reed@android.com00dae862009-06-10 15:38:48 +00002098 }
epoger@google.com310478e2013-04-03 18:00:39 +00002099
2100 SkTArray<SkString> modes;
2101 gmmain.GetRenderModesEncountered(modes);
epoger@google.comc8263e72013-04-10 12:17:34 +00002102 bool reportError = false;
2103 if (gmmain.NumSignificantErrors() > 0) {
2104 reportError = true;
2105 }
epoger@google.com51dbabe2013-04-10 15:24:53 +00002106 int expectedNumberOfTests = gmsRun * (configs.count() + modes.count());
epoger@google.com310478e2013-04-03 18:00:39 +00002107
2108 // Output summary to stdout.
epoger@google.com51dbabe2013-04-10 15:24:53 +00002109 if (FLAGS_verbose) {
2110 gm_fprintf(stdout, "Ran %d GMs\n", gmsRun);
2111 gm_fprintf(stdout, "... over %2d configs [%s]\n", configs.count(),
2112 list_all_config_names(configs).c_str());
2113 gm_fprintf(stdout, "... and %2d modes [%s]\n", modes.count(), list_all(modes).c_str());
2114 gm_fprintf(stdout, "... so there should be a total of %d tests.\n", expectedNumberOfTests);
2115 }
2116 gmmain.ListErrors(FLAGS_verbose);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002117
epoger@google.com07947d92013-04-11 15:41:02 +00002118 // TODO(epoger): Enable this check for Android, too, once we resolve
2119 // https://code.google.com/p/skia/issues/detail?id=1222
2120 // ('GM is unexpectedly skipping tests on Android')
2121#ifndef SK_BUILD_FOR_ANDROID
epoger@google.comc8263e72013-04-10 12:17:34 +00002122 if (expectedNumberOfTests != gmmain.fTestsRun) {
2123 gm_fprintf(stderr, "expected %d tests, but ran or skipped %d tests\n",
2124 expectedNumberOfTests, gmmain.fTestsRun);
2125 reportError = true;
2126 }
2127#endif
2128
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002129 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.com76c913d2013-04-26 15:06:44 +00002130 Json::Value root = CreateJsonTree(
2131 gmmain.fJsonExpectedResults,
2132 gmmain.fJsonActualResults_Failed, gmmain.fJsonActualResults_FailureIgnored,
2133 gmmain.fJsonActualResults_NoComparison, gmmain.fJsonActualResults_Succeeded);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002134 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002135 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002136 stream.write(jsonStdString.c_str(), jsonStdString.length());
2137 }
2138
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002139#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002140
robertphillips@google.com59552022012-08-31 13:07:37 +00002141#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002142 for (int i = 0; i < configs.count(); i++) {
2143 ConfigData config = gRec[configs[i]];
2144
epoger@google.comb0f8b432013-04-10 18:46:25 +00002145 if (FLAGS_verbose && (kGPU_Backend == config.fBackend)) {
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002146 GrContext* gr = grFactory->get(config.fGLContextType);
2147
epoger@google.com5efdd0c2013-03-13 14:18:40 +00002148 gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002149 gr->printCacheStats();
2150 }
2151 }
2152#endif
2153
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002154 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002155#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002156 SkGraphics::Term();
2157
epoger@google.comc8263e72013-04-10 12:17:34 +00002158 return (reportError) ? -1 : 0;
reed@android.com00dae862009-06-10 15:38:48 +00002159}
caryclark@google.com5987f582012-10-02 18:33:14 +00002160
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002161void GMMain::installFilter(SkCanvas* canvas) {
2162 if (FLAGS_forceBWtext) {
2163 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
2164 }
2165}
2166
borenet@google.com7158e6a2012-11-01 17:43:44 +00002167#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00002168int main(int argc, char * const argv[]) {
2169 return tool_main(argc, (char**) argv);
2170}
2171#endif