blob: 6cb47aa0473edfb24d5b204121e5d4ba5917cfaa [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.comee8a8e32012-12-18 19:13:49 +000021#include "SkBitmapChecksummer.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.comf60494b2013-04-03 17:02:53 +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
reed@google.com8923c6c2011-11-08 14:59:38 +000066extern bool gSkSuppressFontCachePurgeSpew;
67
reed@google.com07700442010-12-20 19:46:07 +000068#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000069 #include "SkPDFDevice.h"
70 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000071#endif
reed@android.com00dae862009-06-10 15:38:48 +000072
epoger@google.come3cc2eb2012-01-18 20:11:13 +000073// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
74// stop writing out XPS-format image baselines in gm.
75#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000076#ifdef SK_SUPPORT_XPS
77 #include "SkXPSDevice.h"
78#endif
79
reed@google.com46cce912011-06-29 12:54:46 +000080#ifdef SK_BUILD_FOR_MAC
81 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000082 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000083#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000084 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000085#endif
86
reed@android.com00dae862009-06-10 15:38:48 +000087using namespace skiagm;
88
reed@android.com00dae862009-06-10 15:38:48 +000089class Iter {
90public:
91 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000092 this->reset();
93 }
94
95 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000096 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000097 }
reed@google.comd4dfd102011-01-18 21:05:42 +000098
reed@android.comdd0ac282009-06-20 02:38:16 +000099 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +0000100 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000101 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +0000102 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +0000103 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +0000104 }
105 return NULL;
106 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000107
reed@android.com00dae862009-06-10 15:38:48 +0000108 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +0000109 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +0000110 int count = 0;
111 while (reg) {
112 count += 1;
113 reg = reg->next();
114 }
115 return count;
116 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000117
reed@android.com00dae862009-06-10 15:38:48 +0000118private:
119 const GMRegistry* fReg;
120};
121
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000122enum Backend {
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000123 kRaster_Backend,
124 kGPU_Backend,
125 kPDF_Backend,
126 kXPS_Backend,
127};
128
129enum BbhType {
130 kNone_BbhType,
131 kRTree_BbhType,
132 kTileGrid_BbhType,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000133};
134
bsalomon@google.com7361f542012-04-19 19:15:35 +0000135enum ConfigFlags {
136 kNone_ConfigFlag = 0x0,
137 /* Write GM images if a write path is provided. */
138 kWrite_ConfigFlag = 0x1,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000139 /* Read reference GM images if a read path is provided. */
bsalomon@google.com7361f542012-04-19 19:15:35 +0000140 kRead_ConfigFlag = 0x2,
141 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
142};
143
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000144struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000145 SkBitmap::Config fConfig;
146 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000147 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000148 int fSampleCnt; // GPU backend only
149 ConfigFlags fFlags;
150 const char* fName;
bsalomon@google.com4c75f242013-03-19 18:58:43 +0000151 bool fRunByDefault;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000152};
153
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000154class BWTextDrawFilter : public SkDrawFilter {
155public:
reed@google.com971aca72012-11-26 20:26:54 +0000156 virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000157};
reed@google.com971aca72012-11-26 20:26:54 +0000158bool BWTextDrawFilter::filter(SkPaint* p, Type t) {
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000159 if (kText_Type == t) {
160 p->setAntiAlias(false);
161 }
reed@google.com971aca72012-11-26 20:26:54 +0000162 return true;
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000163}
164
scroggo@google.com565254b2012-06-28 15:41:32 +0000165struct PipeFlagComboData {
166 const char* name;
167 uint32_t flags;
168};
169
170static PipeFlagComboData gPipeWritingFlagCombos[] = {
171 { "", 0 },
172 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000173 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000174 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000175};
176
epoger@google.comde961632012-10-26 18:56:36 +0000177class GMMain {
178public:
epoger@google.comf60494b2013-04-03 17:02:53 +0000179 GMMain() : fUseFileHierarchy(false), fMismatchPath(NULL), fTestsRun(0),
180 fRenderModesEncountered(1) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000181 fIgnorableErrorCombination.add(kMissingExpectations_ErrorType);
epoger@google.come8ebeb12012-10-29 16:42:11 +0000182 }
183
184 SkString make_name(const char shortName[], const char configName[]) {
185 SkString name;
epoger@google.com57f7abc2012-11-13 03:41:55 +0000186 if (0 == strlen(configName)) {
187 name.append(shortName);
188 } else if (fUseFileHierarchy) {
epoger@google.come8ebeb12012-10-29 16:42:11 +0000189 name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
190 } else {
191 name.appendf("%s_%s", shortName, configName);
192 }
epoger@google.comde961632012-10-26 18:56:36 +0000193 return name;
194 }
195
epoger@google.com5f6a0072013-01-31 16:30:55 +0000196 /* since PNG insists on unpremultiplying our alpha, we take no
197 precision chances and force all pixels to be 100% opaque,
198 otherwise on compare we may not get a perfect match.
199 */
200 static void force_all_opaque(const SkBitmap& bitmap) {
201 SkBitmap::Config config = bitmap.config();
202 switch (config) {
203 case SkBitmap::kARGB_8888_Config:
204 force_all_opaque_8888(bitmap);
205 break;
206 case SkBitmap::kRGB_565_Config:
207 // nothing to do here; 565 bitmaps are inherently opaque
208 break;
209 default:
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000210 gm_fprintf(stderr, "unsupported bitmap config %d\n", config);
211 DEBUGFAIL_SEE_STDERR;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000212 }
213 }
214
215 static void force_all_opaque_8888(const SkBitmap& bitmap) {
216 SkAutoLockPixels lock(bitmap);
217 for (int y = 0; y < bitmap.height(); y++) {
218 for (int x = 0; x < bitmap.width(); x++) {
219 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
220 }
221 }
222 }
223
224 static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
225 // TODO(epoger): Now that we have removed force_all_opaque()
226 // from this method, we should be able to get rid of the
227 // transformation to 8888 format also.
228 SkBitmap copy;
229 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
230 return SkImageEncoder::EncodeFile(path.c_str(), copy,
231 SkImageEncoder::kPNG_Type, 100);
232 }
233
epoger@google.com6f6568b2013-03-22 17:29:46 +0000234 /**
epoger@google.comf60494b2013-04-03 17:02:53 +0000235 * Add all render modes encountered thus far to the "modes" array.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000236 */
epoger@google.comf60494b2013-04-03 17:02:53 +0000237 void GetRenderModesEncountered(SkTArray<SkString> &modes) {
238 SkTDict<int>::Iter iter(this->fRenderModesEncountered);
239 const char* mode;
240 while ((mode = iter.next(NULL)) != NULL) {
241 SkString modeAsString = SkString(mode);
242 // TODO(epoger): It seems a bit silly that all of these modes were
243 // recorded with a leading "-" which we have to remove here
244 // (except for mode "", which means plain old original mode).
245 // But that's how renderModeDescriptor has been passed into
246 // compare_test_results_to_reference_bitmap() historically,
247 // and changing that now may affect other parts of our code.
248 if (modeAsString.startsWith("-")) {
249 modeAsString.remove(0, 1);
250 modes.push_back(modeAsString);
251 }
252 }
253 }
254
255 /**
256 * Records the results of this test in fTestsRun and fFailedTests.
257 *
258 * We even record successes, and errors that we regard as
259 * "ignorable"; we can filter them out later.
260 */
261 void RecordTestResults(const ErrorCombination& errorCombination, const SkString& name,
262 const char renderModeDescriptor []) {
263 // Things to do regardless of errorCombination.
264 fTestsRun++;
265 int renderModeCount = 0;
266 this->fRenderModesEncountered.find(renderModeDescriptor, &renderModeCount);
267 renderModeCount++;
268 this->fRenderModesEncountered.set(renderModeDescriptor, renderModeCount);
269
epoger@google.com6f6568b2013-03-22 17:29:46 +0000270 if (errorCombination.isEmpty()) {
epoger@google.com5f6a0072013-01-31 16:30:55 +0000271 return;
epoger@google.comeb066362013-03-08 09:39:36 +0000272 }
273
epoger@google.comf60494b2013-04-03 17:02:53 +0000274 // Things to do only if there is some error condition.
275 SkString fullName = make_name(name.c_str(), renderModeDescriptor);
276 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
277 ErrorType type = static_cast<ErrorType>(typeInt);
278 if (errorCombination.includes(type)) {
279 fFailedTests[type].push_back(fullName);
epoger@google.com5f6a0072013-01-31 16:30:55 +0000280 }
281 }
282 }
283
epoger@google.comf60494b2013-04-03 17:02:53 +0000284 /**
285 * Return the number of significant (non-ignorable) errors we have
286 * encountered so far.
287 */
288 int NumSignificantErrors() {
289 int significantErrors = 0;
290 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
291 ErrorType type = static_cast<ErrorType>(typeInt);
292 if (!fIgnorableErrorCombination.includes(type)) {
293 significantErrors += fFailedTests[type].count();
294 }
295 }
296 return significantErrors;
297 }
298
299 /**
300 * List contents of fFailedTests to stdout.
301 */
302 void ListErrors() {
303 // First, print a single summary line.
304 SkString summary;
305 summary.appendf("Ran %d tests:", fTestsRun);
306 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
307 ErrorType type = static_cast<ErrorType>(typeInt);
308 summary.appendf(" %s=%d", getErrorTypeName(type), fFailedTests[type].count());
309 }
310 gm_fprintf(stdout, "%s\n", summary.c_str());
311
312 // Now, for each failure type, list the tests that failed that way.
313 for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
314 SkString line;
315 ErrorType type = static_cast<ErrorType>(typeInt);
316 if (fIgnorableErrorCombination.includes(type)) {
317 line.append("[ ] ");
318 } else {
319 line.append("[*] ");
320 }
321
322 SkTArray<SkString> *failedTestsOfThisType = &fFailedTests[type];
323 int count = failedTestsOfThisType->count();
324 line.appendf("%d %s:", count, getErrorTypeName(type));
325 for (int i = 0; i < count; ++i) {
326 line.append(" ");
327 line.append((*failedTestsOfThisType)[i]);
328 }
329 gm_fprintf(stdout, "%s\n", line.c_str());
330 }
331 gm_fprintf(stdout, "(results marked with [*] will cause nonzero return value)\n");
332 }
333
epoger@google.com5f6a0072013-01-31 16:30:55 +0000334 static bool write_document(const SkString& path,
335 const SkDynamicMemoryWStream& document) {
336 SkFILEWStream stream(path.c_str());
337 SkAutoDataUnref data(document.copyToData());
338 return stream.writeData(data.get());
339 }
340
epoger@google.com37269602013-01-19 04:21:27 +0000341 /**
epoger@google.com5f6a0072013-01-31 16:30:55 +0000342 * Prepare an SkBitmap to render a GM into.
343 *
344 * After you've rendered the GM into the SkBitmap, you must call
345 * complete_bitmap()!
346 *
347 * @todo thudson 22 April 2011 - could refactor this to take in
348 * a factory to generate the context, always call readPixels()
349 * (logically a noop for rasters, if wasted time), and thus collapse the
350 * GPU special case and also let this be used for SkPicture testing.
351 */
352 static void setup_bitmap(const ConfigData& gRec, SkISize& size,
353 SkBitmap* bitmap) {
354 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
355 bitmap->allocPixels();
356 bitmap->eraseColor(SK_ColorTRANSPARENT);
357 }
358
359 /**
360 * Any finalization steps we need to perform on the SkBitmap after
361 * we have rendered the GM into it.
epoger@google.com37269602013-01-19 04:21:27 +0000362 *
363 * It's too bad that we are throwing away alpha channel data
364 * we could otherwise be examining, but this had always been happening
365 * before... it was buried within the compare() method at
366 * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
367 *
368 * Apparently we need this, at least for bitmaps that are either:
369 * (a) destined to be written out as PNG files, or
370 * (b) compared against bitmaps read in from PNG files
371 * for the reasons described just above the force_all_opaque() method.
372 *
373 * Neglecting to do this led to the difficult-to-diagnose
374 * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
375 * spurious pixel_error messages as of r7258')
376 *
377 * TODO(epoger): Come up with a better solution that allows us to
378 * compare full pixel data, including alpha channel, while still being
379 * robust in the face of transformations to/from PNG files.
380 * Options include:
381 *
382 * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
383 * will be written to, or compared against, PNG files.
384 * PRO: Preserve/compare alpha channel info for the non-PNG cases
385 * (comparing different renderModes in-memory)
386 * CON: The bitmaps (and checksums) for these non-PNG cases would be
387 * different than those for the PNG-compared cases, and in the
388 * case of a failed renderMode comparison, how would we write the
389 * image to disk for examination?
390 *
391 * 2. Always compute image checksums from PNG format (either
392 * directly from the the bytes of a PNG file, or capturing the
393 * bytes we would have written to disk if we were writing the
394 * bitmap out as a PNG).
395 * PRO: I think this would allow us to never force opaque, and to
396 * the extent that alpha channel data can be preserved in a PNG
397 * file, we could observe it.
398 * CON: If we read a bitmap from disk, we need to take its checksum
399 * from the source PNG (we can't compute it from the bitmap we
400 * read out of the PNG, because we will have already premultiplied
401 * the alpha).
402 * CON: Seems wasteful to convert a bitmap to PNG format just to take
403 * its checksum. (Although we're wasting lots of effort already
404 * calling force_all_opaque().)
405 *
406 * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
407 * so we can transform images back and forth without fear of off-by-one
408 * errors.
409 * CON: Math is hard.
410 *
411 * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
412 * channel), rather than demanding absolute equality.
413 * CON: Can't do this with checksums.
414 */
epoger@google.com5f6a0072013-01-31 16:30:55 +0000415 static void complete_bitmap(SkBitmap* bitmap) {
416 force_all_opaque(*bitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000417 }
418
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000419 static void installFilter(SkCanvas* canvas);
epoger@google.comde961632012-10-26 18:56:36 +0000420
reed@google.comaef73612012-11-16 13:41:45 +0000421 static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000422 SkAutoCanvasRestore acr(canvas, true);
423
424 if (!isPDF) {
425 canvas->concat(gm->getInitialTransform());
426 }
427 installFilter(canvas);
reed@google.comaef73612012-11-16 13:41:45 +0000428 gm->setCanvasIsDeferred(isDeferred);
epoger@google.comde961632012-10-26 18:56:36 +0000429 gm->draw(canvas);
430 canvas->setDrawFilter(NULL);
431 }
432
epoger@google.com6f6568b2013-03-22 17:29:46 +0000433 static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000434 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000435 SkBitmap* bitmap,
436 bool deferred) {
epoger@google.comde961632012-10-26 18:56:36 +0000437 SkISize size (gm->getISize());
438 setup_bitmap(gRec, size, bitmap);
439
440 SkAutoTUnref<SkCanvas> canvas;
441
442 if (gRec.fBackend == kRaster_Backend) {
443 SkAutoTUnref<SkDevice> device(new SkDevice(*bitmap));
444 if (deferred) {
445 canvas.reset(new SkDeferredCanvas(device));
446 } else {
447 canvas.reset(new SkCanvas(device));
448 }
reed@google.comaef73612012-11-16 13:41:45 +0000449 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000450 canvas->flush();
451 }
452#if SK_SUPPORT_GPU
453 else { // GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000454 SkAutoTUnref<SkDevice> device(SkGpuDevice::Create(gpuTarget));
epoger@google.comde961632012-10-26 18:56:36 +0000455 if (deferred) {
456 canvas.reset(new SkDeferredCanvas(device));
457 } else {
458 canvas.reset(new SkCanvas(device));
459 }
reed@google.comaef73612012-11-16 13:41:45 +0000460 invokeGM(gm, canvas, false, deferred);
epoger@google.comde961632012-10-26 18:56:36 +0000461 // the device is as large as the current rendertarget, so
462 // we explicitly only readback the amount we expect (in
463 // size) overwrite our previous allocation
464 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
465 size.fHeight);
466 canvas->readPixels(bitmap, 0, 0);
467 }
468#endif
epoger@google.com5f6a0072013-01-31 16:30:55 +0000469 complete_bitmap(bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000470 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000471 }
472
473 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
junov@chromium.orgc938c482012-12-19 15:24:38 +0000474 SkPicture* pict, SkBitmap* bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000475 SkScalar scale = SK_Scalar1,
476 bool tile = false) {
epoger@google.comde961632012-10-26 18:56:36 +0000477 SkISize size = gm->getISize();
478 setup_bitmap(gRec, size, bitmap);
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000479
480 if (tile) {
481 // Generate the result image by rendering to tiles and accumulating
482 // the results in 'bitmap'
483
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000484 // This 16x16 tiling matches the settings applied to 'pict' in
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000485 // 'generate_new_picture'
486 SkISize tileSize = SkISize::Make(16, 16);
487
488 SkBitmap tileBM;
489 setup_bitmap(gRec, tileSize, &tileBM);
490 SkCanvas tileCanvas(tileBM);
491 installFilter(&tileCanvas);
492
493 SkCanvas bmpCanvas(*bitmap);
494 SkPaint bmpPaint;
495 bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
496
497 for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
498 for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
499 int saveCount = tileCanvas.save();
500 SkMatrix mat(tileCanvas.getTotalMatrix());
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000501 mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000502 SkIntToScalar(-yTile*tileSize.height()));
503 tileCanvas.setMatrix(mat);
504 pict->draw(&tileCanvas);
505 tileCanvas.flush();
506 tileCanvas.restoreToCount(saveCount);
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000507 bmpCanvas.drawBitmap(tileBM,
508 SkIntToScalar(xTile * tileSize.width()),
robertphillips@google.com5a7d0292013-04-02 15:18:41 +0000509 SkIntToScalar(yTile * tileSize.height()),
510 &bmpPaint);
511 }
512 }
513 } else {
514 SkCanvas canvas(*bitmap);
515 installFilter(&canvas);
516 canvas.scale(scale, scale);
517 canvas.drawPicture(*pict);
518 complete_bitmap(bitmap);
519 }
epoger@google.comde961632012-10-26 18:56:36 +0000520 }
521
522 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
523#ifdef SK_SUPPORT_PDF
524 SkMatrix initialTransform = gm->getInitialTransform();
525 SkISize pageSize = gm->getISize();
526 SkPDFDevice* dev = NULL;
527 if (initialTransform.isIdentity()) {
528 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
529 } else {
530 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
531 SkIntToScalar(pageSize.height()));
532 initialTransform.mapRect(&content);
533 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
534 SkIntToScalar(pageSize.height()));
535 SkISize contentSize =
536 SkISize::Make(SkScalarRoundToInt(content.width()),
537 SkScalarRoundToInt(content.height()));
538 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
539 }
540 SkAutoUnref aur(dev);
541
542 SkCanvas c(dev);
reed@google.comaef73612012-11-16 13:41:45 +0000543 invokeGM(gm, &c, true, false);
epoger@google.comde961632012-10-26 18:56:36 +0000544
545 SkPDFDocument doc;
546 doc.appendPage(dev);
547 doc.emitPDF(&pdf);
548#endif
549 }
550
551 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
552#ifdef SK_SUPPORT_XPS
553 SkISize size = gm->getISize();
554
555 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
556 SkIntToScalar(size.height()));
557 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
558 static const SkScalar upm = 72 * inchesPerMeter;
559 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
560 static const SkScalar ppm = 200 * inchesPerMeter;
561 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
562
563 SkXPSDevice* dev = new SkXPSDevice();
564 SkAutoUnref aur(dev);
565
566 SkCanvas c(dev);
567 dev->beginPortfolio(&xps);
568 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
reed@google.comaef73612012-11-16 13:41:45 +0000569 invokeGM(gm, &c, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000570 dev->endSheet();
571 dev->endPortfolio();
572
573#endif
574 }
575
epoger@google.com6f6568b2013-03-22 17:29:46 +0000576 ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
577 const char renderModeDescriptor [], const SkString& name,
578 SkBitmap& bitmap, SkDynamicMemoryWStream* document) {
epoger@google.comde961632012-10-26 18:56:36 +0000579 SkString path;
580 bool success = false;
581 if (gRec.fBackend == kRaster_Backend ||
582 gRec.fBackend == kGPU_Backend ||
583 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
584
epoger@google.com37269602013-01-19 04:21:27 +0000585 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
586 "png");
epoger@google.comde961632012-10-26 18:56:36 +0000587 success = write_bitmap(path, bitmap);
588 }
589 if (kPDF_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000590 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
591 "pdf");
epoger@google.comde961632012-10-26 18:56:36 +0000592 success = write_document(path, *document);
593 }
594 if (kXPS_Backend == gRec.fBackend) {
epoger@google.com37269602013-01-19 04:21:27 +0000595 path = make_filename(writePath, renderModeDescriptor, name.c_str(),
596 "xps");
epoger@google.comde961632012-10-26 18:56:36 +0000597 success = write_document(path, *document);
598 }
599 if (success) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000600 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000601 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000602 gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000603 ErrorCombination errors(kWritingReferenceImage_ErrorType);
epoger@google.comf60494b2013-04-03 17:02:53 +0000604 // TODO(epoger): Don't call RecordTestResults() here...
605 // Instead, we should make sure to call RecordTestResults
606 // exactly ONCE per test. (Otherwise, gmmain.fTestsRun
607 // will be incremented twice for this test: once in
608 // compare_test_results_to_stored_expectations() before
609 // that method calls this one, and again here.)
610 //
611 // When we make that change, we should probably add a
612 // WritingReferenceImage test to the gm self-tests.)
613 RecordTestResults(errors, name, renderModeDescriptor);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000614 return errors;
epoger@google.comde961632012-10-26 18:56:36 +0000615 }
616 }
617
epoger@google.com37269602013-01-19 04:21:27 +0000618 /**
epoger@google.com84a18022013-02-01 20:39:15 +0000619 * Log more detail about the mistmatch between expectedBitmap and
620 * actualBitmap.
621 */
622 void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
623 const char *testName) {
624 const int expectedWidth = expectedBitmap.width();
625 const int expectedHeight = expectedBitmap.height();
626 const int width = actualBitmap.width();
627 const int height = actualBitmap.height();
628 if ((expectedWidth != width) || (expectedHeight != height)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000629 gm_fprintf(stderr, "---- %s: dimension mismatch --"
630 " expected [%d %d], actual [%d %d]\n",
631 testName, expectedWidth, expectedHeight, width, height);
epoger@google.com84a18022013-02-01 20:39:15 +0000632 return;
633 }
634
635 if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
636 (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000637 gm_fprintf(stderr, "---- %s: not computing max per-channel"
638 " pixel mismatch because non-8888\n", testName);
epoger@google.com84a18022013-02-01 20:39:15 +0000639 return;
640 }
641
642 SkAutoLockPixels alp0(expectedBitmap);
643 SkAutoLockPixels alp1(actualBitmap);
644 int errR = 0;
645 int errG = 0;
646 int errB = 0;
647 int errA = 0;
648 int differingPixels = 0;
649
650 for (int y = 0; y < height; ++y) {
651 const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
652 const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
653 for (int x = 0; x < width; ++x) {
654 SkPMColor expectedPixel = *expectedPixelPtr++;
655 SkPMColor actualPixel = *actualPixelPtr++;
656 if (expectedPixel != actualPixel) {
657 differingPixels++;
658 errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
659 (int)SkGetPackedR32(actualPixel)));
660 errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
661 (int)SkGetPackedG32(actualPixel)));
662 errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
663 (int)SkGetPackedB32(actualPixel)));
664 errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
665 (int)SkGetPackedA32(actualPixel)));
666 }
667 }
668 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +0000669 gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
670 " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
671 testName, differingPixels, width*height, errR, errG, errB, errA);
epoger@google.com84a18022013-02-01 20:39:15 +0000672 }
673
674 /**
epoger@google.com6f6568b2013-03-22 17:29:46 +0000675 * Compares actual checksum to expectations, returning the set of errors
676 * (if any) that we saw along the way.
epoger@google.com37269602013-01-19 04:21:27 +0000677 *
678 * If fMismatchPath has been set, and there are pixel diffs, then the
679 * actual bitmap will be written out to a file within fMismatchPath.
680 *
681 * @param expectations what expectations to compare actualBitmap against
682 * @param actualBitmap the image we actually generated
683 * @param baseNameString name of test without renderModeDescriptor added
684 * @param renderModeDescriptor e.g., "-rtree", "-deferred"
685 * @param addToJsonSummary whether to add these results (both actual and
686 * expected) to the JSON summary
687 *
688 * TODO: For now, addToJsonSummary is only set to true within
689 * compare_test_results_to_stored_expectations(), so results of our
690 * in-memory comparisons (Rtree vs regular, etc.) are not written to the
691 * JSON summary. We may wish to change that.
692 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000693 ErrorCombination compare_to_expectations(Expectations expectations,
694 const SkBitmap& actualBitmap,
695 const SkString& baseNameString,
696 const char renderModeDescriptor[],
697 bool addToJsonSummary=false) {
698 ErrorCombination errors;
epoger@google.com5f6a0072013-01-31 16:30:55 +0000699 Checksum actualChecksum = SkBitmapChecksummer::Compute64(actualBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000700 SkString completeNameString = baseNameString;
701 completeNameString.append(renderModeDescriptor);
702 const char* completeName = completeNameString.c_str();
epoger@google.comee8a8e32012-12-18 19:13:49 +0000703
epoger@google.com37269602013-01-19 04:21:27 +0000704 if (expectations.empty()) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000705 errors.add(kMissingExpectations_ErrorType);
706 } else if (!expectations.match(actualChecksum)) {
707 errors.add(kImageMismatch_ErrorType);
epoger@google.com84a18022013-02-01 20:39:15 +0000708
709 // Write out the "actuals" for any mismatches, if we have
710 // been directed to do so.
epoger@google.com37269602013-01-19 04:21:27 +0000711 if (fMismatchPath) {
712 SkString path =
713 make_filename(fMismatchPath, renderModeDescriptor,
714 baseNameString.c_str(), "png");
715 write_bitmap(path, actualBitmap);
716 }
epoger@google.com84a18022013-02-01 20:39:15 +0000717
718 // If we have access to a single expected bitmap, log more
719 // detail about the mismatch.
720 const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
721 if (NULL != expectedBitmapPtr) {
722 report_bitmap_diffs(*expectedBitmapPtr, actualBitmap, completeName);
723 }
epoger@google.coma243b222013-01-17 17:54:28 +0000724 }
epoger@google.comf60494b2013-04-03 17:02:53 +0000725 RecordTestResults(errors, baseNameString, renderModeDescriptor);
epoger@google.coma243b222013-01-17 17:54:28 +0000726
epoger@google.com37269602013-01-19 04:21:27 +0000727 if (addToJsonSummary) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000728 add_actual_results_to_json_summary(completeName, actualChecksum, errors,
epoger@google.com37269602013-01-19 04:21:27 +0000729 expectations.ignoreFailure());
730 add_expected_results_to_json_summary(completeName, expectations);
731 }
epoger@google.coma243b222013-01-17 17:54:28 +0000732
epoger@google.com6f6568b2013-03-22 17:29:46 +0000733 return errors;
epoger@google.com06b8a192013-01-15 19:10:16 +0000734 }
735
epoger@google.com37269602013-01-19 04:21:27 +0000736 /**
737 * Add this result to the appropriate JSON collection of actual results,
738 * depending on status.
739 */
740 void add_actual_results_to_json_summary(const char testName[],
741 Checksum actualChecksum,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000742 ErrorCombination result,
epoger@google.com37269602013-01-19 04:21:27 +0000743 bool ignoreFailure) {
744 Json::Value actualResults;
745 actualResults[kJsonKey_ActualResults_AnyStatus_Checksum] =
746 asJsonValue(actualChecksum);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000747 if (result.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +0000748 this->fJsonActualResults_Succeeded[testName] = actualResults;
749 } else {
750 if (ignoreFailure) {
751 // TODO: Once we have added the ability to compare
752 // actual results against expectations in a JSON file
753 // (where we can set ignore-failure to either true or
epoger@google.com84a18022013-02-01 20:39:15 +0000754 // false), add test cases that exercise ignored
epoger@google.com6f6568b2013-03-22 17:29:46 +0000755 // failures (both for kMissingExpectations_ErrorType
756 // and kImageMismatch_ErrorType).
epoger@google.com37269602013-01-19 04:21:27 +0000757 this->fJsonActualResults_FailureIgnored[testName] =
758 actualResults;
759 } else {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000760 if (result.includes(kMissingExpectations_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000761 // TODO: What about the case where there IS an
762 // expected image checksum, but that gm test
763 // doesn't actually run? For now, those cases
764 // will always be ignored, because gm only looks
765 // at expectations that correspond to gm tests
766 // that were actually run.
767 //
768 // Once we have the ability to express
769 // expectations as a JSON file, we should fix this
770 // (and add a test case for which an expectation
771 // is given but the test is never run).
772 this->fJsonActualResults_NoComparison[testName] =
773 actualResults;
epoger@google.comeb066362013-03-08 09:39:36 +0000774 }
epoger@google.com6f6568b2013-03-22 17:29:46 +0000775 if (result.includes(kImageMismatch_ErrorType)) {
epoger@google.com37269602013-01-19 04:21:27 +0000776 this->fJsonActualResults_Failed[testName] = actualResults;
epoger@google.com37269602013-01-19 04:21:27 +0000777 }
778 }
779 }
780 }
781
782 /**
783 * Add this test to the JSON collection of expected results.
784 */
785 void add_expected_results_to_json_summary(const char testName[],
786 Expectations expectations) {
787 // For now, we assume that this collection starts out empty and we
788 // just fill it in as we go; once gm accepts a JSON file as input,
789 // we'll have to change that.
790 Json::Value expectedResults;
791 expectedResults[kJsonKey_ExpectedResults_Checksums] =
792 expectations.allowedChecksumsAsJson();
793 expectedResults[kJsonKey_ExpectedResults_IgnoreFailure] =
794 expectations.ignoreFailure();
795 this->fJsonExpectedResults[testName] = expectedResults;
796 }
797
798 /**
799 * Compare actualBitmap to expectations stored in this->fExpectationsSource.
800 *
801 * @param gm which test generated the actualBitmap
802 * @param gRec
803 * @param writePath unless this is NULL, write out actual images into this
804 * directory
805 * @param actualBitmap bitmap generated by this run
806 * @param pdf
807 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000808 ErrorCombination compare_test_results_to_stored_expectations(
epoger@google.com37269602013-01-19 04:21:27 +0000809 GM* gm, const ConfigData& gRec, const char writePath[],
810 SkBitmap& actualBitmap, SkDynamicMemoryWStream* pdf) {
811
epoger@google.coma243b222013-01-17 17:54:28 +0000812 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000813 ErrorCombination errors;
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000814
epoger@google.com6f6568b2013-03-22 17:29:46 +0000815 ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
epoger@google.com37269602013-01-19 04:21:27 +0000816 if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
817 /*
818 * Get the expected results for this test, as one or more allowed
819 * checksums. The current implementation of expectationsSource
820 * get this by computing the checksum of a single PNG file on disk.
821 *
822 * TODO(epoger): This relies on the fact that
823 * force_all_opaque() was called on the bitmap before it
824 * was written to disk as a PNG in the first place. If
825 * not, the checksum returned here may not match the
826 * checksum of actualBitmap, which *has* been run through
827 * force_all_opaque().
epoger@google.com5f6a0072013-01-31 16:30:55 +0000828 * See comments above complete_bitmap() for more detail.
epoger@google.com37269602013-01-19 04:21:27 +0000829 */
830 Expectations expectations = expectationsSource->get(name.c_str());
epoger@google.com6f6568b2013-03-22 17:29:46 +0000831 errors.add(compare_to_expectations(expectations, actualBitmap,
832 name, "", true));
epoger@google.com37269602013-01-19 04:21:27 +0000833 } else {
834 // If we are running without expectations, we still want to
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000835 // record the actual results.
epoger@google.com5f6a0072013-01-31 16:30:55 +0000836 Checksum actualChecksum =
837 SkBitmapChecksummer::Compute64(actualBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000838 add_actual_results_to_json_summary(name.c_str(), actualChecksum,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000839 ErrorCombination(kMissingExpectations_ErrorType),
epoger@google.com37269602013-01-19 04:21:27 +0000840 false);
epoger@google.comf60494b2013-04-03 17:02:53 +0000841 RecordTestResults(ErrorCombination(kMissingExpectations_ErrorType), name, "");
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000842 }
epoger@google.com37269602013-01-19 04:21:27 +0000843
844 // TODO: Consider moving this into compare_to_expectations(),
845 // similar to fMismatchPath... for now, we don't do that, because
846 // we don't want to write out the actual bitmaps for all
847 // renderModes of all tests! That would be a lot of files.
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000848 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
epoger@google.com6f6568b2013-03-22 17:29:46 +0000849 errors.add(write_reference_image(gRec, writePath, "",
850 name, actualBitmap, pdf));
epoger@google.com9ef89ce2013-01-18 21:45:42 +0000851 }
epoger@google.com37269602013-01-19 04:21:27 +0000852
epoger@google.com6f6568b2013-03-22 17:29:46 +0000853 return errors;
epoger@google.coma243b222013-01-17 17:54:28 +0000854 }
855
epoger@google.com37269602013-01-19 04:21:27 +0000856 /**
857 * Compare actualBitmap to referenceBitmap.
858 *
859 * @param gm which test generated the bitmap
860 * @param gRec
861 * @param renderModeDescriptor
862 * @param actualBitmap actual bitmap generated by this run
863 * @param referenceBitmap bitmap we expected to be generated
864 */
epoger@google.com6f6568b2013-03-22 17:29:46 +0000865 ErrorCombination compare_test_results_to_reference_bitmap(
epoger@google.com37269602013-01-19 04:21:27 +0000866 GM* gm, const ConfigData& gRec, const char renderModeDescriptor [],
867 SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
868
epoger@google.comf60494b2013-04-03 17:02:53 +0000869 // TODO(epoger): This method is run to compare results across
870 // different rendering modes (as opposed to
871 // compare_test_results_to_stored_expectations(), which
872 // compares results against expectations stored on disk). If
873 // we would like the GenerateGMs step to distinguish between
874 // those two types of mismatches, we should report image
875 // mismatches in here with a different ErrorType.
epoger@google.com37269602013-01-19 04:21:27 +0000876 SkASSERT(referenceBitmap);
877 SkString name = make_name(gm->shortName(), gRec.fName);
epoger@google.com84a18022013-02-01 20:39:15 +0000878 Expectations expectations(*referenceBitmap);
epoger@google.com37269602013-01-19 04:21:27 +0000879 return compare_to_expectations(expectations, actualBitmap,
880 name, renderModeDescriptor);
881 }
882
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000883 static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
884 SkScalar scale = SK_Scalar1) {
epoger@google.comde961632012-10-26 18:56:36 +0000885 // Pictures are refcounted so must be on heap
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000886 SkPicture* pict;
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000887 int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
888 int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
skia.committer@gmail.comd8b27992012-12-20 02:01:41 +0000889
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000890 if (kTileGrid_BbhType == bbhType) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000891 SkTileGridPicture::TileGridInfo info;
892 info.fMargin.setEmpty();
893 info.fOffset.setZero();
894 info.fTileInterval.set(16, 16);
895 pict = new SkTileGridPicture(width, height, info);
junov@chromium.org3cb834b2012-12-13 16:39:53 +0000896 } else {
897 pict = new SkPicture;
898 }
junov@chromium.org20bd04e2013-01-16 18:43:36 +0000899 if (kNone_BbhType != bbhType) {
900 recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
901 }
junov@chromium.org706ff2f2012-12-19 15:55:40 +0000902 SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
junov@chromium.orgc938c482012-12-19 15:24:38 +0000903 cv->scale(scale, scale);
reed@google.comaef73612012-11-16 13:41:45 +0000904 invokeGM(gm, cv, false, false);
epoger@google.comde961632012-10-26 18:56:36 +0000905 pict->endRecording();
906
907 return pict;
908 }
909
910 static SkPicture* stream_to_new_picture(const SkPicture& src) {
911
912 // To do in-memory commiunications with a stream, we need to:
913 // * create a dynamic memory stream
914 // * copy it into a buffer
915 // * create a read stream from it
916 // ?!?!
917
918 SkDynamicMemoryWStream storage;
919 src.serialize(&storage);
920
scroggo@google.com09fd4d22013-03-20 14:20:18 +0000921 size_t streamSize = storage.getOffset();
epoger@google.comde961632012-10-26 18:56:36 +0000922 SkAutoMalloc dstStorage(streamSize);
923 void* dst = dstStorage.get();
924 //char* dst = new char [streamSize];
925 //@todo thudson 22 April 2011 when can we safely delete [] dst?
926 storage.copyTo(dst);
927 SkMemoryStream pictReadback(dst, streamSize);
928 SkPicture* retval = new SkPicture (&pictReadback);
929 return retval;
930 }
931
932 // Test: draw into a bitmap or pdf.
epoger@google.com15655b22013-01-08 18:47:31 +0000933 // Depending on flags, possibly compare to an expected image.
epoger@google.com6f6568b2013-03-22 17:29:46 +0000934 ErrorCombination test_drawing(GM* gm,
935 const ConfigData& gRec,
936 const char writePath [],
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000937 GrSurface* gpuTarget,
epoger@google.com6f6568b2013-03-22 17:29:46 +0000938 SkBitmap* bitmap) {
epoger@google.comde961632012-10-26 18:56:36 +0000939 SkDynamicMemoryWStream document;
940
941 if (gRec.fBackend == kRaster_Backend ||
942 gRec.fBackend == kGPU_Backend) {
943 // Early exit if we can't generate the image.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000944 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000945 if (!errors.isEmpty()) {
epoger@google.com37269602013-01-19 04:21:27 +0000946 // TODO: Add a test to exercise what the stdout and
947 // JSON look like if we get an "early error" while
948 // trying to generate the image.
epoger@google.comde961632012-10-26 18:56:36 +0000949 return errors;
950 }
951 } else if (gRec.fBackend == kPDF_Backend) {
952 generate_pdf(gm, document);
953#if CAN_IMAGE_PDF
954 SkAutoDataUnref data(document.copyToData());
955 SkMemoryStream stream(data->data(), data->size());
956 SkPDFDocumentToBitmap(&stream, bitmap);
957#endif
958 } else if (gRec.fBackend == kXPS_Backend) {
959 generate_xps(gm, document);
960 }
epoger@google.com37269602013-01-19 04:21:27 +0000961 return compare_test_results_to_stored_expectations(
962 gm, gRec, writePath, *bitmap, &document);
epoger@google.comde961632012-10-26 18:56:36 +0000963 }
964
epoger@google.com6f6568b2013-03-22 17:29:46 +0000965 ErrorCombination test_deferred_drawing(GM* gm,
966 const ConfigData& gRec,
967 const SkBitmap& referenceBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000968 GrSurface* gpuTarget) {
epoger@google.comde961632012-10-26 18:56:36 +0000969 SkDynamicMemoryWStream document;
970
971 if (gRec.fBackend == kRaster_Backend ||
972 gRec.fBackend == kGPU_Backend) {
973 SkBitmap bitmap;
974 // Early exit if we can't generate the image, but this is
975 // expected in some cases, so don't report a test failure.
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000976 ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
epoger@google.com6f6568b2013-03-22 17:29:46 +0000977 // TODO(epoger): This logic is the opposite of what is
978 // described above... if we succeeded in generating the
979 // -deferred image, we exit early! We should fix this
980 // ASAP, because it is hiding -deferred errors... but for
981 // now, I'm leaving the logic as it is so that the
982 // refactoring change
983 // https://codereview.chromium.org/12992003/ is unblocked.
984 //
985 // Filed as https://code.google.com/p/skia/issues/detail?id=1180
986 // ('image-surface gm test is failing in "deferred" mode,
987 // and gm is not reporting the failure')
988 if (errors.isEmpty()) {
epoger@google.comf60494b2013-04-03 17:02:53 +0000989 // TODO(epoger): Report this as a new ErrorType,
990 // something like kImageGeneration_ErrorType?
epoger@google.com6f6568b2013-03-22 17:29:46 +0000991 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000992 }
epoger@google.com37269602013-01-19 04:21:27 +0000993 return compare_test_results_to_reference_bitmap(
994 gm, gRec, "-deferred", bitmap, &referenceBitmap);
epoger@google.comde961632012-10-26 18:56:36 +0000995 }
epoger@google.com6f6568b2013-03-22 17:29:46 +0000996 return kEmpty_ErrorCombination;
epoger@google.comde961632012-10-26 18:56:36 +0000997 }
998
epoger@google.com6f6568b2013-03-22 17:29:46 +0000999 ErrorCombination test_pipe_playback(GM* gm,
1000 const ConfigData& gRec,
1001 const SkBitmap& referenceBitmap) {
1002 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001003 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
1004 SkBitmap bitmap;
1005 SkISize size = gm->getISize();
1006 setup_bitmap(gRec, size, &bitmap);
1007 SkCanvas canvas(bitmap);
scroggo@google.com0b735632013-03-19 17:38:50 +00001008 installFilter(&canvas);
epoger@google.comde961632012-10-26 18:56:36 +00001009 PipeController pipeController(&canvas);
1010 SkGPipeWriter writer;
1011 SkCanvas* pipeCanvas = writer.startRecording(
1012 &pipeController, gPipeWritingFlagCombos[i].flags);
reed@google.comaef73612012-11-16 13:41:45 +00001013 invokeGM(gm, pipeCanvas, false, false);
epoger@google.com5f6a0072013-01-31 16:30:55 +00001014 complete_bitmap(&bitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001015 writer.endRecording();
1016 SkString string("-pipe");
1017 string.append(gPipeWritingFlagCombos[i].name);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001018 errors.add(compare_test_results_to_reference_bitmap(
1019 gm, gRec, string.c_str(), bitmap, &referenceBitmap));
1020 if (!errors.isEmpty()) {
epoger@google.comde961632012-10-26 18:56:36 +00001021 break;
1022 }
1023 }
1024 return errors;
1025 }
1026
epoger@google.com6f6568b2013-03-22 17:29:46 +00001027 ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
1028 const SkBitmap& referenceBitmap) {
1029 ErrorCombination errors;
epoger@google.comde961632012-10-26 18:56:36 +00001030 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
1031 SkBitmap bitmap;
1032 SkISize size = gm->getISize();
1033 setup_bitmap(gRec, size, &bitmap);
1034 SkCanvas canvas(bitmap);
scroggo@google.com0b735632013-03-19 17:38:50 +00001035 installFilter(&canvas);
epoger@google.comde961632012-10-26 18:56:36 +00001036 TiledPipeController pipeController(bitmap);
1037 SkGPipeWriter writer;
1038 SkCanvas* pipeCanvas = writer.startRecording(
1039 &pipeController, gPipeWritingFlagCombos[i].flags);
reed@google.comaef73612012-11-16 13:41:45 +00001040 invokeGM(gm, pipeCanvas, false, false);
epoger@google.com5f6a0072013-01-31 16:30:55 +00001041 complete_bitmap(&bitmap);
epoger@google.comde961632012-10-26 18:56:36 +00001042 writer.endRecording();
1043 SkString string("-tiled pipe");
1044 string.append(gPipeWritingFlagCombos[i].name);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001045 errors.add(compare_test_results_to_reference_bitmap(
1046 gm, gRec, string.c_str(), bitmap, &referenceBitmap));
1047 if (!errors.isEmpty()) {
epoger@google.comde961632012-10-26 18:56:36 +00001048 break;
1049 }
1050 }
1051 return errors;
1052 }
epoger@google.come8ebeb12012-10-29 16:42:11 +00001053
1054 //
1055 // member variables.
1056 // They are public for now, to allow easier setting by tool_main().
1057 //
1058
epoger@google.come8ebeb12012-10-29 16:42:11 +00001059 bool fUseFileHierarchy;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001060 ErrorCombination fIgnorableErrorCombination;
epoger@google.come8ebeb12012-10-29 16:42:11 +00001061
junov@chromium.org95146eb2013-01-11 21:04:40 +00001062 const char* fMismatchPath;
1063
epoger@google.comf60494b2013-04-03 17:02:53 +00001064 // collection of tests that have failed with each ErrorType
1065 SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
1066 int fTestsRun;
1067 SkTDict<int> fRenderModesEncountered;
epoger@google.com57f7abc2012-11-13 03:41:55 +00001068
epoger@google.com37269602013-01-19 04:21:27 +00001069 // Where to read expectations (expected image checksums, etc.) from.
1070 // If unset, we don't do comparisons.
1071 SkAutoTUnref<ExpectationsSource> fExpectationsSource;
1072
1073 // JSON summaries that we generate as we go (just for output).
epoger@google.comee8a8e32012-12-18 19:13:49 +00001074 Json::Value fJsonExpectedResults;
1075 Json::Value fJsonActualResults_Failed;
1076 Json::Value fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001077 Json::Value fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001078 Json::Value fJsonActualResults_Succeeded;
1079
epoger@google.comde961632012-10-26 18:56:36 +00001080}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +00001081
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001082#if SK_SUPPORT_GPU
1083static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1084#else
1085static const GLContextType kDontCare_GLContextType = 0;
1086#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +00001087
1088// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +00001089// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +00001090static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
1091 kWrite_ConfigFlag;
1092
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001093static const ConfigData gRec[] = {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001094 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001095#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 +00001096 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444", true },
reed@google.com69dc4ff2012-11-29 21:21:54 +00001097#endif
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001098 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565", true },
1099#if SK_SUPPORT_GPU
1100 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu", true },
1101 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16", true },
1102 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 4, kRW_ConfigFlag, "msaa4", false},
bsalomon@google.com7361f542012-04-19 19:15:35 +00001103 /* The debug context does not generate images */
scroggo@google.com0f567c62013-03-20 15:35:08 +00001104 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "gpudebug", GR_DEBUG},
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001105#if SK_ANGLE
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001106 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle", true },
1107 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001108#endif // SK_ANGLE
1109#ifdef SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001110 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001111#endif // SK_MESA
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001112#endif // SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +00001113#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001114 /* 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 +00001115 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps", true },
robertphillips@google.coma73e8602012-08-02 17:56:02 +00001116#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +00001117#ifdef SK_SUPPORT_PDF
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001118 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf", true },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001119#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +00001120};
1121
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001122static SkString configUsage() {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001123 SkString result;
1124 result.appendf("Space delimited list of which configs to run. Possible options: [");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001125 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1126 if (i > 0) {
scroggo@google.com0f567c62013-03-20 15:35:08 +00001127 result.append("|");
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001128 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001129 result.appendf("%s", gRec[i].fName);
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001130 }
scroggo@google.com0f567c62013-03-20 15:35:08 +00001131 result.append("]\n");
1132 result.appendf("The default value is: \"");
1133 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1134 if (gRec[i].fRunByDefault) {
1135 if (i > 0) {
1136 result.append(" ");
1137 }
1138 result.appendf("%s", gRec[i].fName);
1139 }
1140 }
1141 result.appendf("\"");
1142
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001143 return result;
scroggo@google.com0b735632013-03-19 17:38:50 +00001144}
scroggo@google.com7d519302013-03-19 17:28:10 +00001145
epoger@google.com6f6568b2013-03-22 17:29:46 +00001146// Macro magic to convert a numeric preprocessor token into a string.
1147// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1148// This should probably be moved into one of our common headers...
1149#define TOSTRING_INTERNAL(x) #x
1150#define TOSTRING(x) TOSTRING_INTERNAL(x)
1151
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001152// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
scroggo@google.com0f567c62013-03-20 15:35:08 +00001153DEFINE_string(config, "", configUsage().c_str());
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001154DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
1155DEFINE_bool(enableMissingWarning, true, "Print message to stderr (but don't fail) if "
1156 "unable to read a reference image for any tests.");
1157DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1158DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1159#if SK_SUPPORT_GPU
1160DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
epoger@google.com6f6568b2013-03-22 17:29:46 +00001161 "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1162 "use the default. 0 for either disables the cache.");
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001163#endif
1164DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1165 "when reading/writing files.");
1166DEFINE_string(match, "", "Only run tests whose name includes this substring/these substrings "
1167 "(more than one can be supplied, separated by spaces).");
1168DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1169 "pixel mismatches into this directory.");
1170DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1171 "testIndex %% divisor == remainder.");
1172DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1173DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1174DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1175 "any differences between those and the newly generated ones.");
1176DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1177DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1178DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1179DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
1180DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1181DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1182DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1183 "factors to be used for tileGrid playback testing. Default value: 1.0");
1184DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
1185DEFINE_bool2(verbose, v, false, "Print diagnostics (e.g. list each config to be tested).");
1186DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
1187DEFINE_string2(writePicturePath, wp, "", "Write .skp files into this directory.");
1188
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001189static int findConfig(const char config[]) {
1190 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1191 if (!strcmp(config, gRec[i].fName)) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001192 return (int) i;
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001193 }
1194 }
1195 return -1;
1196}
1197
reed@google.comb2a51622011-10-31 16:30:04 +00001198static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
1199 if (0 == array.count()) {
1200 // no names, so don't skip anything
1201 return false;
1202 }
1203 for (int i = 0; i < array.count(); ++i) {
1204 if (strstr(name, array[i])) {
1205 // found the name, so don't skip
1206 return false;
1207 }
1208 }
1209 return true;
1210}
1211
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001212namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001213#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +00001214SkAutoTUnref<GrContext> gGrContext;
1215/**
bsalomon@google.comc7a24d22012-11-01 18:03:48 +00001216 * Sets the global GrContext, accessible by individual GMs
bsalomon@google.com7361f542012-04-19 19:15:35 +00001217 */
caryclark@google.com13130862012-06-06 12:10:45 +00001218static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +00001219 SkSafeRef(grContext);
1220 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001221}
bsalomon@google.com7361f542012-04-19 19:15:35 +00001222
1223/**
1224 * Gets the global GrContext, can be called by GM tests.
1225 */
caryclark@google.com13130862012-06-06 12:10:45 +00001226GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +00001227GrContext* GetGr() {
1228 return gGrContext.get();
1229}
1230
1231/**
1232 * Sets the global GrContext and then resets it to its previous value at
1233 * destruction.
1234 */
1235class AutoResetGr : SkNoncopyable {
1236public:
1237 AutoResetGr() : fOld(NULL) {}
1238 void set(GrContext* context) {
1239 SkASSERT(NULL == fOld);
1240 fOld = GetGr();
1241 SkSafeRef(fOld);
1242 SetGr(context);
1243 }
1244 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1245private:
1246 GrContext* fOld;
1247};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001248#else
epoger@google.com80724df2013-03-21 13:49:54 +00001249GrContext* GetGr();
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001250GrContext* GetGr() { return NULL; }
1251#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +00001252}
1253
reed@google.comfb2cd422013-01-04 14:43:03 +00001254template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1255 int index = array->find(value);
1256 if (index < 0) {
1257 *array->append() = value;
1258 }
1259}
1260
epoger@google.com80724df2013-03-21 13:49:54 +00001261/**
1262 * Run this test in a number of different configs (8888, 565, PDF,
1263 * etc.), confirming that the resulting bitmaps match expectations
1264 * (which may be different for each config).
1265 *
1266 * Returns all errors encountered while doing so.
1267 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001268ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1269 GrContextFactory *grFactory);
1270ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1271 GrContextFactory *grFactory) {
1272 ErrorCombination errorsForAllConfigs;
epoger@google.com80724df2013-03-21 13:49:54 +00001273 uint32_t gmFlags = gm->getFlags();
1274
epoger@google.com80724df2013-03-21 13:49:54 +00001275 for (int i = 0; i < configs.count(); i++) {
1276 ConfigData config = gRec[configs[i]];
1277
1278 // Skip any tests that we don't even need to try.
1279 if ((kPDF_Backend == config.fBackend) &&
1280 (!FLAGS_pdf|| (gmFlags & GM::kSkipPDF_Flag))) {
1281 continue;
1282 }
1283 if ((gmFlags & GM::kSkip565_Flag) &&
1284 (kRaster_Backend == config.fBackend) &&
1285 (SkBitmap::kRGB_565_Config == config.fConfig)) {
1286 continue;
1287 }
1288 if ((gmFlags & GM::kSkipGPU_Flag) &&
1289 kGPU_Backend == config.fBackend) {
1290 continue;
1291 }
1292
1293 // Now we know that we want to run this test and record its
1294 // success or failure.
epoger@google.com6f6568b2013-03-22 17:29:46 +00001295 ErrorCombination errorsForThisConfig;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001296 GrSurface* gpuTarget = NULL;
epoger@google.com80724df2013-03-21 13:49:54 +00001297#if SK_SUPPORT_GPU
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001298 SkAutoTUnref<GrSurface> auGpuTarget;
epoger@google.com80724df2013-03-21 13:49:54 +00001299 AutoResetGr autogr;
epoger@google.com6f6568b2013-03-22 17:29:46 +00001300 if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
epoger@google.com80724df2013-03-21 13:49:54 +00001301 GrContext* gr = grFactory->get(config.fGLContextType);
1302 bool grSuccess = false;
1303 if (gr) {
1304 // create a render target to back the device
1305 GrTextureDesc desc;
1306 desc.fConfig = kSkia8888_GrPixelConfig;
1307 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1308 desc.fWidth = gm->getISize().width();
1309 desc.fHeight = gm->getISize().height();
1310 desc.fSampleCnt = config.fSampleCnt;
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001311 auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1312 if (NULL != auGpuTarget) {
1313 gpuTarget = auGpuTarget;
1314 grSuccess = true;
epoger@google.com80724df2013-03-21 13:49:54 +00001315 autogr.set(gr);
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001316 // Set the user specified cache limits if non-default.
1317 size_t bytes;
1318 int count;
1319 gr->getTextureCacheLimits(&count, &bytes);
1320 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1321 bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1322 }
1323 if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1324 count = gGpuCacheSizeCount;
1325 }
1326 gr->setTextureCacheLimits(count, bytes);
epoger@google.com80724df2013-03-21 13:49:54 +00001327 }
epoger@google.com80724df2013-03-21 13:49:54 +00001328 }
1329 if (!grSuccess) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001330 errorsForThisConfig.add(kNoGpuContext_ErrorType);
epoger@google.com80724df2013-03-21 13:49:54 +00001331 }
1332 }
1333#endif
1334
1335 SkBitmap comparisonBitmap;
1336
1337 const char* writePath;
1338 if (FLAGS_writePath.count() == 1) {
1339 writePath = FLAGS_writePath[0];
1340 } else {
1341 writePath = NULL;
1342 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001343 if (errorsForThisConfig.isEmpty()) {
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001344 errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
1345 &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001346 }
1347
epoger@google.com6f6568b2013-03-22 17:29:46 +00001348 if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1349 (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1350 errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
bsalomon@google.com123ac1d2013-03-28 19:18:12 +00001351 gpuTarget));
epoger@google.com80724df2013-03-21 13:49:54 +00001352 }
1353
epoger@google.com6f6568b2013-03-22 17:29:46 +00001354 errorsForAllConfigs.add(errorsForThisConfig);
epoger@google.com80724df2013-03-21 13:49:54 +00001355 }
1356 return errorsForAllConfigs;
1357}
1358
1359/**
1360 * Run this test in a number of different drawing modes (pipe,
1361 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1362 * *exactly* match comparisonBitmap.
1363 *
1364 * Returns all errors encountered while doing so.
1365 */
epoger@google.com6f6568b2013-03-22 17:29:46 +00001366ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1367 const SkBitmap &comparisonBitmap,
1368 const SkTDArray<SkScalar> &tileGridReplayScales);
1369ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1370 const SkBitmap &comparisonBitmap,
1371 const SkTDArray<SkScalar> &tileGridReplayScales) {
1372 ErrorCombination errorsForAllModes;
epoger@google.com80724df2013-03-21 13:49:54 +00001373 uint32_t gmFlags = gm->getFlags();
1374
epoger@google.comf60494b2013-04-03 17:02:53 +00001375 // TODO(epoger): We should start recording any per-GM skipped
1376 // modes (i.e. those we skipped due to gmFlags) with a new
1377 // ErrorType, perhaps named kIntentionallySkipped_ErrorType.
epoger@google.com80724df2013-03-21 13:49:54 +00001378 if (!(gmFlags & GM::kSkipPicture_Flag)) {
1379
epoger@google.com6f6568b2013-03-22 17:29:46 +00001380 ErrorCombination pictErrors;
epoger@google.com80724df2013-03-21 13:49:54 +00001381
1382 //SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
1383 SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1384 SkAutoUnref aur(pict);
1385
1386 if (FLAGS_replay) {
1387 SkBitmap bitmap;
1388 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001389 pictErrors.add(gmmain.compare_test_results_to_reference_bitmap(
1390 gm, compareConfig, "-replay", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001391 }
1392
epoger@google.com6f6568b2013-03-22 17:29:46 +00001393 if ((pictErrors.isEmpty()) && FLAGS_serialize) {
epoger@google.com80724df2013-03-21 13:49:54 +00001394 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1395 SkAutoUnref aurr(repict);
1396
1397 SkBitmap bitmap;
1398 gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001399 pictErrors.add(gmmain.compare_test_results_to_reference_bitmap(
1400 gm, compareConfig, "-serialize", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001401 }
1402
1403 if (FLAGS_writePicturePath.count() == 1) {
1404 const char* pictureSuffix = "skp";
1405 SkString path = make_filename(FLAGS_writePicturePath[0], "",
1406 gm->shortName(), pictureSuffix);
1407 SkFILEWStream stream(path.c_str());
1408 pict->serialize(&stream);
1409 }
1410
epoger@google.com6f6568b2013-03-22 17:29:46 +00001411 errorsForAllModes.add(pictErrors);
epoger@google.com80724df2013-03-21 13:49:54 +00001412 }
1413
1414 // TODO: add a test in which the RTree rendering results in a
1415 // different bitmap than the standard rendering. It should
1416 // show up as failed in the JSON summary, and should be listed
1417 // in the stdout also.
1418 if (!(gmFlags & GM::kSkipPicture_Flag) && FLAGS_rtree) {
1419 SkPicture* pict = gmmain.generate_new_picture(
1420 gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
1421 SkAutoUnref aur(pict);
1422 SkBitmap bitmap;
1423 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
epoger@google.com6f6568b2013-03-22 17:29:46 +00001424 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1425 gm, compareConfig, "-rtree", bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001426 }
1427
1428 if (!(gmFlags & GM::kSkipPicture_Flag) && FLAGS_tileGrid) {
1429 for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1430 SkScalar replayScale = tileGridReplayScales[scaleIndex];
1431 if ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1) {
1432 continue;
1433 }
1434 // We record with the reciprocal scale to obtain a replay
1435 // result that can be validated against comparisonBitmap.
1436 SkScalar recordScale = SkScalarInvert(replayScale);
1437 SkPicture* pict = gmmain.generate_new_picture(
1438 gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag, recordScale);
1439 SkAutoUnref aur(pict);
1440 SkBitmap bitmap;
robertphillips@google.com5a7d0292013-04-02 15:18:41 +00001441 // We cannot yet pass 'true' to generate_image_from_picture to
1442 // perform actual tiled rendering (see Issue 1198 -
1443 // https://code.google.com/p/skia/issues/detail?id=1198)
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +00001444 gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
robertphillips@google.com5a7d0292013-04-02 15:18:41 +00001445 replayScale /*, true */);
epoger@google.com80724df2013-03-21 13:49:54 +00001446 SkString suffix("-tilegrid");
1447 if (SK_Scalar1 != replayScale) {
1448 suffix += "-scale-";
1449 suffix.appendScalar(replayScale);
1450 }
epoger@google.com6f6568b2013-03-22 17:29:46 +00001451 errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1452 gm, compareConfig, suffix.c_str(), bitmap, &comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001453 }
1454 }
1455
1456 // run the pipe centric GM steps
1457 if (!(gmFlags & GM::kSkipPipe_Flag)) {
1458
epoger@google.com6f6568b2013-03-22 17:29:46 +00001459 ErrorCombination pipeErrors;
epoger@google.com80724df2013-03-21 13:49:54 +00001460
1461 if (FLAGS_pipe) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001462 pipeErrors.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001463 }
1464
epoger@google.com6f6568b2013-03-22 17:29:46 +00001465 if ((pipeErrors.isEmpty()) &&
epoger@google.com80724df2013-03-21 13:49:54 +00001466 FLAGS_tiledPipe && !(gmFlags & GM::kSkipTiled_Flag)) {
epoger@google.com6f6568b2013-03-22 17:29:46 +00001467 pipeErrors.add(gmmain.test_tiled_pipe_playback(gm, compareConfig, comparisonBitmap));
epoger@google.com80724df2013-03-21 13:49:54 +00001468 }
1469
epoger@google.com6f6568b2013-03-22 17:29:46 +00001470 errorsForAllModes.add(pipeErrors);
epoger@google.com80724df2013-03-21 13:49:54 +00001471 }
1472 return errorsForAllModes;
1473}
1474
epoger@google.comf60494b2013-04-03 17:02:53 +00001475/**
1476 * Return a list of all entries in an array of strings as a single string
1477 * of this form:
1478 * "item1", "item2", "item3"
1479 */
1480SkString list_all(const SkTArray<SkString> &stringArray);
1481SkString list_all(const SkTArray<SkString> &stringArray) {
1482 SkString total;
1483 for (int i = 0; i < stringArray.count(); i++) {
1484 if (i > 0) {
1485 total.append(", ");
1486 }
1487 total.append("\"");
1488 total.append(stringArray[i]);
1489 total.append("\"");
1490 }
1491 return total;
1492}
1493
1494/**
1495 * Return a list of configuration names, as a single string of this form:
1496 * "item1", "item2", "item3"
1497 *
1498 * @param configs configurations, as a list of indices into gRec
1499 */
1500SkString list_all_config_names(const SkTDArray<size_t> &configs);
1501SkString list_all_config_names(const SkTDArray<size_t> &configs) {
1502 SkString total;
1503 for (int i = 0; i < configs.count(); i++) {
1504 if (i > 0) {
1505 total.append(", ");
1506 }
1507 total.append("\"");
1508 total.append(gRec[configs[i]].fName);
1509 total.append("\"");
1510 }
1511 return total;
1512}
1513
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001514int tool_main(int argc, char** argv);
1515int tool_main(int argc, char** argv) {
1516
1517#if SK_ENABLE_INST_COUNT
1518 gPrintInstCount = true;
1519#endif
1520
1521 SkGraphics::Init();
1522 // we don't need to see this during a run
1523 gSkSuppressFontCachePurgeSpew = true;
1524
1525 setSystemPreferences();
1526 GMMain gmmain;
1527
1528 SkTDArray<size_t> configs;
1529 SkTDArray<size_t> excludeConfigs;
1530 bool userConfig = false;
1531
1532 SkString usage;
1533 usage.printf("Run the golden master tests.\n");
scroggo@google.comd9ba9a02013-03-21 19:43:15 +00001534 SkCommandLineFlags::SetUsage(usage.c_str());
1535 SkCommandLineFlags::Parse(argc, argv);
epoger@google.com0b62b3d2013-03-20 17:59:28 +00001536
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001537 gmmain.fUseFileHierarchy = FLAGS_hierarchy;
1538 if (FLAGS_mismatchPath.count() == 1) {
1539 gmmain.fMismatchPath = FLAGS_mismatchPath[0];
1540 }
1541
1542 for (int i = 0; i < FLAGS_config.count(); i++) {
1543 int index = findConfig(FLAGS_config[i]);
1544 if (index >= 0) {
1545 appendUnique<size_t>(&configs, index);
1546 userConfig = true;
scroggo@google.com0b735632013-03-19 17:38:50 +00001547 } else {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001548 gm_fprintf(stderr, "unrecognized config %s\n", FLAGS_config[i]);
scroggo@google.com7d519302013-03-19 17:28:10 +00001549 return -1;
1550 }
1551 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001552
1553 for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1554 int index = findConfig(FLAGS_excludeConfig[i]);
1555 if (index >= 0) {
1556 *excludeConfigs.append() = index;
1557 } else {
1558 gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1559 return -1;
1560 }
1561 }
1562
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001563 int moduloRemainder = -1;
1564 int moduloDivisor = -1;
1565
1566 if (FLAGS_modulo.count() == 2) {
1567 moduloRemainder = atoi(FLAGS_modulo[0]);
1568 moduloDivisor = atoi(FLAGS_modulo[1]);
1569 if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
1570 gm_fprintf(stderr, "invalid modulo values.");
1571 return -1;
1572 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001573 }
reed@google.com873cb1e2010-12-23 15:00:45 +00001574
epoger@google.com6f6568b2013-03-22 17:29:46 +00001575#if SK_SUPPORT_GPU
1576 if (FLAGS_gpuCacheSize.count() > 0) {
1577 if (FLAGS_gpuCacheSize.count() != 2) {
1578 gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
1579 return -1;
1580 }
1581 gGpuCacheSizeBytes = atoi(FLAGS_gpuCacheSize[0]);
1582 gGpuCacheSizeCount = atoi(FLAGS_gpuCacheSize[1]);
1583 } else {
1584 gGpuCacheSizeBytes = DEFAULT_CACHE_VALUE;
1585 gGpuCacheSizeCount = DEFAULT_CACHE_VALUE;
1586 }
1587#endif
1588
1589 SkTDArray<SkScalar> tileGridReplayScales;
1590 *tileGridReplayScales.append() = SK_Scalar1; // By default only test at scale 1.0
1591 if (FLAGS_tileGridReplayScales.count() > 0) {
1592 tileGridReplayScales.reset();
1593 for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
1594 double val = atof(FLAGS_tileGridReplayScales[i]);
1595 if (0 < val) {
1596 *tileGridReplayScales.append() = SkDoubleToScalar(val);
1597 }
1598 }
1599 if (0 == tileGridReplayScales.count()) {
1600 // Should have at least one scale
1601 gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
1602 return -1;
1603 }
1604 }
1605
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001606 if (!userConfig) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001607 // if no config is specified by user, add the defaults
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001608 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001609 if (gRec[i].fRunByDefault) {
1610 *configs.append() = i;
1611 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001612 }
1613 }
reed@google.comfb2cd422013-01-04 14:43:03 +00001614 // now remove any explicitly excluded configs
1615 for (int i = 0; i < excludeConfigs.count(); ++i) {
1616 int index = configs.find(excludeConfigs[i]);
1617 if (index >= 0) {
1618 configs.remove(index);
1619 // now assert that there was only one copy in configs[]
1620 SkASSERT(configs.find(excludeConfigs[i]) < 0);
1621 }
1622 }
1623
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001624#if SK_SUPPORT_GPU
1625 GrContextFactory* grFactory = new GrContextFactory;
1626 for (int i = 0; i < configs.count(); ++i) {
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001627 size_t index = configs[i];
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001628 if (kGPU_Backend == gRec[index].fBackend) {
1629 GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1630 if (NULL == ctx) {
1631 SkDebugf("GrContext could not be created for config %s. Config will be skipped.",
1632 gRec[index].fName);
1633 configs.remove(i);
1634 --i;
1635 }
1636 if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
1637 SkDebugf("Sample count (%d) of config %s is not supported. Config will be skipped.",
1638 gRec[index].fSampleCnt, gRec[index].fName);
1639 configs.remove(i);
1640 --i;
1641 }
1642 }
1643 }
epoger@google.com80724df2013-03-21 13:49:54 +00001644#else
1645 GrContextFactory* grFactory = NULL;
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001646#endif
1647
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001648 if (FLAGS_verbose) {
reed@google.comfb2cd422013-01-04 14:43:03 +00001649 SkString str;
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001650 str.printf("%d configs:", configs.count());
reed@google.comfb2cd422013-01-04 14:43:03 +00001651 for (int i = 0; i < configs.count(); ++i) {
1652 str.appendf(" %s", gRec[configs[i]].fName);
1653 }
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001654 gm_fprintf(stderr, "%s\n", str.c_str());
reed@google.comfb2cd422013-01-04 14:43:03 +00001655 }
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001656
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001657 if (FLAGS_resourcePath.count() == 1) {
1658 GM::SetResourcePath(FLAGS_resourcePath[0]);
1659 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001660
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001661 if (FLAGS_readPath.count() == 1) {
1662 const char* readPath = FLAGS_readPath[0];
epoger@google.com37269602013-01-19 04:21:27 +00001663 if (!sk_exists(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001664 gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001665 return -1;
1666 }
1667 if (sk_isdir(readPath)) {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001668 gm_fprintf(stdout, "reading from %s\n", readPath);
epoger@google.com37269602013-01-19 04:21:27 +00001669 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1670 IndividualImageExpectationsSource,
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001671 (readPath, FLAGS_enableMissingWarning)));
epoger@google.com37269602013-01-19 04:21:27 +00001672 } else {
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001673 gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
epoger@google.comd271d242013-02-13 18:14:48 +00001674 gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1675 JsonExpectationsSource, (readPath)));
epoger@google.com37269602013-01-19 04:21:27 +00001676 }
chudy@google.comf32f6e82012-07-12 15:42:37 +00001677 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001678 if (FLAGS_writePath.count() == 1) {
1679 gm_fprintf(stderr, "writing to %s\n", FLAGS_writePath[0]);
reed@android.com00f883e2010-12-14 17:46:14 +00001680 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001681 if (FLAGS_writePicturePath.count() == 1) {
1682 gm_fprintf(stderr, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +00001683 }
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001684 if (FLAGS_resourcePath.count() == 1) {
1685 gm_fprintf(stderr, "reading resources from %s\n", FLAGS_resourcePath[0]);
robertphillips@google.com8570b5c2012-03-20 17:40:58 +00001686 }
1687
epoger@google.com82cb65b2012-10-29 18:59:17 +00001688 if (moduloDivisor <= 0) {
1689 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001690 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001691 if (moduloRemainder < 0 || moduloRemainder >= moduloDivisor) {
1692 moduloRemainder = -1;
reed@google.comae7b8f32012-10-18 21:30:57 +00001693 }
1694
epoger@google.comf60494b2013-04-03 17:02:53 +00001695 int gmsRun = 0;
reed@google.comae7b8f32012-10-18 21:30:57 +00001696 int gmIndex = -1;
1697 SkString moduloStr;
1698
epoger@google.come8ebeb12012-10-29 16:42:11 +00001699 // If we will be writing out files, prepare subdirectories.
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001700 if (FLAGS_writePath.count() == 1) {
1701 if (!sk_mkdir(FLAGS_writePath[0])) {
epoger@google.come8ebeb12012-10-29 16:42:11 +00001702 return -1;
1703 }
1704 if (gmmain.fUseFileHierarchy) {
1705 for (int i = 0; i < configs.count(); i++) {
1706 ConfigData config = gRec[configs[i]];
1707 SkString subdir;
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001708 subdir.appendf("%s%c%s", FLAGS_writePath[0], SkPATH_SEPARATOR,
epoger@google.come8ebeb12012-10-29 16:42:11 +00001709 config.fName);
1710 if (!sk_mkdir(subdir.c_str())) {
1711 return -1;
1712 }
1713 }
1714 }
1715 }
1716
bsalomon@google.com7361f542012-04-19 19:15:35 +00001717 Iter iter;
1718 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00001719 while ((gm = iter.next()) != NULL) {
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +00001720
reed@google.comae7b8f32012-10-18 21:30:57 +00001721 ++gmIndex;
epoger@google.com82cb65b2012-10-29 18:59:17 +00001722 if (moduloRemainder >= 0) {
1723 if ((gmIndex % moduloDivisor) != moduloRemainder) {
reed@google.comae7b8f32012-10-18 21:30:57 +00001724 continue;
1725 }
epoger@google.com82cb65b2012-10-29 18:59:17 +00001726 moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
reed@google.comae7b8f32012-10-18 21:30:57 +00001727 }
1728
reed@google.comece2b022011-07-25 14:28:57 +00001729 const char* shortName = gm->shortName();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001730 if (skip_name(FLAGS_match, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00001731 SkDELETE(gm);
1732 continue;
1733 }
1734
epoger@google.comf60494b2013-04-03 17:02:53 +00001735 gmsRun++;
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001736 SkISize size = gm->getISize();
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001737 gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
1738 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00001739
epoger@google.comf60494b2013-04-03 17:02:53 +00001740 run_multiple_configs(gmmain, gm, configs, grFactory);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001741
1742 SkBitmap comparisonBitmap;
1743 const ConfigData compareConfig =
bsalomon@google.com4c75f242013-03-19 18:58:43 +00001744 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
epoger@google.comf60494b2013-04-03 17:02:53 +00001745 gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001746
epoger@google.com80724df2013-03-21 13:49:54 +00001747 // TODO(epoger): only run this if gmmain.generate_image() succeeded?
1748 // Otherwise, what are we comparing against?
epoger@google.comf60494b2013-04-03 17:02:53 +00001749 run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap, tileGridReplayScales);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001750
reed@android.com00dae862009-06-10 15:38:48 +00001751 SkDELETE(gm);
1752 }
epoger@google.comf60494b2013-04-03 17:02:53 +00001753
1754 SkTArray<SkString> modes;
1755 gmmain.GetRenderModesEncountered(modes);
1756
1757 // Output summary to stdout.
1758 gm_fprintf(stdout, "Ran %d GMs\n", gmsRun);
1759 gm_fprintf(stdout, "... over %2d configs [%s]\n", configs.count(),
1760 list_all_config_names(configs).c_str());
1761 gm_fprintf(stdout, "... and %2d modes [%s]\n", modes.count(), list_all(modes).c_str());
1762 gm_fprintf(stdout, "... so there should be a total of %d tests.\n",
1763 gmsRun * (configs.count() + modes.count()));
1764
1765 // TODO(epoger): Ultimately, we should signal an error if the
1766 // expected total number of tests (displayed above) does not match
1767 // gmmain.fTestsRun. But for now, there are cases where those
1768 // numbers won't match: specifically, if some configs/modes are
1769 // skipped on a per-GM basis (due to gm->getFlags() for a specific
1770 // GM). Later on, we should record tests like that using some new
1771 // ErrorType, like kIntentionallySkipped_ErrorType. Then we could
1772 // signal an error if the totals didn't match up.
epoger@google.com57f7abc2012-11-13 03:41:55 +00001773 gmmain.ListErrors();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001774
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001775 if (FLAGS_writeJsonSummaryPath.count() == 1) {
epoger@google.comee8a8e32012-12-18 19:13:49 +00001776 Json::Value actualResults;
1777 actualResults[kJsonKey_ActualResults_Failed] =
1778 gmmain.fJsonActualResults_Failed;
1779 actualResults[kJsonKey_ActualResults_FailureIgnored] =
1780 gmmain.fJsonActualResults_FailureIgnored;
epoger@google.com9c56a8d2012-12-20 18:34:29 +00001781 actualResults[kJsonKey_ActualResults_NoComparison] =
1782 gmmain.fJsonActualResults_NoComparison;
epoger@google.comee8a8e32012-12-18 19:13:49 +00001783 actualResults[kJsonKey_ActualResults_Succeeded] =
1784 gmmain.fJsonActualResults_Succeeded;
1785 Json::Value root;
1786 root[kJsonKey_ActualResults] = actualResults;
1787 root[kJsonKey_ExpectedResults] = gmmain.fJsonExpectedResults;
1788 std::string jsonStdString = root.toStyledString();
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001789 SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
epoger@google.comee8a8e32012-12-18 19:13:49 +00001790 stream.write(jsonStdString.c_str(), jsonStdString.length());
1791 }
1792
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001793#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001794
robertphillips@google.com59552022012-08-31 13:07:37 +00001795#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001796 for (int i = 0; i < configs.count(); i++) {
1797 ConfigData config = gRec[configs[i]];
1798
1799 if (kGPU_Backend == config.fBackend) {
1800 GrContext* gr = grFactory->get(config.fGLContextType);
1801
epoger@google.com5efdd0c2013-03-13 14:18:40 +00001802 gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001803 gr->printCacheStats();
1804 }
1805 }
1806#endif
1807
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001808 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001809#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001810 SkGraphics::Term();
1811
epoger@google.comf60494b2013-04-03 17:02:53 +00001812 return (0 == gmmain.NumSignificantErrors()) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001813}
caryclark@google.com5987f582012-10-02 18:33:14 +00001814
scroggo@google.com09fd4d22013-03-20 14:20:18 +00001815void GMMain::installFilter(SkCanvas* canvas) {
1816 if (FLAGS_forceBWtext) {
1817 canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
1818 }
1819}
1820
borenet@google.com7158e6a2012-11-01 17:43:44 +00001821#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +00001822int main(int argc, char * const argv[]) {
1823 return tool_main(argc, (char**) argv);
1824}
1825#endif