blob: ea749a5c71efbf9bada2a2dfcbd4c41f98186dca [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
bsalomon@google.com971d0c82011-08-19 17:22:05 +00007
epoger@google.com57f7abc2012-11-13 03:41:55 +00008/*
9 * Code for the "gm" (Golden Master) rendering comparison tool.
10 *
11 * If you make changes to this, re-run the self-tests at gm/tests/run.sh
12 * to make sure they still pass... you may need to change the expected
13 * results of the self-test.
14 */
15
bungeman@google.comb29c8832011-10-10 13:19:10 +000016#include "gm.h"
epoger@google.com6f6568b2013-03-22 17:29:46 +000017#include "gm_error.h"
epoger@google.com37269602013-01-19 04:21:27 +000018#include "gm_expectations.h"
epoger@google.com7bc13a62012-02-14 14:53:59 +000019#include "system_preferences.h"
epoger@google.com5f6a0072013-01-31 16:30:55 +000020#include "SkBitmap.h"
reed@android.comb9b9a182009-07-08 02:54:47 +000021#include "SkColorPriv.h"
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000022#include "SkCommandLineFlags.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000023#include "SkData.h"
junov@google.com4370aed2012-01-18 16:21:08 +000024#include "SkDeferredCanvas.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000025#include "SkDevice.h"
epoger@google.comde961632012-10-26 18:56:36 +000026#include "SkDrawFilter.h"
commit-bot@chromium.orgc41295d2013-06-18 20:06:36 +000027#include "SkForceLinking.h"
scroggo@google.com5af9b202012-06-04 17:17:36 +000028#include "SkGPipe.h"
reed@android.com8015dd82009-06-21 00:49:18 +000029#include "SkGraphics.h"
30#include "SkImageDecoder.h"
31#include "SkImageEncoder.h"
epoger@google.come8ebeb12012-10-29 16:42:11 +000032#include "SkOSFile.h"
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +000033#include "SkPDFRasterizer.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000034#include "SkPicture.h"
robertphillips@google.com977b9c82012-06-05 19:35:09 +000035#include "SkRefCnt.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000036#include "SkStream.h"
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000037#include "SkTArray.h"
epoger@google.com310478e2013-04-03 18:00:39 +000038#include "SkTDict.h"
junov@chromium.org3cb834b2012-12-13 16:39:53 +000039#include "SkTileGridPicture.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000040#include "SamplePipeControllers.h"
reed@google.com07700442010-12-20 19:46:07 +000041
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000042#ifdef SK_DEBUG
43static const bool kDebugOnly = true;
44#else
45static const bool kDebugOnly = false;
46#endif
47
epoger@google.comed5eb4e2013-07-23 17:56:20 +000048__SK_FORCE_IMAGE_DECODER_LINKING;
49
bsalomon@google.com50c79d82013-01-08 20:31:53 +000050#ifdef SK_BUILD_FOR_WIN
51 // json includes xlocale which generates warning 4530 because we're compiling without
epoger@google.com37269602013-01-19 04:21:27 +000052 // exceptions; see https://code.google.com/p/skia/issues/detail?id=1067
bsalomon@google.com50c79d82013-01-08 20:31:53 +000053 #pragma warning(push)
54 #pragma warning(disable : 4530)
55#endif
epoger@google.comee8a8e32012-12-18 19:13:49 +000056#include "json/value.h"
bsalomon@google.com50c79d82013-01-08 20:31:53 +000057#ifdef SK_BUILD_FOR_WIN
58 #pragma warning(pop)
59#endif
epoger@google.comee8a8e32012-12-18 19:13:49 +000060
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000061#if SK_SUPPORT_GPU
62#include "GrContextFactory.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000063#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000064typedef GrContextFactory::GLContextType GLContextType;
epoger@google.com6f6568b2013-03-22 17:29:46 +000065#define DEFAULT_CACHE_VALUE -1
66static int gGpuCacheSizeBytes;
67static int gGpuCacheSizeCount;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000068#else
epoger@google.com80724df2013-03-21 13:49:54 +000069class GrContextFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000070class GrContext;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +000071class GrSurface;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000072typedef int GLContextType;
73#endif
74
epoger@google.com76c913d2013-04-26 15:06:44 +000075#define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message")
76
reed@google.com8923c6c2011-11-08 14:59:38 +000077extern bool gSkSuppressFontCachePurgeSpew;
78
reed@google.com07700442010-12-20 19:46:07 +000079#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000080 #include "SkPDFDevice.h"
81 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000082#endif
reed@android.com00dae862009-06-10 15:38:48 +000083
epoger@google.come3cc2eb2012-01-18 20:11:13 +000084// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
85// stop writing out XPS-format image baselines in gm.
86#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000087#ifdef SK_SUPPORT_XPS
88 #include "SkXPSDevice.h"
89#endif
90
reed@google.com46cce912011-06-29 12:54:46 +000091#ifdef SK_BUILD_FOR_MAC
92 #include "SkCGUtils.h"
reed@google.com46cce912011-06-29 12:54:46 +000093#endif
94
reed@android.com00dae862009-06-10 15:38:48 +000095using namespace skiagm;
96
reed@android.com00dae862009-06-10 15:38:48 +000097class Iter {
98public:
99 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +0000100 this->reset();
101 }
102
103 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +0000104 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +0000105 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000106
reed@android.comdd0ac282009-06-20 02:38:16 +0000107 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +0000108 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000109 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +0000110 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +0000111 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +0000112 }
113 return NULL;
114 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000115
reed@android.com00dae862009-06-10 15:38:48 +0000116 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +0000117 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +0000118 int count = 0;
119 while (reg) {
120 count += 1;
121 reg = reg->next();
122 }
123 return count;
124 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000125
reed@android.com00dae862009-06-10 15:38:48 +0000126private:
127 const GMRegistry* fReg;
128};
129
epoger@google.comce057fe2013-05-14 15:17:46 +0000130// TODO(epoger): Right now, various places in this code assume that all the
131// image files read/written by GM use this file extension.
132// Search for references to this constant to find these assumptions.
133const static char kPNG_FileExtension[] = "png";
134
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000135enum Backend {
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000136 kRaster_Backend,
137 kGPU_Backend,
138 kPDF_Backend,
139 kXPS_Backend,
140};
141
142enum BbhType {
143 kNone_BbhType,
144 kRTree_BbhType,
145 kTileGrid_BbhType,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000146};
147
bsalomon@google.com7361f542012-04-19 19:15:35 +0000148enum ConfigFlags {
149 kNone_ConfigFlag = 0x0,
150 /* Write GM images if a write path is provided. */
151 kWrite_ConfigFlag = 0x1,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000152 /* Read reference GM images if a read path is provided. */
bsalomon@google.com7361f542012-04-19 19:15:35 +0000153 kRead_ConfigFlag = 0x2,
154 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
155};
156
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000157struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000158 SkBitmap::Config fConfig;
159 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000160 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000161 int fSampleCnt; // GPU backend only
162 ConfigFlags fFlags;
163 const char* fName;
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000164 bool fRunByDefault;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000165};
166
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000167struct PDFRasterizerData {
168 bool (*fRasterizerFunction)(SkStream*, SkBitmap*);
169 const char* fName;
170 bool fRunByDefault;
171};
172
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000173class BWTextDrawFilter : public SkDrawFilter {
174public:
reed@google.com971aca72012-11-26 20:26:54 +0000175 virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000176};
reed@google.com971aca72012-11-26 20:26:54 +0000177bool BWTextDrawFilter::filter(SkPaint* p, Type t) {
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000178 if (kText_Type == t) {
179 p->setAntiAlias(false);
180 }
reed@google.com971aca72012-11-26 20:26:54 +0000181 return true;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000182}
183
scroggo@google.com565254b2012-06-28 15:41:32 +0000184struct PipeFlagComboData {
185 const char* name;
186 uint32_t flags;
187};
188
189static PipeFlagComboData gPipeWritingFlagCombos[] = {
190 { "", 0 },
191 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000192 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000193 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000194};
195
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000196static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect);
197
epoger@google.com5079d2c2013-04-12 14:11:21 +0000198const static ErrorCombination kDefaultIgnorableErrorTypes = ErrorCombination()
199 .plus(kMissingExpectations_ErrorType)
200 .plus(kIntentionallySkipped_ErrorType);
201
epoger@google.comde961632012-10-26 18:56:36 +0000202class GMMain {
203public:
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000204 GMMain() : fUseFileHierarchy(false), fWriteChecksumBasedFilenames(false),
205 fIgnorableErrorTypes(kDefaultIgnorableErrorTypes),
epoger@google.com5f995452013-06-21 18:16:47 +0000206 fMismatchPath(NULL), fMissingExpectationsPath(NULL), fTestsRun(0),
207 fRenderModesEncountered(1) {}
epoger@google.come8ebeb12012-10-29 16:42:11 +0000208
epoger@google.com659c8c02013-05-21 15:45:45 +0000209 /**
210 * Assemble shortNamePlusConfig from (surprise!) shortName and configName.
211 *
212 * The method for doing so depends on whether we are using hierarchical naming.
213 * For example, shortName "selftest1" and configName "8888" could be assembled into
214 * either "selftest1_8888" or "8888/selftest1".
215 */
216 SkString make_shortname_plus_config(const char *shortName, const char *configName) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000217 SkString name;
epoger@google.com57f7abc2012-11-13 03:41:55 +0000218 if (0 == strlen(configName)) {
219 name.append(shortName);
220 } else if (fUseFileHierarchy) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000221 name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
222 } else {
223 name.appendf("%s_%s", shortName, configName);
224 }
epoger@google.comde961632012-10-26 18:56:36 +0000225 return name;
226 }
227
epoger@google.com659c8c02013-05-21 15:45:45 +0000228 /**
229 * Assemble filename, suitable for writing out the results of a particular test.
230 */
231 SkString make_filename(const char *path,
232 const char *shortName,
233 const char *configName,
234 const char *renderModeDescriptor,
235 const char *suffix) {
236 SkString filename = make_shortname_plus_config(shortName, configName);
237 filename.append(renderModeDescriptor);
238 filename.appendUnichar('.');
239 filename.append(suffix);
scroggo@google.comccd7afb2013-05-28 16:45:07 +0000240 return SkOSPath::SkPathJoin(path, filename.c_str());
epoger@google.com659c8c02013-05-21 15:45:45 +0000241 }
242
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000243 /**
244 * Assemble filename suitable for writing out an SkBitmap.
245 */
246 SkString make_bitmap_filename(const char *path,
247 const char *shortName,
248 const char *configName,
249 const char *renderModeDescriptor,
250 const GmResultDigest &bitmapDigest) {
251 if (fWriteChecksumBasedFilenames) {
252 SkString filename;
253 filename.append(bitmapDigest.getHashType());
254 filename.appendUnichar('_');
255 filename.append(shortName);
256 filename.appendUnichar('_');
257 filename.append(bitmapDigest.getDigestValue());
258 filename.appendUnichar('.');
259 filename.append(kPNG_FileExtension);
260 return SkOSPath::SkPathJoin(path, filename.c_str());
261 } else {
262 return make_filename(path, shortName, configName, renderModeDescriptor,
263 kPNG_FileExtension);
264 }
265 }
266
epoger@google.com5f6a0072013-01-31 16:30:55 +0000267 /* since PNG insists on unpremultiplying our alpha, we take no
268 precision chances and force all pixels to be 100% opaque,
269 otherwise on compare we may not get a perfect match.
270 */
271 static void force_all_opaque(const SkBitmap& bitmap) {
272 SkBitmap::Config config = bitmap.config();
273 switch (config) {
274 case SkBitmap::kARGB_8888_Config:
275 force_all_opaque_8888(bitmap);
276 break;
277 case SkBitmap::kRGB_565_Config:
278 // nothing to do here; 565 bitmaps are inherently opaque
279 break;
280 default:
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000281 gm_fprintf(stderr, "unsupported bitmap config %d\n", config);
282 DEBUGFAIL_SEE_STDERR;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000283 }
284 }
285
286 static void force_all_opaque_8888(const SkBitmap& bitmap) {
287 SkAutoLockPixels lock(bitmap);
288 for (int y = 0; y < bitmap.height(); y++) {
289 for (int x = 0; x < bitmap.width(); x++) {
290 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
291 }
292 }
293 }
294
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000295 static ErrorCombination write_bitmap(const SkString& path, const SkBitmap& bitmap) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000296 // TODO(epoger): Now that we have removed force_all_opaque()
297 // from this method, we should be able to get rid of the
298 // transformation to 8888 format also.
299 SkBitmap copy;
300 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000301 if (!SkImageEncoder::EncodeFile(path.c_str(), copy,
302 SkImageEncoder::kPNG_Type,
303 100)) {
304 gm_fprintf(stderr, "FAILED to write bitmap: %s\n", path.c_str());
305 return ErrorCombination(kWritingReferenceImage_ErrorType);
306 }
307 return kEmpty_ErrorCombination;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000308 }
309
epoger@google.com6f6568b2013-03-22 17:29:46 +0000310 /**
epoger@google.com310478e2013-04-03 18:00:39 +0000311 * Add all render modes encountered thus far to the "modes" array.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000312 */
epoger@google.com310478e2013-04-03 18:00:39 +0000313 void GetRenderModesEncountered(SkTArray<SkString> &modes) {
314 SkTDict<int>::Iter iter(this->fRenderModesEncountered);
315 const char* mode;
316 while ((mode = iter.next(NULL)) != NULL) {
317 SkString modeAsString = SkString(mode);
318 // TODO(epoger): It seems a bit silly that all of these modes were
319 // recorded with a leading "-" which we have to remove here
320 // (except for mode "", which means plain old original mode).
321 // But that's how renderModeDescriptor has been passed into
322 // compare_test_results_to_reference_bitmap() historically,
323 // and changing that now may affect other parts of our code.
324 if (modeAsString.startsWith("-")) {
325 modeAsString.remove(0, 1);
326 modes.push_back(modeAsString);
327 }
328 }
329 }
330
331 /**
332 * Records the results of this test in fTestsRun and fFailedTests.
333 *
334 * We even record successes, and errors that we regard as
335 * "ignorable"; we can filter them out later.
336 */
epoger@google.com659c8c02013-05-21 15:45:45 +0000337 void RecordTestResults(const ErrorCombination& errorCombination,
338 const SkString& shortNamePlusConfig,
epoger@google.com310478e2013-04-03 18:00:39 +0000339 const char renderModeDescriptor []) {
340 // Things to do regardless of errorCombination.
341 fTestsRun++;
342 int renderModeCount = 0;
343 this->fRenderModesEncountered.find(renderModeDescriptor, &renderModeCount);
344 renderModeCount++;
345 this->fRenderModesEncountered.set(renderModeDescriptor, renderModeCount);
346
epoger@google.com6f6568b2013-03-22 17:29:46 +0000347 if (errorCombination.isEmpty()) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000348 return;
epoger@google.comeb066362013-03-08 09:39:36 +0000349 }
350
epoger@google.com310478e2013-04-03 18:00:39 +0000351 // Things to do only if there is some error condition.
epoger@google.com659c8c02013-05-21 15:45:45 +0000352 SkString fullName = shortNamePlusConfig;
epoger@google.comcaac3db2013-04-04 19:23:11 +0000353 fullName.append(renderModeDescriptor);
epoger@google.com310478e2013-04-03 18:00:39 +0000354 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
355 ErrorType type = static_cast<ErrorType>(typeInt);
356 if (errorCombination.includes(type)) {
357 fFailedTests[type].push_back(fullName);
epoger@google.comf60494b2013-04-03 17:02:53 +0000358 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000359 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000360 }
361
epoger@google.com310478e2013-04-03 18:00:39 +0000362 /**
363 * Return the number of significant (non-ignorable) errors we have
364 * encountered so far.
365 */
366 int NumSignificantErrors() {
367 int significantErrors = 0;
368 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
369 ErrorType type = static_cast<ErrorType>(typeInt);
epoger@google.com5079d2c2013-04-12 14:11:21 +0000370 if (!fIgnorableErrorTypes.includes(type)) {
epoger@google.com310478e2013-04-03 18:00:39 +0000371 significantErrors += fFailedTests[type].count();
372 }
373 }
374 return significantErrors;
375 }
376
377 /**
epoger@google.com51dbabe2013-04-10 15:24:53 +0000378 * Display the summary of results with this ErrorType.
379 *
380 * @param type which ErrorType
381 * @param verbose whether to be all verbose about it
epoger@google.com310478e2013-04-03 18:00:39 +0000382 */
epoger@google.com51dbabe2013-04-10 15:24:53 +0000383 void DisplayResultTypeSummary(ErrorType type, bool verbose) {
epoger@google.com5079d2c2013-04-12 14:11:21 +0000384 bool isIgnorableType = fIgnorableErrorTypes.includes(type);
epoger@google.com51dbabe2013-04-10 15:24:53 +0000385
386 SkString line;
387 if (isIgnorableType) {
388 line.append("[ ] ");
389 } else {
390 line.append("[*] ");
391 }
392
393 SkTArray<SkString> *failedTestsOfThisType = &fFailedTests[type];
394 int count = failedTestsOfThisType->count();
395 line.appendf("%d %s", count, getErrorTypeName(type));
396 if (!isIgnorableType || verbose) {
397 line.append(":");
398 for (int i = 0; i < count; ++i) {
399 line.append(" ");
400 line.append((*failedTestsOfThisType)[i]);
401 }
402 }
403 gm_fprintf(stdout, "%s\n", line.c_str());
404 }
405
406 /**
407 * List contents of fFailedTests to stdout.
408 *
409 * @param verbose whether to be all verbose about it
410 */
411 void ListErrors(bool verbose) {
epoger@google.com310478e2013-04-03 18:00:39 +0000412 // First, print a single summary line.
413 SkString summary;
414 summary.appendf("Ran %d tests:", fTestsRun);
415 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
416 ErrorType type = static_cast<ErrorType>(typeInt);
417 summary.appendf(" %s=%d", getErrorTypeName(type), fFailedTests[type].count());
418 }
419 gm_fprintf(stdout, "%s\n", summary.c_str());
420
421 // Now, for each failure type, list the tests that failed that way.
422 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
epoger@google.com51dbabe2013-04-10 15:24:53 +0000423 this->DisplayResultTypeSummary(static_cast<ErrorType>(typeInt), verbose);
epoger@google.com310478e2013-04-03 18:00:39 +0000424 }
425 gm_fprintf(stdout, "(results marked with [*] will cause nonzero return value)\n");
426 }
427
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000428 static ErrorCombination write_document(const SkString& path, SkStreamAsset* asset) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000429 SkFILEWStream stream(path.c_str());
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000430 if (!stream.writeStream(asset, asset->getLength())) {
431 gm_fprintf(stderr, "FAILED to write document: %s\n", path.c_str());
432 return ErrorCombination(kWritingReferenceImage_ErrorType);
433 }
434 return kEmpty_ErrorCombination;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000435 }
436
epoger@google.com37269602013-01-19 04:21:27 +0000437 /**
epoger@google.com5f6a0072013-01-31 16:30:55 +0000438 * Prepare an SkBitmap to render a GM into.
439 *
440 * After you've rendered the GM into the SkBitmap, you must call
441 * complete_bitmap()!
442 *
443 * @todo thudson 22 April 2011 - could refactor this to take in
444 * a factory to generate the context, always call readPixels()
445 * (logically a noop for rasters, if wasted time), and thus collapse the
446 * GPU special case and also let this be used for SkPicture testing.
447 */
448 static void setup_bitmap(const ConfigData& gRec, SkISize& size,
449 SkBitmap* bitmap) {
450 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
451 bitmap->allocPixels();
452 bitmap->eraseColor(SK_ColorTRANSPARENT);
453 }
454
455 /**
456 * Any finalization steps we need to perform on the SkBitmap after
457 * we have rendered the GM into it.
epoger@google.com37269602013-01-19 04:21:27 +0000458 *
459 * It's too bad that we are throwing away alpha channel data
460 * we could otherwise be examining, but this had always been happening
461 * before... it was buried within the compare() method at
462 * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
463 *
464 * Apparently we need this, at least for bitmaps that are either:
465 * (a) destined to be written out as PNG files, or
466 * (b) compared against bitmaps read in from PNG files
467 * for the reasons described just above the force_all_opaque() method.
468 *
469 * Neglecting to do this led to the difficult-to-diagnose
470 * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
471 * spurious pixel_error messages as of r7258')
472 *
473 * TODO(epoger): Come up with a better solution that allows us to
474 * compare full pixel data, including alpha channel, while still being
475 * robust in the face of transformations to/from PNG files.
476 * Options include:
477 *
478 * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
479 * will be written to, or compared against, PNG files.
480 * PRO: Preserve/compare alpha channel info for the non-PNG cases
481 * (comparing different renderModes in-memory)
epoger@google.com908f5832013-04-12 02:23:55 +0000482 * CON: The bitmaps (and hash digests) for these non-PNG cases would be
epoger@google.com37269602013-01-19 04:21:27 +0000483 * different than those for the PNG-compared cases, and in the
484 * case of a failed renderMode comparison, how would we write the
485 * image to disk for examination?
486 *
epoger@google.com908f5832013-04-12 02:23:55 +0000487 * 2. Always compute image hash digests from PNG format (either
epoger@google.com37269602013-01-19 04:21:27 +0000488 * directly from the the bytes of a PNG file, or capturing the
489 * bytes we would have written to disk if we were writing the
490 * bitmap out as a PNG).
491 * PRO: I think this would allow us to never force opaque, and to
492 * the extent that alpha channel data can be preserved in a PNG
493 * file, we could observe it.
epoger@google.com908f5832013-04-12 02:23:55 +0000494 * CON: If we read a bitmap from disk, we need to take its hash digest
epoger@google.com37269602013-01-19 04:21:27 +0000495 * from the source PNG (we can't compute it from the bitmap we
496 * read out of the PNG, because we will have already premultiplied
497 * the alpha).
498 * CON: Seems wasteful to convert a bitmap to PNG format just to take
epoger@google.com908f5832013-04-12 02:23:55 +0000499 * its hash digest. (Although we're wasting lots of effort already
epoger@google.com37269602013-01-19 04:21:27 +0000500 * calling force_all_opaque().)
501 *
502 * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
503 * so we can transform images back and forth without fear of off-by-one
504 * errors.
505 * CON: Math is hard.
506 *
507 * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
508 * channel), rather than demanding absolute equality.
epoger@google.com908f5832013-04-12 02:23:55 +0000509 * CON: Can't do this with hash digests.
epoger@google.com37269602013-01-19 04:21:27 +0000510 */
epoger@google.com5f6a0072013-01-31 16:30:55 +0000511 static void complete_bitmap(SkBitmap* bitmap) {
512 force_all_opaque(*bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000513 }
514
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000515 static void installFilter(SkCanvas* canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000516
reed@google.comaef73612012-11-16 13:41:45 +0000517 static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000518 SkAutoCanvasRestore acr(canvas, true);
519
520 if (!isPDF) {
521 canvas->concat(gm->getInitialTransform());
522 }
523 installFilter(canvas);
reed@google.comaef73612012-11-16 13:41:45 +0000524 gm->setCanvasIsDeferred(isDeferred);
epoger@google.comde961632012-10-26 18:56:36 +0000525 gm->draw(canvas);
526 canvas->setDrawFilter(NULL);
527 }
528
epoger@google.com6f6568b2013-03-22 17:29:46 +0000529 static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000530 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000531 SkBitmap* bitmap,
532 bool deferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000533 SkISize size (gm->getISize());
534 setup_bitmap(gRec, size, bitmap);
535
536 SkAutoTUnref<SkCanvas> canvas;
537
538 if (gRec.fBackend == kRaster_Backend) {
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000539 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*bitmap)));
epoger@google.comde961632012-10-26 18:56:36 +0000540 if (deferred) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000541 canvas.reset(SkDeferredCanvas::Create(device));
epoger@google.comde961632012-10-26 18:56:36 +0000542 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000543 canvas.reset(SkNEW_ARGS(SkCanvas, (device)));
epoger@google.comde961632012-10-26 18:56:36 +0000544 }
reed@google.comaef73612012-11-16 13:41:45 +0000545 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000546 canvas->flush();
547 }
548#if SK_SUPPORT_GPU
549 else { // GPU
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000550 SkAutoTUnref<SkDevice> device(SkGpuDevice::Create(gpuTarget));
epoger@google.comde961632012-10-26 18:56:36 +0000551 if (deferred) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000552 canvas.reset(SkDeferredCanvas::Create(device));
epoger@google.comde961632012-10-26 18:56:36 +0000553 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000554 canvas.reset(SkNEW_ARGS(SkCanvas, (device)));
epoger@google.comde961632012-10-26 18:56:36 +0000555 }
reed@google.comaef73612012-11-16 13:41:45 +0000556 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000557 // the device is as large as the current rendertarget, so
558 // we explicitly only readback the amount we expect (in
559 // size) overwrite our previous allocation
560 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
561 size.fHeight);
562 canvas->readPixels(bitmap, 0, 0);
563 }
564#endif
epoger@google.com5f6a0072013-01-31 16:30:55 +0000565 complete_bitmap(bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000566 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000567 }
568
569 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
junov@chromium.orgc938c482012-12-19 15:24:38 +0000570 SkPicture* pict, SkBitmap* bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000571 SkScalar scale = SK_Scalar1,
572 bool tile = false) {
epoger@google.comde961632012-10-26 18:56:36 +0000573 SkISize size = gm->getISize();
574 setup_bitmap(gRec, size, bitmap);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000575
576 if (tile) {
577 // Generate the result image by rendering to tiles and accumulating
578 // the results in 'bitmap'
579
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000580 // This 16x16 tiling matches the settings applied to 'pict' in
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000581 // 'generate_new_picture'
582 SkISize tileSize = SkISize::Make(16, 16);
583
584 SkBitmap tileBM;
585 setup_bitmap(gRec, tileSize, &tileBM);
586 SkCanvas tileCanvas(tileBM);
587 installFilter(&tileCanvas);
588
589 SkCanvas bmpCanvas(*bitmap);
590 SkPaint bmpPaint;
591 bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
592
593 for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
594 for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
595 int saveCount = tileCanvas.save();
596 SkMatrix mat(tileCanvas.getTotalMatrix());
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000597 mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000598 SkIntToScalar(-yTile*tileSize.height()));
599 tileCanvas.setMatrix(mat);
600 pict->draw(&tileCanvas);
601 tileCanvas.flush();
602 tileCanvas.restoreToCount(saveCount);
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000603 bmpCanvas.drawBitmap(tileBM,
604 SkIntToScalar(xTile * tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000605 SkIntToScalar(yTile * tileSize.height()),
606 &bmpPaint);
607 }
608 }
609 } else {
610 SkCanvas canvas(*bitmap);
611 installFilter(&canvas);
612 canvas.scale(scale, scale);
613 canvas.drawPicture(*pict);
614 complete_bitmap(bitmap);
615 }
epoger@google.comde961632012-10-26 18:56:36 +0000616 }
617
618 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
619#ifdef SK_SUPPORT_PDF
620 SkMatrix initialTransform = gm->getInitialTransform();
621 SkISize pageSize = gm->getISize();
622 SkPDFDevice* dev = NULL;
623 if (initialTransform.isIdentity()) {
624 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
625 } else {
626 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
627 SkIntToScalar(pageSize.height()));
628 initialTransform.mapRect(&content);
629 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
630 SkIntToScalar(pageSize.height()));
631 SkISize contentSize =
632 SkISize::Make(SkScalarRoundToInt(content.width()),
633 SkScalarRoundToInt(content.height()));
634 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
635 }
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000636 dev->setDCTEncoder(encode_to_dct_stream);
epoger@google.comde961632012-10-26 18:56:36 +0000637 SkAutoUnref aur(dev);
638
639 SkCanvas c(dev);
reed@google.comaef73612012-11-16 13:41:45 +0000640 invokeGM(gm, &c, true, false);
epoger@google.comde961632012-10-26 18:56:36 +0000641
642 SkPDFDocument doc;
643 doc.appendPage(dev);
644 doc.emitPDF(&pdf);
645#endif
646 }
647
648 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
649#ifdef SK_SUPPORT_XPS
650 SkISize size = gm->getISize();
651
652 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
653 SkIntToScalar(size.height()));
654 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
655 static const SkScalar upm = 72 * inchesPerMeter;
656 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
657 static const SkScalar ppm = 200 * inchesPerMeter;
658 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
659
660 SkXPSDevice* dev = new SkXPSDevice();
661 SkAutoUnref aur(dev);
662
663 SkCanvas c(dev);
664 dev->beginPortfolio(&xps);
665 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000666 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000667 dev->endSheet();
668 dev->endPortfolio();
669
670#endif
671 }
672
epoger@google.com37269602013-01-19 04:21:27 +0000673 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000674 * Log more detail about the mistmatch between expectedBitmap and
675 * actualBitmap.
676 */
677 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
678 const char *testName) {
679 const int expectedWidth = expectedBitmap.width();
680 const int expectedHeight = expectedBitmap.height();
681 const int width = actualBitmap.width();
682 const int height = actualBitmap.height();
683 if ((expectedWidth != width) || (expectedHeight != height)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000684 gm_fprintf(stderr, "---- %s: dimension mismatch --"
685 " expected [%d %d], actual [%d %d]\n",
686 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000687 return;
688 }
689
690 if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
691 (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000692 gm_fprintf(stderr, "---- %s: not computing max per-channel"
693 " pixel mismatch because non-8888\n", testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000694 return;
695 }
696
697 SkAutoLockPixels alp0(expectedBitmap);
698 SkAutoLockPixels alp1(actualBitmap);
699 int errR = 0;
700 int errG = 0;
701 int errB = 0;
702 int errA = 0;
703 int differingPixels = 0;
704
705 for (int y = 0; y < height; ++y) {
706 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
707 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
708 for (int x = 0; x < width; ++x) {
709 SkPMColor expectedPixel = *expectedPixelPtr++;
710 SkPMColor actualPixel = *actualPixelPtr++;
711 if (expectedPixel != actualPixel) {
712 differingPixels++;
713 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
714 (int)SkGetPackedR32(actualPixel)));
715 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
716 (int)SkGetPackedG32(actualPixel)));
717 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
718 (int)SkGetPackedB32(actualPixel)));
719 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
720 (int)SkGetPackedA32(actualPixel)));
721 }
722 }
723 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000724 gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
725 " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
726 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000727 }
728
729 /**
epoger@google.com908f5832013-04-12 02:23:55 +0000730 * Compares actual hash digest to expectations, returning the set of errors
epoger@google.com6f6568b2013-03-22 17:29:46 +0000731 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000732 *
733 * If fMismatchPath has been set, and there are pixel diffs, then the
734 * actual bitmap will be written out to a file within fMismatchPath.
epoger@google.com5f995452013-06-21 18:16:47 +0000735 * And similarly for fMissingExpectationsPath...
epoger@google.com37269602013-01-19 04:21:27 +0000736 *
737 * @param expectations what expectations to compare actualBitmap against
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000738 * @param actualBitmapAndDigest the SkBitmap we actually generated, and its GmResultDigest
epoger@google.com659c8c02013-05-21 15:45:45 +0000739 * @param shortName name of test, e.g. "selftest1"
740 * @param configName name of config, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000741 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
742 * @param addToJsonSummary whether to add these results (both actual and
epoger@google.comcaac3db2013-04-04 19:23:11 +0000743 * expected) to the JSON summary. Regardless of this setting, if
744 * we find an image mismatch in this test, we will write these
745 * results to the JSON summary. (This is so that we will always
746 * report errors across rendering modes, such as pipe vs tiled.
747 * See https://codereview.chromium.org/13650002/ )
epoger@google.com37269602013-01-19 04:21:27 +0000748 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000749 ErrorCombination compare_to_expectations(Expectations expectations,
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000750 const BitmapAndDigest& actualBitmapAndDigest,
epoger@google.com659c8c02013-05-21 15:45:45 +0000751 const char *shortName, const char *configName,
752 const char *renderModeDescriptor,
epoger@google.comcaac3db2013-04-04 19:23:11 +0000753 bool addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000754 ErrorCombination errors;
epoger@google.com659c8c02013-05-21 15:45:45 +0000755 SkString shortNamePlusConfig = make_shortname_plus_config(shortName, configName);
756 SkString completeNameString(shortNamePlusConfig);
epoger@google.com37269602013-01-19 04:21:27 +0000757 completeNameString.append(renderModeDescriptor);
epoger@google.comce057fe2013-05-14 15:17:46 +0000758 completeNameString.append(".");
759 completeNameString.append(kPNG_FileExtension);
epoger@google.com37269602013-01-19 04:21:27 +0000760 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000761
epoger@google.com37269602013-01-19 04:21:27 +0000762 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000763 errors.add(kMissingExpectations_ErrorType);
epoger@google.com5f995452013-06-21 18:16:47 +0000764
765 // Write out the "actuals" for any tests without expectations, if we have
766 // been directed to do so.
767 if (fMissingExpectationsPath) {
768 SkString path = make_bitmap_filename(fMissingExpectationsPath, shortName,
769 configName, renderModeDescriptor,
770 actualBitmapAndDigest.fDigest);
771 write_bitmap(path, actualBitmapAndDigest.fBitmap);
772 }
773
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000774 } else if (!expectations.match(actualBitmapAndDigest.fDigest)) {
epoger@google.comcaac3db2013-04-04 19:23:11 +0000775 addToJsonSummary = true;
776 // The error mode we record depends on whether this was running
777 // in a non-standard renderMode.
778 if ('\0' == *renderModeDescriptor) {
779 errors.add(kExpectationsMismatch_ErrorType);
780 } else {
781 errors.add(kRenderModeMismatch_ErrorType);
782 }
epoger@google.com84a18022013-02-01 20:39:15 +0000783
784 // Write out the "actuals" for any mismatches, if we have
785 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000786 if (fMismatchPath) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000787 SkString path = make_bitmap_filename(fMismatchPath, shortName, configName,
788 renderModeDescriptor,
789 actualBitmapAndDigest.fDigest);
790 write_bitmap(path, actualBitmapAndDigest.fBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000791 }
epoger@google.com84a18022013-02-01 20:39:15 +0000792
793 // If we have access to a single expected bitmap, log more
794 // detail about the mismatch.
795 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
796 if (NULL != expectedBitmapPtr) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000797 report_bitmap_diffs(*expectedBitmapPtr, actualBitmapAndDigest.fBitmap,
798 completeName);
epoger@google.com84a18022013-02-01 20:39:15 +0000799 }
epoger@google.coma243b222013-01-17 17:54:28 +0000800 }
epoger@google.coma243b222013-01-17 17:54:28 +0000801
epoger@google.com37269602013-01-19 04:21:27 +0000802 if (addToJsonSummary) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000803 add_actual_results_to_json_summary(completeName, actualBitmapAndDigest.fDigest, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000804 expectations.ignoreFailure());
805 add_expected_results_to_json_summary(completeName, expectations);
806 }
epoger@google.coma243b222013-01-17 17:54:28 +0000807
epoger@google.com6f6568b2013-03-22 17:29:46 +0000808 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000809 }
810
epoger@google.com37269602013-01-19 04:21:27 +0000811 /**
812 * Add this result to the appropriate JSON collection of actual results,
epoger@google.com76c913d2013-04-26 15:06:44 +0000813 * depending on errors encountered.
epoger@google.com37269602013-01-19 04:21:27 +0000814 */
815 void add_actual_results_to_json_summary(const char testName[],
epoger@google.comd4993ff2013-05-24 14:33:28 +0000816 const GmResultDigest &actualResultDigest,
epoger@google.com76c913d2013-04-26 15:06:44 +0000817 ErrorCombination errors,
epoger@google.com37269602013-01-19 04:21:27 +0000818 bool ignoreFailure) {
epoger@google.comd4993ff2013-05-24 14:33:28 +0000819 Json::Value jsonActualResults = actualResultDigest.asJsonTypeValuePair();
epoger@google.com76c913d2013-04-26 15:06:44 +0000820 if (errors.isEmpty()) {
821 this->fJsonActualResults_Succeeded[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000822 } else {
823 if (ignoreFailure) {
824 // TODO: Once we have added the ability to compare
825 // actual results against expectations in a JSON file
826 // (where we can set ignore-failure to either true or
epoger@google.com84a18022013-02-01 20:39:15 +0000827 // false), add test cases that exercise ignored
epoger@google.com6f6568b2013-03-22 17:29:46 +0000828 // failures (both for kMissingExpectations_ErrorType
epoger@google.comcaac3db2013-04-04 19:23:11 +0000829 // and kExpectationsMismatch_ErrorType).
epoger@google.com37269602013-01-19 04:21:27 +0000830 this->fJsonActualResults_FailureIgnored[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000831 jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000832 } else {
epoger@google.com76c913d2013-04-26 15:06:44 +0000833 if (errors.includes(kMissingExpectations_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000834 // TODO: What about the case where there IS an
epoger@google.com908f5832013-04-12 02:23:55 +0000835 // expected image hash digest, but that gm test
epoger@google.com37269602013-01-19 04:21:27 +0000836 // doesn't actually run? For now, those cases
837 // will always be ignored, because gm only looks
838 // at expectations that correspond to gm tests
839 // that were actually run.
840 //
841 // Once we have the ability to express
842 // expectations as a JSON file, we should fix this
843 // (and add a test case for which an expectation
844 // is given but the test is never run).
845 this->fJsonActualResults_NoComparison[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000846 jsonActualResults;
epoger@google.comeb066362013-03-08 09:39:36 +0000847 }
epoger@google.com76c913d2013-04-26 15:06:44 +0000848 if (errors.includes(kExpectationsMismatch_ErrorType) ||
849 errors.includes(kRenderModeMismatch_ErrorType)) {
850 this->fJsonActualResults_Failed[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000851 }
852 }
853 }
854 }
855
856 /**
857 * Add this test to the JSON collection of expected results.
858 */
859 void add_expected_results_to_json_summary(const char testName[],
860 Expectations expectations) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000861 this->fJsonExpectedResults[testName] = expectations.asJsonValue();
epoger@google.com37269602013-01-19 04:21:27 +0000862 }
863
864 /**
865 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
866 *
867 * @param gm which test generated the actualBitmap
868 * @param gRec
vandebo@chromium.org8fc37662013-08-21 18:04:09 +0000869 * @param configName The config name to look for in the expectation file.
epoger@google.come33e1372013-07-08 19:13:33 +0000870 * @param actualBitmapAndDigest ptr to bitmap generated by this run, or NULL
871 * if we don't have a usable bitmap representation
epoger@google.com37269602013-01-19 04:21:27 +0000872 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000873 ErrorCombination compare_test_results_to_stored_expectations(
vandebo@chromium.org8fc37662013-08-21 18:04:09 +0000874 GM* gm, const ConfigData& gRec, const char* configName,
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000875 const BitmapAndDigest* actualBitmapAndDigest) {
epoger@google.com37269602013-01-19 04:21:27 +0000876
vandebo@chromium.org8fc37662013-08-21 18:04:09 +0000877 SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(), configName);
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000878
epoger@google.com659c8c02013-05-21 15:45:45 +0000879 ErrorCombination errors;
epoger@google.come33e1372013-07-08 19:13:33 +0000880
881 if (NULL == actualBitmapAndDigest) {
882 // Note that we intentionally skipped validating the results for
883 // this test, because we don't know how to generate an SkBitmap
884 // version of the output.
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000885 errors.add(ErrorCombination(kIntentionallySkipped_ErrorType));
epoger@google.comc824c832013-07-12 15:52:59 +0000886 } else if (!(gRec.fFlags & kWrite_ConfigFlag)) {
887 // We don't record the results for this test or compare them
888 // against any expectations, because the output image isn't
889 // meaningful.
890 // See https://code.google.com/p/skia/issues/detail?id=1410 ('some
891 // GM result images not available for download from Google Storage')
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000892 errors.add(ErrorCombination(kIntentionallySkipped_ErrorType));
epoger@google.come33e1372013-07-08 19:13:33 +0000893 } else {
894 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000895 SkString nameWithExtension(shortNamePlusConfig);
896 nameWithExtension.append(".");
897 nameWithExtension.append(kPNG_FileExtension);
898
epoger@google.come33e1372013-07-08 19:13:33 +0000899 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
900 /*
901 * Get the expected results for this test, as one or more allowed
902 * hash digests. The current implementation of expectationsSource
903 * get this by computing the hash digest of a single PNG file on disk.
904 *
905 * TODO(epoger): This relies on the fact that
906 * force_all_opaque() was called on the bitmap before it
907 * was written to disk as a PNG in the first place. If
908 * not, the hash digest returned here may not match the
909 * hash digest of actualBitmap, which *has* been run through
910 * force_all_opaque().
911 * See comments above complete_bitmap() for more detail.
912 */
913 Expectations expectations = expectationsSource->get(nameWithExtension.c_str());
914 errors.add(compare_to_expectations(expectations, *actualBitmapAndDigest,
vandebo@chromium.org8fc37662013-08-21 18:04:09 +0000915 gm->shortName(), configName, "", true));
epoger@google.come33e1372013-07-08 19:13:33 +0000916 } else {
917 // If we are running without expectations, we still want to
918 // record the actual results.
919 add_actual_results_to_json_summary(nameWithExtension.c_str(),
920 actualBitmapAndDigest->fDigest,
921 ErrorCombination(kMissingExpectations_ErrorType),
922 false);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000923 errors.add(ErrorCombination(kMissingExpectations_ErrorType));
epoger@google.come33e1372013-07-08 19:13:33 +0000924 }
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000925 }
epoger@google.com6f6568b2013-03-22 17:29:46 +0000926 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000927 }
928
epoger@google.com37269602013-01-19 04:21:27 +0000929 /**
930 * Compare actualBitmap to referenceBitmap.
931 *
epoger@google.com659c8c02013-05-21 15:45:45 +0000932 * @param shortName test name, e.g. "selftest1"
933 * @param configName configuration name, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000934 * @param renderModeDescriptor
935 * @param actualBitmap actual bitmap generated by this run
936 * @param referenceBitmap bitmap we expected to be generated
937 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000938 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +0000939 const char *shortName, const char *configName, const char *renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000940 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
941
942 SkASSERT(referenceBitmap);
epoger@google.com84a18022013-02-01 20:39:15 +0000943 Expectations expectations(*referenceBitmap);
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000944 BitmapAndDigest actualBitmapAndDigest(actualBitmap);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +0000945
946 // TODO: Eliminate RecordTestResults from here.
947 // Results recording code for the test_drawing path has been refactored so that
948 // RecordTestResults is only called once, at the topmost level. However, the
949 // other paths have not yet been refactored, and RecordTestResults has been added
950 // here to maintain proper behavior for calls not coming from the test_drawing path.
951 ErrorCombination errors;
952 errors.add(compare_to_expectations(expectations, actualBitmapAndDigest, shortName,
953 configName, renderModeDescriptor, false));
954 SkString shortNamePlusConfig = make_shortname_plus_config(shortName, configName);
955 RecordTestResults(errors, shortNamePlusConfig, renderModeDescriptor);
956
957 return errors;
epoger@google.com37269602013-01-19 04:21:27 +0000958 }
959
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000960 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
961 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +0000962 // Pictures are refcounted so must be on heap
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000963 SkPicture* pict;
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000964 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
965 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +0000966
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000967 if (kTileGrid_BbhType == bbhType) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000968 SkTileGridPicture::TileGridInfo info;
969 info.fMargin.setEmpty();
970 info.fOffset.setZero();
971 info.fTileInterval.set(16, 16);
972 pict = new SkTileGridPicture(width, height, info);
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000973 } else {
974 pict = new SkPicture;
975 }
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000976 if (kNone_BbhType != bbhType) {
977 recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
978 }
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000979 SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +0000980 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +0000981 invokeGM(gm, cv, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000982 pict->endRecording();
983
984 return pict;
985 }
986
commit-bot@chromium.org805df1a2013-08-16 19:18:12 +0000987 static SkData* bitmap_encoder(size_t* pixelRefOffset, const SkBitmap& bm) {
988 SkPixelRef* pr = bm.pixelRef();
989 if (pr != NULL) {
990 SkData* data = pr->refEncodedData();
991 if (data != NULL) {
992 *pixelRefOffset = bm.pixelRefOffset();
993 return data;
994 }
995 }
996 return NULL;
997 }
998
epoger@google.comde961632012-10-26 18:56:36 +0000999 static SkPicture* stream_to_new_picture(const SkPicture& src) {
epoger@google.comde961632012-10-26 18:56:36 +00001000 SkDynamicMemoryWStream storage;
commit-bot@chromium.org805df1a2013-08-16 19:18:12 +00001001 src.serialize(&storage, &bitmap_encoder);
bungeman@google.comc29f3d82013-07-19 22:32:11 +00001002 SkAutoTUnref<SkStreamAsset> pictReadback(storage.detachAsStream());
commit-bot@chromium.org805df1a2013-08-16 19:18:12 +00001003 SkPicture* retval = SkPicture::CreateFromStream(pictReadback,
1004 &SkImageDecoder::DecodeMemory);
epoger@google.comde961632012-10-26 18:56:36 +00001005 return retval;
1006 }
1007
1008 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +00001009 // Depending on flags, possibly compare to an expected image.
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001010 // If writePath is not NULL, also write images (or documents) to the specified path.
1011 ErrorCombination test_drawing(GM* gm, const ConfigData& gRec,
1012 const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
epoger@google.com6f6568b2013-03-22 17:29:46 +00001013 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001014 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +00001015 SkBitmap* bitmap) {
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001016 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001017 SkDynamicMemoryWStream document;
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001018 SkString path;
epoger@google.comde961632012-10-26 18:56:36 +00001019
1020 if (gRec.fBackend == kRaster_Backend ||
1021 gRec.fBackend == kGPU_Backend) {
1022 // Early exit if we can't generate the image.
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001023 errors.add(generate_image(gm, gRec, gpuTarget, bitmap, false));
epoger@google.com6f6568b2013-03-22 17:29:46 +00001024 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +00001025 // TODO: Add a test to exercise what the stdout and
1026 // JSON look like if we get an "early error" while
1027 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +00001028 return errors;
1029 }
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001030 BitmapAndDigest bitmapAndDigest(*bitmap);
1031 errors.add(compare_test_results_to_stored_expectations(
vandebo@chromium.org8fc37662013-08-21 18:04:09 +00001032 gm, gRec, gRec.fName, &bitmapAndDigest));
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001033
1034 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
1035 path = make_bitmap_filename(writePath, gm->shortName(), gRec.fName,
1036 "", bitmapAndDigest.fDigest);
1037 errors.add(write_bitmap(path, bitmapAndDigest.fBitmap));
1038 }
epoger@google.comde961632012-10-26 18:56:36 +00001039 } else if (gRec.fBackend == kPDF_Backend) {
1040 generate_pdf(gm, document);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001041
1042 SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream());
1043 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
1044 path = make_filename(writePath, gm->shortName(), gRec.fName, "", "pdf");
1045 errors.add(write_document(path, documentStream));
1046 }
1047
1048 if (!(gm->getFlags() & GM::kSkipPDFRasterization_Flag)) {
1049 for (int i = 0; i < pdfRasterizers.count(); i++) {
1050 SkBitmap pdfBitmap;
1051 SkASSERT(documentStream->rewind());
1052 bool success = (*pdfRasterizers[i]->fRasterizerFunction)(
1053 documentStream.get(), &pdfBitmap);
1054 if (!success) {
1055 gm_fprintf(stderr, "FAILED to render PDF for %s using renderer %s\n",
1056 gm->shortName(),
1057 pdfRasterizers[i]->fName);
1058 continue;
1059 }
1060
vandebo@chromium.org8fc37662013-08-21 18:04:09 +00001061 SkString configName(gRec.fName);
1062 configName.append("-");
1063 configName.append(pdfRasterizers[i]->fName);
1064
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001065 BitmapAndDigest bitmapAndDigest(pdfBitmap);
1066 errors.add(compare_test_results_to_stored_expectations(
vandebo@chromium.org8fc37662013-08-21 18:04:09 +00001067 gm, gRec, configName.c_str(), &bitmapAndDigest));
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001068
1069 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
1070 path = make_bitmap_filename(writePath, gm->shortName(), configName.c_str(),
1071 "", bitmapAndDigest.fDigest);
1072 errors.add(write_bitmap(path, bitmapAndDigest.fBitmap));
1073 }
1074 }
1075 } else {
1076 errors.add(kIntentionallySkipped_ErrorType);
1077 }
epoger@google.comde961632012-10-26 18:56:36 +00001078 } else if (gRec.fBackend == kXPS_Backend) {
1079 generate_xps(gm, document);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001080 SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream());
epoger@google.come33e1372013-07-08 19:13:33 +00001081
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001082 errors.add(compare_test_results_to_stored_expectations(
vandebo@chromium.org8fc37662013-08-21 18:04:09 +00001083 gm, gRec, gRec.fName, NULL));
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001084
1085 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
1086 path = make_filename(writePath, gm->shortName(), gRec.fName, "", "xps");
1087 errors.add(write_document(path, documentStream));
1088 }
epoger@google.come33e1372013-07-08 19:13:33 +00001089 } else {
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001090 SkASSERT(false);
epoger@google.come33e1372013-07-08 19:13:33 +00001091 }
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001092 return errors;
epoger@google.comde961632012-10-26 18:56:36 +00001093 }
1094
epoger@google.com6f6568b2013-03-22 17:29:46 +00001095 ErrorCombination test_deferred_drawing(GM* gm,
1096 const ConfigData& gRec,
1097 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001098 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +00001099 if (gRec.fBackend == kRaster_Backend ||
1100 gRec.fBackend == kGPU_Backend) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001101 const char renderModeDescriptor[] = "-deferred";
epoger@google.comde961632012-10-26 18:56:36 +00001102 SkBitmap bitmap;
1103 // Early exit if we can't generate the image, but this is
1104 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001105 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001106 // TODO(epoger): This logic is the opposite of what is
1107 // described above... if we succeeded in generating the
1108 // -deferred image, we exit early! We should fix this
1109 // ASAP, because it is hiding -deferred errors... but for
1110 // now, I'm leaving the logic as it is so that the
1111 // refactoring change
1112 // https://codereview.chromium.org/12992003/ is unblocked.
1113 //
1114 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
1115 // ('image-surface gm test is failing in "deferred" mode,
1116 // and gm is not reporting the failure')
1117 if (errors.isEmpty()) {
epoger@google.com310478e2013-04-03 18:00:39 +00001118 // TODO(epoger): Report this as a new ErrorType,
1119 // something like kImageGeneration_ErrorType?
epoger@google.com6f6568b2013-03-22 17:29:46 +00001120 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001121 }
epoger@google.com37269602013-01-19 04:21:27 +00001122 return compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001123 gm->shortName(), gRec.fName, renderModeDescriptor, bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001124 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001125 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001126 }
1127
epoger@google.comcaac3db2013-04-04 19:23:11 +00001128 ErrorCombination test_pipe_playback(GM* gm, const ConfigData& gRec,
1129 const SkBitmap& referenceBitmap, bool simulateFailure) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001130 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(),
1131 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001132 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001133 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001134 SkString renderModeDescriptor("-pipe");
1135 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1136
commit-bot@chromium.org805df1a2013-08-16 19:18:12 +00001137 if (gm->getFlags() & GM::kSkipPipe_Flag
1138 || (gPipeWritingFlagCombos[i].flags == SkGPipeWriter::kCrossProcess_Flag
1139 && gm->getFlags() & GM::kSkipPipeCrossProcess_Flag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001140 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001141 renderModeDescriptor.c_str());
1142 errors.add(kIntentionallySkipped_ErrorType);
1143 } else {
1144 SkBitmap bitmap;
1145 SkISize size = gm->getISize();
1146 setup_bitmap(gRec, size, &bitmap);
1147 SkCanvas canvas(bitmap);
1148 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001149 // Pass a decoding function so the factory GM (which has an SkBitmap
1150 // with encoded data) will not fail playback.
1151 PipeController pipeController(&canvas, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001152 SkGPipeWriter writer;
1153 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001154 gPipeWritingFlagCombos[i].flags,
1155 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001156 if (!simulateFailure) {
1157 invokeGM(gm, pipeCanvas, false, false);
1158 }
1159 complete_bitmap(&bitmap);
1160 writer.endRecording();
1161 errors.add(compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001162 gm->shortName(), gRec.fName, renderModeDescriptor.c_str(), bitmap,
1163 &referenceBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001164 if (!errors.isEmpty()) {
1165 break;
1166 }
epoger@google.comde961632012-10-26 18:56:36 +00001167 }
1168 }
1169 return errors;
1170 }
1171
epoger@google.com6f6568b2013-03-22 17:29:46 +00001172 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
1173 const SkBitmap& referenceBitmap) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001174 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(),
1175 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001176 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001177 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001178 SkString renderModeDescriptor("-tiled pipe");
1179 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1180
1181 if ((gm->getFlags() & GM::kSkipPipe_Flag) ||
1182 (gm->getFlags() & GM::kSkipTiled_Flag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001183 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001184 renderModeDescriptor.c_str());
1185 errors.add(kIntentionallySkipped_ErrorType);
1186 } else {
1187 SkBitmap bitmap;
1188 SkISize size = gm->getISize();
1189 setup_bitmap(gRec, size, &bitmap);
1190 SkCanvas canvas(bitmap);
1191 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001192 TiledPipeController pipeController(bitmap, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001193 SkGPipeWriter writer;
1194 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001195 gPipeWritingFlagCombos[i].flags,
1196 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001197 invokeGM(gm, pipeCanvas, false, false);
1198 complete_bitmap(&bitmap);
1199 writer.endRecording();
epoger@google.com659c8c02013-05-21 15:45:45 +00001200 errors.add(compare_test_results_to_reference_bitmap(gm->shortName(), gRec.fName,
epoger@google.comc8263e72013-04-10 12:17:34 +00001201 renderModeDescriptor.c_str(),
1202 bitmap, &referenceBitmap));
1203 if (!errors.isEmpty()) {
1204 break;
1205 }
epoger@google.comde961632012-10-26 18:56:36 +00001206 }
1207 }
1208 return errors;
1209 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00001210
1211 //
1212 // member variables.
1213 // They are public for now, to allow easier setting by tool_main().
1214 //
1215
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001216 bool fUseFileHierarchy, fWriteChecksumBasedFilenames;
epoger@google.com5079d2c2013-04-12 14:11:21 +00001217 ErrorCombination fIgnorableErrorTypes;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001218
junov@chromium.org95146eb2013-01-11 21:04:40 +00001219 const char* fMismatchPath;
epoger@google.com5f995452013-06-21 18:16:47 +00001220 const char* fMissingExpectationsPath;
junov@chromium.org95146eb2013-01-11 21:04:40 +00001221
epoger@google.com310478e2013-04-03 18:00:39 +00001222 // collection of tests that have failed with each ErrorType
1223 SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
1224 int fTestsRun;
1225 SkTDict<int> fRenderModesEncountered;
epoger@google.com57f7abc2012-11-13 03:41:55 +00001226
epoger@google.com908f5832013-04-12 02:23:55 +00001227 // Where to read expectations (expected image hash digests, etc.) from.
epoger@google.com37269602013-01-19 04:21:27 +00001228 // If unset, we don't do comparisons.
1229 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
1230
1231 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +00001232 Json::Value fJsonExpectedResults;
1233 Json::Value fJsonActualResults_Failed;
1234 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001235 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001236 Json::Value fJsonActualResults_Succeeded;
1237
epoger@google.comde961632012-10-26 18:56:36 +00001238}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +00001239
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001240#if SK_SUPPORT_GPU
1241static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1242#else
1243static const GLContextType kDontCare_GLContextType = 0;
1244#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +00001245
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001246static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001247 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001248#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 +00001249 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001250#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001251 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
1252#if SK_SUPPORT_GPU
1253 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
robertphillips@google.com7bb28a12013-06-06 21:29:39 +00001254 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", false},
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001255 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
epoger@google.comc824c832013-07-12 15:52:59 +00001256 /* The gpudebug context does not generate meaningful images, so don't record
1257 * the images it generates! We only run it to look for asserts. */
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001258 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", kDebugOnly},
robertphillips@google.comd6543e52013-07-18 17:39:14 +00001259 /* The gpunull context does the least amount of work possible and doesn't
1260 generate meaninful images, so don't record them!. It can be run to
1261 isolate the CPU-side processing expense from the GPU-side.
1262 */
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001263 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNull_GLContextType, 0, kNone_ConfigFlag, "gpunull", kDebugOnly},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001264#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001265 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
1266 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001267#endif // SK_ANGLE
1268#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001269 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001270#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001271#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +00001272#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001273 /* 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 +00001274 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +00001275#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001276#ifdef SK_SUPPORT_PDF
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001277 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001278#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001279};
1280
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001281static const PDFRasterizerData kPDFRasterizers[] = {
1282#ifdef SK_BUILD_FOR_MAC
1283 { &SkPDFDocumentToBitmap, "mac", true },
1284#endif
1285#ifdef SK_BUILD_POPPLER
1286 { &SkPopplerRasterizePDF, "poppler", true },
1287#endif
1288};
1289
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001290static const char kDefaultsConfigStr[] = "defaults";
1291static const char kExcludeConfigChar = '~';
1292
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001293static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001294 SkString result;
1295 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001296 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001297 SkASSERT(gRec[i].fName != kDefaultsConfigStr);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001298 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001299 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001300 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001301 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001302 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001303 result.append("]\n");
1304 result.appendf("The default value is: \"");
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001305 SkString firstDefault;
1306 SkString allButFirstDefaults;
1307 SkString nonDefault;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001308 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1309 if (gRec[i].fRunByDefault) {
1310 if (i > 0) {
1311 result.append(" ");
1312 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001313 result.append(gRec[i].fName);
1314 if (firstDefault.isEmpty()) {
1315 firstDefault = gRec[i].fName;
1316 } else {
1317 if (!allButFirstDefaults.isEmpty()) {
1318 allButFirstDefaults.append(" ");
1319 }
1320 allButFirstDefaults.append(gRec[i].fName);
1321 }
1322 } else {
1323 nonDefault = gRec[i].fName;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001324 }
1325 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001326 result.append("\"\n");
1327 result.appendf("\"%s\" evaluates to the default set of configs.\n", kDefaultsConfigStr);
1328 result.appendf("Prepending \"%c\" on a config name excludes it from the set of configs to run.\n"
1329 "Exclusions always override inclusions regardless of order.\n",
1330 kExcludeConfigChar);
1331 result.appendf("E.g. \"--config %s %c%s %s\" will run these configs:\n\t%s %s",
1332 kDefaultsConfigStr,
1333 kExcludeConfigChar,
1334 firstDefault.c_str(),
1335 nonDefault.c_str(),
1336 allButFirstDefaults.c_str(),
1337 nonDefault.c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001338 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001339}
scroggo@google.com7d519302013-03-19 17:28:10 +00001340
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001341static SkString pdfRasterizerUsage() {
1342 SkString result;
1343 result.appendf("Space delimited list of which PDF rasterizers to run. Possible options: [");
1344 // For this (and further) loops through kPDFRasterizers, there is a typecast to int to avoid
1345 // the compiler giving an "comparison of unsigned expression < 0 is always false" warning
1346 // and turning it into a build-breaking error.
1347 for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
1348 if (i > 0) {
1349 result.append(" ");
1350 }
1351 result.append(kPDFRasterizers[i].fName);
1352 }
1353 result.append("]\n");
1354 result.append("The default value is: \"");
1355 for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
1356 if (kPDFRasterizers[i].fRunByDefault) {
1357 if (i > 0) {
1358 result.append(" ");
1359 }
1360 result.append(kPDFRasterizers[i].fName);
1361 }
1362 }
1363 result.append("\"");
1364 return result;
1365}
1366
epoger@google.com6f6568b2013-03-22 17:29:46 +00001367// Macro magic to convert a numeric preprocessor token into a string.
1368// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1369// This should probably be moved into one of our common headers...
1370#define TOSTRING_INTERNAL(x) #x
1371#define TOSTRING(x) TOSTRING_INTERNAL(x)
1372
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001373// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001374DEFINE_string(config, "", configUsage().c_str());
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001375DEFINE_string(pdfRasterizers, "", pdfRasterizerUsage().c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001376DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001377DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1378DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1379#if SK_SUPPORT_GPU
1380DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001381 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1382 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001383#endif
1384DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1385 "when reading/writing files.");
epoger@google.com5079d2c2013-04-12 14:11:21 +00001386DEFINE_string(ignoreErrorTypes, kDefaultIgnorableErrorTypes.asString(" ").c_str(),
1387 "Space-separated list of ErrorTypes that should be ignored. If any *other* error "
1388 "types are encountered, the tool will exit with a nonzero return value.");
caryclark@google.com512c9b62013-05-10 15:16:13 +00001389DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
1390 "Multiple matches may be separated by spaces.\n"
1391 "~ causes a matching test to always be skipped\n"
1392 "^ requires the start of the test to match\n"
1393 "$ requires the end of the test to match\n"
1394 "^ and $ requires an exact match\n"
1395 "If a test does not match any list entry,\n"
1396 "it is skipped unless some list entry starts with ~");
epoger@google.com5f995452013-06-21 18:16:47 +00001397DEFINE_string(missingExpectationsPath, "", "Write images for tests without expectations "
1398 "into this directory.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001399DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1400 "pixel mismatches into this directory.");
1401DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1402 "testIndex %% divisor == remainder.");
1403DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1404DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1405DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1406 "any differences between those and the newly generated ones.");
1407DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1408DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1409DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1410DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
epoger@google.comcaac3db2013-04-04 19:23:11 +00001411DEFINE_bool(simulatePipePlaybackFailure, false, "Simulate a rendering failure in pipe mode only.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001412DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1413DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1414DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1415 "factors to be used for tileGrid playback testing. Default value: 1.0");
epoger@google.comb0f8b432013-04-10 18:46:25 +00001416DEFINE_bool2(verbose, v, false, "Give more detail (e.g. list all GMs run, more info about "
1417 "each test).");
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001418DEFINE_bool(writeChecksumBasedFilenames, false, "When writing out actual images, use checksum-"
1419 "based filenames, as rebaseline.py will use when downloading them from Google Storage");
1420DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001421DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
scroggo@google.com604e0c22013-04-09 21:25:46 +00001422DEFINE_string2(writePicturePath, p, "", "Write .skp files into this directory.");
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001423DEFINE_int32(pdfJpegQuality, -1, "Encodes images in JPEG at quality level N, "
1424 "which can be in range 0-100). N = -1 will disable JPEG compression. "
1425 "Default is N = 100, maximum quality.");
1426
1427static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect) {
1428 // Filter output of warnings that JPEG is not available for the image.
1429 if (bitmap.width() >= 65500 || bitmap.height() >= 65500) return false;
1430 if (FLAGS_pdfJpegQuality == -1) return false;
1431
1432 SkIRect bitmapBounds;
1433 SkBitmap subset;
1434 const SkBitmap* bitmapToUse = &bitmap;
1435 bitmap.getBounds(&bitmapBounds);
1436 if (rect != bitmapBounds) {
1437 SkAssertResult(bitmap.extractSubset(&subset, rect));
1438 bitmapToUse = &subset;
1439 }
1440
1441#if defined(SK_BUILD_FOR_MAC)
1442 // Workaround bug #1043 where bitmaps with referenced pixels cause
1443 // CGImageDestinationFinalize to crash
1444 SkBitmap copy;
1445 bitmapToUse->deepCopyTo(&copy, bitmapToUse->config());
1446 bitmapToUse = &copy;
1447#endif
1448
1449 return SkImageEncoder::EncodeStream(stream,
1450 *bitmapToUse,
1451 SkImageEncoder::kJPEG_Type,
1452 FLAGS_pdfJpegQuality);
1453}
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001454
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001455static int findConfig(const char config[]) {
1456 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1457 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001458 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001459 }
1460 }
1461 return -1;
1462}
1463
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001464static const PDFRasterizerData* findPDFRasterizer(const char rasterizer[]) {
1465 for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); i++) {
1466 if (!strcmp(rasterizer, kPDFRasterizers[i].fName)) {
1467 return &kPDFRasterizers[i];
1468 }
1469 }
1470 return NULL;
1471}
1472
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001473namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001474#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +00001475SkAutoTUnref<GrContext> gGrContext;
1476/**
bsalomon@google.comc7a24d22012-11-01 18:03:48 +00001477 * Sets the global GrContext, accessible by individual GMs
bsalomon@google.com7361f542012-04-19 19:15:35 +00001478 */
caryclark@google.com13130862012-06-06 12:10:45 +00001479static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +00001480 SkSafeRef(grContext);
1481 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001482}
bsalomon@google.com7361f542012-04-19 19:15:35 +00001483
1484/**
1485 * Gets the global GrContext, can be called by GM tests.
1486 */
caryclark@google.com13130862012-06-06 12:10:45 +00001487GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +00001488GrContext* GetGr() {
1489 return gGrContext.get();
1490}
1491
1492/**
1493 * Sets the global GrContext and then resets it to its previous value at
1494 * destruction.
1495 */
1496class AutoResetGr : SkNoncopyable {
1497public:
1498 AutoResetGr() : fOld(NULL) {}
1499 void set(GrContext* context) {
1500 SkASSERT(NULL == fOld);
1501 fOld = GetGr();
1502 SkSafeRef(fOld);
1503 SetGr(context);
1504 }
1505 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1506private:
1507 GrContext* fOld;
1508};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001509#else
epoger@google.com80724df2013-03-21 13:49:54 +00001510GrContext* GetGr();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001511GrContext* GetGr() { return NULL; }
1512#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001513}
1514
reed@google.comfb2cd422013-01-04 14:43:03 +00001515template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1516 int index = array->find(value);
1517 if (index < 0) {
1518 *array->append() = value;
1519 }
1520}
1521
epoger@google.com80724df2013-03-21 13:49:54 +00001522/**
1523 * Run this test in a number of different configs (8888, 565, PDF,
1524 * etc.), confirming that the resulting bitmaps match expectations
1525 * (which may be different for each config).
1526 *
1527 * Returns all errors encountered while doing so.
1528 */
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001529ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm,
1530 const SkTDArray<size_t> &configs,
1531 const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
epoger@google.com6f6568b2013-03-22 17:29:46 +00001532 GrContextFactory *grFactory);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001533ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm,
1534 const SkTDArray<size_t> &configs,
1535 const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
epoger@google.com6f6568b2013-03-22 17:29:46 +00001536 GrContextFactory *grFactory) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001537 const char renderModeDescriptor[] = "";
epoger@google.com6f6568b2013-03-22 17:29:46 +00001538 ErrorCombination errorsForAllConfigs;
epoger@google.com80724df2013-03-21 13:49:54 +00001539 uint32_t gmFlags = gm->getFlags();
1540
epoger@google.com80724df2013-03-21 13:49:54 +00001541 for (int i = 0; i < configs.count(); i++) {
1542 ConfigData config = gRec[configs[i]];
epoger@google.com659c8c02013-05-21 15:45:45 +00001543 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->shortName(),
1544 config.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001545
1546 // Skip any tests that we don't even need to try.
epoger@google.comc8263e72013-04-10 12:17:34 +00001547 // If any of these were skipped on a per-GM basis, record them as
1548 // kIntentionallySkipped.
1549 if (kPDF_Backend == config.fBackend) {
1550 if (!FLAGS_pdf) {
epoger@google.com80724df2013-03-21 13:49:54 +00001551 continue;
1552 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001553 if (gmFlags & GM::kSkipPDF_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001554 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001555 renderModeDescriptor);
1556 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1557 continue;
1558 }
1559 }
epoger@google.com80724df2013-03-21 13:49:54 +00001560 if ((gmFlags & GM::kSkip565_Flag) &&
1561 (kRaster_Backend == config.fBackend) &&
1562 (SkBitmap::kRGB_565_Config == config.fConfig)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001563 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001564 renderModeDescriptor);
1565 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001566 continue;
1567 }
commit-bot@chromium.org78a10782013-08-21 19:27:48 +00001568 if (((gmFlags & GM::kSkipGPU_Flag) && kGPU_Backend == config.fBackend) ||
1569 ((gmFlags & GM::kGPUOnly_Flag) && kGPU_Backend != config.fBackend)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001570 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001571 renderModeDescriptor);
1572 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001573 continue;
1574 }
1575
1576 // Now we know that we want to run this test and record its
1577 // success or failure.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001578 ErrorCombination errorsForThisConfig;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001579 GrSurface* gpuTarget = NULL;
epoger@google.com80724df2013-03-21 13:49:54 +00001580#if SK_SUPPORT_GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001581 SkAutoTUnref<GrSurface> auGpuTarget;
epoger@google.com80724df2013-03-21 13:49:54 +00001582 AutoResetGr autogr;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001583 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
epoger@google.com80724df2013-03-21 13:49:54 +00001584 GrContext* gr = grFactory->get(config.fGLContextType);
1585 bool grSuccess = false;
1586 if (gr) {
1587 // create a render target to back the device
1588 GrTextureDesc desc;
1589 desc.fConfig = kSkia8888_GrPixelConfig;
1590 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1591 desc.fWidth = gm->getISize().width();
1592 desc.fHeight = gm->getISize().height();
1593 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001594 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1595 if (NULL != auGpuTarget) {
1596 gpuTarget = auGpuTarget;
1597 grSuccess = true;
epoger@google.com80724df2013-03-21 13:49:54 +00001598 autogr.set(gr);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001599 // Set the user specified cache limits if non-default.
1600 size_t bytes;
1601 int count;
1602 gr->getTextureCacheLimits(&count, &bytes);
1603 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1604 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1605 }
1606 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1607 count = gGpuCacheSizeCount;
1608 }
1609 gr->setTextureCacheLimits(count, bytes);
epoger@google.com80724df2013-03-21 13:49:54 +00001610 }
epoger@google.com80724df2013-03-21 13:49:54 +00001611 }
1612 if (!grSuccess) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001613 errorsForThisConfig.add(kNoGpuContext_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001614 }
1615 }
1616#endif
1617
1618 SkBitmap comparisonBitmap;
1619
1620 const char* writePath;
1621 if (FLAGS_writePath.count() == 1) {
1622 writePath = FLAGS_writePath[0];
1623 } else {
1624 writePath = NULL;
1625 }
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001626
epoger@google.com6f6568b2013-03-22 17:29:46 +00001627 if (errorsForThisConfig.isEmpty()) {
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001628 errorsForThisConfig.add(gmmain.test_drawing(gm, config, pdfRasterizers,
1629 writePath, gpuTarget,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001630 &comparisonBitmap));
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001631 gmmain.RecordTestResults(errorsForThisConfig, shortNamePlusConfig, "");
epoger@google.com80724df2013-03-21 13:49:54 +00001632 }
1633
epoger@google.com6f6568b2013-03-22 17:29:46 +00001634 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1635 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1636 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001637 gpuTarget));
epoger@google.com80724df2013-03-21 13:49:54 +00001638 }
1639
epoger@google.com6f6568b2013-03-22 17:29:46 +00001640 errorsForAllConfigs.add(errorsForThisConfig);
epoger@google.com80724df2013-03-21 13:49:54 +00001641 }
1642 return errorsForAllConfigs;
1643}
1644
1645/**
1646 * Run this test in a number of different drawing modes (pipe,
1647 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1648 * *exactly* match comparisonBitmap.
1649 *
1650 * Returns all errors encountered while doing so.
1651 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001652ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1653 const SkBitmap &comparisonBitmap,
1654 const SkTDArray<SkScalar> &tileGridReplayScales);
1655ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1656 const SkBitmap &comparisonBitmap,
1657 const SkTDArray<SkScalar> &tileGridReplayScales) {
1658 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001659 uint32_t gmFlags = gm->getFlags();
epoger@google.com659c8c02013-05-21 15:45:45 +00001660 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->shortName(),
1661 compareConfig.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001662
epoger@google.comc8263e72013-04-10 12:17:34 +00001663 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1664 SkAutoUnref aur(pict);
1665 if (FLAGS_replay) {
1666 const char renderModeDescriptor[] = "-replay";
1667 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001668 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1669 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001670 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1671 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001672 SkBitmap bitmap;
1673 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001674 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001675 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1676 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001677 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001678 }
epoger@google.com80724df2013-03-21 13:49:54 +00001679
epoger@google.comc8263e72013-04-10 12:17:34 +00001680 if (FLAGS_serialize) {
1681 const char renderModeDescriptor[] = "-serialize";
1682 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001683 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1684 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001685 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1686 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001687 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1688 SkAutoUnref aurr(repict);
epoger@google.com80724df2013-03-21 13:49:54 +00001689 SkBitmap bitmap;
1690 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001691 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001692 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1693 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001694 }
epoger@google.com80724df2013-03-21 13:49:54 +00001695 }
1696
epoger@google.comc8263e72013-04-10 12:17:34 +00001697 if ((1 == FLAGS_writePicturePath.count()) &&
1698 !(gmFlags & GM::kSkipPicture_Flag)) {
1699 const char* pictureSuffix = "skp";
epoger@google.com659c8c02013-05-21 15:45:45 +00001700 // TODO(epoger): Make sure this still works even though the
1701 // filename now contains the config name (it used to contain
1702 // just the shortName). I think this is actually an
1703 // *improvement*, because now runs with different configs will
1704 // write out their SkPictures to separate files rather than
1705 // overwriting each other. But we should make sure it doesn't
1706 // break anybody.
1707 SkString path = gmmain.make_filename(FLAGS_writePicturePath[0], gm->shortName(),
1708 compareConfig.fName, "", pictureSuffix);
epoger@google.comc8263e72013-04-10 12:17:34 +00001709 SkFILEWStream stream(path.c_str());
1710 pict->serialize(&stream);
epoger@google.com80724df2013-03-21 13:49:54 +00001711 }
1712
epoger@google.comc8263e72013-04-10 12:17:34 +00001713 if (FLAGS_rtree) {
1714 const char renderModeDescriptor[] = "-rtree";
1715 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001716 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1717 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001718 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1719 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001720 SkPicture* pict = gmmain.generate_new_picture(
epoger@google.comc8263e72013-04-10 12:17:34 +00001721 gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
epoger@google.com80724df2013-03-21 13:49:54 +00001722 SkAutoUnref aur(pict);
1723 SkBitmap bitmap;
epoger@google.comc8263e72013-04-10 12:17:34 +00001724 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001725 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001726 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1727 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001728 }
1729 }
1730
1731 if (FLAGS_tileGrid) {
1732 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1733 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1734 SkString renderModeDescriptor("-tilegrid");
1735 if (SK_Scalar1 != replayScale) {
1736 renderModeDescriptor += "-scale-";
1737 renderModeDescriptor.appendScalar(replayScale);
1738 }
1739
1740 if ((gmFlags & GM::kSkipPicture_Flag) ||
1741 ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001742 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001743 renderModeDescriptor.c_str());
1744 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1745 } else {
1746 // We record with the reciprocal scale to obtain a replay
1747 // result that can be validated against comparisonBitmap.
1748 SkScalar recordScale = SkScalarInvert(replayScale);
1749 SkPicture* pict = gmmain.generate_new_picture(
1750 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag,
1751 recordScale);
1752 SkAutoUnref aur(pict);
1753 SkBitmap bitmap;
1754 // We cannot yet pass 'true' to generate_image_from_picture to
1755 // perform actual tiled rendering (see Issue 1198 -
1756 // https://code.google.com/p/skia/issues/detail?id=1198)
1757 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
1758 replayScale /*, true */);
1759 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001760 gm->shortName(), compareConfig.fName, renderModeDescriptor.c_str(), bitmap,
1761 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001762 }
epoger@google.com80724df2013-03-21 13:49:54 +00001763 }
1764 }
1765
1766 // run the pipe centric GM steps
epoger@google.comc8263e72013-04-10 12:17:34 +00001767 if (FLAGS_pipe) {
1768 errorsForAllModes.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap,
1769 FLAGS_simulatePipePlaybackFailure));
1770 if (FLAGS_tiledPipe) {
1771 errorsForAllModes.add(gmmain.test_tiled_pipe_playback(gm, compareConfig,
1772 comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001773 }
epoger@google.com80724df2013-03-21 13:49:54 +00001774 }
1775 return errorsForAllModes;
1776}
1777
epoger@google.com310478e2013-04-03 18:00:39 +00001778/**
1779 * Return a list of all entries in an array of strings as a single string
1780 * of this form:
1781 * "item1", "item2", "item3"
1782 */
1783SkString list_all(const SkTArray<SkString> &stringArray);
1784SkString list_all(const SkTArray<SkString> &stringArray) {
1785 SkString total;
1786 for (int i = 0; i < stringArray.count(); i++) {
1787 if (i > 0) {
1788 total.append(", ");
1789 }
1790 total.append("\"");
1791 total.append(stringArray[i]);
1792 total.append("\"");
1793 }
1794 return total;
1795}
1796
1797/**
1798 * Return a list of configuration names, as a single string of this form:
1799 * "item1", "item2", "item3"
1800 *
1801 * @param configs configurations, as a list of indices into gRec
1802 */
1803SkString list_all_config_names(const SkTDArray<size_t> &configs);
1804SkString list_all_config_names(const SkTDArray<size_t> &configs) {
1805 SkString total;
1806 for (int i = 0; i < configs.count(); i++) {
1807 if (i > 0) {
1808 total.append(", ");
1809 }
1810 total.append("\"");
1811 total.append(gRec[configs[i]].fName);
1812 total.append("\"");
1813 }
1814 return total;
1815}
1816
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001817static bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1818 const SkTDArray<size_t> &configs,
1819 const SkTDArray<const PDFRasterizerData*>& pdfRasterizers) {
epoger@google.comfdea3252013-05-02 18:24:03 +00001820 if (!sk_mkdir(root)) {
1821 return false;
1822 }
1823 if (useFileHierarchy) {
1824 for (int i = 0; i < configs.count(); i++) {
1825 ConfigData config = gRec[configs[i]];
1826 SkString subdir;
1827 subdir.appendf("%s%c%s", root, SkPATH_SEPARATOR, config.fName);
1828 if (!sk_mkdir(subdir.c_str())) {
1829 return false;
1830 }
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001831
1832 if (config.fBackend == kPDF_Backend) {
1833 for (int j = 0; j < pdfRasterizers.count(); j++) {
1834 SkString pdfSubdir = subdir;
vandebo@chromium.org8fc37662013-08-21 18:04:09 +00001835 pdfSubdir.appendf("-%s", pdfRasterizers[j]->fName);
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001836 if (!sk_mkdir(pdfSubdir.c_str())) {
1837 return false;
1838 }
1839 }
1840 }
epoger@google.comfdea3252013-05-02 18:24:03 +00001841 }
1842 }
1843 return true;
1844}
1845
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00001846static bool parse_flags_configs(SkTDArray<size_t>* outConfigs,
1847 GrContextFactory* grFactory) {
1848 SkTDArray<size_t> excludeConfigs;
1849
1850 for (int i = 0; i < FLAGS_config.count(); i++) {
1851 const char* config = FLAGS_config[i];
1852 bool exclude = false;
1853 if (*config == kExcludeConfigChar) {
1854 exclude = true;
1855 config += 1;
1856 }
1857 int index = findConfig(config);
1858 if (index >= 0) {
1859 if (exclude) {
1860 *excludeConfigs.append() = index;
1861 } else {
1862 appendUnique<size_t>(outConfigs, index);
1863 }
1864 } else if (0 == strcmp(kDefaultsConfigStr, config)) {
1865 if (exclude) {
1866 gm_fprintf(stderr, "%c%s is not allowed.\n",
1867 kExcludeConfigChar, kDefaultsConfigStr);
1868 return false;
1869 }
1870 for (size_t c = 0; c < SK_ARRAY_COUNT(gRec); ++c) {
1871 if (gRec[c].fRunByDefault) {
1872 appendUnique<size_t>(outConfigs, c);
1873 }
1874 }
1875 } else {
1876 gm_fprintf(stderr, "unrecognized config %s\n", config);
1877 return false;
1878 }
1879 }
1880
1881 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1882 int index = findConfig(FLAGS_excludeConfig[i]);
1883 if (index >= 0) {
1884 *excludeConfigs.append() = index;
1885 } else {
1886 gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1887 return false;
1888 }
1889 }
1890
1891 if (outConfigs->count() == 0) {
1892 // if no config is specified by user, add the defaults
1893 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1894 if (gRec[i].fRunByDefault) {
1895 *outConfigs->append() = i;
1896 }
1897 }
1898 }
1899 // now remove any explicitly excluded configs
1900 for (int i = 0; i < excludeConfigs.count(); ++i) {
1901 int index = outConfigs->find(excludeConfigs[i]);
1902 if (index >= 0) {
1903 outConfigs->remove(index);
1904 // now assert that there was only one copy in configs[]
1905 SkASSERT(outConfigs->find(excludeConfigs[i]) < 0);
1906 }
1907 }
1908
1909#if SK_SUPPORT_GPU
1910 SkASSERT(grFactory != NULL);
1911 for (int i = 0; i < outConfigs->count(); ++i) {
1912 size_t index = (*outConfigs)[i];
1913 if (kGPU_Backend == gRec[index].fBackend) {
1914 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1915 if (NULL == ctx) {
1916 gm_fprintf(stderr, "GrContext could not be created for config %s."
1917 " Config will be skipped.\n", gRec[index].fName);
1918 outConfigs->remove(i);
1919 --i;
1920 continue;
1921 }
1922 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
1923 gm_fprintf(stderr, "Sample count (%d) of config %s is not supported."
1924 " Config will be skipped.\n",
1925 gRec[index].fSampleCnt, gRec[index].fName);
1926 outConfigs->remove(i);
1927 --i;
1928 }
1929 }
1930 }
1931#endif
1932
1933 if (outConfigs->isEmpty()) {
1934 gm_fprintf(stderr, "No configs to run.");
1935 return false;
1936 }
1937
1938 // now show the user the set of configs that will be run.
1939 SkString configStr("These configs will be run:");
1940 // show the user the config that will run.
1941 for (int i = 0; i < outConfigs->count(); ++i) {
1942 configStr.appendf(" %s", gRec[(*outConfigs)[i]].fName);
1943 }
1944 gm_fprintf(stdout, "%s\n", configStr.c_str());
1945
1946 return true;
1947}
1948
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00001949static bool parse_flags_pdf_rasterizers(const SkTDArray<size_t>& configs,
1950 SkTDArray<const PDFRasterizerData*>* outRasterizers) {
1951 // No need to run this check (and display the PDF rasterizers message)
1952 // if no PDF backends are in the configs.
1953 bool configHasPDF = false;
1954 for (int i = 0; i < configs.count(); i++) {
1955 if (gRec[configs[i]].fBackend == kPDF_Backend) {
1956 configHasPDF = true;
1957 break;
1958 }
1959 }
1960 if (!configHasPDF) {
1961 return true;
1962 }
1963
1964 for (int i = 0; i < FLAGS_pdfRasterizers.count(); i++) {
1965 const char* rasterizer = FLAGS_pdfRasterizers[i];
1966 const PDFRasterizerData* rasterizerPtr = findPDFRasterizer(rasterizer);
1967
1968 if (rasterizerPtr == NULL) {
1969 gm_fprintf(stderr, "unrecognized rasterizer %s\n", rasterizer);
1970 return false;
1971 }
1972 appendUnique<const PDFRasterizerData*>(outRasterizers,
1973 rasterizerPtr);
1974 }
1975
1976 if (outRasterizers->count() == 0) {
1977 // if no config is specified by user, add the defaults
1978 for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
1979 if (kPDFRasterizers[i].fRunByDefault) {
1980 *outRasterizers->append() = &kPDFRasterizers[i];
1981 }
1982 }
1983 }
1984
1985 // now show the user the set of configs that will be run.
1986 SkString configStr("These PDF rasterizers will be run:");
1987 // show the user the config that will run.
1988 for (int i = 0; i < outRasterizers->count(); ++i) {
1989 configStr.appendf(" %s", (*outRasterizers)[i]->fName);
1990 }
1991 gm_fprintf(stdout, "%s\n", configStr.c_str());
1992
1993 return true;
1994}
1995
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00001996static bool parse_flags_ignore_error_types(ErrorCombination* outErrorTypes) {
1997 if (FLAGS_ignoreErrorTypes.count() > 0) {
1998 *outErrorTypes = ErrorCombination();
1999 for (int i = 0; i < FLAGS_ignoreErrorTypes.count(); i++) {
2000 ErrorType type;
2001 const char *name = FLAGS_ignoreErrorTypes[i];
2002 if (!getErrorTypeByName(name, &type)) {
2003 gm_fprintf(stderr, "cannot find ErrorType with name '%s'\n", name);
2004 return false;
2005 } else {
2006 outErrorTypes->add(type);
2007 }
2008 }
2009 }
2010 return true;
2011}
2012
2013static bool parse_flags_modulo(int* moduloRemainder, int* moduloDivisor) {
2014 if (FLAGS_modulo.count() == 2) {
2015 *moduloRemainder = atoi(FLAGS_modulo[0]);
2016 *moduloDivisor = atoi(FLAGS_modulo[1]);
2017 if (*moduloRemainder < 0 || *moduloDivisor <= 0 ||
2018 *moduloRemainder >= *moduloDivisor) {
2019 gm_fprintf(stderr, "invalid modulo values.");
2020 return false;
2021 }
2022 }
2023 return true;
2024}
2025
2026#if SK_SUPPORT_GPU
2027static bool parse_flags_gpu_cache(int* sizeBytes, int* sizeCount) {
2028 if (FLAGS_gpuCacheSize.count() > 0) {
2029 if (FLAGS_gpuCacheSize.count() != 2) {
2030 gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
2031 return false;
2032 }
2033 *sizeBytes = atoi(FLAGS_gpuCacheSize[0]);
2034 *sizeCount = atoi(FLAGS_gpuCacheSize[1]);
2035 } else {
2036 *sizeBytes = DEFAULT_CACHE_VALUE;
2037 *sizeCount = DEFAULT_CACHE_VALUE;
2038 }
2039 return true;
2040}
2041#endif
2042
2043static bool parse_flags_tile_grid_replay_scales(SkTDArray<SkScalar>* outScales) {
2044 *outScales->append() = SK_Scalar1; // By default only test at scale 1.0
2045 if (FLAGS_tileGridReplayScales.count() > 0) {
2046 outScales->reset();
2047 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
2048 double val = atof(FLAGS_tileGridReplayScales[i]);
2049 if (0 < val) {
2050 *outScales->append() = SkDoubleToScalar(val);
2051 }
2052 }
2053 if (0 == outScales->count()) {
2054 // Should have at least one scale
2055 gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
2056 return false;
2057 }
2058 }
2059 return true;
2060}
2061
2062static bool parse_flags_gmmain_paths(GMMain* gmmain) {
2063 gmmain->fUseFileHierarchy = FLAGS_hierarchy;
2064 gmmain->fWriteChecksumBasedFilenames = FLAGS_writeChecksumBasedFilenames;
2065
2066 if (FLAGS_mismatchPath.count() == 1) {
2067 gmmain->fMismatchPath = FLAGS_mismatchPath[0];
2068 }
2069
2070 if (FLAGS_missingExpectationsPath.count() == 1) {
2071 gmmain->fMissingExpectationsPath = FLAGS_missingExpectationsPath[0];
2072 }
2073
2074 if (FLAGS_readPath.count() == 1) {
2075 const char* readPath = FLAGS_readPath[0];
2076 if (!sk_exists(readPath)) {
2077 gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
2078 return false;
2079 }
2080 if (sk_isdir(readPath)) {
2081 if (FLAGS_verbose) {
2082 gm_fprintf(stdout, "reading from %s\n", readPath);
2083 }
2084 gmmain->fExpectationsSource.reset(SkNEW_ARGS(
2085 IndividualImageExpectationsSource, (readPath)));
2086 } else {
2087 if (FLAGS_verbose) {
2088 gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
2089 }
2090 gmmain->fExpectationsSource.reset(SkNEW_ARGS(JsonExpectationsSource, (readPath)));
2091 }
2092 }
2093 return true;
2094}
2095
2096static bool parse_flags_match_strs(SkTDArray<const char*>* matchStrs) {
2097 for (int i = 0; i < FLAGS_match.count(); ++i) {
2098 matchStrs->push(FLAGS_match[i]);
2099 }
2100 return true;
2101}
2102
2103static bool parse_flags_resource_path() {
2104 if (FLAGS_resourcePath.count() == 1) {
2105 GM::SetResourcePath(FLAGS_resourcePath[0]);
2106 }
2107 return true;
2108}
2109
2110static bool parse_flags_jpeg_quality() {
2111 if (FLAGS_pdfJpegQuality < -1 || FLAGS_pdfJpegQuality > 100) {
2112 gm_fprintf(stderr, "%s\n", "pdfJpegQuality must be in [-1 .. 100] range.");
2113 return false;
2114 }
2115 return true;
2116}
2117
epoger@google.com0b62b3d2013-03-20 17:59:28 +00002118int tool_main(int argc, char** argv);
2119int tool_main(int argc, char** argv) {
2120
2121#if SK_ENABLE_INST_COUNT
2122 gPrintInstCount = true;
2123#endif
2124
2125 SkGraphics::Init();
2126 // we don't need to see this during a run
2127 gSkSuppressFontCachePurgeSpew = true;
2128
2129 setSystemPreferences();
2130 GMMain gmmain;
2131
epoger@google.com0b62b3d2013-03-20 17:59:28 +00002132 SkString usage;
2133 usage.printf("Run the golden master tests.\n");
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00002134 SkCommandLineFlags::SetUsage(usage.c_str());
2135 SkCommandLineFlags::Parse(argc, argv);
epoger@google.com0b62b3d2013-03-20 17:59:28 +00002136
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002137 SkTDArray<size_t> configs;
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002138
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002139 int moduloRemainder = -1;
2140 int moduloDivisor = -1;
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002141 SkTDArray<const PDFRasterizerData*> pdfRasterizers;
epoger@google.com6f6568b2013-03-22 17:29:46 +00002142 SkTDArray<SkScalar> tileGridReplayScales;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002143#if SK_SUPPORT_GPU
2144 GrContextFactory* grFactory = new GrContextFactory;
epoger@google.com80724df2013-03-21 13:49:54 +00002145#else
2146 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002147#endif
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002148 SkTDArray<const char*> matchStrs;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002149
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002150 if (!parse_flags_modulo(&moduloRemainder, &moduloDivisor) ||
2151 !parse_flags_ignore_error_types(&gmmain.fIgnorableErrorTypes) ||
2152#if SK_SUPPORT_GPU
2153 !parse_flags_gpu_cache(&gGpuCacheSizeBytes, &gGpuCacheSizeCount) ||
2154#endif
2155 !parse_flags_tile_grid_replay_scales(&tileGridReplayScales) ||
2156 !parse_flags_resource_path() ||
2157 !parse_flags_match_strs(&matchStrs) ||
2158 !parse_flags_jpeg_quality() ||
2159 !parse_flags_configs(&configs, grFactory) ||
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002160 !parse_flags_pdf_rasterizers(configs, &pdfRasterizers) ||
commit-bot@chromium.orgab882bf2013-08-14 21:56:47 +00002161 !parse_flags_gmmain_paths(&gmmain)) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00002162 return -1;
2163 }
2164
epoger@google.comb0f8b432013-04-10 18:46:25 +00002165 if (FLAGS_verbose) {
2166 if (FLAGS_writePath.count() == 1) {
2167 gm_fprintf(stdout, "writing to %s\n", FLAGS_writePath[0]);
2168 }
epoger@google.comfdea3252013-05-02 18:24:03 +00002169 if (NULL != gmmain.fMismatchPath) {
2170 gm_fprintf(stdout, "writing mismatches to %s\n", gmmain.fMismatchPath);
2171 }
epoger@google.com5f995452013-06-21 18:16:47 +00002172 if (NULL != gmmain.fMissingExpectationsPath) {
2173 gm_fprintf(stdout, "writing images without expectations to %s\n",
2174 gmmain.fMissingExpectationsPath);
2175 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00002176 if (FLAGS_writePicturePath.count() == 1) {
2177 gm_fprintf(stdout, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
2178 }
2179 if (FLAGS_resourcePath.count() == 1) {
2180 gm_fprintf(stdout, "reading resources from %s\n", FLAGS_resourcePath[0]);
2181 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00002182 }
2183
epoger@google.com310478e2013-04-03 18:00:39 +00002184 int gmsRun = 0;
reed@google.comae7b8f32012-10-18 21:30:57 +00002185 int gmIndex = -1;
2186 SkString moduloStr;
2187
epoger@google.come8ebeb12012-10-29 16:42:11 +00002188 // If we will be writing out files, prepare subdirectories.
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002189 if (FLAGS_writePath.count() == 1) {
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002190 if (!prepare_subdirectories(FLAGS_writePath[0], gmmain.fUseFileHierarchy,
2191 configs, pdfRasterizers)) {
epoger@google.come8ebeb12012-10-29 16:42:11 +00002192 return -1;
2193 }
epoger@google.comfdea3252013-05-02 18:24:03 +00002194 }
2195 if (NULL != gmmain.fMismatchPath) {
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002196 if (!prepare_subdirectories(gmmain.fMismatchPath, gmmain.fUseFileHierarchy,
2197 configs, pdfRasterizers)) {
epoger@google.comfdea3252013-05-02 18:24:03 +00002198 return -1;
epoger@google.come8ebeb12012-10-29 16:42:11 +00002199 }
2200 }
epoger@google.com5f995452013-06-21 18:16:47 +00002201 if (NULL != gmmain.fMissingExpectationsPath) {
2202 if (!prepare_subdirectories(gmmain.fMissingExpectationsPath, gmmain.fUseFileHierarchy,
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002203 configs, pdfRasterizers)) {
epoger@google.com5f995452013-06-21 18:16:47 +00002204 return -1;
2205 }
2206 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00002207
bsalomon@google.com7361f542012-04-19 19:15:35 +00002208 Iter iter;
2209 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00002210 while ((gm = iter.next()) != NULL) {
scroggo@google.com7fd2d702013-04-16 19:11:14 +00002211 SkAutoTDelete<GM> adgm(gm);
reed@google.comae7b8f32012-10-18 21:30:57 +00002212 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00002213 if (moduloRemainder >= 0) {
2214 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00002215 continue;
2216 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00002217 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00002218 }
2219
reed@google.comece2b022011-07-25 14:28:57 +00002220 const char* shortName = gm->shortName();
sglez@google.com586db932013-07-24 17:24:23 +00002221
sglez@google.com586db932013-07-24 17:24:23 +00002222 if (SkCommandLineFlags::ShouldSkip(matchStrs, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00002223 continue;
2224 }
2225
epoger@google.com310478e2013-04-03 18:00:39 +00002226 gmsRun++;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00002227 SkISize size = gm->getISize();
epoger@google.combcbf5aa2013-04-12 02:11:54 +00002228 gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
2229 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00002230
vandebo@chromium.org0dcbece2013-08-20 23:08:40 +00002231 run_multiple_configs(gmmain, gm, configs, pdfRasterizers, grFactory);
djsollen@google.comebce16d2012-10-26 14:07:13 +00002232
2233 SkBitmap comparisonBitmap;
2234 const ConfigData compareConfig =
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002235 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
epoger@google.com310478e2013-04-03 18:00:39 +00002236 gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false);
djsollen@google.comebce16d2012-10-26 14:07:13 +00002237
epoger@google.com80724df2013-03-21 13:49:54 +00002238 // TODO(epoger): only run this if gmmain.generate_image() succeeded?
2239 // Otherwise, what are we comparing against?
epoger@google.com310478e2013-04-03 18:00:39 +00002240 run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap, tileGridReplayScales);
reed@android.com00dae862009-06-10 15:38:48 +00002241 }
epoger@google.com310478e2013-04-03 18:00:39 +00002242
2243 SkTArray<SkString> modes;
2244 gmmain.GetRenderModesEncountered(modes);
epoger@google.comc8263e72013-04-10 12:17:34 +00002245 bool reportError = false;
2246 if (gmmain.NumSignificantErrors() > 0) {
2247 reportError = true;
2248 }
epoger@google.com51dbabe2013-04-10 15:24:53 +00002249 int expectedNumberOfTests = gmsRun * (configs.count() + modes.count());
epoger@google.com310478e2013-04-03 18:00:39 +00002250
2251 // Output summary to stdout.
epoger@google.com51dbabe2013-04-10 15:24:53 +00002252 if (FLAGS_verbose) {
2253 gm_fprintf(stdout, "Ran %d GMs\n", gmsRun);
2254 gm_fprintf(stdout, "... over %2d configs [%s]\n", configs.count(),
2255 list_all_config_names(configs).c_str());
2256 gm_fprintf(stdout, "... and %2d modes [%s]\n", modes.count(), list_all(modes).c_str());
2257 gm_fprintf(stdout, "... so there should be a total of %d tests.\n", expectedNumberOfTests);
2258 }
2259 gmmain.ListErrors(FLAGS_verbose);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002260
epoger@google.com07947d92013-04-11 15:41:02 +00002261 // TODO(epoger): Enable this check for Android, too, once we resolve
2262 // https://code.google.com/p/skia/issues/detail?id=1222
2263 // ('GM is unexpectedly skipping tests on Android')
2264#ifndef SK_BUILD_FOR_ANDROID
epoger@google.comc8263e72013-04-10 12:17:34 +00002265 if (expectedNumberOfTests != gmmain.fTestsRun) {
2266 gm_fprintf(stderr, "expected %d tests, but ran or skipped %d tests\n",
2267 expectedNumberOfTests, gmmain.fTestsRun);
2268 reportError = true;
2269 }
2270#endif
2271
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002272 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.com76c913d2013-04-26 15:06:44 +00002273 Json::Value root = CreateJsonTree(
2274 gmmain.fJsonExpectedResults,
2275 gmmain.fJsonActualResults_Failed, gmmain.fJsonActualResults_FailureIgnored,
2276 gmmain.fJsonActualResults_NoComparison, gmmain.fJsonActualResults_Succeeded);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002277 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002278 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002279 stream.write(jsonStdString.c_str(), jsonStdString.length());
2280 }
2281
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002282#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002283
robertphillips@google.com59552022012-08-31 13:07:37 +00002284#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002285 for (int i = 0; i < configs.count(); i++) {
2286 ConfigData config = gRec[configs[i]];
2287
epoger@google.comb0f8b432013-04-10 18:46:25 +00002288 if (FLAGS_verbose && (kGPU_Backend == config.fBackend)) {
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002289 GrContext* gr = grFactory->get(config.fGLContextType);
2290
epoger@google.com5efdd0c2013-03-13 14:18:40 +00002291 gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002292 gr->printCacheStats();
2293 }
2294 }
2295#endif
2296
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002297 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002298#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002299 SkGraphics::Term();
2300
epoger@google.comc8263e72013-04-10 12:17:34 +00002301 return (reportError) ? -1 : 0;
reed@android.com00dae862009-06-10 15:38:48 +00002302}
caryclark@google.com5987f582012-10-02 18:33:14 +00002303
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002304void GMMain::installFilter(SkCanvas* canvas) {
2305 if (FLAGS_forceBWtext) {
2306 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
2307 }
2308}
2309
borenet@google.com7158e6a2012-11-01 17:43:44 +00002310#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00002311int main(int argc, char * const argv[]) {
2312 return tool_main(argc, (char**) argv);
2313}
2314#endif