blob: df8d28f952aadfcda0ad9fbf305f16843238d6e0 [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
epoger@google.comce057fe2013-05-14 15:17:46 +0000124// TODO(epoger): Right now, various places in this code assume that all the
125// image files read/written by GM use this file extension.
126// Search for references to this constant to find these assumptions.
127const static char kPNG_FileExtension[] = "png";
128
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000129enum Backend {
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000130 kRaster_Backend,
131 kGPU_Backend,
132 kPDF_Backend,
133 kXPS_Backend,
134};
135
136enum BbhType {
137 kNone_BbhType,
138 kRTree_BbhType,
139 kTileGrid_BbhType,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000140};
141
bsalomon@google.com7361f542012-04-19 19:15:35 +0000142enum ConfigFlags {
143 kNone_ConfigFlag = 0x0,
144 /* Write GM images if a write path is provided. */
145 kWrite_ConfigFlag = 0x1,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000146 /* Read reference GM images if a read path is provided. */
bsalomon@google.com7361f542012-04-19 19:15:35 +0000147 kRead_ConfigFlag = 0x2,
148 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
149};
150
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000151struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000152 SkBitmap::Config fConfig;
153 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000154 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000155 int fSampleCnt; // GPU backend only
156 ConfigFlags fFlags;
157 const char* fName;
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000158 bool fRunByDefault;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000159};
160
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000161class BWTextDrawFilter : public SkDrawFilter {
162public:
reed@google.com971aca72012-11-26 20:26:54 +0000163 virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000164};
reed@google.com971aca72012-11-26 20:26:54 +0000165bool BWTextDrawFilter::filter(SkPaint* p, Type t) {
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000166 if (kText_Type == t) {
167 p->setAntiAlias(false);
168 }
reed@google.com971aca72012-11-26 20:26:54 +0000169 return true;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000170}
171
scroggo@google.com565254b2012-06-28 15:41:32 +0000172struct PipeFlagComboData {
173 const char* name;
174 uint32_t flags;
175};
176
177static PipeFlagComboData gPipeWritingFlagCombos[] = {
178 { "", 0 },
179 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000180 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000181 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000182};
183
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000184static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect);
185
epoger@google.com5079d2c2013-04-12 14:11:21 +0000186const static ErrorCombination kDefaultIgnorableErrorTypes = ErrorCombination()
187 .plus(kMissingExpectations_ErrorType)
188 .plus(kIntentionallySkipped_ErrorType);
189
epoger@google.comde961632012-10-26 18:56:36 +0000190class GMMain {
191public:
epoger@google.com5079d2c2013-04-12 14:11:21 +0000192 GMMain() : fUseFileHierarchy(false), fIgnorableErrorTypes(kDefaultIgnorableErrorTypes),
193 fMismatchPath(NULL), fTestsRun(0), fRenderModesEncountered(1) {}
epoger@google.come8ebeb12012-10-29 16:42:11 +0000194
epoger@google.com659c8c02013-05-21 15:45:45 +0000195 /**
196 * Assemble shortNamePlusConfig from (surprise!) shortName and configName.
197 *
198 * The method for doing so depends on whether we are using hierarchical naming.
199 * For example, shortName "selftest1" and configName "8888" could be assembled into
200 * either "selftest1_8888" or "8888/selftest1".
201 */
202 SkString make_shortname_plus_config(const char *shortName, const char *configName) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000203 SkString name;
epoger@google.com57f7abc2012-11-13 03:41:55 +0000204 if (0 == strlen(configName)) {
205 name.append(shortName);
206 } else if (fUseFileHierarchy) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000207 name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
208 } else {
209 name.appendf("%s_%s", shortName, configName);
210 }
epoger@google.comde961632012-10-26 18:56:36 +0000211 return name;
212 }
213
epoger@google.com659c8c02013-05-21 15:45:45 +0000214 /**
215 * Assemble filename, suitable for writing out the results of a particular test.
216 */
217 SkString make_filename(const char *path,
218 const char *shortName,
219 const char *configName,
220 const char *renderModeDescriptor,
221 const char *suffix) {
222 SkString filename = make_shortname_plus_config(shortName, configName);
223 filename.append(renderModeDescriptor);
224 filename.appendUnichar('.');
225 filename.append(suffix);
226 return SkPathJoin(path, filename.c_str());
227 }
228
epoger@google.com5f6a0072013-01-31 16:30:55 +0000229 /* since PNG insists on unpremultiplying our alpha, we take no
230 precision chances and force all pixels to be 100% opaque,
231 otherwise on compare we may not get a perfect match.
232 */
233 static void force_all_opaque(const SkBitmap& bitmap) {
234 SkBitmap::Config config = bitmap.config();
235 switch (config) {
236 case SkBitmap::kARGB_8888_Config:
237 force_all_opaque_8888(bitmap);
238 break;
239 case SkBitmap::kRGB_565_Config:
240 // nothing to do here; 565 bitmaps are inherently opaque
241 break;
242 default:
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000243 gm_fprintf(stderr, "unsupported bitmap config %d\n", config);
244 DEBUGFAIL_SEE_STDERR;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000245 }
246 }
247
248 static void force_all_opaque_8888(const SkBitmap& bitmap) {
249 SkAutoLockPixels lock(bitmap);
250 for (int y = 0; y < bitmap.height(); y++) {
251 for (int x = 0; x < bitmap.width(); x++) {
252 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
253 }
254 }
255 }
256
257 static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
258 // TODO(epoger): Now that we have removed force_all_opaque()
259 // from this method, we should be able to get rid of the
260 // transformation to 8888 format also.
261 SkBitmap copy;
262 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
263 return SkImageEncoder::EncodeFile(path.c_str(), copy,
264 SkImageEncoder::kPNG_Type, 100);
265 }
266
epoger@google.com6f6568b2013-03-22 17:29:46 +0000267 /**
epoger@google.com310478e2013-04-03 18:00:39 +0000268 * Add all render modes encountered thus far to the "modes" array.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000269 */
epoger@google.com310478e2013-04-03 18:00:39 +0000270 void GetRenderModesEncountered(SkTArray<SkString> &modes) {
271 SkTDict<int>::Iter iter(this->fRenderModesEncountered);
272 const char* mode;
273 while ((mode = iter.next(NULL)) != NULL) {
274 SkString modeAsString = SkString(mode);
275 // TODO(epoger): It seems a bit silly that all of these modes were
276 // recorded with a leading "-" which we have to remove here
277 // (except for mode "", which means plain old original mode).
278 // But that's how renderModeDescriptor has been passed into
279 // compare_test_results_to_reference_bitmap() historically,
280 // and changing that now may affect other parts of our code.
281 if (modeAsString.startsWith("-")) {
282 modeAsString.remove(0, 1);
283 modes.push_back(modeAsString);
284 }
285 }
286 }
287
288 /**
289 * Records the results of this test in fTestsRun and fFailedTests.
290 *
291 * We even record successes, and errors that we regard as
292 * "ignorable"; we can filter them out later.
293 */
epoger@google.com659c8c02013-05-21 15:45:45 +0000294 void RecordTestResults(const ErrorCombination& errorCombination,
295 const SkString& shortNamePlusConfig,
epoger@google.com310478e2013-04-03 18:00:39 +0000296 const char renderModeDescriptor []) {
297 // Things to do regardless of errorCombination.
298 fTestsRun++;
299 int renderModeCount = 0;
300 this->fRenderModesEncountered.find(renderModeDescriptor, &renderModeCount);
301 renderModeCount++;
302 this->fRenderModesEncountered.set(renderModeDescriptor, renderModeCount);
303
epoger@google.com6f6568b2013-03-22 17:29:46 +0000304 if (errorCombination.isEmpty()) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000305 return;
epoger@google.comeb066362013-03-08 09:39:36 +0000306 }
307
epoger@google.com310478e2013-04-03 18:00:39 +0000308 // Things to do only if there is some error condition.
epoger@google.com659c8c02013-05-21 15:45:45 +0000309 SkString fullName = shortNamePlusConfig;
epoger@google.comcaac3db2013-04-04 19:23:11 +0000310 fullName.append(renderModeDescriptor);
epoger@google.com310478e2013-04-03 18:00:39 +0000311 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
312 ErrorType type = static_cast<ErrorType>(typeInt);
313 if (errorCombination.includes(type)) {
314 fFailedTests[type].push_back(fullName);
epoger@google.comf60494b2013-04-03 17:02:53 +0000315 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000316 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000317 }
318
epoger@google.com310478e2013-04-03 18:00:39 +0000319 /**
320 * Return the number of significant (non-ignorable) errors we have
321 * encountered so far.
322 */
323 int NumSignificantErrors() {
324 int significantErrors = 0;
325 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
326 ErrorType type = static_cast<ErrorType>(typeInt);
epoger@google.com5079d2c2013-04-12 14:11:21 +0000327 if (!fIgnorableErrorTypes.includes(type)) {
epoger@google.com310478e2013-04-03 18:00:39 +0000328 significantErrors += fFailedTests[type].count();
329 }
330 }
331 return significantErrors;
332 }
333
334 /**
epoger@google.com51dbabe2013-04-10 15:24:53 +0000335 * Display the summary of results with this ErrorType.
336 *
337 * @param type which ErrorType
338 * @param verbose whether to be all verbose about it
epoger@google.com310478e2013-04-03 18:00:39 +0000339 */
epoger@google.com51dbabe2013-04-10 15:24:53 +0000340 void DisplayResultTypeSummary(ErrorType type, bool verbose) {
epoger@google.com5079d2c2013-04-12 14:11:21 +0000341 bool isIgnorableType = fIgnorableErrorTypes.includes(type);
epoger@google.com51dbabe2013-04-10 15:24:53 +0000342
343 SkString line;
344 if (isIgnorableType) {
345 line.append("[ ] ");
346 } else {
347 line.append("[*] ");
348 }
349
350 SkTArray<SkString> *failedTestsOfThisType = &fFailedTests[type];
351 int count = failedTestsOfThisType->count();
352 line.appendf("%d %s", count, getErrorTypeName(type));
353 if (!isIgnorableType || verbose) {
354 line.append(":");
355 for (int i = 0; i < count; ++i) {
356 line.append(" ");
357 line.append((*failedTestsOfThisType)[i]);
358 }
359 }
360 gm_fprintf(stdout, "%s\n", line.c_str());
361 }
362
363 /**
364 * List contents of fFailedTests to stdout.
365 *
366 * @param verbose whether to be all verbose about it
367 */
368 void ListErrors(bool verbose) {
epoger@google.com310478e2013-04-03 18:00:39 +0000369 // First, print a single summary line.
370 SkString summary;
371 summary.appendf("Ran %d tests:", fTestsRun);
372 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
373 ErrorType type = static_cast<ErrorType>(typeInt);
374 summary.appendf(" %s=%d", getErrorTypeName(type), fFailedTests[type].count());
375 }
376 gm_fprintf(stdout, "%s\n", summary.c_str());
377
378 // Now, for each failure type, list the tests that failed that way.
379 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
epoger@google.com51dbabe2013-04-10 15:24:53 +0000380 this->DisplayResultTypeSummary(static_cast<ErrorType>(typeInt), verbose);
epoger@google.com310478e2013-04-03 18:00:39 +0000381 }
382 gm_fprintf(stdout, "(results marked with [*] will cause nonzero return value)\n");
383 }
384
epoger@google.com5f6a0072013-01-31 16:30:55 +0000385 static bool write_document(const SkString& path,
386 const SkDynamicMemoryWStream& document) {
387 SkFILEWStream stream(path.c_str());
388 SkAutoDataUnref data(document.copyToData());
bungeman@google.com1ca36ec2013-05-06 14:28:43 +0000389 return stream.write(data->data(), data->size());
epoger@google.com5f6a0072013-01-31 16:30:55 +0000390 }
391
epoger@google.com37269602013-01-19 04:21:27 +0000392 /**
epoger@google.com5f6a0072013-01-31 16:30:55 +0000393 * Prepare an SkBitmap to render a GM into.
394 *
395 * After you've rendered the GM into the SkBitmap, you must call
396 * complete_bitmap()!
397 *
398 * @todo thudson 22 April 2011 - could refactor this to take in
399 * a factory to generate the context, always call readPixels()
400 * (logically a noop for rasters, if wasted time), and thus collapse the
401 * GPU special case and also let this be used for SkPicture testing.
402 */
403 static void setup_bitmap(const ConfigData& gRec, SkISize& size,
404 SkBitmap* bitmap) {
405 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
406 bitmap->allocPixels();
407 bitmap->eraseColor(SK_ColorTRANSPARENT);
408 }
409
410 /**
411 * Any finalization steps we need to perform on the SkBitmap after
412 * we have rendered the GM into it.
epoger@google.com37269602013-01-19 04:21:27 +0000413 *
414 * It's too bad that we are throwing away alpha channel data
415 * we could otherwise be examining, but this had always been happening
416 * before... it was buried within the compare() method at
417 * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
418 *
419 * Apparently we need this, at least for bitmaps that are either:
420 * (a) destined to be written out as PNG files, or
421 * (b) compared against bitmaps read in from PNG files
422 * for the reasons described just above the force_all_opaque() method.
423 *
424 * Neglecting to do this led to the difficult-to-diagnose
425 * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
426 * spurious pixel_error messages as of r7258')
427 *
428 * TODO(epoger): Come up with a better solution that allows us to
429 * compare full pixel data, including alpha channel, while still being
430 * robust in the face of transformations to/from PNG files.
431 * Options include:
432 *
433 * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
434 * will be written to, or compared against, PNG files.
435 * PRO: Preserve/compare alpha channel info for the non-PNG cases
436 * (comparing different renderModes in-memory)
epoger@google.com908f5832013-04-12 02:23:55 +0000437 * CON: The bitmaps (and hash digests) for these non-PNG cases would be
epoger@google.com37269602013-01-19 04:21:27 +0000438 * different than those for the PNG-compared cases, and in the
439 * case of a failed renderMode comparison, how would we write the
440 * image to disk for examination?
441 *
epoger@google.com908f5832013-04-12 02:23:55 +0000442 * 2. Always compute image hash digests from PNG format (either
epoger@google.com37269602013-01-19 04:21:27 +0000443 * directly from the the bytes of a PNG file, or capturing the
444 * bytes we would have written to disk if we were writing the
445 * bitmap out as a PNG).
446 * PRO: I think this would allow us to never force opaque, and to
447 * the extent that alpha channel data can be preserved in a PNG
448 * file, we could observe it.
epoger@google.com908f5832013-04-12 02:23:55 +0000449 * CON: If we read a bitmap from disk, we need to take its hash digest
epoger@google.com37269602013-01-19 04:21:27 +0000450 * from the source PNG (we can't compute it from the bitmap we
451 * read out of the PNG, because we will have already premultiplied
452 * the alpha).
453 * CON: Seems wasteful to convert a bitmap to PNG format just to take
epoger@google.com908f5832013-04-12 02:23:55 +0000454 * its hash digest. (Although we're wasting lots of effort already
epoger@google.com37269602013-01-19 04:21:27 +0000455 * calling force_all_opaque().)
456 *
457 * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
458 * so we can transform images back and forth without fear of off-by-one
459 * errors.
460 * CON: Math is hard.
461 *
462 * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
463 * channel), rather than demanding absolute equality.
epoger@google.com908f5832013-04-12 02:23:55 +0000464 * CON: Can't do this with hash digests.
epoger@google.com37269602013-01-19 04:21:27 +0000465 */
epoger@google.com5f6a0072013-01-31 16:30:55 +0000466 static void complete_bitmap(SkBitmap* bitmap) {
467 force_all_opaque(*bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000468 }
469
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000470 static void installFilter(SkCanvas* canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000471
reed@google.comaef73612012-11-16 13:41:45 +0000472 static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000473 SkAutoCanvasRestore acr(canvas, true);
474
475 if (!isPDF) {
476 canvas->concat(gm->getInitialTransform());
477 }
478 installFilter(canvas);
reed@google.comaef73612012-11-16 13:41:45 +0000479 gm->setCanvasIsDeferred(isDeferred);
epoger@google.comde961632012-10-26 18:56:36 +0000480 gm->draw(canvas);
481 canvas->setDrawFilter(NULL);
482 }
483
epoger@google.com6f6568b2013-03-22 17:29:46 +0000484 static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000485 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000486 SkBitmap* bitmap,
487 bool deferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000488 SkISize size (gm->getISize());
489 setup_bitmap(gRec, size, bitmap);
490
491 SkAutoTUnref<SkCanvas> canvas;
492
493 if (gRec.fBackend == kRaster_Backend) {
494 SkAutoTUnref<SkDevice> device(new SkDevice(*bitmap));
495 if (deferred) {
496 canvas.reset(new SkDeferredCanvas(device));
497 } else {
498 canvas.reset(new SkCanvas(device));
499 }
reed@google.comaef73612012-11-16 13:41:45 +0000500 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000501 canvas->flush();
502 }
503#if SK_SUPPORT_GPU
504 else { // GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000505 SkAutoTUnref<SkDevice> device(SkGpuDevice::Create(gpuTarget));
epoger@google.comde961632012-10-26 18:56:36 +0000506 if (deferred) {
507 canvas.reset(new SkDeferredCanvas(device));
508 } else {
509 canvas.reset(new SkCanvas(device));
510 }
reed@google.comaef73612012-11-16 13:41:45 +0000511 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000512 // the device is as large as the current rendertarget, so
513 // we explicitly only readback the amount we expect (in
514 // size) overwrite our previous allocation
515 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
516 size.fHeight);
517 canvas->readPixels(bitmap, 0, 0);
518 }
519#endif
epoger@google.com5f6a0072013-01-31 16:30:55 +0000520 complete_bitmap(bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000521 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000522 }
523
524 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
junov@chromium.orgc938c482012-12-19 15:24:38 +0000525 SkPicture* pict, SkBitmap* bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000526 SkScalar scale = SK_Scalar1,
527 bool tile = false) {
epoger@google.comde961632012-10-26 18:56:36 +0000528 SkISize size = gm->getISize();
529 setup_bitmap(gRec, size, bitmap);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000530
531 if (tile) {
532 // Generate the result image by rendering to tiles and accumulating
533 // the results in 'bitmap'
534
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000535 // This 16x16 tiling matches the settings applied to 'pict' in
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000536 // 'generate_new_picture'
537 SkISize tileSize = SkISize::Make(16, 16);
538
539 SkBitmap tileBM;
540 setup_bitmap(gRec, tileSize, &tileBM);
541 SkCanvas tileCanvas(tileBM);
542 installFilter(&tileCanvas);
543
544 SkCanvas bmpCanvas(*bitmap);
545 SkPaint bmpPaint;
546 bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
547
548 for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
549 for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
550 int saveCount = tileCanvas.save();
551 SkMatrix mat(tileCanvas.getTotalMatrix());
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000552 mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000553 SkIntToScalar(-yTile*tileSize.height()));
554 tileCanvas.setMatrix(mat);
555 pict->draw(&tileCanvas);
556 tileCanvas.flush();
557 tileCanvas.restoreToCount(saveCount);
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000558 bmpCanvas.drawBitmap(tileBM,
559 SkIntToScalar(xTile * tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000560 SkIntToScalar(yTile * tileSize.height()),
561 &bmpPaint);
562 }
563 }
564 } else {
565 SkCanvas canvas(*bitmap);
566 installFilter(&canvas);
567 canvas.scale(scale, scale);
568 canvas.drawPicture(*pict);
569 complete_bitmap(bitmap);
570 }
epoger@google.comde961632012-10-26 18:56:36 +0000571 }
572
573 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
574#ifdef SK_SUPPORT_PDF
575 SkMatrix initialTransform = gm->getInitialTransform();
576 SkISize pageSize = gm->getISize();
577 SkPDFDevice* dev = NULL;
578 if (initialTransform.isIdentity()) {
579 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
580 } else {
581 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
582 SkIntToScalar(pageSize.height()));
583 initialTransform.mapRect(&content);
584 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
585 SkIntToScalar(pageSize.height()));
586 SkISize contentSize =
587 SkISize::Make(SkScalarRoundToInt(content.width()),
588 SkScalarRoundToInt(content.height()));
589 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
590 }
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000591 dev->setDCTEncoder(encode_to_dct_stream);
epoger@google.comde961632012-10-26 18:56:36 +0000592 SkAutoUnref aur(dev);
593
594 SkCanvas c(dev);
reed@google.comaef73612012-11-16 13:41:45 +0000595 invokeGM(gm, &c, true, false);
epoger@google.comde961632012-10-26 18:56:36 +0000596
597 SkPDFDocument doc;
598 doc.appendPage(dev);
599 doc.emitPDF(&pdf);
600#endif
601 }
602
603 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
604#ifdef SK_SUPPORT_XPS
605 SkISize size = gm->getISize();
606
607 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
608 SkIntToScalar(size.height()));
609 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
610 static const SkScalar upm = 72 * inchesPerMeter;
611 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
612 static const SkScalar ppm = 200 * inchesPerMeter;
613 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
614
615 SkXPSDevice* dev = new SkXPSDevice();
616 SkAutoUnref aur(dev);
617
618 SkCanvas c(dev);
619 dev->beginPortfolio(&xps);
620 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000621 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000622 dev->endSheet();
623 dev->endPortfolio();
624
625#endif
626 }
627
epoger@google.com6f6568b2013-03-22 17:29:46 +0000628 ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
epoger@google.com659c8c02013-05-21 15:45:45 +0000629 const char renderModeDescriptor [],
630 const char *shortName, SkBitmap& bitmap,
631 SkDynamicMemoryWStream* document) {
epoger@google.comde961632012-10-26 18:56:36 +0000632 SkString path;
633 bool success = false;
634 if (gRec.fBackend == kRaster_Backend ||
635 gRec.fBackend == kGPU_Backend ||
636 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
637
epoger@google.com659c8c02013-05-21 15:45:45 +0000638 path = make_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.comce057fe2013-05-14 15:17:46 +0000639 kPNG_FileExtension);
epoger@google.comde961632012-10-26 18:56:36 +0000640 success = write_bitmap(path, bitmap);
641 }
642 if (kPDF_Backend == gRec.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000643 path = make_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000644 "pdf");
epoger@google.comde961632012-10-26 18:56:36 +0000645 success = write_document(path, *document);
646 }
647 if (kXPS_Backend == gRec.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000648 path = make_filename(writePath, shortName, gRec.fName, renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000649 "xps");
epoger@google.comde961632012-10-26 18:56:36 +0000650 success = write_document(path, *document);
651 }
652 if (success) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000653 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000654 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000655 gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000656 ErrorCombination errors(kWritingReferenceImage_ErrorType);
epoger@google.com310478e2013-04-03 18:00:39 +0000657 // TODO(epoger): Don't call RecordTestResults() here...
658 // Instead, we should make sure to call RecordTestResults
659 // exactly ONCE per test. (Otherwise, gmmain.fTestsRun
660 // will be incremented twice for this test: once in
661 // compare_test_results_to_stored_expectations() before
662 // that method calls this one, and again here.)
663 //
664 // When we make that change, we should probably add a
665 // WritingReferenceImage test to the gm self-tests.)
epoger@google.com659c8c02013-05-21 15:45:45 +0000666 RecordTestResults(errors, make_shortname_plus_config(shortName, gRec.fName),
667 renderModeDescriptor);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000668 return errors;
epoger@google.comde961632012-10-26 18:56:36 +0000669 }
670 }
671
epoger@google.com37269602013-01-19 04:21:27 +0000672 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000673 * Log more detail about the mistmatch between expectedBitmap and
674 * actualBitmap.
675 */
676 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
677 const char *testName) {
678 const int expectedWidth = expectedBitmap.width();
679 const int expectedHeight = expectedBitmap.height();
680 const int width = actualBitmap.width();
681 const int height = actualBitmap.height();
682 if ((expectedWidth != width) || (expectedHeight != height)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000683 gm_fprintf(stderr, "---- %s: dimension mismatch --"
684 " expected [%d %d], actual [%d %d]\n",
685 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000686 return;
687 }
688
689 if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
690 (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000691 gm_fprintf(stderr, "---- %s: not computing max per-channel"
692 " pixel mismatch because non-8888\n", testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000693 return;
694 }
695
696 SkAutoLockPixels alp0(expectedBitmap);
697 SkAutoLockPixels alp1(actualBitmap);
698 int errR = 0;
699 int errG = 0;
700 int errB = 0;
701 int errA = 0;
702 int differingPixels = 0;
703
704 for (int y = 0; y < height; ++y) {
705 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
706 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
707 for (int x = 0; x < width; ++x) {
708 SkPMColor expectedPixel = *expectedPixelPtr++;
709 SkPMColor actualPixel = *actualPixelPtr++;
710 if (expectedPixel != actualPixel) {
711 differingPixels++;
712 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
713 (int)SkGetPackedR32(actualPixel)));
714 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
715 (int)SkGetPackedG32(actualPixel)));
716 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
717 (int)SkGetPackedB32(actualPixel)));
718 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
719 (int)SkGetPackedA32(actualPixel)));
720 }
721 }
722 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000723 gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
724 " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
725 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000726 }
727
728 /**
epoger@google.com908f5832013-04-12 02:23:55 +0000729 * Compares actual hash digest to expectations, returning the set of errors
epoger@google.com6f6568b2013-03-22 17:29:46 +0000730 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000731 *
732 * If fMismatchPath has been set, and there are pixel diffs, then the
733 * actual bitmap will be written out to a file within fMismatchPath.
734 *
735 * @param expectations what expectations to compare actualBitmap against
736 * @param actualBitmap the image we actually generated
epoger@google.com659c8c02013-05-21 15:45:45 +0000737 * @param shortName name of test, e.g. "selftest1"
738 * @param configName name of config, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000739 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
740 * @param addToJsonSummary whether to add these results (both actual and
epoger@google.comcaac3db2013-04-04 19:23:11 +0000741 * expected) to the JSON summary. Regardless of this setting, if
742 * we find an image mismatch in this test, we will write these
743 * results to the JSON summary. (This is so that we will always
744 * report errors across rendering modes, such as pipe vs tiled.
745 * See https://codereview.chromium.org/13650002/ )
epoger@google.com37269602013-01-19 04:21:27 +0000746 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000747 ErrorCombination compare_to_expectations(Expectations expectations,
748 const SkBitmap& actualBitmap,
epoger@google.com659c8c02013-05-21 15:45:45 +0000749 const char *shortName, const char *configName,
750 const char *renderModeDescriptor,
epoger@google.comcaac3db2013-04-04 19:23:11 +0000751 bool addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000752 ErrorCombination errors;
epoger@google.com908f5832013-04-12 02:23:55 +0000753 SkHashDigest actualBitmapHash;
754 // TODO(epoger): Better handling for error returned by ComputeDigest()?
755 // For now, we just report a digest of 0 in error cases, like before.
756 if (!SkBitmapHasher::ComputeDigest(actualBitmap, &actualBitmapHash)) {
757 actualBitmapHash = 0;
758 }
epoger@google.com659c8c02013-05-21 15:45:45 +0000759 SkString shortNamePlusConfig = make_shortname_plus_config(shortName, configName);
760 SkString completeNameString(shortNamePlusConfig);
epoger@google.com37269602013-01-19 04:21:27 +0000761 completeNameString.append(renderModeDescriptor);
epoger@google.comce057fe2013-05-14 15:17:46 +0000762 completeNameString.append(".");
763 completeNameString.append(kPNG_FileExtension);
epoger@google.com37269602013-01-19 04:21:27 +0000764 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000765
epoger@google.com37269602013-01-19 04:21:27 +0000766 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000767 errors.add(kMissingExpectations_ErrorType);
epoger@google.com908f5832013-04-12 02:23:55 +0000768 } else if (!expectations.match(actualBitmapHash)) {
epoger@google.comcaac3db2013-04-04 19:23:11 +0000769 addToJsonSummary = true;
770 // The error mode we record depends on whether this was running
771 // in a non-standard renderMode.
772 if ('\0' == *renderModeDescriptor) {
773 errors.add(kExpectationsMismatch_ErrorType);
774 } else {
775 errors.add(kRenderModeMismatch_ErrorType);
776 }
epoger@google.com84a18022013-02-01 20:39:15 +0000777
778 // Write out the "actuals" for any mismatches, if we have
779 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000780 if (fMismatchPath) {
781 SkString path =
epoger@google.com659c8c02013-05-21 15:45:45 +0000782 make_filename(fMismatchPath, shortName, configName, renderModeDescriptor,
783 kPNG_FileExtension);
epoger@google.com37269602013-01-19 04:21:27 +0000784 write_bitmap(path, actualBitmap);
785 }
epoger@google.com84a18022013-02-01 20:39:15 +0000786
787 // If we have access to a single expected bitmap, log more
788 // detail about the mismatch.
789 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
790 if (NULL != expectedBitmapPtr) {
791 report_bitmap_diffs(*expectedBitmapPtr, actualBitmap, completeName);
792 }
epoger@google.coma243b222013-01-17 17:54:28 +0000793 }
epoger@google.com659c8c02013-05-21 15:45:45 +0000794 RecordTestResults(errors, shortNamePlusConfig, renderModeDescriptor);
epoger@google.coma243b222013-01-17 17:54:28 +0000795
epoger@google.com37269602013-01-19 04:21:27 +0000796 if (addToJsonSummary) {
epoger@google.com908f5832013-04-12 02:23:55 +0000797 add_actual_results_to_json_summary(completeName, actualBitmapHash, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000798 expectations.ignoreFailure());
799 add_expected_results_to_json_summary(completeName, expectations);
800 }
epoger@google.coma243b222013-01-17 17:54:28 +0000801
epoger@google.com6f6568b2013-03-22 17:29:46 +0000802 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000803 }
804
epoger@google.com37269602013-01-19 04:21:27 +0000805 /**
806 * Add this result to the appropriate JSON collection of actual results,
epoger@google.com76c913d2013-04-26 15:06:44 +0000807 * depending on errors encountered.
epoger@google.com37269602013-01-19 04:21:27 +0000808 */
809 void add_actual_results_to_json_summary(const char testName[],
epoger@google.com76c913d2013-04-26 15:06:44 +0000810 const SkHashDigest& actualResult,
811 ErrorCombination errors,
epoger@google.com37269602013-01-19 04:21:27 +0000812 bool ignoreFailure) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000813 Json::Value jsonActualResults = ActualResultAsJsonValue(actualResult);
814 if (errors.isEmpty()) {
815 this->fJsonActualResults_Succeeded[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000816 } else {
817 if (ignoreFailure) {
818 // TODO: Once we have added the ability to compare
819 // actual results against expectations in a JSON file
820 // (where we can set ignore-failure to either true or
epoger@google.com84a18022013-02-01 20:39:15 +0000821 // false), add test cases that exercise ignored
epoger@google.com6f6568b2013-03-22 17:29:46 +0000822 // failures (both for kMissingExpectations_ErrorType
epoger@google.comcaac3db2013-04-04 19:23:11 +0000823 // and kExpectationsMismatch_ErrorType).
epoger@google.com37269602013-01-19 04:21:27 +0000824 this->fJsonActualResults_FailureIgnored[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000825 jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000826 } else {
epoger@google.com76c913d2013-04-26 15:06:44 +0000827 if (errors.includes(kMissingExpectations_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000828 // TODO: What about the case where there IS an
epoger@google.com908f5832013-04-12 02:23:55 +0000829 // expected image hash digest, but that gm test
epoger@google.com37269602013-01-19 04:21:27 +0000830 // doesn't actually run? For now, those cases
831 // will always be ignored, because gm only looks
832 // at expectations that correspond to gm tests
833 // that were actually run.
834 //
835 // Once we have the ability to express
836 // expectations as a JSON file, we should fix this
837 // (and add a test case for which an expectation
838 // is given but the test is never run).
839 this->fJsonActualResults_NoComparison[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000840 jsonActualResults;
epoger@google.comeb066362013-03-08 09:39:36 +0000841 }
epoger@google.com76c913d2013-04-26 15:06:44 +0000842 if (errors.includes(kExpectationsMismatch_ErrorType) ||
843 errors.includes(kRenderModeMismatch_ErrorType)) {
844 this->fJsonActualResults_Failed[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000845 }
846 }
847 }
848 }
849
850 /**
851 * Add this test to the JSON collection of expected results.
852 */
853 void add_expected_results_to_json_summary(const char testName[],
854 Expectations expectations) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000855 this->fJsonExpectedResults[testName] = expectations.asJsonValue();
epoger@google.com37269602013-01-19 04:21:27 +0000856 }
857
858 /**
859 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
860 *
861 * @param gm which test generated the actualBitmap
862 * @param gRec
863 * @param writePath unless this is NULL, write out actual images into this
864 * directory
865 * @param actualBitmap bitmap generated by this run
866 * @param pdf
867 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000868 ErrorCombination compare_test_results_to_stored_expectations(
epoger@google.com37269602013-01-19 04:21:27 +0000869 GM* gm, const ConfigData& gRec, const char writePath[],
870 SkBitmap& actualBitmap, SkDynamicMemoryWStream* pdf) {
871
epoger@google.com659c8c02013-05-21 15:45:45 +0000872 SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(), gRec.fName);
873 SkString nameWithExtension(shortNamePlusConfig);
874 nameWithExtension.append(".");
875 nameWithExtension.append(kPNG_FileExtension);
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000876
epoger@google.com659c8c02013-05-21 15:45:45 +0000877 ErrorCombination errors;
epoger@google.com6f6568b2013-03-22 17:29:46 +0000878 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
epoger@google.com37269602013-01-19 04:21:27 +0000879 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
880 /*
881 * Get the expected results for this test, as one or more allowed
epoger@google.com908f5832013-04-12 02:23:55 +0000882 * hash digests. The current implementation of expectationsSource
883 * get this by computing the hash digest of a single PNG file on disk.
epoger@google.com37269602013-01-19 04:21:27 +0000884 *
885 * TODO(epoger): This relies on the fact that
886 * force_all_opaque() was called on the bitmap before it
887 * was written to disk as a PNG in the first place. If
epoger@google.com908f5832013-04-12 02:23:55 +0000888 * not, the hash digest returned here may not match the
889 * hash digest of actualBitmap, which *has* been run through
epoger@google.com37269602013-01-19 04:21:27 +0000890 * force_all_opaque().
epoger@google.com5f6a0072013-01-31 16:30:55 +0000891 * See comments above complete_bitmap() for more detail.
epoger@google.com37269602013-01-19 04:21:27 +0000892 */
epoger@google.comce057fe2013-05-14 15:17:46 +0000893 Expectations expectations = expectationsSource->get(nameWithExtension.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000894 errors.add(compare_to_expectations(expectations, actualBitmap,
epoger@google.com659c8c02013-05-21 15:45:45 +0000895 gm->shortName(), gRec.fName, "", true));
epoger@google.com37269602013-01-19 04:21:27 +0000896 } else {
897 // If we are running without expectations, we still want to
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000898 // record the actual results.
epoger@google.com908f5832013-04-12 02:23:55 +0000899 SkHashDigest actualBitmapHash;
900 // TODO(epoger): Better handling for error returned by ComputeDigest()?
901 // For now, we just report a digest of 0 in error cases, like before.
902 if (!SkBitmapHasher::ComputeDigest(actualBitmap, &actualBitmapHash)) {
903 actualBitmapHash = 0;
904 }
epoger@google.comce057fe2013-05-14 15:17:46 +0000905 add_actual_results_to_json_summary(nameWithExtension.c_str(), actualBitmapHash,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000906 ErrorCombination(kMissingExpectations_ErrorType),
epoger@google.com37269602013-01-19 04:21:27 +0000907 false);
epoger@google.com659c8c02013-05-21 15:45:45 +0000908 RecordTestResults(ErrorCombination(kMissingExpectations_ErrorType),
909 shortNamePlusConfig, "");
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000910 }
epoger@google.com37269602013-01-19 04:21:27 +0000911
912 // TODO: Consider moving this into compare_to_expectations(),
913 // similar to fMismatchPath... for now, we don't do that, because
914 // we don't want to write out the actual bitmaps for all
915 // renderModes of all tests! That would be a lot of files.
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000916 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +0000917 errors.add(write_reference_image(gRec, writePath, "", gm->shortName(),
918 actualBitmap, pdf));
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000919 }
epoger@google.com37269602013-01-19 04:21:27 +0000920
epoger@google.com6f6568b2013-03-22 17:29:46 +0000921 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000922 }
923
epoger@google.com37269602013-01-19 04:21:27 +0000924 /**
925 * Compare actualBitmap to referenceBitmap.
926 *
epoger@google.com659c8c02013-05-21 15:45:45 +0000927 * @param shortName test name, e.g. "selftest1"
928 * @param configName configuration name, e.g. "8888"
epoger@google.com37269602013-01-19 04:21:27 +0000929 * @param renderModeDescriptor
930 * @param actualBitmap actual bitmap generated by this run
931 * @param referenceBitmap bitmap we expected to be generated
932 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000933 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +0000934 const char *shortName, const char *configName, const char *renderModeDescriptor,
epoger@google.com37269602013-01-19 04:21:27 +0000935 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
936
937 SkASSERT(referenceBitmap);
epoger@google.com84a18022013-02-01 20:39:15 +0000938 Expectations expectations(*referenceBitmap);
epoger@google.com659c8c02013-05-21 15:45:45 +0000939 return compare_to_expectations(expectations, actualBitmap, shortName,
940 configName, renderModeDescriptor, false);
epoger@google.com37269602013-01-19 04:21:27 +0000941 }
942
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000943 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
944 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +0000945 // Pictures are refcounted so must be on heap
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000946 SkPicture* pict;
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000947 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
948 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +0000949
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000950 if (kTileGrid_BbhType == bbhType) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000951 SkTileGridPicture::TileGridInfo info;
952 info.fMargin.setEmpty();
953 info.fOffset.setZero();
954 info.fTileInterval.set(16, 16);
955 pict = new SkTileGridPicture(width, height, info);
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000956 } else {
957 pict = new SkPicture;
958 }
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000959 if (kNone_BbhType != bbhType) {
960 recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
961 }
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000962 SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +0000963 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +0000964 invokeGM(gm, cv, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000965 pict->endRecording();
966
967 return pict;
968 }
969
970 static SkPicture* stream_to_new_picture(const SkPicture& src) {
971
972 // To do in-memory commiunications with a stream, we need to:
973 // * create a dynamic memory stream
974 // * copy it into a buffer
975 // * create a read stream from it
976 // ?!?!
977
978 SkDynamicMemoryWStream storage;
979 src.serialize(&storage);
980
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000981 size_t streamSize = storage.getOffset();
epoger@google.comde961632012-10-26 18:56:36 +0000982 SkAutoMalloc dstStorage(streamSize);
983 void* dst = dstStorage.get();
984 //char* dst = new char [streamSize];
985 //@todo thudson 22 April 2011 when can we safely delete [] dst?
986 storage.copyTo(dst);
987 SkMemoryStream pictReadback(dst, streamSize);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000988 bool success;
989 // Pass a decoding bitmap function so that the factory GM (which has an SkBitmap with
990 // encoded data) does not fail.
991 SkPicture* retval = new SkPicture (&pictReadback, &success, &SkImageDecoder::DecodeMemory);
epoger@google.comde961632012-10-26 18:56:36 +0000992 return retval;
993 }
994
995 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +0000996 // Depending on flags, possibly compare to an expected image.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000997 ErrorCombination test_drawing(GM* gm,
998 const ConfigData& gRec,
999 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001000 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +00001001 SkBitmap* bitmap) {
epoger@google.comde961632012-10-26 18:56:36 +00001002 SkDynamicMemoryWStream document;
1003
1004 if (gRec.fBackend == kRaster_Backend ||
1005 gRec.fBackend == kGPU_Backend) {
1006 // Early exit if we can't generate the image.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001007 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001008 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +00001009 // TODO: Add a test to exercise what the stdout and
1010 // JSON look like if we get an "early error" while
1011 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +00001012 return errors;
1013 }
1014 } else if (gRec.fBackend == kPDF_Backend) {
1015 generate_pdf(gm, document);
1016#if CAN_IMAGE_PDF
1017 SkAutoDataUnref data(document.copyToData());
1018 SkMemoryStream stream(data->data(), data->size());
1019 SkPDFDocumentToBitmap(&stream, bitmap);
1020#endif
1021 } else if (gRec.fBackend == kXPS_Backend) {
1022 generate_xps(gm, document);
1023 }
epoger@google.com37269602013-01-19 04:21:27 +00001024 return compare_test_results_to_stored_expectations(
1025 gm, gRec, writePath, *bitmap, &document);
epoger@google.comde961632012-10-26 18:56:36 +00001026 }
1027
epoger@google.com6f6568b2013-03-22 17:29:46 +00001028 ErrorCombination test_deferred_drawing(GM* gm,
1029 const ConfigData& gRec,
1030 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001031 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +00001032 SkDynamicMemoryWStream document;
1033
1034 if (gRec.fBackend == kRaster_Backend ||
1035 gRec.fBackend == kGPU_Backend) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001036 const char renderModeDescriptor[] = "-deferred";
epoger@google.comde961632012-10-26 18:56:36 +00001037 SkBitmap bitmap;
1038 // Early exit if we can't generate the image, but this is
1039 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001040 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001041 // TODO(epoger): This logic is the opposite of what is
1042 // described above... if we succeeded in generating the
1043 // -deferred image, we exit early! We should fix this
1044 // ASAP, because it is hiding -deferred errors... but for
1045 // now, I'm leaving the logic as it is so that the
1046 // refactoring change
1047 // https://codereview.chromium.org/12992003/ is unblocked.
1048 //
1049 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
1050 // ('image-surface gm test is failing in "deferred" mode,
1051 // and gm is not reporting the failure')
1052 if (errors.isEmpty()) {
epoger@google.com310478e2013-04-03 18:00:39 +00001053 // TODO(epoger): Report this as a new ErrorType,
1054 // something like kImageGeneration_ErrorType?
epoger@google.com6f6568b2013-03-22 17:29:46 +00001055 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001056 }
epoger@google.com37269602013-01-19 04:21:27 +00001057 return compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001058 gm->shortName(), gRec.fName, renderModeDescriptor, bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001059 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001060 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001061 }
1062
epoger@google.comcaac3db2013-04-04 19:23:11 +00001063 ErrorCombination test_pipe_playback(GM* gm, const ConfigData& gRec,
1064 const SkBitmap& referenceBitmap, bool simulateFailure) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001065 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(),
1066 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001067 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001068 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001069 SkString renderModeDescriptor("-pipe");
1070 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1071
1072 if (gm->getFlags() & GM::kSkipPipe_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001073 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001074 renderModeDescriptor.c_str());
1075 errors.add(kIntentionallySkipped_ErrorType);
1076 } else {
1077 SkBitmap bitmap;
1078 SkISize size = gm->getISize();
1079 setup_bitmap(gRec, size, &bitmap);
1080 SkCanvas canvas(bitmap);
1081 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001082 // Pass a decoding function so the factory GM (which has an SkBitmap
1083 // with encoded data) will not fail playback.
1084 PipeController pipeController(&canvas, &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 if (!simulateFailure) {
1090 invokeGM(gm, pipeCanvas, false, false);
1091 }
1092 complete_bitmap(&bitmap);
1093 writer.endRecording();
1094 errors.add(compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001095 gm->shortName(), gRec.fName, renderModeDescriptor.c_str(), bitmap,
1096 &referenceBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001097 if (!errors.isEmpty()) {
1098 break;
1099 }
epoger@google.comde961632012-10-26 18:56:36 +00001100 }
1101 }
1102 return errors;
1103 }
1104
epoger@google.com6f6568b2013-03-22 17:29:46 +00001105 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
1106 const SkBitmap& referenceBitmap) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001107 const SkString shortNamePlusConfig = make_shortname_plus_config(gm->shortName(),
1108 gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001109 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001110 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001111 SkString renderModeDescriptor("-tiled pipe");
1112 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1113
1114 if ((gm->getFlags() & GM::kSkipPipe_Flag) ||
1115 (gm->getFlags() & GM::kSkipTiled_Flag)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001116 RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001117 renderModeDescriptor.c_str());
1118 errors.add(kIntentionallySkipped_ErrorType);
1119 } else {
1120 SkBitmap bitmap;
1121 SkISize size = gm->getISize();
1122 setup_bitmap(gRec, size, &bitmap);
1123 SkCanvas canvas(bitmap);
1124 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001125 TiledPipeController pipeController(bitmap, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001126 SkGPipeWriter writer;
1127 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001128 gPipeWritingFlagCombos[i].flags,
1129 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001130 invokeGM(gm, pipeCanvas, false, false);
1131 complete_bitmap(&bitmap);
1132 writer.endRecording();
epoger@google.com659c8c02013-05-21 15:45:45 +00001133 errors.add(compare_test_results_to_reference_bitmap(gm->shortName(), gRec.fName,
epoger@google.comc8263e72013-04-10 12:17:34 +00001134 renderModeDescriptor.c_str(),
1135 bitmap, &referenceBitmap));
1136 if (!errors.isEmpty()) {
1137 break;
1138 }
epoger@google.comde961632012-10-26 18:56:36 +00001139 }
1140 }
1141 return errors;
1142 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00001143
1144 //
1145 // member variables.
1146 // They are public for now, to allow easier setting by tool_main().
1147 //
1148
epoger@google.come8ebeb12012-10-29 16:42:11 +00001149 bool fUseFileHierarchy;
epoger@google.com5079d2c2013-04-12 14:11:21 +00001150 ErrorCombination fIgnorableErrorTypes;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001151
junov@chromium.org95146eb2013-01-11 21:04:40 +00001152 const char* fMismatchPath;
1153
epoger@google.com310478e2013-04-03 18:00:39 +00001154 // collection of tests that have failed with each ErrorType
1155 SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
1156 int fTestsRun;
1157 SkTDict<int> fRenderModesEncountered;
epoger@google.com57f7abc2012-11-13 03:41:55 +00001158
epoger@google.com908f5832013-04-12 02:23:55 +00001159 // Where to read expectations (expected image hash digests, etc.) from.
epoger@google.com37269602013-01-19 04:21:27 +00001160 // If unset, we don't do comparisons.
1161 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
1162
1163 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +00001164 Json::Value fJsonExpectedResults;
1165 Json::Value fJsonActualResults_Failed;
1166 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001167 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001168 Json::Value fJsonActualResults_Succeeded;
1169
epoger@google.comde961632012-10-26 18:56:36 +00001170}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +00001171
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001172#if SK_SUPPORT_GPU
1173static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1174#else
1175static const GLContextType kDontCare_GLContextType = 0;
1176#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +00001177
1178// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +00001179// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +00001180static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
1181 kWrite_ConfigFlag;
1182
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001183static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001184 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001185#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 +00001186 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001187#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001188 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
1189#if SK_SUPPORT_GPU
1190 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
1191 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", true },
1192 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
bsalomon@google.com7361f542012-04-19 19:15:35 +00001193 /* The debug context does not generate images */
scroggo@google.com0f567c62013-03-20 15:35:08 +00001194 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", GR_DEBUG},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001195#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001196 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
1197 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001198#endif // SK_ANGLE
1199#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001200 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001201#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001202#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +00001203#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001204 /* 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 +00001205 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +00001206#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001207#ifdef SK_SUPPORT_PDF
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001208 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001209#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001210};
1211
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001212static const char kDefaultsConfigStr[] = "defaults";
1213static const char kExcludeConfigChar = '~';
1214
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001215static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001216 SkString result;
1217 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001218 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001219 SkASSERT(gRec[i].fName != kDefaultsConfigStr);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001220 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001221 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001222 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001223 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001224 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001225 result.append("]\n");
1226 result.appendf("The default value is: \"");
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001227 SkString firstDefault;
1228 SkString allButFirstDefaults;
1229 SkString nonDefault;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001230 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1231 if (gRec[i].fRunByDefault) {
1232 if (i > 0) {
1233 result.append(" ");
1234 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001235 result.append(gRec[i].fName);
1236 if (firstDefault.isEmpty()) {
1237 firstDefault = gRec[i].fName;
1238 } else {
1239 if (!allButFirstDefaults.isEmpty()) {
1240 allButFirstDefaults.append(" ");
1241 }
1242 allButFirstDefaults.append(gRec[i].fName);
1243 }
1244 } else {
1245 nonDefault = gRec[i].fName;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001246 }
1247 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001248 result.append("\"\n");
1249 result.appendf("\"%s\" evaluates to the default set of configs.\n", kDefaultsConfigStr);
1250 result.appendf("Prepending \"%c\" on a config name excludes it from the set of configs to run.\n"
1251 "Exclusions always override inclusions regardless of order.\n",
1252 kExcludeConfigChar);
1253 result.appendf("E.g. \"--config %s %c%s %s\" will run these configs:\n\t%s %s",
1254 kDefaultsConfigStr,
1255 kExcludeConfigChar,
1256 firstDefault.c_str(),
1257 nonDefault.c_str(),
1258 allButFirstDefaults.c_str(),
1259 nonDefault.c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001260 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001261}
scroggo@google.com7d519302013-03-19 17:28:10 +00001262
epoger@google.com6f6568b2013-03-22 17:29:46 +00001263// Macro magic to convert a numeric preprocessor token into a string.
1264// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1265// This should probably be moved into one of our common headers...
1266#define TOSTRING_INTERNAL(x) #x
1267#define TOSTRING(x) TOSTRING_INTERNAL(x)
1268
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001269// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001270DEFINE_string(config, "", configUsage().c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001271DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001272DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1273DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1274#if SK_SUPPORT_GPU
1275DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001276 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1277 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001278#endif
1279DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1280 "when reading/writing files.");
epoger@google.com5079d2c2013-04-12 14:11:21 +00001281DEFINE_string(ignoreErrorTypes, kDefaultIgnorableErrorTypes.asString(" ").c_str(),
1282 "Space-separated list of ErrorTypes that should be ignored. If any *other* error "
1283 "types are encountered, the tool will exit with a nonzero return value.");
caryclark@google.com512c9b62013-05-10 15:16:13 +00001284DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
1285 "Multiple matches may be separated by spaces.\n"
1286 "~ causes a matching test to always be skipped\n"
1287 "^ requires the start of the test to match\n"
1288 "$ requires the end of the test to match\n"
1289 "^ and $ requires an exact match\n"
1290 "If a test does not match any list entry,\n"
1291 "it is skipped unless some list entry starts with ~");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001292DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1293 "pixel mismatches into this directory.");
1294DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1295 "testIndex %% divisor == remainder.");
1296DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1297DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1298DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1299 "any differences between those and the newly generated ones.");
1300DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1301DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1302DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1303DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
epoger@google.comcaac3db2013-04-04 19:23:11 +00001304DEFINE_bool(simulatePipePlaybackFailure, false, "Simulate a rendering failure in pipe mode only.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001305DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1306DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1307DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1308 "factors to be used for tileGrid playback testing. Default value: 1.0");
1309DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
epoger@google.comb0f8b432013-04-10 18:46:25 +00001310DEFINE_bool2(verbose, v, false, "Give more detail (e.g. list all GMs run, more info about "
1311 "each test).");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001312DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
scroggo@google.com604e0c22013-04-09 21:25:46 +00001313DEFINE_string2(writePicturePath, p, "", "Write .skp files into this directory.");
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001314DEFINE_int32(pdfJpegQuality, -1, "Encodes images in JPEG at quality level N, "
1315 "which can be in range 0-100). N = -1 will disable JPEG compression. "
1316 "Default is N = 100, maximum quality.");
1317
1318static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect) {
1319 // Filter output of warnings that JPEG is not available for the image.
1320 if (bitmap.width() >= 65500 || bitmap.height() >= 65500) return false;
1321 if (FLAGS_pdfJpegQuality == -1) return false;
1322
1323 SkIRect bitmapBounds;
1324 SkBitmap subset;
1325 const SkBitmap* bitmapToUse = &bitmap;
1326 bitmap.getBounds(&bitmapBounds);
1327 if (rect != bitmapBounds) {
1328 SkAssertResult(bitmap.extractSubset(&subset, rect));
1329 bitmapToUse = &subset;
1330 }
1331
1332#if defined(SK_BUILD_FOR_MAC)
1333 // Workaround bug #1043 where bitmaps with referenced pixels cause
1334 // CGImageDestinationFinalize to crash
1335 SkBitmap copy;
1336 bitmapToUse->deepCopyTo(&copy, bitmapToUse->config());
1337 bitmapToUse = &copy;
1338#endif
1339
1340 return SkImageEncoder::EncodeStream(stream,
1341 *bitmapToUse,
1342 SkImageEncoder::kJPEG_Type,
1343 FLAGS_pdfJpegQuality);
1344}
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001345
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001346static int findConfig(const char config[]) {
1347 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1348 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001349 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001350 }
1351 }
1352 return -1;
1353}
1354
scroggo@google.comb7dbf632013-04-23 15:38:09 +00001355static bool skip_name(SkCommandLineFlags::StringArray array, const char name[]) {
caryclark@google.com512c9b62013-05-10 15:16:13 +00001356 // FIXME: this duplicates the logic in test/skia_test.cpp -- consolidate
1357 int count = array.count();
1358 size_t testLen = strlen(name);
1359 bool anyExclude = count == 0;
reed@google.comb2a51622011-10-31 16:30:04 +00001360 for (int i = 0; i < array.count(); ++i) {
caryclark@google.com512c9b62013-05-10 15:16:13 +00001361 const char* matchName = array[i];
1362 size_t matchLen = strlen(matchName);
1363 bool matchExclude, matchStart, matchEnd;
1364 if ((matchExclude = matchName[0] == '~')) {
1365 anyExclude = true;
1366 matchName++;
1367 matchLen--;
1368 }
1369 if ((matchStart = matchName[0] == '^')) {
1370 matchName++;
1371 matchLen--;
1372 }
1373 if ((matchEnd = matchName[matchLen - 1] == '$')) {
1374 matchLen--;
1375 }
1376 if (matchStart ? (!matchEnd || matchLen == testLen)
1377 && strncmp(name, matchName, matchLen) == 0
1378 : matchEnd ? matchLen <= testLen
1379 && strncmp(name + testLen - matchLen, matchName, matchLen) == 0
1380 : strstr(name, matchName) != 0) {
1381 return matchExclude;
reed@google.comb2a51622011-10-31 16:30:04 +00001382 }
1383 }
caryclark@google.com512c9b62013-05-10 15:16:13 +00001384 return !anyExclude;
reed@google.comb2a51622011-10-31 16:30:04 +00001385}
1386
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001387namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001388#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +00001389SkAutoTUnref<GrContext> gGrContext;
1390/**
bsalomon@google.comc7a24d22012-11-01 18:03:48 +00001391 * Sets the global GrContext, accessible by individual GMs
bsalomon@google.com7361f542012-04-19 19:15:35 +00001392 */
caryclark@google.com13130862012-06-06 12:10:45 +00001393static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +00001394 SkSafeRef(grContext);
1395 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001396}
bsalomon@google.com7361f542012-04-19 19:15:35 +00001397
1398/**
1399 * Gets the global GrContext, can be called by GM tests.
1400 */
caryclark@google.com13130862012-06-06 12:10:45 +00001401GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +00001402GrContext* GetGr() {
1403 return gGrContext.get();
1404}
1405
1406/**
1407 * Sets the global GrContext and then resets it to its previous value at
1408 * destruction.
1409 */
1410class AutoResetGr : SkNoncopyable {
1411public:
1412 AutoResetGr() : fOld(NULL) {}
1413 void set(GrContext* context) {
1414 SkASSERT(NULL == fOld);
1415 fOld = GetGr();
1416 SkSafeRef(fOld);
1417 SetGr(context);
1418 }
1419 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1420private:
1421 GrContext* fOld;
1422};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001423#else
epoger@google.com80724df2013-03-21 13:49:54 +00001424GrContext* GetGr();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001425GrContext* GetGr() { return NULL; }
1426#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001427}
1428
reed@google.comfb2cd422013-01-04 14:43:03 +00001429template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1430 int index = array->find(value);
1431 if (index < 0) {
1432 *array->append() = value;
1433 }
1434}
1435
epoger@google.com80724df2013-03-21 13:49:54 +00001436/**
1437 * Run this test in a number of different configs (8888, 565, PDF,
1438 * etc.), confirming that the resulting bitmaps match expectations
1439 * (which may be different for each config).
1440 *
1441 * Returns all errors encountered while doing so.
1442 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001443ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1444 GrContextFactory *grFactory);
1445ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1446 GrContextFactory *grFactory) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001447 const char renderModeDescriptor[] = "";
epoger@google.com6f6568b2013-03-22 17:29:46 +00001448 ErrorCombination errorsForAllConfigs;
epoger@google.com80724df2013-03-21 13:49:54 +00001449 uint32_t gmFlags = gm->getFlags();
1450
epoger@google.com80724df2013-03-21 13:49:54 +00001451 for (int i = 0; i < configs.count(); i++) {
1452 ConfigData config = gRec[configs[i]];
epoger@google.com659c8c02013-05-21 15:45:45 +00001453 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->shortName(),
1454 config.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001455
1456 // Skip any tests that we don't even need to try.
epoger@google.comc8263e72013-04-10 12:17:34 +00001457 // If any of these were skipped on a per-GM basis, record them as
1458 // kIntentionallySkipped.
1459 if (kPDF_Backend == config.fBackend) {
1460 if (!FLAGS_pdf) {
epoger@google.com80724df2013-03-21 13:49:54 +00001461 continue;
1462 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001463 if (gmFlags & GM::kSkipPDF_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001464 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001465 renderModeDescriptor);
1466 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1467 continue;
1468 }
1469 }
epoger@google.com80724df2013-03-21 13:49:54 +00001470 if ((gmFlags & GM::kSkip565_Flag) &&
1471 (kRaster_Backend == config.fBackend) &&
1472 (SkBitmap::kRGB_565_Config == config.fConfig)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001473 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001474 renderModeDescriptor);
1475 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001476 continue;
1477 }
1478 if ((gmFlags & GM::kSkipGPU_Flag) &&
1479 kGPU_Backend == config.fBackend) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001480 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001481 renderModeDescriptor);
1482 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001483 continue;
1484 }
1485
1486 // Now we know that we want to run this test and record its
1487 // success or failure.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001488 ErrorCombination errorsForThisConfig;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001489 GrSurface* gpuTarget = NULL;
epoger@google.com80724df2013-03-21 13:49:54 +00001490#if SK_SUPPORT_GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001491 SkAutoTUnref<GrSurface> auGpuTarget;
epoger@google.com80724df2013-03-21 13:49:54 +00001492 AutoResetGr autogr;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001493 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
epoger@google.com80724df2013-03-21 13:49:54 +00001494 GrContext* gr = grFactory->get(config.fGLContextType);
1495 bool grSuccess = false;
1496 if (gr) {
1497 // create a render target to back the device
1498 GrTextureDesc desc;
1499 desc.fConfig = kSkia8888_GrPixelConfig;
1500 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1501 desc.fWidth = gm->getISize().width();
1502 desc.fHeight = gm->getISize().height();
1503 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001504 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1505 if (NULL != auGpuTarget) {
1506 gpuTarget = auGpuTarget;
1507 grSuccess = true;
epoger@google.com80724df2013-03-21 13:49:54 +00001508 autogr.set(gr);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001509 // Set the user specified cache limits if non-default.
1510 size_t bytes;
1511 int count;
1512 gr->getTextureCacheLimits(&count, &bytes);
1513 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1514 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1515 }
1516 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1517 count = gGpuCacheSizeCount;
1518 }
1519 gr->setTextureCacheLimits(count, bytes);
epoger@google.com80724df2013-03-21 13:49:54 +00001520 }
epoger@google.com80724df2013-03-21 13:49:54 +00001521 }
1522 if (!grSuccess) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001523 errorsForThisConfig.add(kNoGpuContext_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001524 }
1525 }
1526#endif
1527
1528 SkBitmap comparisonBitmap;
1529
1530 const char* writePath;
1531 if (FLAGS_writePath.count() == 1) {
1532 writePath = FLAGS_writePath[0];
1533 } else {
1534 writePath = NULL;
1535 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001536 if (errorsForThisConfig.isEmpty()) {
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001537 errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
1538 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001539 }
1540
epoger@google.com6f6568b2013-03-22 17:29:46 +00001541 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1542 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1543 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001544 gpuTarget));
epoger@google.com80724df2013-03-21 13:49:54 +00001545 }
1546
epoger@google.com6f6568b2013-03-22 17:29:46 +00001547 errorsForAllConfigs.add(errorsForThisConfig);
epoger@google.com80724df2013-03-21 13:49:54 +00001548 }
1549 return errorsForAllConfigs;
1550}
1551
1552/**
1553 * Run this test in a number of different drawing modes (pipe,
1554 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1555 * *exactly* match comparisonBitmap.
1556 *
1557 * Returns all errors encountered while doing so.
1558 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001559ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1560 const SkBitmap &comparisonBitmap,
1561 const SkTDArray<SkScalar> &tileGridReplayScales);
1562ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1563 const SkBitmap &comparisonBitmap,
1564 const SkTDArray<SkScalar> &tileGridReplayScales) {
1565 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001566 uint32_t gmFlags = gm->getFlags();
epoger@google.com659c8c02013-05-21 15:45:45 +00001567 const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->shortName(),
1568 compareConfig.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001569
epoger@google.comc8263e72013-04-10 12:17:34 +00001570 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1571 SkAutoUnref aur(pict);
1572 if (FLAGS_replay) {
1573 const char renderModeDescriptor[] = "-replay";
1574 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001575 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1576 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001577 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1578 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001579 SkBitmap bitmap;
1580 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001581 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001582 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1583 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001584 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001585 }
epoger@google.com80724df2013-03-21 13:49:54 +00001586
epoger@google.comc8263e72013-04-10 12:17:34 +00001587 if (FLAGS_serialize) {
1588 const char renderModeDescriptor[] = "-serialize";
1589 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001590 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1591 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001592 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1593 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001594 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1595 SkAutoUnref aurr(repict);
epoger@google.com80724df2013-03-21 13:49:54 +00001596 SkBitmap bitmap;
1597 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001598 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001599 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1600 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001601 }
epoger@google.com80724df2013-03-21 13:49:54 +00001602 }
1603
epoger@google.comc8263e72013-04-10 12:17:34 +00001604 if ((1 == FLAGS_writePicturePath.count()) &&
1605 !(gmFlags & GM::kSkipPicture_Flag)) {
1606 const char* pictureSuffix = "skp";
epoger@google.com659c8c02013-05-21 15:45:45 +00001607 // TODO(epoger): Make sure this still works even though the
1608 // filename now contains the config name (it used to contain
1609 // just the shortName). I think this is actually an
1610 // *improvement*, because now runs with different configs will
1611 // write out their SkPictures to separate files rather than
1612 // overwriting each other. But we should make sure it doesn't
1613 // break anybody.
1614 SkString path = gmmain.make_filename(FLAGS_writePicturePath[0], gm->shortName(),
1615 compareConfig.fName, "", pictureSuffix);
epoger@google.comc8263e72013-04-10 12:17:34 +00001616 SkFILEWStream stream(path.c_str());
1617 pict->serialize(&stream);
epoger@google.com80724df2013-03-21 13:49:54 +00001618 }
1619
epoger@google.comc8263e72013-04-10 12:17:34 +00001620 if (FLAGS_rtree) {
1621 const char renderModeDescriptor[] = "-rtree";
1622 if (gmFlags & GM::kSkipPicture_Flag) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001623 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1624 renderModeDescriptor);
epoger@google.comc8263e72013-04-10 12:17:34 +00001625 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1626 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001627 SkPicture* pict = gmmain.generate_new_picture(
epoger@google.comc8263e72013-04-10 12:17:34 +00001628 gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
epoger@google.com80724df2013-03-21 13:49:54 +00001629 SkAutoUnref aur(pict);
1630 SkBitmap bitmap;
epoger@google.comc8263e72013-04-10 12:17:34 +00001631 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001632 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001633 gm->shortName(), compareConfig.fName, renderModeDescriptor, bitmap,
1634 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001635 }
1636 }
1637
1638 if (FLAGS_tileGrid) {
1639 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1640 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1641 SkString renderModeDescriptor("-tilegrid");
1642 if (SK_Scalar1 != replayScale) {
1643 renderModeDescriptor += "-scale-";
1644 renderModeDescriptor.appendScalar(replayScale);
1645 }
1646
1647 if ((gmFlags & GM::kSkipPicture_Flag) ||
1648 ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1)) {
epoger@google.com659c8c02013-05-21 15:45:45 +00001649 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
epoger@google.comc8263e72013-04-10 12:17:34 +00001650 renderModeDescriptor.c_str());
1651 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1652 } else {
1653 // We record with the reciprocal scale to obtain a replay
1654 // result that can be validated against comparisonBitmap.
1655 SkScalar recordScale = SkScalarInvert(replayScale);
1656 SkPicture* pict = gmmain.generate_new_picture(
1657 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag,
1658 recordScale);
1659 SkAutoUnref aur(pict);
1660 SkBitmap bitmap;
1661 // We cannot yet pass 'true' to generate_image_from_picture to
1662 // perform actual tiled rendering (see Issue 1198 -
1663 // https://code.google.com/p/skia/issues/detail?id=1198)
1664 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
1665 replayScale /*, true */);
1666 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.com659c8c02013-05-21 15:45:45 +00001667 gm->shortName(), compareConfig.fName, renderModeDescriptor.c_str(), bitmap,
1668 &comparisonBitmap));
epoger@google.comc8263e72013-04-10 12:17:34 +00001669 }
epoger@google.com80724df2013-03-21 13:49:54 +00001670 }
1671 }
1672
1673 // run the pipe centric GM steps
epoger@google.comc8263e72013-04-10 12:17:34 +00001674 if (FLAGS_pipe) {
1675 errorsForAllModes.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap,
1676 FLAGS_simulatePipePlaybackFailure));
1677 if (FLAGS_tiledPipe) {
1678 errorsForAllModes.add(gmmain.test_tiled_pipe_playback(gm, compareConfig,
1679 comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001680 }
epoger@google.com80724df2013-03-21 13:49:54 +00001681 }
1682 return errorsForAllModes;
1683}
1684
epoger@google.com310478e2013-04-03 18:00:39 +00001685/**
1686 * Return a list of all entries in an array of strings as a single string
1687 * of this form:
1688 * "item1", "item2", "item3"
1689 */
1690SkString list_all(const SkTArray<SkString> &stringArray);
1691SkString list_all(const SkTArray<SkString> &stringArray) {
1692 SkString total;
1693 for (int i = 0; i < stringArray.count(); i++) {
1694 if (i > 0) {
1695 total.append(", ");
1696 }
1697 total.append("\"");
1698 total.append(stringArray[i]);
1699 total.append("\"");
1700 }
1701 return total;
1702}
1703
1704/**
1705 * Return a list of configuration names, as a single string of this form:
1706 * "item1", "item2", "item3"
1707 *
1708 * @param configs configurations, as a list of indices into gRec
1709 */
1710SkString list_all_config_names(const SkTDArray<size_t> &configs);
1711SkString list_all_config_names(const SkTDArray<size_t> &configs) {
1712 SkString total;
1713 for (int i = 0; i < configs.count(); i++) {
1714 if (i > 0) {
1715 total.append(", ");
1716 }
1717 total.append("\"");
1718 total.append(gRec[configs[i]].fName);
1719 total.append("\"");
1720 }
1721 return total;
1722}
1723
epoger@google.comfdea3252013-05-02 18:24:03 +00001724bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1725 const SkTDArray<size_t> &configs);
1726bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1727 const SkTDArray<size_t> &configs) {
1728 if (!sk_mkdir(root)) {
1729 return false;
1730 }
1731 if (useFileHierarchy) {
1732 for (int i = 0; i < configs.count(); i++) {
1733 ConfigData config = gRec[configs[i]];
1734 SkString subdir;
1735 subdir.appendf("%s%c%s", root, SkPATH_SEPARATOR, config.fName);
1736 if (!sk_mkdir(subdir.c_str())) {
1737 return false;
1738 }
1739 }
1740 }
1741 return true;
1742}
1743
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001744int tool_main(int argc, char** argv);
1745int tool_main(int argc, char** argv) {
1746
1747#if SK_ENABLE_INST_COUNT
1748 gPrintInstCount = true;
1749#endif
1750
1751 SkGraphics::Init();
1752 // we don't need to see this during a run
1753 gSkSuppressFontCachePurgeSpew = true;
1754
1755 setSystemPreferences();
1756 GMMain gmmain;
1757
1758 SkTDArray<size_t> configs;
1759 SkTDArray<size_t> excludeConfigs;
1760 bool userConfig = false;
1761
1762 SkString usage;
1763 usage.printf("Run the golden master tests.\n");
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00001764 SkCommandLineFlags::SetUsage(usage.c_str());
1765 SkCommandLineFlags::Parse(argc, argv);
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001766
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001767 gmmain.fUseFileHierarchy = FLAGS_hierarchy;
1768 if (FLAGS_mismatchPath.count() == 1) {
1769 gmmain.fMismatchPath = FLAGS_mismatchPath[0];
1770 }
1771
1772 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001773 const char* config = FLAGS_config[i];
1774 userConfig = true;
1775 bool exclude = false;
1776 if (*config == kExcludeConfigChar) {
1777 exclude = true;
1778 config += 1;
1779 }
1780 int index = findConfig(config);
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001781 if (index >= 0) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001782 if (exclude) {
1783 *excludeConfigs.append() = index;
1784 } else {
1785 appendUnique<size_t>(&configs, index);
1786 }
1787 } else if (0 == strcmp(kDefaultsConfigStr, config)) {
1788 for (size_t c = 0; c < SK_ARRAY_COUNT(gRec); ++c) {
1789 if (gRec[c].fRunByDefault) {
1790 if (exclude) {
1791 gm_fprintf(stderr, "%c%s is not allowed.\n",
1792 kExcludeConfigChar, kDefaultsConfigStr);
1793 return -1;
1794 } else {
1795 appendUnique<size_t>(&configs, c);
1796 }
1797 }
1798 }
scroggo@google.com0b735632013-03-19 17:38:50 +00001799 } else {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001800 gm_fprintf(stderr, "unrecognized config %s\n", config);
scroggo@google.com7d519302013-03-19 17:28:10 +00001801 return -1;
1802 }
1803 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001804
1805 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1806 int index = findConfig(FLAGS_excludeConfig[i]);
1807 if (index >= 0) {
1808 *excludeConfigs.append() = index;
1809 } else {
1810 gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1811 return -1;
1812 }
1813 }
1814
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001815 int moduloRemainder = -1;
1816 int moduloDivisor = -1;
1817
1818 if (FLAGS_modulo.count() == 2) {
1819 moduloRemainder = atoi(FLAGS_modulo[0]);
1820 moduloDivisor = atoi(FLAGS_modulo[1]);
1821 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
1822 gm_fprintf(stderr, "invalid modulo values.");
1823 return -1;
1824 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001825 }
reed@google.com873cb1e2010-12-23 15:00:45 +00001826
epoger@google.com5079d2c2013-04-12 14:11:21 +00001827 if (FLAGS_ignoreErrorTypes.count() > 0) {
1828 gmmain.fIgnorableErrorTypes = ErrorCombination();
1829 for (int i = 0; i < FLAGS_ignoreErrorTypes.count(); i++) {
1830 ErrorType type;
1831 const char *name = FLAGS_ignoreErrorTypes[i];
1832 if (!getErrorTypeByName(name, &type)) {
1833 gm_fprintf(stderr, "cannot find ErrorType with name '%s'\n", name);
1834 return -1;
1835 } else {
1836 gmmain.fIgnorableErrorTypes.add(type);
1837 }
1838 }
1839 }
1840
epoger@google.com6f6568b2013-03-22 17:29:46 +00001841#if SK_SUPPORT_GPU
1842 if (FLAGS_gpuCacheSize.count() > 0) {
1843 if (FLAGS_gpuCacheSize.count() != 2) {
1844 gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
1845 return -1;
1846 }
1847 gGpuCacheSizeBytes = atoi(FLAGS_gpuCacheSize[0]);
1848 gGpuCacheSizeCount = atoi(FLAGS_gpuCacheSize[1]);
1849 } else {
1850 gGpuCacheSizeBytes = DEFAULT_CACHE_VALUE;
1851 gGpuCacheSizeCount = DEFAULT_CACHE_VALUE;
1852 }
1853#endif
1854
1855 SkTDArray<SkScalar> tileGridReplayScales;
1856 *tileGridReplayScales.append() = SK_Scalar1; // By default only test at scale 1.0
1857 if (FLAGS_tileGridReplayScales.count() > 0) {
1858 tileGridReplayScales.reset();
1859 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
1860 double val = atof(FLAGS_tileGridReplayScales[i]);
1861 if (0 < val) {
1862 *tileGridReplayScales.append() = SkDoubleToScalar(val);
1863 }
1864 }
1865 if (0 == tileGridReplayScales.count()) {
1866 // Should have at least one scale
1867 gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
1868 return -1;
1869 }
1870 }
1871
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001872 if (!userConfig) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001873 // if no config is specified by user, add the defaults
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001874 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001875 if (gRec[i].fRunByDefault) {
1876 *configs.append() = i;
1877 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001878 }
1879 }
reed@google.comfb2cd422013-01-04 14:43:03 +00001880 // now remove any explicitly excluded configs
1881 for (int i = 0; i < excludeConfigs.count(); ++i) {
1882 int index = configs.find(excludeConfigs[i]);
1883 if (index >= 0) {
1884 configs.remove(index);
1885 // now assert that there was only one copy in configs[]
1886 SkASSERT(configs.find(excludeConfigs[i]) < 0);
1887 }
1888 }
1889
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001890#if SK_SUPPORT_GPU
1891 GrContextFactory* grFactory = new GrContextFactory;
1892 for (int i = 0; i < configs.count(); ++i) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001893 size_t index = configs[i];
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001894 if (kGPU_Backend == gRec[index].fBackend) {
1895 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1896 if (NULL == ctx) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001897 gm_fprintf(stderr, "GrContext could not be created for config %s."
1898 " Config will be skipped.\n", gRec[index].fName);
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001899 configs.remove(i);
1900 --i;
1901 }
1902 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001903 gm_fprintf(stderr, "Sample count (%d) of config %s is not supported."
1904 " Config will be skipped.\n", gRec[index].fSampleCnt, gRec[index].fName);
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001905 configs.remove(i);
1906 --i;
1907 }
1908 }
1909 }
epoger@google.com80724df2013-03-21 13:49:54 +00001910#else
1911 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001912#endif
1913
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001914 if (configs.isEmpty()) {
1915 gm_fprintf(stderr, "No configs to run.");
1916 return -1;
1917 }
1918
1919 // now show the user the set of configs that will be run.
1920 SkString configStr("These configs will be run: ");
1921 // show the user the config that will run.
1922 for (int i = 0; i < configs.count(); ++i) {
1923 configStr.appendf("%s%s", gRec[configs[i]].fName, (i == configs.count() - 1) ? "\n" : " ");
1924 }
1925 gm_fprintf(stdout, "%s", configStr.c_str());
1926
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001927 if (FLAGS_resourcePath.count() == 1) {
1928 GM::SetResourcePath(FLAGS_resourcePath[0]);
1929 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001930
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001931 if (FLAGS_readPath.count() == 1) {
1932 const char* readPath = FLAGS_readPath[0];
epoger@google.com37269602013-01-19 04:21:27 +00001933 if (!sk_exists(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001934 gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001935 return -1;
1936 }
1937 if (sk_isdir(readPath)) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001938 if (FLAGS_verbose) {
1939 gm_fprintf(stdout, "reading from %s\n", readPath);
1940 }
epoger@google.com37269602013-01-19 04:21:27 +00001941 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
epoger@google.comb0f8b432013-04-10 18:46:25 +00001942 IndividualImageExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001943 } else {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001944 if (FLAGS_verbose) {
1945 gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
1946 }
epoger@google.comd271d242013-02-13 18:14:48 +00001947 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1948 JsonExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001949 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00001950 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00001951 if (FLAGS_verbose) {
1952 if (FLAGS_writePath.count() == 1) {
1953 gm_fprintf(stdout, "writing to %s\n", FLAGS_writePath[0]);
1954 }
epoger@google.comfdea3252013-05-02 18:24:03 +00001955 if (NULL != gmmain.fMismatchPath) {
1956 gm_fprintf(stdout, "writing mismatches to %s\n", gmmain.fMismatchPath);
1957 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00001958 if (FLAGS_writePicturePath.count() == 1) {
1959 gm_fprintf(stdout, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
1960 }
1961 if (FLAGS_resourcePath.count() == 1) {
1962 gm_fprintf(stdout, "reading resources from %s\n", FLAGS_resourcePath[0]);
1963 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001964 }
1965
epoger@google.com82cb65b2012-10-29 18:59:17 +00001966 if (moduloDivisor <= 0) {
1967 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001968 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001969 if (moduloRemainder < 0 || moduloRemainder >= moduloDivisor) {
1970 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001971 }
1972
epoger@google.com310478e2013-04-03 18:00:39 +00001973 int gmsRun = 0;
reed@google.comae7b8f32012-10-18 21:30:57 +00001974 int gmIndex = -1;
1975 SkString moduloStr;
1976
epoger@google.come8ebeb12012-10-29 16:42:11 +00001977 // If we will be writing out files, prepare subdirectories.
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001978 if (FLAGS_writePath.count() == 1) {
epoger@google.comfdea3252013-05-02 18:24:03 +00001979 if (!prepare_subdirectories(FLAGS_writePath[0], gmmain.fUseFileHierarchy, configs)) {
epoger@google.come8ebeb12012-10-29 16:42:11 +00001980 return -1;
1981 }
epoger@google.comfdea3252013-05-02 18:24:03 +00001982 }
1983 if (NULL != gmmain.fMismatchPath) {
1984 if (!prepare_subdirectories(gmmain.fMismatchPath, gmmain.fUseFileHierarchy, configs)) {
1985 return -1;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001986 }
1987 }
1988
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001989 if (FLAGS_pdfJpegQuality < -1 || FLAGS_pdfJpegQuality > 100) {
1990 gm_fprintf(stderr, "%s\n", "pdfJpegQuality must be in [-1 .. 100] range.");
1991 }
1992
bsalomon@google.com7361f542012-04-19 19:15:35 +00001993 Iter iter;
1994 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00001995 while ((gm = iter.next()) != NULL) {
scroggo@google.com7fd2d702013-04-16 19:11:14 +00001996 SkAutoTDelete<GM> adgm(gm);
reed@google.comae7b8f32012-10-18 21:30:57 +00001997 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00001998 if (moduloRemainder >= 0) {
1999 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00002000 continue;
2001 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00002002 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00002003 }
2004
reed@google.comece2b022011-07-25 14:28:57 +00002005 const char* shortName = gm->shortName();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002006 if (skip_name(FLAGS_match, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00002007 continue;
2008 }
2009
epoger@google.com310478e2013-04-03 18:00:39 +00002010 gmsRun++;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00002011 SkISize size = gm->getISize();
epoger@google.combcbf5aa2013-04-12 02:11:54 +00002012 gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
2013 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00002014
epoger@google.com310478e2013-04-03 18:00:39 +00002015 run_multiple_configs(gmmain, gm, configs, grFactory);
djsollen@google.comebce16d2012-10-26 14:07:13 +00002016
2017 SkBitmap comparisonBitmap;
2018 const ConfigData compareConfig =
bsalomon@google.com4c75f242013-03-19 18:58:43 +00002019 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
epoger@google.com310478e2013-04-03 18:00:39 +00002020 gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false);
djsollen@google.comebce16d2012-10-26 14:07:13 +00002021
epoger@google.com80724df2013-03-21 13:49:54 +00002022 // TODO(epoger): only run this if gmmain.generate_image() succeeded?
2023 // Otherwise, what are we comparing against?
epoger@google.com310478e2013-04-03 18:00:39 +00002024 run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap, tileGridReplayScales);
reed@android.com00dae862009-06-10 15:38:48 +00002025 }
epoger@google.com310478e2013-04-03 18:00:39 +00002026
2027 SkTArray<SkString> modes;
2028 gmmain.GetRenderModesEncountered(modes);
epoger@google.comc8263e72013-04-10 12:17:34 +00002029 bool reportError = false;
2030 if (gmmain.NumSignificantErrors() > 0) {
2031 reportError = true;
2032 }
epoger@google.com51dbabe2013-04-10 15:24:53 +00002033 int expectedNumberOfTests = gmsRun * (configs.count() + modes.count());
epoger@google.com310478e2013-04-03 18:00:39 +00002034
2035 // Output summary to stdout.
epoger@google.com51dbabe2013-04-10 15:24:53 +00002036 if (FLAGS_verbose) {
2037 gm_fprintf(stdout, "Ran %d GMs\n", gmsRun);
2038 gm_fprintf(stdout, "... over %2d configs [%s]\n", configs.count(),
2039 list_all_config_names(configs).c_str());
2040 gm_fprintf(stdout, "... and %2d modes [%s]\n", modes.count(), list_all(modes).c_str());
2041 gm_fprintf(stdout, "... so there should be a total of %d tests.\n", expectedNumberOfTests);
2042 }
2043 gmmain.ListErrors(FLAGS_verbose);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002044
epoger@google.com07947d92013-04-11 15:41:02 +00002045 // TODO(epoger): Enable this check for Android, too, once we resolve
2046 // https://code.google.com/p/skia/issues/detail?id=1222
2047 // ('GM is unexpectedly skipping tests on Android')
2048#ifndef SK_BUILD_FOR_ANDROID
epoger@google.comc8263e72013-04-10 12:17:34 +00002049 if (expectedNumberOfTests != gmmain.fTestsRun) {
2050 gm_fprintf(stderr, "expected %d tests, but ran or skipped %d tests\n",
2051 expectedNumberOfTests, gmmain.fTestsRun);
2052 reportError = true;
2053 }
2054#endif
2055
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002056 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.com76c913d2013-04-26 15:06:44 +00002057 Json::Value root = CreateJsonTree(
2058 gmmain.fJsonExpectedResults,
2059 gmmain.fJsonActualResults_Failed, gmmain.fJsonActualResults_FailureIgnored,
2060 gmmain.fJsonActualResults_NoComparison, gmmain.fJsonActualResults_Succeeded);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002061 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002062 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002063 stream.write(jsonStdString.c_str(), jsonStdString.length());
2064 }
2065
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002066#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002067
robertphillips@google.com59552022012-08-31 13:07:37 +00002068#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002069 for (int i = 0; i < configs.count(); i++) {
2070 ConfigData config = gRec[configs[i]];
2071
epoger@google.comb0f8b432013-04-10 18:46:25 +00002072 if (FLAGS_verbose && (kGPU_Backend == config.fBackend)) {
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002073 GrContext* gr = grFactory->get(config.fGLContextType);
2074
epoger@google.com5efdd0c2013-03-13 14:18:40 +00002075 gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002076 gr->printCacheStats();
2077 }
2078 }
2079#endif
2080
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002081 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002082#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002083 SkGraphics::Term();
2084
epoger@google.comc8263e72013-04-10 12:17:34 +00002085 return (reportError) ? -1 : 0;
reed@android.com00dae862009-06-10 15:38:48 +00002086}
caryclark@google.com5987f582012-10-02 18:33:14 +00002087
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002088void GMMain::installFilter(SkCanvas* canvas) {
2089 if (FLAGS_forceBWtext) {
2090 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
2091 }
2092}
2093
borenet@google.com7158e6a2012-11-01 17:43:44 +00002094#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00002095int main(int argc, char * const argv[]) {
2096 return tool_main(argc, (char**) argv);
2097}
2098#endif