gmmain.cpp revision 07947d95657743a9c4c3bfec31e1205eb81e5910
1/*
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 */
7
8/*
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
16#include "gm.h"
17#include "gm_error.h"
18#include "gm_expectations.h"
19#include "system_preferences.h"
20#include "SkBitmap.h"
21#include "SkBitmapChecksummer.h"
22#include "SkColorPriv.h"
23#include "SkCommandLineFlags.h"
24#include "SkData.h"
25#include "SkDeferredCanvas.h"
26#include "SkDevice.h"
27#include "SkDrawFilter.h"
28#include "SkGPipe.h"
29#include "SkGraphics.h"
30#include "SkImageDecoder.h"
31#include "SkImageEncoder.h"
32#include "SkOSFile.h"
33#include "SkPicture.h"
34#include "SkRefCnt.h"
35#include "SkStream.h"
36#include "SkTArray.h"
37#include "SkTDict.h"
38#include "SkTileGridPicture.h"
39#include "SamplePipeControllers.h"
40
41#ifdef SK_BUILD_FOR_WIN
42    // json includes xlocale which generates warning 4530 because we're compiling without
43    // exceptions; see https://code.google.com/p/skia/issues/detail?id=1067
44    #pragma warning(push)
45    #pragma warning(disable : 4530)
46#endif
47#include "json/value.h"
48#ifdef SK_BUILD_FOR_WIN
49    #pragma warning(pop)
50#endif
51
52#if SK_SUPPORT_GPU
53#include "GrContextFactory.h"
54#include "SkGpuDevice.h"
55typedef GrContextFactory::GLContextType GLContextType;
56#define DEFAULT_CACHE_VALUE -1
57static int gGpuCacheSizeBytes;
58static int gGpuCacheSizeCount;
59#else
60class GrContextFactory;
61class GrContext;
62class GrSurface;
63typedef int GLContextType;
64#endif
65
66extern bool gSkSuppressFontCachePurgeSpew;
67
68#ifdef SK_SUPPORT_PDF
69    #include "SkPDFDevice.h"
70    #include "SkPDFDocument.h"
71#endif
72
73// 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
76#ifdef SK_SUPPORT_XPS
77    #include "SkXPSDevice.h"
78#endif
79
80#ifdef SK_BUILD_FOR_MAC
81    #include "SkCGUtils.h"
82    #define CAN_IMAGE_PDF   1
83#else
84    #define CAN_IMAGE_PDF   0
85#endif
86
87using namespace skiagm;
88
89class Iter {
90public:
91    Iter() {
92        this->reset();
93    }
94
95    void reset() {
96        fReg = GMRegistry::Head();
97    }
98
99    GM* next() {
100        if (fReg) {
101            GMRegistry::Factory fact = fReg->factory();
102            fReg = fReg->next();
103            return fact(0);
104        }
105        return NULL;
106    }
107
108    static int Count() {
109        const GMRegistry* reg = GMRegistry::Head();
110        int count = 0;
111        while (reg) {
112            count += 1;
113            reg = reg->next();
114        }
115        return count;
116    }
117
118private:
119    const GMRegistry* fReg;
120};
121
122enum Backend {
123    kRaster_Backend,
124    kGPU_Backend,
125    kPDF_Backend,
126    kXPS_Backend,
127};
128
129enum BbhType {
130    kNone_BbhType,
131    kRTree_BbhType,
132    kTileGrid_BbhType,
133};
134
135enum ConfigFlags {
136    kNone_ConfigFlag  = 0x0,
137    /* Write GM images if a write path is provided. */
138    kWrite_ConfigFlag = 0x1,
139    /* Read reference GM images if a read path is provided. */
140    kRead_ConfigFlag  = 0x2,
141    kRW_ConfigFlag    = (kWrite_ConfigFlag | kRead_ConfigFlag),
142};
143
144struct ConfigData {
145    SkBitmap::Config                fConfig;
146    Backend                         fBackend;
147    GLContextType                   fGLContextType; // GPU backend only
148    int                             fSampleCnt;     // GPU backend only
149    ConfigFlags                     fFlags;
150    const char*                     fName;
151    bool                            fRunByDefault;
152};
153
154class BWTextDrawFilter : public SkDrawFilter {
155public:
156    virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
157};
158bool BWTextDrawFilter::filter(SkPaint* p, Type t) {
159    if (kText_Type == t) {
160        p->setAntiAlias(false);
161    }
162    return true;
163}
164
165struct PipeFlagComboData {
166    const char* name;
167    uint32_t flags;
168};
169
170static PipeFlagComboData gPipeWritingFlagCombos[] = {
171    { "", 0 },
172    { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
173    { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
174        | SkGPipeWriter::kSharedAddressSpace_Flag }
175};
176
177class GMMain {
178public:
179    GMMain() : fUseFileHierarchy(false), fMismatchPath(NULL), fTestsRun(0),
180               fRenderModesEncountered(1) {
181        fIgnorableErrorCombination.add(kMissingExpectations_ErrorType);
182        fIgnorableErrorCombination.add(kIntentionallySkipped_ErrorType);
183    }
184
185    SkString make_name(const char shortName[], const char configName[]) {
186        SkString name;
187        if (0 == strlen(configName)) {
188            name.append(shortName);
189        } else if (fUseFileHierarchy) {
190            name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
191        } else {
192            name.appendf("%s_%s", shortName, configName);
193        }
194        return name;
195    }
196
197    /* since PNG insists on unpremultiplying our alpha, we take no
198       precision chances and force all pixels to be 100% opaque,
199       otherwise on compare we may not get a perfect match.
200    */
201    static void force_all_opaque(const SkBitmap& bitmap) {
202        SkBitmap::Config config = bitmap.config();
203        switch (config) {
204        case SkBitmap::kARGB_8888_Config:
205            force_all_opaque_8888(bitmap);
206            break;
207        case SkBitmap::kRGB_565_Config:
208            // nothing to do here; 565 bitmaps are inherently opaque
209            break;
210        default:
211            gm_fprintf(stderr, "unsupported bitmap config %d\n", config);
212            DEBUGFAIL_SEE_STDERR;
213        }
214    }
215
216    static void force_all_opaque_8888(const SkBitmap& bitmap) {
217        SkAutoLockPixels lock(bitmap);
218        for (int y = 0; y < bitmap.height(); y++) {
219            for (int x = 0; x < bitmap.width(); x++) {
220                *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
221            }
222        }
223    }
224
225    static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
226        // TODO(epoger): Now that we have removed force_all_opaque()
227        // from this method, we should be able to get rid of the
228        // transformation to 8888 format also.
229        SkBitmap copy;
230        bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
231        return SkImageEncoder::EncodeFile(path.c_str(), copy,
232                                          SkImageEncoder::kPNG_Type, 100);
233    }
234
235    /**
236     * Add all render modes encountered thus far to the "modes" array.
237     */
238    void GetRenderModesEncountered(SkTArray<SkString> &modes) {
239        SkTDict<int>::Iter iter(this->fRenderModesEncountered);
240        const char* mode;
241        while ((mode = iter.next(NULL)) != NULL) {
242            SkString modeAsString = SkString(mode);
243            // TODO(epoger): It seems a bit silly that all of these modes were
244            // recorded with a leading "-" which we have to remove here
245            // (except for mode "", which means plain old original mode).
246            // But that's how renderModeDescriptor has been passed into
247            // compare_test_results_to_reference_bitmap() historically,
248            // and changing that now may affect other parts of our code.
249            if (modeAsString.startsWith("-")) {
250                modeAsString.remove(0, 1);
251                modes.push_back(modeAsString);
252            }
253        }
254    }
255
256    /**
257     * Records the results of this test in fTestsRun and fFailedTests.
258     *
259     * We even record successes, and errors that we regard as
260     * "ignorable"; we can filter them out later.
261     */
262    void RecordTestResults(const ErrorCombination& errorCombination, const SkString& name,
263                           const char renderModeDescriptor []) {
264        // Things to do regardless of errorCombination.
265        fTestsRun++;
266        int renderModeCount = 0;
267        this->fRenderModesEncountered.find(renderModeDescriptor, &renderModeCount);
268        renderModeCount++;
269        this->fRenderModesEncountered.set(renderModeDescriptor, renderModeCount);
270
271        if (errorCombination.isEmpty()) {
272            return;
273        }
274
275        // Things to do only if there is some error condition.
276        SkString fullName = name;
277        fullName.append(renderModeDescriptor);
278        for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
279            ErrorType type = static_cast<ErrorType>(typeInt);
280            if (errorCombination.includes(type)) {
281                fFailedTests[type].push_back(fullName);
282            }
283        }
284    }
285
286    /**
287     * Return the number of significant (non-ignorable) errors we have
288     * encountered so far.
289     */
290    int NumSignificantErrors() {
291        int significantErrors = 0;
292        for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
293            ErrorType type = static_cast<ErrorType>(typeInt);
294            if (!fIgnorableErrorCombination.includes(type)) {
295                significantErrors += fFailedTests[type].count();
296            }
297        }
298        return significantErrors;
299    }
300
301    /**
302     * Display the summary of results with this ErrorType.
303     *
304     * @param type which ErrorType
305     * @param verbose whether to be all verbose about it
306     */
307    void DisplayResultTypeSummary(ErrorType type, bool verbose) {
308        bool isIgnorableType = fIgnorableErrorCombination.includes(type);
309
310        SkString line;
311        if (isIgnorableType) {
312            line.append("[ ] ");
313        } else {
314            line.append("[*] ");
315        }
316
317        SkTArray<SkString> *failedTestsOfThisType = &fFailedTests[type];
318        int count = failedTestsOfThisType->count();
319        line.appendf("%d %s", count, getErrorTypeName(type));
320        if (!isIgnorableType || verbose) {
321            line.append(":");
322            for (int i = 0; i < count; ++i) {
323                line.append(" ");
324                line.append((*failedTestsOfThisType)[i]);
325            }
326        }
327        gm_fprintf(stdout, "%s\n", line.c_str());
328    }
329
330    /**
331     * List contents of fFailedTests to stdout.
332     *
333     * @param verbose whether to be all verbose about it
334     */
335    void ListErrors(bool verbose) {
336        // First, print a single summary line.
337        SkString summary;
338        summary.appendf("Ran %d tests:", fTestsRun);
339        for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
340            ErrorType type = static_cast<ErrorType>(typeInt);
341            summary.appendf(" %s=%d", getErrorTypeName(type), fFailedTests[type].count());
342        }
343        gm_fprintf(stdout, "%s\n", summary.c_str());
344
345        // Now, for each failure type, list the tests that failed that way.
346        for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
347            this->DisplayResultTypeSummary(static_cast<ErrorType>(typeInt), verbose);
348        }
349        gm_fprintf(stdout, "(results marked with [*] will cause nonzero return value)\n");
350    }
351
352    static bool write_document(const SkString& path,
353                               const SkDynamicMemoryWStream& document) {
354        SkFILEWStream stream(path.c_str());
355        SkAutoDataUnref data(document.copyToData());
356        return stream.writeData(data.get());
357    }
358
359    /**
360     * Prepare an SkBitmap to render a GM into.
361     *
362     * After you've rendered the GM into the SkBitmap, you must call
363     * complete_bitmap()!
364     *
365     * @todo thudson 22 April 2011 - could refactor this to take in
366     * a factory to generate the context, always call readPixels()
367     * (logically a noop for rasters, if wasted time), and thus collapse the
368     * GPU special case and also let this be used for SkPicture testing.
369     */
370    static void setup_bitmap(const ConfigData& gRec, SkISize& size,
371                             SkBitmap* bitmap) {
372        bitmap->setConfig(gRec.fConfig, size.width(), size.height());
373        bitmap->allocPixels();
374        bitmap->eraseColor(SK_ColorTRANSPARENT);
375    }
376
377    /**
378     * Any finalization steps we need to perform on the SkBitmap after
379     * we have rendered the GM into it.
380     *
381     * It's too bad that we are throwing away alpha channel data
382     * we could otherwise be examining, but this had always been happening
383     * before... it was buried within the compare() method at
384     * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
385     *
386     * Apparently we need this, at least for bitmaps that are either:
387     * (a) destined to be written out as PNG files, or
388     * (b) compared against bitmaps read in from PNG files
389     * for the reasons described just above the force_all_opaque() method.
390     *
391     * Neglecting to do this led to the difficult-to-diagnose
392     * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
393     * spurious pixel_error messages as of r7258')
394     *
395     * TODO(epoger): Come up with a better solution that allows us to
396     * compare full pixel data, including alpha channel, while still being
397     * robust in the face of transformations to/from PNG files.
398     * Options include:
399     *
400     * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
401     *    will be written to, or compared against, PNG files.
402     *    PRO: Preserve/compare alpha channel info for the non-PNG cases
403     *         (comparing different renderModes in-memory)
404     *    CON: The bitmaps (and checksums) for these non-PNG cases would be
405     *         different than those for the PNG-compared cases, and in the
406     *         case of a failed renderMode comparison, how would we write the
407     *         image to disk for examination?
408     *
409     * 2. Always compute image checksums from PNG format (either
410     *    directly from the the bytes of a PNG file, or capturing the
411     *    bytes we would have written to disk if we were writing the
412     *    bitmap out as a PNG).
413     *    PRO: I think this would allow us to never force opaque, and to
414     *         the extent that alpha channel data can be preserved in a PNG
415     *         file, we could observe it.
416     *    CON: If we read a bitmap from disk, we need to take its checksum
417     *         from the source PNG (we can't compute it from the bitmap we
418     *         read out of the PNG, because we will have already premultiplied
419     *         the alpha).
420     *    CON: Seems wasteful to convert a bitmap to PNG format just to take
421     *         its checksum. (Although we're wasting lots of effort already
422     *         calling force_all_opaque().)
423     *
424     * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
425     *    so we can transform images back and forth without fear of off-by-one
426     *    errors.
427     *    CON: Math is hard.
428     *
429     * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
430     *    channel), rather than demanding absolute equality.
431     *    CON: Can't do this with checksums.
432     */
433    static void complete_bitmap(SkBitmap* bitmap) {
434        force_all_opaque(*bitmap);
435    }
436
437    static void installFilter(SkCanvas* canvas);
438
439    static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
440        SkAutoCanvasRestore acr(canvas, true);
441
442        if (!isPDF) {
443            canvas->concat(gm->getInitialTransform());
444        }
445        installFilter(canvas);
446        gm->setCanvasIsDeferred(isDeferred);
447        gm->draw(canvas);
448        canvas->setDrawFilter(NULL);
449    }
450
451    static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
452                                           GrSurface* gpuTarget,
453                                           SkBitmap* bitmap,
454                                           bool deferred) {
455        SkISize size (gm->getISize());
456        setup_bitmap(gRec, size, bitmap);
457
458        SkAutoTUnref<SkCanvas> canvas;
459
460        if (gRec.fBackend == kRaster_Backend) {
461            SkAutoTUnref<SkDevice> device(new SkDevice(*bitmap));
462            if (deferred) {
463                canvas.reset(new SkDeferredCanvas(device));
464            } else {
465                canvas.reset(new SkCanvas(device));
466            }
467            invokeGM(gm, canvas, false, deferred);
468            canvas->flush();
469        }
470#if SK_SUPPORT_GPU
471        else {  // GPU
472            SkAutoTUnref<SkDevice> device(SkGpuDevice::Create(gpuTarget));
473            if (deferred) {
474                canvas.reset(new SkDeferredCanvas(device));
475            } else {
476                canvas.reset(new SkCanvas(device));
477            }
478            invokeGM(gm, canvas, false, deferred);
479            // the device is as large as the current rendertarget, so
480            // we explicitly only readback the amount we expect (in
481            // size) overwrite our previous allocation
482            bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth,
483                              size.fHeight);
484            canvas->readPixels(bitmap, 0, 0);
485        }
486#endif
487        complete_bitmap(bitmap);
488        return kEmpty_ErrorCombination;
489    }
490
491    static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
492                                            SkPicture* pict, SkBitmap* bitmap,
493                                            SkScalar scale = SK_Scalar1,
494                                            bool tile = false) {
495        SkISize size = gm->getISize();
496        setup_bitmap(gRec, size, bitmap);
497
498        if (tile) {
499            // Generate the result image by rendering to tiles and accumulating
500            // the results in 'bitmap'
501
502            // This 16x16 tiling matches the settings applied to 'pict' in
503            // 'generate_new_picture'
504            SkISize tileSize = SkISize::Make(16, 16);
505
506            SkBitmap tileBM;
507            setup_bitmap(gRec, tileSize, &tileBM);
508            SkCanvas tileCanvas(tileBM);
509            installFilter(&tileCanvas);
510
511            SkCanvas bmpCanvas(*bitmap);
512            SkPaint bmpPaint;
513            bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
514
515            for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
516                for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
517                    int saveCount = tileCanvas.save();
518                    SkMatrix mat(tileCanvas.getTotalMatrix());
519                    mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
520                                      SkIntToScalar(-yTile*tileSize.height()));
521                    tileCanvas.setMatrix(mat);
522                    pict->draw(&tileCanvas);
523                    tileCanvas.flush();
524                    tileCanvas.restoreToCount(saveCount);
525                    bmpCanvas.drawBitmap(tileBM,
526                                         SkIntToScalar(xTile * tileSize.width()),
527                                         SkIntToScalar(yTile * tileSize.height()),
528                                         &bmpPaint);
529                }
530            }
531        } else {
532            SkCanvas canvas(*bitmap);
533            installFilter(&canvas);
534            canvas.scale(scale, scale);
535            canvas.drawPicture(*pict);
536            complete_bitmap(bitmap);
537        }
538    }
539
540    static void generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
541#ifdef SK_SUPPORT_PDF
542        SkMatrix initialTransform = gm->getInitialTransform();
543        SkISize pageSize = gm->getISize();
544        SkPDFDevice* dev = NULL;
545        if (initialTransform.isIdentity()) {
546            dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
547        } else {
548            SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
549                                            SkIntToScalar(pageSize.height()));
550            initialTransform.mapRect(&content);
551            content.intersect(0, 0, SkIntToScalar(pageSize.width()),
552                              SkIntToScalar(pageSize.height()));
553            SkISize contentSize =
554                SkISize::Make(SkScalarRoundToInt(content.width()),
555                              SkScalarRoundToInt(content.height()));
556            dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
557        }
558        SkAutoUnref aur(dev);
559
560        SkCanvas c(dev);
561        invokeGM(gm, &c, true, false);
562
563        SkPDFDocument doc;
564        doc.appendPage(dev);
565        doc.emitPDF(&pdf);
566#endif
567    }
568
569    static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
570#ifdef SK_SUPPORT_XPS
571        SkISize size = gm->getISize();
572
573        SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
574                                       SkIntToScalar(size.height()));
575        static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
576        static const SkScalar upm = 72 * inchesPerMeter;
577        SkVector unitsPerMeter = SkPoint::Make(upm, upm);
578        static const SkScalar ppm = 200 * inchesPerMeter;
579        SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
580
581        SkXPSDevice* dev = new SkXPSDevice();
582        SkAutoUnref aur(dev);
583
584        SkCanvas c(dev);
585        dev->beginPortfolio(&xps);
586        dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
587        invokeGM(gm, &c, false, false);
588        dev->endSheet();
589        dev->endPortfolio();
590
591#endif
592    }
593
594    ErrorCombination write_reference_image(const ConfigData& gRec, const char writePath [],
595                                           const char renderModeDescriptor [], const SkString& name,
596                                           SkBitmap& bitmap, SkDynamicMemoryWStream* document) {
597        SkString path;
598        bool success = false;
599        if (gRec.fBackend == kRaster_Backend ||
600            gRec.fBackend == kGPU_Backend ||
601            (gRec.fBackend == kPDF_Backend && CAN_IMAGE_PDF)) {
602
603            path = make_filename(writePath, renderModeDescriptor, name.c_str(),
604                                 "png");
605            success = write_bitmap(path, bitmap);
606        }
607        if (kPDF_Backend == gRec.fBackend) {
608            path = make_filename(writePath, renderModeDescriptor, name.c_str(),
609                                 "pdf");
610            success = write_document(path, *document);
611        }
612        if (kXPS_Backend == gRec.fBackend) {
613            path = make_filename(writePath, renderModeDescriptor, name.c_str(),
614                                 "xps");
615            success = write_document(path, *document);
616        }
617        if (success) {
618            return kEmpty_ErrorCombination;
619        } else {
620            gm_fprintf(stderr, "FAILED to write %s\n", path.c_str());
621            ErrorCombination errors(kWritingReferenceImage_ErrorType);
622            // TODO(epoger): Don't call RecordTestResults() here...
623            // Instead, we should make sure to call RecordTestResults
624            // exactly ONCE per test.  (Otherwise, gmmain.fTestsRun
625            // will be incremented twice for this test: once in
626            // compare_test_results_to_stored_expectations() before
627            // that method calls this one, and again here.)
628            //
629            // When we make that change, we should probably add a
630            // WritingReferenceImage test to the gm self-tests.)
631            RecordTestResults(errors, name, renderModeDescriptor);
632            return errors;
633        }
634    }
635
636    /**
637     * Log more detail about the mistmatch between expectedBitmap and
638     * actualBitmap.
639     */
640    void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
641                             const char *testName) {
642        const int expectedWidth = expectedBitmap.width();
643        const int expectedHeight = expectedBitmap.height();
644        const int width = actualBitmap.width();
645        const int height = actualBitmap.height();
646        if ((expectedWidth != width) || (expectedHeight != height)) {
647            gm_fprintf(stderr, "---- %s: dimension mismatch --"
648                       " expected [%d %d], actual [%d %d]\n",
649                       testName, expectedWidth, expectedHeight, width, height);
650            return;
651        }
652
653        if ((SkBitmap::kARGB_8888_Config != expectedBitmap.config()) ||
654            (SkBitmap::kARGB_8888_Config != actualBitmap.config())) {
655            gm_fprintf(stderr, "---- %s: not computing max per-channel"
656                       " pixel mismatch because non-8888\n", testName);
657            return;
658        }
659
660        SkAutoLockPixels alp0(expectedBitmap);
661        SkAutoLockPixels alp1(actualBitmap);
662        int errR = 0;
663        int errG = 0;
664        int errB = 0;
665        int errA = 0;
666        int differingPixels = 0;
667
668        for (int y = 0; y < height; ++y) {
669            const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
670            const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
671            for (int x = 0; x < width; ++x) {
672                SkPMColor expectedPixel = *expectedPixelPtr++;
673                SkPMColor actualPixel = *actualPixelPtr++;
674                if (expectedPixel != actualPixel) {
675                    differingPixels++;
676                    errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
677                                                 (int)SkGetPackedR32(actualPixel)));
678                    errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
679                                                 (int)SkGetPackedG32(actualPixel)));
680                    errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
681                                                 (int)SkGetPackedB32(actualPixel)));
682                    errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
683                                                 (int)SkGetPackedA32(actualPixel)));
684                }
685            }
686        }
687        gm_fprintf(stderr, "---- %s: %d (of %d) differing pixels,"
688                   " max per-channel mismatch R=%d G=%d B=%d A=%d\n",
689                   testName, differingPixels, width*height, errR, errG, errB, errA);
690    }
691
692    /**
693     * Compares actual checksum to expectations, returning the set of errors
694     * (if any) that we saw along the way.
695     *
696     * If fMismatchPath has been set, and there are pixel diffs, then the
697     * actual bitmap will be written out to a file within fMismatchPath.
698     *
699     * @param expectations what expectations to compare actualBitmap against
700     * @param actualBitmap the image we actually generated
701     * @param baseNameString name of test without renderModeDescriptor added
702     * @param renderModeDescriptor e.g., "-rtree", "-deferred"
703     * @param addToJsonSummary whether to add these results (both actual and
704     *        expected) to the JSON summary. Regardless of this setting, if
705     *        we find an image mismatch in this test, we will write these
706     *        results to the JSON summary.  (This is so that we will always
707     *        report errors across rendering modes, such as pipe vs tiled.
708     *        See https://codereview.chromium.org/13650002/ )
709     */
710    ErrorCombination compare_to_expectations(Expectations expectations,
711                                             const SkBitmap& actualBitmap,
712                                             const SkString& baseNameString,
713                                             const char renderModeDescriptor[],
714                                             bool addToJsonSummary) {
715        ErrorCombination errors;
716        Checksum actualChecksum = SkBitmapChecksummer::Compute64(actualBitmap);
717        SkString completeNameString = baseNameString;
718        completeNameString.append(renderModeDescriptor);
719        const char* completeName = completeNameString.c_str();
720
721        if (expectations.empty()) {
722            errors.add(kMissingExpectations_ErrorType);
723        } else if (!expectations.match(actualChecksum)) {
724            addToJsonSummary = true;
725            // The error mode we record depends on whether this was running
726            // in a non-standard renderMode.
727            if ('\0' == *renderModeDescriptor) {
728                errors.add(kExpectationsMismatch_ErrorType);
729            } else {
730                errors.add(kRenderModeMismatch_ErrorType);
731            }
732
733            // Write out the "actuals" for any mismatches, if we have
734            // been directed to do so.
735            if (fMismatchPath) {
736                SkString path =
737                    make_filename(fMismatchPath, renderModeDescriptor,
738                                  baseNameString.c_str(), "png");
739                write_bitmap(path, actualBitmap);
740            }
741
742            // If we have access to a single expected bitmap, log more
743            // detail about the mismatch.
744            const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
745            if (NULL != expectedBitmapPtr) {
746                report_bitmap_diffs(*expectedBitmapPtr, actualBitmap, completeName);
747            }
748        }
749        RecordTestResults(errors, baseNameString, renderModeDescriptor);
750
751        if (addToJsonSummary) {
752            add_actual_results_to_json_summary(completeName, actualChecksum, errors,
753                                               expectations.ignoreFailure());
754            add_expected_results_to_json_summary(completeName, expectations);
755        }
756
757        return errors;
758    }
759
760    /**
761     * Add this result to the appropriate JSON collection of actual results,
762     * depending on status.
763     */
764    void add_actual_results_to_json_summary(const char testName[],
765                                            Checksum actualChecksum,
766                                            ErrorCombination result,
767                                            bool ignoreFailure) {
768        Json::Value actualResults;
769        actualResults[kJsonKey_ActualResults_AnyStatus_Checksum] =
770            asJsonValue(actualChecksum);
771        if (result.isEmpty()) {
772            this->fJsonActualResults_Succeeded[testName] = actualResults;
773        } else {
774            if (ignoreFailure) {
775                // TODO: Once we have added the ability to compare
776                // actual results against expectations in a JSON file
777                // (where we can set ignore-failure to either true or
778                // false), add test cases that exercise ignored
779                // failures (both for kMissingExpectations_ErrorType
780                // and kExpectationsMismatch_ErrorType).
781                this->fJsonActualResults_FailureIgnored[testName] =
782                    actualResults;
783            } else {
784                if (result.includes(kMissingExpectations_ErrorType)) {
785                    // TODO: What about the case where there IS an
786                    // expected image checksum, but that gm test
787                    // doesn't actually run?  For now, those cases
788                    // will always be ignored, because gm only looks
789                    // at expectations that correspond to gm tests
790                    // that were actually run.
791                    //
792                    // Once we have the ability to express
793                    // expectations as a JSON file, we should fix this
794                    // (and add a test case for which an expectation
795                    // is given but the test is never run).
796                    this->fJsonActualResults_NoComparison[testName] =
797                        actualResults;
798                }
799                if (result.includes(kExpectationsMismatch_ErrorType) ||
800                    result.includes(kRenderModeMismatch_ErrorType)) {
801                    this->fJsonActualResults_Failed[testName] = actualResults;
802                }
803            }
804        }
805    }
806
807    /**
808     * Add this test to the JSON collection of expected results.
809     */
810    void add_expected_results_to_json_summary(const char testName[],
811                                              Expectations expectations) {
812        // For now, we assume that this collection starts out empty and we
813        // just fill it in as we go; once gm accepts a JSON file as input,
814        // we'll have to change that.
815        Json::Value expectedResults;
816        expectedResults[kJsonKey_ExpectedResults_Checksums] =
817            expectations.allowedChecksumsAsJson();
818        expectedResults[kJsonKey_ExpectedResults_IgnoreFailure] =
819            expectations.ignoreFailure();
820        this->fJsonExpectedResults[testName] = expectedResults;
821    }
822
823    /**
824     * Compare actualBitmap to expectations stored in this->fExpectationsSource.
825     *
826     * @param gm which test generated the actualBitmap
827     * @param gRec
828     * @param writePath unless this is NULL, write out actual images into this
829     *        directory
830     * @param actualBitmap bitmap generated by this run
831     * @param pdf
832     */
833    ErrorCombination compare_test_results_to_stored_expectations(
834        GM* gm, const ConfigData& gRec, const char writePath[],
835        SkBitmap& actualBitmap, SkDynamicMemoryWStream* pdf) {
836
837        SkString name = make_name(gm->shortName(), gRec.fName);
838        ErrorCombination errors;
839
840        ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
841        if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
842            /*
843             * Get the expected results for this test, as one or more allowed
844             * checksums. The current implementation of expectationsSource
845             * get this by computing the checksum of a single PNG file on disk.
846             *
847             * TODO(epoger): This relies on the fact that
848             * force_all_opaque() was called on the bitmap before it
849             * was written to disk as a PNG in the first place. If
850             * not, the checksum returned here may not match the
851             * checksum of actualBitmap, which *has* been run through
852             * force_all_opaque().
853             * See comments above complete_bitmap() for more detail.
854             */
855            Expectations expectations = expectationsSource->get(name.c_str());
856            errors.add(compare_to_expectations(expectations, actualBitmap,
857                                               name, "", true));
858        } else {
859            // If we are running without expectations, we still want to
860            // record the actual results.
861            Checksum actualChecksum =
862                SkBitmapChecksummer::Compute64(actualBitmap);
863            add_actual_results_to_json_summary(name.c_str(), actualChecksum,
864                                               ErrorCombination(kMissingExpectations_ErrorType),
865                                               false);
866            RecordTestResults(ErrorCombination(kMissingExpectations_ErrorType), name, "");
867        }
868
869        // TODO: Consider moving this into compare_to_expectations(),
870        // similar to fMismatchPath... for now, we don't do that, because
871        // we don't want to write out the actual bitmaps for all
872        // renderModes of all tests!  That would be a lot of files.
873        if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
874            errors.add(write_reference_image(gRec, writePath, "",
875                                             name, actualBitmap, pdf));
876        }
877
878        return errors;
879    }
880
881    /**
882     * Compare actualBitmap to referenceBitmap.
883     *
884     * @param baseNameString name of test without renderModeDescriptor added
885     * @param renderModeDescriptor
886     * @param actualBitmap actual bitmap generated by this run
887     * @param referenceBitmap bitmap we expected to be generated
888     */
889    ErrorCombination compare_test_results_to_reference_bitmap(
890        const SkString& baseNameString, const char renderModeDescriptor[],
891        SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
892
893        SkASSERT(referenceBitmap);
894        Expectations expectations(*referenceBitmap);
895        return compare_to_expectations(expectations, actualBitmap,
896                                       baseNameString, renderModeDescriptor, false);
897    }
898
899    static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
900                                           SkScalar scale = SK_Scalar1) {
901        // Pictures are refcounted so must be on heap
902        SkPicture* pict;
903        int width = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().width()), scale));
904        int height = SkScalarCeilToInt(SkScalarMul(SkIntToScalar(gm->getISize().height()), scale));
905
906        if (kTileGrid_BbhType == bbhType) {
907            SkTileGridPicture::TileGridInfo info;
908            info.fMargin.setEmpty();
909            info.fOffset.setZero();
910            info.fTileInterval.set(16, 16);
911            pict = new SkTileGridPicture(width, height, info);
912        } else {
913            pict = new SkPicture;
914        }
915        if (kNone_BbhType != bbhType) {
916            recordFlags |= SkPicture::kOptimizeForClippedPlayback_RecordingFlag;
917        }
918        SkCanvas* cv = pict->beginRecording(width, height, recordFlags);
919        cv->scale(scale, scale);
920        invokeGM(gm, cv, false, false);
921        pict->endRecording();
922
923        return pict;
924    }
925
926    static SkPicture* stream_to_new_picture(const SkPicture& src) {
927
928        // To do in-memory commiunications with a stream, we need to:
929        // * create a dynamic memory stream
930        // * copy it into a buffer
931        // * create a read stream from it
932        // ?!?!
933
934        SkDynamicMemoryWStream storage;
935        src.serialize(&storage);
936
937        size_t streamSize = storage.getOffset();
938        SkAutoMalloc dstStorage(streamSize);
939        void* dst = dstStorage.get();
940        //char* dst = new char [streamSize];
941        //@todo thudson 22 April 2011 when can we safely delete [] dst?
942        storage.copyTo(dst);
943        SkMemoryStream pictReadback(dst, streamSize);
944        SkPicture* retval = new SkPicture (&pictReadback);
945        return retval;
946    }
947
948    // Test: draw into a bitmap or pdf.
949    // Depending on flags, possibly compare to an expected image.
950    ErrorCombination test_drawing(GM* gm,
951                                  const ConfigData& gRec,
952                                  const char writePath [],
953                                  GrSurface* gpuTarget,
954                                  SkBitmap* bitmap) {
955        SkDynamicMemoryWStream document;
956
957        if (gRec.fBackend == kRaster_Backend ||
958            gRec.fBackend == kGPU_Backend) {
959            // Early exit if we can't generate the image.
960            ErrorCombination errors = generate_image(gm, gRec, gpuTarget, bitmap, false);
961            if (!errors.isEmpty()) {
962                // TODO: Add a test to exercise what the stdout and
963                // JSON look like if we get an "early error" while
964                // trying to generate the image.
965                return errors;
966            }
967        } else if (gRec.fBackend == kPDF_Backend) {
968            generate_pdf(gm, document);
969#if CAN_IMAGE_PDF
970            SkAutoDataUnref data(document.copyToData());
971            SkMemoryStream stream(data->data(), data->size());
972            SkPDFDocumentToBitmap(&stream, bitmap);
973#endif
974        } else if (gRec.fBackend == kXPS_Backend) {
975            generate_xps(gm, document);
976        }
977        return compare_test_results_to_stored_expectations(
978            gm, gRec, writePath, *bitmap, &document);
979    }
980
981    ErrorCombination test_deferred_drawing(GM* gm,
982                                           const ConfigData& gRec,
983                                           const SkBitmap& referenceBitmap,
984                                           GrSurface* gpuTarget) {
985        SkDynamicMemoryWStream document;
986
987        if (gRec.fBackend == kRaster_Backend ||
988            gRec.fBackend == kGPU_Backend) {
989            const char renderModeDescriptor[] = "-deferred";
990            SkBitmap bitmap;
991            // Early exit if we can't generate the image, but this is
992            // expected in some cases, so don't report a test failure.
993            ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
994            // TODO(epoger): This logic is the opposite of what is
995            // described above... if we succeeded in generating the
996            // -deferred image, we exit early!  We should fix this
997            // ASAP, because it is hiding -deferred errors... but for
998            // now, I'm leaving the logic as it is so that the
999            // refactoring change
1000            // https://codereview.chromium.org/12992003/ is unblocked.
1001            //
1002            // Filed as https://code.google.com/p/skia/issues/detail?id=1180
1003            // ('image-surface gm test is failing in "deferred" mode,
1004            // and gm is not reporting the failure')
1005            if (errors.isEmpty()) {
1006                // TODO(epoger): Report this as a new ErrorType,
1007                // something like kImageGeneration_ErrorType?
1008                return kEmpty_ErrorCombination;
1009            }
1010            const SkString name = make_name(gm->shortName(), gRec.fName);
1011            return compare_test_results_to_reference_bitmap(
1012                name, renderModeDescriptor, bitmap, &referenceBitmap);
1013        }
1014        return kEmpty_ErrorCombination;
1015    }
1016
1017    ErrorCombination test_pipe_playback(GM* gm, const ConfigData& gRec,
1018                                        const SkBitmap& referenceBitmap, bool simulateFailure) {
1019        const SkString name = make_name(gm->shortName(), gRec.fName);
1020        ErrorCombination errors;
1021        for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
1022            SkString renderModeDescriptor("-pipe");
1023            renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1024
1025            if (gm->getFlags() & GM::kSkipPipe_Flag) {
1026                RecordTestResults(kIntentionallySkipped_ErrorType, name,
1027                                  renderModeDescriptor.c_str());
1028                errors.add(kIntentionallySkipped_ErrorType);
1029            } else {
1030                SkBitmap bitmap;
1031                SkISize size = gm->getISize();
1032                setup_bitmap(gRec, size, &bitmap);
1033                SkCanvas canvas(bitmap);
1034                installFilter(&canvas);
1035                PipeController pipeController(&canvas);
1036                SkGPipeWriter writer;
1037                SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
1038                                                             gPipeWritingFlagCombos[i].flags,
1039                                                             size.width(), size.height());
1040                if (!simulateFailure) {
1041                    invokeGM(gm, pipeCanvas, false, false);
1042                }
1043                complete_bitmap(&bitmap);
1044                writer.endRecording();
1045                errors.add(compare_test_results_to_reference_bitmap(
1046                    name, renderModeDescriptor.c_str(), bitmap, &referenceBitmap));
1047                if (!errors.isEmpty()) {
1048                    break;
1049                }
1050            }
1051        }
1052        return errors;
1053    }
1054
1055    ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
1056                                              const SkBitmap& referenceBitmap) {
1057        const SkString name = make_name(gm->shortName(), gRec.fName);
1058        ErrorCombination errors;
1059        for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
1060            SkString renderModeDescriptor("-tiled pipe");
1061            renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1062
1063            if ((gm->getFlags() & GM::kSkipPipe_Flag) ||
1064                (gm->getFlags() & GM::kSkipTiled_Flag)) {
1065                RecordTestResults(kIntentionallySkipped_ErrorType, name,
1066                                  renderModeDescriptor.c_str());
1067                errors.add(kIntentionallySkipped_ErrorType);
1068            } else {
1069                SkBitmap bitmap;
1070                SkISize size = gm->getISize();
1071                setup_bitmap(gRec, size, &bitmap);
1072                SkCanvas canvas(bitmap);
1073                installFilter(&canvas);
1074                TiledPipeController pipeController(bitmap);
1075                SkGPipeWriter writer;
1076                SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
1077                                                             gPipeWritingFlagCombos[i].flags,
1078                                                             size.width(), size.height());
1079                invokeGM(gm, pipeCanvas, false, false);
1080                complete_bitmap(&bitmap);
1081                writer.endRecording();
1082                errors.add(compare_test_results_to_reference_bitmap(name,
1083                                                                    renderModeDescriptor.c_str(),
1084                                                                    bitmap, &referenceBitmap));
1085                if (!errors.isEmpty()) {
1086                    break;
1087                }
1088            }
1089        }
1090        return errors;
1091    }
1092
1093    //
1094    // member variables.
1095    // They are public for now, to allow easier setting by tool_main().
1096    //
1097
1098    bool fUseFileHierarchy;
1099    ErrorCombination fIgnorableErrorCombination;
1100
1101    const char* fMismatchPath;
1102
1103    // collection of tests that have failed with each ErrorType
1104    SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
1105    int fTestsRun;
1106    SkTDict<int> fRenderModesEncountered;
1107
1108    // Where to read expectations (expected image checksums, etc.) from.
1109    // If unset, we don't do comparisons.
1110    SkAutoTUnref<ExpectationsSource> fExpectationsSource;
1111
1112    // JSON summaries that we generate as we go (just for output).
1113    Json::Value fJsonExpectedResults;
1114    Json::Value fJsonActualResults_Failed;
1115    Json::Value fJsonActualResults_FailureIgnored;
1116    Json::Value fJsonActualResults_NoComparison;
1117    Json::Value fJsonActualResults_Succeeded;
1118
1119}; // end of GMMain class definition
1120
1121#if SK_SUPPORT_GPU
1122static const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1123#else
1124static const GLContextType kDontCare_GLContextType = 0;
1125#endif
1126
1127// If the platform does not support writing PNGs of PDFs then there will be no
1128// reference images to read. However, we can always write the .pdf files
1129static const ConfigFlags kPDFConfigFlags = CAN_IMAGE_PDF ? kRW_ConfigFlag :
1130                                                           kWrite_ConfigFlag;
1131
1132static const ConfigData gRec[] = {
1133    { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType,                  0, kRW_ConfigFlag,    "8888",         true },
1134#if 0   // stop testing this (for now at least) since we want to remove support for it (soon please!!!)
1135    { SkBitmap::kARGB_4444_Config, kRaster_Backend, kDontCare_GLContextType,                  0, kRW_ConfigFlag,    "4444",         true },
1136#endif
1137    { SkBitmap::kRGB_565_Config,   kRaster_Backend, kDontCare_GLContextType,                  0, kRW_ConfigFlag,    "565",          true },
1138#if SK_SUPPORT_GPU
1139    { SkBitmap::kARGB_8888_Config, kGPU_Backend,    GrContextFactory::kNative_GLContextType,  0, kRW_ConfigFlag,    "gpu",          true },
1140    { SkBitmap::kARGB_8888_Config, kGPU_Backend,    GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag,    "msaa16",       true },
1141    { SkBitmap::kARGB_8888_Config, kGPU_Backend,    GrContextFactory::kNative_GLContextType,  4, kRW_ConfigFlag,    "msaa4",        false},
1142    /* The debug context does not generate images */
1143    { SkBitmap::kARGB_8888_Config, kGPU_Backend,    GrContextFactory::kDebug_GLContextType,   0, kNone_ConfigFlag,  "gpudebug",     GR_DEBUG},
1144#if SK_ANGLE
1145    { SkBitmap::kARGB_8888_Config, kGPU_Backend,    GrContextFactory::kANGLE_GLContextType,   0, kRW_ConfigFlag,    "angle",        true },
1146    { SkBitmap::kARGB_8888_Config, kGPU_Backend,    GrContextFactory::kANGLE_GLContextType,  16, kRW_ConfigFlag,    "anglemsaa16",  true },
1147#endif // SK_ANGLE
1148#ifdef SK_MESA
1149    { SkBitmap::kARGB_8888_Config, kGPU_Backend,    GrContextFactory::kMESA_GLContextType,    0, kRW_ConfigFlag,    "mesa",         true },
1150#endif // SK_MESA
1151#endif // SK_SUPPORT_GPU
1152#ifdef SK_SUPPORT_XPS
1153    /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
1154    { SkBitmap::kARGB_8888_Config, kXPS_Backend,    kDontCare_GLContextType,                  0, kWrite_ConfigFlag, "xps",          true },
1155#endif // SK_SUPPORT_XPS
1156#ifdef SK_SUPPORT_PDF
1157    { SkBitmap::kARGB_8888_Config, kPDF_Backend,    kDontCare_GLContextType,                  0, kPDFConfigFlags,   "pdf",          true },
1158#endif // SK_SUPPORT_PDF
1159};
1160
1161static SkString configUsage() {
1162    SkString result;
1163    result.appendf("Space delimited list of which configs to run. Possible options: [");
1164    for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1165        if (i > 0) {
1166            result.append("|");
1167        }
1168        result.appendf("%s", gRec[i].fName);
1169    }
1170    result.append("]\n");
1171    result.appendf("The default value is: \"");
1172    for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1173        if (gRec[i].fRunByDefault) {
1174            if (i > 0) {
1175                result.append(" ");
1176            }
1177            result.appendf("%s", gRec[i].fName);
1178        }
1179    }
1180    result.appendf("\"");
1181
1182    return result;
1183}
1184
1185// Macro magic to convert a numeric preprocessor token into a string.
1186// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
1187// This should probably be moved into one of our common headers...
1188#define TOSTRING_INTERNAL(x) #x
1189#define TOSTRING(x) TOSTRING_INTERNAL(x)
1190
1191// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
1192DEFINE_string(config, "", configUsage().c_str());
1193DEFINE_bool(deferred, true, "Exercise the deferred rendering test pass.");
1194DEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
1195DEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
1196#if SK_SUPPORT_GPU
1197DEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
1198              "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
1199              "use the default. 0 for either disables the cache.");
1200#endif
1201DEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
1202            "when reading/writing files.");
1203DEFINE_string(match, "",  "Only run tests whose name includes this substring/these substrings "
1204              "(more than one can be supplied, separated by spaces).");
1205DEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
1206              "pixel mismatches into this directory.");
1207DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
1208              "testIndex %% divisor == remainder.");
1209DEFINE_bool(pdf, true, "Exercise the pdf rendering test pass.");
1210DEFINE_bool(pipe, true, "Exercise the SkGPipe replay test pass.");
1211DEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
1212               "any differences between those and the newly generated ones.");
1213DEFINE_bool(replay, true, "Exercise the SkPicture replay test pass.");
1214DEFINE_string2(resourcePath, i, "", "Directory that stores image resources.");
1215DEFINE_bool(rtree, true, "Exercise the R-Tree variant of SkPicture test pass.");
1216DEFINE_bool(serialize, true, "Exercise the SkPicture serialization & deserialization test pass.");
1217DEFINE_bool(simulatePipePlaybackFailure, false, "Simulate a rendering failure in pipe mode only.");
1218DEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
1219DEFINE_bool(tileGrid, true, "Exercise the tile grid variant of SkPicture.");
1220DEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
1221              "factors to be used for tileGrid playback testing. Default value: 1.0");
1222DEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
1223DEFINE_bool2(verbose, v, false, "Give more detail (e.g. list all GMs run, more info about "
1224             "each test).");
1225DEFINE_string2(writePath, w, "",  "Write rendered images into this directory.");
1226DEFINE_string2(writePicturePath, p, "", "Write .skp files into this directory.");
1227
1228static int findConfig(const char config[]) {
1229    for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
1230        if (!strcmp(config, gRec[i].fName)) {
1231            return (int) i;
1232        }
1233    }
1234    return -1;
1235}
1236
1237static bool skip_name(const SkTDArray<const char*> array, const char name[]) {
1238    if (0 == array.count()) {
1239        // no names, so don't skip anything
1240        return false;
1241    }
1242    for (int i = 0; i < array.count(); ++i) {
1243        if (strstr(name, array[i])) {
1244            // found the name, so don't skip
1245            return false;
1246        }
1247    }
1248    return true;
1249}
1250
1251namespace skiagm {
1252#if SK_SUPPORT_GPU
1253SkAutoTUnref<GrContext> gGrContext;
1254/**
1255 * Sets the global GrContext, accessible by individual GMs
1256 */
1257static void SetGr(GrContext* grContext) {
1258    SkSafeRef(grContext);
1259    gGrContext.reset(grContext);
1260}
1261
1262/**
1263 * Gets the global GrContext, can be called by GM tests.
1264 */
1265GrContext* GetGr();
1266GrContext* GetGr() {
1267    return gGrContext.get();
1268}
1269
1270/**
1271 * Sets the global GrContext and then resets it to its previous value at
1272 * destruction.
1273 */
1274class AutoResetGr : SkNoncopyable {
1275public:
1276    AutoResetGr() : fOld(NULL) {}
1277    void set(GrContext* context) {
1278        SkASSERT(NULL == fOld);
1279        fOld = GetGr();
1280        SkSafeRef(fOld);
1281        SetGr(context);
1282    }
1283    ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
1284private:
1285    GrContext* fOld;
1286};
1287#else
1288GrContext* GetGr();
1289GrContext* GetGr() { return NULL; }
1290#endif
1291}
1292
1293template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1294    int index = array->find(value);
1295    if (index < 0) {
1296        *array->append() = value;
1297    }
1298}
1299
1300/**
1301 * Run this test in a number of different configs (8888, 565, PDF,
1302 * etc.), confirming that the resulting bitmaps match expectations
1303 * (which may be different for each config).
1304 *
1305 * Returns all errors encountered while doing so.
1306 */
1307ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1308                                      GrContextFactory *grFactory);
1309ErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm, const SkTDArray<size_t> &configs,
1310                                      GrContextFactory *grFactory) {
1311    const char renderModeDescriptor[] = "";
1312    ErrorCombination errorsForAllConfigs;
1313    uint32_t gmFlags = gm->getFlags();
1314
1315    for (int i = 0; i < configs.count(); i++) {
1316        ConfigData config = gRec[configs[i]];
1317        const SkString name = gmmain.make_name(gm->shortName(), config.fName);
1318
1319        // Skip any tests that we don't even need to try.
1320        // If any of these were skipped on a per-GM basis, record them as
1321        // kIntentionallySkipped.
1322        if (kPDF_Backend == config.fBackend) {
1323            if (!FLAGS_pdf) {
1324                continue;
1325            }
1326            if (gmFlags & GM::kSkipPDF_Flag) {
1327                gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1328                                         renderModeDescriptor);
1329                errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1330                continue;
1331            }
1332        }
1333        if ((gmFlags & GM::kSkip565_Flag) &&
1334            (kRaster_Backend == config.fBackend) &&
1335            (SkBitmap::kRGB_565_Config == config.fConfig)) {
1336            gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1337                                     renderModeDescriptor);
1338            errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1339            continue;
1340        }
1341        if ((gmFlags & GM::kSkipGPU_Flag) &&
1342            kGPU_Backend == config.fBackend) {
1343            gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1344                                     renderModeDescriptor);
1345            errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
1346            continue;
1347        }
1348
1349        // Now we know that we want to run this test and record its
1350        // success or failure.
1351        ErrorCombination errorsForThisConfig;
1352        GrSurface* gpuTarget = NULL;
1353#if SK_SUPPORT_GPU
1354        SkAutoTUnref<GrSurface> auGpuTarget;
1355        AutoResetGr autogr;
1356        if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
1357            GrContext* gr = grFactory->get(config.fGLContextType);
1358            bool grSuccess = false;
1359            if (gr) {
1360                // create a render target to back the device
1361                GrTextureDesc desc;
1362                desc.fConfig = kSkia8888_GrPixelConfig;
1363                desc.fFlags = kRenderTarget_GrTextureFlagBit;
1364                desc.fWidth = gm->getISize().width();
1365                desc.fHeight = gm->getISize().height();
1366                desc.fSampleCnt = config.fSampleCnt;
1367                auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
1368                if (NULL != auGpuTarget) {
1369                    gpuTarget = auGpuTarget;
1370                    grSuccess = true;
1371                    autogr.set(gr);
1372                    // Set the user specified cache limits if non-default.
1373                    size_t bytes;
1374                    int count;
1375                    gr->getTextureCacheLimits(&count, &bytes);
1376                    if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
1377                        bytes = static_cast<size_t>(gGpuCacheSizeBytes);
1378                    }
1379                    if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
1380                        count = gGpuCacheSizeCount;
1381                    }
1382                    gr->setTextureCacheLimits(count, bytes);
1383                }
1384            }
1385            if (!grSuccess) {
1386                errorsForThisConfig.add(kNoGpuContext_ErrorType);
1387            }
1388        }
1389#endif
1390
1391        SkBitmap comparisonBitmap;
1392
1393        const char* writePath;
1394        if (FLAGS_writePath.count() == 1) {
1395            writePath = FLAGS_writePath[0];
1396        } else {
1397            writePath = NULL;
1398        }
1399        if (errorsForThisConfig.isEmpty()) {
1400            errorsForThisConfig.add(gmmain.test_drawing(gm,config, writePath, gpuTarget,
1401                                                        &comparisonBitmap));
1402        }
1403
1404        if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
1405            (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
1406            errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
1407                                                                 gpuTarget));
1408        }
1409
1410        errorsForAllConfigs.add(errorsForThisConfig);
1411    }
1412    return errorsForAllConfigs;
1413}
1414
1415/**
1416 * Run this test in a number of different drawing modes (pipe,
1417 * deferred, tiled, etc.), confirming that the resulting bitmaps all
1418 * *exactly* match comparisonBitmap.
1419 *
1420 * Returns all errors encountered while doing so.
1421 */
1422ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1423                                    const SkBitmap &comparisonBitmap,
1424                                    const SkTDArray<SkScalar> &tileGridReplayScales);
1425ErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
1426                                    const SkBitmap &comparisonBitmap,
1427                                    const SkTDArray<SkScalar> &tileGridReplayScales) {
1428    ErrorCombination errorsForAllModes;
1429    uint32_t gmFlags = gm->getFlags();
1430    const SkString name = gmmain.make_name(gm->shortName(), compareConfig.fName);
1431
1432    SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1433    SkAutoUnref aur(pict);
1434    if (FLAGS_replay) {
1435        const char renderModeDescriptor[] = "-replay";
1436        if (gmFlags & GM::kSkipPicture_Flag) {
1437            gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name, renderModeDescriptor);
1438            errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1439        } else {
1440            SkBitmap bitmap;
1441            gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
1442            errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1443                name, renderModeDescriptor, bitmap, &comparisonBitmap));
1444        }
1445    }
1446
1447    if (FLAGS_serialize) {
1448        const char renderModeDescriptor[] = "-serialize";
1449        if (gmFlags & GM::kSkipPicture_Flag) {
1450            gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name, renderModeDescriptor);
1451            errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1452        } else {
1453            SkPicture* repict = gmmain.stream_to_new_picture(*pict);
1454            SkAutoUnref aurr(repict);
1455            SkBitmap bitmap;
1456            gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
1457            errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1458                name, renderModeDescriptor, bitmap, &comparisonBitmap));
1459        }
1460    }
1461
1462    if ((1 == FLAGS_writePicturePath.count()) &&
1463        !(gmFlags & GM::kSkipPicture_Flag)) {
1464        const char* pictureSuffix = "skp";
1465        SkString path = make_filename(FLAGS_writePicturePath[0], "",
1466                                      gm->shortName(), pictureSuffix);
1467        SkFILEWStream stream(path.c_str());
1468        pict->serialize(&stream);
1469    }
1470
1471    if (FLAGS_rtree) {
1472        const char renderModeDescriptor[] = "-rtree";
1473        if (gmFlags & GM::kSkipPicture_Flag) {
1474            gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name, renderModeDescriptor);
1475            errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1476        } else {
1477            SkPicture* pict = gmmain.generate_new_picture(
1478                gm, kRTree_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag);
1479            SkAutoUnref aur(pict);
1480            SkBitmap bitmap;
1481            gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
1482            errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1483                name, renderModeDescriptor, bitmap, &comparisonBitmap));
1484        }
1485    }
1486
1487    if (FLAGS_tileGrid) {
1488        for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1489            SkScalar replayScale = tileGridReplayScales[scaleIndex];
1490            SkString renderModeDescriptor("-tilegrid");
1491            if (SK_Scalar1 != replayScale) {
1492                renderModeDescriptor += "-scale-";
1493                renderModeDescriptor.appendScalar(replayScale);
1494            }
1495
1496            if ((gmFlags & GM::kSkipPicture_Flag) ||
1497                ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1)) {
1498                gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, name,
1499                                         renderModeDescriptor.c_str());
1500                errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1501            } else {
1502                // We record with the reciprocal scale to obtain a replay
1503                // result that can be validated against comparisonBitmap.
1504                SkScalar recordScale = SkScalarInvert(replayScale);
1505                SkPicture* pict = gmmain.generate_new_picture(
1506                    gm, kTileGrid_BbhType, SkPicture::kUsePathBoundsForClip_RecordingFlag,
1507                    recordScale);
1508                SkAutoUnref aur(pict);
1509                SkBitmap bitmap;
1510                // We cannot yet pass 'true' to generate_image_from_picture to
1511                // perform actual tiled rendering (see Issue 1198 -
1512                // https://code.google.com/p/skia/issues/detail?id=1198)
1513                gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
1514                                                   replayScale /*, true */);
1515                errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1516                    name, renderModeDescriptor.c_str(), bitmap, &comparisonBitmap));
1517            }
1518        }
1519    }
1520
1521    // run the pipe centric GM steps
1522    if (FLAGS_pipe) {
1523        errorsForAllModes.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap,
1524                                                        FLAGS_simulatePipePlaybackFailure));
1525        if (FLAGS_tiledPipe) {
1526            errorsForAllModes.add(gmmain.test_tiled_pipe_playback(gm, compareConfig,
1527                                                                  comparisonBitmap));
1528        }
1529    }
1530    return errorsForAllModes;
1531}
1532
1533/**
1534 * Return a list of all entries in an array of strings as a single string
1535 * of this form:
1536 * "item1", "item2", "item3"
1537 */
1538SkString list_all(const SkTArray<SkString> &stringArray);
1539SkString list_all(const SkTArray<SkString> &stringArray) {
1540    SkString total;
1541    for (int i = 0; i < stringArray.count(); i++) {
1542        if (i > 0) {
1543            total.append(", ");
1544        }
1545        total.append("\"");
1546        total.append(stringArray[i]);
1547        total.append("\"");
1548    }
1549    return total;
1550}
1551
1552/**
1553 * Return a list of configuration names, as a single string of this form:
1554 * "item1", "item2", "item3"
1555 *
1556 * @param configs configurations, as a list of indices into gRec
1557 */
1558SkString list_all_config_names(const SkTDArray<size_t> &configs);
1559SkString list_all_config_names(const SkTDArray<size_t> &configs) {
1560    SkString total;
1561    for (int i = 0; i < configs.count(); i++) {
1562        if (i > 0) {
1563            total.append(", ");
1564        }
1565        total.append("\"");
1566        total.append(gRec[configs[i]].fName);
1567        total.append("\"");
1568    }
1569    return total;
1570}
1571
1572int tool_main(int argc, char** argv);
1573int tool_main(int argc, char** argv) {
1574
1575#if SK_ENABLE_INST_COUNT
1576    gPrintInstCount = true;
1577#endif
1578
1579    SkGraphics::Init();
1580    // we don't need to see this during a run
1581    gSkSuppressFontCachePurgeSpew = true;
1582
1583    setSystemPreferences();
1584    GMMain gmmain;
1585
1586    SkTDArray<size_t> configs;
1587    SkTDArray<size_t> excludeConfigs;
1588    bool userConfig = false;
1589
1590    SkString usage;
1591    usage.printf("Run the golden master tests.\n");
1592    SkCommandLineFlags::SetUsage(usage.c_str());
1593    SkCommandLineFlags::Parse(argc, argv);
1594
1595    gmmain.fUseFileHierarchy = FLAGS_hierarchy;
1596    if (FLAGS_mismatchPath.count() == 1) {
1597        gmmain.fMismatchPath = FLAGS_mismatchPath[0];
1598    }
1599
1600    for (int i = 0; i < FLAGS_config.count(); i++) {
1601        int index = findConfig(FLAGS_config[i]);
1602        if (index >= 0) {
1603            appendUnique<size_t>(&configs, index);
1604            userConfig = true;
1605        } else {
1606            gm_fprintf(stderr, "unrecognized config %s\n", FLAGS_config[i]);
1607            return -1;
1608        }
1609    }
1610
1611    for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
1612        int index = findConfig(FLAGS_excludeConfig[i]);
1613        if (index >= 0) {
1614            *excludeConfigs.append() = index;
1615        } else {
1616            gm_fprintf(stderr, "unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1617            return -1;
1618        }
1619    }
1620
1621    int moduloRemainder = -1;
1622    int moduloDivisor = -1;
1623
1624    if (FLAGS_modulo.count() == 2) {
1625        moduloRemainder = atoi(FLAGS_modulo[0]);
1626        moduloDivisor = atoi(FLAGS_modulo[1]);
1627        if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) {
1628            gm_fprintf(stderr, "invalid modulo values.");
1629            return -1;
1630        }
1631    }
1632
1633#if SK_SUPPORT_GPU
1634    if (FLAGS_gpuCacheSize.count() > 0) {
1635        if (FLAGS_gpuCacheSize.count() != 2) {
1636            gm_fprintf(stderr, "--gpuCacheSize requires two arguments\n");
1637            return -1;
1638        }
1639        gGpuCacheSizeBytes = atoi(FLAGS_gpuCacheSize[0]);
1640        gGpuCacheSizeCount = atoi(FLAGS_gpuCacheSize[1]);
1641    } else {
1642        gGpuCacheSizeBytes = DEFAULT_CACHE_VALUE;
1643        gGpuCacheSizeCount = DEFAULT_CACHE_VALUE;
1644    }
1645#endif
1646
1647    SkTDArray<SkScalar> tileGridReplayScales;
1648    *tileGridReplayScales.append() = SK_Scalar1; // By default only test at scale 1.0
1649    if (FLAGS_tileGridReplayScales.count() > 0) {
1650        tileGridReplayScales.reset();
1651        for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
1652            double val = atof(FLAGS_tileGridReplayScales[i]);
1653            if (0 < val) {
1654                *tileGridReplayScales.append() = SkDoubleToScalar(val);
1655            }
1656        }
1657        if (0 == tileGridReplayScales.count()) {
1658            // Should have at least one scale
1659            gm_fprintf(stderr, "--tileGridReplayScales requires at least one scale.\n");
1660            return -1;
1661        }
1662    }
1663
1664    if (!userConfig) {
1665        // if no config is specified by user, add the defaults
1666        for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1667            if (gRec[i].fRunByDefault) {
1668                *configs.append() = i;
1669            }
1670        }
1671    }
1672    // now remove any explicitly excluded configs
1673    for (int i = 0; i < excludeConfigs.count(); ++i) {
1674        int index = configs.find(excludeConfigs[i]);
1675        if (index >= 0) {
1676            configs.remove(index);
1677            // now assert that there was only one copy in configs[]
1678            SkASSERT(configs.find(excludeConfigs[i]) < 0);
1679        }
1680    }
1681
1682#if SK_SUPPORT_GPU
1683    GrContextFactory* grFactory = new GrContextFactory;
1684    for (int i = 0; i < configs.count(); ++i) {
1685        size_t index = configs[i];
1686        if (kGPU_Backend == gRec[index].fBackend) {
1687            GrContext* ctx = grFactory->get(gRec[index].fGLContextType);
1688            if (NULL == ctx) {
1689                gm_fprintf(stderr, "GrContext could not be created for config %s."
1690                           " Config will be skipped.\n", gRec[index].fName);
1691                configs.remove(i);
1692                --i;
1693            }
1694            if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
1695                gm_fprintf(stderr, "Sample count (%d) of config %s is not supported."
1696                           " Config will be skipped.\n", gRec[index].fSampleCnt, gRec[index].fName);
1697                configs.remove(i);
1698                --i;
1699            }
1700        }
1701    }
1702#else
1703    GrContextFactory* grFactory = NULL;
1704#endif
1705
1706    if (FLAGS_resourcePath.count() == 1) {
1707        GM::SetResourcePath(FLAGS_resourcePath[0]);
1708    }
1709
1710    if (FLAGS_readPath.count() == 1) {
1711        const char* readPath = FLAGS_readPath[0];
1712        if (!sk_exists(readPath)) {
1713            gm_fprintf(stderr, "readPath %s does not exist!\n", readPath);
1714            return -1;
1715        }
1716        if (sk_isdir(readPath)) {
1717            if (FLAGS_verbose) {
1718                gm_fprintf(stdout, "reading from %s\n", readPath);
1719            }
1720            gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1721                IndividualImageExpectationsSource, (readPath)));
1722        } else {
1723            if (FLAGS_verbose) {
1724                gm_fprintf(stdout, "reading expectations from JSON summary file %s\n", readPath);
1725            }
1726            gmmain.fExpectationsSource.reset(SkNEW_ARGS(
1727                JsonExpectationsSource, (readPath)));
1728        }
1729    }
1730    if (FLAGS_verbose) {
1731        if (FLAGS_writePath.count() == 1) {
1732            gm_fprintf(stdout, "writing to %s\n", FLAGS_writePath[0]);
1733        }
1734        if (FLAGS_writePicturePath.count() == 1) {
1735            gm_fprintf(stdout, "writing pictures to %s\n", FLAGS_writePicturePath[0]);
1736        }
1737        if (FLAGS_resourcePath.count() == 1) {
1738            gm_fprintf(stdout, "reading resources from %s\n", FLAGS_resourcePath[0]);
1739        }
1740    }
1741
1742    if (moduloDivisor <= 0) {
1743        moduloRemainder = -1;
1744    }
1745    if (moduloRemainder < 0 || moduloRemainder >= moduloDivisor) {
1746        moduloRemainder = -1;
1747    }
1748
1749    int gmsRun = 0;
1750    int gmIndex = -1;
1751    SkString moduloStr;
1752
1753    // If we will be writing out files, prepare subdirectories.
1754    if (FLAGS_writePath.count() == 1) {
1755        if (!sk_mkdir(FLAGS_writePath[0])) {
1756            return -1;
1757        }
1758        if (gmmain.fUseFileHierarchy) {
1759            for (int i = 0; i < configs.count(); i++) {
1760                ConfigData config = gRec[configs[i]];
1761                SkString subdir;
1762                subdir.appendf("%s%c%s", FLAGS_writePath[0], SkPATH_SEPARATOR,
1763                               config.fName);
1764                if (!sk_mkdir(subdir.c_str())) {
1765                    return -1;
1766                }
1767            }
1768        }
1769    }
1770
1771    Iter iter;
1772    GM* gm;
1773    while ((gm = iter.next()) != NULL) {
1774
1775        ++gmIndex;
1776        if (moduloRemainder >= 0) {
1777            if ((gmIndex % moduloDivisor) != moduloRemainder) {
1778                continue;
1779            }
1780            moduloStr.printf("[%d.%d] ", gmIndex, moduloDivisor);
1781        }
1782
1783        const char* shortName = gm->shortName();
1784        if (skip_name(FLAGS_match, shortName)) {
1785            SkDELETE(gm);
1786            continue;
1787        }
1788
1789        gmsRun++;
1790        SkISize size = gm->getISize();
1791        if (FLAGS_verbose) {
1792            gm_fprintf(stdout, "%sdrawing... %s [%d %d]\n", moduloStr.c_str(), shortName,
1793                       size.width(), size.height());
1794        }
1795
1796        run_multiple_configs(gmmain, gm, configs, grFactory);
1797
1798        SkBitmap comparisonBitmap;
1799        const ConfigData compareConfig =
1800            { SkBitmap::kARGB_8888_Config, kRaster_Backend, kDontCare_GLContextType, 0, kRW_ConfigFlag, "comparison", false };
1801        gmmain.generate_image(gm, compareConfig, NULL, &comparisonBitmap, false);
1802
1803        // TODO(epoger): only run this if gmmain.generate_image() succeeded?
1804        // Otherwise, what are we comparing against?
1805        run_multiple_modes(gmmain, gm, compareConfig, comparisonBitmap, tileGridReplayScales);
1806
1807        SkDELETE(gm);
1808    }
1809
1810    SkTArray<SkString> modes;
1811    gmmain.GetRenderModesEncountered(modes);
1812    bool reportError = false;
1813    if (gmmain.NumSignificantErrors() > 0) {
1814        reportError = true;
1815    }
1816    int expectedNumberOfTests = gmsRun * (configs.count() + modes.count());
1817
1818    // Output summary to stdout.
1819    if (FLAGS_verbose) {
1820        gm_fprintf(stdout, "Ran %d GMs\n", gmsRun);
1821        gm_fprintf(stdout, "... over %2d configs [%s]\n", configs.count(),
1822                   list_all_config_names(configs).c_str());
1823        gm_fprintf(stdout, "...  and %2d modes   [%s]\n", modes.count(), list_all(modes).c_str());
1824        gm_fprintf(stdout, "... so there should be a total of %d tests.\n", expectedNumberOfTests);
1825    }
1826    gmmain.ListErrors(FLAGS_verbose);
1827
1828    // TODO(epoger): Enable this check for Android, too, once we resolve
1829    // https://code.google.com/p/skia/issues/detail?id=1222
1830    // ('GM is unexpectedly skipping tests on Android')
1831#ifndef SK_BUILD_FOR_ANDROID
1832    if (expectedNumberOfTests != gmmain.fTestsRun) {
1833        gm_fprintf(stderr, "expected %d tests, but ran or skipped %d tests\n",
1834                   expectedNumberOfTests, gmmain.fTestsRun);
1835        reportError = true;
1836    }
1837#endif
1838
1839    if (FLAGS_writeJsonSummaryPath.count() == 1) {
1840        Json::Value actualResults;
1841        actualResults[kJsonKey_ActualResults_Failed] =
1842            gmmain.fJsonActualResults_Failed;
1843        actualResults[kJsonKey_ActualResults_FailureIgnored] =
1844            gmmain.fJsonActualResults_FailureIgnored;
1845        actualResults[kJsonKey_ActualResults_NoComparison] =
1846            gmmain.fJsonActualResults_NoComparison;
1847        actualResults[kJsonKey_ActualResults_Succeeded] =
1848            gmmain.fJsonActualResults_Succeeded;
1849        Json::Value root;
1850        root[kJsonKey_ActualResults] = actualResults;
1851        root[kJsonKey_ExpectedResults] = gmmain.fJsonExpectedResults;
1852        std::string jsonStdString = root.toStyledString();
1853        SkFILEWStream stream(FLAGS_writeJsonSummaryPath[0]);
1854        stream.write(jsonStdString.c_str(), jsonStdString.length());
1855    }
1856
1857#if SK_SUPPORT_GPU
1858
1859#if GR_CACHE_STATS
1860    for (int i = 0; i < configs.count(); i++) {
1861        ConfigData config = gRec[configs[i]];
1862
1863        if (FLAGS_verbose && (kGPU_Backend == config.fBackend)) {
1864            GrContext* gr = grFactory->get(config.fGLContextType);
1865
1866            gm_fprintf(stdout, "config: %s %x\n", config.fName, gr);
1867            gr->printCacheStats();
1868        }
1869    }
1870#endif
1871
1872    delete grFactory;
1873#endif
1874    SkGraphics::Term();
1875
1876    return (reportError) ? -1 : 0;
1877}
1878
1879void GMMain::installFilter(SkCanvas* canvas) {
1880    if (FLAGS_forceBWtext) {
1881        canvas->setDrawFilter(SkNEW(BWTextDrawFilter))->unref();
1882    }
1883}
1884
1885#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
1886int main(int argc, char * const argv[]) {
1887    return tool_main(argc, (char**) argv);
1888}
1889#endif
1890