blob: b73971441c78e7590a01776597534e213d8fde8b [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"
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000026#include "SkDocument.h"
epoger@google.comde961632012-10-26 18:56:36 +000027#include "SkDrawFilter.h"
commit-bot@chromium.orgc41295d2013-06-18 20:06:36 +000028#include "SkForceLinking.h"
scroggo@google.com5af9b202012-06-04 17:17:36 +000029#include "SkGPipe.h"
reed@android.com8015dd82009-06-21 00:49:18 +000030#include "SkGraphics.h"
31#include "SkImageDecoder.h"
32#include "SkImageEncoder.h"
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000033#include "SkJSONCPP.h"
epoger@google.come8ebeb12012-10-29 16:42:11 +000034#include "SkOSFile.h"
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +000035#include "SkPDFRasterizer.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000036#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000037#include "SkPictureRecorder.h"
robertphillips@google.com977b9c82012-06-05 19:35:09 +000038#include "SkRefCnt.h"
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000039#include "SkScalar.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000040#include "SkStream.h"
epoger@google.comf711f322013-10-18 14:55:47 +000041#include "SkString.h"
reed@google.com11db6fa2014-02-04 15:30:57 +000042#include "SkSurface.h"
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000043#include "SkTArray.h"
epoger@google.com310478e2013-04-03 18:00:39 +000044#include "SkTDict.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000045#include "SamplePipeControllers.h"
reed@google.com07700442010-12-20 19:46:07 +000046
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000047#ifdef SK_DEBUG
48static const bool kDebugOnly = true;
commit-bot@chromium.org8065ec52014-03-11 15:57:40 +000049#define GR_DUMP_FONT_CACHE 0
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000050#else
51static const bool kDebugOnly = false;
52#endif
53
epoger@google.comed5eb4e2013-07-23 17:56:20 +000054__SK_FORCE_IMAGE_DECODER_LINKING;
55
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000056#if SK_SUPPORT_GPU
57#include "GrContextFactory.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000058#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000059typedef GrContextFactory::GLContextType GLContextType;
epoger@google.com6f6568b2013-03-22 17:29:46 +000060#define DEFAULT_CACHE_VALUE -1
61static int gGpuCacheSizeBytes;
62static int gGpuCacheSizeCount;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000063#else
epoger@google.com80724df2013-03-21 13:49:54 +000064class GrContextFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000065class GrContext;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +000066class GrSurface;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000067typedef int GLContextType;
68#endif
69
epoger@google.com76c913d2013-04-26 15:06:44 +000070#define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message")
71
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000072DECLARE_bool(useDocumentInsteadOfDevice);
reed@google.com8923c6c2011-11-08 14:59:38 +000073
reed@google.com07700442010-12-20 19:46:07 +000074#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000075 #include "SkPDFDevice.h"
76 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000077#endif
reed@android.com00dae862009-06-10 15:38:48 +000078
epoger@google.come3cc2eb2012-01-18 20:11:13 +000079// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
80// stop writing out XPS-format image baselines in gm.
81#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000082#ifdef SK_SUPPORT_XPS
83 #include "SkXPSDevice.h"
84#endif
85
reed@google.com46cce912011-06-29 12:54:46 +000086#ifdef SK_BUILD_FOR_MAC
87 #include "SkCGUtils.h"
reed@google.com46cce912011-06-29 12:54:46 +000088#endif
89
reed@android.com00dae862009-06-10 15:38:48 +000090using namespace skiagm;
91
reed@android.com00dae862009-06-10 15:38:48 +000092class Iter {
93public:
94 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000095 this->reset();
96 }
97
98 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000099 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +0000100 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000101
reed@android.comdd0ac282009-06-20 02:38:16 +0000102 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +0000103 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000104 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +0000105 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +0000106 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +0000107 }
108 return NULL;
109 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000110
reed@android.com00dae862009-06-10 15:38:48 +0000111 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +0000112 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +0000113 int count = 0;
114 while (reg) {
115 count += 1;
116 reg = reg->next();
117 }
118 return count;
119 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000120
reed@android.com00dae862009-06-10 15:38:48 +0000121private:
122 const GMRegistry* fReg;
123};
124
epoger@google.comce057fe2013-05-14 15:17:46 +0000125// TODO(epoger): Right now, various places in this code assume that all the
126// image files read/written by GM use this file extension.
127// Search for references to this constant to find these assumptions.
128const static char kPNG_FileExtension[] = "png";
129
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000130enum Backend {
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000131 kRaster_Backend,
132 kGPU_Backend,
133 kPDF_Backend,
134 kXPS_Backend,
135};
136
137enum BbhType {
138 kNone_BbhType,
139 kRTree_BbhType,
140 kTileGrid_BbhType,
commit-bot@chromium.orgd393b172014-04-16 16:02:10 +0000141 kQuadTree_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
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000163struct PDFRasterizerData {
164 bool (*fRasterizerFunction)(SkStream*, SkBitmap*);
165 const char* fName;
166 bool fRunByDefault;
167};
168
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000169class BWTextDrawFilter : public SkDrawFilter {
170public:
reed@google.com971aca72012-11-26 20:26:54 +0000171 virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000172};
reed@google.com971aca72012-11-26 20:26:54 +0000173bool BWTextDrawFilter::filter(SkPaint* p, Type t) {
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000174 if (kText_Type == t) {
175 p->setAntiAlias(false);
176 }
reed@google.com971aca72012-11-26 20:26:54 +0000177 return true;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000178}
179
scroggo@google.com565254b2012-06-28 15:41:32 +0000180struct PipeFlagComboData {
181 const char* name;
182 uint32_t flags;
183};
184
185static PipeFlagComboData gPipeWritingFlagCombos[] = {
186 { "", 0 },
187 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000188 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000189 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000190};
191
commit-bot@chromium.org608ea652013-10-03 19:29:21 +0000192static SkData* encode_to_dct_data(size_t* pixelRefOffset, const SkBitmap& bitmap);
edisonn@google.com73a7ea32013-11-11 20:55:15 +0000193DECLARE_int32(pdfRasterDpi);
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000194
epoger@google.com5079d2c2013-04-12 14:11:21 +0000195const static ErrorCombination kDefaultIgnorableErrorTypes = ErrorCombination()
196 .plus(kMissingExpectations_ErrorType)
197 .plus(kIntentionallySkipped_ErrorType);
198
epoger@google.comde961632012-10-26 18:56:36 +0000199class GMMain {
200public:
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000201 GMMain() : fUseFileHierarchy(false), fWriteChecksumBasedFilenames(false),
202 fIgnorableErrorTypes(kDefaultIgnorableErrorTypes),
epoger@google.com5f995452013-06-21 18:16:47 +0000203 fMismatchPath(NULL), fMissingExpectationsPath(NULL), fTestsRun(0),
204 fRenderModesEncountered(1) {}
epoger@google.come8ebeb12012-10-29 16:42:11 +0000205
epoger@google.com659c8c02013-05-21 15:45:45 +0000206 /**
207 * Assemble shortNamePlusConfig from (surprise!) shortName and configName.
208 *
209 * The method for doing so depends on whether we are using hierarchical naming.
210 * For example, shortName "selftest1" and configName "8888" could be assembled into
211 * either "selftest1_8888" or "8888/selftest1".
212 */
213 SkString make_shortname_plus_config(const char *shortName, const char *configName) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000214 SkString name;
epoger@google.com57f7abc2012-11-13 03:41:55 +0000215 if (0 == strlen(configName)) {
216 name.append(shortName);
217 } else if (fUseFileHierarchy) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000218 name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
219 } else {
220 name.appendf("%s_%s", shortName, configName);
221 }
epoger@google.comde961632012-10-26 18:56:36 +0000222 return name;
223 }
224
epoger@google.com659c8c02013-05-21 15:45:45 +0000225 /**
226 * Assemble filename, suitable for writing out the results of a particular test.
227 */
228 SkString make_filename(const char *path,
229 const char *shortName,
230 const char *configName,
231 const char *renderModeDescriptor,
232 const char *suffix) {
233 SkString filename = make_shortname_plus_config(shortName, configName);
234 filename.append(renderModeDescriptor);
235 filename.appendUnichar('.');
236 filename.append(suffix);
scroggo@google.comccd7afb2013-05-28 16:45:07 +0000237 return SkOSPath::SkPathJoin(path, filename.c_str());
epoger@google.com659c8c02013-05-21 15:45:45 +0000238 }
239
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000240 /**
241 * Assemble filename suitable for writing out an SkBitmap.
242 */
243 SkString make_bitmap_filename(const char *path,
244 const char *shortName,
245 const char *configName,
246 const char *renderModeDescriptor,
247 const GmResultDigest &bitmapDigest) {
248 if (fWriteChecksumBasedFilenames) {
249 SkString filename;
250 filename.append(bitmapDigest.getHashType());
251 filename.appendUnichar('_');
252 filename.append(shortName);
253 filename.appendUnichar('_');
254 filename.append(bitmapDigest.getDigestValue());
255 filename.appendUnichar('.');
256 filename.append(kPNG_FileExtension);
257 return SkOSPath::SkPathJoin(path, filename.c_str());
258 } else {
259 return make_filename(path, shortName, configName, renderModeDescriptor,
260 kPNG_FileExtension);
261 }
262 }
263
epoger@google.com5f6a0072013-01-31 16:30:55 +0000264 /* since PNG insists on unpremultiplying our alpha, we take no
265 precision chances and force all pixels to be 100% opaque,
266 otherwise on compare we may not get a perfect match.
267 */
268 static void force_all_opaque(const SkBitmap& bitmap) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000269 SkColorType colorType = bitmap.colorType();
270 switch (colorType) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000271 case kN32_SkColorType:
epoger@google.com5f6a0072013-01-31 16:30:55 +0000272 force_all_opaque_8888(bitmap);
273 break;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000274 case kRGB_565_SkColorType:
epoger@google.com5f6a0072013-01-31 16:30:55 +0000275 // nothing to do here; 565 bitmaps are inherently opaque
276 break;
277 default:
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +0000278 SkDebugf("unsupported bitmap colorType %d\n", colorType);
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000279 DEBUGFAIL_SEE_STDERR;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000280 }
281 }
282
283 static void force_all_opaque_8888(const SkBitmap& bitmap) {
284 SkAutoLockPixels lock(bitmap);
285 for (int y = 0; y < bitmap.height(); y++) {
286 for (int x = 0; x < bitmap.width(); x++) {
287 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
288 }
289 }
290 }
291
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000292 static ErrorCombination write_bitmap(const SkString& path, const SkBitmap& bitmap) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000293 // TODO(epoger): Now that we have removed force_all_opaque()
294 // from this method, we should be able to get rid of the
295 // transformation to 8888 format also.
296 SkBitmap copy;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000297 bitmap.copyTo(&copy, kN32_SkColorType);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000298 if (!SkImageEncoder::EncodeFile(path.c_str(), copy,
299 SkImageEncoder::kPNG_Type,
300 100)) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +0000301 SkDebugf("FAILED to write bitmap: %s\n", path.c_str());
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000302 return ErrorCombination(kWritingReferenceImage_ErrorType);
303 }
304 return kEmpty_ErrorCombination;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000305 }
306
epoger@google.com6f6568b2013-03-22 17:29:46 +0000307 /**
epoger@google.com310478e2013-04-03 18:00:39 +0000308 * Add all render modes encountered thus far to the "modes" array.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000309 */
epoger@google.com310478e2013-04-03 18:00:39 +0000310 void GetRenderModesEncountered(SkTArray<SkString> &modes) {
311 SkTDict<int>::Iter iter(this->fRenderModesEncountered);
312 const char* mode;
313 while ((mode = iter.next(NULL)) != NULL) {
314 SkString modeAsString = SkString(mode);
315 // TODO(epoger): It seems a bit silly that all of these modes were
316 // recorded with a leading "-" which we have to remove here
317 // (except for mode "", which means plain old original mode).
318 // But that's how renderModeDescriptor has been passed into
319 // compare_test_results_to_reference_bitmap() historically,
320 // and changing that now may affect other parts of our code.
321 if (modeAsString.startsWith("-")) {
322 modeAsString.remove(0, 1);
epoger@google.com310478e2013-04-03 18:00:39 +0000323 }
commit-bot@chromium.org85195482014-01-13 18:27:59 +0000324 modes.push_back(modeAsString);
epoger@google.com310478e2013-04-03 18:00:39 +0000325 }
326 }
327
328 /**
epoger@google.com3a882dd2013-10-07 18:55:09 +0000329 * Returns true if failures on this test should be ignored.
330 */
commit-bot@chromium.org3e62ebf2014-01-14 02:54:11 +0000331 bool ShouldIgnoreTest(const char *name) const {
332 for (int i = 0; i < fIgnorableTestNames.count(); i++) {
333 if (fIgnorableTestNames[i].equals(name)) {
epoger@google.com3a882dd2013-10-07 18:55:09 +0000334 return true;
335 }
336 }
337 return false;
338 }
339
340 /**
commit-bot@chromium.org85195482014-01-13 18:27:59 +0000341 * Calls RecordTestResults to record that we skipped a test.
342 *
343 * Depending on the backend, this may mean that we skipped a single rendermode, or all
344 * rendermodes; see http://skbug.com/1994 and https://codereview.chromium.org/129203002/
345 */
346 void RecordSkippedTest(const SkString& shortNamePlusConfig,
347 const char renderModeDescriptor [], Backend backend) {
348 if (kRaster_Backend == backend) {
349 // Skipping a test on kRaster_Backend means that we will skip ALL renderModes
350 // (as opposed to other backends, on which we only run the default renderMode).
351 //
352 // We cannot call RecordTestResults yet, because we won't know the full set of
353 // renderModes until we have run all tests.
354 fTestsSkippedOnAllRenderModes.push_back(shortNamePlusConfig);
355 } else {
356 this->RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
357 renderModeDescriptor);
358 }
359 }
360
361 /**
epoger@google.com310478e2013-04-03 18:00:39 +0000362 * Records the results of this test in fTestsRun and fFailedTests.
363 *
364 * We even record successes, and errors that we regard as
365 * "ignorable"; we can filter them out later.
366 */
epoger@google.com659c8c02013-05-21 15:45:45 +0000367 void RecordTestResults(const ErrorCombination& errorCombination,
368 const SkString& shortNamePlusConfig,
epoger@google.com310478e2013-04-03 18:00:39 +0000369 const char renderModeDescriptor []) {
370 // Things to do regardless of errorCombination.
371 fTestsRun++;
372 int renderModeCount = 0;
373 this->fRenderModesEncountered.find(renderModeDescriptor, &renderModeCount);
374 renderModeCount++;
375 this->fRenderModesEncountered.set(renderModeDescriptor, renderModeCount);
376
epoger@google.com6f6568b2013-03-22 17:29:46 +0000377 if (errorCombination.isEmpty()) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000378 return;
epoger@google.comeb066362013-03-08 09:39:36 +0000379 }
380
epoger@google.com310478e2013-04-03 18:00:39 +0000381 // Things to do only if there is some error condition.
epoger@google.com659c8c02013-05-21 15:45:45 +0000382 SkString fullName = shortNamePlusConfig;
epoger@google.comcaac3db2013-04-04 19:23:11 +0000383 fullName.append(renderModeDescriptor);
epoger@google.com310478e2013-04-03 18:00:39 +0000384 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
385 ErrorType type = static_cast<ErrorType>(typeInt);
386 if (errorCombination.includes(type)) {
387 fFailedTests[type].push_back(fullName);
epoger@google.comf60494b2013-04-03 17:02:53 +0000388 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000389 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000390 }
391
epoger@google.com310478e2013-04-03 18:00:39 +0000392 /**
393 * Return the number of significant (non-ignorable) errors we have
394 * encountered so far.
395 */
396 int NumSignificantErrors() {
397 int significantErrors = 0;
398 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
399 ErrorType type = static_cast<ErrorType>(typeInt);
epoger@google.com5079d2c2013-04-12 14:11:21 +0000400 if (!fIgnorableErrorTypes.includes(type)) {
epoger@google.com310478e2013-04-03 18:00:39 +0000401 significantErrors += fFailedTests[type].count();
402 }
403 }
404 return significantErrors;
405 }
406
407 /**
epoger@google.com51dbabe2013-04-10 15:24:53 +0000408 * Display the summary of results with this ErrorType.
409 *
410 * @param type which ErrorType
411 * @param verbose whether to be all verbose about it
epoger@google.com310478e2013-04-03 18:00:39 +0000412 */
epoger@google.com51dbabe2013-04-10 15:24:53 +0000413 void DisplayResultTypeSummary(ErrorType type, bool verbose) {
epoger@google.com5079d2c2013-04-12 14:11:21 +0000414 bool isIgnorableType = fIgnorableErrorTypes.includes(type);
epoger@google.com51dbabe2013-04-10 15:24:53 +0000415
416 SkString line;
417 if (isIgnorableType) {
418 line.append("[ ] ");
419 } else {
420 line.append("[*] ");
421 }
422
423 SkTArray<SkString> *failedTestsOfThisType = &fFailedTests[type];
424 int count = failedTestsOfThisType->count();
425 line.appendf("%d %s", count, getErrorTypeName(type));
426 if (!isIgnorableType || verbose) {
427 line.append(":");
428 for (int i = 0; i < count; ++i) {
429 line.append(" ");
430 line.append((*failedTestsOfThisType)[i]);
431 }
432 }
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +0000433 SkDebugf("%s\n", line.c_str());
epoger@google.com51dbabe2013-04-10 15:24:53 +0000434 }
435
436 /**
437 * List contents of fFailedTests to stdout.
438 *
439 * @param verbose whether to be all verbose about it
440 */
441 void ListErrors(bool verbose) {
epoger@google.com310478e2013-04-03 18:00:39 +0000442 // First, print a single summary line.
443 SkString summary;
444 summary.appendf("Ran %d tests:", fTestsRun);
445 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
446 ErrorType type = static_cast<ErrorType>(typeInt);
447 summary.appendf(" %s=%d", getErrorTypeName(type), fFailedTests[type].count());
448 }
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +0000449 SkDebugf("%s\n", summary.c_str());
epoger@google.com310478e2013-04-03 18:00:39 +0000450
451 // Now, for each failure type, list the tests that failed that way.
452 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
epoger@google.com51dbabe2013-04-10 15:24:53 +0000453 this->DisplayResultTypeSummary(static_cast<ErrorType>(typeInt), verbose);
epoger@google.com310478e2013-04-03 18:00:39 +0000454 }
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +0000455 SkDebugf("(results marked with [*] will cause nonzero return value)\n");
epoger@google.com310478e2013-04-03 18:00:39 +0000456 }
457
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000458 static ErrorCombination write_document(const SkString& path, SkStreamAsset* asset) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000459 SkFILEWStream stream(path.c_str());
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000460 if (!stream.writeStream(asset, asset->getLength())) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +0000461 SkDebugf("FAILED to write document: %s\n", path.c_str());
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000462 return ErrorCombination(kWritingReferenceImage_ErrorType);
463 }
464 return kEmpty_ErrorCombination;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000465 }
466
epoger@google.com37269602013-01-19 04:21:27 +0000467 /**
epoger@google.com5f6a0072013-01-31 16:30:55 +0000468 * Prepare an SkBitmap to render a GM into.
469 *
470 * After you've rendered the GM into the SkBitmap, you must call
471 * complete_bitmap()!
472 *
473 * @todo thudson 22 April 2011 - could refactor this to take in
474 * a factory to generate the context, always call readPixels()
475 * (logically a noop for rasters, if wasted time), and thus collapse the
476 * GPU special case and also let this be used for SkPicture testing.
477 */
478 static void setup_bitmap(const ConfigData& gRec, SkISize& size,
479 SkBitmap* bitmap) {
reed@google.comeb9a46c2014-01-25 16:46:20 +0000480 bitmap->allocConfigPixels(gRec.fConfig, size.width(), size.height());
epoger@google.com5f6a0072013-01-31 16:30:55 +0000481 bitmap->eraseColor(SK_ColorTRANSPARENT);
482 }
483
484 /**
485 * Any finalization steps we need to perform on the SkBitmap after
486 * we have rendered the GM into it.
epoger@google.com37269602013-01-19 04:21:27 +0000487 *
488 * It's too bad that we are throwing away alpha channel data
489 * we could otherwise be examining, but this had always been happening
490 * before... it was buried within the compare() method at
491 * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
492 *
493 * Apparently we need this, at least for bitmaps that are either:
494 * (a) destined to be written out as PNG files, or
495 * (b) compared against bitmaps read in from PNG files
496 * for the reasons described just above the force_all_opaque() method.
497 *
498 * Neglecting to do this led to the difficult-to-diagnose
499 * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
500 * spurious pixel_error messages as of r7258')
501 *
502 * TODO(epoger): Come up with a better solution that allows us to
503 * compare full pixel data, including alpha channel, while still being
504 * robust in the face of transformations to/from PNG files.
505 * Options include:
506 *
507 * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
508 * will be written to, or compared against, PNG files.
509 * PRO: Preserve/compare alpha channel info for the non-PNG cases
510 * (comparing different renderModes in-memory)
epoger@google.com908f5832013-04-12 02:23:55 +0000511 * CON: The bitmaps (and hash digests) for these non-PNG cases would be
epoger@google.com37269602013-01-19 04:21:27 +0000512 * different than those for the PNG-compared cases, and in the
513 * case of a failed renderMode comparison, how would we write the
514 * image to disk for examination?
515 *
epoger@google.com908f5832013-04-12 02:23:55 +0000516 * 2. Always compute image hash digests from PNG format (either
epoger@google.com37269602013-01-19 04:21:27 +0000517 * directly from the the bytes of a PNG file, or capturing the
518 * bytes we would have written to disk if we were writing the
519 * bitmap out as a PNG).
520 * PRO: I think this would allow us to never force opaque, and to
521 * the extent that alpha channel data can be preserved in a PNG
522 * file, we could observe it.
epoger@google.com908f5832013-04-12 02:23:55 +0000523 * CON: If we read a bitmap from disk, we need to take its hash digest
epoger@google.com37269602013-01-19 04:21:27 +0000524 * from the source PNG (we can't compute it from the bitmap we
525 * read out of the PNG, because we will have already premultiplied
526 * the alpha).
527 * CON: Seems wasteful to convert a bitmap to PNG format just to take
epoger@google.com908f5832013-04-12 02:23:55 +0000528 * its hash digest. (Although we're wasting lots of effort already
epoger@google.com37269602013-01-19 04:21:27 +0000529 * calling force_all_opaque().)
530 *
531 * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
532 * so we can transform images back and forth without fear of off-by-one
533 * errors.
534 * CON: Math is hard.
535 *
536 * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
537 * channel), rather than demanding absolute equality.
epoger@google.com908f5832013-04-12 02:23:55 +0000538 * CON: Can't do this with hash digests.
epoger@google.com37269602013-01-19 04:21:27 +0000539 */
epoger@google.com5f6a0072013-01-31 16:30:55 +0000540 static void complete_bitmap(SkBitmap* bitmap) {
541 force_all_opaque(*bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000542 }
543
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000544 static void installFilter(SkCanvas* canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000545
reed@google.comaef73612012-11-16 13:41:45 +0000546 static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000547 SkAutoCanvasRestore acr(canvas, true);
548
549 if (!isPDF) {
550 canvas->concat(gm->getInitialTransform());
551 }
552 installFilter(canvas);
reed@google.comaef73612012-11-16 13:41:45 +0000553 gm->setCanvasIsDeferred(isDeferred);
epoger@google.comde961632012-10-26 18:56:36 +0000554 gm->draw(canvas);
555 canvas->setDrawFilter(NULL);
556 }
557
epoger@google.com6f6568b2013-03-22 17:29:46 +0000558 static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000559 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000560 SkBitmap* bitmap,
561 bool deferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000562 SkISize size (gm->getISize());
563 setup_bitmap(gRec, size, bitmap);
commit-bot@chromium.org466f5f32014-05-27 21:30:37 +0000564 const SkImageInfo info = bitmap->info();
epoger@google.comde961632012-10-26 18:56:36 +0000565
reed@google.com11db6fa2014-02-04 15:30:57 +0000566 SkAutoTUnref<SkSurface> surface;
epoger@google.comde961632012-10-26 18:56:36 +0000567 SkAutoTUnref<SkCanvas> canvas;
568
569 if (gRec.fBackend == kRaster_Backend) {
reed@google.com11db6fa2014-02-04 15:30:57 +0000570 surface.reset(SkSurface::NewRasterDirect(info,
571 bitmap->getPixels(),
572 bitmap->rowBytes()));
epoger@google.comde961632012-10-26 18:56:36 +0000573 if (deferred) {
reed@google.com11db6fa2014-02-04 15:30:57 +0000574 canvas.reset(SkDeferredCanvas::Create(surface));
epoger@google.comde961632012-10-26 18:56:36 +0000575 } else {
reed@google.com11db6fa2014-02-04 15:30:57 +0000576 canvas.reset(SkRef(surface->getCanvas()));
epoger@google.comde961632012-10-26 18:56:36 +0000577 }
reed@google.comaef73612012-11-16 13:41:45 +0000578 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000579 canvas->flush();
580 }
581#if SK_SUPPORT_GPU
582 else { // GPU
reed@google.com11db6fa2014-02-04 15:30:57 +0000583 surface.reset(SkSurface::NewRenderTargetDirect(gpuTarget->asRenderTarget()));
epoger@google.comde961632012-10-26 18:56:36 +0000584 if (deferred) {
reed@google.com11db6fa2014-02-04 15:30:57 +0000585 canvas.reset(SkDeferredCanvas::Create(surface));
epoger@google.comde961632012-10-26 18:56:36 +0000586 } else {
reed@google.com11db6fa2014-02-04 15:30:57 +0000587 canvas.reset(SkRef(surface->getCanvas()));
epoger@google.comde961632012-10-26 18:56:36 +0000588 }
reed@google.comaef73612012-11-16 13:41:45 +0000589 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000590 // the device is as large as the current rendertarget, so
591 // we explicitly only readback the amount we expect (in
592 // size) overwrite our previous allocation
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000593 bitmap->setInfo(SkImageInfo::MakeN32Premul(size.fWidth, size.fHeight));
epoger@google.comde961632012-10-26 18:56:36 +0000594 canvas->readPixels(bitmap, 0, 0);
595 }
596#endif
epoger@google.com5f6a0072013-01-31 16:30:55 +0000597 complete_bitmap(bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000598 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000599 }
600
601 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
junov@chromium.orgc938c482012-12-19 15:24:38 +0000602 SkPicture* pict, SkBitmap* bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000603 SkScalar scale = SK_Scalar1,
604 bool tile = false) {
epoger@google.comde961632012-10-26 18:56:36 +0000605 SkISize size = gm->getISize();
606 setup_bitmap(gRec, size, bitmap);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000607
608 if (tile) {
609 // Generate the result image by rendering to tiles and accumulating
610 // the results in 'bitmap'
611
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000612 // This 16x16 tiling matches the settings applied to 'pict' in
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000613 // 'generate_new_picture'
614 SkISize tileSize = SkISize::Make(16, 16);
615
616 SkBitmap tileBM;
617 setup_bitmap(gRec, tileSize, &tileBM);
618 SkCanvas tileCanvas(tileBM);
619 installFilter(&tileCanvas);
620
621 SkCanvas bmpCanvas(*bitmap);
622 SkPaint bmpPaint;
623 bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
624
625 for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
626 for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
627 int saveCount = tileCanvas.save();
628 SkMatrix mat(tileCanvas.getTotalMatrix());
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000629 mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000630 SkIntToScalar(-yTile*tileSize.height()));
631 tileCanvas.setMatrix(mat);
632 pict->draw(&tileCanvas);
633 tileCanvas.flush();
634 tileCanvas.restoreToCount(saveCount);
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000635 bmpCanvas.drawBitmap(tileBM,
636 SkIntToScalar(xTile * tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000637 SkIntToScalar(yTile * tileSize.height()),
638 &bmpPaint);
639 }
640 }
641 } else {
642 SkCanvas canvas(*bitmap);
643 installFilter(&canvas);
644 canvas.scale(scale, scale);
robertphillips9b14f262014-06-04 05:40:44 -0700645 canvas.drawPicture(pict);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000646 complete_bitmap(bitmap);
647 }
epoger@google.comde961632012-10-26 18:56:36 +0000648 }
649
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000650 static bool generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
epoger@google.comde961632012-10-26 18:56:36 +0000651#ifdef SK_SUPPORT_PDF
652 SkMatrix initialTransform = gm->getInitialTransform();
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000653 if (FLAGS_useDocumentInsteadOfDevice) {
654 SkISize pageISize = gm->getISize();
edisonn@google.com73a7ea32013-11-11 20:55:15 +0000655 SkAutoTUnref<SkDocument> pdfDoc(
656 SkDocument::CreatePDF(&pdf, NULL,
657 encode_to_dct_data,
658 SkIntToScalar(FLAGS_pdfRasterDpi)));
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000659
660 if (!pdfDoc.get()) {
661 return false;
662 }
663
664 SkCanvas* canvas = NULL;
665 canvas = pdfDoc->beginPage(SkIntToScalar(pageISize.width()),
666 SkIntToScalar(pageISize.height()));
667 canvas->concat(initialTransform);
668
669 invokeGM(gm, canvas, true, false);
670
671 return pdfDoc->close();
epoger@google.comde961632012-10-26 18:56:36 +0000672 } else {
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000673 SkISize pageSize = gm->getISize();
674 SkPDFDevice* dev = NULL;
675 if (initialTransform.isIdentity()) {
676 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
677 } else {
678 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
679 SkIntToScalar(pageSize.height()));
680 initialTransform.mapRect(&content);
681 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
682 SkIntToScalar(pageSize.height()));
683 SkISize contentSize =
684 SkISize::Make(SkScalarRoundToInt(content.width()),
685 SkScalarRoundToInt(content.height()));
686 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
687 }
688 dev->setDCTEncoder(encode_to_dct_data);
edisonn@google.com73a7ea32013-11-11 20:55:15 +0000689 dev->setRasterDpi(SkIntToScalar(FLAGS_pdfRasterDpi));
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000690 SkAutoUnref aur(dev);
691 SkCanvas c(dev);
692 invokeGM(gm, &c, true, false);
693 SkPDFDocument doc;
694 doc.appendPage(dev);
695 doc.emitPDF(&pdf);
epoger@google.comde961632012-10-26 18:56:36 +0000696 }
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000697#endif // SK_SUPPORT_PDF
698 return true; // Do not report failure if pdf is not supported.
epoger@google.comde961632012-10-26 18:56:36 +0000699 }
700
701 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
702#ifdef SK_SUPPORT_XPS
703 SkISize size = gm->getISize();
704
705 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
706 SkIntToScalar(size.height()));
707 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
708 static const SkScalar upm = 72 * inchesPerMeter;
709 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
710 static const SkScalar ppm = 200 * inchesPerMeter;
711 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
712
713 SkXPSDevice* dev = new SkXPSDevice();
714 SkAutoUnref aur(dev);
715
716 SkCanvas c(dev);
717 dev->beginPortfolio(&xps);
718 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000719 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000720 dev->endSheet();
721 dev->endPortfolio();
722
723#endif
724 }
725
epoger@google.com37269602013-01-19 04:21:27 +0000726 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000727 * Log more detail about the mistmatch between expectedBitmap and
728 * actualBitmap.
729 */
730 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
731 const char *testName) {
732 const int expectedWidth = expectedBitmap.width();
733 const int expectedHeight = expectedBitmap.height();
734 const int width = actualBitmap.width();
735 const int height = actualBitmap.height();
736 if ((expectedWidth != width) || (expectedHeight != height)) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +0000737 SkDebugf("---- %s: dimension mismatch -- expected [%d %d], actual [%d %d]\n",
738 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000739 return;
740 }
741
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000742 if ((kN32_SkColorType != expectedBitmap.colorType()) ||
743 (kN32_SkColorType != actualBitmap.colorType())) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +0000744 SkDebugf("---- %s: not computing max per-channel pixel mismatch because non-8888\n",
745 testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000746 return;
747 }
748
749 SkAutoLockPixels alp0(expectedBitmap);
750 SkAutoLockPixels alp1(actualBitmap);
751 int errR = 0;
752 int errG = 0;
753 int errB = 0;
754 int errA = 0;
755 int differingPixels = 0;
756
757 for (int y = 0; y < height; ++y) {
758 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
759 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
760 for (int x = 0; x < width; ++x) {
761 SkPMColor expectedPixel = *expectedPixelPtr++;
762 SkPMColor actualPixel = *actualPixelPtr++;
763 if (expectedPixel != actualPixel) {
764 differingPixels++;
765 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
766 (int)SkGetPackedR32(actualPixel)));
767 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
768 (int)SkGetPackedG32(actualPixel)));
769 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
770 (int)SkGetPackedB32(actualPixel)));
771 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
772 (int)SkGetPackedA32(actualPixel)));
773 }
774 }
775 }
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +0000776 SkDebugf("---- %s: %d (of %d) differing pixels, "
777 "max per-channel mismatch R=%d G=%d B=%d A=%d\n",
778 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000779 }
780
781 /**
epoger@google.com908f5832013-04-12 02:23:55 +0000782 * Compares actual hash digest to expectations, returning the set of errors
epoger@google.com6f6568b2013-03-22 17:29:46 +0000783 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000784 *
785 * If fMismatchPath has been set, and there are pixel diffs, then the
786 * actual bitmap will be written out to a file within fMismatchPath.
epoger@google.com5f995452013-06-21 18:16:47 +0000787 * And similarly for fMissingExpectationsPath...
epoger@google.com37269602013-01-19 04:21:27 +0000788 *
789 * @param expectations what expectations to compare actualBitmap against
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000790 * @param actualBitmapAndDigest the SkBitmap we actually generated, and its GmResultDigest
epoger@google.com659c8c02013-05-21 15:45:45 +0000791 * @param shortName name of test, e.g. "selftest1"
792 * @param configName name of config, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000793 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
794 * @param addToJsonSummary whether to add these results (both actual and
epoger@google.comcaac3db2013-04-04 19:23:11 +0000795 * expected) to the JSON summary. Regardless of this setting, if
796 * we find an image mismatch in this test, we will write these
797 * results to the JSON summary. (This is so that we will always
798 * report errors across rendering modes, such as pipe vs tiled.
799 * See https://codereview.chromium.org/13650002/ )
epoger@google.com37269602013-01-19 04:21:27 +0000800 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000801 ErrorCombination compare_to_expectations(Expectations expectations,
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000802 const BitmapAndDigest& actualBitmapAndDigest,
epoger@google.com659c8c02013-05-21 15:45:45 +0000803 const char *shortName, const char *configName,
804 const char *renderModeDescriptor,
epoger@google.comcaac3db2013-04-04 19:23:11 +0000805 bool addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000806 ErrorCombination errors;
epoger@google.com659c8c02013-05-21 15:45:45 +0000807 SkString shortNamePlusConfig = make_shortname_plus_config(shortName, configName);
808 SkString completeNameString(shortNamePlusConfig);
epoger@google.com37269602013-01-19 04:21:27 +0000809 completeNameString.append(renderModeDescriptor);
epoger@google.comce057fe2013-05-14 15:17:46 +0000810 completeNameString.append(".");
811 completeNameString.append(kPNG_FileExtension);
epoger@google.com37269602013-01-19 04:21:27 +0000812 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000813
epoger@google.com37269602013-01-19 04:21:27 +0000814 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000815 errors.add(kMissingExpectations_ErrorType);
epoger@google.com5f995452013-06-21 18:16:47 +0000816
817 // Write out the "actuals" for any tests without expectations, if we have
818 // been directed to do so.
819 if (fMissingExpectationsPath) {
820 SkString path = make_bitmap_filename(fMissingExpectationsPath, shortName,
821 configName, renderModeDescriptor,
822 actualBitmapAndDigest.fDigest);
823 write_bitmap(path, actualBitmapAndDigest.fBitmap);
824 }
825
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000826 } else if (!expectations.match(actualBitmapAndDigest.fDigest)) {
epoger@google.comcaac3db2013-04-04 19:23:11 +0000827 addToJsonSummary = true;
828 // The error mode we record depends on whether this was running
829 // in a non-standard renderMode.
830 if ('\0' == *renderModeDescriptor) {
831 errors.add(kExpectationsMismatch_ErrorType);
832 } else {
833 errors.add(kRenderModeMismatch_ErrorType);
834 }
epoger@google.com84a18022013-02-01 20:39:15 +0000835
836 // Write out the "actuals" for any mismatches, if we have
837 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000838 if (fMismatchPath) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000839 SkString path = make_bitmap_filename(fMismatchPath, shortName, configName,
840 renderModeDescriptor,
841 actualBitmapAndDigest.fDigest);
842 write_bitmap(path, actualBitmapAndDigest.fBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000843 }
epoger@google.com84a18022013-02-01 20:39:15 +0000844
845 // If we have access to a single expected bitmap, log more
846 // detail about the mismatch.
847 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
848 if (NULL != expectedBitmapPtr) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000849 report_bitmap_diffs(*expectedBitmapPtr, actualBitmapAndDigest.fBitmap,
850 completeName);
epoger@google.com84a18022013-02-01 20:39:15 +0000851 }
epoger@google.coma243b222013-01-17 17:54:28 +0000852 }
epoger@google.coma243b222013-01-17 17:54:28 +0000853
epoger@google.com37269602013-01-19 04:21:27 +0000854 if (addToJsonSummary) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000855 add_actual_results_to_json_summary(completeName, actualBitmapAndDigest.fDigest, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000856 expectations.ignoreFailure());
857 add_expected_results_to_json_summary(completeName, expectations);
858 }
epoger@google.coma243b222013-01-17 17:54:28 +0000859
epoger@google.com6f6568b2013-03-22 17:29:46 +0000860 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000861 }
862
epoger@google.com37269602013-01-19 04:21:27 +0000863 /**
epoger@google.com1ddfbc22013-10-10 17:24:20 +0000864 * Add this result to the appropriate JSON collection of actual results (but just ONE),
epoger@google.com76c913d2013-04-26 15:06:44 +0000865 * depending on errors encountered.
epoger@google.com37269602013-01-19 04:21:27 +0000866 */
867 void add_actual_results_to_json_summary(const char testName[],
epoger@google.comd4993ff2013-05-24 14:33:28 +0000868 const GmResultDigest &actualResultDigest,
epoger@google.com76c913d2013-04-26 15:06:44 +0000869 ErrorCombination errors,
epoger@google.com37269602013-01-19 04:21:27 +0000870 bool ignoreFailure) {
commit-bot@chromium.org25c10662014-05-16 17:56:43 +0000871#ifdef SK_BUILD_JSON_WRITER
epoger@google.comd4993ff2013-05-24 14:33:28 +0000872 Json::Value jsonActualResults = actualResultDigest.asJsonTypeValuePair();
epoger@google.com1ddfbc22013-10-10 17:24:20 +0000873 Json::Value *resultCollection = NULL;
874
epoger@google.com76c913d2013-04-26 15:06:44 +0000875 if (errors.isEmpty()) {
epoger@google.com1ddfbc22013-10-10 17:24:20 +0000876 resultCollection = &this->fJsonActualResults_Succeeded;
877 } else if (errors.includes(kRenderModeMismatch_ErrorType)) {
878 resultCollection = &this->fJsonActualResults_Failed;
879 } else if (errors.includes(kExpectationsMismatch_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000880 if (ignoreFailure) {
epoger@google.com1ddfbc22013-10-10 17:24:20 +0000881 resultCollection = &this->fJsonActualResults_FailureIgnored;
epoger@google.com37269602013-01-19 04:21:27 +0000882 } else {
epoger@google.com1ddfbc22013-10-10 17:24:20 +0000883 resultCollection = &this->fJsonActualResults_Failed;
epoger@google.com37269602013-01-19 04:21:27 +0000884 }
epoger@google.com1ddfbc22013-10-10 17:24:20 +0000885 } else if (errors.includes(kMissingExpectations_ErrorType)) {
886 // TODO: What about the case where there IS an expected
887 // image hash digest, but that gm test doesn't actually
888 // run? For now, those cases will always be ignored,
889 // because gm only looks at expectations that correspond
890 // to gm tests that were actually run.
891 //
892 // Once we have the ability to express expectations as a
893 // JSON file, we should fix this (and add a test case for
894 // which an expectation is given but the test is never
895 // run).
896 resultCollection = &this->fJsonActualResults_NoComparison;
897 }
898
899 // If none of the above cases match, we don't add it to ANY tally of actual results.
900 if (resultCollection) {
901 (*resultCollection)[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000902 }
commit-bot@chromium.org25c10662014-05-16 17:56:43 +0000903#endif
epoger@google.com37269602013-01-19 04:21:27 +0000904 }
905
906 /**
907 * Add this test to the JSON collection of expected results.
908 */
909 void add_expected_results_to_json_summary(const char testName[],
910 Expectations expectations) {
commit-bot@chromium.org25c10662014-05-16 17:56:43 +0000911#ifdef SK_BUILD_JSON_WRITER
epoger@google.com76c913d2013-04-26 15:06:44 +0000912 this->fJsonExpectedResults[testName] = expectations.asJsonValue();
commit-bot@chromium.org25c10662014-05-16 17:56:43 +0000913#endif
epoger@google.com37269602013-01-19 04:21:27 +0000914 }
915
916 /**
917 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
918 *
919 * @param gm which test generated the actualBitmap
920 * @param gRec
vandebo@chromium.org8fc37662013-08-21 18:04:09 +0000921 * @param configName The config name to look for in the expectation file.
epoger@google.come33e1372013-07-08 19:13:33 +0000922 * @param actualBitmapAndDigest ptr to bitmap generated by this run, or NULL
923 * if we don't have a usable bitmap representation
epoger@google.com37269602013-01-19 04:21:27 +0000924 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000925 ErrorCombination compare_test_results_to_stored_expectations(
vandebo@chromium.org8fc37662013-08-21 18:04:09 +0000926 GM* gm, const ConfigData& gRec, const char* configName,
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000927 const BitmapAndDigest* actualBitmapAndDigest) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000928 ErrorCombination errors;
epoger@google.come33e1372013-07-08 19:13:33 +0000929
930 if (NULL == actualBitmapAndDigest) {
931 // Note that we intentionally skipped validating the results for
932 // this test, because we don't know how to generate an SkBitmap
933 // version of the output.
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000934 errors.add(ErrorCombination(kIntentionallySkipped_ErrorType));
epoger@google.comc824c832013-07-12 15:52:59 +0000935 } else if (!(gRec.fFlags & kWrite_ConfigFlag)) {
936 // We don't record the results for this test or compare them
937 // against any expectations, because the output image isn't
938 // meaningful.
939 // See https://code.google.com/p/skia/issues/detail?id=1410 ('some
940 // GM result images not available for download from Google Storage')
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000941 errors.add(ErrorCombination(kIntentionallySkipped_ErrorType));
epoger@google.come33e1372013-07-08 19:13:33 +0000942 } else {
943 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000944 SkString nameWithExtension = make_shortname_plus_config(gm->getName(), configName);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000945 nameWithExtension.append(".");
946 nameWithExtension.append(kPNG_FileExtension);
947
epoger@google.come33e1372013-07-08 19:13:33 +0000948 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
949 /*
950 * Get the expected results for this test, as one or more allowed
951 * hash digests. The current implementation of expectationsSource
952 * get this by computing the hash digest of a single PNG file on disk.
953 *
954 * TODO(epoger): This relies on the fact that
955 * force_all_opaque() was called on the bitmap before it
956 * was written to disk as a PNG in the first place. If
957 * not, the hash digest returned here may not match the
958 * hash digest of actualBitmap, which *has* been run through
959 * force_all_opaque().
960 * See comments above complete_bitmap() for more detail.
961 */
962 Expectations expectations = expectationsSource->get(nameWithExtension.c_str());
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000963 if (this->ShouldIgnoreTest(gm->getName())) {
epoger@google.comdefc4872013-09-19 06:18:27 +0000964 expectations.setIgnoreFailure(true);
965 }
epoger@google.come33e1372013-07-08 19:13:33 +0000966 errors.add(compare_to_expectations(expectations, *actualBitmapAndDigest,
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000967 gm->getName(), configName, "", true));
epoger@google.come33e1372013-07-08 19:13:33 +0000968 } else {
969 // If we are running without expectations, we still want to
970 // record the actual results.
971 add_actual_results_to_json_summary(nameWithExtension.c_str(),
972 actualBitmapAndDigest->fDigest,
973 ErrorCombination(kMissingExpectations_ErrorType),
974 false);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000975 errors.add(ErrorCombination(kMissingExpectations_ErrorType));
epoger@google.come33e1372013-07-08 19:13:33 +0000976 }
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000977 }
epoger@google.com6f6568b2013-03-22 17:29:46 +0000978 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000979 }
980
epoger@google.com37269602013-01-19 04:21:27 +0000981 /**
982 * Compare actualBitmap to referenceBitmap.
983 *
epoger@google.com659c8c02013-05-21 15:45:45 +0000984 * @param shortName test name, e.g. "selftest1"
985 * @param configName configuration name, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000986 * @param renderModeDescriptor
987 * @param actualBitmap actual bitmap generated by this run
988 * @param referenceBitmap bitmap we expected to be generated
989 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000990 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +0000991 const char *shortName, const char *configName, const char *renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000992 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
993
994 SkASSERT(referenceBitmap);
epoger@google.com84a18022013-02-01 20:39:15 +0000995 Expectations expectations(*referenceBitmap);
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000996 BitmapAndDigest actualBitmapAndDigest(actualBitmap);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000997
998 // TODO: Eliminate RecordTestResults from here.
999 // Results recording code for the test_drawing path has been refactored so that
1000 // RecordTestResults is only called once, at the topmost level. However, the
1001 // other paths have not yet been refactored, and RecordTestResults has been added
1002 // here to maintain proper behavior for calls not coming from the test_drawing path.
1003 ErrorCombination errors;
1004 errors.add(compare_to_expectations(expectations, actualBitmapAndDigest, shortName,
1005 configName, renderModeDescriptor, false));
1006 SkString shortNamePlusConfig = make_shortname_plus_config(shortName, configName);
1007 RecordTestResults(errors, shortNamePlusConfig, renderModeDescriptor);
1008
1009 return errors;
epoger@google.com37269602013-01-19 04:21:27 +00001010 }
1011
junov@chromium.org20bd04e2013-01-16 18:43:36 +00001012 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
1013 SkScalar scale = SK_Scalar1) {
junov@chromium.org706ff2f2012-12-19 15:55:40 +00001014 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
1015 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +00001016
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001017 SkAutoTDelete<SkBBHFactory> factory;
junov@chromium.org3cb834b2012-12-13 16:39:53 +00001018 if (kTileGrid_BbhType == bbhType) {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001019 SkTileGridFactory::TileGridInfo info;
junov@chromium.org29b19e52013-02-27 18:35:16 +00001020 info.fMargin.setEmpty();
1021 info.fOffset.setZero();
1022 info.fTileInterval.set(16, 16);
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001023 factory.reset(SkNEW_ARGS(SkTileGridFactory, (info)));
commit-bot@chromium.orgd393b172014-04-16 16:02:10 +00001024 } else if (kQuadTree_BbhType == bbhType) {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001025 factory.reset(SkNEW(SkQuadTreeFactory));
commit-bot@chromium.orgd393b172014-04-16 16:02:10 +00001026 } else if (kRTree_BbhType == bbhType) {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001027 factory.reset(SkNEW(SkRTreeFactory));
junov@chromium.org20bd04e2013-01-16 18:43:36 +00001028 }
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +00001029 SkPictureRecorder recorder;
1030 SkCanvas* cv = recorder.beginRecording(width, height, factory.get(), recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +00001031 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +00001032 invokeGM(gm, cv, false, false);
robertphillips@google.com84b18c72014-04-13 19:09:42 +00001033 return recorder.endRecording();
epoger@google.comde961632012-10-26 18:56:36 +00001034 }
1035
1036 static SkPicture* stream_to_new_picture(const SkPicture& src) {
epoger@google.comde961632012-10-26 18:56:36 +00001037 SkDynamicMemoryWStream storage;
reed@google.com672588b2014-01-08 15:42:01 +00001038 src.serialize(&storage, NULL);
bungeman@google.comc29f3d82013-07-19 22:32:11 +00001039 SkAutoTUnref<SkStreamAsset> pictReadback(storage.detachAsStream());
commit-bot@chromium.org805df1a2013-08-16 19:18:12 +00001040 SkPicture* retval = SkPicture::CreateFromStream(pictReadback,
1041 &SkImageDecoder::DecodeMemory);
epoger@google.comde961632012-10-26 18:56:36 +00001042 return retval;
1043 }
1044
1045 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +00001046 // Depending on flags, possibly compare to an expected image.
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001047 // If writePath is not NULL, also write images (or documents) to the specified path.
1048 ErrorCombination test_drawing(GM* gm, const ConfigData& gRec,
1049 const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
epoger@google.com6f6568b2013-03-22 17:29:46 +00001050 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001051 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +00001052 SkBitmap* bitmap) {
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001053 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001054 SkDynamicMemoryWStream document;
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001055 SkString path;
epoger@google.comde961632012-10-26 18:56:36 +00001056
1057 if (gRec.fBackend == kRaster_Backend ||
1058 gRec.fBackend == kGPU_Backend) {
1059 // Early exit if we can't generate the image.
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001060 errors.add(generate_image(gm, gRec, gpuTarget, bitmap, false));
epoger@google.com6f6568b2013-03-22 17:29:46 +00001061 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +00001062 // TODO: Add a test to exercise what the stdout and
1063 // JSON look like if we get an "early error" while
1064 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +00001065 return errors;
1066 }
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001067 BitmapAndDigest bitmapAndDigest(*bitmap);
1068 errors.add(compare_test_results_to_stored_expectations(
vandebo@chromium.org8fc37662013-08-21 18:04:09 +00001069 gm, gRec, gRec.fName, &bitmapAndDigest));
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001070
1071 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001072 path = make_bitmap_filename(writePath, gm->getName(), gRec.fName,
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001073 "", bitmapAndDigest.fDigest);
1074 errors.add(write_bitmap(path, bitmapAndDigest.fBitmap));
1075 }
epoger@google.comde961632012-10-26 18:56:36 +00001076 } else if (gRec.fBackend == kPDF_Backend) {
commit-bot@chromium.org5e009892013-10-14 13:42:12 +00001077 if (!generate_pdf(gm, document)) {
1078 errors.add(kGeneratePdfFailed_ErrorType);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001079 } else {
commit-bot@chromium.org5e009892013-10-14 13:42:12 +00001080 SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream());
1081 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001082 path = make_filename(writePath, gm->getName(), gRec.fName, "", "pdf");
commit-bot@chromium.org5e009892013-10-14 13:42:12 +00001083 errors.add(write_document(path, documentStream));
1084 }
1085
1086 if (!(gm->getFlags() & GM::kSkipPDFRasterization_Flag)) {
1087 for (int i = 0; i < pdfRasterizers.count(); i++) {
1088 SkBitmap pdfBitmap;
vandebo@chromium.org969967e2013-12-09 23:22:15 +00001089 documentStream->rewind();
commit-bot@chromium.org5e009892013-10-14 13:42:12 +00001090 bool success = (*pdfRasterizers[i]->fRasterizerFunction)(
1091 documentStream.get(), &pdfBitmap);
1092 if (!success) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00001093 SkDebugf("FAILED to render PDF for %s using renderer %s\n",
1094 gm->getName(),
1095 pdfRasterizers[i]->fName);
commit-bot@chromium.org5e009892013-10-14 13:42:12 +00001096 continue;
1097 }
1098
1099 SkString configName(gRec.fName);
1100 configName.append("-");
1101 configName.append(pdfRasterizers[i]->fName);
1102
1103 BitmapAndDigest bitmapAndDigest(pdfBitmap);
1104 errors.add(compare_test_results_to_stored_expectations(
1105 gm, gRec, configName.c_str(), &bitmapAndDigest));
1106
1107 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001108 path = make_bitmap_filename(writePath, gm->getName(),
commit-bot@chromium.org5e009892013-10-14 13:42:12 +00001109 configName.c_str(),
1110 "", bitmapAndDigest.fDigest);
1111 errors.add(write_bitmap(path, bitmapAndDigest.fBitmap));
1112 }
1113 }
1114 } else {
1115 errors.add(kIntentionallySkipped_ErrorType);
1116 }
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001117 }
epoger@google.comde961632012-10-26 18:56:36 +00001118 } else if (gRec.fBackend == kXPS_Backend) {
1119 generate_xps(gm, document);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001120 SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream());
epoger@google.come33e1372013-07-08 19:13:33 +00001121
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001122 errors.add(compare_test_results_to_stored_expectations(
vandebo@chromium.org8fc37662013-08-21 18:04:09 +00001123 gm, gRec, gRec.fName, NULL));
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001124
1125 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001126 path = make_filename(writePath, gm->getName(), gRec.fName, "", "xps");
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001127 errors.add(write_document(path, documentStream));
1128 }
epoger@google.come33e1372013-07-08 19:13:33 +00001129 } else {
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001130 SkASSERT(false);
epoger@google.come33e1372013-07-08 19:13:33 +00001131 }
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001132 return errors;
epoger@google.comde961632012-10-26 18:56:36 +00001133 }
1134
epoger@google.com6f6568b2013-03-22 17:29:46 +00001135 ErrorCombination test_deferred_drawing(GM* gm,
1136 const ConfigData& gRec,
1137 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001138 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +00001139 if (gRec.fBackend == kRaster_Backend ||
1140 gRec.fBackend == kGPU_Backend) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001141 const char renderModeDescriptor[] = "-deferred";
epoger@google.comde961632012-10-26 18:56:36 +00001142 SkBitmap bitmap;
1143 // Early exit if we can't generate the image, but this is
1144 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001145 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001146 // TODO(epoger): This logic is the opposite of what is
1147 // described above... if we succeeded in generating the
1148 // -deferred image, we exit early! We should fix this
1149 // ASAP, because it is hiding -deferred errors... but for
1150 // now, I'm leaving the logic as it is so that the
1151 // refactoring change
1152 // https://codereview.chromium.org/12992003/ is unblocked.
1153 //
1154 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
1155 // ('image-surface gm test is failing in "deferred" mode,
1156 // and gm is not reporting the failure')
1157 if (errors.isEmpty()) {
epoger@google.com310478e2013-04-03 18:00:39 +00001158 // TODO(epoger): Report this as a new ErrorType,
1159 // something like kImageGeneration_ErrorType?
epoger@google.com6f6568b2013-03-22 17:29:46 +00001160 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001161 }
epoger@google.com37269602013-01-19 04:21:27 +00001162 return compare_test_results_to_reference_bitmap(
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001163 gm->getName(), gRec.fName, renderModeDescriptor, bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001164 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001165 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001166 }
1167
epoger@google.comcaac3db2013-04-04 19:23:11 +00001168 ErrorCombination test_pipe_playback(GM* gm, const ConfigData& gRec,
1169 const SkBitmap& referenceBitmap, bool simulateFailure) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001170 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->getName(),
epoger@google.com659c8c02013-05-21 15:45:45 +00001171 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001172 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001173 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001174 SkString renderModeDescriptor("-pipe");
1175 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1176
commit-bot@chromium.org805df1a2013-08-16 19:18:12 +00001177 if (gm->getFlags() & GM::kSkipPipe_Flag
1178 || (gPipeWritingFlagCombos[i].flags == SkGPipeWriter::kCrossProcess_Flag
1179 && gm->getFlags() & GM::kSkipPipeCrossProcess_Flag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001180 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001181 renderModeDescriptor.c_str());
1182 errors.add(kIntentionallySkipped_ErrorType);
1183 } else {
1184 SkBitmap bitmap;
1185 SkISize size = gm->getISize();
1186 setup_bitmap(gRec, size, &bitmap);
1187 SkCanvas canvas(bitmap);
1188 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001189 // Pass a decoding function so the factory GM (which has an SkBitmap
1190 // with encoded data) will not fail playback.
1191 PipeController pipeController(&canvas, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001192 SkGPipeWriter writer;
1193 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001194 gPipeWritingFlagCombos[i].flags,
1195 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001196 if (!simulateFailure) {
1197 invokeGM(gm, pipeCanvas, false, false);
1198 }
1199 complete_bitmap(&bitmap);
1200 writer.endRecording();
1201 errors.add(compare_test_results_to_reference_bitmap(
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001202 gm->getName(), gRec.fName, renderModeDescriptor.c_str(), bitmap,
epoger@google.com659c8c02013-05-21 15:45:45 +00001203 &referenceBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001204 if (!errors.isEmpty()) {
1205 break;
1206 }
epoger@google.comde961632012-10-26 18:56:36 +00001207 }
1208 }
1209 return errors;
1210 }
1211
epoger@google.com6f6568b2013-03-22 17:29:46 +00001212 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
1213 const SkBitmap& referenceBitmap) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001214 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->getName(),
epoger@google.com659c8c02013-05-21 15:45:45 +00001215 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001216 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001217 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001218 SkString renderModeDescriptor("-tiled pipe");
1219 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1220
1221 if ((gm->getFlags() & GM::kSkipPipe_Flag) ||
1222 (gm->getFlags() & GM::kSkipTiled_Flag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001223 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001224 renderModeDescriptor.c_str());
1225 errors.add(kIntentionallySkipped_ErrorType);
1226 } else {
1227 SkBitmap bitmap;
1228 SkISize size = gm->getISize();
1229 setup_bitmap(gRec, size, &bitmap);
1230 SkCanvas canvas(bitmap);
1231 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001232 TiledPipeController pipeController(bitmap, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001233 SkGPipeWriter writer;
1234 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001235 gPipeWritingFlagCombos[i].flags,
1236 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001237 invokeGM(gm, pipeCanvas, false, false);
1238 complete_bitmap(&bitmap);
1239 writer.endRecording();
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001240 errors.add(compare_test_results_to_reference_bitmap(gm->getName(), gRec.fName,
epoger@google.comc8263e72013-04-10 12:17:34 +00001241 renderModeDescriptor.c_str(),
1242 bitmap, &referenceBitmap));
1243 if (!errors.isEmpty()) {
1244 break;
1245 }
epoger@google.comde961632012-10-26 18:56:36 +00001246 }
1247 }
1248 return errors;
1249 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00001250
1251 //
1252 // member variables.
1253 // They are public for now, to allow easier setting by tool_main().
1254 //
1255
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001256 bool fUseFileHierarchy, fWriteChecksumBasedFilenames;
epoger@google.com5079d2c2013-04-12 14:11:21 +00001257 ErrorCombination fIgnorableErrorTypes;
commit-bot@chromium.org3e62ebf2014-01-14 02:54:11 +00001258 SkTArray<SkString> fIgnorableTestNames;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001259
junov@chromium.org95146eb2013-01-11 21:04:40 +00001260 const char* fMismatchPath;
epoger@google.com5f995452013-06-21 18:16:47 +00001261 const char* fMissingExpectationsPath;
junov@chromium.org95146eb2013-01-11 21:04:40 +00001262
epoger@google.com310478e2013-04-03 18:00:39 +00001263 // collection of tests that have failed with each ErrorType
1264 SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
commit-bot@chromium.org85195482014-01-13 18:27:59 +00001265 SkTArray<SkString> fTestsSkippedOnAllRenderModes;
epoger@google.com310478e2013-04-03 18:00:39 +00001266 int fTestsRun;
1267 SkTDict<int> fRenderModesEncountered;
epoger@google.com57f7abc2012-11-13 03:41:55 +00001268
epoger@google.com908f5832013-04-12 02:23:55 +00001269 // Where to read expectations (expected image hash digests, etc.) from.
epoger@google.com37269602013-01-19 04:21:27 +00001270 // If unset, we don't do comparisons.
1271 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
1272
commit-bot@chromium.org25c10662014-05-16 17:56:43 +00001273#ifdef SK_BUILD_JSON_WRITER
epoger@google.com37269602013-01-19 04:21:27 +00001274 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +00001275 Json::Value fJsonExpectedResults;
1276 Json::Value fJsonActualResults_Failed;
1277 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001278 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001279 Json::Value fJsonActualResults_Succeeded;
commit-bot@chromium.org25c10662014-05-16 17:56:43 +00001280#endif
epoger@google.comde961632012-10-26 18:56:36 +00001281}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +00001282
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001283#if SK_SUPPORT_GPU
1284static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1285#else
1286static const GLContextType kDontCare_GLContextType = 0;
1287#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +00001288
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001289static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001290 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001291#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 +00001292 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001293#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001294 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
1295#if SK_SUPPORT_GPU
1296 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
robertphillips@google.com7bb28a12013-06-06 21:29:39 +00001297 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", false},
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001298 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
commit-bot@chromium.org440ddf42014-01-26 18:07:40 +00001299 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNVPR_GLContextType, 4, kRW_ConfigFlag, "nvprmsaa4", true },
1300 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNVPR_GLContextType, 16, kRW_ConfigFlag, "nvprmsaa16", false},
epoger@google.comc824c832013-07-12 15:52:59 +00001301 /* The gpudebug context does not generate meaningful images, so don't record
1302 * the images it generates! We only run it to look for asserts. */
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001303 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", kDebugOnly},
robertphillips@google.comd6543e52013-07-18 17:39:14 +00001304 /* The gpunull context does the least amount of work possible and doesn't
1305 generate meaninful images, so don't record them!. It can be run to
1306 isolate the CPU-side processing expense from the GPU-side.
1307 */
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001308 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNull_GLContextType, 0, kNone_ConfigFlag, "gpunull", kDebugOnly},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001309#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001310 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
1311 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001312#endif // SK_ANGLE
1313#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001314 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001315#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001316#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +00001317#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001318 /* 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 +00001319 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +00001320#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001321#ifdef SK_SUPPORT_PDF
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001322 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001323#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001324};
1325
bungeman@google.com5d20cae2014-05-09 15:22:41 +00001326static bool SkNoRasterizePDF(SkStream*, SkBitmap*) { return false; }
1327
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001328static const PDFRasterizerData kPDFRasterizers[] = {
1329#ifdef SK_BUILD_FOR_MAC
1330 { &SkPDFDocumentToBitmap, "mac", true },
1331#endif
1332#ifdef SK_BUILD_POPPLER
1333 { &SkPopplerRasterizePDF, "poppler", true },
1334#endif
commit-bot@chromium.orgffd178c2013-11-11 15:10:47 +00001335#ifdef SK_BUILD_NATIVE_PDF_RENDERER
1336 { &SkNativeRasterizePDF, "native", true },
1337#endif // SK_BUILD_NATIVE_PDF_RENDERER
bungeman@google.com5d20cae2014-05-09 15:22:41 +00001338 // The following exists so that this array is never zero length.
1339 { &SkNoRasterizePDF, "none", false},
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001340};
1341
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001342static const char kDefaultsConfigStr[] = "defaults";
1343static const char kExcludeConfigChar = '~';
1344
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001345static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001346 SkString result;
1347 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001348 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001349 SkASSERT(gRec[i].fName != kDefaultsConfigStr);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001350 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001351 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001352 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001353 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001354 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001355 result.append("]\n");
1356 result.appendf("The default value is: \"");
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001357 SkString firstDefault;
1358 SkString allButFirstDefaults;
1359 SkString nonDefault;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001360 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1361 if (gRec[i].fRunByDefault) {
1362 if (i > 0) {
1363 result.append(" ");
1364 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001365 result.append(gRec[i].fName);
1366 if (firstDefault.isEmpty()) {
1367 firstDefault = gRec[i].fName;
1368 } else {
1369 if (!allButFirstDefaults.isEmpty()) {
1370 allButFirstDefaults.append(" ");
1371 }
1372 allButFirstDefaults.append(gRec[i].fName);
1373 }
1374 } else {
1375 nonDefault = gRec[i].fName;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001376 }
1377 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001378 result.append("\"\n");
1379 result.appendf("\"%s\" evaluates to the default set of configs.\n", kDefaultsConfigStr);
1380 result.appendf("Prepending \"%c\" on a config name excludes it from the set of configs to run.\n"
1381 "Exclusions always override inclusions regardless of order.\n",
1382 kExcludeConfigChar);
1383 result.appendf("E.g. \"--config %s %c%s %s\" will run these configs:\n\t%s %s",
1384 kDefaultsConfigStr,
1385 kExcludeConfigChar,
1386 firstDefault.c_str(),
1387 nonDefault.c_str(),
1388 allButFirstDefaults.c_str(),
1389 nonDefault.c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001390 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001391}
scroggo@google.com7d519302013-03-19 17:28:10 +00001392
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001393static SkString pdfRasterizerUsage() {
1394 SkString result;
1395 result.appendf("Space delimited list of which PDF rasterizers to run. Possible options: [");
1396 // For this (and further) loops through kPDFRasterizers, there is a typecast to int to avoid
1397 // the compiler giving an "comparison of unsigned expression < 0 is always false" warning
1398 // and turning it into a build-breaking error.
1399 for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
1400 if (i > 0) {
1401 result.append(" ");
1402 }
1403 result.append(kPDFRasterizers[i].fName);
1404 }
1405 result.append("]\n");
1406 result.append("The default value is: \"");
1407 for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
1408 if (kPDFRasterizers[i].fRunByDefault) {
1409 if (i > 0) {
1410 result.append(" ");
1411 }
1412 result.append(kPDFRasterizers[i].fName);
1413 }
1414 }
1415 result.append("\"");
1416 return result;
1417}
1418
epoger@google.com6f6568b2013-03-22 17:29:46 +00001419// Macro magic to convert a numeric preprocessor token into a string.
1420// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1421// This should probably be moved into one of our common headers...
1422#define TOSTRING_INTERNAL(x) #x
1423#define TOSTRING(x) TOSTRING_INTERNAL(x)
1424
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001425// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001426DEFINE_string(config, "", configUsage().c_str());
vandebo@chromium.orgf8afb2b2013-11-06 16:32:15 +00001427DEFINE_string(pdfRasterizers, "default", pdfRasterizerUsage().c_str());
epoger@google.com5e497382013-09-30 07:01:55 +00001428DEFINE_bool(deferred, false, "Exercise the deferred rendering test pass.");
commit-bot@chromium.orgb17ccc92014-03-13 16:16:36 +00001429DEFINE_bool(dryRun, false, "Don't actually run the tests, just print what would have been done.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001430DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1431DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1432#if SK_SUPPORT_GPU
1433DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001434 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1435 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001436#endif
1437DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1438 "when reading/writing files.");
epoger@google.com5079d2c2013-04-12 14:11:21 +00001439DEFINE_string(ignoreErrorTypes, kDefaultIgnorableErrorTypes.asString(" ").c_str(),
1440 "Space-separated list of ErrorTypes that should be ignored. If any *other* error "
1441 "types are encountered, the tool will exit with a nonzero return value.");
epoger@google.comf711f322013-10-18 14:55:47 +00001442DEFINE_string(ignoreFailuresFile, "", "Path to file containing a list of tests for which we "
1443 "should ignore failures.\n"
1444 "The file should list one test per line, except for comment lines starting with #");
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +00001445DEFINE_bool2(leaks, l, false, "show leaked ref cnt'd objects.");
caryclark@google.com512c9b62013-05-10 15:16:13 +00001446DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
1447 "Multiple matches may be separated by spaces.\n"
1448 "~ causes a matching test to always be skipped\n"
1449 "^ requires the start of the test to match\n"
1450 "$ requires the end of the test to match\n"
1451 "^ and $ requires an exact match\n"
1452 "If a test does not match any list entry,\n"
1453 "it is skipped unless some list entry starts with ~");
epoger@google.com5f995452013-06-21 18:16:47 +00001454DEFINE_string(missingExpectationsPath, "", "Write images for tests without expectations "
1455 "into this directory.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001456DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1457 "pixel mismatches into this directory.");
1458DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1459 "testIndex %% divisor == remainder.");
epoger@google.com5e497382013-09-30 07:01:55 +00001460DEFINE_bool(pipe, false, "Exercise the SkGPipe replay test pass.");
commit-bot@chromium.orgd393b172014-04-16 16:02:10 +00001461DEFINE_bool(quadtree, false, "Exercise the QuadTree variant of SkPicture test pass.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001462DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1463 "any differences between those and the newly generated ones.");
epoger@google.com5e497382013-09-30 07:01:55 +00001464DEFINE_bool(replay, false, "Exercise the SkPicture replay test pass.");
djsollen@google.comac8f3a42013-10-04 14:57:00 +00001465#if SK_SUPPORT_GPU
1466DEFINE_bool(resetGpuContext, false, "Reset the GrContext prior to running each GM.");
1467#endif
tfarina@chromium.org725a64c2013-12-31 14:29:52 +00001468DEFINE_string2(resourcePath, i, "resources", "Directory that stores image resources.");
epoger@google.com5e497382013-09-30 07:01:55 +00001469DEFINE_bool(rtree, false, "Exercise the R-Tree variant of SkPicture test pass.");
1470DEFINE_bool(serialize, false, "Exercise the SkPicture serialization & deserialization test pass.");
epoger@google.comcaac3db2013-04-04 19:23:11 +00001471DEFINE_bool(simulatePipePlaybackFailure, false, "Simulate a rendering failure in pipe mode only.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001472DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
epoger@google.com5e497382013-09-30 07:01:55 +00001473DEFINE_bool(tileGrid, false, "Exercise the tile grid variant of SkPicture.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001474DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1475 "factors to be used for tileGrid playback testing. Default value: 1.0");
epoger@google.comb0f8b432013-04-10 18:46:25 +00001476DEFINE_bool2(verbose, v, false, "Give more detail (e.g. list all GMs run, more info about "
1477 "each test).");
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001478DEFINE_bool(writeChecksumBasedFilenames, false, "When writing out actual images, use checksum-"
1479 "based filenames, as rebaseline.py will use when downloading them from Google Storage");
commit-bot@chromium.org25c10662014-05-16 17:56:43 +00001480#ifdef SK_BUILD_JSON_WRITER
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001481DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
commit-bot@chromium.org25c10662014-05-16 17:56:43 +00001482#endif
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001483DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
scroggo@google.com604e0c22013-04-09 21:25:46 +00001484DEFINE_string2(writePicturePath, p, "", "Write .skp files into this directory.");
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001485DEFINE_int32(pdfJpegQuality, -1, "Encodes images in JPEG at quality level N, "
1486 "which can be in range 0-100). N = -1 will disable JPEG compression. "
1487 "Default is N = 100, maximum quality.");
commit-bot@chromium.orgf4f9df42013-09-26 20:44:24 +00001488// TODO(edisonn): pass a matrix instead of forcePerspectiveMatrix
1489// Either the 9 numbers defining the matrix
1490// or probably more readable would be to replace it with a set of a few predicates
1491// Like --prerotate 100 200 10 --posttranslate 10, 10
1492// Probably define spacial names like centerx, centery, top, bottom, left, right
1493// then we can write something reabable like --rotate centerx centery 90
1494DEFINE_bool(forcePerspectiveMatrix, false, "Force a perspective matrix.");
commit-bot@chromium.org5e009892013-10-14 13:42:12 +00001495DEFINE_bool(useDocumentInsteadOfDevice, false, "Use SkDocument::CreateFoo instead of SkFooDevice.");
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001496DEFINE_int32(pdfRasterDpi, 72, "Scale at which at which the non suported "
1497 "features in PDF are rasterized. Must be be in range 0-10000. "
1498 "Default is 72. N = 0 will disable rasterizing features like "
1499 "text shadows or perspective bitmaps.");
reed@google.com672588b2014-01-08 15:42:01 +00001500static SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) {
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001501 // Filter output of warnings that JPEG is not available for the image.
commit-bot@chromium.org608ea652013-10-03 19:29:21 +00001502 if (bitmap.width() >= 65500 || bitmap.height() >= 65500) return NULL;
1503 if (FLAGS_pdfJpegQuality == -1) return NULL;
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001504
commit-bot@chromium.org608ea652013-10-03 19:29:21 +00001505 SkBitmap bm = bitmap;
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001506#if defined(SK_BUILD_FOR_MAC)
1507 // Workaround bug #1043 where bitmaps with referenced pixels cause
1508 // CGImageDestinationFinalize to crash
1509 SkBitmap copy;
commit-bot@chromium.orgd5f032d2014-02-24 18:51:43 +00001510 bitmap.deepCopyTo(&copy);
commit-bot@chromium.org608ea652013-10-03 19:29:21 +00001511 bm = copy;
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001512#endif
1513
commit-bot@chromium.org608ea652013-10-03 19:29:21 +00001514 SkPixelRef* pr = bm.pixelRef();
1515 if (pr != NULL) {
1516 SkData* data = pr->refEncodedData();
1517 if (data != NULL) {
commit-bot@chromium.org608ea652013-10-03 19:29:21 +00001518 return data;
1519 }
1520 }
1521
commit-bot@chromium.org608ea652013-10-03 19:29:21 +00001522 return SkImageEncoder::EncodeData(bm,
1523 SkImageEncoder::kJPEG_Type,
1524 FLAGS_pdfJpegQuality);
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001525}
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001526
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001527static int findConfig(const char config[]) {
1528 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1529 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001530 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001531 }
1532 }
1533 return -1;
1534}
1535
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001536static const PDFRasterizerData* findPDFRasterizer(const char rasterizer[]) {
1537 for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); i++) {
1538 if (!strcmp(rasterizer, kPDFRasterizers[i].fName)) {
1539 return &kPDFRasterizers[i];
1540 }
1541 }
1542 return NULL;
1543}
1544
reed@google.comfb2cd422013-01-04 14:43:03 +00001545template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1546 int index = array->find(value);
1547 if (index < 0) {
1548 *array->append() = value;
1549 }
1550}
1551
epoger@google.com80724df2013-03-21 13:49:54 +00001552/**
epoger@google.com80724df2013-03-21 13:49:54 +00001553 * Run this test in a number of different drawing modes (pipe,
1554 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1555 * *exactly* match comparisonBitmap.
1556 *
1557 * Returns all errors encountered while doing so.
1558 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001559ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1560 const SkBitmap &comparisonBitmap,
1561 const SkTDArray<SkScalar> &tileGridReplayScales);
1562ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1563 const SkBitmap &comparisonBitmap,
1564 const SkTDArray<SkScalar> &tileGridReplayScales) {
1565 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001566 uint32_t gmFlags = gm->getFlags();
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001567 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->getName(),
epoger@google.com659c8c02013-05-21 15:45:45 +00001568 compareConfig.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001569
epoger@google.comc8263e72013-04-10 12:17:34 +00001570 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1571 SkAutoUnref aur(pict);
1572 if (FLAGS_replay) {
1573 const char renderModeDescriptor[] = "-replay";
1574 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001575 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1576 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001577 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1578 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001579 SkBitmap bitmap;
1580 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001581 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001582 gm->getName(), compareConfig.fName, renderModeDescriptor, bitmap,
epoger@google.com659c8c02013-05-21 15:45:45 +00001583 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001584 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001585 }
epoger@google.com80724df2013-03-21 13:49:54 +00001586
epoger@google.comc8263e72013-04-10 12:17:34 +00001587 if (FLAGS_serialize) {
1588 const char renderModeDescriptor[] = "-serialize";
1589 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001590 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1591 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001592 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1593 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001594 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1595 SkAutoUnref aurr(repict);
epoger@google.com80724df2013-03-21 13:49:54 +00001596 SkBitmap bitmap;
1597 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001598 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001599 gm->getName(), compareConfig.fName, renderModeDescriptor, bitmap,
epoger@google.com659c8c02013-05-21 15:45:45 +00001600 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001601 }
epoger@google.com80724df2013-03-21 13:49:54 +00001602 }
1603
epoger@google.comc8263e72013-04-10 12:17:34 +00001604 if ((1 == FLAGS_writePicturePath.count()) &&
1605 !(gmFlags & GM::kSkipPicture_Flag)) {
1606 const char* pictureSuffix = "skp";
epoger@google.com659c8c02013-05-21 15:45:45 +00001607 // TODO(epoger): Make sure this still works even though the
1608 // filename now contains the config name (it used to contain
1609 // just the shortName). I think this is actually an
1610 // *improvement*, because now runs with different configs will
1611 // write out their SkPictures to separate files rather than
1612 // overwriting each other. But we should make sure it doesn't
1613 // break anybody.
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001614 SkString path = gmmain.make_filename(FLAGS_writePicturePath[0], gm->getName(),
epoger@google.com659c8c02013-05-21 15:45:45 +00001615 compareConfig.fName, "", pictureSuffix);
epoger@google.comc8263e72013-04-10 12:17:34 +00001616 SkFILEWStream stream(path.c_str());
1617 pict->serialize(&stream);
epoger@google.com80724df2013-03-21 13:49:54 +00001618 }
1619
epoger@google.comc8263e72013-04-10 12:17:34 +00001620 if (FLAGS_rtree) {
1621 const char renderModeDescriptor[] = "-rtree";
commit-bot@chromium.orgd393b172014-04-16 16:02:10 +00001622 if ((gmFlags & GM::kSkipPicture_Flag) || (gmFlags & GM::kSkipTiled_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 {
commit-bot@chromium.orgd393b172014-04-16 16:02:10 +00001627 SkPicture* pict = gmmain.generate_new_picture(gm, kRTree_BbhType, 0);
1628 SkAutoUnref aur(pict);
1629 SkBitmap bitmap;
1630 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
1631 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1632 gm->getName(), compareConfig.fName, renderModeDescriptor, bitmap,
1633 &comparisonBitmap));
1634 }
1635 }
1636
1637 if (FLAGS_quadtree) {
1638 const char renderModeDescriptor[] = "-quadtree";
1639 if ((gmFlags & GM::kSkipPicture_Flag) || (gmFlags & GM::kSkipTiled_Flag)) {
1640 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1641 renderModeDescriptor);
1642 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1643 } else {
1644 SkPicture* pict = gmmain.generate_new_picture(gm, kQuadTree_BbhType, 0);
epoger@google.com80724df2013-03-21 13:49:54 +00001645 SkAutoUnref aur(pict);
1646 SkBitmap bitmap;
epoger@google.comc8263e72013-04-10 12:17:34 +00001647 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001648 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001649 gm->getName(), compareConfig.fName, renderModeDescriptor, bitmap,
epoger@google.com659c8c02013-05-21 15:45:45 +00001650 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001651 }
1652 }
1653
1654 if (FLAGS_tileGrid) {
1655 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1656 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1657 SkString renderModeDescriptor("-tilegrid");
1658 if (SK_Scalar1 != replayScale) {
1659 renderModeDescriptor += "-scale-";
1660 renderModeDescriptor.appendScalar(replayScale);
1661 }
1662
1663 if ((gmFlags & GM::kSkipPicture_Flag) ||
senorblanco@chromium.orgd4d44f02014-02-20 17:17:57 +00001664 (gmFlags & GM::kSkipTiled_Flag) ||
epoger@google.comc8263e72013-04-10 12:17:34 +00001665 ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001666 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001667 renderModeDescriptor.c_str());
1668 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1669 } else {
1670 // We record with the reciprocal scale to obtain a replay
1671 // result that can be validated against comparisonBitmap.
1672 SkScalar recordScale = SkScalarInvert(replayScale);
1673 SkPicture* pict = gmmain.generate_new_picture(
1674 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag,
1675 recordScale);
1676 SkAutoUnref aur(pict);
1677 SkBitmap bitmap;
1678 // We cannot yet pass 'true' to generate_image_from_picture to
1679 // perform actual tiled rendering (see Issue 1198 -
1680 // https://code.google.com/p/skia/issues/detail?id=1198)
1681 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
1682 replayScale /*, true */);
1683 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001684 gm->getName(), compareConfig.fName, renderModeDescriptor.c_str(), bitmap,
epoger@google.com659c8c02013-05-21 15:45:45 +00001685 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001686 }
epoger@google.com80724df2013-03-21 13:49:54 +00001687 }
1688 }
1689
1690 // run the pipe centric GM steps
epoger@google.comc8263e72013-04-10 12:17:34 +00001691 if (FLAGS_pipe) {
1692 errorsForAllModes.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap,
1693 FLAGS_simulatePipePlaybackFailure));
1694 if (FLAGS_tiledPipe) {
1695 errorsForAllModes.add(gmmain.test_tiled_pipe_playback(gm, compareConfig,
1696 comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001697 }
epoger@google.com80724df2013-03-21 13:49:54 +00001698 }
1699 return errorsForAllModes;
1700}
1701
commit-bot@chromium.org85195482014-01-13 18:27:59 +00001702
1703/**
1704 * Run this test in a number of different configs (8888, 565, PDF,
1705 * etc.), confirming that the resulting bitmaps match expectations
1706 * (which may be different for each config).
1707 *
1708 * Returns all errors encountered while doing so.
1709 */
1710ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm,
1711 const SkTDArray<size_t> &configs,
1712 const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
1713 const SkTDArray<SkScalar> &tileGridReplayScales,
1714 GrContextFactory *grFactory);
1715ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm,
1716 const SkTDArray<size_t> &configs,
1717 const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
1718 const SkTDArray<SkScalar> &tileGridReplayScales,
1719 GrContextFactory *grFactory) {
1720 const char renderModeDescriptor[] = "";
1721 ErrorCombination errorsForAllConfigs;
1722 uint32_t gmFlags = gm->getFlags();
1723
1724 for (int i = 0; i < configs.count(); i++) {
1725 ConfigData config = gRec[configs[i]];
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001726 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->getName(),
commit-bot@chromium.org85195482014-01-13 18:27:59 +00001727 config.fName);
1728
1729 // Skip any tests that we don't even need to try.
1730 // If any of these were skipped on a per-GM basis, record them as
1731 // kIntentionallySkipped.
1732 if (kPDF_Backend == config.fBackend) {
1733 if (gmFlags & GM::kSkipPDF_Flag) {
1734 gmmain.RecordSkippedTest(shortNamePlusConfig,
1735 renderModeDescriptor,
1736 config.fBackend);
1737 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1738 continue;
1739 }
1740 }
1741 if ((gmFlags & GM::kSkip565_Flag) &&
1742 (kRaster_Backend == config.fBackend) &&
1743 (SkBitmap::kRGB_565_Config == config.fConfig)) {
1744 gmmain.RecordSkippedTest(shortNamePlusConfig,
1745 renderModeDescriptor,
1746 config.fBackend);
1747 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1748 continue;
1749 }
1750 if (((gmFlags & GM::kSkipGPU_Flag) && kGPU_Backend == config.fBackend) ||
1751 ((gmFlags & GM::kGPUOnly_Flag) && kGPU_Backend != config.fBackend)) {
1752 gmmain.RecordSkippedTest(shortNamePlusConfig,
1753 renderModeDescriptor,
1754 config.fBackend);
1755 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1756 continue;
1757 }
1758
1759 // Now we know that we want to run this test and record its
1760 // success or failure.
1761 ErrorCombination errorsForThisConfig;
1762 GrSurface* gpuTarget = NULL;
1763#if SK_SUPPORT_GPU
1764 SkAutoTUnref<GrSurface> auGpuTarget;
1765 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
1766 if (FLAGS_resetGpuContext) {
1767 grFactory->destroyContexts();
1768 }
1769 GrContext* gr = grFactory->get(config.fGLContextType);
1770 bool grSuccess = false;
1771 if (gr) {
1772 // create a render target to back the device
1773 GrTextureDesc desc;
1774 desc.fConfig = kSkia8888_GrPixelConfig;
1775 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1776 desc.fWidth = gm->getISize().width();
1777 desc.fHeight = gm->getISize().height();
1778 desc.fSampleCnt = config.fSampleCnt;
1779 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1780 if (NULL != auGpuTarget) {
1781 gpuTarget = auGpuTarget;
1782 grSuccess = true;
1783 // Set the user specified cache limits if non-default.
1784 size_t bytes;
1785 int count;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001786 gr->getResourceCacheLimits(&count, &bytes);
commit-bot@chromium.org85195482014-01-13 18:27:59 +00001787 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1788 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1789 }
1790 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1791 count = gGpuCacheSizeCount;
1792 }
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001793 gr->setResourceCacheLimits(count, bytes);
commit-bot@chromium.org85195482014-01-13 18:27:59 +00001794 }
1795 }
1796 if (!grSuccess) {
1797 errorsForThisConfig.add(kNoGpuContext_ErrorType);
1798 }
1799 }
1800#endif
1801
1802 SkBitmap comparisonBitmap;
1803
1804 const char* writePath;
1805 if (FLAGS_writePath.count() == 1) {
1806 writePath = FLAGS_writePath[0];
1807 } else {
1808 writePath = NULL;
1809 }
1810
1811 if (errorsForThisConfig.isEmpty()) {
1812 errorsForThisConfig.add(gmmain.test_drawing(gm, config, pdfRasterizers,
1813 writePath, gpuTarget,
1814 &comparisonBitmap));
1815 gmmain.RecordTestResults(errorsForThisConfig, shortNamePlusConfig, "");
1816 }
1817
1818 // TODO: run only if gmmain.test_drawing succeeded.
1819 if (kRaster_Backend == config.fBackend) {
1820 run_multiple_modes(gmmain, gm, config, comparisonBitmap, tileGridReplayScales);
1821 }
1822
1823 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1824 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1825 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
1826 gpuTarget));
1827 }
1828
1829 errorsForAllConfigs.add(errorsForThisConfig);
1830 }
1831 return errorsForAllConfigs;
1832}
1833
1834
epoger@google.com310478e2013-04-03 18:00:39 +00001835/**
epoger@google.comf711f322013-10-18 14:55:47 +00001836 * Read individual lines from a file, pushing them into the given array.
1837 *
1838 * @param filename path to the file to read
1839 * @param lines array of strings to add the lines to
1840 * @returns true if able to read lines from the file
1841 */
1842static bool read_lines_from_file(const char* filename, SkTArray<SkString> &lines) {
1843 SkAutoTUnref<SkStream> streamWrapper(SkStream::NewFromFile(filename));
1844 SkStream *stream = streamWrapper.get();
1845 if (!stream) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00001846 SkDebugf("unable to read file '%s'\n", filename);
epoger@google.comf711f322013-10-18 14:55:47 +00001847 return false;
1848 }
1849
1850 char c;
1851 SkString line;
1852 while (1 == stream->read(&c, 1)) {
1853 // If we hit either CR or LF, we've completed a line.
1854 //
1855 // TODO: If the file uses both CR and LF, this will return an extra blank
1856 // line for each line of the file. Which is OK for current purposes...
1857 //
1858 // TODO: Does this properly handle unicode? It doesn't matter for
1859 // current purposes...
1860 if ((c == 0x0d) || (c == 0x0a)) {
1861 lines.push_back(line);
1862 line.reset();
1863 } else {
1864 line.append(&c, 1);
1865 }
1866 }
1867 lines.push_back(line);
1868 return true;
1869}
1870
1871/**
epoger@google.com310478e2013-04-03 18:00:39 +00001872 * Return a list of all entries in an array of strings as a single string
1873 * of this form:
1874 * "item1", "item2", "item3"
1875 */
1876SkString list_all(const SkTArray<SkString> &stringArray);
1877SkString list_all(const SkTArray<SkString> &stringArray) {
1878 SkString total;
1879 for (int i = 0; i < stringArray.count(); i++) {
1880 if (i > 0) {
1881 total.append(", ");
1882 }
1883 total.append("\"");
1884 total.append(stringArray[i]);
1885 total.append("\"");
1886 }
1887 return total;
1888}
1889
1890/**
1891 * Return a list of configuration names, as a single string of this form:
1892 * "item1", "item2", "item3"
1893 *
1894 * @param configs configurations, as a list of indices into gRec
1895 */
1896SkString list_all_config_names(const SkTDArray<size_t> &configs);
1897SkString list_all_config_names(const SkTDArray<size_t> &configs) {
1898 SkString total;
1899 for (int i = 0; i < configs.count(); i++) {
1900 if (i > 0) {
1901 total.append(", ");
1902 }
1903 total.append("\"");
1904 total.append(gRec[configs[i]].fName);
1905 total.append("\"");
1906 }
1907 return total;
1908}
1909
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001910static bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1911 const SkTDArray<size_t> &configs,
1912 const SkTDArray<const PDFRasterizerData*>& pdfRasterizers) {
epoger@google.comfdea3252013-05-02 18:24:03 +00001913 if (!sk_mkdir(root)) {
1914 return false;
1915 }
1916 if (useFileHierarchy) {
1917 for (int i = 0; i < configs.count(); i++) {
1918 ConfigData config = gRec[configs[i]];
1919 SkString subdir;
1920 subdir.appendf("%s%c%s", root, SkPATH_SEPARATOR, config.fName);
1921 if (!sk_mkdir(subdir.c_str())) {
1922 return false;
1923 }
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001924
1925 if (config.fBackend == kPDF_Backend) {
1926 for (int j = 0; j < pdfRasterizers.count(); j++) {
1927 SkString pdfSubdir = subdir;
vandebo@chromium.org8fc37662013-08-21 18:04:09 +00001928 pdfSubdir.appendf("-%s", pdfRasterizers[j]->fName);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001929 if (!sk_mkdir(pdfSubdir.c_str())) {
1930 return false;
1931 }
1932 }
1933 }
epoger@google.comfdea3252013-05-02 18:24:03 +00001934 }
1935 }
1936 return true;
1937}
1938
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00001939static bool parse_flags_configs(SkTDArray<size_t>* outConfigs,
1940 GrContextFactory* grFactory) {
1941 SkTDArray<size_t> excludeConfigs;
1942
1943 for (int i = 0; i < FLAGS_config.count(); i++) {
1944 const char* config = FLAGS_config[i];
1945 bool exclude = false;
1946 if (*config == kExcludeConfigChar) {
1947 exclude = true;
1948 config += 1;
1949 }
1950 int index = findConfig(config);
1951 if (index >= 0) {
1952 if (exclude) {
1953 *excludeConfigs.append() = index;
1954 } else {
1955 appendUnique<size_t>(outConfigs, index);
1956 }
1957 } else if (0 == strcmp(kDefaultsConfigStr, config)) {
1958 if (exclude) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00001959 SkDebugf("%c%s is not allowed.\n",
1960 kExcludeConfigChar, kDefaultsConfigStr);
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00001961 return false;
1962 }
1963 for (size_t c = 0; c < SK_ARRAY_COUNT(gRec); ++c) {
1964 if (gRec[c].fRunByDefault) {
1965 appendUnique<size_t>(outConfigs, c);
1966 }
1967 }
1968 } else {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00001969 SkDebugf("unrecognized config %s\n", config);
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00001970 return false;
1971 }
1972 }
1973
1974 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1975 int index = findConfig(FLAGS_excludeConfig[i]);
1976 if (index >= 0) {
1977 *excludeConfigs.append() = index;
1978 } else {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00001979 SkDebugf("unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00001980 return false;
1981 }
1982 }
1983
1984 if (outConfigs->count() == 0) {
1985 // if no config is specified by user, add the defaults
1986 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1987 if (gRec[i].fRunByDefault) {
1988 *outConfigs->append() = i;
1989 }
1990 }
1991 }
1992 // now remove any explicitly excluded configs
1993 for (int i = 0; i < excludeConfigs.count(); ++i) {
1994 int index = outConfigs->find(excludeConfigs[i]);
1995 if (index >= 0) {
1996 outConfigs->remove(index);
1997 // now assert that there was only one copy in configs[]
1998 SkASSERT(outConfigs->find(excludeConfigs[i]) < 0);
1999 }
2000 }
2001
2002#if SK_SUPPORT_GPU
2003 SkASSERT(grFactory != NULL);
2004 for (int i = 0; i < outConfigs->count(); ++i) {
2005 size_t index = (*outConfigs)[i];
2006 if (kGPU_Backend == gRec[index].fBackend) {
2007 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
2008 if (NULL == ctx) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002009 SkDebugf("GrContext could not be created for config %s. Config will be skipped.\n",
2010 gRec[index].fName);
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002011 outConfigs->remove(i);
2012 --i;
2013 continue;
2014 }
2015 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002016 SkDebugf("Sample count (%d) of config %s is not supported."
2017 " Config will be skipped.\n",
2018 gRec[index].fSampleCnt, gRec[index].fName);
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002019 outConfigs->remove(i);
2020 --i;
2021 }
2022 }
2023 }
2024#endif
2025
2026 if (outConfigs->isEmpty()) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002027 SkDebugf("No configs to run.");
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002028 return false;
2029 }
2030
2031 // now show the user the set of configs that will be run.
2032 SkString configStr("These configs will be run:");
2033 // show the user the config that will run.
2034 for (int i = 0; i < outConfigs->count(); ++i) {
2035 configStr.appendf(" %s", gRec[(*outConfigs)[i]].fName);
2036 }
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002037 SkDebugf("%s\n", configStr.c_str());
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002038
2039 return true;
2040}
2041
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002042static bool parse_flags_pdf_rasterizers(const SkTDArray<size_t>& configs,
2043 SkTDArray<const PDFRasterizerData*>* outRasterizers) {
2044 // No need to run this check (and display the PDF rasterizers message)
2045 // if no PDF backends are in the configs.
2046 bool configHasPDF = false;
2047 for (int i = 0; i < configs.count(); i++) {
2048 if (gRec[configs[i]].fBackend == kPDF_Backend) {
2049 configHasPDF = true;
2050 break;
2051 }
2052 }
2053 if (!configHasPDF) {
2054 return true;
2055 }
2056
vandebo@chromium.orgf8afb2b2013-11-06 16:32:15 +00002057 if (FLAGS_pdfRasterizers.count() == 1 &&
2058 !strcmp(FLAGS_pdfRasterizers[0], "default")) {
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002059 for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
2060 if (kPDFRasterizers[i].fRunByDefault) {
2061 *outRasterizers->append() = &kPDFRasterizers[i];
2062 }
2063 }
vandebo@chromium.orgf8afb2b2013-11-06 16:32:15 +00002064 } else {
2065 for (int i = 0; i < FLAGS_pdfRasterizers.count(); i++) {
2066 const char* rasterizer = FLAGS_pdfRasterizers[i];
2067 const PDFRasterizerData* rasterizerPtr =
2068 findPDFRasterizer(rasterizer);
2069 if (rasterizerPtr == NULL) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002070 SkDebugf("unrecognized rasterizer %s\n", rasterizer);
vandebo@chromium.orgf8afb2b2013-11-06 16:32:15 +00002071 return false;
2072 }
2073 appendUnique<const PDFRasterizerData*>(outRasterizers,
2074 rasterizerPtr);
2075 }
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002076 }
2077
2078 // now show the user the set of configs that will be run.
2079 SkString configStr("These PDF rasterizers will be run:");
2080 // show the user the config that will run.
2081 for (int i = 0; i < outRasterizers->count(); ++i) {
2082 configStr.appendf(" %s", (*outRasterizers)[i]->fName);
2083 }
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002084 SkDebugf("%s\n", configStr.c_str());
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002085
2086 return true;
2087}
2088
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002089static bool parse_flags_ignore_error_types(ErrorCombination* outErrorTypes) {
2090 if (FLAGS_ignoreErrorTypes.count() > 0) {
2091 *outErrorTypes = ErrorCombination();
2092 for (int i = 0; i < FLAGS_ignoreErrorTypes.count(); i++) {
2093 ErrorType type;
2094 const char *name = FLAGS_ignoreErrorTypes[i];
2095 if (!getErrorTypeByName(name, &type)) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002096 SkDebugf("cannot find ErrorType with name '%s'\n", name);
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002097 return false;
2098 } else {
2099 outErrorTypes->add(type);
2100 }
2101 }
2102 }
2103 return true;
2104}
2105
epoger@google.com3a882dd2013-10-07 18:55:09 +00002106/**
commit-bot@chromium.org3e62ebf2014-01-14 02:54:11 +00002107 * Replace contents of ignoreTestNames with a list of test names, indicating
epoger@google.com3a882dd2013-10-07 18:55:09 +00002108 * which tests' failures should be ignored.
2109 */
commit-bot@chromium.org3e62ebf2014-01-14 02:54:11 +00002110static bool parse_flags_ignore_tests(SkTArray<SkString> &ignoreTestNames) {
2111 ignoreTestNames.reset();
epoger@google.comf711f322013-10-18 14:55:47 +00002112
epoger@google.comf711f322013-10-18 14:55:47 +00002113 // Parse --ignoreFailuresFile
2114 for (int i = 0; i < FLAGS_ignoreFailuresFile.count(); i++) {
2115 SkTArray<SkString> linesFromFile;
2116 if (!read_lines_from_file(FLAGS_ignoreFailuresFile[i], linesFromFile)) {
2117 return false;
2118 } else {
2119 for (int j = 0; j < linesFromFile.count(); j++) {
2120 SkString thisLine = linesFromFile[j];
2121 if (thisLine.isEmpty() || thisLine.startsWith('#')) {
2122 // skip this line
2123 } else {
commit-bot@chromium.org3e62ebf2014-01-14 02:54:11 +00002124 ignoreTestNames.push_back(thisLine);
epoger@google.comf711f322013-10-18 14:55:47 +00002125 }
2126 }
2127 }
2128 }
2129
epoger@google.com3a882dd2013-10-07 18:55:09 +00002130 return true;
2131}
2132
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002133static bool parse_flags_modulo(int* moduloRemainder, int* moduloDivisor) {
2134 if (FLAGS_modulo.count() == 2) {
2135 *moduloRemainder = atoi(FLAGS_modulo[0]);
2136 *moduloDivisor = atoi(FLAGS_modulo[1]);
2137 if (*moduloRemainder < 0 || *moduloDivisor <= 0 ||
2138 *moduloRemainder >= *moduloDivisor) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002139 SkDebugf("invalid modulo values.");
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002140 return false;
2141 }
2142 }
2143 return true;
2144}
2145
2146#if SK_SUPPORT_GPU
2147static bool parse_flags_gpu_cache(int* sizeBytes, int* sizeCount) {
2148 if (FLAGS_gpuCacheSize.count() > 0) {
2149 if (FLAGS_gpuCacheSize.count() != 2) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002150 SkDebugf("--gpuCacheSize requires two arguments\n");
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002151 return false;
2152 }
2153 *sizeBytes = atoi(FLAGS_gpuCacheSize[0]);
2154 *sizeCount = atoi(FLAGS_gpuCacheSize[1]);
2155 } else {
2156 *sizeBytes = DEFAULT_CACHE_VALUE;
2157 *sizeCount = DEFAULT_CACHE_VALUE;
2158 }
2159 return true;
2160}
2161#endif
2162
2163static bool parse_flags_tile_grid_replay_scales(SkTDArray<SkScalar>* outScales) {
2164 *outScales->append() = SK_Scalar1; // By default only test at scale 1.0
2165 if (FLAGS_tileGridReplayScales.count() > 0) {
2166 outScales->reset();
2167 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
2168 double val = atof(FLAGS_tileGridReplayScales[i]);
2169 if (0 < val) {
2170 *outScales->append() = SkDoubleToScalar(val);
2171 }
2172 }
2173 if (0 == outScales->count()) {
2174 // Should have at least one scale
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002175 SkDebugf("--tileGridReplayScales requires at least one scale.\n");
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002176 return false;
2177 }
2178 }
2179 return true;
2180}
2181
2182static bool parse_flags_gmmain_paths(GMMain* gmmain) {
2183 gmmain->fUseFileHierarchy = FLAGS_hierarchy;
2184 gmmain->fWriteChecksumBasedFilenames = FLAGS_writeChecksumBasedFilenames;
2185
2186 if (FLAGS_mismatchPath.count() == 1) {
2187 gmmain->fMismatchPath = FLAGS_mismatchPath[0];
2188 }
2189
2190 if (FLAGS_missingExpectationsPath.count() == 1) {
2191 gmmain->fMissingExpectationsPath = FLAGS_missingExpectationsPath[0];
2192 }
2193
2194 if (FLAGS_readPath.count() == 1) {
2195 const char* readPath = FLAGS_readPath[0];
2196 if (!sk_exists(readPath)) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002197 SkDebugf("readPath %s does not exist!\n", readPath);
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002198 return false;
2199 }
2200 if (sk_isdir(readPath)) {
2201 if (FLAGS_verbose) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002202 SkDebugf("reading from %s\n", readPath);
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002203 }
2204 gmmain->fExpectationsSource.reset(SkNEW_ARGS(
2205 IndividualImageExpectationsSource, (readPath)));
2206 } else {
commit-bot@chromium.org25c10662014-05-16 17:56:43 +00002207#ifdef SK_BUILD_JSON_WRITER
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002208 if (FLAGS_verbose) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002209 SkDebugf("reading expectations from JSON summary file %s\n", readPath);
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002210 }
2211 gmmain->fExpectationsSource.reset(SkNEW_ARGS(JsonExpectationsSource, (readPath)));
commit-bot@chromium.org25c10662014-05-16 17:56:43 +00002212#endif
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002213 }
2214 }
2215 return true;
2216}
2217
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002218static bool parse_flags_resource_path() {
2219 if (FLAGS_resourcePath.count() == 1) {
2220 GM::SetResourcePath(FLAGS_resourcePath[0]);
2221 }
2222 return true;
2223}
2224
2225static bool parse_flags_jpeg_quality() {
2226 if (FLAGS_pdfJpegQuality < -1 || FLAGS_pdfJpegQuality > 100) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002227 SkDebugf("%s\n", "pdfJpegQuality must be in [-1 .. 100] range.");
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002228 return false;
2229 }
2230 return true;
2231}
2232
epoger@google.com0b62b3d2013-03-20 17:59:28 +00002233int tool_main(int argc, char** argv);
2234int tool_main(int argc, char** argv) {
2235
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +00002236 SkString usage;
2237 usage.printf("Run the golden master tests.\n");
2238 SkCommandLineFlags::SetUsage(usage.c_str());
2239 SkCommandLineFlags::Parse(argc, argv);
2240
epoger@google.com0b62b3d2013-03-20 17:59:28 +00002241#if SK_ENABLE_INST_COUNT
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +00002242 if (FLAGS_leaks) {
2243 gPrintInstCount = true;
2244 }
epoger@google.com0b62b3d2013-03-20 17:59:28 +00002245#endif
2246
2247 SkGraphics::Init();
epoger@google.com0b62b3d2013-03-20 17:59:28 +00002248
2249 setSystemPreferences();
2250 GMMain gmmain;
2251
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002252 SkTDArray<size_t> configs;
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002253
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002254 int moduloRemainder = -1;
2255 int moduloDivisor = -1;
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002256 SkTDArray<const PDFRasterizerData*> pdfRasterizers;
epoger@google.com6f6568b2013-03-22 17:29:46 +00002257 SkTDArray<SkScalar> tileGridReplayScales;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002258#if SK_SUPPORT_GPU
2259 GrContextFactory* grFactory = new GrContextFactory;
epoger@google.com80724df2013-03-21 13:49:54 +00002260#else
2261 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002262#endif
2263
commit-bot@chromium.orgb17ccc92014-03-13 16:16:36 +00002264 if (FLAGS_dryRun) {
2265 SkDebugf( "Doing a dry run; no tests will actually be executed.\n");
2266 }
2267
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002268 if (!parse_flags_modulo(&moduloRemainder, &moduloDivisor) ||
2269 !parse_flags_ignore_error_types(&gmmain.fIgnorableErrorTypes) ||
commit-bot@chromium.org3e62ebf2014-01-14 02:54:11 +00002270 !parse_flags_ignore_tests(gmmain.fIgnorableTestNames) ||
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002271#if SK_SUPPORT_GPU
2272 !parse_flags_gpu_cache(&gGpuCacheSizeBytes, &gGpuCacheSizeCount) ||
2273#endif
2274 !parse_flags_tile_grid_replay_scales(&tileGridReplayScales) ||
2275 !parse_flags_resource_path() ||
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002276 !parse_flags_jpeg_quality() ||
2277 !parse_flags_configs(&configs, grFactory) ||
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002278 !parse_flags_pdf_rasterizers(configs, &pdfRasterizers) ||
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002279 !parse_flags_gmmain_paths(&gmmain)) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00002280 return -1;
2281 }
2282
epoger@google.comb0f8b432013-04-10 18:46:25 +00002283 if (FLAGS_verbose) {
2284 if (FLAGS_writePath.count() == 1) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002285 SkDebugf("writing to %s\n", FLAGS_writePath[0]);
epoger@google.comb0f8b432013-04-10 18:46:25 +00002286 }
epoger@google.comfdea3252013-05-02 18:24:03 +00002287 if (NULL != gmmain.fMismatchPath) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002288 SkDebugf("writing mismatches to %s\n", gmmain.fMismatchPath);
epoger@google.comfdea3252013-05-02 18:24:03 +00002289 }
epoger@google.com5f995452013-06-21 18:16:47 +00002290 if (NULL != gmmain.fMissingExpectationsPath) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002291 SkDebugf("writing images without expectations to %s\n",
2292 gmmain.fMissingExpectationsPath);
epoger@google.com5f995452013-06-21 18:16:47 +00002293 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00002294 if (FLAGS_writePicturePath.count() == 1) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002295 SkDebugf("writing pictures to %s\n", FLAGS_writePicturePath[0]);
epoger@google.comb0f8b432013-04-10 18:46:25 +00002296 }
2297 if (FLAGS_resourcePath.count() == 1) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002298 SkDebugf("reading resources from %s\n", FLAGS_resourcePath[0]);
epoger@google.comb0f8b432013-04-10 18:46:25 +00002299 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00002300 }
2301
epoger@google.com310478e2013-04-03 18:00:39 +00002302 int gmsRun = 0;
reed@google.comae7b8f32012-10-18 21:30:57 +00002303 int gmIndex = -1;
2304 SkString moduloStr;
2305
commit-bot@chromium.orgb17ccc92014-03-13 16:16:36 +00002306 if (!FLAGS_dryRun) {
2307 // If we will be writing out files, prepare subdirectories.
2308 if (FLAGS_writePath.count() == 1) {
2309 if (!prepare_subdirectories(FLAGS_writePath[0], gmmain.fUseFileHierarchy,
2310 configs, pdfRasterizers)) {
2311 return -1;
2312 }
2313 }
2314 if (NULL != gmmain.fMismatchPath) {
2315 if (!prepare_subdirectories(gmmain.fMismatchPath, gmmain.fUseFileHierarchy,
2316 configs, pdfRasterizers)) {
2317 return -1;
2318 }
2319 }
2320 if (NULL != gmmain.fMissingExpectationsPath) {
2321 if (!prepare_subdirectories(gmmain.fMissingExpectationsPath, gmmain.fUseFileHierarchy,
2322 configs, pdfRasterizers)) {
2323 return -1;
2324 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00002325 }
epoger@google.comfdea3252013-05-02 18:24:03 +00002326 }
bsalomon@google.com7361f542012-04-19 19:15:35 +00002327 Iter iter;
2328 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00002329 while ((gm = iter.next()) != NULL) {
commit-bot@chromium.orgf4f9df42013-09-26 20:44:24 +00002330 if (FLAGS_forcePerspectiveMatrix) {
2331 SkMatrix perspective;
2332 perspective.setIdentity();
2333 perspective.setPerspY(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000)));
2334 perspective.setSkewX(SkScalarDiv(SkIntToScalar(8),
2335 SkIntToScalar(25)));
2336
2337 gm->setStarterMatrix(perspective);
skia.committer@gmail.com65caeaf2013-09-27 07:01:29 +00002338 }
scroggo@google.com7fd2d702013-04-16 19:11:14 +00002339 SkAutoTDelete<GM> adgm(gm);
reed@google.comae7b8f32012-10-18 21:30:57 +00002340 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00002341 if (moduloRemainder >= 0) {
2342 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00002343 continue;
2344 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00002345 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00002346 }
2347
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00002348 const char* shortName = gm->getName();
sglez@google.com586db932013-07-24 17:24:23 +00002349
commit-bot@chromium.orga6f37e72013-08-30 15:52:46 +00002350 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00002351 continue;
2352 }
2353
epoger@google.com310478e2013-04-03 18:00:39 +00002354 gmsRun++;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00002355 SkISize size = gm->getISize();
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002356 SkDebugf("%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
2357 size.width(), size.height());
commit-bot@chromium.orgb17ccc92014-03-13 16:16:36 +00002358 if (!FLAGS_dryRun)
2359 run_multiple_configs(gmmain, gm, configs, pdfRasterizers, tileGridReplayScales, grFactory);
reed@android.com00dae862009-06-10 15:38:48 +00002360 }
epoger@google.com310478e2013-04-03 18:00:39 +00002361
commit-bot@chromium.orgb17ccc92014-03-13 16:16:36 +00002362 if (FLAGS_dryRun)
2363 return 0;
2364
epoger@google.com310478e2013-04-03 18:00:39 +00002365 SkTArray<SkString> modes;
2366 gmmain.GetRenderModesEncountered(modes);
commit-bot@chromium.org85195482014-01-13 18:27:59 +00002367 int modeCount = modes.count();
2368
2369 // Now that we have run all the tests and thus know the full set of renderModes that we
2370 // tried to run, we can call RecordTestResults() to record the cases in which we skipped
2371 // ALL renderModes.
2372 // See http://skbug.com/1994 and https://codereview.chromium.org/129203002/
2373 int testCount = gmmain.fTestsSkippedOnAllRenderModes.count();
2374 for (int testNum = 0; testNum < testCount; ++testNum) {
2375 const SkString &shortNamePlusConfig = gmmain.fTestsSkippedOnAllRenderModes[testNum];
2376 for (int modeNum = 0; modeNum < modeCount; ++modeNum) {
2377 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
2378 modes[modeNum].c_str());
2379 }
2380 }
2381
epoger@google.comc8263e72013-04-10 12:17:34 +00002382 bool reportError = false;
2383 if (gmmain.NumSignificantErrors() > 0) {
2384 reportError = true;
2385 }
commit-bot@chromium.org85195482014-01-13 18:27:59 +00002386
2387 // We test every GM against every config, and for every raster config also test every mode.
2388 int rasterConfigs = 0;
2389 for (int i = 0; i < configs.count(); i++) {
2390 if (gRec[configs[i]].fBackend == kRaster_Backend) {
2391 rasterConfigs++;
2392 }
2393 }
2394 // For raster configs, we run all renderModes; for non-raster configs, just default renderMode
2395 const int expectedNumberOfTests = rasterConfigs * gmsRun * modeCount
2396 + (configs.count() - rasterConfigs) * gmsRun;
epoger@google.com310478e2013-04-03 18:00:39 +00002397
2398 // Output summary to stdout.
epoger@google.com51dbabe2013-04-10 15:24:53 +00002399 if (FLAGS_verbose) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002400 SkDebugf("Ran %d GMs\n", gmsRun);
2401 SkDebugf("... over %2d configs [%s]\n", configs.count(),
2402 list_all_config_names(configs).c_str());
2403 SkDebugf("... and %2d modes [%s]\n", modeCount, list_all(modes).c_str());
2404 SkDebugf("... so there should be a total of %d tests.\n", expectedNumberOfTests);
epoger@google.com51dbabe2013-04-10 15:24:53 +00002405 }
2406 gmmain.ListErrors(FLAGS_verbose);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002407
epoger@google.com07947d92013-04-11 15:41:02 +00002408 // TODO(epoger): Enable this check for Android, too, once we resolve
2409 // https://code.google.com/p/skia/issues/detail?id=1222
2410 // ('GM is unexpectedly skipping tests on Android')
2411#ifndef SK_BUILD_FOR_ANDROID
epoger@google.comc8263e72013-04-10 12:17:34 +00002412 if (expectedNumberOfTests != gmmain.fTestsRun) {
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002413 SkDebugf("expected %d tests, but ran or skipped %d tests\n",
2414 expectedNumberOfTests, gmmain.fTestsRun);
epoger@google.comc8263e72013-04-10 12:17:34 +00002415 reportError = true;
2416 }
2417#endif
2418
commit-bot@chromium.org25c10662014-05-16 17:56:43 +00002419#ifdef SK_BUILD_JSON_WRITER
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002420 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.com76c913d2013-04-26 15:06:44 +00002421 Json::Value root = CreateJsonTree(
2422 gmmain.fJsonExpectedResults,
2423 gmmain.fJsonActualResults_Failed, gmmain.fJsonActualResults_FailureIgnored,
2424 gmmain.fJsonActualResults_NoComparison, gmmain.fJsonActualResults_Succeeded);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002425 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002426 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002427 stream.write(jsonStdString.c_str(), jsonStdString.length());
2428 }
commit-bot@chromium.org25c10662014-05-16 17:56:43 +00002429#endif
epoger@google.comee8a8e32012-12-18 19:13:49 +00002430
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002431#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002432
robertphillips@google.com59552022012-08-31 13:07:37 +00002433#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002434 for (int i = 0; i < configs.count(); i++) {
2435 ConfigData config = gRec[configs[i]];
2436
epoger@google.comb0f8b432013-04-10 18:46:25 +00002437 if (FLAGS_verbose && (kGPU_Backend == config.fBackend)) {
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002438 GrContext* gr = grFactory->get(config.fGLContextType);
2439
commit-bot@chromium.org2b3a2042014-02-27 18:45:26 +00002440 SkDebugf("config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002441 gr->printCacheStats();
2442 }
2443 }
2444#endif
2445
commit-bot@chromium.org8065ec52014-03-11 15:57:40 +00002446#if GR_DUMP_FONT_CACHE
2447 for (int i = 0; i < configs.count(); i++) {
2448 ConfigData config = gRec[configs[i]];
2449
2450 if (kGPU_Backend == config.fBackend) {
2451 GrContext* gr = grFactory->get(config.fGLContextType);
2452
2453 gr->dumpFontCache();
2454 }
2455 }
2456#endif
2457
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002458 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002459#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002460 SkGraphics::Term();
2461
epoger@google.comc8263e72013-04-10 12:17:34 +00002462 return (reportError) ? -1 : 0;
reed@android.com00dae862009-06-10 15:38:48 +00002463}
caryclark@google.com5987f582012-10-02 18:33:14 +00002464
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002465void GMMain::installFilter(SkCanvas* canvas) {
2466 if (FLAGS_forceBWtext) {
2467 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
2468 }
2469}
2470
borenet@google.com7158e6a2012-11-01 17:43:44 +00002471#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00002472int main(int argc, char * const argv[]) {
2473 return tool_main(argc, (char**) argv);
2474}
2475#endif