blob: c0471d5868ec401717346efc7d0537bcaef91b2e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
bsalomon@google.com971d0c82011-08-19 17:22:05 +00007
epoger@google.com57f7abc2012-11-13 03:41:55 +00008/*
9 * Code for the "gm" (Golden Master) rendering comparison tool.
10 *
11 * If you make changes to this, re-run the self-tests at gm/tests/run.sh
12 * to make sure they still pass... you may need to change the expected
13 * results of the self-test.
14 */
15
bungeman@google.comb29c8832011-10-10 13:19:10 +000016#include "gm.h"
epoger@google.com6f6568b2013-03-22 17:29:46 +000017#include "gm_error.h"
epoger@google.com37269602013-01-19 04:21:27 +000018#include "gm_expectations.h"
epoger@google.com7bc13a62012-02-14 14:53:59 +000019#include "system_preferences.h"
epoger@google.com5f6a0072013-01-31 16:30:55 +000020#include "SkBitmap.h"
reed@android.comb9b9a182009-07-08 02:54:47 +000021#include "SkColorPriv.h"
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000022#include "SkCommandLineFlags.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000023#include "SkData.h"
junov@google.com4370aed2012-01-18 16:21:08 +000024#include "SkDeferredCanvas.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000025#include "SkDevice.h"
epoger@google.comde961632012-10-26 18:56:36 +000026#include "SkDrawFilter.h"
commit-bot@chromium.orgc41295d2013-06-18 20:06:36 +000027#include "SkForceLinking.h"
scroggo@google.com5af9b202012-06-04 17:17:36 +000028#include "SkGPipe.h"
reed@android.com8015dd82009-06-21 00:49:18 +000029#include "SkGraphics.h"
30#include "SkImageDecoder.h"
31#include "SkImageEncoder.h"
epoger@google.come8ebeb12012-10-29 16:42:11 +000032#include "SkOSFile.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000033#include "SkPicture.h"
robertphillips@google.com977b9c82012-06-05 19:35:09 +000034#include "SkRefCnt.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000035#include "SkStream.h"
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000036#include "SkTArray.h"
epoger@google.com310478e2013-04-03 18:00:39 +000037#include "SkTDict.h"
junov@chromium.org3cb834b2012-12-13 16:39:53 +000038#include "SkTileGridPicture.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000039#include "SamplePipeControllers.h"
reed@google.com07700442010-12-20 19:46:07 +000040
bsalomon@google.com50c79d82013-01-08 20:31:53 +000041#ifdef SK_BUILD_FOR_WIN
42 // json includes xlocale which generates warning 4530 because we're compiling without
epoger@google.com37269602013-01-19 04:21:27 +000043 // exceptions; see https://code.google.com/p/skia/issues/detail?id=1067
bsalomon@google.com50c79d82013-01-08 20:31:53 +000044 #pragma warning(push)
45 #pragma warning(disable : 4530)
46#endif
epoger@google.comee8a8e32012-12-18 19:13:49 +000047#include "json/value.h"
bsalomon@google.com50c79d82013-01-08 20:31:53 +000048#ifdef SK_BUILD_FOR_WIN
49 #pragma warning(pop)
50#endif
epoger@google.comee8a8e32012-12-18 19:13:49 +000051
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000052#if SK_SUPPORT_GPU
53#include "GrContextFactory.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000054#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000055typedef GrContextFactory::GLContextType GLContextType;
epoger@google.com6f6568b2013-03-22 17:29:46 +000056#define DEFAULT_CACHE_VALUE -1
57static int gGpuCacheSizeBytes;
58static int gGpuCacheSizeCount;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000059#else
epoger@google.com80724df2013-03-21 13:49:54 +000060class GrContextFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000061class GrContext;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +000062class GrSurface;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000063typedef int GLContextType;
64#endif
65
epoger@google.com76c913d2013-04-26 15:06:44 +000066#define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message")
67
reed@google.com8923c6c2011-11-08 14:59:38 +000068extern bool gSkSuppressFontCachePurgeSpew;
69
reed@google.com07700442010-12-20 19:46:07 +000070#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000071 #include "SkPDFDevice.h"
72 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000073#endif
reed@android.com00dae862009-06-10 15:38:48 +000074
epoger@google.come3cc2eb2012-01-18 20:11:13 +000075// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
76// stop writing out XPS-format image baselines in gm.
77#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000078#ifdef SK_SUPPORT_XPS
79 #include "SkXPSDevice.h"
80#endif
81
reed@google.com46cce912011-06-29 12:54:46 +000082#ifdef SK_BUILD_FOR_MAC
83 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000084 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000085#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000086 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000087#endif
88
reed@android.com00dae862009-06-10 15:38:48 +000089using namespace skiagm;
90
reed@android.com00dae862009-06-10 15:38:48 +000091class Iter {
92public:
93 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000094 this->reset();
95 }
96
97 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000098 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000099 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000100
reed@android.comdd0ac282009-06-20 02:38:16 +0000101 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +0000102 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000103 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +0000104 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +0000105 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +0000106 }
107 return NULL;
108 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000109
reed@android.com00dae862009-06-10 15:38:48 +0000110 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +0000111 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +0000112 int count = 0;
113 while (reg) {
114 count += 1;
115 reg = reg->next();
116 }
117 return count;
118 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000119
reed@android.com00dae862009-06-10 15:38:48 +0000120private:
121 const GMRegistry* fReg;
122};
123
epoger@google.comce057fe2013-05-14 15:17:46 +0000124// TODO(epoger): Right now, various places in this code assume that all the
125// image files read/written by GM use this file extension.
126// Search for references to this constant to find these assumptions.
127const static char kPNG_FileExtension[] = "png";
128
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000129enum Backend {
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000130 kRaster_Backend,
131 kGPU_Backend,
132 kPDF_Backend,
133 kXPS_Backend,
134};
135
136enum BbhType {
137 kNone_BbhType,
138 kRTree_BbhType,
139 kTileGrid_BbhType,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000140};
141
bsalomon@google.com7361f542012-04-19 19:15:35 +0000142enum ConfigFlags {
143 kNone_ConfigFlag = 0x0,
144 /* Write GM images if a write path is provided. */
145 kWrite_ConfigFlag = 0x1,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000146 /* Read reference GM images if a read path is provided. */
bsalomon@google.com7361f542012-04-19 19:15:35 +0000147 kRead_ConfigFlag = 0x2,
148 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
149};
150
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000151struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000152 SkBitmap::Config fConfig;
153 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000154 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000155 int fSampleCnt; // GPU backend only
156 ConfigFlags fFlags;
157 const char* fName;
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000158 bool fRunByDefault;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000159};
160
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000161class BWTextDrawFilter : public SkDrawFilter {
162public:
reed@google.com971aca72012-11-26 20:26:54 +0000163 virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000164};
reed@google.com971aca72012-11-26 20:26:54 +0000165bool BWTextDrawFilter::filter(SkPaint* p, Type t) {
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000166 if (kText_Type == t) {
167 p->setAntiAlias(false);
168 }
reed@google.com971aca72012-11-26 20:26:54 +0000169 return true;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000170}
171
scroggo@google.com565254b2012-06-28 15:41:32 +0000172struct PipeFlagComboData {
173 const char* name;
174 uint32_t flags;
175};
176
177static PipeFlagComboData gPipeWritingFlagCombos[] = {
178 { "", 0 },
179 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000180 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000181 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000182};
183
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000184static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect);
185
epoger@google.com5079d2c2013-04-12 14:11:21 +0000186const static ErrorCombination kDefaultIgnorableErrorTypes = ErrorCombination()
187 .plus(kMissingExpectations_ErrorType)
188 .plus(kIntentionallySkipped_ErrorType);
189
epoger@google.comde961632012-10-26 18:56:36 +0000190class GMMain {
191public:
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000192 GMMain() : fUseFileHierarchy(false), fWriteChecksumBasedFilenames(false),
193 fIgnorableErrorTypes(kDefaultIgnorableErrorTypes),
epoger@google.com5f995452013-06-21 18:16:47 +0000194 fMismatchPath(NULL), fMissingExpectationsPath(NULL), fTestsRun(0),
195 fRenderModesEncountered(1) {}
epoger@google.come8ebeb12012-10-29 16:42:11 +0000196
epoger@google.com659c8c02013-05-21 15:45:45 +0000197 /**
198 * Assemble shortNamePlusConfig from (surprise!) shortName and configName.
199 *
200 * The method for doing so depends on whether we are using hierarchical naming.
201 * For example, shortName "selftest1" and configName "8888" could be assembled into
202 * either "selftest1_8888" or "8888/selftest1".
203 */
204 SkString make_shortname_plus_config(const char *shortName, const char *configName) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000205 SkString name;
epoger@google.com57f7abc2012-11-13 03:41:55 +0000206 if (0 == strlen(configName)) {
207 name.append(shortName);
208 } else if (fUseFileHierarchy) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000209 name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
210 } else {
211 name.appendf("%s_%s", shortName, configName);
212 }
epoger@google.comde961632012-10-26 18:56:36 +0000213 return name;
214 }
215
epoger@google.com659c8c02013-05-21 15:45:45 +0000216 /**
217 * Assemble filename, suitable for writing out the results of a particular test.
218 */
219 SkString make_filename(const char *path,
220 const char *shortName,
221 const char *configName,
222 const char *renderModeDescriptor,
223 const char *suffix) {
224 SkString filename = make_shortname_plus_config(shortName, configName);
225 filename.append(renderModeDescriptor);
226 filename.appendUnichar('.');
227 filename.append(suffix);
scroggo@google.comccd7afb2013-05-28 16:45:07 +0000228 return SkOSPath::SkPathJoin(path, filename.c_str());
epoger@google.com659c8c02013-05-21 15:45:45 +0000229 }
230
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000231 /**
232 * Assemble filename suitable for writing out an SkBitmap.
233 */
234 SkString make_bitmap_filename(const char *path,
235 const char *shortName,
236 const char *configName,
237 const char *renderModeDescriptor,
238 const GmResultDigest &bitmapDigest) {
239 if (fWriteChecksumBasedFilenames) {
240 SkString filename;
241 filename.append(bitmapDigest.getHashType());
242 filename.appendUnichar('_');
243 filename.append(shortName);
244 filename.appendUnichar('_');
245 filename.append(bitmapDigest.getDigestValue());
246 filename.appendUnichar('.');
247 filename.append(kPNG_FileExtension);
248 return SkOSPath::SkPathJoin(path, filename.c_str());
249 } else {
250 return make_filename(path, shortName, configName, renderModeDescriptor,
251 kPNG_FileExtension);
252 }
253 }
254
epoger@google.com5f6a0072013-01-31 16:30:55 +0000255 /* since PNG insists on unpremultiplying our alpha, we take no
256 precision chances and force all pixels to be 100% opaque,
257 otherwise on compare we may not get a perfect match.
258 */
259 static void force_all_opaque(const SkBitmap& bitmap) {
260 SkBitmap::Config config = bitmap.config();
261 switch (config) {
262 case SkBitmap::kARGB_8888_Config:
263 force_all_opaque_8888(bitmap);
264 break;
265 case SkBitmap::kRGB_565_Config:
266 // nothing to do here; 565 bitmaps are inherently opaque
267 break;
268 default:
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000269 gm_fprintf(stderr, "unsupported bitmap config %d\n", config);
270 DEBUGFAIL_SEE_STDERR;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000271 }
272 }
273
274 static void force_all_opaque_8888(const SkBitmap& bitmap) {
275 SkAutoLockPixels lock(bitmap);
276 for (int y = 0; y < bitmap.height(); y++) {
277 for (int x = 0; x < bitmap.width(); x++) {
278 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
279 }
280 }
281 }
282
283 static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
284 // TODO(epoger): Now that we have removed force_all_opaque()
285 // from this method, we should be able to get rid of the
286 // transformation to 8888 format also.
287 SkBitmap copy;
288 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
289 return SkImageEncoder::EncodeFile(path.c_str(), copy,
290 SkImageEncoder::kPNG_Type, 100);
291 }
292
epoger@google.com6f6568b2013-03-22 17:29:46 +0000293 /**
epoger@google.com310478e2013-04-03 18:00:39 +0000294 * Add all render modes encountered thus far to the "modes" array.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000295 */
epoger@google.com310478e2013-04-03 18:00:39 +0000296 void GetRenderModesEncountered(SkTArray<SkString> &modes) {
297 SkTDict<int>::Iter iter(this->fRenderModesEncountered);
298 const char* mode;
299 while ((mode = iter.next(NULL)) != NULL) {
300 SkString modeAsString = SkString(mode);
301 // TODO(epoger): It seems a bit silly that all of these modes were
302 // recorded with a leading "-" which we have to remove here
303 // (except for mode "", which means plain old original mode).
304 // But that's how renderModeDescriptor has been passed into
305 // compare_test_results_to_reference_bitmap() historically,
306 // and changing that now may affect other parts of our code.
307 if (modeAsString.startsWith("-")) {
308 modeAsString.remove(0, 1);
309 modes.push_back(modeAsString);
310 }
311 }
312 }
313
314 /**
315 * Records the results of this test in fTestsRun and fFailedTests.
316 *
317 * We even record successes, and errors that we regard as
318 * "ignorable"; we can filter them out later.
319 */
epoger@google.com659c8c02013-05-21 15:45:45 +0000320 void RecordTestResults(const ErrorCombination& errorCombination,
321 const SkString& shortNamePlusConfig,
epoger@google.com310478e2013-04-03 18:00:39 +0000322 const char renderModeDescriptor []) {
323 // Things to do regardless of errorCombination.
324 fTestsRun++;
325 int renderModeCount = 0;
326 this->fRenderModesEncountered.find(renderModeDescriptor, &renderModeCount);
327 renderModeCount++;
328 this->fRenderModesEncountered.set(renderModeDescriptor, renderModeCount);
329
epoger@google.com6f6568b2013-03-22 17:29:46 +0000330 if (errorCombination.isEmpty()) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000331 return;
epoger@google.comeb066362013-03-08 09:39:36 +0000332 }
333
epoger@google.com310478e2013-04-03 18:00:39 +0000334 // Things to do only if there is some error condition.
epoger@google.com659c8c02013-05-21 15:45:45 +0000335 SkString fullName = shortNamePlusConfig;
epoger@google.comcaac3db2013-04-04 19:23:11 +0000336 fullName.append(renderModeDescriptor);
epoger@google.com310478e2013-04-03 18:00:39 +0000337 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
338 ErrorType type = static_cast<ErrorType>(typeInt);
339 if (errorCombination.includes(type)) {
340 fFailedTests[type].push_back(fullName);
epoger@google.comf60494b2013-04-03 17:02:53 +0000341 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000342 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000343 }
344
epoger@google.com310478e2013-04-03 18:00:39 +0000345 /**
346 * Return the number of significant (non-ignorable) errors we have
347 * encountered so far.
348 */
349 int NumSignificantErrors() {
350 int significantErrors = 0;
351 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
352 ErrorType type = static_cast<ErrorType>(typeInt);
epoger@google.com5079d2c2013-04-12 14:11:21 +0000353 if (!fIgnorableErrorTypes.includes(type)) {
epoger@google.com310478e2013-04-03 18:00:39 +0000354 significantErrors += fFailedTests[type].count();
355 }
356 }
357 return significantErrors;
358 }
359
360 /**
epoger@google.com51dbabe2013-04-10 15:24:53 +0000361 * Display the summary of results with this ErrorType.
362 *
363 * @param type which ErrorType
364 * @param verbose whether to be all verbose about it
epoger@google.com310478e2013-04-03 18:00:39 +0000365 */
epoger@google.com51dbabe2013-04-10 15:24:53 +0000366 void DisplayResultTypeSummary(ErrorType type, bool verbose) {
epoger@google.com5079d2c2013-04-12 14:11:21 +0000367 bool isIgnorableType = fIgnorableErrorTypes.includes(type);
epoger@google.com51dbabe2013-04-10 15:24:53 +0000368
369 SkString line;
370 if (isIgnorableType) {
371 line.append("[ ] ");
372 } else {
373 line.append("[*] ");
374 }
375
376 SkTArray<SkString> *failedTestsOfThisType = &fFailedTests[type];
377 int count = failedTestsOfThisType->count();
378 line.appendf("%d %s", count, getErrorTypeName(type));
379 if (!isIgnorableType || verbose) {
380 line.append(":");
381 for (int i = 0; i < count; ++i) {
382 line.append(" ");
383 line.append((*failedTestsOfThisType)[i]);
384 }
385 }
386 gm_fprintf(stdout, "%s\n", line.c_str());
387 }
388
389 /**
390 * List contents of fFailedTests to stdout.
391 *
392 * @param verbose whether to be all verbose about it
393 */
394 void ListErrors(bool verbose) {
epoger@google.com310478e2013-04-03 18:00:39 +0000395 // First, print a single summary line.
396 SkString summary;
397 summary.appendf("Ran %d tests:", fTestsRun);
398 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
399 ErrorType type = static_cast<ErrorType>(typeInt);
400 summary.appendf(" %s=%d", getErrorTypeName(type), fFailedTests[type].count());
401 }
402 gm_fprintf(stdout, "%s\n", summary.c_str());
403
404 // Now, for each failure type, list the tests that failed that way.
405 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
epoger@google.com51dbabe2013-04-10 15:24:53 +0000406 this->DisplayResultTypeSummary(static_cast<ErrorType>(typeInt), verbose);
epoger@google.com310478e2013-04-03 18:00:39 +0000407 }
408 gm_fprintf(stdout, "(results marked with [*] will cause nonzero return value)\n");
409 }
410
bungeman@google.com88682b72013-07-19 13:55:41 +0000411 static bool write_document(const SkString& path, SkStreamAsset* asset) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000412 SkFILEWStream stream(path.c_str());
bungeman@google.com88682b72013-07-19 13:55:41 +0000413 return stream.writeStream(asset, asset->getLength());
epoger@google.com5f6a0072013-01-31 16:30:55 +0000414 }
415
epoger@google.com37269602013-01-19 04:21:27 +0000416 /**
epoger@google.com5f6a0072013-01-31 16:30:55 +0000417 * Prepare an SkBitmap to render a GM into.
418 *
419 * After you've rendered the GM into the SkBitmap, you must call
420 * complete_bitmap()!
421 *
422 * @todo thudson 22 April 2011 - could refactor this to take in
423 * a factory to generate the context, always call readPixels()
424 * (logically a noop for rasters, if wasted time), and thus collapse the
425 * GPU special case and also let this be used for SkPicture testing.
426 */
427 static void setup_bitmap(const ConfigData& gRec, SkISize& size,
428 SkBitmap* bitmap) {
429 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
430 bitmap->allocPixels();
431 bitmap->eraseColor(SK_ColorTRANSPARENT);
432 }
433
434 /**
435 * Any finalization steps we need to perform on the SkBitmap after
436 * we have rendered the GM into it.
epoger@google.com37269602013-01-19 04:21:27 +0000437 *
438 * It's too bad that we are throwing away alpha channel data
439 * we could otherwise be examining, but this had always been happening
440 * before... it was buried within the compare() method at
441 * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
442 *
443 * Apparently we need this, at least for bitmaps that are either:
444 * (a) destined to be written out as PNG files, or
445 * (b) compared against bitmaps read in from PNG files
446 * for the reasons described just above the force_all_opaque() method.
447 *
448 * Neglecting to do this led to the difficult-to-diagnose
449 * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
450 * spurious pixel_error messages as of r7258')
451 *
452 * TODO(epoger): Come up with a better solution that allows us to
453 * compare full pixel data, including alpha channel, while still being
454 * robust in the face of transformations to/from PNG files.
455 * Options include:
456 *
457 * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
458 * will be written to, or compared against, PNG files.
459 * PRO: Preserve/compare alpha channel info for the non-PNG cases
460 * (comparing different renderModes in-memory)
epoger@google.com908f5832013-04-12 02:23:55 +0000461 * CON: The bitmaps (and hash digests) for these non-PNG cases would be
epoger@google.com37269602013-01-19 04:21:27 +0000462 * different than those for the PNG-compared cases, and in the
463 * case of a failed renderMode comparison, how would we write the
464 * image to disk for examination?
465 *
epoger@google.com908f5832013-04-12 02:23:55 +0000466 * 2. Always compute image hash digests from PNG format (either
epoger@google.com37269602013-01-19 04:21:27 +0000467 * directly from the the bytes of a PNG file, or capturing the
468 * bytes we would have written to disk if we were writing the
469 * bitmap out as a PNG).
470 * PRO: I think this would allow us to never force opaque, and to
471 * the extent that alpha channel data can be preserved in a PNG
472 * file, we could observe it.
epoger@google.com908f5832013-04-12 02:23:55 +0000473 * CON: If we read a bitmap from disk, we need to take its hash digest
epoger@google.com37269602013-01-19 04:21:27 +0000474 * from the source PNG (we can't compute it from the bitmap we
475 * read out of the PNG, because we will have already premultiplied
476 * the alpha).
477 * CON: Seems wasteful to convert a bitmap to PNG format just to take
epoger@google.com908f5832013-04-12 02:23:55 +0000478 * its hash digest. (Although we're wasting lots of effort already
epoger@google.com37269602013-01-19 04:21:27 +0000479 * calling force_all_opaque().)
480 *
481 * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
482 * so we can transform images back and forth without fear of off-by-one
483 * errors.
484 * CON: Math is hard.
485 *
486 * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
487 * channel), rather than demanding absolute equality.
epoger@google.com908f5832013-04-12 02:23:55 +0000488 * CON: Can't do this with hash digests.
epoger@google.com37269602013-01-19 04:21:27 +0000489 */
epoger@google.com5f6a0072013-01-31 16:30:55 +0000490 static void complete_bitmap(SkBitmap* bitmap) {
491 force_all_opaque(*bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000492 }
493
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000494 static void installFilter(SkCanvas* canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000495
reed@google.comaef73612012-11-16 13:41:45 +0000496 static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000497 SkAutoCanvasRestore acr(canvas, true);
498
499 if (!isPDF) {
500 canvas->concat(gm->getInitialTransform());
501 }
502 installFilter(canvas);
reed@google.comaef73612012-11-16 13:41:45 +0000503 gm->setCanvasIsDeferred(isDeferred);
epoger@google.comde961632012-10-26 18:56:36 +0000504 gm->draw(canvas);
505 canvas->setDrawFilter(NULL);
506 }
507
epoger@google.com6f6568b2013-03-22 17:29:46 +0000508 static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000509 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000510 SkBitmap* bitmap,
511 bool deferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000512 SkISize size (gm->getISize());
513 setup_bitmap(gRec, size, bitmap);
514
515 SkAutoTUnref<SkCanvas> canvas;
516
517 if (gRec.fBackend == kRaster_Backend) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000518 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (*bitmap)));
epoger@google.comde961632012-10-26 18:56:36 +0000519 if (deferred) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000520#if SK_DEFERRED_CANVAS_USES_FACTORIES
521 canvas.reset(SkDeferredCanvas::Create(device));
522#else
523 canvas.reset(SkNEW_ARGS(SkDeferredCanvas, (device)));
524#endif
epoger@google.comde961632012-10-26 18:56:36 +0000525 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000526 canvas.reset(SkNEW_ARGS(SkCanvas, (device)));
epoger@google.comde961632012-10-26 18:56:36 +0000527 }
reed@google.comaef73612012-11-16 13:41:45 +0000528 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000529 canvas->flush();
530 }
531#if SK_SUPPORT_GPU
532 else { // GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000533 SkAutoTUnref<SkDevice> device(SkGpuDevice::Create(gpuTarget));
epoger@google.comde961632012-10-26 18:56:36 +0000534 if (deferred) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000535#if SK_DEFERRED_CANVAS_USES_FACTORIES
536 canvas.reset(SkDeferredCanvas::Create(device));
537#else
538 canvas.reset(SkNEW_ARGS(SkDeferredCanvas, (device)));
539#endif
epoger@google.comde961632012-10-26 18:56:36 +0000540 } else {
junov@chromium.org66070a52013-05-28 17:39:08 +0000541 canvas.reset(SkNEW_ARGS(SkCanvas, (device)));
epoger@google.comde961632012-10-26 18:56:36 +0000542 }
reed@google.comaef73612012-11-16 13:41:45 +0000543 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000544 // the device is as large as the current rendertarget, so
545 // we explicitly only readback the amount we expect (in
546 // size) overwrite our previous allocation
547 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
548 size.fHeight);
549 canvas->readPixels(bitmap, 0, 0);
550 }
551#endif
epoger@google.com5f6a0072013-01-31 16:30:55 +0000552 complete_bitmap(bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000553 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000554 }
555
556 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
junov@chromium.orgc938c482012-12-19 15:24:38 +0000557 SkPicture* pict, SkBitmap* bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000558 SkScalar scale = SK_Scalar1,
559 bool tile = false) {
epoger@google.comde961632012-10-26 18:56:36 +0000560 SkISize size = gm->getISize();
561 setup_bitmap(gRec, size, bitmap);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000562
563 if (tile) {
564 // Generate the result image by rendering to tiles and accumulating
565 // the results in 'bitmap'
566
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000567 // This 16x16 tiling matches the settings applied to 'pict' in
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000568 // 'generate_new_picture'
569 SkISize tileSize = SkISize::Make(16, 16);
570
571 SkBitmap tileBM;
572 setup_bitmap(gRec, tileSize, &tileBM);
573 SkCanvas tileCanvas(tileBM);
574 installFilter(&tileCanvas);
575
576 SkCanvas bmpCanvas(*bitmap);
577 SkPaint bmpPaint;
578 bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
579
580 for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
581 for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
582 int saveCount = tileCanvas.save();
583 SkMatrix mat(tileCanvas.getTotalMatrix());
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000584 mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000585 SkIntToScalar(-yTile*tileSize.height()));
586 tileCanvas.setMatrix(mat);
587 pict->draw(&tileCanvas);
588 tileCanvas.flush();
589 tileCanvas.restoreToCount(saveCount);
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000590 bmpCanvas.drawBitmap(tileBM,
591 SkIntToScalar(xTile * tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000592 SkIntToScalar(yTile * tileSize.height()),
593 &bmpPaint);
594 }
595 }
596 } else {
597 SkCanvas canvas(*bitmap);
598 installFilter(&canvas);
599 canvas.scale(scale, scale);
600 canvas.drawPicture(*pict);
601 complete_bitmap(bitmap);
602 }
epoger@google.comde961632012-10-26 18:56:36 +0000603 }
604
605 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
606#ifdef SK_SUPPORT_PDF
607 SkMatrix initialTransform = gm->getInitialTransform();
608 SkISize pageSize = gm->getISize();
609 SkPDFDevice* dev = NULL;
610 if (initialTransform.isIdentity()) {
611 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
612 } else {
613 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
614 SkIntToScalar(pageSize.height()));
615 initialTransform.mapRect(&content);
616 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
617 SkIntToScalar(pageSize.height()));
618 SkISize contentSize =
619 SkISize::Make(SkScalarRoundToInt(content.width()),
620 SkScalarRoundToInt(content.height()));
621 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
622 }
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000623 dev->setDCTEncoder(encode_to_dct_stream);
epoger@google.comde961632012-10-26 18:56:36 +0000624 SkAutoUnref aur(dev);
625
626 SkCanvas c(dev);
reed@google.comaef73612012-11-16 13:41:45 +0000627 invokeGM(gm, &c, true, false);
epoger@google.comde961632012-10-26 18:56:36 +0000628
629 SkPDFDocument doc;
630 doc.appendPage(dev);
631 doc.emitPDF(&pdf);
632#endif
633 }
634
635 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
636#ifdef SK_SUPPORT_XPS
637 SkISize size = gm->getISize();
638
639 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
640 SkIntToScalar(size.height()));
641 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
642 static const SkScalar upm = 72 * inchesPerMeter;
643 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
644 static const SkScalar ppm = 200 * inchesPerMeter;
645 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
646
647 SkXPSDevice* dev = new SkXPSDevice();
648 SkAutoUnref aur(dev);
649
650 SkCanvas c(dev);
651 dev->beginPortfolio(&xps);
652 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000653 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000654 dev->endSheet();
655 dev->endPortfolio();
656
657#endif
658 }
659
epoger@google.com6f6568b2013-03-22 17:29:46 +0000660 ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
epoger@google.com659c8c02013-05-21 15:45:45 +0000661 const char renderModeDescriptor [],
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000662 const char *shortName,
epoger@google.come33e1372013-07-08 19:13:33 +0000663 const BitmapAndDigest* bitmapAndDigest,
bungeman@google.com88682b72013-07-19 13:55:41 +0000664 SkStreamAsset* document) {
epoger@google.comde961632012-10-26 18:56:36 +0000665 SkString path;
666 bool success = false;
667 if (gRec.fBackend == kRaster_Backend ||
668 gRec.fBackend == kGPU_Backend ||
669 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
670
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000671 path = make_bitmap_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.come33e1372013-07-08 19:13:33 +0000672 bitmapAndDigest->fDigest);
673 success = write_bitmap(path, bitmapAndDigest->fBitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000674 }
675 if (kPDF_Backend == gRec.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000676 path = make_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000677 "pdf");
bungeman@google.com88682b72013-07-19 13:55:41 +0000678 success = write_document(path, document);
epoger@google.comde961632012-10-26 18:56:36 +0000679 }
680 if (kXPS_Backend == gRec.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000681 path = make_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000682 "xps");
bungeman@google.com88682b72013-07-19 13:55:41 +0000683 success = write_document(path, document);
epoger@google.comde961632012-10-26 18:56:36 +0000684 }
685 if (success) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000686 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000687 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000688 gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000689 ErrorCombination errors(kWritingReferenceImage_ErrorType);
epoger@google.com310478e2013-04-03 18:00:39 +0000690 // TODO(epoger): Don't call RecordTestResults() here...
691 // Instead, we should make sure to call RecordTestResults
692 // exactly ONCE per test. (Otherwise, gmmain.fTestsRun
693 // will be incremented twice for this test: once in
694 // compare_test_results_to_stored_expectations() before
695 // that method calls this one, and again here.)
696 //
697 // When we make that change, we should probably add a
698 // WritingReferenceImage test to the gm self-tests.)
epoger@google.com659c8c02013-05-21 15:45:45 +0000699 RecordTestResults(errors, make_shortname_plus_config(shortName, gRec.fName),
700 renderModeDescriptor);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000701 return errors;
epoger@google.comde961632012-10-26 18:56:36 +0000702 }
703 }
704
epoger@google.com37269602013-01-19 04:21:27 +0000705 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000706 * Log more detail about the mistmatch between expectedBitmap and
707 * actualBitmap.
708 */
709 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
710 const char *testName) {
711 const int expectedWidth = expectedBitmap.width();
712 const int expectedHeight = expectedBitmap.height();
713 const int width = actualBitmap.width();
714 const int height = actualBitmap.height();
715 if ((expectedWidth != width) || (expectedHeight != height)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000716 gm_fprintf(stderr, "---- %s: dimension mismatch --"
717 " expected [%d %d], actual [%d %d]\n",
718 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000719 return;
720 }
721
722 if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
723 (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000724 gm_fprintf(stderr, "---- %s: not computing max per-channel"
725 " pixel mismatch because non-8888\n", testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000726 return;
727 }
728
729 SkAutoLockPixels alp0(expectedBitmap);
730 SkAutoLockPixels alp1(actualBitmap);
731 int errR = 0;
732 int errG = 0;
733 int errB = 0;
734 int errA = 0;
735 int differingPixels = 0;
736
737 for (int y = 0; y < height; ++y) {
738 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
739 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
740 for (int x = 0; x < width; ++x) {
741 SkPMColor expectedPixel = *expectedPixelPtr++;
742 SkPMColor actualPixel = *actualPixelPtr++;
743 if (expectedPixel != actualPixel) {
744 differingPixels++;
745 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
746 (int)SkGetPackedR32(actualPixel)));
747 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
748 (int)SkGetPackedG32(actualPixel)));
749 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
750 (int)SkGetPackedB32(actualPixel)));
751 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
752 (int)SkGetPackedA32(actualPixel)));
753 }
754 }
755 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000756 gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
757 " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
758 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000759 }
760
761 /**
epoger@google.com908f5832013-04-12 02:23:55 +0000762 * Compares actual hash digest to expectations, returning the set of errors
epoger@google.com6f6568b2013-03-22 17:29:46 +0000763 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000764 *
765 * If fMismatchPath has been set, and there are pixel diffs, then the
766 * actual bitmap will be written out to a file within fMismatchPath.
epoger@google.com5f995452013-06-21 18:16:47 +0000767 * And similarly for fMissingExpectationsPath...
epoger@google.com37269602013-01-19 04:21:27 +0000768 *
769 * @param expectations what expectations to compare actualBitmap against
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000770 * @param actualBitmapAndDigest the SkBitmap we actually generated, and its GmResultDigest
epoger@google.com659c8c02013-05-21 15:45:45 +0000771 * @param shortName name of test, e.g. "selftest1"
772 * @param configName name of config, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000773 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
774 * @param addToJsonSummary whether to add these results (both actual and
epoger@google.comcaac3db2013-04-04 19:23:11 +0000775 * expected) to the JSON summary. Regardless of this setting, if
776 * we find an image mismatch in this test, we will write these
777 * results to the JSON summary. (This is so that we will always
778 * report errors across rendering modes, such as pipe vs tiled.
779 * See https://codereview.chromium.org/13650002/ )
epoger@google.com37269602013-01-19 04:21:27 +0000780 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000781 ErrorCombination compare_to_expectations(Expectations expectations,
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000782 const BitmapAndDigest& actualBitmapAndDigest,
epoger@google.com659c8c02013-05-21 15:45:45 +0000783 const char *shortName, const char *configName,
784 const char *renderModeDescriptor,
epoger@google.comcaac3db2013-04-04 19:23:11 +0000785 bool addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000786 ErrorCombination errors;
epoger@google.com659c8c02013-05-21 15:45:45 +0000787 SkString shortNamePlusConfig = make_shortname_plus_config(shortName, configName);
788 SkString completeNameString(shortNamePlusConfig);
epoger@google.com37269602013-01-19 04:21:27 +0000789 completeNameString.append(renderModeDescriptor);
epoger@google.comce057fe2013-05-14 15:17:46 +0000790 completeNameString.append(".");
791 completeNameString.append(kPNG_FileExtension);
epoger@google.com37269602013-01-19 04:21:27 +0000792 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000793
epoger@google.com37269602013-01-19 04:21:27 +0000794 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000795 errors.add(kMissingExpectations_ErrorType);
epoger@google.com5f995452013-06-21 18:16:47 +0000796
797 // Write out the "actuals" for any tests without expectations, if we have
798 // been directed to do so.
799 if (fMissingExpectationsPath) {
800 SkString path = make_bitmap_filename(fMissingExpectationsPath, shortName,
801 configName, renderModeDescriptor,
802 actualBitmapAndDigest.fDigest);
803 write_bitmap(path, actualBitmapAndDigest.fBitmap);
804 }
805
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000806 } else if (!expectations.match(actualBitmapAndDigest.fDigest)) {
epoger@google.comcaac3db2013-04-04 19:23:11 +0000807 addToJsonSummary = true;
808 // The error mode we record depends on whether this was running
809 // in a non-standard renderMode.
810 if ('\0' == *renderModeDescriptor) {
811 errors.add(kExpectationsMismatch_ErrorType);
812 } else {
813 errors.add(kRenderModeMismatch_ErrorType);
814 }
epoger@google.com84a18022013-02-01 20:39:15 +0000815
816 // Write out the "actuals" for any mismatches, if we have
817 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000818 if (fMismatchPath) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000819 SkString path = make_bitmap_filename(fMismatchPath, shortName, configName,
820 renderModeDescriptor,
821 actualBitmapAndDigest.fDigest);
822 write_bitmap(path, actualBitmapAndDigest.fBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000823 }
epoger@google.com84a18022013-02-01 20:39:15 +0000824
825 // If we have access to a single expected bitmap, log more
826 // detail about the mismatch.
827 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
828 if (NULL != expectedBitmapPtr) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000829 report_bitmap_diffs(*expectedBitmapPtr, actualBitmapAndDigest.fBitmap,
830 completeName);
epoger@google.com84a18022013-02-01 20:39:15 +0000831 }
epoger@google.coma243b222013-01-17 17:54:28 +0000832 }
epoger@google.com659c8c02013-05-21 15:45:45 +0000833 RecordTestResults(errors, shortNamePlusConfig, renderModeDescriptor);
epoger@google.coma243b222013-01-17 17:54:28 +0000834
epoger@google.com37269602013-01-19 04:21:27 +0000835 if (addToJsonSummary) {
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000836 add_actual_results_to_json_summary(completeName, actualBitmapAndDigest.fDigest, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000837 expectations.ignoreFailure());
838 add_expected_results_to_json_summary(completeName, expectations);
839 }
epoger@google.coma243b222013-01-17 17:54:28 +0000840
epoger@google.com6f6568b2013-03-22 17:29:46 +0000841 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000842 }
843
epoger@google.com37269602013-01-19 04:21:27 +0000844 /**
845 * Add this result to the appropriate JSON collection of actual results,
epoger@google.com76c913d2013-04-26 15:06:44 +0000846 * depending on errors encountered.
epoger@google.com37269602013-01-19 04:21:27 +0000847 */
848 void add_actual_results_to_json_summary(const char testName[],
epoger@google.comd4993ff2013-05-24 14:33:28 +0000849 const GmResultDigest &actualResultDigest,
epoger@google.com76c913d2013-04-26 15:06:44 +0000850 ErrorCombination errors,
epoger@google.com37269602013-01-19 04:21:27 +0000851 bool ignoreFailure) {
epoger@google.comd4993ff2013-05-24 14:33:28 +0000852 Json::Value jsonActualResults = actualResultDigest.asJsonTypeValuePair();
epoger@google.com76c913d2013-04-26 15:06:44 +0000853 if (errors.isEmpty()) {
854 this->fJsonActualResults_Succeeded[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000855 } else {
856 if (ignoreFailure) {
857 // TODO: Once we have added the ability to compare
858 // actual results against expectations in a JSON file
859 // (where we can set ignore-failure to either true or
epoger@google.com84a18022013-02-01 20:39:15 +0000860 // false), add test cases that exercise ignored
epoger@google.com6f6568b2013-03-22 17:29:46 +0000861 // failures (both for kMissingExpectations_ErrorType
epoger@google.comcaac3db2013-04-04 19:23:11 +0000862 // and kExpectationsMismatch_ErrorType).
epoger@google.com37269602013-01-19 04:21:27 +0000863 this->fJsonActualResults_FailureIgnored[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000864 jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000865 } else {
epoger@google.com76c913d2013-04-26 15:06:44 +0000866 if (errors.includes(kMissingExpectations_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000867 // TODO: What about the case where there IS an
epoger@google.com908f5832013-04-12 02:23:55 +0000868 // expected image hash digest, but that gm test
epoger@google.com37269602013-01-19 04:21:27 +0000869 // doesn't actually run? For now, those cases
870 // will always be ignored, because gm only looks
871 // at expectations that correspond to gm tests
872 // that were actually run.
873 //
874 // Once we have the ability to express
875 // expectations as a JSON file, we should fix this
876 // (and add a test case for which an expectation
877 // is given but the test is never run).
878 this->fJsonActualResults_NoComparison[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000879 jsonActualResults;
epoger@google.comeb066362013-03-08 09:39:36 +0000880 }
epoger@google.com76c913d2013-04-26 15:06:44 +0000881 if (errors.includes(kExpectationsMismatch_ErrorType) ||
882 errors.includes(kRenderModeMismatch_ErrorType)) {
883 this->fJsonActualResults_Failed[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000884 }
885 }
886 }
887 }
888
889 /**
890 * Add this test to the JSON collection of expected results.
891 */
892 void add_expected_results_to_json_summary(const char testName[],
893 Expectations expectations) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000894 this->fJsonExpectedResults[testName] = expectations.asJsonValue();
epoger@google.com37269602013-01-19 04:21:27 +0000895 }
896
897 /**
898 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
899 *
900 * @param gm which test generated the actualBitmap
901 * @param gRec
902 * @param writePath unless this is NULL, write out actual images into this
903 * directory
epoger@google.come33e1372013-07-08 19:13:33 +0000904 * @param actualBitmapAndDigest ptr to bitmap generated by this run, or NULL
905 * if we don't have a usable bitmap representation
906 * @param document pdf or xps representation, if appropriate
epoger@google.com37269602013-01-19 04:21:27 +0000907 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000908 ErrorCombination compare_test_results_to_stored_expectations(
epoger@google.com37269602013-01-19 04:21:27 +0000909 GM* gm, const ConfigData& gRec, const char writePath[],
bungeman@google.com88682b72013-07-19 13:55:41 +0000910 const BitmapAndDigest* actualBitmapAndDigest, SkStreamAsset* document) {
epoger@google.com37269602013-01-19 04:21:27 +0000911
epoger@google.com659c8c02013-05-21 15:45:45 +0000912 SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(), gRec.fName);
913 SkString nameWithExtension(shortNamePlusConfig);
914 nameWithExtension.append(".");
915 nameWithExtension.append(kPNG_FileExtension);
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000916
epoger@google.com659c8c02013-05-21 15:45:45 +0000917 ErrorCombination errors;
epoger@google.come33e1372013-07-08 19:13:33 +0000918
919 if (NULL == actualBitmapAndDigest) {
920 // Note that we intentionally skipped validating the results for
921 // this test, because we don't know how to generate an SkBitmap
922 // version of the output.
923 RecordTestResults(ErrorCombination(kIntentionallySkipped_ErrorType),
epoger@google.com659c8c02013-05-21 15:45:45 +0000924 shortNamePlusConfig, "");
epoger@google.comc824c832013-07-12 15:52:59 +0000925 } else if (!(gRec.fFlags & kWrite_ConfigFlag)) {
926 // We don't record the results for this test or compare them
927 // against any expectations, because the output image isn't
928 // meaningful.
929 // See https://code.google.com/p/skia/issues/detail?id=1410 ('some
930 // GM result images not available for download from Google Storage')
931 RecordTestResults(ErrorCombination(kIntentionallySkipped_ErrorType),
932 shortNamePlusConfig, "");
epoger@google.come33e1372013-07-08 19:13:33 +0000933 } else {
934 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
935 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
936 /*
937 * Get the expected results for this test, as one or more allowed
938 * hash digests. The current implementation of expectationsSource
939 * get this by computing the hash digest of a single PNG file on disk.
940 *
941 * TODO(epoger): This relies on the fact that
942 * force_all_opaque() was called on the bitmap before it
943 * was written to disk as a PNG in the first place. If
944 * not, the hash digest returned here may not match the
945 * hash digest of actualBitmap, which *has* been run through
946 * force_all_opaque().
947 * See comments above complete_bitmap() for more detail.
948 */
949 Expectations expectations = expectationsSource->get(nameWithExtension.c_str());
950 errors.add(compare_to_expectations(expectations, *actualBitmapAndDigest,
951 gm->shortName(), gRec.fName, "", true));
952 } else {
953 // If we are running without expectations, we still want to
954 // record the actual results.
955 add_actual_results_to_json_summary(nameWithExtension.c_str(),
956 actualBitmapAndDigest->fDigest,
957 ErrorCombination(kMissingExpectations_ErrorType),
958 false);
959 RecordTestResults(ErrorCombination(kMissingExpectations_ErrorType),
960 shortNamePlusConfig, "");
961 }
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000962 }
epoger@google.com37269602013-01-19 04:21:27 +0000963
964 // TODO: Consider moving this into compare_to_expectations(),
965 // similar to fMismatchPath... for now, we don't do that, because
966 // we don't want to write out the actual bitmaps for all
967 // renderModes of all tests! That would be a lot of files.
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000968 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000969 errors.add(write_reference_image(gRec, writePath, "", gm->shortName(),
epoger@google.come33e1372013-07-08 19:13:33 +0000970 actualBitmapAndDigest, document));
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000971 }
epoger@google.com37269602013-01-19 04:21:27 +0000972
epoger@google.com6f6568b2013-03-22 17:29:46 +0000973 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000974 }
975
epoger@google.com37269602013-01-19 04:21:27 +0000976 /**
977 * Compare actualBitmap to referenceBitmap.
978 *
epoger@google.com659c8c02013-05-21 15:45:45 +0000979 * @param shortName test name, e.g. "selftest1"
980 * @param configName configuration name, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000981 * @param renderModeDescriptor
982 * @param actualBitmap actual bitmap generated by this run
983 * @param referenceBitmap bitmap we expected to be generated
984 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000985 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +0000986 const char *shortName, const char *configName, const char *renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000987 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
988
989 SkASSERT(referenceBitmap);
epoger@google.com84a18022013-02-01 20:39:15 +0000990 Expectations expectations(*referenceBitmap);
epoger@google.com6f7f14d2013-06-19 18:28:31 +0000991 BitmapAndDigest actualBitmapAndDigest(actualBitmap);
992 return compare_to_expectations(expectations, actualBitmapAndDigest, shortName,
epoger@google.com659c8c02013-05-21 15:45:45 +0000993 configName, renderModeDescriptor, false);
epoger@google.com37269602013-01-19 04:21:27 +0000994 }
995
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000996 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
997 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +0000998 // Pictures are refcounted so must be on heap
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000999 SkPicture* pict;
junov@chromium.org706ff2f2012-12-19 15:55:40 +00001000 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
1001 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +00001002
junov@chromium.org3cb834b2012-12-13 16:39:53 +00001003 if (kTileGrid_BbhType == bbhType) {
junov@chromium.org29b19e52013-02-27 18:35:16 +00001004 SkTileGridPicture::TileGridInfo info;
1005 info.fMargin.setEmpty();
1006 info.fOffset.setZero();
1007 info.fTileInterval.set(16, 16);
1008 pict = new SkTileGridPicture(width, height, info);
junov@chromium.org3cb834b2012-12-13 16:39:53 +00001009 } else {
1010 pict = new SkPicture;
1011 }
junov@chromium.org20bd04e2013-01-16 18:43:36 +00001012 if (kNone_BbhType != bbhType) {
1013 recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
1014 }
junov@chromium.org706ff2f2012-12-19 15:55:40 +00001015 SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +00001016 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +00001017 invokeGM(gm, cv, false, false);
epoger@google.comde961632012-10-26 18:56:36 +00001018 pict->endRecording();
1019
1020 return pict;
1021 }
1022
1023 static SkPicture* stream_to_new_picture(const SkPicture& src) {
epoger@google.comde961632012-10-26 18:56:36 +00001024 SkDynamicMemoryWStream storage;
1025 src.serialize(&storage);
bungeman@google.comc29f3d82013-07-19 22:32:11 +00001026 SkAutoTUnref<SkStreamAsset> pictReadback(storage.detachAsStream());
bungeman@google.com88682b72013-07-19 13:55:41 +00001027 SkPicture* retval = SkPicture::CreateFromStream(pictReadback);
epoger@google.comde961632012-10-26 18:56:36 +00001028 return retval;
1029 }
1030
1031 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +00001032 // Depending on flags, possibly compare to an expected image.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001033 ErrorCombination test_drawing(GM* gm,
1034 const ConfigData& gRec,
1035 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001036 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +00001037 SkBitmap* bitmap) {
epoger@google.comde961632012-10-26 18:56:36 +00001038 SkDynamicMemoryWStream document;
1039
1040 if (gRec.fBackend == kRaster_Backend ||
1041 gRec.fBackend == kGPU_Backend) {
1042 // Early exit if we can't generate the image.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001043 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001044 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +00001045 // TODO: Add a test to exercise what the stdout and
1046 // JSON look like if we get an "early error" while
1047 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +00001048 return errors;
1049 }
1050 } else if (gRec.fBackend == kPDF_Backend) {
1051 generate_pdf(gm, document);
1052#if CAN_IMAGE_PDF
1053 SkAutoDataUnref data(document.copyToData());
1054 SkMemoryStream stream(data->data(), data->size());
1055 SkPDFDocumentToBitmap(&stream, bitmap);
epoger@google.come33e1372013-07-08 19:13:33 +00001056#else
1057 bitmap = NULL; // we don't generate a bitmap rendering of the PDF file
epoger@google.comde961632012-10-26 18:56:36 +00001058#endif
1059 } else if (gRec.fBackend == kXPS_Backend) {
1060 generate_xps(gm, document);
epoger@google.come33e1372013-07-08 19:13:33 +00001061 bitmap = NULL; // we don't generate a bitmap rendering of the XPS file
epoger@google.comde961632012-10-26 18:56:36 +00001062 }
epoger@google.come33e1372013-07-08 19:13:33 +00001063
bungeman@google.comc29f3d82013-07-19 22:32:11 +00001064 SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream());
epoger@google.come33e1372013-07-08 19:13:33 +00001065 if (NULL == bitmap) {
1066 return compare_test_results_to_stored_expectations(
bungeman@google.com88682b72013-07-19 13:55:41 +00001067 gm, gRec, writePath, NULL, documentStream);
epoger@google.come33e1372013-07-08 19:13:33 +00001068 } else {
1069 BitmapAndDigest bitmapAndDigest(*bitmap);
1070 return compare_test_results_to_stored_expectations(
bungeman@google.com88682b72013-07-19 13:55:41 +00001071 gm, gRec, writePath, &bitmapAndDigest, documentStream);
epoger@google.come33e1372013-07-08 19:13:33 +00001072 }
epoger@google.comde961632012-10-26 18:56:36 +00001073 }
1074
epoger@google.com6f6568b2013-03-22 17:29:46 +00001075 ErrorCombination test_deferred_drawing(GM* gm,
1076 const ConfigData& gRec,
1077 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001078 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +00001079 if (gRec.fBackend == kRaster_Backend ||
1080 gRec.fBackend == kGPU_Backend) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001081 const char renderModeDescriptor[] = "-deferred";
epoger@google.comde961632012-10-26 18:56:36 +00001082 SkBitmap bitmap;
1083 // Early exit if we can't generate the image, but this is
1084 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001085 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001086 // TODO(epoger): This logic is the opposite of what is
1087 // described above... if we succeeded in generating the
1088 // -deferred image, we exit early! We should fix this
1089 // ASAP, because it is hiding -deferred errors... but for
1090 // now, I'm leaving the logic as it is so that the
1091 // refactoring change
1092 // https://codereview.chromium.org/12992003/ is unblocked.
1093 //
1094 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
1095 // ('image-surface gm test is failing in "deferred" mode,
1096 // and gm is not reporting the failure')
1097 if (errors.isEmpty()) {
epoger@google.com310478e2013-04-03 18:00:39 +00001098 // TODO(epoger): Report this as a new ErrorType,
1099 // something like kImageGeneration_ErrorType?
epoger@google.com6f6568b2013-03-22 17:29:46 +00001100 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001101 }
epoger@google.com37269602013-01-19 04:21:27 +00001102 return compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001103 gm->shortName(), gRec.fName, renderModeDescriptor, bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001104 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001105 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001106 }
1107
epoger@google.comcaac3db2013-04-04 19:23:11 +00001108 ErrorCombination test_pipe_playback(GM* gm, const ConfigData& gRec,
1109 const SkBitmap& referenceBitmap, bool simulateFailure) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001110 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(),
1111 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001112 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001113 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001114 SkString renderModeDescriptor("-pipe");
1115 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1116
1117 if (gm->getFlags() & GM::kSkipPipe_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001118 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001119 renderModeDescriptor.c_str());
1120 errors.add(kIntentionallySkipped_ErrorType);
1121 } else {
1122 SkBitmap bitmap;
1123 SkISize size = gm->getISize();
1124 setup_bitmap(gRec, size, &bitmap);
1125 SkCanvas canvas(bitmap);
1126 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001127 // Pass a decoding function so the factory GM (which has an SkBitmap
1128 // with encoded data) will not fail playback.
1129 PipeController pipeController(&canvas, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001130 SkGPipeWriter writer;
1131 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001132 gPipeWritingFlagCombos[i].flags,
1133 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001134 if (!simulateFailure) {
1135 invokeGM(gm, pipeCanvas, false, false);
1136 }
1137 complete_bitmap(&bitmap);
1138 writer.endRecording();
1139 errors.add(compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001140 gm->shortName(), gRec.fName, renderModeDescriptor.c_str(), bitmap,
1141 &referenceBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001142 if (!errors.isEmpty()) {
1143 break;
1144 }
epoger@google.comde961632012-10-26 18:56:36 +00001145 }
1146 }
1147 return errors;
1148 }
1149
epoger@google.com6f6568b2013-03-22 17:29:46 +00001150 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
1151 const SkBitmap& referenceBitmap) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001152 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(),
1153 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001154 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001155 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001156 SkString renderModeDescriptor("-tiled pipe");
1157 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1158
1159 if ((gm->getFlags() & GM::kSkipPipe_Flag) ||
1160 (gm->getFlags() & GM::kSkipTiled_Flag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001161 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001162 renderModeDescriptor.c_str());
1163 errors.add(kIntentionallySkipped_ErrorType);
1164 } else {
1165 SkBitmap bitmap;
1166 SkISize size = gm->getISize();
1167 setup_bitmap(gRec, size, &bitmap);
1168 SkCanvas canvas(bitmap);
1169 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001170 TiledPipeController pipeController(bitmap, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001171 SkGPipeWriter writer;
1172 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001173 gPipeWritingFlagCombos[i].flags,
1174 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001175 invokeGM(gm, pipeCanvas, false, false);
1176 complete_bitmap(&bitmap);
1177 writer.endRecording();
epoger@google.com659c8c02013-05-21 15:45:45 +00001178 errors.add(compare_test_results_to_reference_bitmap(gm->shortName(), gRec.fName,
epoger@google.comc8263e72013-04-10 12:17:34 +00001179 renderModeDescriptor.c_str(),
1180 bitmap, &referenceBitmap));
1181 if (!errors.isEmpty()) {
1182 break;
1183 }
epoger@google.comde961632012-10-26 18:56:36 +00001184 }
1185 }
1186 return errors;
1187 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00001188
1189 //
1190 // member variables.
1191 // They are public for now, to allow easier setting by tool_main().
1192 //
1193
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001194 bool fUseFileHierarchy, fWriteChecksumBasedFilenames;
epoger@google.com5079d2c2013-04-12 14:11:21 +00001195 ErrorCombination fIgnorableErrorTypes;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001196
junov@chromium.org95146eb2013-01-11 21:04:40 +00001197 const char* fMismatchPath;
epoger@google.com5f995452013-06-21 18:16:47 +00001198 const char* fMissingExpectationsPath;
junov@chromium.org95146eb2013-01-11 21:04:40 +00001199
epoger@google.com310478e2013-04-03 18:00:39 +00001200 // collection of tests that have failed with each ErrorType
1201 SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
1202 int fTestsRun;
1203 SkTDict<int> fRenderModesEncountered;
epoger@google.com57f7abc2012-11-13 03:41:55 +00001204
epoger@google.com908f5832013-04-12 02:23:55 +00001205 // Where to read expectations (expected image hash digests, etc.) from.
epoger@google.com37269602013-01-19 04:21:27 +00001206 // If unset, we don't do comparisons.
1207 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
1208
1209 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +00001210 Json::Value fJsonExpectedResults;
1211 Json::Value fJsonActualResults_Failed;
1212 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001213 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001214 Json::Value fJsonActualResults_Succeeded;
1215
epoger@google.comde961632012-10-26 18:56:36 +00001216}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +00001217
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001218#if SK_SUPPORT_GPU
1219static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1220#else
1221static const GLContextType kDontCare_GLContextType = 0;
1222#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +00001223
1224// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +00001225// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +00001226static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
1227 kWrite_ConfigFlag;
1228
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001229static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001230 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001231#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 +00001232 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001233#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001234 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
1235#if SK_SUPPORT_GPU
1236 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
robertphillips@google.com7bb28a12013-06-06 21:29:39 +00001237 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", false},
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001238 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
epoger@google.comc824c832013-07-12 15:52:59 +00001239 /* The gpudebug context does not generate meaningful images, so don't record
1240 * the images it generates! We only run it to look for asserts. */
scroggo@google.com0f567c62013-03-20 15:35:08 +00001241 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", GR_DEBUG},
robertphillips@google.comd6543e52013-07-18 17:39:14 +00001242 /* The gpunull context does the least amount of work possible and doesn't
1243 generate meaninful images, so don't record them!. It can be run to
1244 isolate the CPU-side processing expense from the GPU-side.
1245 */
1246 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNull_GLContextType, 0, kNone_ConfigFlag, "gpunull", GR_DEBUG},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001247#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001248 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
1249 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001250#endif // SK_ANGLE
1251#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001252 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001253#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001254#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +00001255#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001256 /* 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 +00001257 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +00001258#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001259#ifdef SK_SUPPORT_PDF
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001260 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001261#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001262};
1263
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001264static const char kDefaultsConfigStr[] = "defaults";
1265static const char kExcludeConfigChar = '~';
1266
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001267static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001268 SkString result;
1269 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001270 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001271 SkASSERT(gRec[i].fName != kDefaultsConfigStr);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001272 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001273 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001274 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001275 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001276 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001277 result.append("]\n");
1278 result.appendf("The default value is: \"");
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001279 SkString firstDefault;
1280 SkString allButFirstDefaults;
1281 SkString nonDefault;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001282 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1283 if (gRec[i].fRunByDefault) {
1284 if (i > 0) {
1285 result.append(" ");
1286 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001287 result.append(gRec[i].fName);
1288 if (firstDefault.isEmpty()) {
1289 firstDefault = gRec[i].fName;
1290 } else {
1291 if (!allButFirstDefaults.isEmpty()) {
1292 allButFirstDefaults.append(" ");
1293 }
1294 allButFirstDefaults.append(gRec[i].fName);
1295 }
1296 } else {
1297 nonDefault = gRec[i].fName;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001298 }
1299 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001300 result.append("\"\n");
1301 result.appendf("\"%s\" evaluates to the default set of configs.\n", kDefaultsConfigStr);
1302 result.appendf("Prepending \"%c\" on a config name excludes it from the set of configs to run.\n"
1303 "Exclusions always override inclusions regardless of order.\n",
1304 kExcludeConfigChar);
1305 result.appendf("E.g. \"--config %s %c%s %s\" will run these configs:\n\t%s %s",
1306 kDefaultsConfigStr,
1307 kExcludeConfigChar,
1308 firstDefault.c_str(),
1309 nonDefault.c_str(),
1310 allButFirstDefaults.c_str(),
1311 nonDefault.c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001312 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001313}
scroggo@google.com7d519302013-03-19 17:28:10 +00001314
epoger@google.com6f6568b2013-03-22 17:29:46 +00001315// Macro magic to convert a numeric preprocessor token into a string.
1316// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1317// This should probably be moved into one of our common headers...
1318#define TOSTRING_INTERNAL(x) #x
1319#define TOSTRING(x) TOSTRING_INTERNAL(x)
1320
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001321// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001322DEFINE_string(config, "", configUsage().c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001323DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001324DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1325DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1326#if SK_SUPPORT_GPU
1327DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001328 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1329 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001330#endif
1331DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1332 "when reading/writing files.");
epoger@google.com5079d2c2013-04-12 14:11:21 +00001333DEFINE_string(ignoreErrorTypes, kDefaultIgnorableErrorTypes.asString(" ").c_str(),
1334 "Space-separated list of ErrorTypes that should be ignored. If any *other* error "
1335 "types are encountered, the tool will exit with a nonzero return value.");
caryclark@google.com512c9b62013-05-10 15:16:13 +00001336DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
1337 "Multiple matches may be separated by spaces.\n"
1338 "~ causes a matching test to always be skipped\n"
1339 "^ requires the start of the test to match\n"
1340 "$ requires the end of the test to match\n"
1341 "^ and $ requires an exact match\n"
1342 "If a test does not match any list entry,\n"
1343 "it is skipped unless some list entry starts with ~");
epoger@google.com5f995452013-06-21 18:16:47 +00001344DEFINE_string(missingExpectationsPath, "", "Write images for tests without expectations "
1345 "into this directory.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001346DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1347 "pixel mismatches into this directory.");
1348DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1349 "testIndex %% divisor == remainder.");
1350DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1351DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1352DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1353 "any differences between those and the newly generated ones.");
1354DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1355DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1356DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1357DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
epoger@google.comcaac3db2013-04-04 19:23:11 +00001358DEFINE_bool(simulatePipePlaybackFailure, false, "Simulate a rendering failure in pipe mode only.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001359DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1360DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1361DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1362 "factors to be used for tileGrid playback testing. Default value: 1.0");
epoger@google.comb0f8b432013-04-10 18:46:25 +00001363DEFINE_bool2(verbose, v, false, "Give more detail (e.g. list all GMs run, more info about "
1364 "each test).");
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001365DEFINE_bool(writeChecksumBasedFilenames, false, "When writing out actual images, use checksum-"
1366 "based filenames, as rebaseline.py will use when downloading them from Google Storage");
1367DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001368DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
scroggo@google.com604e0c22013-04-09 21:25:46 +00001369DEFINE_string2(writePicturePath, p, "", "Write .skp files into this directory.");
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001370DEFINE_int32(pdfJpegQuality, -1, "Encodes images in JPEG at quality level N, "
1371 "which can be in range 0-100). N = -1 will disable JPEG compression. "
1372 "Default is N = 100, maximum quality.");
1373
1374static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect) {
1375 // Filter output of warnings that JPEG is not available for the image.
1376 if (bitmap.width() >= 65500 || bitmap.height() >= 65500) return false;
1377 if (FLAGS_pdfJpegQuality == -1) return false;
1378
1379 SkIRect bitmapBounds;
1380 SkBitmap subset;
1381 const SkBitmap* bitmapToUse = &bitmap;
1382 bitmap.getBounds(&bitmapBounds);
1383 if (rect != bitmapBounds) {
1384 SkAssertResult(bitmap.extractSubset(&subset, rect));
1385 bitmapToUse = &subset;
1386 }
1387
1388#if defined(SK_BUILD_FOR_MAC)
1389 // Workaround bug #1043 where bitmaps with referenced pixels cause
1390 // CGImageDestinationFinalize to crash
1391 SkBitmap copy;
1392 bitmapToUse->deepCopyTo(&copy, bitmapToUse->config());
1393 bitmapToUse = &copy;
1394#endif
1395
1396 return SkImageEncoder::EncodeStream(stream,
1397 *bitmapToUse,
1398 SkImageEncoder::kJPEG_Type,
1399 FLAGS_pdfJpegQuality);
1400}
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001401
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001402static int findConfig(const char config[]) {
1403 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1404 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001405 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001406 }
1407 }
1408 return -1;
1409}
1410
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001411namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001412#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +00001413SkAutoTUnref<GrContext> gGrContext;
1414/**
bsalomon@google.comc7a24d22012-11-01 18:03:48 +00001415 * Sets the global GrContext, accessible by individual GMs
bsalomon@google.com7361f542012-04-19 19:15:35 +00001416 */
caryclark@google.com13130862012-06-06 12:10:45 +00001417static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +00001418 SkSafeRef(grContext);
1419 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001420}
bsalomon@google.com7361f542012-04-19 19:15:35 +00001421
1422/**
1423 * Gets the global GrContext, can be called by GM tests.
1424 */
caryclark@google.com13130862012-06-06 12:10:45 +00001425GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +00001426GrContext* GetGr() {
1427 return gGrContext.get();
1428}
1429
1430/**
1431 * Sets the global GrContext and then resets it to its previous value at
1432 * destruction.
1433 */
1434class AutoResetGr : SkNoncopyable {
1435public:
1436 AutoResetGr() : fOld(NULL) {}
1437 void set(GrContext* context) {
1438 SkASSERT(NULL == fOld);
1439 fOld = GetGr();
1440 SkSafeRef(fOld);
1441 SetGr(context);
1442 }
1443 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1444private:
1445 GrContext* fOld;
1446};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001447#else
epoger@google.com80724df2013-03-21 13:49:54 +00001448GrContext* GetGr();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001449GrContext* GetGr() { return NULL; }
1450#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001451}
1452
reed@google.comfb2cd422013-01-04 14:43:03 +00001453template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1454 int index = array->find(value);
1455 if (index < 0) {
1456 *array->append() = value;
1457 }
1458}
1459
epoger@google.com80724df2013-03-21 13:49:54 +00001460/**
1461 * Run this test in a number of different configs (8888, 565, PDF,
1462 * etc.), confirming that the resulting bitmaps match expectations
1463 * (which may be different for each config).
1464 *
1465 * Returns all errors encountered while doing so.
1466 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001467ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1468 GrContextFactory *grFactory);
1469ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1470 GrContextFactory *grFactory) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001471 const char renderModeDescriptor[] = "";
epoger@google.com6f6568b2013-03-22 17:29:46 +00001472 ErrorCombination errorsForAllConfigs;
epoger@google.com80724df2013-03-21 13:49:54 +00001473 uint32_t gmFlags = gm->getFlags();
1474
epoger@google.com80724df2013-03-21 13:49:54 +00001475 for (int i = 0; i < configs.count(); i++) {
1476 ConfigData config = gRec[configs[i]];
epoger@google.com659c8c02013-05-21 15:45:45 +00001477 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->shortName(),
1478 config.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001479
1480 // Skip any tests that we don't even need to try.
epoger@google.comc8263e72013-04-10 12:17:34 +00001481 // If any of these were skipped on a per-GM basis, record them as
1482 // kIntentionallySkipped.
1483 if (kPDF_Backend == config.fBackend) {
1484 if (!FLAGS_pdf) {
epoger@google.com80724df2013-03-21 13:49:54 +00001485 continue;
1486 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001487 if (gmFlags & GM::kSkipPDF_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001488 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001489 renderModeDescriptor);
1490 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1491 continue;
1492 }
1493 }
epoger@google.com80724df2013-03-21 13:49:54 +00001494 if ((gmFlags & GM::kSkip565_Flag) &&
1495 (kRaster_Backend == config.fBackend) &&
1496 (SkBitmap::kRGB_565_Config == config.fConfig)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001497 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001498 renderModeDescriptor);
1499 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001500 continue;
1501 }
1502 if ((gmFlags & GM::kSkipGPU_Flag) &&
1503 kGPU_Backend == config.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001504 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001505 renderModeDescriptor);
1506 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001507 continue;
1508 }
1509
1510 // Now we know that we want to run this test and record its
1511 // success or failure.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001512 ErrorCombination errorsForThisConfig;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001513 GrSurface* gpuTarget = NULL;
epoger@google.com80724df2013-03-21 13:49:54 +00001514#if SK_SUPPORT_GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001515 SkAutoTUnref<GrSurface> auGpuTarget;
epoger@google.com80724df2013-03-21 13:49:54 +00001516 AutoResetGr autogr;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001517 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
epoger@google.com80724df2013-03-21 13:49:54 +00001518 GrContext* gr = grFactory->get(config.fGLContextType);
1519 bool grSuccess = false;
1520 if (gr) {
1521 // create a render target to back the device
1522 GrTextureDesc desc;
1523 desc.fConfig = kSkia8888_GrPixelConfig;
1524 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1525 desc.fWidth = gm->getISize().width();
1526 desc.fHeight = gm->getISize().height();
1527 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001528 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1529 if (NULL != auGpuTarget) {
1530 gpuTarget = auGpuTarget;
1531 grSuccess = true;
epoger@google.com80724df2013-03-21 13:49:54 +00001532 autogr.set(gr);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001533 // Set the user specified cache limits if non-default.
1534 size_t bytes;
1535 int count;
1536 gr->getTextureCacheLimits(&count, &bytes);
1537 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1538 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1539 }
1540 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1541 count = gGpuCacheSizeCount;
1542 }
1543 gr->setTextureCacheLimits(count, bytes);
epoger@google.com80724df2013-03-21 13:49:54 +00001544 }
epoger@google.com80724df2013-03-21 13:49:54 +00001545 }
1546 if (!grSuccess) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001547 errorsForThisConfig.add(kNoGpuContext_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001548 }
1549 }
1550#endif
1551
1552 SkBitmap comparisonBitmap;
1553
1554 const char* writePath;
1555 if (FLAGS_writePath.count() == 1) {
1556 writePath = FLAGS_writePath[0];
1557 } else {
1558 writePath = NULL;
1559 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001560 if (errorsForThisConfig.isEmpty()) {
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001561 errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
1562 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001563 }
1564
epoger@google.com6f6568b2013-03-22 17:29:46 +00001565 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1566 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1567 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001568 gpuTarget));
epoger@google.com80724df2013-03-21 13:49:54 +00001569 }
1570
epoger@google.com6f6568b2013-03-22 17:29:46 +00001571 errorsForAllConfigs.add(errorsForThisConfig);
epoger@google.com80724df2013-03-21 13:49:54 +00001572 }
1573 return errorsForAllConfigs;
1574}
1575
1576/**
1577 * Run this test in a number of different drawing modes (pipe,
1578 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1579 * *exactly* match comparisonBitmap.
1580 *
1581 * Returns all errors encountered while doing so.
1582 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001583ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1584 const SkBitmap &comparisonBitmap,
1585 const SkTDArray<SkScalar> &tileGridReplayScales);
1586ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1587 const SkBitmap &comparisonBitmap,
1588 const SkTDArray<SkScalar> &tileGridReplayScales) {
1589 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001590 uint32_t gmFlags = gm->getFlags();
epoger@google.com659c8c02013-05-21 15:45:45 +00001591 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->shortName(),
1592 compareConfig.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001593
epoger@google.comc8263e72013-04-10 12:17:34 +00001594 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1595 SkAutoUnref aur(pict);
1596 if (FLAGS_replay) {
1597 const char renderModeDescriptor[] = "-replay";
1598 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001599 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1600 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001601 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1602 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001603 SkBitmap bitmap;
1604 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001605 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001606 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1607 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001608 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001609 }
epoger@google.com80724df2013-03-21 13:49:54 +00001610
epoger@google.comc8263e72013-04-10 12:17:34 +00001611 if (FLAGS_serialize) {
1612 const char renderModeDescriptor[] = "-serialize";
1613 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001614 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1615 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001616 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1617 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001618 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1619 SkAutoUnref aurr(repict);
epoger@google.com80724df2013-03-21 13:49:54 +00001620 SkBitmap bitmap;
1621 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001622 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001623 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1624 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001625 }
epoger@google.com80724df2013-03-21 13:49:54 +00001626 }
1627
epoger@google.comc8263e72013-04-10 12:17:34 +00001628 if ((1 == FLAGS_writePicturePath.count()) &&
1629 !(gmFlags & GM::kSkipPicture_Flag)) {
1630 const char* pictureSuffix = "skp";
epoger@google.com659c8c02013-05-21 15:45:45 +00001631 // TODO(epoger): Make sure this still works even though the
1632 // filename now contains the config name (it used to contain
1633 // just the shortName). I think this is actually an
1634 // *improvement*, because now runs with different configs will
1635 // write out their SkPictures to separate files rather than
1636 // overwriting each other. But we should make sure it doesn't
1637 // break anybody.
1638 SkString path = gmmain.make_filename(FLAGS_writePicturePath[0], gm->shortName(),
1639 compareConfig.fName, "", pictureSuffix);
epoger@google.comc8263e72013-04-10 12:17:34 +00001640 SkFILEWStream stream(path.c_str());
1641 pict->serialize(&stream);
epoger@google.com80724df2013-03-21 13:49:54 +00001642 }
1643
epoger@google.comc8263e72013-04-10 12:17:34 +00001644 if (FLAGS_rtree) {
1645 const char renderModeDescriptor[] = "-rtree";
1646 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001647 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1648 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001649 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1650 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001651 SkPicture* pict = gmmain.generate_new_picture(
epoger@google.comc8263e72013-04-10 12:17:34 +00001652 gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
epoger@google.com80724df2013-03-21 13:49:54 +00001653 SkAutoUnref aur(pict);
1654 SkBitmap bitmap;
epoger@google.comc8263e72013-04-10 12:17:34 +00001655 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001656 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001657 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1658 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001659 }
1660 }
1661
1662 if (FLAGS_tileGrid) {
1663 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1664 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1665 SkString renderModeDescriptor("-tilegrid");
1666 if (SK_Scalar1 != replayScale) {
1667 renderModeDescriptor += "-scale-";
1668 renderModeDescriptor.appendScalar(replayScale);
1669 }
1670
1671 if ((gmFlags & GM::kSkipPicture_Flag) ||
1672 ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001673 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001674 renderModeDescriptor.c_str());
1675 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1676 } else {
1677 // We record with the reciprocal scale to obtain a replay
1678 // result that can be validated against comparisonBitmap.
1679 SkScalar recordScale = SkScalarInvert(replayScale);
1680 SkPicture* pict = gmmain.generate_new_picture(
1681 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag,
1682 recordScale);
1683 SkAutoUnref aur(pict);
1684 SkBitmap bitmap;
1685 // We cannot yet pass 'true' to generate_image_from_picture to
1686 // perform actual tiled rendering (see Issue 1198 -
1687 // https://code.google.com/p/skia/issues/detail?id=1198)
1688 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
1689 replayScale /*, true */);
1690 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001691 gm->shortName(), compareConfig.fName, renderModeDescriptor.c_str(), bitmap,
1692 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001693 }
epoger@google.com80724df2013-03-21 13:49:54 +00001694 }
1695 }
1696
1697 // run the pipe centric GM steps
epoger@google.comc8263e72013-04-10 12:17:34 +00001698 if (FLAGS_pipe) {
1699 errorsForAllModes.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap,
1700 FLAGS_simulatePipePlaybackFailure));
1701 if (FLAGS_tiledPipe) {
1702 errorsForAllModes.add(gmmain.test_tiled_pipe_playback(gm, compareConfig,
1703 comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001704 }
epoger@google.com80724df2013-03-21 13:49:54 +00001705 }
1706 return errorsForAllModes;
1707}
1708
epoger@google.com310478e2013-04-03 18:00:39 +00001709/**
1710 * Return a list of all entries in an array of strings as a single string
1711 * of this form:
1712 * "item1", "item2", "item3"
1713 */
1714SkString list_all(const SkTArray<SkString> &stringArray);
1715SkString list_all(const SkTArray<SkString> &stringArray) {
1716 SkString total;
1717 for (int i = 0; i < stringArray.count(); i++) {
1718 if (i > 0) {
1719 total.append(", ");
1720 }
1721 total.append("\"");
1722 total.append(stringArray[i]);
1723 total.append("\"");
1724 }
1725 return total;
1726}
1727
1728/**
1729 * Return a list of configuration names, as a single string of this form:
1730 * "item1", "item2", "item3"
1731 *
1732 * @param configs configurations, as a list of indices into gRec
1733 */
1734SkString list_all_config_names(const SkTDArray<size_t> &configs);
1735SkString list_all_config_names(const SkTDArray<size_t> &configs) {
1736 SkString total;
1737 for (int i = 0; i < configs.count(); i++) {
1738 if (i > 0) {
1739 total.append(", ");
1740 }
1741 total.append("\"");
1742 total.append(gRec[configs[i]].fName);
1743 total.append("\"");
1744 }
1745 return total;
1746}
1747
epoger@google.comfdea3252013-05-02 18:24:03 +00001748bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1749 const SkTDArray<size_t> &configs);
1750bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1751 const SkTDArray<size_t> &configs) {
1752 if (!sk_mkdir(root)) {
1753 return false;
1754 }
1755 if (useFileHierarchy) {
1756 for (int i = 0; i < configs.count(); i++) {
1757 ConfigData config = gRec[configs[i]];
1758 SkString subdir;
1759 subdir.appendf("%s%c%s", root, SkPATH_SEPARATOR, config.fName);
1760 if (!sk_mkdir(subdir.c_str())) {
1761 return false;
1762 }
1763 }
1764 }
1765 return true;
1766}
1767
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001768int tool_main(int argc, char** argv);
1769int tool_main(int argc, char** argv) {
1770
1771#if SK_ENABLE_INST_COUNT
1772 gPrintInstCount = true;
1773#endif
1774
1775 SkGraphics::Init();
1776 // we don't need to see this during a run
1777 gSkSuppressFontCachePurgeSpew = true;
1778
1779 setSystemPreferences();
1780 GMMain gmmain;
1781
1782 SkTDArray<size_t> configs;
1783 SkTDArray<size_t> excludeConfigs;
1784 bool userConfig = false;
1785
1786 SkString usage;
1787 usage.printf("Run the golden master tests.\n");
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00001788 SkCommandLineFlags::SetUsage(usage.c_str());
1789 SkCommandLineFlags::Parse(argc, argv);
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001790
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001791 gmmain.fUseFileHierarchy = FLAGS_hierarchy;
epoger@google.com6f7f14d2013-06-19 18:28:31 +00001792 gmmain.fWriteChecksumBasedFilenames = FLAGS_writeChecksumBasedFilenames;
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001793 if (FLAGS_mismatchPath.count() == 1) {
1794 gmmain.fMismatchPath = FLAGS_mismatchPath[0];
1795 }
epoger@google.com5f995452013-06-21 18:16:47 +00001796 if (FLAGS_missingExpectationsPath.count() == 1) {
1797 gmmain.fMissingExpectationsPath = FLAGS_missingExpectationsPath[0];
1798 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001799
1800 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001801 const char* config = FLAGS_config[i];
1802 userConfig = true;
1803 bool exclude = false;
1804 if (*config == kExcludeConfigChar) {
1805 exclude = true;
1806 config += 1;
1807 }
1808 int index = findConfig(config);
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001809 if (index >= 0) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001810 if (exclude) {
1811 *excludeConfigs.append() = index;
1812 } else {
1813 appendUnique<size_t>(&configs, index);
1814 }
1815 } else if (0 == strcmp(kDefaultsConfigStr, config)) {
1816 for (size_t c = 0; c < SK_ARRAY_COUNT(gRec); ++c) {
1817 if (gRec[c].fRunByDefault) {
1818 if (exclude) {
1819 gm_fprintf(stderr, "%c%s is not allowed.\n",
1820 kExcludeConfigChar, kDefaultsConfigStr);
1821 return -1;
1822 } else {
1823 appendUnique<size_t>(&configs, c);
1824 }
1825 }
1826 }
scroggo@google.com0b735632013-03-19 17:38:50 +00001827 } else {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001828 gm_fprintf(stderr, "unrecognized config %s\n", config);
scroggo@google.com7d519302013-03-19 17:28:10 +00001829 return -1;
1830 }
1831 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001832
1833 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1834 int index = findConfig(FLAGS_excludeConfig[i]);
1835 if (index >= 0) {
1836 *excludeConfigs.append() = index;
1837 } else {
1838 gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1839 return -1;
1840 }
1841 }
1842
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001843 int moduloRemainder = -1;
1844 int moduloDivisor = -1;
1845
1846 if (FLAGS_modulo.count() == 2) {
1847 moduloRemainder = atoi(FLAGS_modulo[0]);
1848 moduloDivisor = atoi(FLAGS_modulo[1]);
1849 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
1850 gm_fprintf(stderr, "invalid modulo values.");
1851 return -1;
1852 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001853 }
reed@google.com873cb1e2010-12-23 15:00:45 +00001854
epoger@google.com5079d2c2013-04-12 14:11:21 +00001855 if (FLAGS_ignoreErrorTypes.count() > 0) {
1856 gmmain.fIgnorableErrorTypes = ErrorCombination();
1857 for (int i = 0; i < FLAGS_ignoreErrorTypes.count(); i++) {
1858 ErrorType type;
1859 const char *name = FLAGS_ignoreErrorTypes[i];
1860 if (!getErrorTypeByName(name, &type)) {
1861 gm_fprintf(stderr, "cannot find ErrorType with name '%s'\n", name);
1862 return -1;
1863 } else {
1864 gmmain.fIgnorableErrorTypes.add(type);
1865 }
1866 }
1867 }
1868
epoger@google.com6f6568b2013-03-22 17:29:46 +00001869#if SK_SUPPORT_GPU
1870 if (FLAGS_gpuCacheSize.count() > 0) {
1871 if (FLAGS_gpuCacheSize.count() != 2) {
1872 gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
1873 return -1;
1874 }
1875 gGpuCacheSizeBytes = atoi(FLAGS_gpuCacheSize[0]);
1876 gGpuCacheSizeCount = atoi(FLAGS_gpuCacheSize[1]);
1877 } else {
1878 gGpuCacheSizeBytes = DEFAULT_CACHE_VALUE;
1879 gGpuCacheSizeCount = DEFAULT_CACHE_VALUE;
1880 }
1881#endif
1882
1883 SkTDArray<SkScalar> tileGridReplayScales;
1884 *tileGridReplayScales.append() = SK_Scalar1; // By default only test at scale 1.0
1885 if (FLAGS_tileGridReplayScales.count() > 0) {
1886 tileGridReplayScales.reset();
1887 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
1888 double val = atof(FLAGS_tileGridReplayScales[i]);
1889 if (0 < val) {
1890 *tileGridReplayScales.append() = SkDoubleToScalar(val);
1891 }
1892 }
1893 if (0 == tileGridReplayScales.count()) {
1894 // Should have at least one scale
1895 gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
1896 return -1;
1897 }
1898 }
1899
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001900 if (!userConfig) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001901 // if no config is specified by user, add the defaults
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001902 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001903 if (gRec[i].fRunByDefault) {
1904 *configs.append() = i;
1905 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001906 }
1907 }
reed@google.comfb2cd422013-01-04 14:43:03 +00001908 // now remove any explicitly excluded configs
1909 for (int i = 0; i < excludeConfigs.count(); ++i) {
1910 int index = configs.find(excludeConfigs[i]);
1911 if (index >= 0) {
1912 configs.remove(index);
1913 // now assert that there was only one copy in configs[]
1914 SkASSERT(configs.find(excludeConfigs[i]) < 0);
1915 }
1916 }
1917
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001918#if SK_SUPPORT_GPU
1919 GrContextFactory* grFactory = new GrContextFactory;
1920 for (int i = 0; i < configs.count(); ++i) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001921 size_t index = configs[i];
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001922 if (kGPU_Backend == gRec[index].fBackend) {
1923 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1924 if (NULL == ctx) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001925 gm_fprintf(stderr, "GrContext could not be created for config %s."
1926 " Config will be skipped.\n", gRec[index].fName);
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001927 configs.remove(i);
1928 --i;
bsalomon@google.com42db2e42013-06-11 19:22:44 +00001929 continue;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001930 }
1931 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001932 gm_fprintf(stderr, "Sample count (%d) of config %s is not supported."
1933 " Config will be skipped.\n", gRec[index].fSampleCnt, gRec[index].fName);
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001934 configs.remove(i);
1935 --i;
1936 }
1937 }
1938 }
epoger@google.com80724df2013-03-21 13:49:54 +00001939#else
1940 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001941#endif
1942
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001943 if (configs.isEmpty()) {
1944 gm_fprintf(stderr, "No configs to run.");
1945 return -1;
1946 }
1947
1948 // now show the user the set of configs that will be run.
1949 SkString configStr("These configs will be run: ");
1950 // show the user the config that will run.
1951 for (int i = 0; i < configs.count(); ++i) {
1952 configStr.appendf("%s%s", gRec[configs[i]].fName, (i == configs.count() - 1) ? "\n" : " ");
1953 }
1954 gm_fprintf(stdout, "%s", configStr.c_str());
1955
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001956 if (FLAGS_resourcePath.count() == 1) {
1957 GM::SetResourcePath(FLAGS_resourcePath[0]);
1958 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001959
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001960 if (FLAGS_readPath.count() == 1) {
1961 const char* readPath = FLAGS_readPath[0];
epoger@google.com37269602013-01-19 04:21:27 +00001962 if (!sk_exists(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001963 gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001964 return -1;
1965 }
1966 if (sk_isdir(readPath)) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001967 if (FLAGS_verbose) {
1968 gm_fprintf(stdout, "reading from %s\n", readPath);
1969 }
epoger@google.com37269602013-01-19 04:21:27 +00001970 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
epoger@google.comb0f8b432013-04-10 18:46:25 +00001971 IndividualImageExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001972 } else {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001973 if (FLAGS_verbose) {
1974 gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
1975 }
epoger@google.comd271d242013-02-13 18:14:48 +00001976 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1977 JsonExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001978 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00001979 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00001980 if (FLAGS_verbose) {
1981 if (FLAGS_writePath.count() == 1) {
1982 gm_fprintf(stdout, "writing to %s\n", FLAGS_writePath[0]);
1983 }
epoger@google.comfdea3252013-05-02 18:24:03 +00001984 if (NULL != gmmain.fMismatchPath) {
1985 gm_fprintf(stdout, "writing mismatches to %s\n", gmmain.fMismatchPath);
1986 }
epoger@google.com5f995452013-06-21 18:16:47 +00001987 if (NULL != gmmain.fMissingExpectationsPath) {
1988 gm_fprintf(stdout, "writing images without expectations to %s\n",
1989 gmmain.fMissingExpectationsPath);
1990 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00001991 if (FLAGS_writePicturePath.count() == 1) {
1992 gm_fprintf(stdout, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
1993 }
1994 if (FLAGS_resourcePath.count() == 1) {
1995 gm_fprintf(stdout, "reading resources from %s\n", FLAGS_resourcePath[0]);
1996 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001997 }
1998
epoger@google.com82cb65b2012-10-29 18:59:17 +00001999 if (moduloDivisor <= 0) {
2000 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00002001 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00002002 if (moduloRemainder < 0 || moduloRemainder >= moduloDivisor) {
2003 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00002004 }
2005
epoger@google.com310478e2013-04-03 18:00:39 +00002006 int gmsRun = 0;
reed@google.comae7b8f32012-10-18 21:30:57 +00002007 int gmIndex = -1;
2008 SkString moduloStr;
2009
epoger@google.come8ebeb12012-10-29 16:42:11 +00002010 // If we will be writing out files, prepare subdirectories.
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002011 if (FLAGS_writePath.count() == 1) {
epoger@google.comfdea3252013-05-02 18:24:03 +00002012 if (!prepare_subdirectories(FLAGS_writePath[0], gmmain.fUseFileHierarchy, configs)) {
epoger@google.come8ebeb12012-10-29 16:42:11 +00002013 return -1;
2014 }
epoger@google.comfdea3252013-05-02 18:24:03 +00002015 }
2016 if (NULL != gmmain.fMismatchPath) {
2017 if (!prepare_subdirectories(gmmain.fMismatchPath, gmmain.fUseFileHierarchy, configs)) {
2018 return -1;
epoger@google.come8ebeb12012-10-29 16:42:11 +00002019 }
2020 }
epoger@google.com5f995452013-06-21 18:16:47 +00002021 if (NULL != gmmain.fMissingExpectationsPath) {
2022 if (!prepare_subdirectories(gmmain.fMissingExpectationsPath, gmmain.fUseFileHierarchy,
2023 configs)) {
2024 return -1;
2025 }
2026 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00002027
edisonn@google.comd9dfa182013-04-24 13:01:01 +00002028 if (FLAGS_pdfJpegQuality < -1 || FLAGS_pdfJpegQuality > 100) {
2029 gm_fprintf(stderr, "%s\n", "pdfJpegQuality must be in [-1 .. 100] range.");
2030 }
2031
bsalomon@google.com7361f542012-04-19 19:15:35 +00002032 Iter iter;
2033 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00002034 while ((gm = iter.next()) != NULL) {
scroggo@google.com7fd2d702013-04-16 19:11:14 +00002035 SkAutoTDelete<GM> adgm(gm);
reed@google.comae7b8f32012-10-18 21:30:57 +00002036 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00002037 if (moduloRemainder >= 0) {
2038 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00002039 continue;
2040 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00002041 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00002042 }
2043
reed@google.comece2b022011-07-25 14:28:57 +00002044 const char* shortName = gm->shortName();
sglez@google.com434251f2013-07-23 17:26:34 +00002045
2046 SkTDArray<const char*> matchStrs;
2047 for (int i = 0; i < FLAGS_match.count(); ++i) {
2048 matchStrs.push(FLAGS_match[i]);
2049 }
2050 if (SkCommandLineFlags::ShouldSkip(matchStrs, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00002051 continue;
2052 }
2053
epoger@google.com310478e2013-04-03 18:00:39 +00002054 gmsRun++;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00002055 SkISize size = gm->getISize();
epoger@google.combcbf5aa2013-04-12 02:11:54 +00002056 gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
2057 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00002058
epoger@google.com310478e2013-04-03 18:00:39 +00002059 run_multiple_configs(gmmain, gm, configs, grFactory);
djsollen@google.comebce16d2012-10-26 14:07:13 +00002060
2061 SkBitmap comparisonBitmap;
2062 const ConfigData compareConfig =
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002063 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
epoger@google.com310478e2013-04-03 18:00:39 +00002064 gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false);
djsollen@google.comebce16d2012-10-26 14:07:13 +00002065
epoger@google.com80724df2013-03-21 13:49:54 +00002066 // TODO(epoger): only run this if gmmain.generate_image() succeeded?
2067 // Otherwise, what are we comparing against?
epoger@google.com310478e2013-04-03 18:00:39 +00002068 run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap, tileGridReplayScales);
reed@android.com00dae862009-06-10 15:38:48 +00002069 }
epoger@google.com310478e2013-04-03 18:00:39 +00002070
2071 SkTArray<SkString> modes;
2072 gmmain.GetRenderModesEncountered(modes);
epoger@google.comc8263e72013-04-10 12:17:34 +00002073 bool reportError = false;
2074 if (gmmain.NumSignificantErrors() > 0) {
2075 reportError = true;
2076 }
epoger@google.com51dbabe2013-04-10 15:24:53 +00002077 int expectedNumberOfTests = gmsRun * (configs.count() + modes.count());
epoger@google.com310478e2013-04-03 18:00:39 +00002078
2079 // Output summary to stdout.
epoger@google.com51dbabe2013-04-10 15:24:53 +00002080 if (FLAGS_verbose) {
2081 gm_fprintf(stdout, "Ran %d GMs\n", gmsRun);
2082 gm_fprintf(stdout, "... over %2d configs [%s]\n", configs.count(),
2083 list_all_config_names(configs).c_str());
2084 gm_fprintf(stdout, "... and %2d modes [%s]\n", modes.count(), list_all(modes).c_str());
2085 gm_fprintf(stdout, "... so there should be a total of %d tests.\n", expectedNumberOfTests);
2086 }
2087 gmmain.ListErrors(FLAGS_verbose);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002088
epoger@google.com07947d92013-04-11 15:41:02 +00002089 // TODO(epoger): Enable this check for Android, too, once we resolve
2090 // https://code.google.com/p/skia/issues/detail?id=1222
2091 // ('GM is unexpectedly skipping tests on Android')
2092#ifndef SK_BUILD_FOR_ANDROID
epoger@google.comc8263e72013-04-10 12:17:34 +00002093 if (expectedNumberOfTests != gmmain.fTestsRun) {
2094 gm_fprintf(stderr, "expected %d tests, but ran or skipped %d tests\n",
2095 expectedNumberOfTests, gmmain.fTestsRun);
2096 reportError = true;
2097 }
2098#endif
2099
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002100 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.com76c913d2013-04-26 15:06:44 +00002101 Json::Value root = CreateJsonTree(
2102 gmmain.fJsonExpectedResults,
2103 gmmain.fJsonActualResults_Failed, gmmain.fJsonActualResults_FailureIgnored,
2104 gmmain.fJsonActualResults_NoComparison, gmmain.fJsonActualResults_Succeeded);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002105 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002106 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002107 stream.write(jsonStdString.c_str(), jsonStdString.length());
2108 }
2109
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002110#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002111
robertphillips@google.com59552022012-08-31 13:07:37 +00002112#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002113 for (int i = 0; i < configs.count(); i++) {
2114 ConfigData config = gRec[configs[i]];
2115
epoger@google.comb0f8b432013-04-10 18:46:25 +00002116 if (FLAGS_verbose && (kGPU_Backend == config.fBackend)) {
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002117 GrContext* gr = grFactory->get(config.fGLContextType);
2118
epoger@google.com5efdd0c2013-03-13 14:18:40 +00002119 gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002120 gr->printCacheStats();
2121 }
2122 }
2123#endif
2124
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002125 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002126#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002127 SkGraphics::Term();
2128
epoger@google.comc8263e72013-04-10 12:17:34 +00002129 return (reportError) ? -1 : 0;
reed@android.com00dae862009-06-10 15:38:48 +00002130}
caryclark@google.com5987f582012-10-02 18:33:14 +00002131
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002132void GMMain::installFilter(SkCanvas* canvas) {
2133 if (FLAGS_forceBWtext) {
2134 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
2135 }
2136}
2137
borenet@google.com7158e6a2012-11-01 17:43:44 +00002138#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00002139int main(int argc, char * const argv[]) {
2140 return tool_main(argc, (char**) argv);
2141}
2142#endif