blob: bf0321460a6ebf7fadc09ca47de0e74372834026 [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
bungeman@google.comb29c8832011-10-10 13:19:10 +00008#include "gm.h"
epoger@google.com7bc13a62012-02-14 14:53:59 +00009#include "system_preferences.h"
reed@android.comb9b9a182009-07-08 02:54:47 +000010#include "SkColorPriv.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000011#include "SkData.h"
junov@google.com4370aed2012-01-18 16:21:08 +000012#include "SkDeferredCanvas.h"
bsalomon@google.com971d0c82011-08-19 17:22:05 +000013#include "SkDevice.h"
epoger@google.comde961632012-10-26 18:56:36 +000014#include "SkDrawFilter.h"
scroggo@google.com5af9b202012-06-04 17:17:36 +000015#include "SkGPipe.h"
reed@android.com8015dd82009-06-21 00:49:18 +000016#include "SkGraphics.h"
17#include "SkImageDecoder.h"
18#include "SkImageEncoder.h"
tomhudson@google.com9875dd12011-04-25 15:49:53 +000019#include "SkPicture.h"
robertphillips@google.com977b9c82012-06-05 19:35:09 +000020#include "SkRefCnt.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000021#include "SkStream.h"
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000022#include "SkTArray.h"
scroggo@google.com72c96722012-06-06 21:07:10 +000023#include "SamplePipeControllers.h"
reed@google.com07700442010-12-20 19:46:07 +000024
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000025#if SK_SUPPORT_GPU
26#include "GrContextFactory.h"
27#include "GrRenderTarget.h"
28#include "SkGpuDevice.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000029typedef GrContextFactory::GLContextType GLContextType;
30#else
31class GrContext;
32class GrRenderTarget;
33typedef int GLContextType;
34#endif
35
mike@reedtribe.org10afbef2011-12-30 16:02:53 +000036static bool gForceBWtext;
37
reed@google.com8923c6c2011-11-08 14:59:38 +000038extern bool gSkSuppressFontCachePurgeSpew;
39
reed@google.com07700442010-12-20 19:46:07 +000040#ifdef SK_SUPPORT_PDF
tomhudson@google.com9875dd12011-04-25 15:49:53 +000041 #include "SkPDFDevice.h"
42 #include "SkPDFDocument.h"
reed@google.com07700442010-12-20 19:46:07 +000043#endif
reed@android.com00dae862009-06-10 15:38:48 +000044
epoger@google.come3cc2eb2012-01-18 20:11:13 +000045// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
46// stop writing out XPS-format image baselines in gm.
47#undef SK_SUPPORT_XPS
bungeman@google.comb29c8832011-10-10 13:19:10 +000048#ifdef SK_SUPPORT_XPS
49 #include "SkXPSDevice.h"
50#endif
51
reed@google.com46cce912011-06-29 12:54:46 +000052#ifdef SK_BUILD_FOR_MAC
53 #include "SkCGUtils.h"
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000054 #define CAN_IMAGE_PDF 1
reed@google.com46cce912011-06-29 12:54:46 +000055#else
bsalomon@google.com0a09eef2011-06-29 19:42:58 +000056 #define CAN_IMAGE_PDF 0
reed@google.com46cce912011-06-29 12:54:46 +000057#endif
58
epoger@google.comc7cf2b32011-12-28 19:31:01 +000059typedef int ErrorBitfield;
60const static ErrorBitfield ERROR_NONE = 0x00;
61const static ErrorBitfield ERROR_NO_GPU_CONTEXT = 0x01;
62const static ErrorBitfield ERROR_PIXEL_MISMATCH = 0x02;
63const static ErrorBitfield ERROR_DIMENSION_MISMATCH = 0x04;
64const static ErrorBitfield ERROR_READING_REFERENCE_IMAGE = 0x08;
65const static ErrorBitfield ERROR_WRITING_REFERENCE_IMAGE = 0x10;
66
epoger@google.comf28dd8a2012-10-25 16:27:34 +000067// TODO: This should be defined as "\\" on Windows, but this is the way this
68// file has been working for a long time. We can fix it later.
69const static char* PATH_SEPARATOR = "/";
70
reed@google.come8fcb502012-05-17 15:28:20 +000071// If true, emit a messange when we can't find a reference image to compare
72static bool gNotifyMissingReadReference;
73
reed@android.com00dae862009-06-10 15:38:48 +000074using namespace skiagm;
75
reed@android.com00dae862009-06-10 15:38:48 +000076class Iter {
77public:
78 Iter() {
bsalomon@google.com39149582011-06-13 21:55:32 +000079 this->reset();
80 }
81
82 void reset() {
reed@android.comdd0ac282009-06-20 02:38:16 +000083 fReg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000084 }
reed@google.comd4dfd102011-01-18 21:05:42 +000085
reed@android.comdd0ac282009-06-20 02:38:16 +000086 GM* next() {
reed@android.com00dae862009-06-10 15:38:48 +000087 if (fReg) {
reed@android.comdd0ac282009-06-20 02:38:16 +000088 GMRegistry::Factory fact = fReg->factory();
reed@android.com00dae862009-06-10 15:38:48 +000089 fReg = fReg->next();
reed@android.comdd0ac282009-06-20 02:38:16 +000090 return fact(0);
reed@android.com00dae862009-06-10 15:38:48 +000091 }
92 return NULL;
93 }
reed@google.comd4dfd102011-01-18 21:05:42 +000094
reed@android.com00dae862009-06-10 15:38:48 +000095 static int Count() {
reed@android.comdd0ac282009-06-20 02:38:16 +000096 const GMRegistry* reg = GMRegistry::Head();
reed@android.com00dae862009-06-10 15:38:48 +000097 int count = 0;
98 while (reg) {
99 count += 1;
100 reg = reg->next();
101 }
102 return count;
103 }
reed@google.comd4dfd102011-01-18 21:05:42 +0000104
reed@android.com00dae862009-06-10 15:38:48 +0000105private:
106 const GMRegistry* fReg;
107};
108
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000109enum Backend {
110 kRaster_Backend,
111 kGPU_Backend,
112 kPDF_Backend,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000113 kXPS_Backend,
vandebo@chromium.org686abdf2011-02-03 23:00:40 +0000114};
115
bsalomon@google.com7361f542012-04-19 19:15:35 +0000116enum ConfigFlags {
117 kNone_ConfigFlag = 0x0,
118 /* Write GM images if a write path is provided. */
119 kWrite_ConfigFlag = 0x1,
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000120 /* Read reference GM images if a read path is provided. */
bsalomon@google.com7361f542012-04-19 19:15:35 +0000121 kRead_ConfigFlag = 0x2,
122 kRW_ConfigFlag = (kWrite_ConfigFlag | kRead_ConfigFlag),
123};
124
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000125struct ConfigData {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000126 SkBitmap::Config fConfig;
127 Backend fBackend;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000128 GLContextType fGLContextType; // GPU backend only
bsalomon@google.com7361f542012-04-19 19:15:35 +0000129 int fSampleCnt; // GPU backend only
130 ConfigFlags fFlags;
131 const char* fName;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000132};
133
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000134class BWTextDrawFilter : public SkDrawFilter {
135public:
136 virtual void filter(SkPaint*, Type) SK_OVERRIDE;
137};
138void BWTextDrawFilter::filter(SkPaint* p, Type t) {
139 if (kText_Type == t) {
140 p->setAntiAlias(false);
141 }
142}
143
scroggo@google.com565254b2012-06-28 15:41:32 +0000144struct PipeFlagComboData {
145 const char* name;
146 uint32_t flags;
147};
148
149static PipeFlagComboData gPipeWritingFlagCombos[] = {
150 { "", 0 },
151 { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
scroggob3c0f482012-07-02 19:07:57 +0000152 { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
scroggo@google.com15011ee2012-07-26 20:03:32 +0000153 | SkGPipeWriter::kSharedAddressSpace_Flag }
scroggo@google.com565254b2012-06-28 15:41:32 +0000154};
155
scroggo@google.com5af9b202012-06-04 17:17:36 +0000156
epoger@google.comde961632012-10-26 18:56:36 +0000157class GMMain {
158public:
159 static SkString make_name(const char shortName[], const char configName[]) {
160 SkString name(shortName);
161 name.appendf("_%s", configName);
162 return name;
163 }
164
165 static SkString make_filename(const char path[],
166 const char pathSuffix[],
167 const SkString& name,
168 const char suffix[]) {
169 SkString filename(path);
170 if (filename.endsWith(PATH_SEPARATOR)) {
171 filename.remove(filename.size() - 1, 1);
172 }
173 filename.append(pathSuffix);
174 filename.append(PATH_SEPARATOR);
175 filename.appendf("%s.%s", name.c_str(), suffix);
176 return filename;
177 }
178
179 /* since PNG insists on unpremultiplying our alpha, we take no
180 precision chances and force all pixels to be 100% opaque,
181 otherwise on compare we may not get a perfect match.
182 */
183 static void force_all_opaque(const SkBitmap& bitmap) {
184 SkAutoLockPixels lock(bitmap);
185 for (int y = 0; y < bitmap.height(); y++) {
186 for (int x = 0; x < bitmap.width(); x++) {
187 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
188 }
scroggo@google.com565254b2012-06-28 15:41:32 +0000189 }
190 }
epoger@google.comde961632012-10-26 18:56:36 +0000191
192 static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
193 SkBitmap copy;
194 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
195 force_all_opaque(copy);
196 return SkImageEncoder::EncodeFile(path.c_str(), copy,
197 SkImageEncoder::kPNG_Type, 100);
198 }
199
200 static inline SkPMColor compute_diff_pmcolor(SkPMColor c0, SkPMColor c1) {
201 int dr = SkGetPackedR32(c0) - SkGetPackedR32(c1);
202 int dg = SkGetPackedG32(c0) - SkGetPackedG32(c1);
203 int db = SkGetPackedB32(c0) - SkGetPackedB32(c1);
204 return SkPackARGB32(0xFF, SkAbs32(dr), SkAbs32(dg), SkAbs32(db));
205 }
206
207 static void compute_diff(const SkBitmap& target, const SkBitmap& base,
208 SkBitmap* diff) {
209 SkAutoLockPixels alp(*diff);
210
211 const int w = target.width();
212 const int h = target.height();
213 for (int y = 0; y < h; y++) {
214 for (int x = 0; x < w; x++) {
215 SkPMColor c0 = *base.getAddr32(x, y);
216 SkPMColor c1 = *target.getAddr32(x, y);
217 SkPMColor d = 0;
218 if (c0 != c1) {
219 d = compute_diff_pmcolor(c0, c1);
220 }
221 *diff->getAddr32(x, y) = d;
222 }
223 }
224 }
225
226 // Compares "target" and "base" bitmaps, returning the result
227 // (ERROR_NONE if the two bitmaps are identical).
228 //
229 // If a "diff" bitmap is passed in, pixel diffs (if any) will be written
230 // into it.
231 //
232 // The "name" and "renderModeDescriptor" arguments are only used
233 // in the debug output.
234 static ErrorBitfield compare(const SkBitmap& target, const SkBitmap& base,
235 const SkString& name,
236 const char* renderModeDescriptor,
237 SkBitmap* diff) {
238 SkBitmap copy;
239 const SkBitmap* bm = &target;
240 if (target.config() != SkBitmap::kARGB_8888_Config) {
241 target.copyTo(&copy, SkBitmap::kARGB_8888_Config);
242 bm = &copy;
243 }
244 SkBitmap baseCopy;
245 const SkBitmap* bp = &base;
246 if (base.config() != SkBitmap::kARGB_8888_Config) {
247 base.copyTo(&baseCopy, SkBitmap::kARGB_8888_Config);
248 bp = &baseCopy;
249 }
250
251 force_all_opaque(*bm);
252 force_all_opaque(*bp);
253
254 const int w = bm->width();
255 const int h = bm->height();
256 if (w != bp->width() || h != bp->height()) {
257 SkDebugf(
258 "---- %s dimensions mismatch for %s base [%d %d] current [%d %d]\n",
259 renderModeDescriptor, name.c_str(),
260 bp->width(), bp->height(), w, h);
261 return ERROR_DIMENSION_MISMATCH;
262 }
263
264 SkAutoLockPixels bmLock(*bm);
265 SkAutoLockPixels baseLock(*bp);
266
267 for (int y = 0; y < h; y++) {
268 for (int x = 0; x < w; x++) {
269 SkPMColor c0 = *bp->getAddr32(x, y);
270 SkPMColor c1 = *bm->getAddr32(x, y);
271 if (c0 != c1) {
272 SkDebugf(
273 "----- %s pixel mismatch for %s at [%d %d] base 0x%08X current 0x%08X\n",
274 renderModeDescriptor, name.c_str(), x, y, c0, c1);
275
276 if (diff) {
277 diff->setConfig(SkBitmap::kARGB_8888_Config, w, h);
278 diff->allocPixels();
279 compute_diff(*bm, *bp, diff);
280 }
281 return ERROR_PIXEL_MISMATCH;
282 }
283 }
284 }
285
286 // they're equal
287 return ERROR_NONE;
288 }
289
290 static bool write_document(const SkString& path,
291 const SkDynamicMemoryWStream& document) {
292 SkFILEWStream stream(path.c_str());
293 SkAutoDataUnref data(document.copyToData());
294 return stream.writeData(data.get());
295 }
296
297 /// Returns true if processing should continue, false to skip the
298 /// remainder of this config for this GM.
299 //@todo thudson 22 April 2011 - could refactor this to take in
300 // a factory to generate the context, always call readPixels()
301 // (logically a noop for rasters, if wasted time), and thus collapse the
302 // GPU special case and also let this be used for SkPicture testing.
303 static void setup_bitmap(const ConfigData& gRec, SkISize& size,
304 SkBitmap* bitmap) {
305 bitmap->setConfig(gRec.fConfig, size.width(), size.height());
306 bitmap->allocPixels();
307 bitmap->eraseColor(0);
308 }
309
310 static void installFilter(SkCanvas* canvas) {
311 if (gForceBWtext) {
312 canvas->setDrawFilter(new BWTextDrawFilter)->unref();
313 }
314 }
315
316 static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF = false) {
317 SkAutoCanvasRestore acr(canvas, true);
318
319 if (!isPDF) {
320 canvas->concat(gm->getInitialTransform());
321 }
322 installFilter(canvas);
323 gm->draw(canvas);
324 canvas->setDrawFilter(NULL);
325 }
326
327 static ErrorBitfield generate_image(GM* gm, const ConfigData& gRec,
328 GrContext* context,
329 GrRenderTarget* rt,
330 SkBitmap* bitmap,
331 bool deferred) {
332 SkISize size (gm->getISize());
333 setup_bitmap(gRec, size, bitmap);
334
335 SkAutoTUnref<SkCanvas> canvas;
336
337 if (gRec.fBackend == kRaster_Backend) {
338 SkAutoTUnref<SkDevice> device(new SkDevice(*bitmap));
339 if (deferred) {
340 canvas.reset(new SkDeferredCanvas(device));
341 } else {
342 canvas.reset(new SkCanvas(device));
343 }
344 invokeGM(gm, canvas);
345 canvas->flush();
346 }
347#if SK_SUPPORT_GPU
348 else { // GPU
349 if (NULL == context) {
350 return ERROR_NO_GPU_CONTEXT;
351 }
352 SkAutoTUnref<SkDevice> device(new SkGpuDevice(context, rt));
353 if (deferred) {
354 canvas.reset(new SkDeferredCanvas(device));
355 } else {
356 canvas.reset(new SkCanvas(device));
357 }
358 invokeGM(gm, canvas);
359 // the device is as large as the current rendertarget, so
360 // we explicitly only readback the amount we expect (in
361 // size) overwrite our previous allocation
362 bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
363 size.fHeight);
364 canvas->readPixels(bitmap, 0, 0);
365 }
366#endif
367 return ERROR_NONE;
368 }
369
370 static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
371 SkPicture* pict, SkBitmap* bitmap) {
372 SkISize size = gm->getISize();
373 setup_bitmap(gRec, size, bitmap);
374 SkCanvas canvas(*bitmap);
375 installFilter(&canvas);
376 canvas.drawPicture(*pict);
377 }
378
379 static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
380#ifdef SK_SUPPORT_PDF
381 SkMatrix initialTransform = gm->getInitialTransform();
382 SkISize pageSize = gm->getISize();
383 SkPDFDevice* dev = NULL;
384 if (initialTransform.isIdentity()) {
385 dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
386 } else {
387 SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
388 SkIntToScalar(pageSize.height()));
389 initialTransform.mapRect(&content);
390 content.intersect(0, 0, SkIntToScalar(pageSize.width()),
391 SkIntToScalar(pageSize.height()));
392 SkISize contentSize =
393 SkISize::Make(SkScalarRoundToInt(content.width()),
394 SkScalarRoundToInt(content.height()));
395 dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
396 }
397 SkAutoUnref aur(dev);
398
399 SkCanvas c(dev);
400 invokeGM(gm, &c, true);
401
402 SkPDFDocument doc;
403 doc.appendPage(dev);
404 doc.emitPDF(&pdf);
405#endif
406 }
407
408 static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
409#ifdef SK_SUPPORT_XPS
410 SkISize size = gm->getISize();
411
412 SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
413 SkIntToScalar(size.height()));
414 static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
415 static const SkScalar upm = 72 * inchesPerMeter;
416 SkVector unitsPerMeter = SkPoint::Make(upm, upm);
417 static const SkScalar ppm = 200 * inchesPerMeter;
418 SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
419
420 SkXPSDevice* dev = new SkXPSDevice();
421 SkAutoUnref aur(dev);
422
423 SkCanvas c(dev);
424 dev->beginPortfolio(&xps);
425 dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
426 invokeGM(gm, &c);
427 dev->endSheet();
428 dev->endPortfolio();
429
430#endif
431 }
432
433 static ErrorBitfield write_reference_image(
434 const ConfigData& gRec, const char writePath [],
435 const char renderModeDescriptor [], const SkString& name,
436 SkBitmap& bitmap, SkDynamicMemoryWStream* document) {
437 SkString path;
438 bool success = false;
439 if (gRec.fBackend == kRaster_Backend ||
440 gRec.fBackend == kGPU_Backend ||
441 (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
442
443 path = make_filename(writePath, renderModeDescriptor, name, "png");
444 success = write_bitmap(path, bitmap);
445 }
446 if (kPDF_Backend == gRec.fBackend) {
447 path = make_filename(writePath, renderModeDescriptor, name, "pdf");
448 success = write_document(path, *document);
449 }
450 if (kXPS_Backend == gRec.fBackend) {
451 path = make_filename(writePath, renderModeDescriptor, name, "xps");
452 success = write_document(path, *document);
453 }
454 if (success) {
455 return ERROR_NONE;
456 } else {
457 fprintf(stderr, "FAILED to write %s\n", path.c_str());
458 return ERROR_WRITING_REFERENCE_IMAGE;
459 }
460 }
461
462 // Compares bitmap "bitmap" to "referenceBitmap"; if they are
463 // different, writes out "bitmap" (in PNG format) within the
464 // diffPath subdir.
465 //
466 // Returns the ErrorBitfield from compare(), describing any differences
467 // between "bitmap" and "referenceBitmap" (or ERROR_NONE if there are none).
468 static ErrorBitfield compare_to_reference_image_in_memory(
469 const SkString& name, SkBitmap &bitmap, const SkBitmap& referenceBitmap,
470 const char diffPath [], const char renderModeDescriptor []) {
471 ErrorBitfield errors;
472 SkBitmap diffBitmap;
473 errors = compare(bitmap, referenceBitmap, name, renderModeDescriptor,
474 diffPath ? &diffBitmap : NULL);
475 if ((ERROR_NONE != errors) && diffPath) {
476 // write out the generated image
477 SkString genName = make_filename(diffPath, "", name, "png");
478 if (!write_bitmap(genName, bitmap)) {
479 errors |= ERROR_WRITING_REFERENCE_IMAGE;
480 }
481 }
482 return errors;
483 }
484
485 // Compares bitmap "bitmap" to a reference bitmap read from disk;
486 // if they are different, writes out "bitmap" (in PNG format)
487 // within the diffPath subdir.
488 //
489 // Returns a description of the difference between "bitmap" and
490 // the reference bitmap, or ERROR_READING_REFERENCE_IMAGE if
491 // unable to read the reference bitmap from disk.
492 static ErrorBitfield compare_to_reference_image_on_disk(
493 const char readPath [], const SkString& name, SkBitmap &bitmap,
494 const char diffPath [], const char renderModeDescriptor []) {
495 SkString path = make_filename(readPath, "", name, "png");
496 SkBitmap referenceBitmap;
497 if (SkImageDecoder::DecodeFile(path.c_str(), &referenceBitmap,
498 SkBitmap::kARGB_8888_Config,
499 SkImageDecoder::kDecodePixels_Mode,
500 NULL)) {
501 return compare_to_reference_image_in_memory(name, bitmap,
502 referenceBitmap,
503 diffPath,
504 renderModeDescriptor);
505 } else {
506 if (gNotifyMissingReadReference) {
507 fprintf(stderr, "FAILED to read %s\n", path.c_str());
508 }
509 return ERROR_READING_REFERENCE_IMAGE;
510 }
511 }
512
513 // NOTE: As far as I can tell, this function is NEVER called with a
514 // non-blank renderModeDescriptor, EXCEPT when readPath and writePath are
515 // both NULL (and thus no images are read from or written to disk).
516 // So I don't trust that the renderModeDescriptor is being used for
517 // anything other than debug output these days.
518 static ErrorBitfield handle_test_results(GM* gm,
519 const ConfigData& gRec,
520 const char writePath [],
521 const char readPath [],
522 const char diffPath [],
523 const char renderModeDescriptor [],
524 SkBitmap& bitmap,
525 SkDynamicMemoryWStream* pdf,
526 const SkBitmap* referenceBitmap) {
527 SkString name = make_name(gm->shortName(), gRec.fName);
528 ErrorBitfield retval = ERROR_NONE;
529
530 if (readPath && (gRec.fFlags & kRead_ConfigFlag)) {
531 retval |= compare_to_reference_image_on_disk(readPath, name, bitmap,
532 diffPath,
533 renderModeDescriptor);
534 }
535 if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
536 retval |= write_reference_image(gRec, writePath,
537 renderModeDescriptor,
538 name, bitmap, pdf);
539 }
540 if (referenceBitmap) {
541 retval |= compare_to_reference_image_in_memory(
542 name, bitmap, *referenceBitmap, diffPath, renderModeDescriptor);
543 }
544 return retval;
545 }
546
547 static SkPicture* generate_new_picture(GM* gm) {
548 // Pictures are refcounted so must be on heap
549 SkPicture* pict = new SkPicture;
550 SkISize size = gm->getISize();
551 SkCanvas* cv = pict->beginRecording(size.width(), size.height());
552 invokeGM(gm, cv);
553 pict->endRecording();
554
555 return pict;
556 }
557
558 static SkPicture* stream_to_new_picture(const SkPicture& src) {
559
560 // To do in-memory commiunications with a stream, we need to:
561 // * create a dynamic memory stream
562 // * copy it into a buffer
563 // * create a read stream from it
564 // ?!?!
565
566 SkDynamicMemoryWStream storage;
567 src.serialize(&storage);
568
569 int streamSize = storage.getOffset();
570 SkAutoMalloc dstStorage(streamSize);
571 void* dst = dstStorage.get();
572 //char* dst = new char [streamSize];
573 //@todo thudson 22 April 2011 when can we safely delete [] dst?
574 storage.copyTo(dst);
575 SkMemoryStream pictReadback(dst, streamSize);
576 SkPicture* retval = new SkPicture (&pictReadback);
577 return retval;
578 }
579
580 // Test: draw into a bitmap or pdf.
581 // Depending on flags, possibly compare to an expected image
582 // and possibly output a diff image if it fails to match.
583 static ErrorBitfield test_drawing(GM* gm,
584 const ConfigData& gRec,
585 const char writePath [],
586 const char readPath [],
587 const char diffPath [],
588 GrContext* context,
589 GrRenderTarget* rt,
590 SkBitmap* bitmap) {
591 SkDynamicMemoryWStream document;
592
593 if (gRec.fBackend == kRaster_Backend ||
594 gRec.fBackend == kGPU_Backend) {
595 // Early exit if we can't generate the image.
596 ErrorBitfield errors = generate_image(gm, gRec, context, rt, bitmap,
597 false);
598 if (ERROR_NONE != errors) {
599 return errors;
600 }
601 } else if (gRec.fBackend == kPDF_Backend) {
602 generate_pdf(gm, document);
603#if CAN_IMAGE_PDF
604 SkAutoDataUnref data(document.copyToData());
605 SkMemoryStream stream(data->data(), data->size());
606 SkPDFDocumentToBitmap(&stream, bitmap);
607#endif
608 } else if (gRec.fBackend == kXPS_Backend) {
609 generate_xps(gm, document);
610 }
611 return handle_test_results(gm, gRec, writePath, readPath, diffPath,
612 "", *bitmap, &document, NULL);
613 }
614
615 static ErrorBitfield test_deferred_drawing(GM* gm,
616 const ConfigData& gRec,
617 const SkBitmap& referenceBitmap,
618 const char diffPath [],
619 GrContext* context,
620 GrRenderTarget* rt) {
621 SkDynamicMemoryWStream document;
622
623 if (gRec.fBackend == kRaster_Backend ||
624 gRec.fBackend == kGPU_Backend) {
625 SkBitmap bitmap;
626 // Early exit if we can't generate the image, but this is
627 // expected in some cases, so don't report a test failure.
628 if (!generate_image(gm, gRec, context, rt, &bitmap, true)) {
629 return ERROR_NONE;
630 }
631 return handle_test_results(gm, gRec, NULL, NULL, diffPath,
632 "-deferred", bitmap, NULL,
633 &referenceBitmap);
634 }
635 return ERROR_NONE;
636 }
637
638 static ErrorBitfield test_pipe_playback(GM* gm,
639 const ConfigData& gRec,
640 const SkBitmap& referenceBitmap,
641 const char readPath [],
642 const char diffPath []) {
643 ErrorBitfield errors = ERROR_NONE;
644 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
645 SkBitmap bitmap;
646 SkISize size = gm->getISize();
647 setup_bitmap(gRec, size, &bitmap);
648 SkCanvas canvas(bitmap);
649 PipeController pipeController(&canvas);
650 SkGPipeWriter writer;
651 SkCanvas* pipeCanvas = writer.startRecording(
652 &pipeController, gPipeWritingFlagCombos[i].flags);
653 invokeGM(gm, pipeCanvas);
654 writer.endRecording();
655 SkString string("-pipe");
656 string.append(gPipeWritingFlagCombos[i].name);
657 errors |= handle_test_results(gm, gRec, NULL, NULL, diffPath,
658 string.c_str(), bitmap, NULL,
659 &referenceBitmap);
660 if (errors != ERROR_NONE) {
661 break;
662 }
663 }
664 return errors;
665 }
666
667 static ErrorBitfield test_tiled_pipe_playback(
668 GM* gm, const ConfigData& gRec, const SkBitmap& referenceBitmap,
669 const char readPath [], const char diffPath []) {
670 ErrorBitfield errors = ERROR_NONE;
671 for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
672 SkBitmap bitmap;
673 SkISize size = gm->getISize();
674 setup_bitmap(gRec, size, &bitmap);
675 SkCanvas canvas(bitmap);
676 TiledPipeController pipeController(bitmap);
677 SkGPipeWriter writer;
678 SkCanvas* pipeCanvas = writer.startRecording(
679 &pipeController, gPipeWritingFlagCombos[i].flags);
680 invokeGM(gm, pipeCanvas);
681 writer.endRecording();
682 SkString string("-tiled pipe");
683 string.append(gPipeWritingFlagCombos[i].name);
684 errors |= handle_test_results(gm, gRec, NULL, NULL, diffPath,
685 string.c_str(), bitmap, NULL,
686 &referenceBitmap);
687 if (errors != ERROR_NONE) {
688 break;
689 }
690 }
691 return errors;
692 }
693}; // end of GMMain class definition
scroggo@google.com72c96722012-06-06 21:07:10 +0000694
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000695#if SK_SUPPORT_GPU
696static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
697#else
698static const GLContextType kDontCare_GLContextType = 0;
699#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +0000700
701// If the platform does not support writing PNGs of PDFs then there will be no
epoger@google.comf28dd8a2012-10-25 16:27:34 +0000702// reference images to read. However, we can always write the .pdf files
bsalomon@google.com7361f542012-04-19 19:15:35 +0000703static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
704 kWrite_ConfigFlag;
705
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000706static const ConfigData gRec[] = {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000707 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "8888" },
708 { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "4444" },
709 { SkBitmap::kRGB_565_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "565" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000710#if defined(SK_SCALAR_IS_FLOAT) && SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +0000711 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 0, kRW_ConfigFlag, "gpu" },
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000712#ifndef SK_BUILD_FOR_ANDROID
713 // currently we don't want to run MSAA tests on Android
bsalomon@google.com7361f542012-04-19 19:15:35 +0000714 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag, "msaa16" },
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000715#endif
bsalomon@google.com7361f542012-04-19 19:15:35 +0000716 /* The debug context does not generate images */
717 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kDebug_GLContextType, 0, kNone_ConfigFlag, "debug" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000718#if SK_ANGLE
bsalomon@google.com7361f542012-04-19 19:15:35 +0000719 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 0, kRW_ConfigFlag, "angle" },
720 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kANGLE_GLContextType, 16, kRW_ConfigFlag, "anglemsaa16" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000721#endif // SK_ANGLE
722#ifdef SK_MESA
bsalomon@google.com7361f542012-04-19 19:15:35 +0000723 { SkBitmap::kARGB_8888_Config, kGPU_Backend, GrContextFactory::kMESA_GLContextType, 0, kRW_ConfigFlag, "mesa" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000724#endif // SK_MESA
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000725#endif // defined(SK_SCALAR_IS_FLOAT) && SK_SUPPORT_GPU
bungeman@google.comb29c8832011-10-10 13:19:10 +0000726#ifdef SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000727 /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
728 { SkBitmap::kARGB_8888_Config, kXPS_Backend, kDontCare_GLContextType, 0, kWrite_ConfigFlag, "xps" },
robertphillips@google.coma73e8602012-08-02 17:56:02 +0000729#endif // SK_SUPPORT_XPS
bsalomon@google.com7361f542012-04-19 19:15:35 +0000730#ifdef SK_SUPPORT_PDF
731 { SkBitmap::kARGB_8888_Config, kPDF_Backend, kDontCare_GLContextType, 0, kPDFConfigFlags, "pdf" },
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000732#endif // SK_SUPPORT_PDF
reed@android.com00dae862009-06-10 15:38:48 +0000733};
734
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000735static void usage(const char * argv0) {
736 SkDebugf("%s\n", argv0);
737 SkDebugf(" [-w writePath] [-r readPath] [-d diffPath] [-i resourcePath]\n");
chudy@google.comf32f6e82012-07-12 15:42:37 +0000738 SkDebugf(" [-wp writePicturePath]\n");
reed@google.come5f48b92012-06-22 15:27:39 +0000739 SkDebugf(" [--config ");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000740 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
741 if (i > 0) {
742 SkDebugf("|");
743 }
744 SkDebugf(gRec[i].fName);
745 }
746 SkDebugf(" ]\n");
scroggo@google.com39362522012-09-14 17:37:46 +0000747 SkDebugf(" [--noreplay] [--nopipe] [--noserialize] [--forceBWtext] [--nopdf] \n"
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000748 " [--tiledPipe] \n"
749 " [--nodeferred] [--match substring] [--notexturecache]\n"
750 " [-h|--help]\n"
751 );
752 SkDebugf(" writePath: directory to write rendered images in.\n");
chudy@google.comf32f6e82012-07-12 15:42:37 +0000753 SkDebugf(" writePicturePath: directory to write images to in .skp format.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000754 SkDebugf(
755 " readPath: directory to read reference images from;\n"
756 " reports if any pixels mismatch between reference and new images\n");
757 SkDebugf(" diffPath: directory to write difference images in.\n");
758 SkDebugf(" resourcePath: directory that stores image resources.\n");
759 SkDebugf(" --noreplay: do not exercise SkPicture replay.\n");
scroggo@google.com565254b2012-06-28 15:41:32 +0000760 SkDebugf(" --nopipe: Skip SkGPipe replay.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000761 SkDebugf(" --tiledPipe: Exercise tiled SkGPipe replay.\n");
762 SkDebugf(
scroggo@google.com39362522012-09-14 17:37:46 +0000763 " --noserialize: do not exercise SkPicture serialization & deserialization.\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000764 SkDebugf(" --forceBWtext: disable text anti-aliasing.\n");
765 SkDebugf(" --nopdf: skip the pdf rendering test pass.\n");
766 SkDebugf(" --nodeferred: skip the deferred rendering test pass.\n");
767 SkDebugf(" --match foo: will only run tests that substring match foo.\n");
768 SkDebugf(" --notexturecache: disable the gpu texture cache.\n");
769 SkDebugf(" -h|--help : Show this help message. \n");
770}
771
772static int findConfig(const char config[]) {
773 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
774 if (!strcmp(config, gRec[i].fName)) {
775 return i;
776 }
777 }
778 return -1;
779}
780
reed@google.comb2a51622011-10-31 16:30:04 +0000781static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
782 if (0 == array.count()) {
783 // no names, so don't skip anything
784 return false;
785 }
786 for (int i = 0; i < array.count(); ++i) {
787 if (strstr(name, array[i])) {
788 // found the name, so don't skip
789 return false;
790 }
791 }
792 return true;
793}
794
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000795namespace skiagm {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000796#if SK_SUPPORT_GPU
bsalomon@google.com7361f542012-04-19 19:15:35 +0000797SkAutoTUnref<GrContext> gGrContext;
798/**
799 * Sets the global GrContext, accessible by indivual GMs
800 */
caryclark@google.com13130862012-06-06 12:10:45 +0000801static void SetGr(GrContext* grContext) {
bsalomon@google.com7361f542012-04-19 19:15:35 +0000802 SkSafeRef(grContext);
803 gGrContext.reset(grContext);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000804}
bsalomon@google.com7361f542012-04-19 19:15:35 +0000805
806/**
807 * Gets the global GrContext, can be called by GM tests.
808 */
caryclark@google.com13130862012-06-06 12:10:45 +0000809GrContext* GetGr();
bsalomon@google.com7361f542012-04-19 19:15:35 +0000810GrContext* GetGr() {
811 return gGrContext.get();
812}
813
814/**
815 * Sets the global GrContext and then resets it to its previous value at
816 * destruction.
817 */
818class AutoResetGr : SkNoncopyable {
819public:
820 AutoResetGr() : fOld(NULL) {}
821 void set(GrContext* context) {
822 SkASSERT(NULL == fOld);
823 fOld = GetGr();
824 SkSafeRef(fOld);
825 SetGr(context);
826 }
827 ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
828private:
829 GrContext* fOld;
830};
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000831#else
832GrContext* GetGr() { return NULL; }
833#endif
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000834}
835
djsollen@google.comebce16d2012-10-26 14:07:13 +0000836static bool is_recordable_failure(ErrorBitfield errorCode) {
837 return ERROR_NONE != errorCode && !(ERROR_READING_REFERENCE_IMAGE & errorCode);
838}
839
caryclark@google.com5987f582012-10-02 18:33:14 +0000840int tool_main(int argc, char** argv);
841int tool_main(int argc, char** argv) {
robertphillips@google.comb74af872012-06-27 19:41:42 +0000842
843#ifdef SK_ENABLE_INST_COUNT
844 gPrintInstCount = true;
845#endif
846
robertphillips@google.com977b9c82012-06-05 19:35:09 +0000847 SkGraphics::Init();
reed@google.com8923c6c2011-11-08 14:59:38 +0000848 // we don't need to see this during a run
849 gSkSuppressFontCachePurgeSpew = true;
reed@google.comd4dfd102011-01-18 21:05:42 +0000850
epoger@google.com7bc13a62012-02-14 14:53:59 +0000851 setSystemPreferences();
epoger@google.comde961632012-10-26 18:56:36 +0000852 GMMain gmmain;
epoger@google.com7bc13a62012-02-14 14:53:59 +0000853
reed@android.com8015dd82009-06-21 00:49:18 +0000854 const char* writePath = NULL; // if non-null, where we write the originals
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000855 const char* writePicturePath = NULL; // if non-null, where we write serialized pictures
reed@android.com8015dd82009-06-21 00:49:18 +0000856 const char* readPath = NULL; // if non-null, were we read from to compare
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000857 const char* diffPath = NULL; // if non-null, where we write our diffs (from compare)
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000858 const char* resourcePath = NULL;// if non-null, where we read from for image resources
reed@android.com8015dd82009-06-21 00:49:18 +0000859
reed@google.comb2a51622011-10-31 16:30:04 +0000860 SkTDArray<const char*> fMatches;
twiz@google.come24a0792012-01-31 18:35:30 +0000861
reed@google.comab973972011-09-19 19:01:38 +0000862 bool doPDF = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000863 bool doReplay = true;
scroggo@google.com565254b2012-06-28 15:41:32 +0000864 bool doPipe = true;
scroggo@google.com72c96722012-06-06 21:07:10 +0000865 bool doTiledPipe = false;
scroggo@google.com39362522012-09-14 17:37:46 +0000866 bool doSerialize = true;
junov@google.com4370aed2012-01-18 16:21:08 +0000867 bool doDeferred = true;
twiz@google.come24a0792012-01-31 18:35:30 +0000868 bool disableTextureCache = false;
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000869 SkTDArray<size_t> configs;
870 bool userConfig = false;
twiz@google.come24a0792012-01-31 18:35:30 +0000871
reed@google.comae7b8f32012-10-18 21:30:57 +0000872 int moduloIndex = -1;
873 int moduloCount = -1;
874
reed@google.come8fcb502012-05-17 15:28:20 +0000875 gNotifyMissingReadReference = true;
876
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000877 const char* const commandName = argv[0];
reed@android.com8015dd82009-06-21 00:49:18 +0000878 char* const* stop = argv + argc;
879 for (++argv; argv < stop; ++argv) {
880 if (strcmp(*argv, "-w") == 0) {
881 argv++;
882 if (argv < stop && **argv) {
883 writePath = *argv;
884 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000885 } else if (strcmp(*argv, "-wp") == 0) {
886 argv++;
887 if (argv < stop && **argv) {
888 writePicturePath = *argv;
889 }
reed@android.com8015dd82009-06-21 00:49:18 +0000890 } else if (strcmp(*argv, "-r") == 0) {
891 argv++;
892 if (argv < stop && **argv) {
893 readPath = *argv;
894 }
reed@google.com3d3f0922010-12-20 21:10:29 +0000895 } else if (strcmp(*argv, "-d") == 0) {
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000896 argv++;
reed@google.com3d3f0922010-12-20 21:10:29 +0000897 if (argv < stop && **argv) {
898 diffPath = *argv;
899 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000900 } else if (strcmp(*argv, "-i") == 0) {
901 argv++;
902 if (argv < stop && **argv) {
903 resourcePath = *argv;
904 }
mike@reedtribe.org10afbef2011-12-30 16:02:53 +0000905 } else if (strcmp(*argv, "--forceBWtext") == 0) {
906 gForceBWtext = true;
scroggo@google.com565254b2012-06-28 15:41:32 +0000907 } else if (strcmp(*argv, "--nopipe") == 0) {
908 doPipe = false;
scroggo@google.com72c96722012-06-06 21:07:10 +0000909 } else if (strcmp(*argv, "--tiledPipe") == 0) {
910 doTiledPipe = true;
reed@google.comb8b09832011-05-26 15:57:56 +0000911 } else if (strcmp(*argv, "--noreplay") == 0) {
912 doReplay = false;
reed@google.comab973972011-09-19 19:01:38 +0000913 } else if (strcmp(*argv, "--nopdf") == 0) {
914 doPDF = false;
junov@google.com4370aed2012-01-18 16:21:08 +0000915 } else if (strcmp(*argv, "--nodeferred") == 0) {
916 doDeferred = false;
reed@google.comae7b8f32012-10-18 21:30:57 +0000917 } else if (strcmp(*argv, "--modulo") == 0) {
918 ++argv;
919 if (argv >= stop) {
920 continue;
921 }
922 moduloIndex = atoi(*argv);
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000923
reed@google.comae7b8f32012-10-18 21:30:57 +0000924 ++argv;
925 if (argv >= stop) {
926 continue;
927 }
928 moduloCount = atoi(*argv);
reed@google.come8fcb502012-05-17 15:28:20 +0000929 } else if (strcmp(*argv, "--disable-missing-warning") == 0) {
930 gNotifyMissingReadReference = false;
931 } else if (strcmp(*argv, "--enable-missing-warning") == 0) {
932 gNotifyMissingReadReference = true;
tomhudson@google.com73fb0422011-04-25 19:20:54 +0000933 } else if (strcmp(*argv, "--serialize") == 0) {
epoger@google.comde961632012-10-26 18:56:36 +0000934 // Leaving in this option so that a user need not modify
935 // their command line arguments to still run.
borenet@google.com14ca1d32012-06-15 13:46:44 +0000936 doSerialize = true;
scroggo@google.com39362522012-09-14 17:37:46 +0000937 } else if (strcmp(*argv, "--noserialize") == 0) {
938 doSerialize = false;
reed@google.comece2b022011-07-25 14:28:57 +0000939 } else if (strcmp(*argv, "--match") == 0) {
940 ++argv;
941 if (argv < stop && **argv) {
reed@google.comb2a51622011-10-31 16:30:04 +0000942 // just record the ptr, no need for a deep copy
943 *fMatches.append() = *argv;
reed@google.comece2b022011-07-25 14:28:57 +0000944 }
twiz@google.come24a0792012-01-31 18:35:30 +0000945 } else if (strcmp(*argv, "--notexturecache") == 0) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000946 disableTextureCache = true;
reed@google.come5f48b92012-06-22 15:27:39 +0000947 } else if (strcmp(*argv, "--config") == 0) {
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000948 argv++;
949 if (argv < stop) {
950 int index = findConfig(*argv);
951 if (index >= 0) {
952 *configs.append() = index;
953 userConfig = true;
954 } else {
955 SkString str;
956 str.printf("unrecognized config %s\n", *argv);
957 SkDebugf(str.c_str());
958 usage(commandName);
959 return -1;
960 }
961 } else {
reed@google.come5f48b92012-06-22 15:27:39 +0000962 SkDebugf("missing arg for --config\n");
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000963 usage(commandName);
964 return -1;
965 }
966 } else if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
967 usage(commandName);
968 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000969 } else {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000970 usage(commandName);
971 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000972 }
973 }
974 if (argv != stop) {
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000975 usage(commandName);
976 return -1;
tomhudson@google.com9875dd12011-04-25 15:49:53 +0000977 }
reed@google.com873cb1e2010-12-23 15:00:45 +0000978
scroggo@google.com5867c0f2012-06-07 17:39:48 +0000979 if (!userConfig) {
980 // if no config is specified by user, we add them all.
981 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
982 *configs.append() = i;
983 }
984 }
985
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000986 GM::SetResourcePath(resourcePath);
987
reed@android.com00f883e2010-12-14 17:46:14 +0000988 if (readPath) {
989 fprintf(stderr, "reading from %s\n", readPath);
chudy@google.comf32f6e82012-07-12 15:42:37 +0000990 }
epoger@google.com9284ccd2012-04-18 13:36:54 +0000991 if (writePath) {
reed@android.com00f883e2010-12-14 17:46:14 +0000992 fprintf(stderr, "writing to %s\n", writePath);
993 }
mike@reedtribe.org5d0c62f2012-06-02 14:50:13 +0000994 if (writePicturePath) {
995 fprintf(stderr, "writing pictures to %s\n", writePicturePath);
996 }
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000997 if (resourcePath) {
998 fprintf(stderr, "reading resources from %s\n", resourcePath);
999 }
1000
reed@google.comae7b8f32012-10-18 21:30:57 +00001001 if (moduloCount <= 0) {
1002 moduloIndex = -1;
1003 }
1004 if (moduloIndex < 0 || moduloIndex >= moduloCount) {
1005 moduloIndex = -1;
1006 }
1007
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001008 // Accumulate success of all tests.
1009 int testsRun = 0;
1010 int testsPassed = 0;
1011 int testsFailed = 0;
1012 int testsMissingReferenceImages = 0;
1013
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001014#if SK_SUPPORT_GPU
1015 GrContextFactory* grFactory = new GrContextFactory;
twiz@google.come24a0792012-01-31 18:35:30 +00001016 if (disableTextureCache) {
1017 skiagm::GetGr()->setTextureCacheLimits(0, 0);
1018 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001019#endif
twiz@google.come24a0792012-01-31 18:35:30 +00001020
robertphillips@google.coma2f80082012-08-02 16:22:47 +00001021 SkTArray<SkString> failedTests;
1022
reed@google.comae7b8f32012-10-18 21:30:57 +00001023 int gmIndex = -1;
1024 SkString moduloStr;
1025
bsalomon@google.com7361f542012-04-19 19:15:35 +00001026 Iter iter;
1027 GM* gm;
reed@android.com00dae862009-06-10 15:38:48 +00001028 while ((gm = iter.next()) != NULL) {
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +00001029
reed@google.comae7b8f32012-10-18 21:30:57 +00001030 ++gmIndex;
1031 if (moduloIndex >= 0) {
1032 if ((gmIndex % moduloCount) != moduloIndex) {
1033 continue;
1034 }
reed@google.comb5a5a9d2012-10-25 17:58:23 +00001035 moduloStr.printf("[%d.%d] ", gmIndex, moduloCount);
reed@google.comae7b8f32012-10-18 21:30:57 +00001036 }
1037
reed@google.comece2b022011-07-25 14:28:57 +00001038 const char* shortName = gm->shortName();
reed@google.comb2a51622011-10-31 16:30:04 +00001039 if (skip_name(fMatches, shortName)) {
reed@google.comece2b022011-07-25 14:28:57 +00001040 SkDELETE(gm);
1041 continue;
1042 }
1043
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001044 SkISize size = gm->getISize();
reed@google.comae7b8f32012-10-18 21:30:57 +00001045 SkDebugf("%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
reed@android.com8015dd82009-06-21 00:49:18 +00001046 size.width(), size.height());
djsollen@google.comebce16d2012-10-26 14:07:13 +00001047
1048 ErrorBitfield testErrors = ERROR_NONE;
1049 uint32_t gmFlags = gm->getFlags();
reed@android.com8015dd82009-06-21 00:49:18 +00001050
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001051 for (int i = 0; i < configs.count(); i++) {
1052 ConfigData config = gRec[configs[i]];
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001053 // Skip any tests that we don't even need to try.
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001054 if ((kPDF_Backend == config.fBackend) &&
bungeman@google.com64e011a2011-09-19 19:31:04 +00001055 (!doPDF || (gmFlags & GM::kSkipPDF_Flag)))
epoger@google.comde961632012-10-26 18:56:36 +00001056 {
1057 continue;
1058 }
reed@google.com1b6c73d2012-10-10 15:17:24 +00001059 if ((gmFlags & GM::kSkip565_Flag) &&
1060 (kRaster_Backend == config.fBackend) &&
1061 (SkBitmap::kRGB_565_Config == config.fConfig)) {
1062 continue;
1063 }
reed@google.comab973972011-09-19 19:01:38 +00001064
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001065 // Now we know that we want to run this test and record its
1066 // success or failure.
djsollen@google.comebce16d2012-10-26 14:07:13 +00001067 ErrorBitfield renderErrors = ERROR_NONE;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001068 GrRenderTarget* renderTarget = NULL;
1069#if SK_SUPPORT_GPU
1070 SkAutoTUnref<GrRenderTarget> rt;
1071 AutoResetGr autogr;
djsollen@google.comebce16d2012-10-26 14:07:13 +00001072 if ((ERROR_NONE == renderErrors) &&
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001073 kGPU_Backend == config.fBackend) {
1074 GrContext* gr = grFactory->get(config.fGLContextType);
1075 bool grSuccess = false;
1076 if (gr) {
1077 // create a render target to back the device
1078 GrTextureDesc desc;
1079 desc.fConfig = kSkia8888_PM_GrPixelConfig;
1080 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1081 desc.fWidth = gm->getISize().width();
1082 desc.fHeight = gm->getISize().height();
1083 desc.fSampleCnt = config.fSampleCnt;
1084 GrTexture* tex = gr->createUncachedTexture(desc, NULL, 0);
1085 if (tex) {
1086 rt.reset(tex->asRenderTarget());
1087 rt.get()->ref();
1088 tex->unref();
1089 autogr.set(gr);
1090 renderTarget = rt.get();
1091 grSuccess = NULL != renderTarget;
1092 }
1093 }
1094 if (!grSuccess) {
djsollen@google.comebce16d2012-10-26 14:07:13 +00001095 renderErrors |= ERROR_NO_GPU_CONTEXT;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001096 }
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001097 }
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001098#endif
vandebo@chromium.org686abdf2011-02-03 23:00:40 +00001099
djsollen@google.comebce16d2012-10-26 14:07:13 +00001100 SkBitmap comparisonBitmap;
1101
1102 if (ERROR_NONE == renderErrors) {
epoger@google.comde961632012-10-26 18:56:36 +00001103 renderErrors |= gmmain.test_drawing(gm, config, writePath,
1104 readPath, diffPath, GetGr(),
1105 renderTarget,
1106 &comparisonBitmap);
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001107 }
1108
djsollen@google.comebce16d2012-10-26 14:07:13 +00001109 if (doDeferred && !renderErrors &&
scroggo@google.com5867c0f2012-06-07 17:39:48 +00001110 (kGPU_Backend == config.fBackend ||
1111 kRaster_Backend == config.fBackend)) {
epoger@google.comde961632012-10-26 18:56:36 +00001112 renderErrors |= gmmain.test_deferred_drawing(gm, config,
1113 comparisonBitmap,
1114 diffPath, GetGr(),
1115 renderTarget);
junov@google.com4370aed2012-01-18 16:21:08 +00001116 }
1117
djsollen@google.comebce16d2012-10-26 14:07:13 +00001118 testErrors |= renderErrors;
1119 if (is_recordable_failure(renderErrors)) {
epoger@google.comde961632012-10-26 18:56:36 +00001120 failedTests.push_back(gmmain.make_name(shortName,
1121 config.fName));
tomhudson@google.com73fb0422011-04-25 19:20:54 +00001122 }
tomhudson@google.com9875dd12011-04-25 15:49:53 +00001123 }
djsollen@google.comebce16d2012-10-26 14:07:13 +00001124
1125 SkBitmap comparisonBitmap;
1126 const ConfigData compareConfig =
1127 { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison" };
epoger@google.comde961632012-10-26 18:56:36 +00001128 testErrors |= gmmain.generate_image(gm, compareConfig, NULL, NULL, &comparisonBitmap, false);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001129
1130 // run the picture centric GM steps
1131 if (!(gmFlags & GM::kSkipPicture_Flag)) {
1132
1133 ErrorBitfield pictErrors = ERROR_NONE;
1134
1135 //SkAutoTUnref<SkPicture> pict(generate_new_picture(gm));
epoger@google.comde961632012-10-26 18:56:36 +00001136 SkPicture* pict = gmmain.generate_new_picture(gm);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001137 SkAutoUnref aur(pict);
1138
1139 if ((ERROR_NONE == testErrors) && doReplay) {
1140 SkBitmap bitmap;
epoger@google.comde961632012-10-26 18:56:36 +00001141 gmmain.generate_image_from_picture(gm, compareConfig, pict,
1142 &bitmap);
1143 pictErrors |= gmmain.handle_test_results(gm, compareConfig,
1144 NULL, NULL, diffPath,
1145 "-replay", bitmap,
1146 NULL,
1147 &comparisonBitmap);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001148 if (is_recordable_failure(pictErrors)) {
epoger@google.comde961632012-10-26 18:56:36 +00001149 failedTests.push_back(gmmain.make_name(shortName,
1150 "pict-replay"));
djsollen@google.comebce16d2012-10-26 14:07:13 +00001151 }
1152 }
1153
epoger@google.comde961632012-10-26 18:56:36 +00001154 if ((ERROR_NONE == testErrors) &&
1155 (ERROR_NONE == pictErrors) &&
1156 doSerialize) {
1157 SkPicture* repict = gmmain.stream_to_new_picture(*pict);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001158 SkAutoUnref aurr(repict);
1159
1160 SkBitmap bitmap;
epoger@google.comde961632012-10-26 18:56:36 +00001161 gmmain.generate_image_from_picture(gm, compareConfig, repict,
1162 &bitmap);
1163 pictErrors |= gmmain.handle_test_results(gm, compareConfig,
1164 NULL, NULL, diffPath,
1165 "-serialize", bitmap,
1166 NULL,
1167 &comparisonBitmap);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001168 if (is_recordable_failure(pictErrors)) {
epoger@google.comde961632012-10-26 18:56:36 +00001169 failedTests.push_back(gmmain.make_name(shortName,
1170 "pict-serialize"));
djsollen@google.comebce16d2012-10-26 14:07:13 +00001171 }
1172 }
1173
1174 if (writePicturePath) {
1175 const char* pictureSuffix = "skp";
epoger@google.comde961632012-10-26 18:56:36 +00001176 SkString path = gmmain.make_filename(writePicturePath, "",
1177 SkString(gm->shortName()),
1178 pictureSuffix);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001179 SkFILEWStream stream(path.c_str());
1180 pict->serialize(&stream);
1181 }
1182
1183 testErrors |= pictErrors;
1184 }
1185
1186 // run the pipe centric GM steps
1187 if (!(gmFlags & GM::kSkipPipe_Flag)) {
1188
1189 ErrorBitfield pipeErrors = ERROR_NONE;
1190
1191 if ((ERROR_NONE == testErrors) && doPipe) {
epoger@google.comde961632012-10-26 18:56:36 +00001192 pipeErrors |= gmmain.test_pipe_playback(gm, compareConfig,
1193 comparisonBitmap,
1194 readPath, diffPath);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001195 if (is_recordable_failure(pipeErrors)) {
epoger@google.comde961632012-10-26 18:56:36 +00001196 failedTests.push_back(gmmain.make_name(shortName, "pipe"));
djsollen@google.comebce16d2012-10-26 14:07:13 +00001197 }
1198 }
1199
1200 if ((ERROR_NONE == testErrors) &&
1201 (ERROR_NONE == pipeErrors) &&
1202 doTiledPipe && !(gmFlags & GM::kSkipTiled_Flag)) {
epoger@google.comde961632012-10-26 18:56:36 +00001203 pipeErrors |= gmmain.test_tiled_pipe_playback(gm, compareConfig,
1204 comparisonBitmap,
1205 readPath,
1206 diffPath);
djsollen@google.comebce16d2012-10-26 14:07:13 +00001207 if (is_recordable_failure(pipeErrors)) {
epoger@google.comde961632012-10-26 18:56:36 +00001208 failedTests.push_back(gmmain.make_name(shortName,
1209 "pipe-tiled"));
djsollen@google.comebce16d2012-10-26 14:07:13 +00001210 }
1211 }
1212
1213 testErrors |= pipeErrors;
1214 }
1215
1216 // Update overall results.
1217 // We only tabulate the particular error types that we currently
1218 // care about (e.g., missing reference images). Later on, if we
1219 // want to also tabulate pixel mismatches vs dimension mistmatches
1220 // (or whatever else), we can do so.
1221 testsRun++;
1222 if (ERROR_NONE == testErrors) {
1223 testsPassed++;
1224 } else if (ERROR_READING_REFERENCE_IMAGE & testErrors) {
1225 testsMissingReferenceImages++;
1226 } else {
1227 testsFailed++;
1228 }
1229
reed@android.com00dae862009-06-10 15:38:48 +00001230 SkDELETE(gm);
1231 }
robertphillips@google.coma2f80082012-08-02 16:22:47 +00001232 SkDebugf("Ran %d tests: %d passed, %d failed, %d missing reference images\n",
1233 testsRun, testsPassed, testsFailed, testsMissingReferenceImages);
1234 for (int i = 0; i < failedTests.count(); ++i) {
1235 SkDebugf("\t\t%s\n", failedTests[i].c_str());
1236 }
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001237
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001238#if SK_SUPPORT_GPU
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001239
robertphillips@google.com59552022012-08-31 13:07:37 +00001240#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001241 for (int i = 0; i < configs.count(); i++) {
1242 ConfigData config = gRec[configs[i]];
1243
1244 if (kGPU_Backend == config.fBackend) {
1245 GrContext* gr = grFactory->get(config.fGLContextType);
1246
1247 SkDebugf("config: %s %x\n", config.fName, gr);
1248 gr->printCacheStats();
1249 }
1250 }
1251#endif
1252
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001253 delete grFactory;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00001254#endif
robertphillips@google.com977b9c82012-06-05 19:35:09 +00001255 SkGraphics::Term();
1256
epoger@google.comc7cf2b32011-12-28 19:31:01 +00001257 return (0 == testsFailed) ? 0 : -1;
reed@android.com00dae862009-06-10 15:38:48 +00001258}
caryclark@google.com5987f582012-10-02 18:33:14 +00001259
1260#if !defined SK_BUILD_FOR_IOS
1261int main(int argc, char * const argv[]) {
1262 return tool_main(argc, (char**) argv);
1263}
1264#endif