blob: cd9b84f140b5536af445990b740b080e8c468d24 [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
195 SkString make_name(const char shortName[], const char configName[]) {
196 SkString name;
epoger@google.com57f7abc2012-11-13 03:41:55 +0000197 if (0 == strlen(configName)) {
198 name.append(shortName);
199 } else if (fUseFileHierarchy) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000200 name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
201 } else {
202 name.appendf("%s_%s", shortName, configName);
203 }
epoger@google.comde961632012-10-26 18:56:36 +0000204 return name;
205 }
206
epoger@google.com5f6a0072013-01-31 16:30:55 +0000207 /* since PNG insists on unpremultiplying our alpha, we take no
208 precision chances and force all pixels to be 100% opaque,
209 otherwise on compare we may not get a perfect match.
210 */
211 static void force_all_opaque(const SkBitmap& bitmap) {
212 SkBitmap::Config config = bitmap.config();
213 switch (config) {
214 case SkBitmap::kARGB_8888_Config:
215 force_all_opaque_8888(bitmap);
216 break;
217 case SkBitmap::kRGB_565_Config:
218 // nothing to do here; 565 bitmaps are inherently opaque
219 break;
220 default:
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000221 gm_fprintf(stderr, "unsupported bitmap config %d\n", config);
222 DEBUGFAIL_SEE_STDERR;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000223 }
224 }
225
226 static void force_all_opaque_8888(const SkBitmap& bitmap) {
227 SkAutoLockPixels lock(bitmap);
228 for (int y = 0; y < bitmap.height(); y++) {
229 for (int x = 0; x < bitmap.width(); x++) {
230 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
231 }
232 }
233 }
234
235 static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
236 // TODO(epoger): Now that we have removed force_all_opaque()
237 // from this method, we should be able to get rid of the
238 // transformation to 8888 format also.
239 SkBitmap copy;
240 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
241 return SkImageEncoder::EncodeFile(path.c_str(), copy,
242 SkImageEncoder::kPNG_Type, 100);
243 }
244
epoger@google.com6f6568b2013-03-22 17:29:46 +0000245 /**
epoger@google.com310478e2013-04-03 18:00:39 +0000246 * Add all render modes encountered thus far to the "modes" array.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000247 */
epoger@google.com310478e2013-04-03 18:00:39 +0000248 void GetRenderModesEncountered(SkTArray<SkString> &modes) {
249 SkTDict<int>::Iter iter(this->fRenderModesEncountered);
250 const char* mode;
251 while ((mode = iter.next(NULL)) != NULL) {
252 SkString modeAsString = SkString(mode);
253 // TODO(epoger): It seems a bit silly that all of these modes were
254 // recorded with a leading "-" which we have to remove here
255 // (except for mode "", which means plain old original mode).
256 // But that's how renderModeDescriptor has been passed into
257 // compare_test_results_to_reference_bitmap() historically,
258 // and changing that now may affect other parts of our code.
259 if (modeAsString.startsWith("-")) {
260 modeAsString.remove(0, 1);
261 modes.push_back(modeAsString);
262 }
263 }
264 }
265
266 /**
267 * Records the results of this test in fTestsRun and fFailedTests.
268 *
269 * We even record successes, and errors that we regard as
270 * "ignorable"; we can filter them out later.
271 */
272 void RecordTestResults(const ErrorCombination& errorCombination, const SkString& name,
273 const char renderModeDescriptor []) {
274 // Things to do regardless of errorCombination.
275 fTestsRun++;
276 int renderModeCount = 0;
277 this->fRenderModesEncountered.find(renderModeDescriptor, &renderModeCount);
278 renderModeCount++;
279 this->fRenderModesEncountered.set(renderModeDescriptor, renderModeCount);
280
epoger@google.com6f6568b2013-03-22 17:29:46 +0000281 if (errorCombination.isEmpty()) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000282 return;
epoger@google.comeb066362013-03-08 09:39:36 +0000283 }
284
epoger@google.com310478e2013-04-03 18:00:39 +0000285 // Things to do only if there is some error condition.
epoger@google.comcaac3db2013-04-04 19:23:11 +0000286 SkString fullName = name;
287 fullName.append(renderModeDescriptor);
epoger@google.com310478e2013-04-03 18:00:39 +0000288 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
289 ErrorType type = static_cast<ErrorType>(typeInt);
290 if (errorCombination.includes(type)) {
291 fFailedTests[type].push_back(fullName);
epoger@google.comf60494b2013-04-03 17:02:53 +0000292 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000293 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000294 }
295
epoger@google.com310478e2013-04-03 18:00:39 +0000296 /**
297 * Return the number of significant (non-ignorable) errors we have
298 * encountered so far.
299 */
300 int NumSignificantErrors() {
301 int significantErrors = 0;
302 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
303 ErrorType type = static_cast<ErrorType>(typeInt);
epoger@google.com5079d2c2013-04-12 14:11:21 +0000304 if (!fIgnorableErrorTypes.includes(type)) {
epoger@google.com310478e2013-04-03 18:00:39 +0000305 significantErrors += fFailedTests[type].count();
306 }
307 }
308 return significantErrors;
309 }
310
311 /**
epoger@google.com51dbabe2013-04-10 15:24:53 +0000312 * Display the summary of results with this ErrorType.
313 *
314 * @param type which ErrorType
315 * @param verbose whether to be all verbose about it
epoger@google.com310478e2013-04-03 18:00:39 +0000316 */
epoger@google.com51dbabe2013-04-10 15:24:53 +0000317 void DisplayResultTypeSummary(ErrorType type, bool verbose) {
epoger@google.com5079d2c2013-04-12 14:11:21 +0000318 bool isIgnorableType = fIgnorableErrorTypes.includes(type);
epoger@google.com51dbabe2013-04-10 15:24:53 +0000319
320 SkString line;
321 if (isIgnorableType) {
322 line.append("[ ] ");
323 } else {
324 line.append("[*] ");
325 }
326
327 SkTArray<SkString> *failedTestsOfThisType = &fFailedTests[type];
328 int count = failedTestsOfThisType->count();
329 line.appendf("%d %s", count, getErrorTypeName(type));
330 if (!isIgnorableType || verbose) {
331 line.append(":");
332 for (int i = 0; i < count; ++i) {
333 line.append(" ");
334 line.append((*failedTestsOfThisType)[i]);
335 }
336 }
337 gm_fprintf(stdout, "%s\n", line.c_str());
338 }
339
340 /**
341 * List contents of fFailedTests to stdout.
342 *
343 * @param verbose whether to be all verbose about it
344 */
345 void ListErrors(bool verbose) {
epoger@google.com310478e2013-04-03 18:00:39 +0000346 // First, print a single summary line.
347 SkString summary;
348 summary.appendf("Ran %d tests:", fTestsRun);
349 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
350 ErrorType type = static_cast<ErrorType>(typeInt);
351 summary.appendf(" %s=%d", getErrorTypeName(type), fFailedTests[type].count());
352 }
353 gm_fprintf(stdout, "%s\n", summary.c_str());
354
355 // Now, for each failure type, list the tests that failed that way.
356 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
epoger@google.com51dbabe2013-04-10 15:24:53 +0000357 this->DisplayResultTypeSummary(static_cast<ErrorType>(typeInt), verbose);
epoger@google.com310478e2013-04-03 18:00:39 +0000358 }
359 gm_fprintf(stdout, "(results marked with [*] will cause nonzero return value)\n");
360 }
361
epoger@google.com5f6a0072013-01-31 16:30:55 +0000362 static bool write_document(const SkString& path,
363 const SkDynamicMemoryWStream& document) {
364 SkFILEWStream stream(path.c_str());
365 SkAutoDataUnref data(document.copyToData());
bungeman@google.com1ca36ec2013-05-06 14:28:43 +0000366 return stream.write(data->data(), data->size());
epoger@google.com5f6a0072013-01-31 16:30:55 +0000367 }
368
epoger@google.com37269602013-01-19 04:21:27 +0000369 /**
epoger@google.com5f6a0072013-01-31 16:30:55 +0000370 * Prepare an SkBitmap to render a GM into.
371 *
372 * After you've rendered the GM into the SkBitmap, you must call
373 * complete_bitmap()!
374 *
375 * @todo thudson 22 April 2011 - could refactor this to take in
376 * a factory to generate the context, always call readPixels()
377 * (logically a noop for rasters, if wasted time), and thus collapse the
378 * GPU special case and also let this be used for SkPicture testing.
379 */
380 static void setup_bitmap(const ConfigData& gRec, SkISize& size,
381 SkBitmap* bitmap) {
382 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
383 bitmap->allocPixels();
384 bitmap->eraseColor(SK_ColorTRANSPARENT);
385 }
386
387 /**
388 * Any finalization steps we need to perform on the SkBitmap after
389 * we have rendered the GM into it.
epoger@google.com37269602013-01-19 04:21:27 +0000390 *
391 * It's too bad that we are throwing away alpha channel data
392 * we could otherwise be examining, but this had always been happening
393 * before... it was buried within the compare() method at
394 * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
395 *
396 * Apparently we need this, at least for bitmaps that are either:
397 * (a) destined to be written out as PNG files, or
398 * (b) compared against bitmaps read in from PNG files
399 * for the reasons described just above the force_all_opaque() method.
400 *
401 * Neglecting to do this led to the difficult-to-diagnose
402 * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
403 * spurious pixel_error messages as of r7258')
404 *
405 * TODO(epoger): Come up with a better solution that allows us to
406 * compare full pixel data, including alpha channel, while still being
407 * robust in the face of transformations to/from PNG files.
408 * Options include:
409 *
410 * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
411 * will be written to, or compared against, PNG files.
412 * PRO: Preserve/compare alpha channel info for the non-PNG cases
413 * (comparing different renderModes in-memory)
epoger@google.com908f5832013-04-12 02:23:55 +0000414 * CON: The bitmaps (and hash digests) for these non-PNG cases would be
epoger@google.com37269602013-01-19 04:21:27 +0000415 * different than those for the PNG-compared cases, and in the
416 * case of a failed renderMode comparison, how would we write the
417 * image to disk for examination?
418 *
epoger@google.com908f5832013-04-12 02:23:55 +0000419 * 2. Always compute image hash digests from PNG format (either
epoger@google.com37269602013-01-19 04:21:27 +0000420 * directly from the the bytes of a PNG file, or capturing the
421 * bytes we would have written to disk if we were writing the
422 * bitmap out as a PNG).
423 * PRO: I think this would allow us to never force opaque, and to
424 * the extent that alpha channel data can be preserved in a PNG
425 * file, we could observe it.
epoger@google.com908f5832013-04-12 02:23:55 +0000426 * CON: If we read a bitmap from disk, we need to take its hash digest
epoger@google.com37269602013-01-19 04:21:27 +0000427 * from the source PNG (we can't compute it from the bitmap we
428 * read out of the PNG, because we will have already premultiplied
429 * the alpha).
430 * CON: Seems wasteful to convert a bitmap to PNG format just to take
epoger@google.com908f5832013-04-12 02:23:55 +0000431 * its hash digest. (Although we're wasting lots of effort already
epoger@google.com37269602013-01-19 04:21:27 +0000432 * calling force_all_opaque().)
433 *
434 * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
435 * so we can transform images back and forth without fear of off-by-one
436 * errors.
437 * CON: Math is hard.
438 *
439 * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
440 * channel), rather than demanding absolute equality.
epoger@google.com908f5832013-04-12 02:23:55 +0000441 * CON: Can't do this with hash digests.
epoger@google.com37269602013-01-19 04:21:27 +0000442 */
epoger@google.com5f6a0072013-01-31 16:30:55 +0000443 static void complete_bitmap(SkBitmap* bitmap) {
444 force_all_opaque(*bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000445 }
446
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000447 static void installFilter(SkCanvas* canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000448
reed@google.comaef73612012-11-16 13:41:45 +0000449 static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000450 SkAutoCanvasRestore acr(canvas, true);
451
452 if (!isPDF) {
453 canvas->concat(gm->getInitialTransform());
454 }
455 installFilter(canvas);
reed@google.comaef73612012-11-16 13:41:45 +0000456 gm->setCanvasIsDeferred(isDeferred);
epoger@google.comde961632012-10-26 18:56:36 +0000457 gm->draw(canvas);
458 canvas->setDrawFilter(NULL);
459 }
460
epoger@google.com6f6568b2013-03-22 17:29:46 +0000461 static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000462 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000463 SkBitmap* bitmap,
464 bool deferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000465 SkISize size (gm->getISize());
466 setup_bitmap(gRec, size, bitmap);
467
468 SkAutoTUnref<SkCanvas> canvas;
469
470 if (gRec.fBackend == kRaster_Backend) {
471 SkAutoTUnref<SkDevice> device(new SkDevice(*bitmap));
472 if (deferred) {
473 canvas.reset(new SkDeferredCanvas(device));
474 } else {
475 canvas.reset(new SkCanvas(device));
476 }
reed@google.comaef73612012-11-16 13:41:45 +0000477 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000478 canvas->flush();
479 }
480#if SK_SUPPORT_GPU
481 else { // GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000482 SkAutoTUnref<SkDevice> device(SkGpuDevice::Create(gpuTarget));
epoger@google.comde961632012-10-26 18:56:36 +0000483 if (deferred) {
484 canvas.reset(new SkDeferredCanvas(device));
485 } else {
486 canvas.reset(new SkCanvas(device));
487 }
reed@google.comaef73612012-11-16 13:41:45 +0000488 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000489 // the device is as large as the current rendertarget, so
490 // we explicitly only readback the amount we expect (in
491 // size) overwrite our previous allocation
492 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
493 size.fHeight);
494 canvas->readPixels(bitmap, 0, 0);
495 }
496#endif
epoger@google.com5f6a0072013-01-31 16:30:55 +0000497 complete_bitmap(bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000498 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000499 }
500
501 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
junov@chromium.orgc938c482012-12-19 15:24:38 +0000502 SkPicture* pict, SkBitmap* bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000503 SkScalar scale = SK_Scalar1,
504 bool tile = false) {
epoger@google.comde961632012-10-26 18:56:36 +0000505 SkISize size = gm->getISize();
506 setup_bitmap(gRec, size, bitmap);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000507
508 if (tile) {
509 // Generate the result image by rendering to tiles and accumulating
510 // the results in 'bitmap'
511
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000512 // This 16x16 tiling matches the settings applied to 'pict' in
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000513 // 'generate_new_picture'
514 SkISize tileSize = SkISize::Make(16, 16);
515
516 SkBitmap tileBM;
517 setup_bitmap(gRec, tileSize, &tileBM);
518 SkCanvas tileCanvas(tileBM);
519 installFilter(&tileCanvas);
520
521 SkCanvas bmpCanvas(*bitmap);
522 SkPaint bmpPaint;
523 bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
524
525 for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
526 for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
527 int saveCount = tileCanvas.save();
528 SkMatrix mat(tileCanvas.getTotalMatrix());
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000529 mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000530 SkIntToScalar(-yTile*tileSize.height()));
531 tileCanvas.setMatrix(mat);
532 pict->draw(&tileCanvas);
533 tileCanvas.flush();
534 tileCanvas.restoreToCount(saveCount);
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000535 bmpCanvas.drawBitmap(tileBM,
536 SkIntToScalar(xTile * tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000537 SkIntToScalar(yTile * tileSize.height()),
538 &bmpPaint);
539 }
540 }
541 } else {
542 SkCanvas canvas(*bitmap);
543 installFilter(&canvas);
544 canvas.scale(scale, scale);
545 canvas.drawPicture(*pict);
546 complete_bitmap(bitmap);
547 }
epoger@google.comde961632012-10-26 18:56:36 +0000548 }
549
550 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
551#ifdef SK_SUPPORT_PDF
552 SkMatrix initialTransform = gm->getInitialTransform();
553 SkISize pageSize = gm->getISize();
554 SkPDFDevice* dev = NULL;
555 if (initialTransform.isIdentity()) {
556 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
557 } else {
558 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
559 SkIntToScalar(pageSize.height()));
560 initialTransform.mapRect(&content);
561 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
562 SkIntToScalar(pageSize.height()));
563 SkISize contentSize =
564 SkISize::Make(SkScalarRoundToInt(content.width()),
565 SkScalarRoundToInt(content.height()));
566 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
567 }
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000568 dev->setDCTEncoder(encode_to_dct_stream);
epoger@google.comde961632012-10-26 18:56:36 +0000569 SkAutoUnref aur(dev);
570
571 SkCanvas c(dev);
reed@google.comaef73612012-11-16 13:41:45 +0000572 invokeGM(gm, &c, true, false);
epoger@google.comde961632012-10-26 18:56:36 +0000573
574 SkPDFDocument doc;
575 doc.appendPage(dev);
576 doc.emitPDF(&pdf);
577#endif
578 }
579
580 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
581#ifdef SK_SUPPORT_XPS
582 SkISize size = gm->getISize();
583
584 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
585 SkIntToScalar(size.height()));
586 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
587 static const SkScalar upm = 72 * inchesPerMeter;
588 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
589 static const SkScalar ppm = 200 * inchesPerMeter;
590 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
591
592 SkXPSDevice* dev = new SkXPSDevice();
593 SkAutoUnref aur(dev);
594
595 SkCanvas c(dev);
596 dev->beginPortfolio(&xps);
597 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000598 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000599 dev->endSheet();
600 dev->endPortfolio();
601
602#endif
603 }
604
epoger@google.com6f6568b2013-03-22 17:29:46 +0000605 ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
606 const char renderModeDescriptor [], const SkString& name,
607 SkBitmap& bitmap, SkDynamicMemoryWStream* document) {
epoger@google.comde961632012-10-26 18:56:36 +0000608 SkString path;
609 bool success = false;
610 if (gRec.fBackend == kRaster_Backend ||
611 gRec.fBackend == kGPU_Backend ||
612 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
613
epoger@google.com37269602013-01-19 04:21:27 +0000614 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
epoger@google.comce057fe2013-05-14 15:17:46 +0000615 kPNG_FileExtension);
epoger@google.comde961632012-10-26 18:56:36 +0000616 success = write_bitmap(path, bitmap);
617 }
618 if (kPDF_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000619 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
620 "pdf");
epoger@google.comde961632012-10-26 18:56:36 +0000621 success = write_document(path, *document);
622 }
623 if (kXPS_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000624 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
625 "xps");
epoger@google.comde961632012-10-26 18:56:36 +0000626 success = write_document(path, *document);
627 }
628 if (success) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000629 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000630 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000631 gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000632 ErrorCombination errors(kWritingReferenceImage_ErrorType);
epoger@google.com310478e2013-04-03 18:00:39 +0000633 // TODO(epoger): Don't call RecordTestResults() here...
634 // Instead, we should make sure to call RecordTestResults
635 // exactly ONCE per test. (Otherwise, gmmain.fTestsRun
636 // will be incremented twice for this test: once in
637 // compare_test_results_to_stored_expectations() before
638 // that method calls this one, and again here.)
639 //
640 // When we make that change, we should probably add a
641 // WritingReferenceImage test to the gm self-tests.)
642 RecordTestResults(errors, name, renderModeDescriptor);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000643 return errors;
epoger@google.comde961632012-10-26 18:56:36 +0000644 }
645 }
646
epoger@google.com37269602013-01-19 04:21:27 +0000647 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000648 * Log more detail about the mistmatch between expectedBitmap and
649 * actualBitmap.
650 */
651 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
652 const char *testName) {
653 const int expectedWidth = expectedBitmap.width();
654 const int expectedHeight = expectedBitmap.height();
655 const int width = actualBitmap.width();
656 const int height = actualBitmap.height();
657 if ((expectedWidth != width) || (expectedHeight != height)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000658 gm_fprintf(stderr, "---- %s: dimension mismatch --"
659 " expected [%d %d], actual [%d %d]\n",
660 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000661 return;
662 }
663
664 if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
665 (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000666 gm_fprintf(stderr, "---- %s: not computing max per-channel"
667 " pixel mismatch because non-8888\n", testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000668 return;
669 }
670
671 SkAutoLockPixels alp0(expectedBitmap);
672 SkAutoLockPixels alp1(actualBitmap);
673 int errR = 0;
674 int errG = 0;
675 int errB = 0;
676 int errA = 0;
677 int differingPixels = 0;
678
679 for (int y = 0; y < height; ++y) {
680 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
681 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
682 for (int x = 0; x < width; ++x) {
683 SkPMColor expectedPixel = *expectedPixelPtr++;
684 SkPMColor actualPixel = *actualPixelPtr++;
685 if (expectedPixel != actualPixel) {
686 differingPixels++;
687 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
688 (int)SkGetPackedR32(actualPixel)));
689 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
690 (int)SkGetPackedG32(actualPixel)));
691 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
692 (int)SkGetPackedB32(actualPixel)));
693 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
694 (int)SkGetPackedA32(actualPixel)));
695 }
696 }
697 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000698 gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
699 " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
700 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000701 }
702
703 /**
epoger@google.com908f5832013-04-12 02:23:55 +0000704 * Compares actual hash digest to expectations, returning the set of errors
epoger@google.com6f6568b2013-03-22 17:29:46 +0000705 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000706 *
707 * If fMismatchPath has been set, and there are pixel diffs, then the
708 * actual bitmap will be written out to a file within fMismatchPath.
709 *
710 * @param expectations what expectations to compare actualBitmap against
711 * @param actualBitmap the image we actually generated
712 * @param baseNameString name of test without renderModeDescriptor added
713 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
714 * @param addToJsonSummary whether to add these results (both actual and
epoger@google.comcaac3db2013-04-04 19:23:11 +0000715 * expected) to the JSON summary. Regardless of this setting, if
716 * we find an image mismatch in this test, we will write these
717 * results to the JSON summary. (This is so that we will always
718 * report errors across rendering modes, such as pipe vs tiled.
719 * See https://codereview.chromium.org/13650002/ )
epoger@google.com37269602013-01-19 04:21:27 +0000720 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000721 ErrorCombination compare_to_expectations(Expectations expectations,
722 const SkBitmap& actualBitmap,
723 const SkString& baseNameString,
724 const char renderModeDescriptor[],
epoger@google.comcaac3db2013-04-04 19:23:11 +0000725 bool addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000726 ErrorCombination errors;
epoger@google.com908f5832013-04-12 02:23:55 +0000727 SkHashDigest actualBitmapHash;
728 // TODO(epoger): Better handling for error returned by ComputeDigest()?
729 // For now, we just report a digest of 0 in error cases, like before.
730 if (!SkBitmapHasher::ComputeDigest(actualBitmap, &actualBitmapHash)) {
731 actualBitmapHash = 0;
732 }
epoger@google.com37269602013-01-19 04:21:27 +0000733 SkString completeNameString = baseNameString;
734 completeNameString.append(renderModeDescriptor);
epoger@google.comce057fe2013-05-14 15:17:46 +0000735 completeNameString.append(".");
736 completeNameString.append(kPNG_FileExtension);
epoger@google.com37269602013-01-19 04:21:27 +0000737 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000738
epoger@google.com37269602013-01-19 04:21:27 +0000739 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000740 errors.add(kMissingExpectations_ErrorType);
epoger@google.com908f5832013-04-12 02:23:55 +0000741 } else if (!expectations.match(actualBitmapHash)) {
epoger@google.comcaac3db2013-04-04 19:23:11 +0000742 addToJsonSummary = true;
743 // The error mode we record depends on whether this was running
744 // in a non-standard renderMode.
745 if ('\0' == *renderModeDescriptor) {
746 errors.add(kExpectationsMismatch_ErrorType);
747 } else {
748 errors.add(kRenderModeMismatch_ErrorType);
749 }
epoger@google.com84a18022013-02-01 20:39:15 +0000750
751 // Write out the "actuals" for any mismatches, if we have
752 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000753 if (fMismatchPath) {
754 SkString path =
755 make_filename(fMismatchPath, renderModeDescriptor,
epoger@google.comce057fe2013-05-14 15:17:46 +0000756 baseNameString.c_str(), kPNG_FileExtension);
epoger@google.com37269602013-01-19 04:21:27 +0000757 write_bitmap(path, actualBitmap);
758 }
epoger@google.com84a18022013-02-01 20:39:15 +0000759
760 // If we have access to a single expected bitmap, log more
761 // detail about the mismatch.
762 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
763 if (NULL != expectedBitmapPtr) {
764 report_bitmap_diffs(*expectedBitmapPtr, actualBitmap, completeName);
765 }
epoger@google.coma243b222013-01-17 17:54:28 +0000766 }
epoger@google.com310478e2013-04-03 18:00:39 +0000767 RecordTestResults(errors, baseNameString, renderModeDescriptor);
epoger@google.coma243b222013-01-17 17:54:28 +0000768
epoger@google.com37269602013-01-19 04:21:27 +0000769 if (addToJsonSummary) {
epoger@google.com908f5832013-04-12 02:23:55 +0000770 add_actual_results_to_json_summary(completeName, actualBitmapHash, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000771 expectations.ignoreFailure());
772 add_expected_results_to_json_summary(completeName, expectations);
773 }
epoger@google.coma243b222013-01-17 17:54:28 +0000774
epoger@google.com6f6568b2013-03-22 17:29:46 +0000775 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000776 }
777
epoger@google.com37269602013-01-19 04:21:27 +0000778 /**
779 * Add this result to the appropriate JSON collection of actual results,
epoger@google.com76c913d2013-04-26 15:06:44 +0000780 * depending on errors encountered.
epoger@google.com37269602013-01-19 04:21:27 +0000781 */
782 void add_actual_results_to_json_summary(const char testName[],
epoger@google.com76c913d2013-04-26 15:06:44 +0000783 const SkHashDigest& actualResult,
784 ErrorCombination errors,
epoger@google.com37269602013-01-19 04:21:27 +0000785 bool ignoreFailure) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000786 Json::Value jsonActualResults = ActualResultAsJsonValue(actualResult);
787 if (errors.isEmpty()) {
788 this->fJsonActualResults_Succeeded[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000789 } else {
790 if (ignoreFailure) {
791 // TODO: Once we have added the ability to compare
792 // actual results against expectations in a JSON file
793 // (where we can set ignore-failure to either true or
epoger@google.com84a18022013-02-01 20:39:15 +0000794 // false), add test cases that exercise ignored
epoger@google.com6f6568b2013-03-22 17:29:46 +0000795 // failures (both for kMissingExpectations_ErrorType
epoger@google.comcaac3db2013-04-04 19:23:11 +0000796 // and kExpectationsMismatch_ErrorType).
epoger@google.com37269602013-01-19 04:21:27 +0000797 this->fJsonActualResults_FailureIgnored[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000798 jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000799 } else {
epoger@google.com76c913d2013-04-26 15:06:44 +0000800 if (errors.includes(kMissingExpectations_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000801 // TODO: What about the case where there IS an
epoger@google.com908f5832013-04-12 02:23:55 +0000802 // expected image hash digest, but that gm test
epoger@google.com37269602013-01-19 04:21:27 +0000803 // doesn't actually run? For now, those cases
804 // will always be ignored, because gm only looks
805 // at expectations that correspond to gm tests
806 // that were actually run.
807 //
808 // Once we have the ability to express
809 // expectations as a JSON file, we should fix this
810 // (and add a test case for which an expectation
811 // is given but the test is never run).
812 this->fJsonActualResults_NoComparison[testName] =
epoger@google.com76c913d2013-04-26 15:06:44 +0000813 jsonActualResults;
epoger@google.comeb066362013-03-08 09:39:36 +0000814 }
epoger@google.com76c913d2013-04-26 15:06:44 +0000815 if (errors.includes(kExpectationsMismatch_ErrorType) ||
816 errors.includes(kRenderModeMismatch_ErrorType)) {
817 this->fJsonActualResults_Failed[testName] = jsonActualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000818 }
819 }
820 }
821 }
822
823 /**
824 * Add this test to the JSON collection of expected results.
825 */
826 void add_expected_results_to_json_summary(const char testName[],
827 Expectations expectations) {
epoger@google.com76c913d2013-04-26 15:06:44 +0000828 this->fJsonExpectedResults[testName] = expectations.asJsonValue();
epoger@google.com37269602013-01-19 04:21:27 +0000829 }
830
831 /**
832 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
833 *
834 * @param gm which test generated the actualBitmap
835 * @param gRec
836 * @param writePath unless this is NULL, write out actual images into this
837 * directory
838 * @param actualBitmap bitmap generated by this run
839 * @param pdf
840 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000841 ErrorCombination compare_test_results_to_stored_expectations(
epoger@google.com37269602013-01-19 04:21:27 +0000842 GM* gm, const ConfigData& gRec, const char writePath[],
843 SkBitmap& actualBitmap, SkDynamicMemoryWStream* pdf) {
844
epoger@google.coma243b222013-01-17 17:54:28 +0000845 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000846 ErrorCombination errors;
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000847
epoger@google.com6f6568b2013-03-22 17:29:46 +0000848 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
epoger@google.com37269602013-01-19 04:21:27 +0000849 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
850 /*
851 * Get the expected results for this test, as one or more allowed
epoger@google.com908f5832013-04-12 02:23:55 +0000852 * hash digests. The current implementation of expectationsSource
853 * get this by computing the hash digest of a single PNG file on disk.
epoger@google.com37269602013-01-19 04:21:27 +0000854 *
855 * TODO(epoger): This relies on the fact that
856 * force_all_opaque() was called on the bitmap before it
857 * was written to disk as a PNG in the first place. If
epoger@google.com908f5832013-04-12 02:23:55 +0000858 * not, the hash digest returned here may not match the
859 * hash digest of actualBitmap, which *has* been run through
epoger@google.com37269602013-01-19 04:21:27 +0000860 * force_all_opaque().
epoger@google.com5f6a0072013-01-31 16:30:55 +0000861 * See comments above complete_bitmap() for more detail.
epoger@google.com37269602013-01-19 04:21:27 +0000862 */
epoger@google.comce057fe2013-05-14 15:17:46 +0000863 SkString nameWithExtension(name);
864 nameWithExtension.append(".");
865 nameWithExtension.append(kPNG_FileExtension);
866 Expectations expectations = expectationsSource->get(nameWithExtension.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000867 errors.add(compare_to_expectations(expectations, actualBitmap,
868 name, "", true));
epoger@google.com37269602013-01-19 04:21:27 +0000869 } else {
870 // If we are running without expectations, we still want to
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000871 // record the actual results.
epoger@google.com908f5832013-04-12 02:23:55 +0000872 SkHashDigest actualBitmapHash;
873 // TODO(epoger): Better handling for error returned by ComputeDigest()?
874 // For now, we just report a digest of 0 in error cases, like before.
875 if (!SkBitmapHasher::ComputeDigest(actualBitmap, &actualBitmapHash)) {
876 actualBitmapHash = 0;
877 }
epoger@google.comce057fe2013-05-14 15:17:46 +0000878 SkString nameWithExtension(name);
879 nameWithExtension.append(".");
880 nameWithExtension.append(kPNG_FileExtension);
881 add_actual_results_to_json_summary(nameWithExtension.c_str(), actualBitmapHash,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000882 ErrorCombination(kMissingExpectations_ErrorType),
epoger@google.com37269602013-01-19 04:21:27 +0000883 false);
epoger@google.com310478e2013-04-03 18:00:39 +0000884 RecordTestResults(ErrorCombination(kMissingExpectations_ErrorType), name, "");
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000885 }
epoger@google.com37269602013-01-19 04:21:27 +0000886
887 // TODO: Consider moving this into compare_to_expectations(),
888 // similar to fMismatchPath... for now, we don't do that, because
889 // we don't want to write out the actual bitmaps for all
890 // renderModes of all tests! That would be a lot of files.
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000891 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000892 errors.add(write_reference_image(gRec, writePath, "",
893 name, actualBitmap, pdf));
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000894 }
epoger@google.com37269602013-01-19 04:21:27 +0000895
epoger@google.com6f6568b2013-03-22 17:29:46 +0000896 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000897 }
898
epoger@google.com37269602013-01-19 04:21:27 +0000899 /**
900 * Compare actualBitmap to referenceBitmap.
901 *
epoger@google.comc8263e72013-04-10 12:17:34 +0000902 * @param baseNameString name of test without renderModeDescriptor added
epoger@google.com37269602013-01-19 04:21:27 +0000903 * @param renderModeDescriptor
904 * @param actualBitmap actual bitmap generated by this run
905 * @param referenceBitmap bitmap we expected to be generated
906 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000907 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.comc8263e72013-04-10 12:17:34 +0000908 const SkString& baseNameString, const char renderModeDescriptor[],
epoger@google.com37269602013-01-19 04:21:27 +0000909 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
910
911 SkASSERT(referenceBitmap);
epoger@google.com84a18022013-02-01 20:39:15 +0000912 Expectations expectations(*referenceBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000913 return compare_to_expectations(expectations, actualBitmap,
epoger@google.comc8263e72013-04-10 12:17:34 +0000914 baseNameString, renderModeDescriptor, false);
epoger@google.com37269602013-01-19 04:21:27 +0000915 }
916
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000917 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
918 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +0000919 // Pictures are refcounted so must be on heap
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000920 SkPicture* pict;
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000921 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
922 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +0000923
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000924 if (kTileGrid_BbhType == bbhType) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000925 SkTileGridPicture::TileGridInfo info;
926 info.fMargin.setEmpty();
927 info.fOffset.setZero();
928 info.fTileInterval.set(16, 16);
929 pict = new SkTileGridPicture(width, height, info);
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000930 } else {
931 pict = new SkPicture;
932 }
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000933 if (kNone_BbhType != bbhType) {
934 recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
935 }
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000936 SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +0000937 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +0000938 invokeGM(gm, cv, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000939 pict->endRecording();
940
941 return pict;
942 }
943
944 static SkPicture* stream_to_new_picture(const SkPicture& src) {
945
946 // To do in-memory commiunications with a stream, we need to:
947 // * create a dynamic memory stream
948 // * copy it into a buffer
949 // * create a read stream from it
950 // ?!?!
951
952 SkDynamicMemoryWStream storage;
953 src.serialize(&storage);
954
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000955 size_t streamSize = storage.getOffset();
epoger@google.comde961632012-10-26 18:56:36 +0000956 SkAutoMalloc dstStorage(streamSize);
957 void* dst = dstStorage.get();
958 //char* dst = new char [streamSize];
959 //@todo thudson 22 April 2011 when can we safely delete [] dst?
960 storage.copyTo(dst);
961 SkMemoryStream pictReadback(dst, streamSize);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +0000962 bool success;
963 // Pass a decoding bitmap function so that the factory GM (which has an SkBitmap with
964 // encoded data) does not fail.
965 SkPicture* retval = new SkPicture (&pictReadback, &success, &SkImageDecoder::DecodeMemory);
epoger@google.comde961632012-10-26 18:56:36 +0000966 return retval;
967 }
968
969 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +0000970 // Depending on flags, possibly compare to an expected image.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000971 ErrorCombination test_drawing(GM* gm,
972 const ConfigData& gRec,
973 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000974 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000975 SkBitmap* bitmap) {
epoger@google.comde961632012-10-26 18:56:36 +0000976 SkDynamicMemoryWStream document;
977
978 if (gRec.fBackend == kRaster_Backend ||
979 gRec.fBackend == kGPU_Backend) {
980 // Early exit if we can't generate the image.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000981 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000982 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +0000983 // TODO: Add a test to exercise what the stdout and
984 // JSON look like if we get an "early error" while
985 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +0000986 return errors;
987 }
988 } else if (gRec.fBackend == kPDF_Backend) {
989 generate_pdf(gm, document);
990#if CAN_IMAGE_PDF
991 SkAutoDataUnref data(document.copyToData());
992 SkMemoryStream stream(data->data(), data->size());
993 SkPDFDocumentToBitmap(&stream, bitmap);
994#endif
995 } else if (gRec.fBackend == kXPS_Backend) {
996 generate_xps(gm, document);
997 }
epoger@google.com37269602013-01-19 04:21:27 +0000998 return compare_test_results_to_stored_expectations(
999 gm, gRec, writePath, *bitmap, &document);
epoger@google.comde961632012-10-26 18:56:36 +00001000 }
1001
epoger@google.com6f6568b2013-03-22 17:29:46 +00001002 ErrorCombination test_deferred_drawing(GM* gm,
1003 const ConfigData& gRec,
1004 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001005 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +00001006 SkDynamicMemoryWStream document;
1007
1008 if (gRec.fBackend == kRaster_Backend ||
1009 gRec.fBackend == kGPU_Backend) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001010 const char renderModeDescriptor[] = "-deferred";
epoger@google.comde961632012-10-26 18:56:36 +00001011 SkBitmap bitmap;
1012 // Early exit if we can't generate the image, but this is
1013 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001014 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001015 // TODO(epoger): This logic is the opposite of what is
1016 // described above... if we succeeded in generating the
1017 // -deferred image, we exit early! We should fix this
1018 // ASAP, because it is hiding -deferred errors... but for
1019 // now, I'm leaving the logic as it is so that the
1020 // refactoring change
1021 // https://codereview.chromium.org/12992003/ is unblocked.
1022 //
1023 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
1024 // ('image-surface gm test is failing in "deferred" mode,
1025 // and gm is not reporting the failure')
1026 if (errors.isEmpty()) {
epoger@google.com310478e2013-04-03 18:00:39 +00001027 // TODO(epoger): Report this as a new ErrorType,
1028 // something like kImageGeneration_ErrorType?
epoger@google.com6f6568b2013-03-22 17:29:46 +00001029 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001030 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001031 const SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com37269602013-01-19 04:21:27 +00001032 return compare_test_results_to_reference_bitmap(
epoger@google.comc8263e72013-04-10 12:17:34 +00001033 name, renderModeDescriptor, bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001034 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001035 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +00001036 }
1037
epoger@google.comcaac3db2013-04-04 19:23:11 +00001038 ErrorCombination test_pipe_playback(GM* gm, const ConfigData& gRec,
1039 const SkBitmap& referenceBitmap, bool simulateFailure) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001040 const SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001041 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001042 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001043 SkString renderModeDescriptor("-pipe");
1044 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1045
1046 if (gm->getFlags() & GM::kSkipPipe_Flag) {
1047 RecordTestResults(kIntentionallySkipped_ErrorType, name,
1048 renderModeDescriptor.c_str());
1049 errors.add(kIntentionallySkipped_ErrorType);
1050 } else {
1051 SkBitmap bitmap;
1052 SkISize size = gm->getISize();
1053 setup_bitmap(gRec, size, &bitmap);
1054 SkCanvas canvas(bitmap);
1055 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001056 // Pass a decoding function so the factory GM (which has an SkBitmap
1057 // with encoded data) will not fail playback.
1058 PipeController pipeController(&canvas, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001059 SkGPipeWriter writer;
1060 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001061 gPipeWritingFlagCombos[i].flags,
1062 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001063 if (!simulateFailure) {
1064 invokeGM(gm, pipeCanvas, false, false);
1065 }
1066 complete_bitmap(&bitmap);
1067 writer.endRecording();
1068 errors.add(compare_test_results_to_reference_bitmap(
1069 name, renderModeDescriptor.c_str(), bitmap, &referenceBitmap));
1070 if (!errors.isEmpty()) {
1071 break;
1072 }
epoger@google.comde961632012-10-26 18:56:36 +00001073 }
1074 }
1075 return errors;
1076 }
1077
epoger@google.com6f6568b2013-03-22 17:29:46 +00001078 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
1079 const SkBitmap& referenceBitmap) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001080 const SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001081 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001082 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001083 SkString renderModeDescriptor("-tiled pipe");
1084 renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1085
1086 if ((gm->getFlags() & GM::kSkipPipe_Flag) ||
1087 (gm->getFlags() & GM::kSkipTiled_Flag)) {
1088 RecordTestResults(kIntentionallySkipped_ErrorType, name,
1089 renderModeDescriptor.c_str());
1090 errors.add(kIntentionallySkipped_ErrorType);
1091 } else {
1092 SkBitmap bitmap;
1093 SkISize size = gm->getISize();
1094 setup_bitmap(gRec, size, &bitmap);
1095 SkCanvas canvas(bitmap);
1096 installFilter(&canvas);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +00001097 TiledPipeController pipeController(bitmap, &SkImageDecoder::DecodeMemory);
epoger@google.comc8263e72013-04-10 12:17:34 +00001098 SkGPipeWriter writer;
1099 SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
scroggo@google.comaef2d3b2013-04-10 18:10:41 +00001100 gPipeWritingFlagCombos[i].flags,
1101 size.width(), size.height());
epoger@google.comc8263e72013-04-10 12:17:34 +00001102 invokeGM(gm, pipeCanvas, false, false);
1103 complete_bitmap(&bitmap);
1104 writer.endRecording();
1105 errors.add(compare_test_results_to_reference_bitmap(name,
1106 renderModeDescriptor.c_str(),
1107 bitmap, &referenceBitmap));
1108 if (!errors.isEmpty()) {
1109 break;
1110 }
epoger@google.comde961632012-10-26 18:56:36 +00001111 }
1112 }
1113 return errors;
1114 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00001115
1116 //
1117 // member variables.
1118 // They are public for now, to allow easier setting by tool_main().
1119 //
1120
epoger@google.come8ebeb12012-10-29 16:42:11 +00001121 bool fUseFileHierarchy;
epoger@google.com5079d2c2013-04-12 14:11:21 +00001122 ErrorCombination fIgnorableErrorTypes;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001123
junov@chromium.org95146eb2013-01-11 21:04:40 +00001124 const char* fMismatchPath;
1125
epoger@google.com310478e2013-04-03 18:00:39 +00001126 // collection of tests that have failed with each ErrorType
1127 SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
1128 int fTestsRun;
1129 SkTDict<int> fRenderModesEncountered;
epoger@google.com57f7abc2012-11-13 03:41:55 +00001130
epoger@google.com908f5832013-04-12 02:23:55 +00001131 // Where to read expectations (expected image hash digests, etc.) from.
epoger@google.com37269602013-01-19 04:21:27 +00001132 // If unset, we don't do comparisons.
1133 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
1134
1135 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +00001136 Json::Value fJsonExpectedResults;
1137 Json::Value fJsonActualResults_Failed;
1138 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001139 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001140 Json::Value fJsonActualResults_Succeeded;
1141
epoger@google.comde961632012-10-26 18:56:36 +00001142}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +00001143
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001144#if SK_SUPPORT_GPU
1145static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1146#else
1147static const GLContextType kDontCare_GLContextType = 0;
1148#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +00001149
1150// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +00001151// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +00001152static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
1153 kWrite_ConfigFlag;
1154
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001155static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001156 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001157#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 +00001158 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001159#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001160 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
1161#if SK_SUPPORT_GPU
1162 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
1163 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", true },
1164 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
bsalomon@google.com7361f542012-04-19 19:15:35 +00001165 /* The debug context does not generate images */
scroggo@google.com0f567c62013-03-20 15:35:08 +00001166 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", GR_DEBUG},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001167#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001168 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
1169 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001170#endif // SK_ANGLE
1171#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001172 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001173#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001174#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +00001175#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001176 /* 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 +00001177 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +00001178#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001179#ifdef SK_SUPPORT_PDF
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001180 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001181#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001182};
1183
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001184static const char kDefaultsConfigStr[] = "defaults";
1185static const char kExcludeConfigChar = '~';
1186
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001187static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001188 SkString result;
1189 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001190 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001191 SkASSERT(gRec[i].fName != kDefaultsConfigStr);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001192 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001193 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001194 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001195 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001196 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001197 result.append("]\n");
1198 result.appendf("The default value is: \"");
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001199 SkString firstDefault;
1200 SkString allButFirstDefaults;
1201 SkString nonDefault;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001202 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1203 if (gRec[i].fRunByDefault) {
1204 if (i > 0) {
1205 result.append(" ");
1206 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001207 result.append(gRec[i].fName);
1208 if (firstDefault.isEmpty()) {
1209 firstDefault = gRec[i].fName;
1210 } else {
1211 if (!allButFirstDefaults.isEmpty()) {
1212 allButFirstDefaults.append(" ");
1213 }
1214 allButFirstDefaults.append(gRec[i].fName);
1215 }
1216 } else {
1217 nonDefault = gRec[i].fName;
scroggo@google.com0f567c62013-03-20 15:35:08 +00001218 }
1219 }
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001220 result.append("\"\n");
1221 result.appendf("\"%s\" evaluates to the default set of configs.\n", kDefaultsConfigStr);
1222 result.appendf("Prepending \"%c\" on a config name excludes it from the set of configs to run.\n"
1223 "Exclusions always override inclusions regardless of order.\n",
1224 kExcludeConfigChar);
1225 result.appendf("E.g. \"--config %s %c%s %s\" will run these configs:\n\t%s %s",
1226 kDefaultsConfigStr,
1227 kExcludeConfigChar,
1228 firstDefault.c_str(),
1229 nonDefault.c_str(),
1230 allButFirstDefaults.c_str(),
1231 nonDefault.c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001232 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001233}
scroggo@google.com7d519302013-03-19 17:28:10 +00001234
epoger@google.com6f6568b2013-03-22 17:29:46 +00001235// Macro magic to convert a numeric preprocessor token into a string.
1236// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1237// This should probably be moved into one of our common headers...
1238#define TOSTRING_INTERNAL(x) #x
1239#define TOSTRING(x) TOSTRING_INTERNAL(x)
1240
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001241// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001242DEFINE_string(config, "", configUsage().c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001243DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001244DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1245DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1246#if SK_SUPPORT_GPU
1247DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001248 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1249 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001250#endif
1251DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1252 "when reading/writing files.");
epoger@google.com5079d2c2013-04-12 14:11:21 +00001253DEFINE_string(ignoreErrorTypes, kDefaultIgnorableErrorTypes.asString(" ").c_str(),
1254 "Space-separated list of ErrorTypes that should be ignored. If any *other* error "
1255 "types are encountered, the tool will exit with a nonzero return value.");
caryclark@google.com512c9b62013-05-10 15:16:13 +00001256DEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
1257 "Multiple matches may be separated by spaces.\n"
1258 "~ causes a matching test to always be skipped\n"
1259 "^ requires the start of the test to match\n"
1260 "$ requires the end of the test to match\n"
1261 "^ and $ requires an exact match\n"
1262 "If a test does not match any list entry,\n"
1263 "it is skipped unless some list entry starts with ~");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001264DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1265 "pixel mismatches into this directory.");
1266DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1267 "testIndex %% divisor == remainder.");
1268DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1269DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1270DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1271 "any differences between those and the newly generated ones.");
1272DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1273DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1274DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1275DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
epoger@google.comcaac3db2013-04-04 19:23:11 +00001276DEFINE_bool(simulatePipePlaybackFailure, false, "Simulate a rendering failure in pipe mode only.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001277DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1278DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1279DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1280 "factors to be used for tileGrid playback testing. Default value: 1.0");
1281DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
epoger@google.comb0f8b432013-04-10 18:46:25 +00001282DEFINE_bool2(verbose, v, false, "Give more detail (e.g. list all GMs run, more info about "
1283 "each test).");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001284DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
scroggo@google.com604e0c22013-04-09 21:25:46 +00001285DEFINE_string2(writePicturePath, p, "", "Write .skp files into this directory.");
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001286DEFINE_int32(pdfJpegQuality, -1, "Encodes images in JPEG at quality level N, "
1287 "which can be in range 0-100). N = -1 will disable JPEG compression. "
1288 "Default is N = 100, maximum quality.");
1289
1290static bool encode_to_dct_stream(SkWStream* stream, const SkBitmap& bitmap, const SkIRect& rect) {
1291 // Filter output of warnings that JPEG is not available for the image.
1292 if (bitmap.width() >= 65500 || bitmap.height() >= 65500) return false;
1293 if (FLAGS_pdfJpegQuality == -1) return false;
1294
1295 SkIRect bitmapBounds;
1296 SkBitmap subset;
1297 const SkBitmap* bitmapToUse = &bitmap;
1298 bitmap.getBounds(&bitmapBounds);
1299 if (rect != bitmapBounds) {
1300 SkAssertResult(bitmap.extractSubset(&subset, rect));
1301 bitmapToUse = &subset;
1302 }
1303
1304#if defined(SK_BUILD_FOR_MAC)
1305 // Workaround bug #1043 where bitmaps with referenced pixels cause
1306 // CGImageDestinationFinalize to crash
1307 SkBitmap copy;
1308 bitmapToUse->deepCopyTo(&copy, bitmapToUse->config());
1309 bitmapToUse = &copy;
1310#endif
1311
1312 return SkImageEncoder::EncodeStream(stream,
1313 *bitmapToUse,
1314 SkImageEncoder::kJPEG_Type,
1315 FLAGS_pdfJpegQuality);
1316}
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001317
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001318static int findConfig(const char config[]) {
1319 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1320 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001321 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001322 }
1323 }
1324 return -1;
1325}
1326
scroggo@google.comb7dbf632013-04-23 15:38:09 +00001327static bool skip_name(SkCommandLineFlags::StringArray array, const char name[]) {
caryclark@google.com512c9b62013-05-10 15:16:13 +00001328 // FIXME: this duplicates the logic in test/skia_test.cpp -- consolidate
1329 int count = array.count();
1330 size_t testLen = strlen(name);
1331 bool anyExclude = count == 0;
reed@google.comb2a51622011-10-31 16:30:04 +00001332 for (int i = 0; i < array.count(); ++i) {
caryclark@google.com512c9b62013-05-10 15:16:13 +00001333 const char* matchName = array[i];
1334 size_t matchLen = strlen(matchName);
1335 bool matchExclude, matchStart, matchEnd;
1336 if ((matchExclude = matchName[0] == '~')) {
1337 anyExclude = true;
1338 matchName++;
1339 matchLen--;
1340 }
1341 if ((matchStart = matchName[0] == '^')) {
1342 matchName++;
1343 matchLen--;
1344 }
1345 if ((matchEnd = matchName[matchLen - 1] == '$')) {
1346 matchLen--;
1347 }
1348 if (matchStart ? (!matchEnd || matchLen == testLen)
1349 && strncmp(name, matchName, matchLen) == 0
1350 : matchEnd ? matchLen <= testLen
1351 && strncmp(name + testLen - matchLen, matchName, matchLen) == 0
1352 : strstr(name, matchName) != 0) {
1353 return matchExclude;
reed@google.comb2a51622011-10-31 16:30:04 +00001354 }
1355 }
caryclark@google.com512c9b62013-05-10 15:16:13 +00001356 return !anyExclude;
reed@google.comb2a51622011-10-31 16:30:04 +00001357}
1358
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001359namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001360#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +00001361SkAutoTUnref<GrContext> gGrContext;
1362/**
bsalomon@google.comc7a24d22012-11-01 18:03:48 +00001363 * Sets the global GrContext, accessible by individual GMs
bsalomon@google.com7361f542012-04-19 19:15:35 +00001364 */
caryclark@google.com13130862012-06-06 12:10:45 +00001365static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +00001366 SkSafeRef(grContext);
1367 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001368}
bsalomon@google.com7361f542012-04-19 19:15:35 +00001369
1370/**
1371 * Gets the global GrContext, can be called by GM tests.
1372 */
caryclark@google.com13130862012-06-06 12:10:45 +00001373GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +00001374GrContext* GetGr() {
1375 return gGrContext.get();
1376}
1377
1378/**
1379 * Sets the global GrContext and then resets it to its previous value at
1380 * destruction.
1381 */
1382class AutoResetGr : SkNoncopyable {
1383public:
1384 AutoResetGr() : fOld(NULL) {}
1385 void set(GrContext* context) {
1386 SkASSERT(NULL == fOld);
1387 fOld = GetGr();
1388 SkSafeRef(fOld);
1389 SetGr(context);
1390 }
1391 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1392private:
1393 GrContext* fOld;
1394};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001395#else
epoger@google.com80724df2013-03-21 13:49:54 +00001396GrContext* GetGr();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001397GrContext* GetGr() { return NULL; }
1398#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001399}
1400
reed@google.comfb2cd422013-01-04 14:43:03 +00001401template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1402 int index = array->find(value);
1403 if (index < 0) {
1404 *array->append() = value;
1405 }
1406}
1407
epoger@google.com80724df2013-03-21 13:49:54 +00001408/**
1409 * Run this test in a number of different configs (8888, 565, PDF,
1410 * etc.), confirming that the resulting bitmaps match expectations
1411 * (which may be different for each config).
1412 *
1413 * Returns all errors encountered while doing so.
1414 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001415ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1416 GrContextFactory *grFactory);
1417ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1418 GrContextFactory *grFactory) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001419 const char renderModeDescriptor[] = "";
epoger@google.com6f6568b2013-03-22 17:29:46 +00001420 ErrorCombination errorsForAllConfigs;
epoger@google.com80724df2013-03-21 13:49:54 +00001421 uint32_t gmFlags = gm->getFlags();
1422
epoger@google.com80724df2013-03-21 13:49:54 +00001423 for (int i = 0; i < configs.count(); i++) {
1424 ConfigData config = gRec[configs[i]];
epoger@google.comc8263e72013-04-10 12:17:34 +00001425 const SkString name = gmmain.make_name(gm->shortName(), config.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001426
1427 // Skip any tests that we don't even need to try.
epoger@google.comc8263e72013-04-10 12:17:34 +00001428 // If any of these were skipped on a per-GM basis, record them as
1429 // kIntentionallySkipped.
1430 if (kPDF_Backend == config.fBackend) {
1431 if (!FLAGS_pdf) {
epoger@google.com80724df2013-03-21 13:49:54 +00001432 continue;
1433 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001434 if (gmFlags & GM::kSkipPDF_Flag) {
1435 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1436 renderModeDescriptor);
1437 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1438 continue;
1439 }
1440 }
epoger@google.com80724df2013-03-21 13:49:54 +00001441 if ((gmFlags & GM::kSkip565_Flag) &&
1442 (kRaster_Backend == config.fBackend) &&
1443 (SkBitmap::kRGB_565_Config == config.fConfig)) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001444 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1445 renderModeDescriptor);
1446 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001447 continue;
1448 }
1449 if ((gmFlags & GM::kSkipGPU_Flag) &&
1450 kGPU_Backend == config.fBackend) {
epoger@google.comc8263e72013-04-10 12:17:34 +00001451 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1452 renderModeDescriptor);
1453 errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001454 continue;
1455 }
1456
1457 // Now we know that we want to run this test and record its
1458 // success or failure.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001459 ErrorCombination errorsForThisConfig;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001460 GrSurface* gpuTarget = NULL;
epoger@google.com80724df2013-03-21 13:49:54 +00001461#if SK_SUPPORT_GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001462 SkAutoTUnref<GrSurface> auGpuTarget;
epoger@google.com80724df2013-03-21 13:49:54 +00001463 AutoResetGr autogr;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001464 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
epoger@google.com80724df2013-03-21 13:49:54 +00001465 GrContext* gr = grFactory->get(config.fGLContextType);
1466 bool grSuccess = false;
1467 if (gr) {
1468 // create a render target to back the device
1469 GrTextureDesc desc;
1470 desc.fConfig = kSkia8888_GrPixelConfig;
1471 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1472 desc.fWidth = gm->getISize().width();
1473 desc.fHeight = gm->getISize().height();
1474 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001475 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1476 if (NULL != auGpuTarget) {
1477 gpuTarget = auGpuTarget;
1478 grSuccess = true;
epoger@google.com80724df2013-03-21 13:49:54 +00001479 autogr.set(gr);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001480 // Set the user specified cache limits if non-default.
1481 size_t bytes;
1482 int count;
1483 gr->getTextureCacheLimits(&count, &bytes);
1484 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1485 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1486 }
1487 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1488 count = gGpuCacheSizeCount;
1489 }
1490 gr->setTextureCacheLimits(count, bytes);
epoger@google.com80724df2013-03-21 13:49:54 +00001491 }
epoger@google.com80724df2013-03-21 13:49:54 +00001492 }
1493 if (!grSuccess) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001494 errorsForThisConfig.add(kNoGpuContext_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001495 }
1496 }
1497#endif
1498
1499 SkBitmap comparisonBitmap;
1500
1501 const char* writePath;
1502 if (FLAGS_writePath.count() == 1) {
1503 writePath = FLAGS_writePath[0];
1504 } else {
1505 writePath = NULL;
1506 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001507 if (errorsForThisConfig.isEmpty()) {
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001508 errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
1509 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001510 }
1511
epoger@google.com6f6568b2013-03-22 17:29:46 +00001512 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1513 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1514 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001515 gpuTarget));
epoger@google.com80724df2013-03-21 13:49:54 +00001516 }
1517
epoger@google.com6f6568b2013-03-22 17:29:46 +00001518 errorsForAllConfigs.add(errorsForThisConfig);
epoger@google.com80724df2013-03-21 13:49:54 +00001519 }
1520 return errorsForAllConfigs;
1521}
1522
1523/**
1524 * Run this test in a number of different drawing modes (pipe,
1525 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1526 * *exactly* match comparisonBitmap.
1527 *
1528 * Returns all errors encountered while doing so.
1529 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001530ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1531 const SkBitmap &comparisonBitmap,
1532 const SkTDArray<SkScalar> &tileGridReplayScales);
1533ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1534 const SkBitmap &comparisonBitmap,
1535 const SkTDArray<SkScalar> &tileGridReplayScales) {
1536 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001537 uint32_t gmFlags = gm->getFlags();
epoger@google.comc8263e72013-04-10 12:17:34 +00001538 const SkString name = gmmain.make_name(gm->shortName(), compareConfig.fName);
epoger@google.com80724df2013-03-21 13:49:54 +00001539
epoger@google.comc8263e72013-04-10 12:17:34 +00001540 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1541 SkAutoUnref aur(pict);
1542 if (FLAGS_replay) {
1543 const char renderModeDescriptor[] = "-replay";
1544 if (gmFlags & GM::kSkipPicture_Flag) {
1545 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name, renderModeDescriptor);
1546 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1547 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001548 SkBitmap bitmap;
1549 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001550 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1551 name, renderModeDescriptor, bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001552 }
epoger@google.comc8263e72013-04-10 12:17:34 +00001553 }
epoger@google.com80724df2013-03-21 13:49:54 +00001554
epoger@google.comc8263e72013-04-10 12:17:34 +00001555 if (FLAGS_serialize) {
1556 const char renderModeDescriptor[] = "-serialize";
1557 if (gmFlags & GM::kSkipPicture_Flag) {
1558 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name, renderModeDescriptor);
1559 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1560 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001561 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1562 SkAutoUnref aurr(repict);
epoger@google.com80724df2013-03-21 13:49:54 +00001563 SkBitmap bitmap;
1564 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.comc8263e72013-04-10 12:17:34 +00001565 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1566 name, renderModeDescriptor, bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001567 }
epoger@google.com80724df2013-03-21 13:49:54 +00001568 }
1569
epoger@google.comc8263e72013-04-10 12:17:34 +00001570 if ((1 == FLAGS_writePicturePath.count()) &&
1571 !(gmFlags & GM::kSkipPicture_Flag)) {
1572 const char* pictureSuffix = "skp";
1573 SkString path = make_filename(FLAGS_writePicturePath[0], "",
1574 gm->shortName(), pictureSuffix);
1575 SkFILEWStream stream(path.c_str());
1576 pict->serialize(&stream);
epoger@google.com80724df2013-03-21 13:49:54 +00001577 }
1578
epoger@google.comc8263e72013-04-10 12:17:34 +00001579 if (FLAGS_rtree) {
1580 const char renderModeDescriptor[] = "-rtree";
1581 if (gmFlags & GM::kSkipPicture_Flag) {
1582 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name, renderModeDescriptor);
1583 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1584 } else {
epoger@google.com80724df2013-03-21 13:49:54 +00001585 SkPicture* pict = gmmain.generate_new_picture(
epoger@google.comc8263e72013-04-10 12:17:34 +00001586 gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
epoger@google.com80724df2013-03-21 13:49:54 +00001587 SkAutoUnref aur(pict);
1588 SkBitmap bitmap;
epoger@google.comc8263e72013-04-10 12:17:34 +00001589 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001590 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
epoger@google.comc8263e72013-04-10 12:17:34 +00001591 name, renderModeDescriptor, bitmap, &comparisonBitmap));
1592 }
1593 }
1594
1595 if (FLAGS_tileGrid) {
1596 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1597 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1598 SkString renderModeDescriptor("-tilegrid");
1599 if (SK_Scalar1 != replayScale) {
1600 renderModeDescriptor += "-scale-";
1601 renderModeDescriptor.appendScalar(replayScale);
1602 }
1603
1604 if ((gmFlags & GM::kSkipPicture_Flag) ||
1605 ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1)) {
1606 gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1607 renderModeDescriptor.c_str());
1608 errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1609 } else {
1610 // We record with the reciprocal scale to obtain a replay
1611 // result that can be validated against comparisonBitmap.
1612 SkScalar recordScale = SkScalarInvert(replayScale);
1613 SkPicture* pict = gmmain.generate_new_picture(
1614 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag,
1615 recordScale);
1616 SkAutoUnref aur(pict);
1617 SkBitmap bitmap;
1618 // We cannot yet pass 'true' to generate_image_from_picture to
1619 // perform actual tiled rendering (see Issue 1198 -
1620 // https://code.google.com/p/skia/issues/detail?id=1198)
1621 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
1622 replayScale /*, true */);
1623 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1624 name, renderModeDescriptor.c_str(), bitmap, &comparisonBitmap));
1625 }
epoger@google.com80724df2013-03-21 13:49:54 +00001626 }
1627 }
1628
1629 // run the pipe centric GM steps
epoger@google.comc8263e72013-04-10 12:17:34 +00001630 if (FLAGS_pipe) {
1631 errorsForAllModes.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap,
1632 FLAGS_simulatePipePlaybackFailure));
1633 if (FLAGS_tiledPipe) {
1634 errorsForAllModes.add(gmmain.test_tiled_pipe_playback(gm, compareConfig,
1635 comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001636 }
epoger@google.com80724df2013-03-21 13:49:54 +00001637 }
1638 return errorsForAllModes;
1639}
1640
epoger@google.com310478e2013-04-03 18:00:39 +00001641/**
1642 * Return a list of all entries in an array of strings as a single string
1643 * of this form:
1644 * "item1", "item2", "item3"
1645 */
1646SkString list_all(const SkTArray<SkString> &stringArray);
1647SkString list_all(const SkTArray<SkString> &stringArray) {
1648 SkString total;
1649 for (int i = 0; i < stringArray.count(); i++) {
1650 if (i > 0) {
1651 total.append(", ");
1652 }
1653 total.append("\"");
1654 total.append(stringArray[i]);
1655 total.append("\"");
1656 }
1657 return total;
1658}
1659
1660/**
1661 * Return a list of configuration names, as a single string of this form:
1662 * "item1", "item2", "item3"
1663 *
1664 * @param configs configurations, as a list of indices into gRec
1665 */
1666SkString list_all_config_names(const SkTDArray<size_t> &configs);
1667SkString list_all_config_names(const SkTDArray<size_t> &configs) {
1668 SkString total;
1669 for (int i = 0; i < configs.count(); i++) {
1670 if (i > 0) {
1671 total.append(", ");
1672 }
1673 total.append("\"");
1674 total.append(gRec[configs[i]].fName);
1675 total.append("\"");
1676 }
1677 return total;
1678}
1679
epoger@google.comfdea3252013-05-02 18:24:03 +00001680bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1681 const SkTDArray<size_t> &configs);
1682bool prepare_subdirectories(const char *root, bool useFileHierarchy,
1683 const SkTDArray<size_t> &configs) {
1684 if (!sk_mkdir(root)) {
1685 return false;
1686 }
1687 if (useFileHierarchy) {
1688 for (int i = 0; i < configs.count(); i++) {
1689 ConfigData config = gRec[configs[i]];
1690 SkString subdir;
1691 subdir.appendf("%s%c%s", root, SkPATH_SEPARATOR, config.fName);
1692 if (!sk_mkdir(subdir.c_str())) {
1693 return false;
1694 }
1695 }
1696 }
1697 return true;
1698}
1699
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001700int tool_main(int argc, char** argv);
1701int tool_main(int argc, char** argv) {
1702
1703#if SK_ENABLE_INST_COUNT
1704 gPrintInstCount = true;
1705#endif
1706
1707 SkGraphics::Init();
1708 // we don't need to see this during a run
1709 gSkSuppressFontCachePurgeSpew = true;
1710
1711 setSystemPreferences();
1712 GMMain gmmain;
1713
1714 SkTDArray<size_t> configs;
1715 SkTDArray<size_t> excludeConfigs;
1716 bool userConfig = false;
1717
1718 SkString usage;
1719 usage.printf("Run the golden master tests.\n");
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00001720 SkCommandLineFlags::SetUsage(usage.c_str());
1721 SkCommandLineFlags::Parse(argc, argv);
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001722
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001723 gmmain.fUseFileHierarchy = FLAGS_hierarchy;
1724 if (FLAGS_mismatchPath.count() == 1) {
1725 gmmain.fMismatchPath = FLAGS_mismatchPath[0];
1726 }
1727
1728 for (int i = 0; i < FLAGS_config.count(); i++) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001729 const char* config = FLAGS_config[i];
1730 userConfig = true;
1731 bool exclude = false;
1732 if (*config == kExcludeConfigChar) {
1733 exclude = true;
1734 config += 1;
1735 }
1736 int index = findConfig(config);
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001737 if (index >= 0) {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001738 if (exclude) {
1739 *excludeConfigs.append() = index;
1740 } else {
1741 appendUnique<size_t>(&configs, index);
1742 }
1743 } else if (0 == strcmp(kDefaultsConfigStr, config)) {
1744 for (size_t c = 0; c < SK_ARRAY_COUNT(gRec); ++c) {
1745 if (gRec[c].fRunByDefault) {
1746 if (exclude) {
1747 gm_fprintf(stderr, "%c%s is not allowed.\n",
1748 kExcludeConfigChar, kDefaultsConfigStr);
1749 return -1;
1750 } else {
1751 appendUnique<size_t>(&configs, c);
1752 }
1753 }
1754 }
scroggo@google.com0b735632013-03-19 17:38:50 +00001755 } else {
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001756 gm_fprintf(stderr, "unrecognized config %s\n", config);
scroggo@google.com7d519302013-03-19 17:28:10 +00001757 return -1;
1758 }
1759 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001760
1761 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1762 int index = findConfig(FLAGS_excludeConfig[i]);
1763 if (index >= 0) {
1764 *excludeConfigs.append() = index;
1765 } else {
1766 gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1767 return -1;
1768 }
1769 }
1770
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001771 int moduloRemainder = -1;
1772 int moduloDivisor = -1;
1773
1774 if (FLAGS_modulo.count() == 2) {
1775 moduloRemainder = atoi(FLAGS_modulo[0]);
1776 moduloDivisor = atoi(FLAGS_modulo[1]);
1777 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
1778 gm_fprintf(stderr, "invalid modulo values.");
1779 return -1;
1780 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001781 }
reed@google.com873cb1e2010-12-23 15:00:45 +00001782
epoger@google.com5079d2c2013-04-12 14:11:21 +00001783 if (FLAGS_ignoreErrorTypes.count() > 0) {
1784 gmmain.fIgnorableErrorTypes = ErrorCombination();
1785 for (int i = 0; i < FLAGS_ignoreErrorTypes.count(); i++) {
1786 ErrorType type;
1787 const char *name = FLAGS_ignoreErrorTypes[i];
1788 if (!getErrorTypeByName(name, &type)) {
1789 gm_fprintf(stderr, "cannot find ErrorType with name '%s'\n", name);
1790 return -1;
1791 } else {
1792 gmmain.fIgnorableErrorTypes.add(type);
1793 }
1794 }
1795 }
1796
epoger@google.com6f6568b2013-03-22 17:29:46 +00001797#if SK_SUPPORT_GPU
1798 if (FLAGS_gpuCacheSize.count() > 0) {
1799 if (FLAGS_gpuCacheSize.count() != 2) {
1800 gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
1801 return -1;
1802 }
1803 gGpuCacheSizeBytes = atoi(FLAGS_gpuCacheSize[0]);
1804 gGpuCacheSizeCount = atoi(FLAGS_gpuCacheSize[1]);
1805 } else {
1806 gGpuCacheSizeBytes = DEFAULT_CACHE_VALUE;
1807 gGpuCacheSizeCount = DEFAULT_CACHE_VALUE;
1808 }
1809#endif
1810
1811 SkTDArray<SkScalar> tileGridReplayScales;
1812 *tileGridReplayScales.append() = SK_Scalar1; // By default only test at scale 1.0
1813 if (FLAGS_tileGridReplayScales.count() > 0) {
1814 tileGridReplayScales.reset();
1815 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
1816 double val = atof(FLAGS_tileGridReplayScales[i]);
1817 if (0 < val) {
1818 *tileGridReplayScales.append() = SkDoubleToScalar(val);
1819 }
1820 }
1821 if (0 == tileGridReplayScales.count()) {
1822 // Should have at least one scale
1823 gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
1824 return -1;
1825 }
1826 }
1827
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001828 if (!userConfig) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001829 // if no config is specified by user, add the defaults
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001830 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001831 if (gRec[i].fRunByDefault) {
1832 *configs.append() = i;
1833 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001834 }
1835 }
reed@google.comfb2cd422013-01-04 14:43:03 +00001836 // now remove any explicitly excluded configs
1837 for (int i = 0; i < excludeConfigs.count(); ++i) {
1838 int index = configs.find(excludeConfigs[i]);
1839 if (index >= 0) {
1840 configs.remove(index);
1841 // now assert that there was only one copy in configs[]
1842 SkASSERT(configs.find(excludeConfigs[i]) < 0);
1843 }
1844 }
1845
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001846#if SK_SUPPORT_GPU
1847 GrContextFactory* grFactory = new GrContextFactory;
1848 for (int i = 0; i < configs.count(); ++i) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001849 size_t index = configs[i];
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001850 if (kGPU_Backend == gRec[index].fBackend) {
1851 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1852 if (NULL == ctx) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001853 gm_fprintf(stderr, "GrContext could not be created for config %s."
1854 " Config will be skipped.\n", gRec[index].fName);
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001855 configs.remove(i);
1856 --i;
1857 }
1858 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001859 gm_fprintf(stderr, "Sample count (%d) of config %s is not supported."
1860 " Config will be skipped.\n", gRec[index].fSampleCnt, gRec[index].fName);
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001861 configs.remove(i);
1862 --i;
1863 }
1864 }
1865 }
epoger@google.com80724df2013-03-21 13:49:54 +00001866#else
1867 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001868#endif
1869
bsalomon@google.comdd8e3532013-04-24 18:07:11 +00001870 if (configs.isEmpty()) {
1871 gm_fprintf(stderr, "No configs to run.");
1872 return -1;
1873 }
1874
1875 // now show the user the set of configs that will be run.
1876 SkString configStr("These configs will be run: ");
1877 // show the user the config that will run.
1878 for (int i = 0; i < configs.count(); ++i) {
1879 configStr.appendf("%s%s", gRec[configs[i]].fName, (i == configs.count() - 1) ? "\n" : " ");
1880 }
1881 gm_fprintf(stdout, "%s", configStr.c_str());
1882
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001883 if (FLAGS_resourcePath.count() == 1) {
1884 GM::SetResourcePath(FLAGS_resourcePath[0]);
1885 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001886
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001887 if (FLAGS_readPath.count() == 1) {
1888 const char* readPath = FLAGS_readPath[0];
epoger@google.com37269602013-01-19 04:21:27 +00001889 if (!sk_exists(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001890 gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001891 return -1;
1892 }
1893 if (sk_isdir(readPath)) {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001894 if (FLAGS_verbose) {
1895 gm_fprintf(stdout, "reading from %s\n", readPath);
1896 }
epoger@google.com37269602013-01-19 04:21:27 +00001897 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
epoger@google.comb0f8b432013-04-10 18:46:25 +00001898 IndividualImageExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001899 } else {
epoger@google.comb0f8b432013-04-10 18:46:25 +00001900 if (FLAGS_verbose) {
1901 gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
1902 }
epoger@google.comd271d242013-02-13 18:14:48 +00001903 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1904 JsonExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001905 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00001906 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00001907 if (FLAGS_verbose) {
1908 if (FLAGS_writePath.count() == 1) {
1909 gm_fprintf(stdout, "writing to %s\n", FLAGS_writePath[0]);
1910 }
epoger@google.comfdea3252013-05-02 18:24:03 +00001911 if (NULL != gmmain.fMismatchPath) {
1912 gm_fprintf(stdout, "writing mismatches to %s\n", gmmain.fMismatchPath);
1913 }
epoger@google.comb0f8b432013-04-10 18:46:25 +00001914 if (FLAGS_writePicturePath.count() == 1) {
1915 gm_fprintf(stdout, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
1916 }
1917 if (FLAGS_resourcePath.count() == 1) {
1918 gm_fprintf(stdout, "reading resources from %s\n", FLAGS_resourcePath[0]);
1919 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001920 }
1921
epoger@google.com82cb65b2012-10-29 18:59:17 +00001922 if (moduloDivisor <= 0) {
1923 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001924 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001925 if (moduloRemainder < 0 || moduloRemainder >= moduloDivisor) {
1926 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001927 }
1928
epoger@google.com310478e2013-04-03 18:00:39 +00001929 int gmsRun = 0;
reed@google.comae7b8f32012-10-18 21:30:57 +00001930 int gmIndex = -1;
1931 SkString moduloStr;
1932
epoger@google.come8ebeb12012-10-29 16:42:11 +00001933 // If we will be writing out files, prepare subdirectories.
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001934 if (FLAGS_writePath.count() == 1) {
epoger@google.comfdea3252013-05-02 18:24:03 +00001935 if (!prepare_subdirectories(FLAGS_writePath[0], gmmain.fUseFileHierarchy, configs)) {
epoger@google.come8ebeb12012-10-29 16:42:11 +00001936 return -1;
1937 }
epoger@google.comfdea3252013-05-02 18:24:03 +00001938 }
1939 if (NULL != gmmain.fMismatchPath) {
1940 if (!prepare_subdirectories(gmmain.fMismatchPath, gmmain.fUseFileHierarchy, configs)) {
1941 return -1;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001942 }
1943 }
1944
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001945 if (FLAGS_pdfJpegQuality < -1 || FLAGS_pdfJpegQuality > 100) {
1946 gm_fprintf(stderr, "%s\n", "pdfJpegQuality must be in [-1 .. 100] range.");
1947 }
1948
bsalomon@google.com7361f542012-04-19 19:15:35 +00001949 Iter iter;
1950 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00001951 while ((gm = iter.next()) != NULL) {
scroggo@google.com7fd2d702013-04-16 19:11:14 +00001952 SkAutoTDelete<GM> adgm(gm);
reed@google.comae7b8f32012-10-18 21:30:57 +00001953 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00001954 if (moduloRemainder >= 0) {
1955 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00001956 continue;
1957 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001958 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00001959 }
1960
reed@google.comece2b022011-07-25 14:28:57 +00001961 const char* shortName = gm->shortName();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001962 if (skip_name(FLAGS_match, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00001963 continue;
1964 }
1965
epoger@google.com310478e2013-04-03 18:00:39 +00001966 gmsRun++;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001967 SkISize size = gm->getISize();
epoger@google.combcbf5aa2013-04-12 02:11:54 +00001968 gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
1969 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00001970
epoger@google.com310478e2013-04-03 18:00:39 +00001971 run_multiple_configs(gmmain, gm, configs, grFactory);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001972
1973 SkBitmap comparisonBitmap;
1974 const ConfigData compareConfig =
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001975 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
epoger@google.com310478e2013-04-03 18:00:39 +00001976 gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001977
epoger@google.com80724df2013-03-21 13:49:54 +00001978 // TODO(epoger): only run this if gmmain.generate_image() succeeded?
1979 // Otherwise, what are we comparing against?
epoger@google.com310478e2013-04-03 18:00:39 +00001980 run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap, tileGridReplayScales);
reed@android.com00dae862009-06-10 15:38:48 +00001981 }
epoger@google.com310478e2013-04-03 18:00:39 +00001982
1983 SkTArray<SkString> modes;
1984 gmmain.GetRenderModesEncountered(modes);
epoger@google.comc8263e72013-04-10 12:17:34 +00001985 bool reportError = false;
1986 if (gmmain.NumSignificantErrors() > 0) {
1987 reportError = true;
1988 }
epoger@google.com51dbabe2013-04-10 15:24:53 +00001989 int expectedNumberOfTests = gmsRun * (configs.count() + modes.count());
epoger@google.com310478e2013-04-03 18:00:39 +00001990
1991 // Output summary to stdout.
epoger@google.com51dbabe2013-04-10 15:24:53 +00001992 if (FLAGS_verbose) {
1993 gm_fprintf(stdout, "Ran %d GMs\n", gmsRun);
1994 gm_fprintf(stdout, "... over %2d configs [%s]\n", configs.count(),
1995 list_all_config_names(configs).c_str());
1996 gm_fprintf(stdout, "... and %2d modes [%s]\n", modes.count(), list_all(modes).c_str());
1997 gm_fprintf(stdout, "... so there should be a total of %d tests.\n", expectedNumberOfTests);
1998 }
1999 gmmain.ListErrors(FLAGS_verbose);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002000
epoger@google.com07947d92013-04-11 15:41:02 +00002001 // TODO(epoger): Enable this check for Android, too, once we resolve
2002 // https://code.google.com/p/skia/issues/detail?id=1222
2003 // ('GM is unexpectedly skipping tests on Android')
2004#ifndef SK_BUILD_FOR_ANDROID
epoger@google.comc8263e72013-04-10 12:17:34 +00002005 if (expectedNumberOfTests != gmmain.fTestsRun) {
2006 gm_fprintf(stderr, "expected %d tests, but ran or skipped %d tests\n",
2007 expectedNumberOfTests, gmmain.fTestsRun);
2008 reportError = true;
2009 }
2010#endif
2011
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002012 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.com76c913d2013-04-26 15:06:44 +00002013 Json::Value root = CreateJsonTree(
2014 gmmain.fJsonExpectedResults,
2015 gmmain.fJsonActualResults_Failed, gmmain.fJsonActualResults_FailureIgnored,
2016 gmmain.fJsonActualResults_NoComparison, gmmain.fJsonActualResults_Succeeded);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002017 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002018 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00002019 stream.write(jsonStdString.c_str(), jsonStdString.length());
2020 }
2021
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002022#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002023
robertphillips@google.com59552022012-08-31 13:07:37 +00002024#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002025 for (int i = 0; i < configs.count(); i++) {
2026 ConfigData config = gRec[configs[i]];
2027
epoger@google.comb0f8b432013-04-10 18:46:25 +00002028 if (FLAGS_verbose && (kGPU_Backend == config.fBackend)) {
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002029 GrContext* gr = grFactory->get(config.fGLContextType);
2030
epoger@google.com5efdd0c2013-03-13 14:18:40 +00002031 gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002032 gr->printCacheStats();
2033 }
2034 }
2035#endif
2036
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002037 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00002038#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00002039 SkGraphics::Term();
2040
epoger@google.comc8263e72013-04-10 12:17:34 +00002041 return (reportError) ? -1 : 0;
reed@android.com00dae862009-06-10 15:38:48 +00002042}
caryclark@google.com5987f582012-10-02 18:33:14 +00002043
scroggo@google.com09fd4d22013-03-20 14:20:18 +00002044void GMMain::installFilter(SkCanvas* canvas) {
2045 if (FLAGS_forceBWtext) {
2046 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
2047 }
2048}
2049
borenet@google.com7158e6a2012-11-01 17:43:44 +00002050#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00002051int main(int argc, char * const argv[]) {
2052 return tool_main(argc, (char**) argv);
2053}
2054#endif