blob: 179ec84b9590a1987ac28229a96ff03e060c7499 [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"
epoger@google.com908f5832013-04-12 02:23:55 +000021#include "SkBitmapHasher.h"
reed@android.comb9b9a182009-07-08 02:54:47 +000022#include "SkColorPriv.h"
scroggo@google.comd9ba9a02013-03-21 19:43:15 +000023#include "SkCommandLineFlags.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000024#include "SkData.h"
junov@google.com4370aed2012-01-18 16:21:08 +000025#include "SkDeferredCanvas.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000026#include "SkDevice.h"
epoger@google.comde961632012-10-26 18:56:36 +000027#include "SkDrawFilter.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
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000124enum Backend {
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000125 kRaster_Backend,
126 kGPU_Backend,
127 kPDF_Backend,
128 kXPS_Backend,
129};
130
131enum BbhType {
132 kNone_BbhType,
133 kRTree_BbhType,
134 kTileGrid_BbhType,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000135};
136
bsalomon@google.com7361f542012-04-19 19:15:35 +0000137enum ConfigFlags {
138 kNone_ConfigFlag = 0x0,
139 /* Write GM images if a write path is provided. */
140 kWrite_ConfigFlag = 0x1,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000141 /* Read reference GM images if a read path is provided. */
bsalomon@google.com7361f542012-04-19 19:15:35 +0000142 kRead_ConfigFlag = 0x2,
143 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
144};
145
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000146struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000147 SkBitmap::Config fConfig;
148 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000149 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000150 int fSampleCnt; // GPU backend only
151 ConfigFlags fFlags;
152 const char* fName;
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000153 bool fRunByDefault;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000154};
155
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000156class BWTextDrawFilter : public SkDrawFilter {
157public:
reed@google.com971aca72012-11-26 20:26:54 +0000158 virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000159};
reed@google.com971aca72012-11-26 20:26:54 +0000160bool BWTextDrawFilter::filter(SkPaint* p, Type t) {
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000161 if (kText_Type == t) {
162 p->setAntiAlias(false);
163 }
reed@google.com971aca72012-11-26 20:26:54 +0000164 return true;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000165}
166
scroggo@google.com565254b2012-06-28 15:41:32 +0000167struct PipeFlagComboData {
168 const char* name;
169 uint32_t flags;
170};
171
172static PipeFlagComboData gPipeWritingFlagCombos[] = {
173 { "", 0 },
174 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000175 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000176 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000177};
178
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000179static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect);
180
epoger@google.com5079d2c2013-04-12 14:11:21 +0000181const static ErrorCombination kDefaultIgnorableErrorTypes = ErrorCombination()
182 .plus(kMissingExpectations_ErrorType)
183 .plus(kIntentionallySkipped_ErrorType);
184
epoger@google.comde961632012-10-26 18:56:36 +0000185class GMMain {
186public:
epoger@google.com5079d2c2013-04-12 14:11:21 +0000187 GMMain() : fUseFileHierarchy(false), fIgnorableErrorTypes(kDefaultIgnorableErrorTypes),
188 fMismatchPath(NULL), fTestsRun(0), fRenderModesEncountered(1) {}
epoger@google.come8ebeb12012-10-29 16:42:11 +0000189
190 SkString make_name(const char shortName[], const char configName[]) {
191 SkString name;
epoger@google.com57f7abc2012-11-13 03:41:55 +0000192 if (0 == strlen(configName)) {
193 name.append(shortName);
194 } else if (fUseFileHierarchy) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000195 name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
196 } else {
197 name.appendf("%s_%s", shortName, configName);
198 }
epoger@google.comde961632012-10-26 18:56:36 +0000199 return name;
200 }
201
epoger@google.com5f6a0072013-01-31 16:30:55 +0000202 /* since PNG insists on unpremultiplying our alpha, we take no
203 precision chances and force all pixels to be 100% opaque,
204 otherwise on compare we may not get a perfect match.
205 */
206 static void force_all_opaque(const SkBitmap& bitmap) {
207 SkBitmap::Config config = bitmap.config();
208 switch (config) {
209 case SkBitmap::kARGB_8888_Config:
210 force_all_opaque_8888(bitmap);
211 break;
212 case SkBitmap::kRGB_565_Config:
213 // nothing to do here; 565 bitmaps are inherently opaque
214 break;
215 default:
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000216 gm_fprintf(stderr, "unsupported bitmap config %d\n", config);
217 DEBUGFAIL_SEE_STDERR;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000218 }
219 }
220
221 static void force_all_opaque_8888(const SkBitmap& bitmap) {
222 SkAutoLockPixels lock(bitmap);
223 for (int y = 0; y < bitmap.height(); y++) {
224 for (int x = 0; x < bitmap.width(); x++) {
225 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
226 }
227 }
228 }
229
230 static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
231 // TODO(epoger): Now that we have removed force_all_opaque()
232 // from this method, we should be able to get rid of the
233 // transformation to 8888 format also.
234 SkBitmap copy;
235 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
236 return SkImageEncoder::EncodeFile(path.c_str(), copy,
237 SkImageEncoder::kPNG_Type, 100);
238 }
239
epoger@google.com6f6568b2013-03-22 17:29:46 +0000240 /**
epoger@google.com310478e2013-04-03 18:00:39 +0000241 * Add all render modes encountered thus far to the "modes" array.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000242 */
epoger@google.com310478e2013-04-03 18:00:39 +0000243 void GetRenderModesEncountered(SkTArray<SkString> &modes) {
244 SkTDict<int>::Iter iter(this->fRenderModesEncountered);
245 const char* mode;
246 while ((mode = iter.next(NULL)) != NULL) {
247 SkString modeAsString = SkString(mode);
248 // TODO(epoger): It seems a bit silly that all of these modes were
249 // recorded with a leading "-" which we have to remove here
250 // (except for mode "", which means plain old original mode).
251 // But that's how renderModeDescriptor has been passed into
252 // compare_test_results_to_reference_bitmap() historically,
253 // and changing that now may affect other parts of our code.
254 if (modeAsString.startsWith("-")) {
255 modeAsString.remove(0, 1);
256 modes.push_back(modeAsString);
257 }
258 }
259 }
260
261 /**
262 * Records the results of this test in fTestsRun and fFailedTests.
263 *
264 * We even record successes, and errors that we regard as
265 * "ignorable"; we can filter them out later.
266 */
267 void RecordTestResults(const ErrorCombination& errorCombination, const SkString& name,
268 const char renderModeDescriptor []) {
269 // Things to do regardless of errorCombination.
270 fTestsRun++;
271 int renderModeCount = 0;
272 this->fRenderModesEncountered.find(renderModeDescriptor, &renderModeCount);
273 renderModeCount++;
274 this->fRenderModesEncountered.set(renderModeDescriptor, renderModeCount);
275
epoger@google.com6f6568b2013-03-22 17:29:46 +0000276 if (errorCombination.isEmpty()) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000277 return;
epoger@google.comeb066362013-03-08 09:39:36 +0000278 }
279
epoger@google.com310478e2013-04-03 18:00:39 +0000280 // Things to do only if there is some error condition.
epoger@google.comcaac3db2013-04-04 19:23:11 +0000281 SkString fullName = name;
282 fullName.append(renderModeDescriptor);
epoger@google.com310478e2013-04-03 18:00:39 +0000283 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
284 ErrorType type = static_cast<ErrorType>(typeInt);
285 if (errorCombination.includes(type)) {
286 fFailedTests[type].push_back(fullName);
epoger@google.comf60494b2013-04-03 17:02:53 +0000287 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000288 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000289 }
290
epoger@google.com310478e2013-04-03 18:00:39 +0000291 /**
292 * Return the number of significant (non-ignorable) errors we have
293 * encountered so far.
294 */
295 int NumSignificantErrors() {
296 int significantErrors = 0;
297 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
298 ErrorType type = static_cast<ErrorType>(typeInt);
epoger@google.com5079d2c2013-04-12 14:11:21 +0000299 if (!fIgnorableErrorTypes.includes(type)) {
epoger@google.com310478e2013-04-03 18:00:39 +0000300 significantErrors += fFailedTests[type].count();
301 }
302 }
303 return significantErrors;
304 }
305
306 /**
epoger@google.com51dbabe2013-04-10 15:24:53 +0000307 * Display the summary of results with this ErrorType.
308 *
309 * @param type which ErrorType
310 * @param verbose whether to be all verbose about it
epoger@google.com310478e2013-04-03 18:00:39 +0000311 */
epoger@google.com51dbabe2013-04-10 15:24:53 +0000312 void DisplayResultTypeSummary(ErrorType type, bool verbose) {
epoger@google.com5079d2c2013-04-12 14:11:21 +0000313 bool isIgnorableType = fIgnorableErrorTypes.includes(type);
epoger@google.com51dbabe2013-04-10 15:24:53 +0000314
315 SkString line;
316 if (isIgnorableType) {
317 line.append("[ ] ");
318 } else {
319 line.append("[*] ");
320 }
321
322 SkTArray<SkString> *failedTestsOfThisType = &fFailedTests[type];
323 int count = failedTestsOfThisType->count();
324 line.appendf("%d %s", count, getErrorTypeName(type));
325 if (!isIgnorableType || verbose) {
326 line.append(":");
327 for (int i = 0; i < count; ++i) {
328 line.append(" ");
329 line.append((*failedTestsOfThisType)[i]);
330 }
331 }
332 gm_fprintf(stdout, "%s\n", line.c_str());
333 }
334
335 /**
336 * List contents of fFailedTests to stdout.
337 *
338 * @param verbose whether to be all verbose about it
339 */
340 void ListErrors(bool verbose) {
epoger@google.com310478e2013-04-03 18:00:39 +0000341 // First, print a single summary line.
342 SkString summary;
343 summary.appendf("Ran %d tests:", fTestsRun);
344 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
345 ErrorType type = static_cast<ErrorType>(typeInt);
346 summary.appendf(" %s=%d", getErrorTypeName(type), fFailedTests[type].count());
347 }
348 gm_fprintf(stdout, "%s\n", summary.c_str());
349
350 // Now, for each failure type, list the tests that failed that way.
351 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
epoger@google.com51dbabe2013-04-10 15:24:53 +0000352 this->DisplayResultTypeSummary(static_cast<ErrorType>(typeInt), verbose);
epoger@google.com310478e2013-04-03 18:00:39 +0000353 }
354 gm_fprintf(stdout, "(results marked with [*] will cause nonzero return value)\n");
355 }
356
epoger@google.com5f6a0072013-01-31 16:30:55 +0000357 static bool write_document(const SkString& path,
358 const SkDynamicMemoryWStream& document) {
359 SkFILEWStream stream(path.c_str());
360 SkAutoDataUnref data(document.copyToData());
bungeman@google.com1ca36ec2013-05-06 14:28:43 +0000361 return stream.write(data->data(), data->size());
epoger@google.com5f6a0072013-01-31 16:30:55 +0000362 }
363
epoger@google.com37269602013-01-19 04:21:27 +0000364 /**
epoger@google.com5f6a0072013-01-31 16:30:55 +0000365 * Prepare an SkBitmap to render a GM into.
366 *
367 * After you've rendered the GM into the SkBitmap, you must call
368 * complete_bitmap()!
369 *
370 * @todo thudson 22 April 2011 - could refactor this to take in
371 * a factory to generate the context, always call readPixels()
372 * (logically a noop for rasters, if wasted time), and thus collapse the
373 * GPU special case and also let this be used for SkPicture testing.
374 */
375 static void setup_bitmap(const ConfigData& gRec, SkISize& size,
376 SkBitmap* bitmap) {
377 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
378 bitmap->allocPixels();
379 bitmap->eraseColor(SK_ColorTRANSPARENT);
380 }
381
382 /**
383 * Any finalization steps we need to perform on the SkBitmap after
384 * we have rendered the GM into it.
epoger@google.com37269602013-01-19 04:21:27 +0000385 *
386 * It's too bad that we are throwing away alpha channel data
387 * we could otherwise be examining, but this had always been happening
388 * before... it was buried within the compare() method at
389 * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
390 *
391 * Apparently we need this, at least for bitmaps that are either:
392 * (a) destined to be written out as PNG files, or
393 * (b) compared against bitmaps read in from PNG files
394 * for the reasons described just above the force_all_opaque() method.
395 *
396 * Neglecting to do this led to the difficult-to-diagnose
397 * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
398 * spurious pixel_error messages as of r7258')
399 *
400 * TODO(epoger): Come up with a better solution that allows us to
401 * compare full pixel data, including alpha channel, while still being
402 * robust in the face of transformations to/from PNG files.
403 * Options include:
404 *
405 * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
406 * will be written to, or compared against, PNG files.
407 * PRO: Preserve/compare alpha channel info for the non-PNG cases
408 * (comparing different renderModes in-memory)
epoger@google.com908f5832013-04-12 02:23:55 +0000409 * CON: The bitmaps (and hash digests) for these non-PNG cases would be
epoger@google.com37269602013-01-19 04:21:27 +0000410 * different than those for the PNG-compared cases, and in the
411 * case of a failed renderMode comparison, how would we write the
412 * image to disk for examination?
413 *
epoger@google.com908f5832013-04-12 02:23:55 +0000414 * 2. Always compute image hash digests from PNG format (either
epoger@google.com37269602013-01-19 04:21:27 +0000415 * directly from the the bytes of a PNG file, or capturing the
416 * bytes we would have written to disk if we were writing the
417 * bitmap out as a PNG).
418 * PRO: I think this would allow us to never force opaque, and to
419 * the extent that alpha channel data can be preserved in a PNG
420 * file, we could observe it.
epoger@google.com908f5832013-04-12 02:23:55 +0000421 * CON: If we read a bitmap from disk, we need to take its hash digest
epoger@google.com37269602013-01-19 04:21:27 +0000422 * from the source PNG (we can't compute it from the bitmap we
423 * read out of the PNG, because we will have already premultiplied
424 * the alpha).
425 * CON: Seems wasteful to convert a bitmap to PNG format just to take
epoger@google.com908f5832013-04-12 02:23:55 +0000426 * its hash digest. (Although we're wasting lots of effort already
epoger@google.com37269602013-01-19 04:21:27 +0000427 * calling force_all_opaque().)
428 *
429 * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
430 * so we can transform images back and forth without fear of off-by-one
431 * errors.
432 * CON: Math is hard.
433 *
434 * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
435 * channel), rather than demanding absolute equality.
epoger@google.com908f5832013-04-12 02:23:55 +0000436 * CON: Can't do this with hash digests.
epoger@google.com37269602013-01-19 04:21:27 +0000437 */
epoger@google.com5f6a0072013-01-31 16:30:55 +0000438 static void complete_bitmap(SkBitmap* bitmap) {
439 force_all_opaque(*bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000440 }
441
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000442 static void installFilter(SkCanvas* canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000443
reed@google.comaef73612012-11-16 13:41:45 +0000444 static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000445 SkAutoCanvasRestore acr(canvas, true);
446
447 if (!isPDF) {
448 canvas->concat(gm->getInitialTransform());
449 }
450 installFilter(canvas);
reed@google.comaef73612012-11-16 13:41:45 +0000451 gm->setCanvasIsDeferred(isDeferred);
epoger@google.comde961632012-10-26 18:56:36 +0000452 gm->draw(canvas);
453 canvas->setDrawFilter(NULL);
454 }
455
epoger@google.com6f6568b2013-03-22 17:29:46 +0000456 static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000457 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000458 SkBitmap* bitmap,
459 bool deferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000460 SkISize size (gm->getISize());
461 setup_bitmap(gRec, size, bitmap);
462
463 SkAutoTUnref<SkCanvas> canvas;
464
465 if (gRec.fBackend == kRaster_Backend) {
466 SkAutoTUnref<SkDevice> device(new SkDevice(*bitmap));
467 if (deferred) {
468 canvas.reset(new SkDeferredCanvas(device));
469 } else {
470 canvas.reset(new SkCanvas(device));
471 }
reed@google.comaef73612012-11-16 13:41:45 +0000472 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000473 canvas->flush();
474 }
475#if SK_SUPPORT_GPU
476 else { // GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000477 SkAutoTUnref<SkDevice> device(SkGpuDevice::Create(gpuTarget));
epoger@google.comde961632012-10-26 18:56:36 +0000478 if (deferred) {
479 canvas.reset(new SkDeferredCanvas(device));
480 } else {
481 canvas.reset(new SkCanvas(device));
482 }
reed@google.comaef73612012-11-16 13:41:45 +0000483 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000484 // the device is as large as the current rendertarget, so
485 // we explicitly only readback the amount we expect (in
486 // size) overwrite our previous allocation
487 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
488 size.fHeight);
489 canvas->readPixels(bitmap, 0, 0);
490 }
491#endif
epoger@google.com5f6a0072013-01-31 16:30:55 +0000492 complete_bitmap(bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000493 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000494 }
495
496 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
junov@chromium.orgc938c482012-12-19 15:24:38 +0000497 SkPicture* pict, SkBitmap* bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000498 SkScalar scale = SK_Scalar1,
499 bool tile = false) {
epoger@google.comde961632012-10-26 18:56:36 +0000500 SkISize size = gm->getISize();
501 setup_bitmap(gRec, size, bitmap);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000502
503 if (tile) {
504 // Generate the result image by rendering to tiles and accumulating
505 // the results in 'bitmap'
506
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000507 // This 16x16 tiling matches the settings applied to 'pict' in
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000508 // 'generate_new_picture'
509 SkISize tileSize = SkISize::Make(16, 16);
510
511 SkBitmap tileBM;
512 setup_bitmap(gRec, tileSize, &tileBM);
513 SkCanvas tileCanvas(tileBM);
514 installFilter(&tileCanvas);
515
516 SkCanvas bmpCanvas(*bitmap);
517 SkPaint bmpPaint;
518 bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
519
520 for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
521 for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
522 int saveCount = tileCanvas.save();
523 SkMatrix mat(tileCanvas.getTotalMatrix());
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000524 mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000525 SkIntToScalar(-yTile*tileSize.height()));
526 tileCanvas.setMatrix(mat);
527 pict->draw(&tileCanvas);
528 tileCanvas.flush();
529 tileCanvas.restoreToCount(saveCount);
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000530 bmpCanvas.drawBitmap(tileBM,
531 SkIntToScalar(xTile * tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000532 SkIntToScalar(yTile * tileSize.height()),
533 &bmpPaint);
534 }
535 }
536 } else {
537 SkCanvas canvas(*bitmap);
538 installFilter(&canvas);
539 canvas.scale(scale, scale);
540 canvas.drawPicture(*pict);
541 complete_bitmap(bitmap);
542 }
epoger@google.comde961632012-10-26 18:56:36 +0000543 }
544
545 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
546#ifdef SK_SUPPORT_PDF
547 SkMatrix initialTransform = gm->getInitialTransform();
548 SkISize pageSize = gm->getISize();
549 SkPDFDevice* dev = NULL;
550 if (initialTransform.isIdentity()) {
551 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
552 } else {
553 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
554 SkIntToScalar(pageSize.height()));
555 initialTransform.mapRect(&content);
556 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
557 SkIntToScalar(pageSize.height()));
558 SkISize contentSize =
559 SkISize::Make(SkScalarRoundToInt(content.width()),
560 SkScalarRoundToInt(content.height()));
561 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
562 }
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000563 dev->setDCTEncoder(encode_to_dct_stream);
epoger@google.comde961632012-10-26 18:56:36 +0000564 SkAutoUnref aur(dev);
565
566 SkCanvas c(dev);
reed@google.comaef73612012-11-16 13:41:45 +0000567 invokeGM(gm, &c, true, false);
epoger@google.comde961632012-10-26 18:56:36 +0000568
569 SkPDFDocument doc;
570 doc.appendPage(dev);
571 doc.emitPDF(&pdf);
572#endif
573 }
574
575 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
576#ifdef SK_SUPPORT_XPS
577 SkISize size = gm->getISize();
578
579 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
580 SkIntToScalar(size.height()));
581 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
582 static const SkScalar upm = 72 * inchesPerMeter;
583 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
584 static const SkScalar ppm = 200 * inchesPerMeter;
585 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
586
587 SkXPSDevice* dev = new SkXPSDevice();
588 SkAutoUnref aur(dev);
589
590 SkCanvas c(dev);
591 dev->beginPortfolio(&xps);
592 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000593 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000594 dev->endSheet();
595 dev->endPortfolio();
596
597#endif
598 }
599
epoger@google.com6f6568b2013-03-22 17:29:46 +0000600 ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
601 const char renderModeDescriptor [], const SkString& name,
602 SkBitmap& bitmap, SkDynamicMemoryWStream* document) {
epoger@google.comde961632012-10-26 18:56:36 +0000603 SkString path;
604 bool success = false;
605 if (gRec.fBackend == kRaster_Backend ||
606 gRec.fBackend == kGPU_Backend ||
607 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
608
epoger@google.com37269602013-01-19 04:21:27 +0000609 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
610 "png");
epoger@google.comde961632012-10-26 18:56:36 +0000611 success = write_bitmap(path, bitmap);
612 }
613 if (kPDF_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000614 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
615 "pdf");
epoger@google.comde961632012-10-26 18:56:36 +0000616 success = write_document(path, *document);
617 }
618 if (kXPS_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000619 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
620 "xps");
epoger@google.comde961632012-10-26 18:56:36 +0000621 success = write_document(path, *document);
622 }
623 if (success) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000624 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000625 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000626 gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000627 ErrorCombination errors(kWritingReferenceImage_ErrorType);
epoger@google.com310478e2013-04-03 18:00:39 +0000628 // TODO(epoger): Don't call RecordTestResults() here...
629 // Instead, we should make sure to call RecordTestResults
630 // exactly ONCE per test. (Otherwise, gmmain.fTestsRun
631 // will be incremented twice for this test: once in
632 // compare_test_results_to_stored_expectations() before
633 // that method calls this one, and again here.)
634 //
635 // When we make that change, we should probably add a
636 // WritingReferenceImage test to the gm self-tests.)
637 RecordTestResults(errors, name, renderModeDescriptor);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000638 return errors;
epoger@google.comde961632012-10-26 18:56:36 +0000639 }
640 }
641
epoger@google.com37269602013-01-19 04:21:27 +0000642 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000643 * Log more detail about the mistmatch between expectedBitmap and
644 * actualBitmap.
645 */
646 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
647 const char *testName) {
648 const int expectedWidth = expectedBitmap.width();
649 const int expectedHeight = expectedBitmap.height();
650 const int width = actualBitmap.width();
651 const int height = actualBitmap.height();
652 if ((expectedWidth != width) || (expectedHeight != height)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000653 gm_fprintf(stderr, "---- %s: dimension mismatch --"
654 " expected [%d %d], actual [%d %d]\n",
655 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000656 return;
657 }
658
659 if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
660 (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000661 gm_fprintf(stderr, "---- %s: not computing max per-channel"
662 " pixel mismatch because non-8888\n", testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000663 return;
664 }
665
666 SkAutoLockPixels alp0(expectedBitmap);
667 SkAutoLockPixels alp1(actualBitmap);
668 int errR = 0;
669 int errG = 0;
670 int errB = 0;
671 int errA = 0;
672 int differingPixels = 0;
673
674 for (int y = 0; y < height; ++y) {
675 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
676 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
677 for (int x = 0; x < width; ++x) {
678 SkPMColor expectedPixel = *expectedPixelPtr++;
679 SkPMColor actualPixel = *actualPixelPtr++;
680 if (expectedPixel != actualPixel) {
681 differingPixels++;
682 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
683 (int)SkGetPackedR32(actualPixel)));
684 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
685 (int)SkGetPackedG32(actualPixel)));
686 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
687 (int)SkGetPackedB32(actualPixel)));
688 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
689 (int)SkGetPackedA32(actualPixel)));
690 }
691 }
692 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000693 gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
694 " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
695 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000696 }
697
698 /**
epoger@google.com908f5832013-04-12 02:23:55 +0000699 * Compares actual hash digest to expectations, returning the set of errors
epoger@google.com6f6568b2013-03-22 17:29:46 +0000700 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000701 *
702 * If fMismatchPath has been set, and there are pixel diffs, then the
703 * actual bitmap will be written out to a file within fMismatchPath.
704 *
705 * @param expectations what expectations to compare actualBitmap against
706 * @param actualBitmap the image we actually generated
707 * @param baseNameString name of test without renderModeDescriptor added
708 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
709 * @param addToJsonSummary whether to add these results (both actual and
epoger@google.comcaac3db2013-04-04 19:23:11 +0000710 * expected) to the JSON summary. Regardless of this setting, if
711 * we find an image mismatch in this test, we will write these
712 * results to the JSON summary. (This is so that we will always
713 * report errors across rendering modes, such as pipe vs tiled.
714 * See https://codereview.chromium.org/13650002/ )
epoger@google.com37269602013-01-19 04:21:27 +0000715 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000716 ErrorCombination compare_to_expectations(Expectations expectations,
717 const SkBitmap& actualBitmap,
718 const SkString& baseNameString,
719 const char renderModeDescriptor[],
epoger@google.comcaac3db2013-04-04 19:23:11 +0000720 bool addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000721 ErrorCombination errors;
epoger@google.com908f5832013-04-12 02:23:55 +0000722 SkHashDigest actualBitmapHash;
723 // TODO(epoger): Better handling for error returned by ComputeDigest()?
724 // For now, we just report a digest of 0 in error cases, like before.
725 if (!SkBitmapHasher::ComputeDigest(actualBitmap, &actualBitmapHash)) {
726 actualBitmapHash = 0;
727 }
epoger@google.com37269602013-01-19 04:21:27 +0000728 SkString completeNameString = baseNameString;
729 completeNameString.append(renderModeDescriptor);
730 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000731
epoger@google.com37269602013-01-19 04:21:27 +0000732 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000733 errors.add(kMissingExpectations_ErrorType);
epoger@google.com908f5832013-04-12 02:23:55 +0000734 } else if (!expectations.match(actualBitmapHash)) {
epoger@google.comcaac3db2013-04-04 19:23:11 +0000735 addToJsonSummary = true;
736 // The error mode we record depends on whether this was running
737 // in a non-standard renderMode.
738 if ('\0' == *renderModeDescriptor) {
739 errors.add(kExpectationsMismatch_ErrorType);
740 } else {
741 errors.add(kRenderModeMismatch_ErrorType);
742 }
epoger@google.com84a18022013-02-01 20:39:15 +0000743
744 // Write out the "actuals" for any mismatches, if we have
745 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000746 if (fMismatchPath) {
747 SkString path =
748 make_filename(fMismatchPath, renderModeDescriptor,
749 baseNameString.c_str(), "png");
750 write_bitmap(path, actualBitmap);
751 }
epoger@google.com84a18022013-02-01 20:39:15 +0000752
753 // If we have access to a single expected bitmap, log more
754 // detail about the mismatch.
755 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
756 if (NULL != expectedBitmapPtr) {
757 report_bitmap_diffs(*expectedBitmapPtr, actualBitmap, completeName);
758 }
epoger@google.coma243b222013-01-17 17:54:28 +0000759 }
epoger@google.com310478e2013-04-03 18:00:39 +0000760 RecordTestResults(errors, baseNameString, renderModeDescriptor);
epoger@google.coma243b222013-01-17 17:54:28 +0000761
epoger@google.com37269602013-01-19 04:21:27 +0000762 if (addToJsonSummary) {
epoger@google.com908f5832013-04-12 02:23:55 +0000763 add_actual_results_to_json_summary(completeName, actualBitmapHash, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000764 expectations.ignoreFailure());
765 add_expected_results_to_json_summary(completeName, expectations);
766 }
epoger@google.coma243b222013-01-17 17:54:28 +0000767
epoger@google.com6f6568b2013-03-22 17:29:46 +0000768 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000769 }
770
epoger@google.com37269602013-01-19 04:21:27 +0000771 /**
772 * Add this result to the appropriate JSON collection of actual results,
epoger@google.com76c913d2013-04-26 15:06:44 +0000773 * depending on errors encountered.
epoger@google.com37269602013-01-19 04:21:27 +0000774 */
775 void add_actual_results_to_json_summary(const char testName[],
epoger@google.com76c913d2013-04-26 15:06:44 +0000776 const SkHashDigest& actualResult,
777 ErrorCombination errors,
epoger@google.com37269602013-01-19 04:21:27 +0000778 bool ignoreFailure) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000779 Json::Value jsonActualResults = ActualResultAsJsonValue(actualResult);
780 if (errors.isEmpty()) {
781 this->fJsonActualResults_Succeeded[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000782 } else {
783 if (ignoreFailure) {
784 // TODO: Once we have added the ability to compare
785 // actual results against expectations in a JSON file
786 // (where we can set ignore-failure to either true or
epoger@google.com84a18022013-02-01 20:39:15 +0000787 // false), add test cases that exercise ignored
epoger@google.com6f6568b2013-03-22 17:29:46 +0000788 // failures (both for kMissingExpectations_ErrorType
epoger@google.comcaac3db2013-04-04 19:23:11 +0000789 // and kExpectationsMismatch_ErrorType).
epoger@google.com37269602013-01-19 04:21:27 +0000790 this->fJsonActualResults_FailureIgnored[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000791 jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000792 } else {
epoger@google.com76c913d2013-04-26 15:06:44 +0000793 if (errors.includes(kMissingExpectations_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000794 // TODO: What about the case where there IS an
epoger@google.com908f5832013-04-12 02:23:55 +0000795 // expected image hash digest, but that gm test
epoger@google.com37269602013-01-19 04:21:27 +0000796 // doesn't actually run? For now, those cases
797 // will always be ignored, because gm only looks
798 // at expectations that correspond to gm tests
799 // that were actually run.
800 //
801 // Once we have the ability to express
802 // expectations as a JSON file, we should fix this
803 // (and add a test case for which an expectation
804 // is given but the test is never run).
805 this->fJsonActualResults_NoComparison[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000806 jsonActualResults;
epoger@google.comeb066362013-03-08 09:39:36 +0000807 }
epoger@google.com76c913d2013-04-26 15:06:44 +0000808 if (errors.includes(kExpectationsMismatch_ErrorType) ||
809 errors.includes(kRenderModeMismatch_ErrorType)) {
810 this->fJsonActualResults_Failed[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000811 }
812 }
813 }
814 }
815
816 /**
817 * Add this test to the JSON collection of expected results.
818 */
819 void add_expected_results_to_json_summary(const char testName[],
820 Expectations expectations) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000821 this->fJsonExpectedResults[testName] = expectations.asJsonValue();
epoger@google.com37269602013-01-19 04:21:27 +0000822 }
823
824 /**
825 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
826 *
827 * @param gm which test generated the actualBitmap
828 * @param gRec
829 * @param writePath unless this is NULL, write out actual images into this
830 * directory
831 * @param actualBitmap bitmap generated by this run
832 * @param pdf
833 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000834 ErrorCombination compare_test_results_to_stored_expectations(
epoger@google.com37269602013-01-19 04:21:27 +0000835 GM* gm, const ConfigData& gRec, const char writePath[],
836 SkBitmap& actualBitmap, SkDynamicMemoryWStream* pdf) {
837
epoger@google.coma243b222013-01-17 17:54:28 +0000838 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000839 ErrorCombination errors;
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000840
epoger@google.com6f6568b2013-03-22 17:29:46 +0000841 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
epoger@google.com37269602013-01-19 04:21:27 +0000842 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
843 /*
844 * Get the expected results for this test, as one or more allowed
epoger@google.com908f5832013-04-12 02:23:55 +0000845 * hash digests. The current implementation of expectationsSource
846 * get this by computing the hash digest of a single PNG file on disk.
epoger@google.com37269602013-01-19 04:21:27 +0000847 *
848 * TODO(epoger): This relies on the fact that
849 * force_all_opaque() was called on the bitmap before it
850 * was written to disk as a PNG in the first place. If
epoger@google.com908f5832013-04-12 02:23:55 +0000851 * not, the hash digest returned here may not match the
852 * hash digest of actualBitmap, which *has* been run through
epoger@google.com37269602013-01-19 04:21:27 +0000853 * force_all_opaque().
epoger@google.com5f6a0072013-01-31 16:30:55 +0000854 * See comments above complete_bitmap() for more detail.
epoger@google.com37269602013-01-19 04:21:27 +0000855 */
856 Expectations expectations = expectationsSource->get(name.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000857 errors.add(compare_to_expectations(expectations, actualBitmap,
858 name, "", true));
epoger@google.com37269602013-01-19 04:21:27 +0000859 } else {
860 // If we are running without expectations, we still want to
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000861 // record the actual results.
epoger@google.com908f5832013-04-12 02:23:55 +0000862 SkHashDigest actualBitmapHash;
863 // TODO(epoger): Better handling for error returned by ComputeDigest()?
864 // For now, we just report a digest of 0 in error cases, like before.
865 if (!SkBitmapHasher::ComputeDigest(actualBitmap, &actualBitmapHash)) {
866 actualBitmapHash = 0;
867 }
868 add_actual_results_to_json_summary(name.c_str(), actualBitmapHash,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000869 ErrorCombination(kMissingExpectations_ErrorType),
epoger@google.com37269602013-01-19 04:21:27 +0000870 false);
epoger@google.com310478e2013-04-03 18:00:39 +0000871 RecordTestResults(ErrorCombination(kMissingExpectations_ErrorType), name, "");
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000872 }
epoger@google.com37269602013-01-19 04:21:27 +0000873
874 // TODO: Consider moving this into compare_to_expectations(),
875 // similar to fMismatchPath... for now, we don't do that, because
876 // we don't want to write out the actual bitmaps for all
877 // renderModes of all tests! That would be a lot of files.
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000878 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000879 errors.add(write_reference_image(gRec, writePath, "",
880 name, actualBitmap, pdf));
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000881 }
epoger@google.com37269602013-01-19 04:21:27 +0000882
epoger@google.com6f6568b2013-03-22 17:29:46 +0000883 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000884 }
885
epoger@google.com37269602013-01-19 04:21:27 +0000886 /**
887 * Compare actualBitmap to referenceBitmap.
888 *
epoger@google.comc8263e72013-04-10 12:17:34 +0000889 * @param baseNameString name of test without renderModeDescriptor added
epoger@google.com37269602013-01-19 04:21:27 +0000890 * @param renderModeDescriptor
891 * @param actualBitmap actual bitmap generated by this run
892 * @param referenceBitmap bitmap we expected to be generated
893 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000894 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.comc8263e72013-04-10 12:17:34 +0000895 const SkString& baseNameString, const char renderModeDescriptor[],
epoger@google.com37269602013-01-19 04:21:27 +0000896 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
897
898 SkASSERT(referenceBitmap);
epoger@google.com84a18022013-02-01 20:39:15 +0000899 Expectations expectations(*referenceBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000900 return compare_to_expectations(expectations, actualBitmap,
epoger@google.comc8263e72013-04-10 12:17:34 +0000901 baseNameString, renderModeDescriptor, false);
epoger@google.com37269602013-01-19 04:21:27 +0000902 }
903
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000904 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
905 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +0000906 // Pictures are refcounted so must be on heap
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000907 SkPicture* pict;
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000908 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
909 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +0000910
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000911 if (kTileGrid_BbhType == bbhType) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000912 SkTileGridPicture::TileGridInfo info;
913 info.fMargin.setEmpty();
914 info.fOffset.setZero();
915 info.fTileInterval.set(16, 16);
916 pict = new SkTileGridPicture(width, height, info);
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000917 } else {
918 pict = new SkPicture;
919 }
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000920 if (kNone_BbhType != bbhType) {
921 recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
922 }
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000923 SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +0000924 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +0000925 invokeGM(gm, cv, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000926 pict->endRecording();
927
928 return pict;
929 }
930
931 static SkPicture* stream_to_new_picture(const SkPicture& src) {
932
933 // To do in-memory commiunications with a stream, we need to:
934 // * create a dynamic memory stream
935 // * copy it into a buffer
936 // * create a read stream from it
937 // ?!?!
938
939 SkDynamicMemoryWStream storage;
940 src.serialize(&storage);
941
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000942 size_t streamSize = storage.getOffset();
epoger@google.comde961632012-10-26 18:56:36 +0000943 SkAutoMalloc dstStorage(streamSize);
944 void* dst = dstStorage.get();
945 //char* dst = new char [streamSize];
946 //@todo thudson 22 April 2011 when can we safely delete [] dst?
947 storage.copyTo(dst);
948 SkMemoryStream pictReadback(dst, streamSize);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000949 bool success;
950 // Pass a decoding bitmap function so that the factory GM (which has an SkBitmap with
951 // encoded data) does not fail.
952 SkPicture* retval = new SkPicture (&pictReadback, &success, &SkImageDecoder::DecodeMemory);
epoger@google.comde961632012-10-26 18:56:36 +0000953 return retval;
954 }
955
956 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +0000957 // Depending on flags, possibly compare to an expected image.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000958 ErrorCombination test_drawing(GM* gm,
959 const ConfigData& gRec,
960 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000961 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000962 SkBitmap* bitmap) {
epoger@google.comde961632012-10-26 18:56:36 +0000963 SkDynamicMemoryWStream document;
964
965 if (gRec.fBackend == kRaster_Backend ||
966 gRec.fBackend == kGPU_Backend) {
967 // Early exit if we can't generate the image.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000968 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000969 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +0000970 // TODO: Add a test to exercise what the stdout and
971 // JSON look like if we get an "early error" while
972 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +0000973 return errors;
974 }
975 } else if (gRec.fBackend == kPDF_Backend) {
976 generate_pdf(gm, document);
977#if CAN_IMAGE_PDF
978 SkAutoDataUnref data(document.copyToData());
979 SkMemoryStream stream(data->data(), data->size());
980 SkPDFDocumentToBitmap(&stream, bitmap);
981#endif
982 } else if (gRec.fBackend == kXPS_Backend) {
983 generate_xps(gm, document);
984 }
epoger@google.com37269602013-01-19 04:21:27 +0000985 return compare_test_results_to_stored_expectations(
986 gm, gRec, writePath, *bitmap, &document);
epoger@google.comde961632012-10-26 18:56:36 +0000987 }
988
epoger@google.com6f6568b2013-03-22 17:29:46 +0000989 ErrorCombination test_deferred_drawing(GM* gm,
990 const ConfigData& gRec,
991 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000992 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +0000993 SkDynamicMemoryWStream document;
994
995 if (gRec.fBackend == kRaster_Backend ||
996 gRec.fBackend == kGPU_Backend) {
epoger@google.comc8263e72013-04-10 12:17:34 +0000997 const char renderModeDescriptor[] = "-deferred";
epoger@google.comde961632012-10-26 18:56:36 +0000998 SkBitmap bitmap;
999 // Early exit if we can't generate the image, but this is
1000 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001001 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001002 // TODO(epoger): This logic is the opposite of what is
1003 // described above... if we succeeded in generating the
1004 // -deferred image, we exit early! We should fix this
1005 // ASAP, because it is hiding -deferred errors... but for
1006 // now, I'm leaving the logic as it is so that the
1007 // refactoring change
1008 // https://codereview.chromium.org/12992003/ is unblocked.
1009 //
1010 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
1011 // ('image-surface gm test is failing in "deferred" mode,
1012 // and gm is not reporting the failure')
1013 if (errors.isEmpty()) {
epoger@google.com310478e2013-04-03 18:00:39 +00001014 // TODO(epoger): Report this as a new ErrorType,
1015 // something like kImageGeneration_ErrorType?
epoger@google.com6f6568b2013-03-22 17:29:46 +00001016 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001017 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001018 const SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com37269602013-01-19 04:21:27 +00001019 return compare_test_results_to_reference_bitmap(
epoger@google.comc8263e72013-04-10 12:17:34 +00001020 name, renderModeDescriptor, bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001021 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001022 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001023 }
1024
epoger@google.comcaac3db2013-04-04 19:23:11 +00001025 ErrorCombination test_pipe_playback(GM* gm, const ConfigData& gRec,
1026 const SkBitmap& referenceBitmap, bool simulateFailure) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001027 const SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001028 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001029 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001030 SkString renderModeDescriptor("-pipe");
1031 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1032
1033 if (gm->getFlags() & GM::kSkipPipe_Flag) {
1034 RecordTestResults(kIntentionallySkipped_ErrorType, name,
1035 renderModeDescriptor.c_str());
1036 errors.add(kIntentionallySkipped_ErrorType);
1037 } else {
1038 SkBitmap bitmap;
1039 SkISize size = gm->getISize();
1040 setup_bitmap(gRec, size, &bitmap);
1041 SkCanvas canvas(bitmap);
1042 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001043 // Pass a decoding function so the factory GM (which has an SkBitmap
1044 // with encoded data) will not fail playback.
1045 PipeController pipeController(&canvas, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001046 SkGPipeWriter writer;
1047 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001048 gPipeWritingFlagCombos[i].flags,
1049 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001050 if (!simulateFailure) {
1051 invokeGM(gm, pipeCanvas, false, false);
1052 }
1053 complete_bitmap(&bitmap);
1054 writer.endRecording();
1055 errors.add(compare_test_results_to_reference_bitmap(
1056 name, renderModeDescriptor.c_str(), bitmap, &referenceBitmap));
1057 if (!errors.isEmpty()) {
1058 break;
1059 }
epoger@google.comde961632012-10-26 18:56:36 +00001060 }
1061 }
1062 return errors;
1063 }
1064
epoger@google.com6f6568b2013-03-22 17:29:46 +00001065 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
1066 const SkBitmap& referenceBitmap) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001067 const SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001068 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001069 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001070 SkString renderModeDescriptor("-tiled pipe");
1071 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1072
1073 if ((gm->getFlags() & GM::kSkipPipe_Flag) ||
1074 (gm->getFlags() & GM::kSkipTiled_Flag)) {
1075 RecordTestResults(kIntentionallySkipped_ErrorType, name,
1076 renderModeDescriptor.c_str());
1077 errors.add(kIntentionallySkipped_ErrorType);
1078 } else {
1079 SkBitmap bitmap;
1080 SkISize size = gm->getISize();
1081 setup_bitmap(gRec, size, &bitmap);
1082 SkCanvas canvas(bitmap);
1083 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001084 TiledPipeController pipeController(bitmap, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001085 SkGPipeWriter writer;
1086 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001087 gPipeWritingFlagCombos[i].flags,
1088 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001089 invokeGM(gm, pipeCanvas, false, false);
1090 complete_bitmap(&bitmap);
1091 writer.endRecording();
1092 errors.add(compare_test_results_to_reference_bitmap(name,
1093 renderModeDescriptor.c_str(),
1094 bitmap, &referenceBitmap));
1095 if (!errors.isEmpty()) {
1096 break;
1097 }
epoger@google.comde961632012-10-26 18:56:36 +00001098 }
1099 }
1100 return errors;
1101 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00001102
1103 //
1104 // member variables.
1105 // They are public for now, to allow easier setting by tool_main().
1106 //
1107
epoger@google.come8ebeb12012-10-29 16:42:11 +00001108 bool fUseFileHierarchy;
epoger@google.com5079d2c2013-04-12 14:11:21 +00001109 ErrorCombination fIgnorableErrorTypes;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001110
junov@chromium.org95146eb2013-01-11 21:04:40 +00001111 const char* fMismatchPath;
1112
epoger@google.com310478e2013-04-03 18:00:39 +00001113 // collection of tests that have failed with each ErrorType
1114 SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
1115 int fTestsRun;
1116 SkTDict<int> fRenderModesEncountered;
epoger@google.com57f7abc2012-11-13 03:41:55 +00001117
epoger@google.com908f5832013-04-12 02:23:55 +00001118 // Where to read expectations (expected image hash digests, etc.) from.
epoger@google.com37269602013-01-19 04:21:27 +00001119 // If unset, we don't do comparisons.
1120 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
1121
1122 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +00001123 Json::Value fJsonExpectedResults;
1124 Json::Value fJsonActualResults_Failed;
1125 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001126 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001127 Json::Value fJsonActualResults_Succeeded;
1128
epoger@google.comde961632012-10-26 18:56:36 +00001129}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +00001130
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001131#if SK_SUPPORT_GPU
1132static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1133#else
1134static const GLContextType kDontCare_GLContextType = 0;
1135#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +00001136
1137// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +00001138// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +00001139static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
1140 kWrite_ConfigFlag;
1141
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001142static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001143 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001144#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 +00001145 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001146#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001147 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
1148#if SK_SUPPORT_GPU
1149 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
1150 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", true },
1151 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
bsalomon@google.com7361f542012-04-19 19:15:35 +00001152 /* The debug context does not generate images */
scroggo@google.com0f567c62013-03-20 15:35:08 +00001153 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", GR_DEBUG},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001154#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001155 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
1156 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001157#endif // SK_ANGLE
1158#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001159 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001160#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001161#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +00001162#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001163 /* 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 +00001164 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +00001165#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001166#ifdef SK_SUPPORT_PDF
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001167 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001168#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001169};
1170
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001171static const char kDefaultsConfigStr[] = "defaults";
1172static const char kExcludeConfigChar = '~';
1173
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001174static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001175 SkString result;
1176 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001177 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001178 SkASSERT(gRec[i].fName != kDefaultsConfigStr);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001179 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001180 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001181 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001182 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001183 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001184 result.append("]\n");
1185 result.appendf("The default value is: \"");
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001186 SkString firstDefault;
1187 SkString allButFirstDefaults;
1188 SkString nonDefault;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001189 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1190 if (gRec[i].fRunByDefault) {
1191 if (i > 0) {
1192 result.append(" ");
1193 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001194 result.append(gRec[i].fName);
1195 if (firstDefault.isEmpty()) {
1196 firstDefault = gRec[i].fName;
1197 } else {
1198 if (!allButFirstDefaults.isEmpty()) {
1199 allButFirstDefaults.append(" ");
1200 }
1201 allButFirstDefaults.append(gRec[i].fName);
1202 }
1203 } else {
1204 nonDefault = gRec[i].fName;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001205 }
1206 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001207 result.append("\"\n");
1208 result.appendf("\"%s\" evaluates to the default set of configs.\n", kDefaultsConfigStr);
1209 result.appendf("Prepending \"%c\" on a config name excludes it from the set of configs to run.\n"
1210 "Exclusions always override inclusions regardless of order.\n",
1211 kExcludeConfigChar);
1212 result.appendf("E.g. \"--config %s %c%s %s\" will run these configs:\n\t%s %s",
1213 kDefaultsConfigStr,
1214 kExcludeConfigChar,
1215 firstDefault.c_str(),
1216 nonDefault.c_str(),
1217 allButFirstDefaults.c_str(),
1218 nonDefault.c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001219 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001220}
scroggo@google.com7d519302013-03-19 17:28:10 +00001221
epoger@google.com6f6568b2013-03-22 17:29:46 +00001222// Macro magic to convert a numeric preprocessor token into a string.
1223// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1224// This should probably be moved into one of our common headers...
1225#define TOSTRING_INTERNAL(x) #x
1226#define TOSTRING(x) TOSTRING_INTERNAL(x)
1227
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001228// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001229DEFINE_string(config, "", configUsage().c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001230DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001231DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1232DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1233#if SK_SUPPORT_GPU
1234DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001235 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1236 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001237#endif
1238DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1239 "when reading/writing files.");
epoger@google.com5079d2c2013-04-12 14:11:21 +00001240DEFINE_string(ignoreErrorTypes, kDefaultIgnorableErrorTypes.asString(" ").c_str(),
1241 "Space-separated list of ErrorTypes that should be ignored. If any *other* error "
1242 "types are encountered, the tool will exit with a nonzero return value.");
caryclark@google.com512c9b62013-05-10 15:16:13 +00001243DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
1244 "Multiple matches may be separated by spaces.\n"
1245 "~ causes a matching test to always be skipped\n"
1246 "^ requires the start of the test to match\n"
1247 "$ requires the end of the test to match\n"
1248 "^ and $ requires an exact match\n"
1249 "If a test does not match any list entry,\n"
1250 "it is skipped unless some list entry starts with ~");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001251DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1252 "pixel mismatches into this directory.");
1253DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1254 "testIndex %% divisor == remainder.");
1255DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1256DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1257DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1258 "any differences between those and the newly generated ones.");
1259DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1260DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1261DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1262DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
epoger@google.comcaac3db2013-04-04 19:23:11 +00001263DEFINE_bool(simulatePipePlaybackFailure, false, "Simulate a rendering failure in pipe mode only.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001264DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1265DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1266DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1267 "factors to be used for tileGrid playback testing. Default value: 1.0");
1268DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
epoger@google.comb0f8b432013-04-10 18:46:25 +00001269DEFINE_bool2(verbose, v, false, "Give more detail (e.g. list all GMs run, more info about "
1270 "each test).");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001271DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
scroggo@google.com604e0c22013-04-09 21:25:46 +00001272DEFINE_string2(writePicturePath, p, "", "Write .skp files into this directory.");
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001273DEFINE_int32(pdfJpegQuality, -1, "Encodes images in JPEG at quality level N, "
1274 "which can be in range 0-100). N = -1 will disable JPEG compression. "
1275 "Default is N = 100, maximum quality.");
1276
1277static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect) {
1278 // Filter output of warnings that JPEG is not available for the image.
1279 if (bitmap.width() >= 65500 || bitmap.height() >= 65500) return false;
1280 if (FLAGS_pdfJpegQuality == -1) return false;
1281
1282 SkIRect bitmapBounds;
1283 SkBitmap subset;
1284 const SkBitmap* bitmapToUse = &bitmap;
1285 bitmap.getBounds(&bitmapBounds);
1286 if (rect != bitmapBounds) {
1287 SkAssertResult(bitmap.extractSubset(&subset, rect));
1288 bitmapToUse = &subset;
1289 }
1290
1291#if defined(SK_BUILD_FOR_MAC)
1292 // Workaround bug #1043 where bitmaps with referenced pixels cause
1293 // CGImageDestinationFinalize to crash
1294 SkBitmap copy;
1295 bitmapToUse->deepCopyTo(&copy, bitmapToUse->config());
1296 bitmapToUse = &copy;
1297#endif
1298
1299 return SkImageEncoder::EncodeStream(stream,
1300 *bitmapToUse,
1301 SkImageEncoder::kJPEG_Type,
1302 FLAGS_pdfJpegQuality);
1303}
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001304
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001305static int findConfig(const char config[]) {
1306 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1307 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001308 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001309 }
1310 }
1311 return -1;
1312}
1313
scroggo@google.comb7dbf632013-04-23 15:38:09 +00001314static bool skip_name(SkCommandLineFlags::StringArray array, const char name[]) {
caryclark@google.com512c9b62013-05-10 15:16:13 +00001315 // FIXME: this duplicates the logic in test/skia_test.cpp -- consolidate
1316 int count = array.count();
1317 size_t testLen = strlen(name);
1318 bool anyExclude = count == 0;
reed@google.comb2a51622011-10-31 16:30:04 +00001319 for (int i = 0; i < array.count(); ++i) {
caryclark@google.com512c9b62013-05-10 15:16:13 +00001320 const char* matchName = array[i];
1321 size_t matchLen = strlen(matchName);
1322 bool matchExclude, matchStart, matchEnd;
1323 if ((matchExclude = matchName[0] == '~')) {
1324 anyExclude = true;
1325 matchName++;
1326 matchLen--;
1327 }
1328 if ((matchStart = matchName[0] == '^')) {
1329 matchName++;
1330 matchLen--;
1331 }
1332 if ((matchEnd = matchName[matchLen - 1] == '$')) {
1333 matchLen--;
1334 }
1335 if (matchStart ? (!matchEnd || matchLen == testLen)
1336 && strncmp(name, matchName, matchLen) == 0
1337 : matchEnd ? matchLen <= testLen
1338 && strncmp(name + testLen - matchLen, matchName, matchLen) == 0
1339 : strstr(name, matchName) != 0) {
1340 return matchExclude;
reed@google.comb2a51622011-10-31 16:30:04 +00001341 }
1342 }
caryclark@google.com512c9b62013-05-10 15:16:13 +00001343 return !anyExclude;
reed@google.comb2a51622011-10-31 16:30:04 +00001344}
1345
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001346namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001347#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +00001348SkAutoTUnref<GrContext> gGrContext;
1349/**
bsalomon@google.comc7a24d22012-11-01 18:03:48 +00001350 * Sets the global GrContext, accessible by individual GMs
bsalomon@google.com7361f542012-04-19 19:15:35 +00001351 */
caryclark@google.com13130862012-06-06 12:10:45 +00001352static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +00001353 SkSafeRef(grContext);
1354 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001355}
bsalomon@google.com7361f542012-04-19 19:15:35 +00001356
1357/**
1358 * Gets the global GrContext, can be called by GM tests.
1359 */
caryclark@google.com13130862012-06-06 12:10:45 +00001360GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +00001361GrContext* GetGr() {
1362 return gGrContext.get();
1363}
1364
1365/**
1366 * Sets the global GrContext and then resets it to its previous value at
1367 * destruction.
1368 */
1369class AutoResetGr : SkNoncopyable {
1370public:
1371 AutoResetGr() : fOld(NULL) {}
1372 void set(GrContext* context) {
1373 SkASSERT(NULL == fOld);
1374 fOld = GetGr();
1375 SkSafeRef(fOld);
1376 SetGr(context);
1377 }
1378 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1379private:
1380 GrContext* fOld;
1381};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001382#else
epoger@google.com80724df2013-03-21 13:49:54 +00001383GrContext* GetGr();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001384GrContext* GetGr() { return NULL; }
1385#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001386}
1387
reed@google.comfb2cd422013-01-04 14:43:03 +00001388template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1389 int index = array->find(value);
1390 if (index < 0) {
1391 *array->append() = value;
1392 }
1393}
1394
epoger@google.com80724df2013-03-21 13:49:54 +00001395/**
1396 * Run this test in a number of different configs (8888, 565, PDF,
1397 * etc.), confirming that the resulting bitmaps match expectations
1398 * (which may be different for each config).
1399 *
1400 * Returns all errors encountered while doing so.
1401 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001402ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1403 GrContextFactory *grFactory);
1404ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1405 GrContextFactory *grFactory) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001406 const char renderModeDescriptor[] = "";
epoger@google.com6f6568b2013-03-22 17:29:46 +00001407 ErrorCombination errorsForAllConfigs;
epoger@google.com80724df2013-03-21 13:49:54 +00001408 uint32_t gmFlags = gm->getFlags();
1409
epoger@google.com80724df2013-03-21 13:49:54 +00001410 for (int i = 0; i < configs.count(); i++) {
1411 ConfigData config = gRec[configs[i]];
epoger@google.comc8263e72013-04-10 12:17:34 +00001412 const SkString name = gmmain.make_name(gm->shortName(), config.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001413
1414 // Skip any tests that we don't even need to try.
epoger@google.comc8263e72013-04-10 12:17:34 +00001415 // If any of these were skipped on a per-GM basis, record them as
1416 // kIntentionallySkipped.
1417 if (kPDF_Backend == config.fBackend) {
1418 if (!FLAGS_pdf) {
epoger@google.com80724df2013-03-21 13:49:54 +00001419 continue;
1420 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001421 if (gmFlags & GM::kSkipPDF_Flag) {
1422 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1423 renderModeDescriptor);
1424 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1425 continue;
1426 }
1427 }
epoger@google.com80724df2013-03-21 13:49:54 +00001428 if ((gmFlags & GM::kSkip565_Flag) &&
1429 (kRaster_Backend == config.fBackend) &&
1430 (SkBitmap::kRGB_565_Config == config.fConfig)) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001431 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1432 renderModeDescriptor);
1433 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001434 continue;
1435 }
1436 if ((gmFlags & GM::kSkipGPU_Flag) &&
1437 kGPU_Backend == config.fBackend) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001438 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1439 renderModeDescriptor);
1440 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001441 continue;
1442 }
1443
1444 // Now we know that we want to run this test and record its
1445 // success or failure.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001446 ErrorCombination errorsForThisConfig;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001447 GrSurface* gpuTarget = NULL;
epoger@google.com80724df2013-03-21 13:49:54 +00001448#if SK_SUPPORT_GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001449 SkAutoTUnref<GrSurface> auGpuTarget;
epoger@google.com80724df2013-03-21 13:49:54 +00001450 AutoResetGr autogr;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001451 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
epoger@google.com80724df2013-03-21 13:49:54 +00001452 GrContext* gr = grFactory->get(config.fGLContextType);
1453 bool grSuccess = false;
1454 if (gr) {
1455 // create a render target to back the device
1456 GrTextureDesc desc;
1457 desc.fConfig = kSkia8888_GrPixelConfig;
1458 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1459 desc.fWidth = gm->getISize().width();
1460 desc.fHeight = gm->getISize().height();
1461 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001462 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1463 if (NULL != auGpuTarget) {
1464 gpuTarget = auGpuTarget;
1465 grSuccess = true;
epoger@google.com80724df2013-03-21 13:49:54 +00001466 autogr.set(gr);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001467 // Set the user specified cache limits if non-default.
1468 size_t bytes;
1469 int count;
1470 gr->getTextureCacheLimits(&count, &bytes);
1471 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1472 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1473 }
1474 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1475 count = gGpuCacheSizeCount;
1476 }
1477 gr->setTextureCacheLimits(count, bytes);
epoger@google.com80724df2013-03-21 13:49:54 +00001478 }
epoger@google.com80724df2013-03-21 13:49:54 +00001479 }
1480 if (!grSuccess) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001481 errorsForThisConfig.add(kNoGpuContext_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001482 }
1483 }
1484#endif
1485
1486 SkBitmap comparisonBitmap;
1487
1488 const char* writePath;
1489 if (FLAGS_writePath.count() == 1) {
1490 writePath = FLAGS_writePath[0];
1491 } else {
1492 writePath = NULL;
1493 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001494 if (errorsForThisConfig.isEmpty()) {
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001495 errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
1496 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001497 }
1498
epoger@google.com6f6568b2013-03-22 17:29:46 +00001499 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1500 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1501 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001502 gpuTarget));
epoger@google.com80724df2013-03-21 13:49:54 +00001503 }
1504
epoger@google.com6f6568b2013-03-22 17:29:46 +00001505 errorsForAllConfigs.add(errorsForThisConfig);
epoger@google.com80724df2013-03-21 13:49:54 +00001506 }
1507 return errorsForAllConfigs;
1508}
1509
1510/**
1511 * Run this test in a number of different drawing modes (pipe,
1512 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1513 * *exactly* match comparisonBitmap.
1514 *
1515 * Returns all errors encountered while doing so.
1516 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001517ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1518 const SkBitmap &comparisonBitmap,
1519 const SkTDArray<SkScalar> &tileGridReplayScales);
1520ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1521 const SkBitmap &comparisonBitmap,
1522 const SkTDArray<SkScalar> &tileGridReplayScales) {
1523 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001524 uint32_t gmFlags = gm->getFlags();
epoger@google.comc8263e72013-04-10 12:17:34 +00001525 const SkString name = gmmain.make_name(gm->shortName(), compareConfig.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001526
epoger@google.comc8263e72013-04-10 12:17:34 +00001527 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1528 SkAutoUnref aur(pict);
1529 if (FLAGS_replay) {
1530 const char renderModeDescriptor[] = "-replay";
1531 if (gmFlags & GM::kSkipPicture_Flag) {
1532 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name, renderModeDescriptor);
1533 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1534 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001535 SkBitmap bitmap;
1536 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001537 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1538 name, renderModeDescriptor, bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001539 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001540 }
epoger@google.com80724df2013-03-21 13:49:54 +00001541
epoger@google.comc8263e72013-04-10 12:17:34 +00001542 if (FLAGS_serialize) {
1543 const char renderModeDescriptor[] = "-serialize";
1544 if (gmFlags & GM::kSkipPicture_Flag) {
1545 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name, renderModeDescriptor);
1546 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1547 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001548 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1549 SkAutoUnref aurr(repict);
epoger@google.com80724df2013-03-21 13:49:54 +00001550 SkBitmap bitmap;
1551 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001552 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1553 name, renderModeDescriptor, bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001554 }
epoger@google.com80724df2013-03-21 13:49:54 +00001555 }
1556
epoger@google.comc8263e72013-04-10 12:17:34 +00001557 if ((1 == FLAGS_writePicturePath.count()) &&
1558 !(gmFlags & GM::kSkipPicture_Flag)) {
1559 const char* pictureSuffix = "skp";
1560 SkString path = make_filename(FLAGS_writePicturePath[0], "",
1561 gm->shortName(), pictureSuffix);
1562 SkFILEWStream stream(path.c_str());
1563 pict->serialize(&stream);
epoger@google.com80724df2013-03-21 13:49:54 +00001564 }
1565
epoger@google.comc8263e72013-04-10 12:17:34 +00001566 if (FLAGS_rtree) {
1567 const char renderModeDescriptor[] = "-rtree";
1568 if (gmFlags & GM::kSkipPicture_Flag) {
1569 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name, renderModeDescriptor);
1570 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1571 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001572 SkPicture* pict = gmmain.generate_new_picture(
epoger@google.comc8263e72013-04-10 12:17:34 +00001573 gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
epoger@google.com80724df2013-03-21 13:49:54 +00001574 SkAutoUnref aur(pict);
1575 SkBitmap bitmap;
epoger@google.comc8263e72013-04-10 12:17:34 +00001576 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001577 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.comc8263e72013-04-10 12:17:34 +00001578 name, renderModeDescriptor, bitmap, &comparisonBitmap));
1579 }
1580 }
1581
1582 if (FLAGS_tileGrid) {
1583 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1584 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1585 SkString renderModeDescriptor("-tilegrid");
1586 if (SK_Scalar1 != replayScale) {
1587 renderModeDescriptor += "-scale-";
1588 renderModeDescriptor.appendScalar(replayScale);
1589 }
1590
1591 if ((gmFlags & GM::kSkipPicture_Flag) ||
1592 ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1)) {
1593 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1594 renderModeDescriptor.c_str());
1595 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1596 } else {
1597 // We record with the reciprocal scale to obtain a replay
1598 // result that can be validated against comparisonBitmap.
1599 SkScalar recordScale = SkScalarInvert(replayScale);
1600 SkPicture* pict = gmmain.generate_new_picture(
1601 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag,
1602 recordScale);
1603 SkAutoUnref aur(pict);
1604 SkBitmap bitmap;
1605 // We cannot yet pass 'true' to generate_image_from_picture to
1606 // perform actual tiled rendering (see Issue 1198 -
1607 // https://code.google.com/p/skia/issues/detail?id=1198)
1608 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
1609 replayScale /*, true */);
1610 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1611 name, renderModeDescriptor.c_str(), bitmap, &comparisonBitmap));
1612 }
epoger@google.com80724df2013-03-21 13:49:54 +00001613 }
1614 }
1615
1616 // run the pipe centric GM steps
epoger@google.comc8263e72013-04-10 12:17:34 +00001617 if (FLAGS_pipe) {
1618 errorsForAllModes.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap,
1619 FLAGS_simulatePipePlaybackFailure));
1620 if (FLAGS_tiledPipe) {
1621 errorsForAllModes.add(gmmain.test_tiled_pipe_playback(gm, compareConfig,
1622 comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001623 }
epoger@google.com80724df2013-03-21 13:49:54 +00001624 }
1625 return errorsForAllModes;
1626}
1627
epoger@google.com310478e2013-04-03 18:00:39 +00001628/**
1629 * Return a list of all entries in an array of strings as a single string
1630 * of this form:
1631 * "item1", "item2", "item3"
1632 */
1633SkString list_all(const SkTArray<SkString> &stringArray);
1634SkString list_all(const SkTArray<SkString> &stringArray) {
1635 SkString total;
1636 for (int i = 0; i < stringArray.count(); i++) {
1637 if (i > 0) {
1638 total.append(", ");
1639 }
1640 total.append("\"");
1641 total.append(stringArray[i]);
1642 total.append("\"");
1643 }
1644 return total;
1645}
1646
1647/**
1648 * Return a list of configuration names, as a single string of this form:
1649 * "item1", "item2", "item3"
1650 *
1651 * @param configs configurations, as a list of indices into gRec
1652 */
1653SkString list_all_config_names(const SkTDArray<size_t> &configs);
1654SkString list_all_config_names(const SkTDArray<size_t> &configs) {
1655 SkString total;
1656 for (int i = 0; i < configs.count(); i++) {
1657 if (i > 0) {
1658 total.append(", ");
1659 }
1660 total.append("\"");
1661 total.append(gRec[configs[i]].fName);
1662 total.append("\"");
1663 }
1664 return total;
1665}
1666
epoger@google.comfdea3252013-05-02 18:24:03 +00001667bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1668 const SkTDArray<size_t> &configs);
1669bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1670 const SkTDArray<size_t> &configs) {
1671 if (!sk_mkdir(root)) {
1672 return false;
1673 }
1674 if (useFileHierarchy) {
1675 for (int i = 0; i < configs.count(); i++) {
1676 ConfigData config = gRec[configs[i]];
1677 SkString subdir;
1678 subdir.appendf("%s%c%s", root, SkPATH_SEPARATOR, config.fName);
1679 if (!sk_mkdir(subdir.c_str())) {
1680 return false;
1681 }
1682 }
1683 }
1684 return true;
1685}
1686
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001687int tool_main(int argc, char** argv);
1688int tool_main(int argc, char** argv) {
1689
1690#if SK_ENABLE_INST_COUNT
1691 gPrintInstCount = true;
1692#endif
1693
1694 SkGraphics::Init();
1695 // we don't need to see this during a run
1696 gSkSuppressFontCachePurgeSpew = true;
1697
1698 setSystemPreferences();
1699 GMMain gmmain;
1700
1701 SkTDArray<size_t> configs;
1702 SkTDArray<size_t> excludeConfigs;
1703 bool userConfig = false;
1704
1705 SkString usage;
1706 usage.printf("Run the golden master tests.\n");
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00001707 SkCommandLineFlags::SetUsage(usage.c_str());
1708 SkCommandLineFlags::Parse(argc, argv);
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001709
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001710 gmmain.fUseFileHierarchy = FLAGS_hierarchy;
1711 if (FLAGS_mismatchPath.count() == 1) {
1712 gmmain.fMismatchPath = FLAGS_mismatchPath[0];
1713 }
1714
1715 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001716 const char* config = FLAGS_config[i];
1717 userConfig = true;
1718 bool exclude = false;
1719 if (*config == kExcludeConfigChar) {
1720 exclude = true;
1721 config += 1;
1722 }
1723 int index = findConfig(config);
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001724 if (index >= 0) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001725 if (exclude) {
1726 *excludeConfigs.append() = index;
1727 } else {
1728 appendUnique<size_t>(&configs, index);
1729 }
1730 } else if (0 == strcmp(kDefaultsConfigStr, config)) {
1731 for (size_t c = 0; c < SK_ARRAY_COUNT(gRec); ++c) {
1732 if (gRec[c].fRunByDefault) {
1733 if (exclude) {
1734 gm_fprintf(stderr, "%c%s is not allowed.\n",
1735 kExcludeConfigChar, kDefaultsConfigStr);
1736 return -1;
1737 } else {
1738 appendUnique<size_t>(&configs, c);
1739 }
1740 }
1741 }
scroggo@google.com0b735632013-03-19 17:38:50 +00001742 } else {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001743 gm_fprintf(stderr, "unrecognized config %s\n", config);
scroggo@google.com7d519302013-03-19 17:28:10 +00001744 return -1;
1745 }
1746 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001747
1748 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1749 int index = findConfig(FLAGS_excludeConfig[i]);
1750 if (index >= 0) {
1751 *excludeConfigs.append() = index;
1752 } else {
1753 gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1754 return -1;
1755 }
1756 }
1757
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001758 int moduloRemainder = -1;
1759 int moduloDivisor = -1;
1760
1761 if (FLAGS_modulo.count() == 2) {
1762 moduloRemainder = atoi(FLAGS_modulo[0]);
1763 moduloDivisor = atoi(FLAGS_modulo[1]);
1764 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
1765 gm_fprintf(stderr, "invalid modulo values.");
1766 return -1;
1767 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001768 }
reed@google.com873cb1e2010-12-23 15:00:45 +00001769
epoger@google.com5079d2c2013-04-12 14:11:21 +00001770 if (FLAGS_ignoreErrorTypes.count() > 0) {
1771 gmmain.fIgnorableErrorTypes = ErrorCombination();
1772 for (int i = 0; i < FLAGS_ignoreErrorTypes.count(); i++) {
1773 ErrorType type;
1774 const char *name = FLAGS_ignoreErrorTypes[i];
1775 if (!getErrorTypeByName(name, &type)) {
1776 gm_fprintf(stderr, "cannot find ErrorType with name '%s'\n", name);
1777 return -1;
1778 } else {
1779 gmmain.fIgnorableErrorTypes.add(type);
1780 }
1781 }
1782 }
1783
epoger@google.com6f6568b2013-03-22 17:29:46 +00001784#if SK_SUPPORT_GPU
1785 if (FLAGS_gpuCacheSize.count() > 0) {
1786 if (FLAGS_gpuCacheSize.count() != 2) {
1787 gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
1788 return -1;
1789 }
1790 gGpuCacheSizeBytes = atoi(FLAGS_gpuCacheSize[0]);
1791 gGpuCacheSizeCount = atoi(FLAGS_gpuCacheSize[1]);
1792 } else {
1793 gGpuCacheSizeBytes = DEFAULT_CACHE_VALUE;
1794 gGpuCacheSizeCount = DEFAULT_CACHE_VALUE;
1795 }
1796#endif
1797
1798 SkTDArray<SkScalar> tileGridReplayScales;
1799 *tileGridReplayScales.append() = SK_Scalar1; // By default only test at scale 1.0
1800 if (FLAGS_tileGridReplayScales.count() > 0) {
1801 tileGridReplayScales.reset();
1802 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
1803 double val = atof(FLAGS_tileGridReplayScales[i]);
1804 if (0 < val) {
1805 *tileGridReplayScales.append() = SkDoubleToScalar(val);
1806 }
1807 }
1808 if (0 == tileGridReplayScales.count()) {
1809 // Should have at least one scale
1810 gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
1811 return -1;
1812 }
1813 }
1814
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001815 if (!userConfig) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001816 // if no config is specified by user, add the defaults
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001817 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001818 if (gRec[i].fRunByDefault) {
1819 *configs.append() = i;
1820 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001821 }
1822 }
reed@google.comfb2cd422013-01-04 14:43:03 +00001823 // now remove any explicitly excluded configs
1824 for (int i = 0; i < excludeConfigs.count(); ++i) {
1825 int index = configs.find(excludeConfigs[i]);
1826 if (index >= 0) {
1827 configs.remove(index);
1828 // now assert that there was only one copy in configs[]
1829 SkASSERT(configs.find(excludeConfigs[i]) < 0);
1830 }
1831 }
1832
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001833#if SK_SUPPORT_GPU
1834 GrContextFactory* grFactory = new GrContextFactory;
1835 for (int i = 0; i < configs.count(); ++i) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001836 size_t index = configs[i];
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001837 if (kGPU_Backend == gRec[index].fBackend) {
1838 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1839 if (NULL == ctx) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001840 gm_fprintf(stderr, "GrContext could not be created for config %s."
1841 " Config will be skipped.\n", gRec[index].fName);
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001842 configs.remove(i);
1843 --i;
1844 }
1845 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001846 gm_fprintf(stderr, "Sample count (%d) of config %s is not supported."
1847 " Config will be skipped.\n", gRec[index].fSampleCnt, gRec[index].fName);
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001848 configs.remove(i);
1849 --i;
1850 }
1851 }
1852 }
epoger@google.com80724df2013-03-21 13:49:54 +00001853#else
1854 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001855#endif
1856
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001857 if (configs.isEmpty()) {
1858 gm_fprintf(stderr, "No configs to run.");
1859 return -1;
1860 }
1861
1862 // now show the user the set of configs that will be run.
1863 SkString configStr("These configs will be run: ");
1864 // show the user the config that will run.
1865 for (int i = 0; i < configs.count(); ++i) {
1866 configStr.appendf("%s%s", gRec[configs[i]].fName, (i == configs.count() - 1) ? "\n" : " ");
1867 }
1868 gm_fprintf(stdout, "%s", configStr.c_str());
1869
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001870 if (FLAGS_resourcePath.count() == 1) {
1871 GM::SetResourcePath(FLAGS_resourcePath[0]);
1872 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001873
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001874 if (FLAGS_readPath.count() == 1) {
1875 const char* readPath = FLAGS_readPath[0];
epoger@google.com37269602013-01-19 04:21:27 +00001876 if (!sk_exists(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001877 gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001878 return -1;
1879 }
1880 if (sk_isdir(readPath)) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001881 if (FLAGS_verbose) {
1882 gm_fprintf(stdout, "reading from %s\n", readPath);
1883 }
epoger@google.com37269602013-01-19 04:21:27 +00001884 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
epoger@google.comb0f8b432013-04-10 18:46:25 +00001885 IndividualImageExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001886 } else {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001887 if (FLAGS_verbose) {
1888 gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
1889 }
epoger@google.comd271d242013-02-13 18:14:48 +00001890 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1891 JsonExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001892 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00001893 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00001894 if (FLAGS_verbose) {
1895 if (FLAGS_writePath.count() == 1) {
1896 gm_fprintf(stdout, "writing to %s\n", FLAGS_writePath[0]);
1897 }
epoger@google.comfdea3252013-05-02 18:24:03 +00001898 if (NULL != gmmain.fMismatchPath) {
1899 gm_fprintf(stdout, "writing mismatches to %s\n", gmmain.fMismatchPath);
1900 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00001901 if (FLAGS_writePicturePath.count() == 1) {
1902 gm_fprintf(stdout, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
1903 }
1904 if (FLAGS_resourcePath.count() == 1) {
1905 gm_fprintf(stdout, "reading resources from %s\n", FLAGS_resourcePath[0]);
1906 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001907 }
1908
epoger@google.com82cb65b2012-10-29 18:59:17 +00001909 if (moduloDivisor <= 0) {
1910 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001911 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001912 if (moduloRemainder < 0 || moduloRemainder >= moduloDivisor) {
1913 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001914 }
1915
epoger@google.com310478e2013-04-03 18:00:39 +00001916 int gmsRun = 0;
reed@google.comae7b8f32012-10-18 21:30:57 +00001917 int gmIndex = -1;
1918 SkString moduloStr;
1919
epoger@google.come8ebeb12012-10-29 16:42:11 +00001920 // If we will be writing out files, prepare subdirectories.
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001921 if (FLAGS_writePath.count() == 1) {
epoger@google.comfdea3252013-05-02 18:24:03 +00001922 if (!prepare_subdirectories(FLAGS_writePath[0], gmmain.fUseFileHierarchy, configs)) {
epoger@google.come8ebeb12012-10-29 16:42:11 +00001923 return -1;
1924 }
epoger@google.comfdea3252013-05-02 18:24:03 +00001925 }
1926 if (NULL != gmmain.fMismatchPath) {
1927 if (!prepare_subdirectories(gmmain.fMismatchPath, gmmain.fUseFileHierarchy, configs)) {
1928 return -1;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001929 }
1930 }
1931
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001932 if (FLAGS_pdfJpegQuality < -1 || FLAGS_pdfJpegQuality > 100) {
1933 gm_fprintf(stderr, "%s\n", "pdfJpegQuality must be in [-1 .. 100] range.");
1934 }
1935
bsalomon@google.com7361f542012-04-19 19:15:35 +00001936 Iter iter;
1937 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00001938 while ((gm = iter.next()) != NULL) {
scroggo@google.com7fd2d702013-04-16 19:11:14 +00001939 SkAutoTDelete<GM> adgm(gm);
reed@google.comae7b8f32012-10-18 21:30:57 +00001940 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00001941 if (moduloRemainder >= 0) {
1942 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00001943 continue;
1944 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001945 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00001946 }
1947
reed@google.comece2b022011-07-25 14:28:57 +00001948 const char* shortName = gm->shortName();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001949 if (skip_name(FLAGS_match, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00001950 continue;
1951 }
1952
epoger@google.com310478e2013-04-03 18:00:39 +00001953 gmsRun++;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001954 SkISize size = gm->getISize();
epoger@google.combcbf5aa2013-04-12 02:11:54 +00001955 gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
1956 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00001957
epoger@google.com310478e2013-04-03 18:00:39 +00001958 run_multiple_configs(gmmain, gm, configs, grFactory);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001959
1960 SkBitmap comparisonBitmap;
1961 const ConfigData compareConfig =
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001962 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
epoger@google.com310478e2013-04-03 18:00:39 +00001963 gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001964
epoger@google.com80724df2013-03-21 13:49:54 +00001965 // TODO(epoger): only run this if gmmain.generate_image() succeeded?
1966 // Otherwise, what are we comparing against?
epoger@google.com310478e2013-04-03 18:00:39 +00001967 run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap, tileGridReplayScales);
reed@android.com00dae862009-06-10 15:38:48 +00001968 }
epoger@google.com310478e2013-04-03 18:00:39 +00001969
1970 SkTArray<SkString> modes;
1971 gmmain.GetRenderModesEncountered(modes);
epoger@google.comc8263e72013-04-10 12:17:34 +00001972 bool reportError = false;
1973 if (gmmain.NumSignificantErrors() > 0) {
1974 reportError = true;
1975 }
epoger@google.com51dbabe2013-04-10 15:24:53 +00001976 int expectedNumberOfTests = gmsRun * (configs.count() + modes.count());
epoger@google.com310478e2013-04-03 18:00:39 +00001977
1978 // Output summary to stdout.
epoger@google.com51dbabe2013-04-10 15:24:53 +00001979 if (FLAGS_verbose) {
1980 gm_fprintf(stdout, "Ran %d GMs\n", gmsRun);
1981 gm_fprintf(stdout, "... over %2d configs [%s]\n", configs.count(),
1982 list_all_config_names(configs).c_str());
1983 gm_fprintf(stdout, "... and %2d modes [%s]\n", modes.count(), list_all(modes).c_str());
1984 gm_fprintf(stdout, "... so there should be a total of %d tests.\n", expectedNumberOfTests);
1985 }
1986 gmmain.ListErrors(FLAGS_verbose);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001987
epoger@google.com07947d92013-04-11 15:41:02 +00001988 // TODO(epoger): Enable this check for Android, too, once we resolve
1989 // https://code.google.com/p/skia/issues/detail?id=1222
1990 // ('GM is unexpectedly skipping tests on Android')
1991#ifndef SK_BUILD_FOR_ANDROID
epoger@google.comc8263e72013-04-10 12:17:34 +00001992 if (expectedNumberOfTests != gmmain.fTestsRun) {
1993 gm_fprintf(stderr, "expected %d tests, but ran or skipped %d tests\n",
1994 expectedNumberOfTests, gmmain.fTestsRun);
1995 reportError = true;
1996 }
1997#endif
1998
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001999 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.com76c913d2013-04-26 15:06:44 +00002000 Json::Value root = CreateJsonTree(
2001 gmmain.fJsonExpectedResults,
2002 gmmain.fJsonActualResults_Failed, gmmain.fJsonActualResults_FailureIgnored,
2003 gmmain.fJsonActualResults_NoComparison, gmmain.fJsonActualResults_Succeeded);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002004 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002005 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002006 stream.write(jsonStdString.c_str(), jsonStdString.length());
2007 }
2008
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002009#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002010
robertphillips@google.com59552022012-08-31 13:07:37 +00002011#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002012 for (int i = 0; i < configs.count(); i++) {
2013 ConfigData config = gRec[configs[i]];
2014
epoger@google.comb0f8b432013-04-10 18:46:25 +00002015 if (FLAGS_verbose && (kGPU_Backend == config.fBackend)) {
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002016 GrContext* gr = grFactory->get(config.fGLContextType);
2017
epoger@google.com5efdd0c2013-03-13 14:18:40 +00002018 gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002019 gr->printCacheStats();
2020 }
2021 }
2022#endif
2023
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002024 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002025#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002026 SkGraphics::Term();
2027
epoger@google.comc8263e72013-04-10 12:17:34 +00002028 return (reportError) ? -1 : 0;
reed@android.com00dae862009-06-10 15:38:48 +00002029}
caryclark@google.com5987f582012-10-02 18:33:14 +00002030
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002031void GMMain::installFilter(SkCanvas* canvas) {
2032 if (FLAGS_forceBWtext) {
2033 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
2034 }
2035}
2036
borenet@google.com7158e6a2012-11-01 17:43:44 +00002037#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00002038int main(int argc, char * const argv[]) {
2039 return tool_main(argc, (char**) argv);
2040}
2041#endif