blob: 773e1e769c72811cf458aac59278b3cd1a3d2c04 [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
epoger@google.comed5eb4e2013-07-23 17:56:20 +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) {
robertphillips@google.com9b051a32013-08-20 20:06:40 +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 canvas.reset(SkDeferredCanvas::Create(device));
epoger@google.comde961632012-10-26 18:56:36 +0000523 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000524 canvas.reset(SkNEW_ARGS(SkCanvas, (device)));
epoger@google.comde961632012-10-26 18:56:36 +0000525 }
reed@google.comaef73612012-11-16 13:41:45 +0000526 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000527 canvas->flush();
528 }
529#if SK_SUPPORT_GPU
530 else { // GPU
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000531 SkAutoTUnref<SkDevice> device(SkGpuDevice::Create(gpuTarget));
epoger@google.comde961632012-10-26 18:56:36 +0000532 if (deferred) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000533 canvas.reset(SkDeferredCanvas::Create(device));
epoger@google.comde961632012-10-26 18:56:36 +0000534 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000535 canvas.reset(SkNEW_ARGS(SkCanvas, (device)));
epoger@google.comde961632012-10-26 18:56:36 +0000536 }
reed@google.comaef73612012-11-16 13:41:45 +0000537 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000538 // the device is as large as the current rendertarget, so
539 // we explicitly only readback the amount we expect (in
540 // size) overwrite our previous allocation
541 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
542 size.fHeight);
543 canvas->readPixels(bitmap, 0, 0);
544 }
545#endif
epoger@google.com5f6a0072013-01-31 16:30:55 +0000546 complete_bitmap(bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000547 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000548 }
549
550 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
junov@chromium.orgc938c482012-12-19 15:24:38 +0000551 SkPicture* pict, SkBitmap* bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000552 SkScalar scale = SK_Scalar1,
553 bool tile = false) {
epoger@google.comde961632012-10-26 18:56:36 +0000554 SkISize size = gm->getISize();
555 setup_bitmap(gRec, size, bitmap);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000556
557 if (tile) {
558 // Generate the result image by rendering to tiles and accumulating
559 // the results in 'bitmap'
560
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000561 // This 16x16 tiling matches the settings applied to 'pict' in
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000562 // 'generate_new_picture'
563 SkISize tileSize = SkISize::Make(16, 16);
564
565 SkBitmap tileBM;
566 setup_bitmap(gRec, tileSize, &tileBM);
567 SkCanvas tileCanvas(tileBM);
568 installFilter(&tileCanvas);
569
570 SkCanvas bmpCanvas(*bitmap);
571 SkPaint bmpPaint;
572 bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
573
574 for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
575 for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
576 int saveCount = tileCanvas.save();
577 SkMatrix mat(tileCanvas.getTotalMatrix());
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000578 mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000579 SkIntToScalar(-yTile*tileSize.height()));
580 tileCanvas.setMatrix(mat);
581 pict->draw(&tileCanvas);
582 tileCanvas.flush();
583 tileCanvas.restoreToCount(saveCount);
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000584 bmpCanvas.drawBitmap(tileBM,
585 SkIntToScalar(xTile * tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000586 SkIntToScalar(yTile * tileSize.height()),
587 &bmpPaint);
588 }
589 }
590 } else {
591 SkCanvas canvas(*bitmap);
592 installFilter(&canvas);
593 canvas.scale(scale, scale);
594 canvas.drawPicture(*pict);
595 complete_bitmap(bitmap);
596 }
epoger@google.comde961632012-10-26 18:56:36 +0000597 }
598
599 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
600#ifdef SK_SUPPORT_PDF
601 SkMatrix initialTransform = gm->getInitialTransform();
602 SkISize pageSize = gm->getISize();
603 SkPDFDevice* dev = NULL;
604 if (initialTransform.isIdentity()) {
605 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
606 } else {
607 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
608 SkIntToScalar(pageSize.height()));
609 initialTransform.mapRect(&content);
610 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
611 SkIntToScalar(pageSize.height()));
612 SkISize contentSize =
613 SkISize::Make(SkScalarRoundToInt(content.width()),
614 SkScalarRoundToInt(content.height()));
615 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
616 }
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000617 dev->setDCTEncoder(encode_to_dct_stream);
epoger@google.comde961632012-10-26 18:56:36 +0000618 SkAutoUnref aur(dev);
619
620 SkCanvas c(dev);
reed@google.comaef73612012-11-16 13:41:45 +0000621 invokeGM(gm, &c, true, false);
epoger@google.comde961632012-10-26 18:56:36 +0000622
623 SkPDFDocument doc;
624 doc.appendPage(dev);
625 doc.emitPDF(&pdf);
626#endif
627 }
628
629 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
630#ifdef SK_SUPPORT_XPS
631 SkISize size = gm->getISize();
632
633 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
634 SkIntToScalar(size.height()));
635 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
636 static const SkScalar upm = 72 * inchesPerMeter;
637 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
638 static const SkScalar ppm = 200 * inchesPerMeter;
639 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
640
641 SkXPSDevice* dev = new SkXPSDevice();
642 SkAutoUnref aur(dev);
643
644 SkCanvas c(dev);
645 dev->beginPortfolio(&xps);
646 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000647 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000648 dev->endSheet();
649 dev->endPortfolio();
650
651#endif
652 }
653
epoger@google.com6f6568b2013-03-22 17:29:46 +0000654 ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
epoger@google.com659c8c02013-05-21 15:45:45 +0000655 const char renderModeDescriptor [],
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000656 const char *shortName,
epoger@google.come33e1372013-07-08 19:13:33 +0000657 const BitmapAndDigest* bitmapAndDigest,
bungeman@google.com88682b72013-07-19 13:55:41 +0000658 SkStreamAsset* document) {
epoger@google.comde961632012-10-26 18:56:36 +0000659 SkString path;
660 bool success = false;
661 if (gRec.fBackend == kRaster_Backend ||
662 gRec.fBackend == kGPU_Backend ||
663 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
664
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000665 path = make_bitmap_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.come33e1372013-07-08 19:13:33 +0000666 bitmapAndDigest->fDigest);
667 success = write_bitmap(path, bitmapAndDigest->fBitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000668 }
669 if (kPDF_Backend == gRec.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000670 path = make_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000671 "pdf");
bungeman@google.com88682b72013-07-19 13:55:41 +0000672 success = write_document(path, document);
epoger@google.comde961632012-10-26 18:56:36 +0000673 }
674 if (kXPS_Backend == gRec.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000675 path = make_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000676 "xps");
bungeman@google.com88682b72013-07-19 13:55:41 +0000677 success = write_document(path, document);
epoger@google.comde961632012-10-26 18:56:36 +0000678 }
679 if (success) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000680 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000681 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000682 gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000683 ErrorCombination errors(kWritingReferenceImage_ErrorType);
epoger@google.com310478e2013-04-03 18:00:39 +0000684 // TODO(epoger): Don't call RecordTestResults() here...
685 // Instead, we should make sure to call RecordTestResults
686 // exactly ONCE per test. (Otherwise, gmmain.fTestsRun
687 // will be incremented twice for this test: once in
688 // compare_test_results_to_stored_expectations() before
689 // that method calls this one, and again here.)
690 //
691 // When we make that change, we should probably add a
692 // WritingReferenceImage test to the gm self-tests.)
epoger@google.com659c8c02013-05-21 15:45:45 +0000693 RecordTestResults(errors, make_shortname_plus_config(shortName, gRec.fName),
694 renderModeDescriptor);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000695 return errors;
epoger@google.comde961632012-10-26 18:56:36 +0000696 }
697 }
698
epoger@google.com37269602013-01-19 04:21:27 +0000699 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000700 * Log more detail about the mistmatch between expectedBitmap and
701 * actualBitmap.
702 */
703 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
704 const char *testName) {
705 const int expectedWidth = expectedBitmap.width();
706 const int expectedHeight = expectedBitmap.height();
707 const int width = actualBitmap.width();
708 const int height = actualBitmap.height();
709 if ((expectedWidth != width) || (expectedHeight != height)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000710 gm_fprintf(stderr, "---- %s: dimension mismatch --"
711 " expected [%d %d], actual [%d %d]\n",
712 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000713 return;
714 }
715
716 if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
717 (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000718 gm_fprintf(stderr, "---- %s: not computing max per-channel"
719 " pixel mismatch because non-8888\n", testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000720 return;
721 }
722
723 SkAutoLockPixels alp0(expectedBitmap);
724 SkAutoLockPixels alp1(actualBitmap);
725 int errR = 0;
726 int errG = 0;
727 int errB = 0;
728 int errA = 0;
729 int differingPixels = 0;
730
731 for (int y = 0; y < height; ++y) {
732 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
733 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
734 for (int x = 0; x < width; ++x) {
735 SkPMColor expectedPixel = *expectedPixelPtr++;
736 SkPMColor actualPixel = *actualPixelPtr++;
737 if (expectedPixel != actualPixel) {
738 differingPixels++;
739 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
740 (int)SkGetPackedR32(actualPixel)));
741 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
742 (int)SkGetPackedG32(actualPixel)));
743 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
744 (int)SkGetPackedB32(actualPixel)));
745 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
746 (int)SkGetPackedA32(actualPixel)));
747 }
748 }
749 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000750 gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
751 " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
752 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000753 }
754
755 /**
epoger@google.com908f5832013-04-12 02:23:55 +0000756 * Compares actual hash digest to expectations, returning the set of errors
epoger@google.com6f6568b2013-03-22 17:29:46 +0000757 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000758 *
759 * If fMismatchPath has been set, and there are pixel diffs, then the
760 * actual bitmap will be written out to a file within fMismatchPath.
epoger@google.com5f995452013-06-21 18:16:47 +0000761 * And similarly for fMissingExpectationsPath...
epoger@google.com37269602013-01-19 04:21:27 +0000762 *
763 * @param expectations what expectations to compare actualBitmap against
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000764 * @param actualBitmapAndDigest the SkBitmap we actually generated, and its GmResultDigest
epoger@google.com659c8c02013-05-21 15:45:45 +0000765 * @param shortName name of test, e.g. "selftest1"
766 * @param configName name of config, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000767 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
768 * @param addToJsonSummary whether to add these results (both actual and
epoger@google.comcaac3db2013-04-04 19:23:11 +0000769 * expected) to the JSON summary. Regardless of this setting, if
770 * we find an image mismatch in this test, we will write these
771 * results to the JSON summary. (This is so that we will always
772 * report errors across rendering modes, such as pipe vs tiled.
773 * See https://codereview.chromium.org/13650002/ )
epoger@google.com37269602013-01-19 04:21:27 +0000774 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000775 ErrorCombination compare_to_expectations(Expectations expectations,
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000776 const BitmapAndDigest& actualBitmapAndDigest,
epoger@google.com659c8c02013-05-21 15:45:45 +0000777 const char *shortName, const char *configName,
778 const char *renderModeDescriptor,
epoger@google.comcaac3db2013-04-04 19:23:11 +0000779 bool addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000780 ErrorCombination errors;
epoger@google.com659c8c02013-05-21 15:45:45 +0000781 SkString shortNamePlusConfig = make_shortname_plus_config(shortName, configName);
782 SkString completeNameString(shortNamePlusConfig);
epoger@google.com37269602013-01-19 04:21:27 +0000783 completeNameString.append(renderModeDescriptor);
epoger@google.comce057fe2013-05-14 15:17:46 +0000784 completeNameString.append(".");
785 completeNameString.append(kPNG_FileExtension);
epoger@google.com37269602013-01-19 04:21:27 +0000786 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000787
epoger@google.com37269602013-01-19 04:21:27 +0000788 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000789 errors.add(kMissingExpectations_ErrorType);
epoger@google.com5f995452013-06-21 18:16:47 +0000790
791 // Write out the "actuals" for any tests without expectations, if we have
792 // been directed to do so.
793 if (fMissingExpectationsPath) {
794 SkString path = make_bitmap_filename(fMissingExpectationsPath, shortName,
795 configName, renderModeDescriptor,
796 actualBitmapAndDigest.fDigest);
797 write_bitmap(path, actualBitmapAndDigest.fBitmap);
798 }
799
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000800 } else if (!expectations.match(actualBitmapAndDigest.fDigest)) {
epoger@google.comcaac3db2013-04-04 19:23:11 +0000801 addToJsonSummary = true;
802 // The error mode we record depends on whether this was running
803 // in a non-standard renderMode.
804 if ('\0' == *renderModeDescriptor) {
805 errors.add(kExpectationsMismatch_ErrorType);
806 } else {
807 errors.add(kRenderModeMismatch_ErrorType);
808 }
epoger@google.com84a18022013-02-01 20:39:15 +0000809
810 // Write out the "actuals" for any mismatches, if we have
811 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000812 if (fMismatchPath) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000813 SkString path = make_bitmap_filename(fMismatchPath, shortName, configName,
814 renderModeDescriptor,
815 actualBitmapAndDigest.fDigest);
816 write_bitmap(path, actualBitmapAndDigest.fBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000817 }
epoger@google.com84a18022013-02-01 20:39:15 +0000818
819 // If we have access to a single expected bitmap, log more
820 // detail about the mismatch.
821 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
822 if (NULL != expectedBitmapPtr) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000823 report_bitmap_diffs(*expectedBitmapPtr, actualBitmapAndDigest.fBitmap,
824 completeName);
epoger@google.com84a18022013-02-01 20:39:15 +0000825 }
epoger@google.coma243b222013-01-17 17:54:28 +0000826 }
epoger@google.com659c8c02013-05-21 15:45:45 +0000827 RecordTestResults(errors, shortNamePlusConfig, renderModeDescriptor);
epoger@google.coma243b222013-01-17 17:54:28 +0000828
epoger@google.com37269602013-01-19 04:21:27 +0000829 if (addToJsonSummary) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000830 add_actual_results_to_json_summary(completeName, actualBitmapAndDigest.fDigest, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000831 expectations.ignoreFailure());
832 add_expected_results_to_json_summary(completeName, expectations);
833 }
epoger@google.coma243b222013-01-17 17:54:28 +0000834
epoger@google.com6f6568b2013-03-22 17:29:46 +0000835 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000836 }
837
epoger@google.com37269602013-01-19 04:21:27 +0000838 /**
839 * Add this result to the appropriate JSON collection of actual results,
epoger@google.com76c913d2013-04-26 15:06:44 +0000840 * depending on errors encountered.
epoger@google.com37269602013-01-19 04:21:27 +0000841 */
842 void add_actual_results_to_json_summary(const char testName[],
epoger@google.comd4993ff2013-05-24 14:33:28 +0000843 const GmResultDigest &actualResultDigest,
epoger@google.com76c913d2013-04-26 15:06:44 +0000844 ErrorCombination errors,
epoger@google.com37269602013-01-19 04:21:27 +0000845 bool ignoreFailure) {
epoger@google.comd4993ff2013-05-24 14:33:28 +0000846 Json::Value jsonActualResults = actualResultDigest.asJsonTypeValuePair();
epoger@google.com76c913d2013-04-26 15:06:44 +0000847 if (errors.isEmpty()) {
848 this->fJsonActualResults_Succeeded[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000849 } else {
850 if (ignoreFailure) {
851 // TODO: Once we have added the ability to compare
852 // actual results against expectations in a JSON file
853 // (where we can set ignore-failure to either true or
epoger@google.com84a18022013-02-01 20:39:15 +0000854 // false), add test cases that exercise ignored
epoger@google.com6f6568b2013-03-22 17:29:46 +0000855 // failures (both for kMissingExpectations_ErrorType
epoger@google.comcaac3db2013-04-04 19:23:11 +0000856 // and kExpectationsMismatch_ErrorType).
epoger@google.com37269602013-01-19 04:21:27 +0000857 this->fJsonActualResults_FailureIgnored[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000858 jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000859 } else {
epoger@google.com76c913d2013-04-26 15:06:44 +0000860 if (errors.includes(kMissingExpectations_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000861 // TODO: What about the case where there IS an
epoger@google.com908f5832013-04-12 02:23:55 +0000862 // expected image hash digest, but that gm test
epoger@google.com37269602013-01-19 04:21:27 +0000863 // doesn't actually run? For now, those cases
864 // will always be ignored, because gm only looks
865 // at expectations that correspond to gm tests
866 // that were actually run.
867 //
868 // Once we have the ability to express
869 // expectations as a JSON file, we should fix this
870 // (and add a test case for which an expectation
871 // is given but the test is never run).
872 this->fJsonActualResults_NoComparison[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000873 jsonActualResults;
epoger@google.comeb066362013-03-08 09:39:36 +0000874 }
epoger@google.com76c913d2013-04-26 15:06:44 +0000875 if (errors.includes(kExpectationsMismatch_ErrorType) ||
876 errors.includes(kRenderModeMismatch_ErrorType)) {
877 this->fJsonActualResults_Failed[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000878 }
879 }
880 }
881 }
882
883 /**
884 * Add this test to the JSON collection of expected results.
885 */
886 void add_expected_results_to_json_summary(const char testName[],
887 Expectations expectations) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000888 this->fJsonExpectedResults[testName] = expectations.asJsonValue();
epoger@google.com37269602013-01-19 04:21:27 +0000889 }
890
891 /**
892 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
893 *
894 * @param gm which test generated the actualBitmap
895 * @param gRec
896 * @param writePath unless this is NULL, write out actual images into this
897 * directory
epoger@google.come33e1372013-07-08 19:13:33 +0000898 * @param actualBitmapAndDigest ptr to bitmap generated by this run, or NULL
899 * if we don't have a usable bitmap representation
900 * @param document pdf or xps representation, if appropriate
epoger@google.com37269602013-01-19 04:21:27 +0000901 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000902 ErrorCombination compare_test_results_to_stored_expectations(
epoger@google.com37269602013-01-19 04:21:27 +0000903 GM* gm, const ConfigData& gRec, const char writePath[],
bungeman@google.com88682b72013-07-19 13:55:41 +0000904 const BitmapAndDigest* actualBitmapAndDigest, SkStreamAsset* document) {
epoger@google.com37269602013-01-19 04:21:27 +0000905
epoger@google.com659c8c02013-05-21 15:45:45 +0000906 SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(), gRec.fName);
907 SkString nameWithExtension(shortNamePlusConfig);
908 nameWithExtension.append(".");
909 nameWithExtension.append(kPNG_FileExtension);
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000910
epoger@google.com659c8c02013-05-21 15:45:45 +0000911 ErrorCombination errors;
epoger@google.come33e1372013-07-08 19:13:33 +0000912
913 if (NULL == actualBitmapAndDigest) {
914 // Note that we intentionally skipped validating the results for
915 // this test, because we don't know how to generate an SkBitmap
916 // version of the output.
917 RecordTestResults(ErrorCombination(kIntentionallySkipped_ErrorType),
epoger@google.com659c8c02013-05-21 15:45:45 +0000918 shortNamePlusConfig, "");
epoger@google.comc824c832013-07-12 15:52:59 +0000919 } else if (!(gRec.fFlags & kWrite_ConfigFlag)) {
920 // We don't record the results for this test or compare them
921 // against any expectations, because the output image isn't
922 // meaningful.
923 // See https://code.google.com/p/skia/issues/detail?id=1410 ('some
924 // GM result images not available for download from Google Storage')
925 RecordTestResults(ErrorCombination(kIntentionallySkipped_ErrorType),
926 shortNamePlusConfig, "");
epoger@google.come33e1372013-07-08 19:13:33 +0000927 } else {
928 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
929 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
930 /*
931 * Get the expected results for this test, as one or more allowed
932 * hash digests. The current implementation of expectationsSource
933 * get this by computing the hash digest of a single PNG file on disk.
934 *
935 * TODO(epoger): This relies on the fact that
936 * force_all_opaque() was called on the bitmap before it
937 * was written to disk as a PNG in the first place. If
938 * not, the hash digest returned here may not match the
939 * hash digest of actualBitmap, which *has* been run through
940 * force_all_opaque().
941 * See comments above complete_bitmap() for more detail.
942 */
943 Expectations expectations = expectationsSource->get(nameWithExtension.c_str());
944 errors.add(compare_to_expectations(expectations, *actualBitmapAndDigest,
945 gm->shortName(), gRec.fName, "", true));
946 } else {
947 // If we are running without expectations, we still want to
948 // record the actual results.
949 add_actual_results_to_json_summary(nameWithExtension.c_str(),
950 actualBitmapAndDigest->fDigest,
951 ErrorCombination(kMissingExpectations_ErrorType),
952 false);
953 RecordTestResults(ErrorCombination(kMissingExpectations_ErrorType),
954 shortNamePlusConfig, "");
955 }
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000956 }
epoger@google.com37269602013-01-19 04:21:27 +0000957
958 // TODO: Consider moving this into compare_to_expectations(),
959 // similar to fMismatchPath... for now, we don't do that, because
960 // we don't want to write out the actual bitmaps for all
961 // renderModes of all tests! That would be a lot of files.
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000962 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000963 errors.add(write_reference_image(gRec, writePath, "", gm->shortName(),
epoger@google.come33e1372013-07-08 19:13:33 +0000964 actualBitmapAndDigest, document));
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000965 }
epoger@google.com37269602013-01-19 04:21:27 +0000966
epoger@google.com6f6568b2013-03-22 17:29:46 +0000967 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000968 }
969
epoger@google.com37269602013-01-19 04:21:27 +0000970 /**
971 * Compare actualBitmap to referenceBitmap.
972 *
epoger@google.com659c8c02013-05-21 15:45:45 +0000973 * @param shortName test name, e.g. "selftest1"
974 * @param configName configuration name, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000975 * @param renderModeDescriptor
976 * @param actualBitmap actual bitmap generated by this run
977 * @param referenceBitmap bitmap we expected to be generated
978 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000979 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +0000980 const char *shortName, const char *configName, const char *renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000981 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
982
983 SkASSERT(referenceBitmap);
epoger@google.com84a18022013-02-01 20:39:15 +0000984 Expectations expectations(*referenceBitmap);
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000985 BitmapAndDigest actualBitmapAndDigest(actualBitmap);
986 return compare_to_expectations(expectations, actualBitmapAndDigest, shortName,
epoger@google.com659c8c02013-05-21 15:45:45 +0000987 configName, renderModeDescriptor, false);
epoger@google.com37269602013-01-19 04:21:27 +0000988 }
989
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000990 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
991 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +0000992 // Pictures are refcounted so must be on heap
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000993 SkPicture* pict;
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000994 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
995 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +0000996
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000997 if (kTileGrid_BbhType == bbhType) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000998 SkTileGridPicture::TileGridInfo info;
999 info.fMargin.setEmpty();
1000 info.fOffset.setZero();
1001 info.fTileInterval.set(16, 16);
1002 pict = new SkTileGridPicture(width, height, info);
junov@chromium.org3cb834b2012-12-13 16:39:53 +00001003 } else {
1004 pict = new SkPicture;
1005 }
junov@chromium.org20bd04e2013-01-16 18:43:36 +00001006 if (kNone_BbhType != bbhType) {
1007 recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
1008 }
junov@chromium.org706ff2f2012-12-19 15:55:40 +00001009 SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +00001010 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +00001011 invokeGM(gm, cv, false, false);
epoger@google.comde961632012-10-26 18:56:36 +00001012 pict->endRecording();
1013
1014 return pict;
1015 }
1016
commit-bot@chromium.org805df1a2013-08-16 19:18:12 +00001017 static SkData* bitmap_encoder(size_t* pixelRefOffset, const SkBitmap& bm) {
1018 SkPixelRef* pr = bm.pixelRef();
1019 if (pr != NULL) {
1020 SkData* data = pr->refEncodedData();
1021 if (data != NULL) {
1022 *pixelRefOffset = bm.pixelRefOffset();
1023 return data;
1024 }
1025 }
1026 return NULL;
1027 }
1028
epoger@google.comde961632012-10-26 18:56:36 +00001029 static SkPicture* stream_to_new_picture(const SkPicture& src) {
epoger@google.comde961632012-10-26 18:56:36 +00001030 SkDynamicMemoryWStream storage;
commit-bot@chromium.org805df1a2013-08-16 19:18:12 +00001031 src.serialize(&storage, &bitmap_encoder);
bungeman@google.comc29f3d82013-07-19 22:32:11 +00001032 SkAutoTUnref<SkStreamAsset> pictReadback(storage.detachAsStream());
commit-bot@chromium.org805df1a2013-08-16 19:18:12 +00001033 SkPicture* retval = SkPicture::CreateFromStream(pictReadback,
1034 &SkImageDecoder::DecodeMemory);
epoger@google.comde961632012-10-26 18:56:36 +00001035 return retval;
1036 }
1037
1038 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +00001039 // Depending on flags, possibly compare to an expected image.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001040 ErrorCombination test_drawing(GM* gm,
1041 const ConfigData& gRec,
1042 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001043 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +00001044 SkBitmap* bitmap) {
epoger@google.comde961632012-10-26 18:56:36 +00001045 SkDynamicMemoryWStream document;
1046
1047 if (gRec.fBackend == kRaster_Backend ||
1048 gRec.fBackend == kGPU_Backend) {
1049 // Early exit if we can't generate the image.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001050 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001051 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +00001052 // TODO: Add a test to exercise what the stdout and
1053 // JSON look like if we get an "early error" while
1054 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +00001055 return errors;
1056 }
1057 } else if (gRec.fBackend == kPDF_Backend) {
1058 generate_pdf(gm, document);
1059#if CAN_IMAGE_PDF
1060 SkAutoDataUnref data(document.copyToData());
1061 SkMemoryStream stream(data->data(), data->size());
1062 SkPDFDocumentToBitmap(&stream, bitmap);
epoger@google.come33e1372013-07-08 19:13:33 +00001063#else
1064 bitmap = NULL; // we don't generate a bitmap rendering of the PDF file
epoger@google.comde961632012-10-26 18:56:36 +00001065#endif
1066 } else if (gRec.fBackend == kXPS_Backend) {
1067 generate_xps(gm, document);
epoger@google.come33e1372013-07-08 19:13:33 +00001068 bitmap = NULL; // we don't generate a bitmap rendering of the XPS file
epoger@google.comde961632012-10-26 18:56:36 +00001069 }
epoger@google.come33e1372013-07-08 19:13:33 +00001070
bungeman@google.comc29f3d82013-07-19 22:32:11 +00001071 SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream());
epoger@google.come33e1372013-07-08 19:13:33 +00001072 if (NULL == bitmap) {
1073 return compare_test_results_to_stored_expectations(
bungeman@google.com88682b72013-07-19 13:55:41 +00001074 gm, gRec, writePath, NULL, documentStream);
epoger@google.come33e1372013-07-08 19:13:33 +00001075 } else {
1076 BitmapAndDigest bitmapAndDigest(*bitmap);
1077 return compare_test_results_to_stored_expectations(
bungeman@google.com88682b72013-07-19 13:55:41 +00001078 gm, gRec, writePath, &bitmapAndDigest, documentStream);
epoger@google.come33e1372013-07-08 19:13:33 +00001079 }
epoger@google.comde961632012-10-26 18:56:36 +00001080 }
1081
epoger@google.com6f6568b2013-03-22 17:29:46 +00001082 ErrorCombination test_deferred_drawing(GM* gm,
1083 const ConfigData& gRec,
1084 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001085 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +00001086 if (gRec.fBackend == kRaster_Backend ||
1087 gRec.fBackend == kGPU_Backend) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001088 const char renderModeDescriptor[] = "-deferred";
epoger@google.comde961632012-10-26 18:56:36 +00001089 SkBitmap bitmap;
1090 // Early exit if we can't generate the image, but this is
1091 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001092 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001093 // TODO(epoger): This logic is the opposite of what is
1094 // described above... if we succeeded in generating the
1095 // -deferred image, we exit early! We should fix this
1096 // ASAP, because it is hiding -deferred errors... but for
1097 // now, I'm leaving the logic as it is so that the
1098 // refactoring change
1099 // https://codereview.chromium.org/12992003/ is unblocked.
1100 //
1101 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
1102 // ('image-surface gm test is failing in "deferred" mode,
1103 // and gm is not reporting the failure')
1104 if (errors.isEmpty()) {
epoger@google.com310478e2013-04-03 18:00:39 +00001105 // TODO(epoger): Report this as a new ErrorType,
1106 // something like kImageGeneration_ErrorType?
epoger@google.com6f6568b2013-03-22 17:29:46 +00001107 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001108 }
epoger@google.com37269602013-01-19 04:21:27 +00001109 return compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001110 gm->shortName(), gRec.fName, renderModeDescriptor, bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001111 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001112 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001113 }
1114
epoger@google.comcaac3db2013-04-04 19:23:11 +00001115 ErrorCombination test_pipe_playback(GM* gm, const ConfigData& gRec,
1116 const SkBitmap& referenceBitmap, bool simulateFailure) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001117 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(),
1118 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001119 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001120 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001121 SkString renderModeDescriptor("-pipe");
1122 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1123
commit-bot@chromium.org805df1a2013-08-16 19:18:12 +00001124 if (gm->getFlags() & GM::kSkipPipe_Flag
1125 || (gPipeWritingFlagCombos[i].flags == SkGPipeWriter::kCrossProcess_Flag
1126 && gm->getFlags() & GM::kSkipPipeCrossProcess_Flag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001127 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001128 renderModeDescriptor.c_str());
1129 errors.add(kIntentionallySkipped_ErrorType);
1130 } else {
1131 SkBitmap bitmap;
1132 SkISize size = gm->getISize();
1133 setup_bitmap(gRec, size, &bitmap);
1134 SkCanvas canvas(bitmap);
1135 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001136 // Pass a decoding function so the factory GM (which has an SkBitmap
1137 // with encoded data) will not fail playback.
1138 PipeController pipeController(&canvas, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001139 SkGPipeWriter writer;
1140 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001141 gPipeWritingFlagCombos[i].flags,
1142 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001143 if (!simulateFailure) {
1144 invokeGM(gm, pipeCanvas, false, false);
1145 }
1146 complete_bitmap(&bitmap);
1147 writer.endRecording();
1148 errors.add(compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001149 gm->shortName(), gRec.fName, renderModeDescriptor.c_str(), bitmap,
1150 &referenceBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001151 if (!errors.isEmpty()) {
1152 break;
1153 }
epoger@google.comde961632012-10-26 18:56:36 +00001154 }
1155 }
1156 return errors;
1157 }
1158
epoger@google.com6f6568b2013-03-22 17:29:46 +00001159 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
1160 const SkBitmap& referenceBitmap) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001161 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(),
1162 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001163 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001164 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001165 SkString renderModeDescriptor("-tiled pipe");
1166 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1167
1168 if ((gm->getFlags() & GM::kSkipPipe_Flag) ||
1169 (gm->getFlags() & GM::kSkipTiled_Flag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001170 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001171 renderModeDescriptor.c_str());
1172 errors.add(kIntentionallySkipped_ErrorType);
1173 } else {
1174 SkBitmap bitmap;
1175 SkISize size = gm->getISize();
1176 setup_bitmap(gRec, size, &bitmap);
1177 SkCanvas canvas(bitmap);
1178 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001179 TiledPipeController pipeController(bitmap, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001180 SkGPipeWriter writer;
1181 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001182 gPipeWritingFlagCombos[i].flags,
1183 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001184 invokeGM(gm, pipeCanvas, false, false);
1185 complete_bitmap(&bitmap);
1186 writer.endRecording();
epoger@google.com659c8c02013-05-21 15:45:45 +00001187 errors.add(compare_test_results_to_reference_bitmap(gm->shortName(), gRec.fName,
epoger@google.comc8263e72013-04-10 12:17:34 +00001188 renderModeDescriptor.c_str(),
1189 bitmap, &referenceBitmap));
1190 if (!errors.isEmpty()) {
1191 break;
1192 }
epoger@google.comde961632012-10-26 18:56:36 +00001193 }
1194 }
1195 return errors;
1196 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00001197
1198 //
1199 // member variables.
1200 // They are public for now, to allow easier setting by tool_main().
1201 //
1202
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001203 bool fUseFileHierarchy, fWriteChecksumBasedFilenames;
epoger@google.com5079d2c2013-04-12 14:11:21 +00001204 ErrorCombination fIgnorableErrorTypes;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001205
junov@chromium.org95146eb2013-01-11 21:04:40 +00001206 const char* fMismatchPath;
epoger@google.com5f995452013-06-21 18:16:47 +00001207 const char* fMissingExpectationsPath;
junov@chromium.org95146eb2013-01-11 21:04:40 +00001208
epoger@google.com310478e2013-04-03 18:00:39 +00001209 // collection of tests that have failed with each ErrorType
1210 SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
1211 int fTestsRun;
1212 SkTDict<int> fRenderModesEncountered;
epoger@google.com57f7abc2012-11-13 03:41:55 +00001213
epoger@google.com908f5832013-04-12 02:23:55 +00001214 // Where to read expectations (expected image hash digests, etc.) from.
epoger@google.com37269602013-01-19 04:21:27 +00001215 // If unset, we don't do comparisons.
1216 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
1217
1218 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +00001219 Json::Value fJsonExpectedResults;
1220 Json::Value fJsonActualResults_Failed;
1221 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001222 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001223 Json::Value fJsonActualResults_Succeeded;
1224
epoger@google.comde961632012-10-26 18:56:36 +00001225}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +00001226
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001227#if SK_SUPPORT_GPU
1228static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1229#else
1230static const GLContextType kDontCare_GLContextType = 0;
1231#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +00001232
1233// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +00001234// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +00001235static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
1236 kWrite_ConfigFlag;
1237
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001238static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001239 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001240#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 +00001241 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001242#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001243 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
1244#if SK_SUPPORT_GPU
1245 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
robertphillips@google.com7bb28a12013-06-06 21:29:39 +00001246 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", false},
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001247 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
epoger@google.comc824c832013-07-12 15:52:59 +00001248 /* The gpudebug context does not generate meaningful images, so don't record
1249 * the images it generates! We only run it to look for asserts. */
scroggo@google.com0f567c62013-03-20 15:35:08 +00001250 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", GR_DEBUG},
robertphillips@google.comd6543e52013-07-18 17:39:14 +00001251 /* The gpunull context does the least amount of work possible and doesn't
1252 generate meaninful images, so don't record them!. It can be run to
1253 isolate the CPU-side processing expense from the GPU-side.
1254 */
1255 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNull_GLContextType, 0, kNone_ConfigFlag, "gpunull", GR_DEBUG},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001256#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001257 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
1258 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001259#endif // SK_ANGLE
1260#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001261 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001262#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001263#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +00001264#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001265 /* 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 +00001266 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +00001267#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001268#ifdef SK_SUPPORT_PDF
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001269 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001270#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001271};
1272
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001273static const char kDefaultsConfigStr[] = "defaults";
1274static const char kExcludeConfigChar = '~';
1275
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001276static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001277 SkString result;
1278 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001279 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001280 SkASSERT(gRec[i].fName != kDefaultsConfigStr);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001281 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001282 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001283 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001284 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001285 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001286 result.append("]\n");
1287 result.appendf("The default value is: \"");
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001288 SkString firstDefault;
1289 SkString allButFirstDefaults;
1290 SkString nonDefault;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001291 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1292 if (gRec[i].fRunByDefault) {
1293 if (i > 0) {
1294 result.append(" ");
1295 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001296 result.append(gRec[i].fName);
1297 if (firstDefault.isEmpty()) {
1298 firstDefault = gRec[i].fName;
1299 } else {
1300 if (!allButFirstDefaults.isEmpty()) {
1301 allButFirstDefaults.append(" ");
1302 }
1303 allButFirstDefaults.append(gRec[i].fName);
1304 }
1305 } else {
1306 nonDefault = gRec[i].fName;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001307 }
1308 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001309 result.append("\"\n");
1310 result.appendf("\"%s\" evaluates to the default set of configs.\n", kDefaultsConfigStr);
1311 result.appendf("Prepending \"%c\" on a config name excludes it from the set of configs to run.\n"
1312 "Exclusions always override inclusions regardless of order.\n",
1313 kExcludeConfigChar);
1314 result.appendf("E.g. \"--config %s %c%s %s\" will run these configs:\n\t%s %s",
1315 kDefaultsConfigStr,
1316 kExcludeConfigChar,
1317 firstDefault.c_str(),
1318 nonDefault.c_str(),
1319 allButFirstDefaults.c_str(),
1320 nonDefault.c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001321 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001322}
scroggo@google.com7d519302013-03-19 17:28:10 +00001323
epoger@google.com6f6568b2013-03-22 17:29:46 +00001324// Macro magic to convert a numeric preprocessor token into a string.
1325// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1326// This should probably be moved into one of our common headers...
1327#define TOSTRING_INTERNAL(x) #x
1328#define TOSTRING(x) TOSTRING_INTERNAL(x)
1329
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001330// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001331DEFINE_string(config, "", configUsage().c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001332DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001333DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1334DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1335#if SK_SUPPORT_GPU
1336DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001337 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1338 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001339#endif
1340DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1341 "when reading/writing files.");
epoger@google.com5079d2c2013-04-12 14:11:21 +00001342DEFINE_string(ignoreErrorTypes, kDefaultIgnorableErrorTypes.asString(" ").c_str(),
1343 "Space-separated list of ErrorTypes that should be ignored. If any *other* error "
1344 "types are encountered, the tool will exit with a nonzero return value.");
caryclark@google.com512c9b62013-05-10 15:16:13 +00001345DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
1346 "Multiple matches may be separated by spaces.\n"
1347 "~ causes a matching test to always be skipped\n"
1348 "^ requires the start of the test to match\n"
1349 "$ requires the end of the test to match\n"
1350 "^ and $ requires an exact match\n"
1351 "If a test does not match any list entry,\n"
1352 "it is skipped unless some list entry starts with ~");
epoger@google.com5f995452013-06-21 18:16:47 +00001353DEFINE_string(missingExpectationsPath, "", "Write images for tests without expectations "
1354 "into this directory.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001355DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1356 "pixel mismatches into this directory.");
1357DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1358 "testIndex %% divisor == remainder.");
1359DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1360DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1361DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1362 "any differences between those and the newly generated ones.");
1363DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1364DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1365DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1366DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
epoger@google.comcaac3db2013-04-04 19:23:11 +00001367DEFINE_bool(simulatePipePlaybackFailure, false, "Simulate a rendering failure in pipe mode only.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001368DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1369DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1370DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1371 "factors to be used for tileGrid playback testing. Default value: 1.0");
epoger@google.comb0f8b432013-04-10 18:46:25 +00001372DEFINE_bool2(verbose, v, false, "Give more detail (e.g. list all GMs run, more info about "
1373 "each test).");
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001374DEFINE_bool(writeChecksumBasedFilenames, false, "When writing out actual images, use checksum-"
1375 "based filenames, as rebaseline.py will use when downloading them from Google Storage");
1376DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001377DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
scroggo@google.com604e0c22013-04-09 21:25:46 +00001378DEFINE_string2(writePicturePath, p, "", "Write .skp files into this directory.");
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001379DEFINE_int32(pdfJpegQuality, -1, "Encodes images in JPEG at quality level N, "
1380 "which can be in range 0-100). N = -1 will disable JPEG compression. "
1381 "Default is N = 100, maximum quality.");
1382
1383static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect) {
1384 // Filter output of warnings that JPEG is not available for the image.
1385 if (bitmap.width() >= 65500 || bitmap.height() >= 65500) return false;
1386 if (FLAGS_pdfJpegQuality == -1) return false;
1387
1388 SkIRect bitmapBounds;
1389 SkBitmap subset;
1390 const SkBitmap* bitmapToUse = &bitmap;
1391 bitmap.getBounds(&bitmapBounds);
1392 if (rect != bitmapBounds) {
1393 SkAssertResult(bitmap.extractSubset(&subset, rect));
1394 bitmapToUse = &subset;
1395 }
1396
1397#if defined(SK_BUILD_FOR_MAC)
1398 // Workaround bug #1043 where bitmaps with referenced pixels cause
1399 // CGImageDestinationFinalize to crash
1400 SkBitmap copy;
1401 bitmapToUse->deepCopyTo(&copy, bitmapToUse->config());
1402 bitmapToUse = &copy;
1403#endif
1404
1405 return SkImageEncoder::EncodeStream(stream,
1406 *bitmapToUse,
1407 SkImageEncoder::kJPEG_Type,
1408 FLAGS_pdfJpegQuality);
1409}
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001410
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001411static int findConfig(const char config[]) {
1412 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1413 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001414 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001415 }
1416 }
1417 return -1;
1418}
1419
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001420namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001421#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +00001422SkAutoTUnref<GrContext> gGrContext;
1423/**
bsalomon@google.comc7a24d22012-11-01 18:03:48 +00001424 * Sets the global GrContext, accessible by individual GMs
bsalomon@google.com7361f542012-04-19 19:15:35 +00001425 */
caryclark@google.com13130862012-06-06 12:10:45 +00001426static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +00001427 SkSafeRef(grContext);
1428 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001429}
bsalomon@google.com7361f542012-04-19 19:15:35 +00001430
1431/**
1432 * Gets the global GrContext, can be called by GM tests.
1433 */
caryclark@google.com13130862012-06-06 12:10:45 +00001434GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +00001435GrContext* GetGr() {
1436 return gGrContext.get();
1437}
1438
1439/**
1440 * Sets the global GrContext and then resets it to its previous value at
1441 * destruction.
1442 */
1443class AutoResetGr : SkNoncopyable {
1444public:
1445 AutoResetGr() : fOld(NULL) {}
1446 void set(GrContext* context) {
1447 SkASSERT(NULL == fOld);
1448 fOld = GetGr();
1449 SkSafeRef(fOld);
1450 SetGr(context);
1451 }
1452 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1453private:
1454 GrContext* fOld;
1455};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001456#else
epoger@google.com80724df2013-03-21 13:49:54 +00001457GrContext* GetGr();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001458GrContext* GetGr() { return NULL; }
1459#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001460}
1461
reed@google.comfb2cd422013-01-04 14:43:03 +00001462template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1463 int index = array->find(value);
1464 if (index < 0) {
1465 *array->append() = value;
1466 }
1467}
1468
epoger@google.com80724df2013-03-21 13:49:54 +00001469/**
1470 * Run this test in a number of different configs (8888, 565, PDF,
1471 * etc.), confirming that the resulting bitmaps match expectations
1472 * (which may be different for each config).
1473 *
1474 * Returns all errors encountered while doing so.
1475 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001476ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1477 GrContextFactory *grFactory);
1478ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1479 GrContextFactory *grFactory) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001480 const char renderModeDescriptor[] = "";
epoger@google.com6f6568b2013-03-22 17:29:46 +00001481 ErrorCombination errorsForAllConfigs;
epoger@google.com80724df2013-03-21 13:49:54 +00001482 uint32_t gmFlags = gm->getFlags();
1483
epoger@google.com80724df2013-03-21 13:49:54 +00001484 for (int i = 0; i < configs.count(); i++) {
1485 ConfigData config = gRec[configs[i]];
epoger@google.com659c8c02013-05-21 15:45:45 +00001486 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->shortName(),
1487 config.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001488
1489 // Skip any tests that we don't even need to try.
epoger@google.comc8263e72013-04-10 12:17:34 +00001490 // If any of these were skipped on a per-GM basis, record them as
1491 // kIntentionallySkipped.
1492 if (kPDF_Backend == config.fBackend) {
1493 if (!FLAGS_pdf) {
epoger@google.com80724df2013-03-21 13:49:54 +00001494 continue;
1495 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001496 if (gmFlags & GM::kSkipPDF_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001497 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001498 renderModeDescriptor);
1499 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1500 continue;
1501 }
1502 }
epoger@google.com80724df2013-03-21 13:49:54 +00001503 if ((gmFlags & GM::kSkip565_Flag) &&
1504 (kRaster_Backend == config.fBackend) &&
1505 (SkBitmap::kRGB_565_Config == config.fConfig)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001506 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001507 renderModeDescriptor);
1508 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001509 continue;
1510 }
1511 if ((gmFlags & GM::kSkipGPU_Flag) &&
1512 kGPU_Backend == config.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001513 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001514 renderModeDescriptor);
1515 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001516 continue;
1517 }
1518
1519 // Now we know that we want to run this test and record its
1520 // success or failure.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001521 ErrorCombination errorsForThisConfig;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001522 GrSurface* gpuTarget = NULL;
epoger@google.com80724df2013-03-21 13:49:54 +00001523#if SK_SUPPORT_GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001524 SkAutoTUnref<GrSurface> auGpuTarget;
epoger@google.com80724df2013-03-21 13:49:54 +00001525 AutoResetGr autogr;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001526 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
epoger@google.com80724df2013-03-21 13:49:54 +00001527 GrContext* gr = grFactory->get(config.fGLContextType);
1528 bool grSuccess = false;
1529 if (gr) {
1530 // create a render target to back the device
1531 GrTextureDesc desc;
1532 desc.fConfig = kSkia8888_GrPixelConfig;
1533 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1534 desc.fWidth = gm->getISize().width();
1535 desc.fHeight = gm->getISize().height();
1536 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001537 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1538 if (NULL != auGpuTarget) {
1539 gpuTarget = auGpuTarget;
1540 grSuccess = true;
epoger@google.com80724df2013-03-21 13:49:54 +00001541 autogr.set(gr);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001542 // Set the user specified cache limits if non-default.
1543 size_t bytes;
1544 int count;
1545 gr->getTextureCacheLimits(&count, &bytes);
1546 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1547 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1548 }
1549 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1550 count = gGpuCacheSizeCount;
1551 }
1552 gr->setTextureCacheLimits(count, bytes);
epoger@google.com80724df2013-03-21 13:49:54 +00001553 }
epoger@google.com80724df2013-03-21 13:49:54 +00001554 }
1555 if (!grSuccess) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001556 errorsForThisConfig.add(kNoGpuContext_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001557 }
1558 }
1559#endif
1560
1561 SkBitmap comparisonBitmap;
1562
1563 const char* writePath;
1564 if (FLAGS_writePath.count() == 1) {
1565 writePath = FLAGS_writePath[0];
1566 } else {
1567 writePath = NULL;
1568 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001569 if (errorsForThisConfig.isEmpty()) {
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001570 errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
1571 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001572 }
1573
epoger@google.com6f6568b2013-03-22 17:29:46 +00001574 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1575 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1576 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001577 gpuTarget));
epoger@google.com80724df2013-03-21 13:49:54 +00001578 }
1579
epoger@google.com6f6568b2013-03-22 17:29:46 +00001580 errorsForAllConfigs.add(errorsForThisConfig);
epoger@google.com80724df2013-03-21 13:49:54 +00001581 }
1582 return errorsForAllConfigs;
1583}
1584
1585/**
1586 * Run this test in a number of different drawing modes (pipe,
1587 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1588 * *exactly* match comparisonBitmap.
1589 *
1590 * Returns all errors encountered while doing so.
1591 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001592ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1593 const SkBitmap &comparisonBitmap,
1594 const SkTDArray<SkScalar> &tileGridReplayScales);
1595ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1596 const SkBitmap &comparisonBitmap,
1597 const SkTDArray<SkScalar> &tileGridReplayScales) {
1598 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001599 uint32_t gmFlags = gm->getFlags();
epoger@google.com659c8c02013-05-21 15:45:45 +00001600 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->shortName(),
1601 compareConfig.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001602
epoger@google.comc8263e72013-04-10 12:17:34 +00001603 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1604 SkAutoUnref aur(pict);
1605 if (FLAGS_replay) {
1606 const char renderModeDescriptor[] = "-replay";
1607 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001608 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1609 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001610 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1611 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001612 SkBitmap bitmap;
1613 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001614 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001615 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1616 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001617 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001618 }
epoger@google.com80724df2013-03-21 13:49:54 +00001619
epoger@google.comc8263e72013-04-10 12:17:34 +00001620 if (FLAGS_serialize) {
1621 const char renderModeDescriptor[] = "-serialize";
1622 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001623 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1624 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001625 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1626 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001627 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1628 SkAutoUnref aurr(repict);
epoger@google.com80724df2013-03-21 13:49:54 +00001629 SkBitmap bitmap;
1630 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001631 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001632 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1633 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001634 }
epoger@google.com80724df2013-03-21 13:49:54 +00001635 }
1636
epoger@google.comc8263e72013-04-10 12:17:34 +00001637 if ((1 == FLAGS_writePicturePath.count()) &&
1638 !(gmFlags & GM::kSkipPicture_Flag)) {
1639 const char* pictureSuffix = "skp";
epoger@google.com659c8c02013-05-21 15:45:45 +00001640 // TODO(epoger): Make sure this still works even though the
1641 // filename now contains the config name (it used to contain
1642 // just the shortName). I think this is actually an
1643 // *improvement*, because now runs with different configs will
1644 // write out their SkPictures to separate files rather than
1645 // overwriting each other. But we should make sure it doesn't
1646 // break anybody.
1647 SkString path = gmmain.make_filename(FLAGS_writePicturePath[0], gm->shortName(),
1648 compareConfig.fName, "", pictureSuffix);
epoger@google.comc8263e72013-04-10 12:17:34 +00001649 SkFILEWStream stream(path.c_str());
1650 pict->serialize(&stream);
epoger@google.com80724df2013-03-21 13:49:54 +00001651 }
1652
epoger@google.comc8263e72013-04-10 12:17:34 +00001653 if (FLAGS_rtree) {
1654 const char renderModeDescriptor[] = "-rtree";
1655 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001656 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1657 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001658 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1659 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001660 SkPicture* pict = gmmain.generate_new_picture(
epoger@google.comc8263e72013-04-10 12:17:34 +00001661 gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
epoger@google.com80724df2013-03-21 13:49:54 +00001662 SkAutoUnref aur(pict);
1663 SkBitmap bitmap;
epoger@google.comc8263e72013-04-10 12:17:34 +00001664 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001665 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001666 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1667 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001668 }
1669 }
1670
1671 if (FLAGS_tileGrid) {
1672 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1673 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1674 SkString renderModeDescriptor("-tilegrid");
1675 if (SK_Scalar1 != replayScale) {
1676 renderModeDescriptor += "-scale-";
1677 renderModeDescriptor.appendScalar(replayScale);
1678 }
1679
1680 if ((gmFlags & GM::kSkipPicture_Flag) ||
1681 ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001682 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001683 renderModeDescriptor.c_str());
1684 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1685 } else {
1686 // We record with the reciprocal scale to obtain a replay
1687 // result that can be validated against comparisonBitmap.
1688 SkScalar recordScale = SkScalarInvert(replayScale);
1689 SkPicture* pict = gmmain.generate_new_picture(
1690 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag,
1691 recordScale);
1692 SkAutoUnref aur(pict);
1693 SkBitmap bitmap;
1694 // We cannot yet pass 'true' to generate_image_from_picture to
1695 // perform actual tiled rendering (see Issue 1198 -
1696 // https://code.google.com/p/skia/issues/detail?id=1198)
1697 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
1698 replayScale /*, true */);
1699 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001700 gm->shortName(), compareConfig.fName, renderModeDescriptor.c_str(), bitmap,
1701 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001702 }
epoger@google.com80724df2013-03-21 13:49:54 +00001703 }
1704 }
1705
1706 // run the pipe centric GM steps
epoger@google.comc8263e72013-04-10 12:17:34 +00001707 if (FLAGS_pipe) {
1708 errorsForAllModes.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap,
1709 FLAGS_simulatePipePlaybackFailure));
1710 if (FLAGS_tiledPipe) {
1711 errorsForAllModes.add(gmmain.test_tiled_pipe_playback(gm, compareConfig,
1712 comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001713 }
epoger@google.com80724df2013-03-21 13:49:54 +00001714 }
1715 return errorsForAllModes;
1716}
1717
epoger@google.com310478e2013-04-03 18:00:39 +00001718/**
1719 * Return a list of all entries in an array of strings as a single string
1720 * of this form:
1721 * "item1", "item2", "item3"
1722 */
1723SkString list_all(const SkTArray<SkString> &stringArray);
1724SkString list_all(const SkTArray<SkString> &stringArray) {
1725 SkString total;
1726 for (int i = 0; i < stringArray.count(); i++) {
1727 if (i > 0) {
1728 total.append(", ");
1729 }
1730 total.append("\"");
1731 total.append(stringArray[i]);
1732 total.append("\"");
1733 }
1734 return total;
1735}
1736
1737/**
1738 * Return a list of configuration names, as a single string of this form:
1739 * "item1", "item2", "item3"
1740 *
1741 * @param configs configurations, as a list of indices into gRec
1742 */
1743SkString list_all_config_names(const SkTDArray<size_t> &configs);
1744SkString list_all_config_names(const SkTDArray<size_t> &configs) {
1745 SkString total;
1746 for (int i = 0; i < configs.count(); i++) {
1747 if (i > 0) {
1748 total.append(", ");
1749 }
1750 total.append("\"");
1751 total.append(gRec[configs[i]].fName);
1752 total.append("\"");
1753 }
1754 return total;
1755}
1756
epoger@google.comfdea3252013-05-02 18:24:03 +00001757bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1758 const SkTDArray<size_t> &configs);
1759bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1760 const SkTDArray<size_t> &configs) {
1761 if (!sk_mkdir(root)) {
1762 return false;
1763 }
1764 if (useFileHierarchy) {
1765 for (int i = 0; i < configs.count(); i++) {
1766 ConfigData config = gRec[configs[i]];
1767 SkString subdir;
1768 subdir.appendf("%s%c%s", root, SkPATH_SEPARATOR, config.fName);
1769 if (!sk_mkdir(subdir.c_str())) {
1770 return false;
1771 }
1772 }
1773 }
1774 return true;
1775}
1776
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00001777static bool parse_flags_configs(SkTDArray<size_t>* outConfigs,
1778 GrContextFactory* grFactory) {
1779 SkTDArray<size_t> excludeConfigs;
1780
1781 for (int i = 0; i < FLAGS_config.count(); i++) {
1782 const char* config = FLAGS_config[i];
1783 bool exclude = false;
1784 if (*config == kExcludeConfigChar) {
1785 exclude = true;
1786 config += 1;
1787 }
1788 int index = findConfig(config);
1789 if (index >= 0) {
1790 if (exclude) {
1791 *excludeConfigs.append() = index;
1792 } else {
1793 appendUnique<size_t>(outConfigs, index);
1794 }
1795 } else if (0 == strcmp(kDefaultsConfigStr, config)) {
1796 if (exclude) {
1797 gm_fprintf(stderr, "%c%s is not allowed.\n",
1798 kExcludeConfigChar, kDefaultsConfigStr);
1799 return false;
1800 }
1801 for (size_t c = 0; c < SK_ARRAY_COUNT(gRec); ++c) {
1802 if (gRec[c].fRunByDefault) {
1803 appendUnique<size_t>(outConfigs, c);
1804 }
1805 }
1806 } else {
1807 gm_fprintf(stderr, "unrecognized config %s\n", config);
1808 return false;
1809 }
1810 }
1811
1812 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1813 int index = findConfig(FLAGS_excludeConfig[i]);
1814 if (index >= 0) {
1815 *excludeConfigs.append() = index;
1816 } else {
1817 gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1818 return false;
1819 }
1820 }
1821
1822 if (outConfigs->count() == 0) {
1823 // if no config is specified by user, add the defaults
1824 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1825 if (gRec[i].fRunByDefault) {
1826 *outConfigs->append() = i;
1827 }
1828 }
1829 }
1830 // now remove any explicitly excluded configs
1831 for (int i = 0; i < excludeConfigs.count(); ++i) {
1832 int index = outConfigs->find(excludeConfigs[i]);
1833 if (index >= 0) {
1834 outConfigs->remove(index);
1835 // now assert that there was only one copy in configs[]
1836 SkASSERT(outConfigs->find(excludeConfigs[i]) < 0);
1837 }
1838 }
1839
1840#if SK_SUPPORT_GPU
1841 SkASSERT(grFactory != NULL);
1842 for (int i = 0; i < outConfigs->count(); ++i) {
1843 size_t index = (*outConfigs)[i];
1844 if (kGPU_Backend == gRec[index].fBackend) {
1845 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1846 if (NULL == ctx) {
1847 gm_fprintf(stderr, "GrContext could not be created for config %s."
1848 " Config will be skipped.\n", gRec[index].fName);
1849 outConfigs->remove(i);
1850 --i;
1851 continue;
1852 }
1853 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
1854 gm_fprintf(stderr, "Sample count (%d) of config %s is not supported."
1855 " Config will be skipped.\n",
1856 gRec[index].fSampleCnt, gRec[index].fName);
1857 outConfigs->remove(i);
1858 --i;
1859 }
1860 }
1861 }
1862#endif
1863
1864 if (outConfigs->isEmpty()) {
1865 gm_fprintf(stderr, "No configs to run.");
1866 return false;
1867 }
1868
1869 // now show the user the set of configs that will be run.
1870 SkString configStr("These configs will be run:");
1871 // show the user the config that will run.
1872 for (int i = 0; i < outConfigs->count(); ++i) {
1873 configStr.appendf(" %s", gRec[(*outConfigs)[i]].fName);
1874 }
1875 gm_fprintf(stdout, "%s\n", configStr.c_str());
1876
1877 return true;
1878}
1879
1880static bool parse_flags_ignore_error_types(ErrorCombination* outErrorTypes) {
1881 if (FLAGS_ignoreErrorTypes.count() > 0) {
1882 *outErrorTypes = ErrorCombination();
1883 for (int i = 0; i < FLAGS_ignoreErrorTypes.count(); i++) {
1884 ErrorType type;
1885 const char *name = FLAGS_ignoreErrorTypes[i];
1886 if (!getErrorTypeByName(name, &type)) {
1887 gm_fprintf(stderr, "cannot find ErrorType with name '%s'\n", name);
1888 return false;
1889 } else {
1890 outErrorTypes->add(type);
1891 }
1892 }
1893 }
1894 return true;
1895}
1896
1897static bool parse_flags_modulo(int* moduloRemainder, int* moduloDivisor) {
1898 if (FLAGS_modulo.count() == 2) {
1899 *moduloRemainder = atoi(FLAGS_modulo[0]);
1900 *moduloDivisor = atoi(FLAGS_modulo[1]);
1901 if (*moduloRemainder < 0 || *moduloDivisor <= 0 ||
1902 *moduloRemainder >= *moduloDivisor) {
1903 gm_fprintf(stderr, "invalid modulo values.");
1904 return false;
1905 }
1906 }
1907 return true;
1908}
1909
1910#if SK_SUPPORT_GPU
1911static bool parse_flags_gpu_cache(int* sizeBytes, int* sizeCount) {
1912 if (FLAGS_gpuCacheSize.count() > 0) {
1913 if (FLAGS_gpuCacheSize.count() != 2) {
1914 gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
1915 return false;
1916 }
1917 *sizeBytes = atoi(FLAGS_gpuCacheSize[0]);
1918 *sizeCount = atoi(FLAGS_gpuCacheSize[1]);
1919 } else {
1920 *sizeBytes = DEFAULT_CACHE_VALUE;
1921 *sizeCount = DEFAULT_CACHE_VALUE;
1922 }
1923 return true;
1924}
1925#endif
1926
1927static bool parse_flags_tile_grid_replay_scales(SkTDArray<SkScalar>* outScales) {
1928 *outScales->append() = SK_Scalar1; // By default only test at scale 1.0
1929 if (FLAGS_tileGridReplayScales.count() > 0) {
1930 outScales->reset();
1931 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
1932 double val = atof(FLAGS_tileGridReplayScales[i]);
1933 if (0 < val) {
1934 *outScales->append() = SkDoubleToScalar(val);
1935 }
1936 }
1937 if (0 == outScales->count()) {
1938 // Should have at least one scale
1939 gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
1940 return false;
1941 }
1942 }
1943 return true;
1944}
1945
1946static bool parse_flags_gmmain_paths(GMMain* gmmain) {
1947 gmmain->fUseFileHierarchy = FLAGS_hierarchy;
1948 gmmain->fWriteChecksumBasedFilenames = FLAGS_writeChecksumBasedFilenames;
1949
1950 if (FLAGS_mismatchPath.count() == 1) {
1951 gmmain->fMismatchPath = FLAGS_mismatchPath[0];
1952 }
1953
1954 if (FLAGS_missingExpectationsPath.count() == 1) {
1955 gmmain->fMissingExpectationsPath = FLAGS_missingExpectationsPath[0];
1956 }
1957
1958 if (FLAGS_readPath.count() == 1) {
1959 const char* readPath = FLAGS_readPath[0];
1960 if (!sk_exists(readPath)) {
1961 gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
1962 return false;
1963 }
1964 if (sk_isdir(readPath)) {
1965 if (FLAGS_verbose) {
1966 gm_fprintf(stdout, "reading from %s\n", readPath);
1967 }
1968 gmmain->fExpectationsSource.reset(SkNEW_ARGS(
1969 IndividualImageExpectationsSource, (readPath)));
1970 } else {
1971 if (FLAGS_verbose) {
1972 gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
1973 }
1974 gmmain->fExpectationsSource.reset(SkNEW_ARGS(JsonExpectationsSource, (readPath)));
1975 }
1976 }
1977 return true;
1978}
1979
1980static bool parse_flags_match_strs(SkTDArray<const char*>* matchStrs) {
1981 for (int i = 0; i < FLAGS_match.count(); ++i) {
1982 matchStrs->push(FLAGS_match[i]);
1983 }
1984 return true;
1985}
1986
1987static bool parse_flags_resource_path() {
1988 if (FLAGS_resourcePath.count() == 1) {
1989 GM::SetResourcePath(FLAGS_resourcePath[0]);
1990 }
1991 return true;
1992}
1993
1994static bool parse_flags_jpeg_quality() {
1995 if (FLAGS_pdfJpegQuality < -1 || FLAGS_pdfJpegQuality > 100) {
1996 gm_fprintf(stderr, "%s\n", "pdfJpegQuality must be in [-1 .. 100] range.");
1997 return false;
1998 }
1999 return true;
2000}
2001
epoger@google.com0b62b3d2013-03-20 17:59:28 +00002002int tool_main(int argc, char** argv);
2003int tool_main(int argc, char** argv) {
2004
2005#if SK_ENABLE_INST_COUNT
2006 gPrintInstCount = true;
2007#endif
2008
2009 SkGraphics::Init();
2010 // we don't need to see this during a run
2011 gSkSuppressFontCachePurgeSpew = true;
2012
2013 setSystemPreferences();
2014 GMMain gmmain;
2015
epoger@google.com0b62b3d2013-03-20 17:59:28 +00002016 SkString usage;
2017 usage.printf("Run the golden master tests.\n");
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00002018 SkCommandLineFlags::SetUsage(usage.c_str());
2019 SkCommandLineFlags::Parse(argc, argv);
epoger@google.com0b62b3d2013-03-20 17:59:28 +00002020
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002021 SkTDArray<size_t> configs;
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002022 int moduloRemainder = -1;
2023 int moduloDivisor = -1;
epoger@google.com6f6568b2013-03-22 17:29:46 +00002024 SkTDArray<SkScalar> tileGridReplayScales;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002025#if SK_SUPPORT_GPU
2026 GrContextFactory* grFactory = new GrContextFactory;
epoger@google.com80724df2013-03-21 13:49:54 +00002027#else
2028 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002029#endif
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002030 SkTDArray<const char*> matchStrs;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002031
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002032 if (!parse_flags_modulo(&moduloRemainder, &moduloDivisor) ||
2033 !parse_flags_ignore_error_types(&gmmain.fIgnorableErrorTypes) ||
2034#if SK_SUPPORT_GPU
2035 !parse_flags_gpu_cache(&gGpuCacheSizeBytes, &gGpuCacheSizeCount) ||
2036#endif
2037 !parse_flags_tile_grid_replay_scales(&tileGridReplayScales) ||
2038 !parse_flags_resource_path() ||
2039 !parse_flags_match_strs(&matchStrs) ||
2040 !parse_flags_jpeg_quality() ||
2041 !parse_flags_configs(&configs, grFactory) ||
2042 !parse_flags_gmmain_paths(&gmmain)) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00002043 return -1;
2044 }
2045
epoger@google.comb0f8b432013-04-10 18:46:25 +00002046 if (FLAGS_verbose) {
2047 if (FLAGS_writePath.count() == 1) {
2048 gm_fprintf(stdout, "writing to %s\n", FLAGS_writePath[0]);
2049 }
epoger@google.comfdea3252013-05-02 18:24:03 +00002050 if (NULL != gmmain.fMismatchPath) {
2051 gm_fprintf(stdout, "writing mismatches to %s\n", gmmain.fMismatchPath);
2052 }
epoger@google.com5f995452013-06-21 18:16:47 +00002053 if (NULL != gmmain.fMissingExpectationsPath) {
2054 gm_fprintf(stdout, "writing images without expectations to %s\n",
2055 gmmain.fMissingExpectationsPath);
2056 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00002057 if (FLAGS_writePicturePath.count() == 1) {
2058 gm_fprintf(stdout, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
2059 }
2060 if (FLAGS_resourcePath.count() == 1) {
2061 gm_fprintf(stdout, "reading resources from %s\n", FLAGS_resourcePath[0]);
2062 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00002063 }
2064
epoger@google.com310478e2013-04-03 18:00:39 +00002065 int gmsRun = 0;
reed@google.comae7b8f32012-10-18 21:30:57 +00002066 int gmIndex = -1;
2067 SkString moduloStr;
2068
epoger@google.come8ebeb12012-10-29 16:42:11 +00002069 // If we will be writing out files, prepare subdirectories.
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002070 if (FLAGS_writePath.count() == 1) {
epoger@google.comfdea3252013-05-02 18:24:03 +00002071 if (!prepare_subdirectories(FLAGS_writePath[0], gmmain.fUseFileHierarchy, configs)) {
epoger@google.come8ebeb12012-10-29 16:42:11 +00002072 return -1;
2073 }
epoger@google.comfdea3252013-05-02 18:24:03 +00002074 }
2075 if (NULL != gmmain.fMismatchPath) {
2076 if (!prepare_subdirectories(gmmain.fMismatchPath, gmmain.fUseFileHierarchy, configs)) {
2077 return -1;
epoger@google.come8ebeb12012-10-29 16:42:11 +00002078 }
2079 }
epoger@google.com5f995452013-06-21 18:16:47 +00002080 if (NULL != gmmain.fMissingExpectationsPath) {
2081 if (!prepare_subdirectories(gmmain.fMissingExpectationsPath, gmmain.fUseFileHierarchy,
2082 configs)) {
2083 return -1;
2084 }
2085 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00002086
bsalomon@google.com7361f542012-04-19 19:15:35 +00002087 Iter iter;
2088 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00002089 while ((gm = iter.next()) != NULL) {
scroggo@google.com7fd2d702013-04-16 19:11:14 +00002090 SkAutoTDelete<GM> adgm(gm);
reed@google.comae7b8f32012-10-18 21:30:57 +00002091 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00002092 if (moduloRemainder >= 0) {
2093 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00002094 continue;
2095 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00002096 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00002097 }
2098
reed@google.comece2b022011-07-25 14:28:57 +00002099 const char* shortName = gm->shortName();
sglez@google.com586db932013-07-24 17:24:23 +00002100
sglez@google.com586db932013-07-24 17:24:23 +00002101 if (SkCommandLineFlags::ShouldSkip(matchStrs, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00002102 continue;
2103 }
2104
epoger@google.com310478e2013-04-03 18:00:39 +00002105 gmsRun++;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00002106 SkISize size = gm->getISize();
epoger@google.combcbf5aa2013-04-12 02:11:54 +00002107 gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
2108 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00002109
epoger@google.com310478e2013-04-03 18:00:39 +00002110 run_multiple_configs(gmmain, gm, configs, grFactory);
djsollen@google.comebce16d2012-10-26 14:07:13 +00002111
2112 SkBitmap comparisonBitmap;
2113 const ConfigData compareConfig =
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002114 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
epoger@google.com310478e2013-04-03 18:00:39 +00002115 gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false);
djsollen@google.comebce16d2012-10-26 14:07:13 +00002116
epoger@google.com80724df2013-03-21 13:49:54 +00002117 // TODO(epoger): only run this if gmmain.generate_image() succeeded?
2118 // Otherwise, what are we comparing against?
epoger@google.com310478e2013-04-03 18:00:39 +00002119 run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap, tileGridReplayScales);
reed@android.com00dae862009-06-10 15:38:48 +00002120 }
epoger@google.com310478e2013-04-03 18:00:39 +00002121
2122 SkTArray<SkString> modes;
2123 gmmain.GetRenderModesEncountered(modes);
epoger@google.comc8263e72013-04-10 12:17:34 +00002124 bool reportError = false;
2125 if (gmmain.NumSignificantErrors() > 0) {
2126 reportError = true;
2127 }
epoger@google.com51dbabe2013-04-10 15:24:53 +00002128 int expectedNumberOfTests = gmsRun * (configs.count() + modes.count());
epoger@google.com310478e2013-04-03 18:00:39 +00002129
2130 // Output summary to stdout.
epoger@google.com51dbabe2013-04-10 15:24:53 +00002131 if (FLAGS_verbose) {
2132 gm_fprintf(stdout, "Ran %d GMs\n", gmsRun);
2133 gm_fprintf(stdout, "... over %2d configs [%s]\n", configs.count(),
2134 list_all_config_names(configs).c_str());
2135 gm_fprintf(stdout, "... and %2d modes [%s]\n", modes.count(), list_all(modes).c_str());
2136 gm_fprintf(stdout, "... so there should be a total of %d tests.\n", expectedNumberOfTests);
2137 }
2138 gmmain.ListErrors(FLAGS_verbose);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002139
epoger@google.com07947d92013-04-11 15:41:02 +00002140 // TODO(epoger): Enable this check for Android, too, once we resolve
2141 // https://code.google.com/p/skia/issues/detail?id=1222
2142 // ('GM is unexpectedly skipping tests on Android')
2143#ifndef SK_BUILD_FOR_ANDROID
epoger@google.comc8263e72013-04-10 12:17:34 +00002144 if (expectedNumberOfTests != gmmain.fTestsRun) {
2145 gm_fprintf(stderr, "expected %d tests, but ran or skipped %d tests\n",
2146 expectedNumberOfTests, gmmain.fTestsRun);
2147 reportError = true;
2148 }
2149#endif
2150
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002151 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.com76c913d2013-04-26 15:06:44 +00002152 Json::Value root = CreateJsonTree(
2153 gmmain.fJsonExpectedResults,
2154 gmmain.fJsonActualResults_Failed, gmmain.fJsonActualResults_FailureIgnored,
2155 gmmain.fJsonActualResults_NoComparison, gmmain.fJsonActualResults_Succeeded);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002156 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002157 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002158 stream.write(jsonStdString.c_str(), jsonStdString.length());
2159 }
2160
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002161#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002162
robertphillips@google.com59552022012-08-31 13:07:37 +00002163#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002164 for (int i = 0; i < configs.count(); i++) {
2165 ConfigData config = gRec[configs[i]];
2166
epoger@google.comb0f8b432013-04-10 18:46:25 +00002167 if (FLAGS_verbose && (kGPU_Backend == config.fBackend)) {
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002168 GrContext* gr = grFactory->get(config.fGLContextType);
2169
epoger@google.com5efdd0c2013-03-13 14:18:40 +00002170 gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002171 gr->printCacheStats();
2172 }
2173 }
2174#endif
2175
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002176 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002177#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002178 SkGraphics::Term();
2179
epoger@google.comc8263e72013-04-10 12:17:34 +00002180 return (reportError) ? -1 : 0;
reed@android.com00dae862009-06-10 15:38:48 +00002181}
caryclark@google.com5987f582012-10-02 18:33:14 +00002182
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002183void GMMain::installFilter(SkCanvas* canvas) {
2184 if (FLAGS_forceBWtext) {
2185 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
2186 }
2187}
2188
borenet@google.com7158e6a2012-11-01 17:43:44 +00002189#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00002190int main(int argc, char * const argv[]) {
2191 return tool_main(argc, (char**) argv);
2192}
2193#endif