1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
7971d0c8049c6bfc7a58f0b41f8f59f9ec9ca077bbsalomon@google.com
857f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com/*
957f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com * Code for the "gm" (Golden Master) rendering comparison tool.
1057f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com *
1157f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com * If you make changes to this, re-run the self-tests at gm/tests/run.sh
1257f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com * to make sure they still pass... you may need to change the expected
1357f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com * results of the self-test.
1457f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com */
1557f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com
16b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "gm.h"
176f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com#include "gm_error.h"
1837269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com#include "gm_expectations.h"
197bc13a62609149f0b535c2f3ff7210eb834d8b36epoger@google.com#include "system_preferences.h"
2030e6e2af14e84216b1c113fd7500d0822bc81daamtklein#include "CrashHandler.h"
21afb4379dbca4d4d3824ace183a7348d24bc1589fmtklein#include "ProcStats.h"
22bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "Resources.h"
23bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "SamplePipeControllers.h"
245f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com#include "SkBitmap.h"
25b9b9a18ab459c2616ac4a52c9f8cc0637d284229reed@android.com#include "SkColorPriv.h"
26d9ba9a05d6f5766fdb1378b6ed84c0659009a8dascroggo@google.com#include "SkCommandLineFlags.h"
278a85d0c4938173476d037d7af0ee3b9436a1234ereed@google.com#include "SkData.h"
284370aedf7f55af74e9ebb4ad1c2e010c08236dfajunov@google.com#include "SkDeferredCanvas.h"
29971d0c8049c6bfc7a58f0b41f8f59f9ec9ca077bbsalomon@google.com#include "SkDevice.h"
305e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org#include "SkDocument.h"
31de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com#include "SkDrawFilter.h"
32c41295d1223136da4f9e4c65b7145360684ede4bcommit-bot@chromium.org#include "SkForceLinking.h"
335af9b2032b552516c9223d9fb22185b022b13c62scroggo@google.com#include "SkGPipe.h"
348015dd83ae37147bb630d4751030868051ad0caereed@android.com#include "SkGraphics.h"
358015dd83ae37147bb630d4751030868051ad0caereed@android.com#include "SkImageDecoder.h"
368015dd83ae37147bb630d4751030868051ad0caereed@android.com#include "SkImageEncoder.h"
37e3bb3bce3e9c1f3bc8ee779b1b3383c18e560bcecommit-bot@chromium.org#include "SkJSONCPP.h"
38e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com#include "SkOSFile.h"
390dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org#include "SkPDFRasterizer.h"
409875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com#include "SkPicture.h"
41770963f23f4fc313db0fa3bac18b1b8aafb55f17robertphillips@google.com#include "SkPictureRecorder.h"
42977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com#include "SkRefCnt.h"
435e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org#include "SkScalar.h"
4472c9672ce274a3b6cb40800d66374edf25b157a3scroggo@google.com#include "SkStream.h"
45f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com#include "SkString.h"
4611db6fa9b9fc30b1f504d9f53e0f6ae7149cc5c5reed@google.com#include "SkSurface.h"
472a48c3adb7cf4fc754f99a41352210b4a99edf04bsalomon@google.com#include "SkTArray.h"
48310478e72c63e639373465216271b81f1e4a9136epoger@google.com#include "SkTDict.h"
490770044da6d61dcbc8d9673fed8dd92460faa314reed@google.com
50515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#ifdef SK_DEBUG
51515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.orgstatic const bool kDebugOnly = true;
528065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org#define GR_DUMP_FONT_CACHE 0
53515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#else
54515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.orgstatic const bool kDebugOnly = false;
55515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org#endif
56515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org
57ed5eb4ef2aa1d6c705bc3ed466f9caba2a230a2bepoger@google.com__SK_FORCE_IMAGE_DECODER_LINKING;
58ed5eb4ef2aa1d6c705bc3ed466f9caba2a230a2bepoger@google.com
59cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
60cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#include "GrContextFactory.h"
61cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#include "SkGpuDevice.h"
62cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.comtypedef GrContextFactory::GLContextType GLContextType;
636f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com#define DEFAULT_CACHE_VALUE -1
646f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.comstatic int gGpuCacheSizeBytes;
656f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.comstatic int gGpuCacheSizeCount;
66cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#else
6780724dfeb320d1152128cd33636c9024952432d3epoger@google.comclass GrContextFactory;
68cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.comclass GrContext;
69123ac1d4eab757052407064623643fdc59f85363bsalomon@google.comclass GrSurface;
70cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.comtypedef int GLContextType;
7180549fcdd50269d7e069d6db02b395fca128056ckkinnunentypedef int GrGLStandard;
72cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
73cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com
7476c913db20de7ae1baa49ae66e943bf7f40781ccepoger@google.com#define DEBUGFAIL_SEE_STDERR SkDEBUGFAIL("see stderr for message")
7576c913db20de7ae1baa49ae66e943bf7f40781ccepoger@google.com
765e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.orgDECLARE_bool(useDocumentInsteadOfDevice);
778923c6cfd580ac9accb11b909fa2a033d69553aareed@google.com
780770044da6d61dcbc8d9673fed8dd92460faa314reed@google.com#ifdef SK_SUPPORT_PDF
799875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com    #include "SkPDFDevice.h"
809875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com    #include "SkPDFDocument.h"
810770044da6d61dcbc8d9673fed8dd92460faa314reed@google.com#endif
8200dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com
83e3cc2eb88fef9b2123c6ea2ed813ce53b6385926epoger@google.com// Until we resolve http://code.google.com/p/skia/issues/detail?id=455 ,
84e3cc2eb88fef9b2123c6ea2ed813ce53b6385926epoger@google.com// stop writing out XPS-format image baselines in gm.
85e3cc2eb88fef9b2123c6ea2ed813ce53b6385926epoger@google.com#undef SK_SUPPORT_XPS
86b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#ifdef SK_SUPPORT_XPS
87b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    #include "SkXPSDevice.h"
88b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#endif
89b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
9046cce91f4859b9c229938d4d649870c0a43b1806reed@google.com#ifdef SK_BUILD_FOR_MAC
9146cce91f4859b9c229938d4d649870c0a43b1806reed@google.com    #include "SkCGUtils.h"
9246cce91f4859b9c229938d4d649870c0a43b1806reed@google.com#endif
9346cce91f4859b9c229938d4d649870c0a43b1806reed@google.com
9400dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.comusing namespace skiagm;
9500dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com
9600dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.comclass Iter {
9700dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.compublic:
9800dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com    Iter() {
993914958a49ee089ddeb04acc16373aae8bc2eaf7bsalomon@google.com        this->reset();
1003914958a49ee089ddeb04acc16373aae8bc2eaf7bsalomon@google.com    }
1013914958a49ee089ddeb04acc16373aae8bc2eaf7bsalomon@google.com
1023914958a49ee089ddeb04acc16373aae8bc2eaf7bsalomon@google.com    void reset() {
103dd0ac281e920b01a63789893cc3e7422789658ddreed@android.com        fReg = GMRegistry::Head();
10400dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com    }
105d4dfd10bb6f9bf3ac6e1ebc9bc3ae22c6d06321freed@google.com
106dd0ac281e920b01a63789893cc3e7422789658ddreed@android.com    GM* next() {
10700dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com        if (fReg) {
108dd0ac281e920b01a63789893cc3e7422789658ddreed@android.com            GMRegistry::Factory fact = fReg->factory();
10900dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com            fReg = fReg->next();
110dd0ac281e920b01a63789893cc3e7422789658ddreed@android.com            return fact(0);
11100dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com        }
11200dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com        return NULL;
11300dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com    }
114d4dfd10bb6f9bf3ac6e1ebc9bc3ae22c6d06321freed@google.com
11500dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com    static int Count() {
116dd0ac281e920b01a63789893cc3e7422789658ddreed@android.com        const GMRegistry* reg = GMRegistry::Head();
11700dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com        int count = 0;
11800dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com        while (reg) {
11900dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com            count += 1;
12000dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com            reg = reg->next();
12100dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com        }
12200dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com        return count;
12300dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com    }
124d4dfd10bb6f9bf3ac6e1ebc9bc3ae22c6d06321freed@google.com
12500dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.comprivate:
12600dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com    const GMRegistry* fReg;
12700dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com};
12800dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com
129ce057fec926f8ec1d60ed4722d1e51a0086e7976epoger@google.com// TODO(epoger): Right now, various places in this code assume that all the
130ce057fec926f8ec1d60ed4722d1e51a0086e7976epoger@google.com// image files read/written by GM use this file extension.
131ce057fec926f8ec1d60ed4722d1e51a0086e7976epoger@google.com// Search for references to this constant to find these assumptions.
132ce057fec926f8ec1d60ed4722d1e51a0086e7976epoger@google.comconst static char kPNG_FileExtension[] = "png";
133ce057fec926f8ec1d60ed4722d1e51a0086e7976epoger@google.com
134686abdfab0e4c45de1fd30774896c46e43a299acvandebo@chromium.orgenum Backend {
1353cb834bd27a16cc60ff30adae96659558c2dc91fjunov@chromium.org    kRaster_Backend,
1363cb834bd27a16cc60ff30adae96659558c2dc91fjunov@chromium.org    kGPU_Backend,
1373cb834bd27a16cc60ff30adae96659558c2dc91fjunov@chromium.org    kPDF_Backend,
1383cb834bd27a16cc60ff30adae96659558c2dc91fjunov@chromium.org    kXPS_Backend,
1393cb834bd27a16cc60ff30adae96659558c2dc91fjunov@chromium.org};
1403cb834bd27a16cc60ff30adae96659558c2dc91fjunov@chromium.org
1413cb834bd27a16cc60ff30adae96659558c2dc91fjunov@chromium.orgenum BbhType {
1423cb834bd27a16cc60ff30adae96659558c2dc91fjunov@chromium.org    kNone_BbhType,
1433cb834bd27a16cc60ff30adae96659558c2dc91fjunov@chromium.org    kRTree_BbhType,
1443cb834bd27a16cc60ff30adae96659558c2dc91fjunov@chromium.org    kTileGrid_BbhType,
145686abdfab0e4c45de1fd30774896c46e43a299acvandebo@chromium.org};
146686abdfab0e4c45de1fd30774896c46e43a299acvandebo@chromium.org
1477361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.comenum ConfigFlags {
1487361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com    kNone_ConfigFlag  = 0x0,
1497361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com    /* Write GM images if a write path is provided. */
1507361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com    kWrite_ConfigFlag = 0x1,
151f28dd8ab109663a6fe67fd4ee3d66248e0dac686epoger@google.com    /* Read reference GM images if a read path is provided. */
1527361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com    kRead_ConfigFlag  = 0x2,
1537361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com    kRW_ConfigFlag    = (kWrite_ConfigFlag | kRead_ConfigFlag),
1547361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com};
1557361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com
1569875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.comstruct ConfigData {
157ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    SkColorType                     fColorType;
1587361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com    Backend                         fBackend;
159cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com    GLContextType                   fGLContextType; // GPU backend only
1607361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com    int                             fSampleCnt;     // GPU backend only
1617361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com    ConfigFlags                     fFlags;
1627361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com    const char*                     fName;
1634c75f241cb75f202cfa51569ba8dc4749a28a9f7bsalomon@google.com    bool                            fRunByDefault;
1649875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com};
1659875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com
1660dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.orgstruct PDFRasterizerData {
1670dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    bool        (*fRasterizerFunction)(SkStream*, SkBitmap*);
1680dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    const char* fName;
1690dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    bool        fRunByDefault;
1700dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org};
1710dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
17210afbefa5b60b0f7e8d2b02f4c996de88aa26830mike@reedtribe.orgclass BWTextDrawFilter : public SkDrawFilter {
17310afbefa5b60b0f7e8d2b02f4c996de88aa26830mike@reedtribe.orgpublic:
174971aca75572ed6e0c5e1cc959173dc58ca7b6b8dreed@google.com    virtual bool filter(SkPaint*, Type) SK_OVERRIDE;
17510afbefa5b60b0f7e8d2b02f4c996de88aa26830mike@reedtribe.org};
176971aca75572ed6e0c5e1cc959173dc58ca7b6b8dreed@google.combool BWTextDrawFilter::filter(SkPaint* p, Type t) {
17710afbefa5b60b0f7e8d2b02f4c996de88aa26830mike@reedtribe.org    if (kText_Type == t) {
17810afbefa5b60b0f7e8d2b02f4c996de88aa26830mike@reedtribe.org        p->setAntiAlias(false);
17910afbefa5b60b0f7e8d2b02f4c996de88aa26830mike@reedtribe.org    }
180971aca75572ed6e0c5e1cc959173dc58ca7b6b8dreed@google.com    return true;
18110afbefa5b60b0f7e8d2b02f4c996de88aa26830mike@reedtribe.org}
18210afbefa5b60b0f7e8d2b02f4c996de88aa26830mike@reedtribe.org
183de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.comstruct PipeFlagComboData {
184de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    const char* name;
185de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    uint32_t flags;
186de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com};
187de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
188de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.comstatic PipeFlagComboData gPipeWritingFlagCombos[] = {
189de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    { "", 0 },
190de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    { " cross-process", SkGPipeWriter::kCrossProcess_Flag },
191de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    { " cross-process, shared address", SkGPipeWriter::kCrossProcess_Flag
192de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        | SkGPipeWriter::kSharedAddressSpace_Flag }
193de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com};
194de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
195608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.orgstatic SkData* encode_to_dct_data(size_t* pixelRefOffset, const SkBitmap& bitmap);
19673a7ea3ae0d37ee28f90d6b38c49fda052638253edisonn@google.comDECLARE_int32(pdfRasterDpi);
197d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com
1985079d2c2a0dc0deacecccc6ef9d1c45c6c7982e4epoger@google.comconst static ErrorCombination kDefaultIgnorableErrorTypes = ErrorCombination()
1995079d2c2a0dc0deacecccc6ef9d1c45c6c7982e4epoger@google.com    .plus(kMissingExpectations_ErrorType)
2005079d2c2a0dc0deacecccc6ef9d1c45c6c7982e4epoger@google.com    .plus(kIntentionallySkipped_ErrorType);
2015079d2c2a0dc0deacecccc6ef9d1c45c6c7982e4epoger@google.com
202de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.comclass GMMain {
203de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.compublic:
2046f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com    GMMain() : fUseFileHierarchy(false), fWriteChecksumBasedFilenames(false),
2056f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com               fIgnorableErrorTypes(kDefaultIgnorableErrorTypes),
2065f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com               fMismatchPath(NULL), fMissingExpectationsPath(NULL), fTestsRun(0),
2075f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com               fRenderModesEncountered(1) {}
208e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com
209659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com    /**
210659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     * Assemble shortNamePlusConfig from (surprise!) shortName and configName.
211659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     *
212659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     * The method for doing so depends on whether we are using hierarchical naming.
213659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     * For example, shortName "selftest1" and configName "8888" could be assembled into
214659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     * either "selftest1_8888" or "8888/selftest1".
215659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     */
216659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com    SkString make_shortname_plus_config(const char *shortName, const char *configName) {
217e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com        SkString name;
21857f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com        if (0 == strlen(configName)) {
21957f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com            name.append(shortName);
22057f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com        } else if (fUseFileHierarchy) {
221e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com            name.appendf("%s%c%s", configName, SkPATH_SEPARATOR, shortName);
222e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com        } else {
223e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com            name.appendf("%s_%s", shortName, configName);
224e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com        }
225de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        return name;
22610afbefa5b60b0f7e8d2b02f4c996de88aa26830mike@reedtribe.org    }
22710afbefa5b60b0f7e8d2b02f4c996de88aa26830mike@reedtribe.org
228659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com    /**
229659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     * Assemble filename, suitable for writing out the results of a particular test.
230659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     */
231659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com    SkString make_filename(const char *path,
232659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                           const char *shortName,
233659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                           const char *configName,
234659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                           const char *renderModeDescriptor,
235659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                           const char *suffix) {
236659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        SkString filename = make_shortname_plus_config(shortName, configName);
237659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        filename.append(renderModeDescriptor);
238659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        filename.appendUnichar('.');
239659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        filename.append(suffix);
240a8e2e1504b9af6ba791637f228debaa23953064atfarina        return SkOSPath::Join(path, filename.c_str());
241659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com    }
242659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com
2436f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com    /**
2446f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com     * Assemble filename suitable for writing out an SkBitmap.
2456f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com     */
2466f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com    SkString make_bitmap_filename(const char *path,
2476f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                                  const char *shortName,
2486f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                                  const char *configName,
2496f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                                  const char *renderModeDescriptor,
2506f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                                  const GmResultDigest &bitmapDigest) {
2516f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com        if (fWriteChecksumBasedFilenames) {
2526f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com            SkString filename;
2536f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com            filename.append(bitmapDigest.getHashType());
2546f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com            filename.appendUnichar('_');
2556f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com            filename.append(shortName);
2566f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com            filename.appendUnichar('_');
2576f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com            filename.append(bitmapDigest.getDigestValue());
2586f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com            filename.appendUnichar('.');
2596f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com            filename.append(kPNG_FileExtension);
260a8e2e1504b9af6ba791637f228debaa23953064atfarina            return SkOSPath::Join(path, filename.c_str());
2616f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com        } else {
2626f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com            return make_filename(path, shortName, configName, renderModeDescriptor,
2636f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                                 kPNG_FileExtension);
2646f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com        }
2656f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com    }
2666f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com
267de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    /* since PNG insists on unpremultiplying our alpha, we take no
268de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com       precision chances and force all pixels to be 100% opaque,
269de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com       otherwise on compare we may not get a perfect match.
270de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    */
271de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    static void force_all_opaque(const SkBitmap& bitmap) {
272dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org        SkColorType colorType = bitmap.colorType();
273dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org        switch (colorType) {
27428fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.org        case kN32_SkColorType:
2755f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com            force_all_opaque_8888(bitmap);
2765f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com            break;
277dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org        case kRGB_565_SkColorType:
2785f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com            // nothing to do here; 565 bitmaps are inherently opaque
2795f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com            break;
2805f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com        default:
2812b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org            SkDebugf("unsupported bitmap colorType %d\n", colorType);
2825efdd0cb9cdf4c2017dc6ad70b2503bf04234d9bepoger@google.com            DEBUGFAIL_SEE_STDERR;
2835f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com        }
2845f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com    }
2855f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com
2865f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com    static void force_all_opaque_8888(const SkBitmap& bitmap) {
287de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkAutoLockPixels lock(bitmap);
288de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        for (int y = 0; y < bitmap.height(); y++) {
289de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            for (int x = 0; x < bitmap.width(); x++) {
290de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com                *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
291de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            }
292de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        }
29379d3cb4ba74651ceaa862005a4244460e4662afdvandebo@chromium.org    }
29410afbefa5b60b0f7e8d2b02f4c996de88aa26830mike@reedtribe.org
2950dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    static ErrorCombination write_bitmap(const SkString& path, const SkBitmap& bitmap) {
2965f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com        // TODO(epoger): Now that we have removed force_all_opaque()
2975f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com        // from this method, we should be able to get rid of the
2985f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com        // transformation to 8888 format also.
299de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkBitmap copy;
30028fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.org        bitmap.copyTo(&copy, kN32_SkColorType);
3010dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        if (!SkImageEncoder::EncodeFile(path.c_str(), copy,
3020dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                                        SkImageEncoder::kPNG_Type,
3030dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                                        100)) {
3042b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org            SkDebugf("FAILED to write bitmap: %s\n", path.c_str());
3050dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            return ErrorCombination(kWritingReferenceImage_ErrorType);
3060dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        }
3070dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        return kEmpty_ErrorCombination;
308de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    }
3099875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com
3106f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com    /**
311310478e72c63e639373465216271b81f1e4a9136epoger@google.com     * Add all render modes encountered thus far to the "modes" array.
3126f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com     */
313310478e72c63e639373465216271b81f1e4a9136epoger@google.com    void GetRenderModesEncountered(SkTArray<SkString> &modes) {
314310478e72c63e639373465216271b81f1e4a9136epoger@google.com        SkTDict<int>::Iter iter(this->fRenderModesEncountered);
315310478e72c63e639373465216271b81f1e4a9136epoger@google.com        const char* mode;
316310478e72c63e639373465216271b81f1e4a9136epoger@google.com        while ((mode = iter.next(NULL)) != NULL) {
317310478e72c63e639373465216271b81f1e4a9136epoger@google.com            SkString modeAsString = SkString(mode);
318310478e72c63e639373465216271b81f1e4a9136epoger@google.com            // TODO(epoger): It seems a bit silly that all of these modes were
319310478e72c63e639373465216271b81f1e4a9136epoger@google.com            // recorded with a leading "-" which we have to remove here
320310478e72c63e639373465216271b81f1e4a9136epoger@google.com            // (except for mode "", which means plain old original mode).
321310478e72c63e639373465216271b81f1e4a9136epoger@google.com            // But that's how renderModeDescriptor has been passed into
322310478e72c63e639373465216271b81f1e4a9136epoger@google.com            // compare_test_results_to_reference_bitmap() historically,
323310478e72c63e639373465216271b81f1e4a9136epoger@google.com            // and changing that now may affect other parts of our code.
324310478e72c63e639373465216271b81f1e4a9136epoger@google.com            if (modeAsString.startsWith("-")) {
325310478e72c63e639373465216271b81f1e4a9136epoger@google.com                modeAsString.remove(0, 1);
326310478e72c63e639373465216271b81f1e4a9136epoger@google.com            }
32785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            modes.push_back(modeAsString);
328310478e72c63e639373465216271b81f1e4a9136epoger@google.com        }
329310478e72c63e639373465216271b81f1e4a9136epoger@google.com    }
330310478e72c63e639373465216271b81f1e4a9136epoger@google.com
331310478e72c63e639373465216271b81f1e4a9136epoger@google.com    /**
3323a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com     * Returns true if failures on this test should be ignored.
3333a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com     */
3343e62ebf93b43139202ce803245411467c7ed13ffcommit-bot@chromium.org    bool ShouldIgnoreTest(const char *name) const {
3353e62ebf93b43139202ce803245411467c7ed13ffcommit-bot@chromium.org        for (int i = 0; i < fIgnorableTestNames.count(); i++) {
3363e62ebf93b43139202ce803245411467c7ed13ffcommit-bot@chromium.org            if (fIgnorableTestNames[i].equals(name)) {
3373a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com                return true;
3383a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com            }
3393a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com        }
3403a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com        return false;
3413a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com    }
3423a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com
3433a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com    /**
34485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org     * Calls RecordTestResults to record that we skipped a test.
34585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org     *
34685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org     * Depending on the backend, this may mean that we skipped a single rendermode, or all
34785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org     * rendermodes; see http://skbug.com/1994 and https://codereview.chromium.org/129203002/
34885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org     */
34985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org    void RecordSkippedTest(const SkString& shortNamePlusConfig,
35085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                           const char renderModeDescriptor [], Backend backend) {
35185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        if (kRaster_Backend == backend) {
35285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            // Skipping a test on kRaster_Backend means that we will skip ALL renderModes
35385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            // (as opposed to other backends, on which we only run the default renderMode).
35485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            //
35585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            // We cannot call RecordTestResults yet, because we won't know the full set of
35685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            // renderModes until we have run all tests.
35785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            fTestsSkippedOnAllRenderModes.push_back(shortNamePlusConfig);
35885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        } else {
35985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            this->RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
36085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                    renderModeDescriptor);
36185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        }
36285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org    }
36385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
36485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org    /**
365310478e72c63e639373465216271b81f1e4a9136epoger@google.com     * Records the results of this test in fTestsRun and fFailedTests.
366310478e72c63e639373465216271b81f1e4a9136epoger@google.com     *
367310478e72c63e639373465216271b81f1e4a9136epoger@google.com     * We even record successes, and errors that we regard as
368310478e72c63e639373465216271b81f1e4a9136epoger@google.com     * "ignorable"; we can filter them out later.
369310478e72c63e639373465216271b81f1e4a9136epoger@google.com     */
370659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com    void RecordTestResults(const ErrorCombination& errorCombination,
371659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                           const SkString& shortNamePlusConfig,
372310478e72c63e639373465216271b81f1e4a9136epoger@google.com                           const char renderModeDescriptor []) {
373310478e72c63e639373465216271b81f1e4a9136epoger@google.com        // Things to do regardless of errorCombination.
374310478e72c63e639373465216271b81f1e4a9136epoger@google.com        fTestsRun++;
375310478e72c63e639373465216271b81f1e4a9136epoger@google.com        int renderModeCount = 0;
376310478e72c63e639373465216271b81f1e4a9136epoger@google.com        this->fRenderModesEncountered.find(renderModeDescriptor, &renderModeCount);
377310478e72c63e639373465216271b81f1e4a9136epoger@google.com        renderModeCount++;
378310478e72c63e639373465216271b81f1e4a9136epoger@google.com        this->fRenderModesEncountered.set(renderModeDescriptor, renderModeCount);
379310478e72c63e639373465216271b81f1e4a9136epoger@google.com
3806f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com        if (errorCombination.isEmpty()) {
38115655b241bfe1d77032e88115f119684722c10e0epoger@google.com            return;
382eb06636133b6b5971964e5bce5ce5ca71d9ea960epoger@google.com        }
383eb06636133b6b5971964e5bce5ce5ca71d9ea960epoger@google.com
384310478e72c63e639373465216271b81f1e4a9136epoger@google.com        // Things to do only if there is some error condition.
385659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        SkString fullName = shortNamePlusConfig;
386caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com        fullName.append(renderModeDescriptor);
387310478e72c63e639373465216271b81f1e4a9136epoger@google.com        for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
388310478e72c63e639373465216271b81f1e4a9136epoger@google.com            ErrorType type = static_cast<ErrorType>(typeInt);
389310478e72c63e639373465216271b81f1e4a9136epoger@google.com            if (errorCombination.includes(type)) {
390310478e72c63e639373465216271b81f1e4a9136epoger@google.com                fFailedTests[type].push_back(fullName);
391310478e72c63e639373465216271b81f1e4a9136epoger@google.com            }
39257f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com        }
393310478e72c63e639373465216271b81f1e4a9136epoger@google.com    }
39415655b241bfe1d77032e88115f119684722c10e0epoger@google.com
395310478e72c63e639373465216271b81f1e4a9136epoger@google.com    /**
396310478e72c63e639373465216271b81f1e4a9136epoger@google.com     * Return the number of significant (non-ignorable) errors we have
397310478e72c63e639373465216271b81f1e4a9136epoger@google.com     * encountered so far.
398310478e72c63e639373465216271b81f1e4a9136epoger@google.com     */
399310478e72c63e639373465216271b81f1e4a9136epoger@google.com    int NumSignificantErrors() {
400310478e72c63e639373465216271b81f1e4a9136epoger@google.com        int significantErrors = 0;
401310478e72c63e639373465216271b81f1e4a9136epoger@google.com        for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
402310478e72c63e639373465216271b81f1e4a9136epoger@google.com            ErrorType type = static_cast<ErrorType>(typeInt);
4035079d2c2a0dc0deacecccc6ef9d1c45c6c7982e4epoger@google.com            if (!fIgnorableErrorTypes.includes(type)) {
404310478e72c63e639373465216271b81f1e4a9136epoger@google.com                significantErrors += fFailedTests[type].count();
405310478e72c63e639373465216271b81f1e4a9136epoger@google.com            }
406310478e72c63e639373465216271b81f1e4a9136epoger@google.com        }
407310478e72c63e639373465216271b81f1e4a9136epoger@google.com        return significantErrors;
40857f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com    }
40957f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com
410310478e72c63e639373465216271b81f1e4a9136epoger@google.com    /**
41151dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com     * Display the summary of results with this ErrorType.
41251dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com     *
41351dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com     * @param type which ErrorType
41451dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com     * @param verbose whether to be all verbose about it
41551dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com     */
41651dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com    void DisplayResultTypeSummary(ErrorType type, bool verbose) {
4175079d2c2a0dc0deacecccc6ef9d1c45c6c7982e4epoger@google.com        bool isIgnorableType = fIgnorableErrorTypes.includes(type);
41851dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com
41951dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com        SkString line;
42051dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com        if (isIgnorableType) {
42151dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com            line.append("[ ] ");
42251dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com        } else {
42351dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com            line.append("[*] ");
42451dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com        }
42551dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com
42651dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com        SkTArray<SkString> *failedTestsOfThisType = &fFailedTests[type];
42751dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com        int count = failedTestsOfThisType->count();
42851dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com        line.appendf("%d %s", count, getErrorTypeName(type));
42951dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com        if (!isIgnorableType || verbose) {
43051dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com            line.append(":");
43151dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com            for (int i = 0; i < count; ++i) {
43251dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com                line.append(" ");
43351dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com                line.append((*failedTestsOfThisType)[i]);
43451dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com            }
43551dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com        }
4362b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org        SkDebugf("%s\n", line.c_str());
43751dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com    }
43851dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com
43951dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com    /**
440310478e72c63e639373465216271b81f1e4a9136epoger@google.com     * List contents of fFailedTests to stdout.
44151dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com     *
44251dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com     * @param verbose whether to be all verbose about it
443310478e72c63e639373465216271b81f1e4a9136epoger@google.com     */
44451dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com    void ListErrors(bool verbose) {
445310478e72c63e639373465216271b81f1e4a9136epoger@google.com        // First, print a single summary line.
446310478e72c63e639373465216271b81f1e4a9136epoger@google.com        SkString summary;
447310478e72c63e639373465216271b81f1e4a9136epoger@google.com        summary.appendf("Ran %d tests:", fTestsRun);
448310478e72c63e639373465216271b81f1e4a9136epoger@google.com        for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
449310478e72c63e639373465216271b81f1e4a9136epoger@google.com            ErrorType type = static_cast<ErrorType>(typeInt);
450310478e72c63e639373465216271b81f1e4a9136epoger@google.com            summary.appendf(" %s=%d", getErrorTypeName(type), fFailedTests[type].count());
451310478e72c63e639373465216271b81f1e4a9136epoger@google.com        }
4522b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org        SkDebugf("%s\n", summary.c_str());
453310478e72c63e639373465216271b81f1e4a9136epoger@google.com
454310478e72c63e639373465216271b81f1e4a9136epoger@google.com        // Now, for each failure type, list the tests that failed that way.
455310478e72c63e639373465216271b81f1e4a9136epoger@google.com        for (int typeInt = 0; typeInt <= kLast_ErrorType; typeInt++) {
45651dbabee67ea1285e1115e535d26944d4da99be5epoger@google.com            this->DisplayResultTypeSummary(static_cast<ErrorType>(typeInt), verbose);
45757f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com        }
4582b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org        SkDebugf("(results marked with [*] will cause nonzero return value)\n");
45957f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com    }
46057f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com
4610dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    static ErrorCombination write_document(const SkString& path, SkStreamAsset* asset) {
462de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkFILEWStream stream(path.c_str());
4630dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        if (!stream.writeStream(asset, asset->getLength())) {
4642b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org            SkDebugf("FAILED to write document: %s\n", path.c_str());
4650dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            return ErrorCombination(kWritingReferenceImage_ErrorType);
4660dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        }
4670dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        return kEmpty_ErrorCombination;
468de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    }
4699875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com
4705f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com    /**
4715f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * Prepare an SkBitmap to render a GM into.
4725f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *
4735f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * After you've rendered the GM into the SkBitmap, you must call
4745f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * complete_bitmap()!
4755f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *
4765f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * @todo thudson 22 April 2011 - could refactor this to take in
4775f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * a factory to generate the context, always call readPixels()
4785f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * (logically a noop for rasters, if wasted time), and thus collapse the
4795f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * GPU special case and also let this be used for SkPicture testing.
4805f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     */
481de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    static void setup_bitmap(const ConfigData& gRec, SkISize& size,
482de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com                             SkBitmap* bitmap) {
483ddd014e65264e3f18b2d4305dd2fbc81549f5180reed        bitmap->allocPixels(SkImageInfo::Make(size.width(), size.height(),
484ddd014e65264e3f18b2d4305dd2fbc81549f5180reed                                              gRec.fColorType, kPremul_SkAlphaType));
485dbfac8a72393eaf01670aeb3244de0e18d8faf98junov@google.com        bitmap->eraseColor(SK_ColorTRANSPARENT);
48679d3cb4ba74651ceaa862005a4244460e4662afdvandebo@chromium.org    }
4879875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com
4885f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com    /**
4895f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * Any finalization steps we need to perform on the SkBitmap after
4905f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * we have rendered the GM into it.
4915f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *
4925f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * It's too bad that we are throwing away alpha channel data
4935f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * we could otherwise be examining, but this had always been happening
4945f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * before... it was buried within the compare() method at
4955f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * https://code.google.com/p/skia/source/browse/trunk/gm/gmmain.cpp?r=7289#305 .
4965f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *
4975f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * Apparently we need this, at least for bitmaps that are either:
4985f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * (a) destined to be written out as PNG files, or
4995f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * (b) compared against bitmaps read in from PNG files
5005f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * for the reasons described just above the force_all_opaque() method.
5015f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *
5025f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * Neglecting to do this led to the difficult-to-diagnose
5035f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * http://code.google.com/p/skia/issues/detail?id=1079 ('gm generating
5045f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * spurious pixel_error messages as of r7258')
5055f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *
5065f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * TODO(epoger): Come up with a better solution that allows us to
5075f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * compare full pixel data, including alpha channel, while still being
5085f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * robust in the face of transformations to/from PNG files.
5095f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * Options include:
5105f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *
5115f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * 1. Continue to call force_all_opaque(), but ONLY for bitmaps that
5125f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *    will be written to, or compared against, PNG files.
5135f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *    PRO: Preserve/compare alpha channel info for the non-PNG cases
5145f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *         (comparing different renderModes in-memory)
515908f5836626d792c5e33ad93f44c6a418a0cc8f5epoger@google.com     *    CON: The bitmaps (and hash digests) for these non-PNG cases would be
5165f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *         different than those for the PNG-compared cases, and in the
5175f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *         case of a failed renderMode comparison, how would we write the
5185f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *         image to disk for examination?
5195f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *
520908f5836626d792c5e33ad93f44c6a418a0cc8f5epoger@google.com     * 2. Always compute image hash digests from PNG format (either
5215f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *    directly from the the bytes of a PNG file, or capturing the
5225f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *    bytes we would have written to disk if we were writing the
5235f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *    bitmap out as a PNG).
5245f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *    PRO: I think this would allow us to never force opaque, and to
5255f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *         the extent that alpha channel data can be preserved in a PNG
5265f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *         file, we could observe it.
527908f5836626d792c5e33ad93f44c6a418a0cc8f5epoger@google.com     *    CON: If we read a bitmap from disk, we need to take its hash digest
5285f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *         from the source PNG (we can't compute it from the bitmap we
5295f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *         read out of the PNG, because we will have already premultiplied
5305f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *         the alpha).
5315f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *    CON: Seems wasteful to convert a bitmap to PNG format just to take
532908f5836626d792c5e33ad93f44c6a418a0cc8f5epoger@google.com     *         its hash digest. (Although we're wasting lots of effort already
5335f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *         calling force_all_opaque().)
5345f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *
5355f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * 3. Make the alpha premultiply/unpremultiply routines 100% consistent,
5365f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *    so we can transform images back and forth without fear of off-by-one
5375f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *    errors.
5385f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *    CON: Math is hard.
5395f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *
5405f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     * 4. Perform a "close enough" comparison of bitmaps (+/- 1 bit in each
5415f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     *    channel), rather than demanding absolute equality.
542908f5836626d792c5e33ad93f44c6a418a0cc8f5epoger@google.com     *    CON: Can't do this with hash digests.
5435f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com     */
5445f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com    static void complete_bitmap(SkBitmap* bitmap) {
5455f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com        force_all_opaque(*bitmap);
5465f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com    }
5475f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com
54809fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com    static void installFilter(SkCanvas* canvas);
5499875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com
550aef73617d1bcede80145a13639668f958a3863d8reed@google.com    static void invokeGM(GM* gm, SkCanvas* canvas, bool isPDF, bool isDeferred) {
551de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkAutoCanvasRestore acr(canvas, true);
5529875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com
553de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        if (!isPDF) {
554de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            canvas->concat(gm->getInitialTransform());
555de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        }
556de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        installFilter(canvas);
557aef73617d1bcede80145a13639668f958a3863d8reed@google.com        gm->setCanvasIsDeferred(isDeferred);
558de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        gm->draw(canvas);
559de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        canvas->setDrawFilter(NULL);
560de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    }
561b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
5626f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com    static ErrorCombination generate_image(GM* gm, const ConfigData& gRec,
563123ac1d4eab757052407064623643fdc59f85363bsalomon@google.com                                           GrSurface* gpuTarget,
5646f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                                           SkBitmap* bitmap,
5656f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                                           bool deferred) {
566de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkISize size (gm->getISize());
567de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        setup_bitmap(gRec, size, bitmap);
568466f5f3e44e703ca58b43ac1c4ac3bfa0e1ff024commit-bot@chromium.org        const SkImageInfo info = bitmap->info();
569de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
57011db6fa9b9fc30b1f504d9f53e0f6ae7149cc5c5reed@google.com        SkAutoTUnref<SkSurface> surface;
571de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkAutoTUnref<SkCanvas> canvas;
572b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
573de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        if (gRec.fBackend == kRaster_Backend) {
57411db6fa9b9fc30b1f504d9f53e0f6ae7149cc5c5reed@google.com            surface.reset(SkSurface::NewRasterDirect(info,
57511db6fa9b9fc30b1f504d9f53e0f6ae7149cc5c5reed@google.com                                                     bitmap->getPixels(),
57611db6fa9b9fc30b1f504d9f53e0f6ae7149cc5c5reed@google.com                                                     bitmap->rowBytes()));
577de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            if (deferred) {
57811db6fa9b9fc30b1f504d9f53e0f6ae7149cc5c5reed@google.com                canvas.reset(SkDeferredCanvas::Create(surface));
579de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            } else {
58011db6fa9b9fc30b1f504d9f53e0f6ae7149cc5c5reed@google.com                canvas.reset(SkRef(surface->getCanvas()));
581de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            }
582aef73617d1bcede80145a13639668f958a3863d8reed@google.com            invokeGM(gm, canvas, false, deferred);
583de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            canvas->flush();
584de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        }
585de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com#if SK_SUPPORT_GPU
586de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        else {  // GPU
58711db6fa9b9fc30b1f504d9f53e0f6ae7149cc5c5reed@google.com            surface.reset(SkSurface::NewRenderTargetDirect(gpuTarget->asRenderTarget()));
588de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            if (deferred) {
58911db6fa9b9fc30b1f504d9f53e0f6ae7149cc5c5reed@google.com                canvas.reset(SkDeferredCanvas::Create(surface));
590de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            } else {
59111db6fa9b9fc30b1f504d9f53e0f6ae7149cc5c5reed@google.com                canvas.reset(SkRef(surface->getCanvas()));
592de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            }
593aef73617d1bcede80145a13639668f958a3863d8reed@google.com            invokeGM(gm, canvas, false, deferred);
594de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            // the device is as large as the current rendertarget, so
595de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            // we explicitly only readback the amount we expect (in
596de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            // size) overwrite our previous allocation
597a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org            bitmap->setInfo(SkImageInfo::MakeN32Premul(size.fWidth, size.fHeight));
598de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            canvas->readPixels(bitmap, 0, 0);
599de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        }
600de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com#endif
6015f6a00775511b5675607c2bfdbb096c0a815025depoger@google.com        complete_bitmap(bitmap);
6026f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com        return kEmpty_ErrorCombination;
60346cce91f4859b9c229938d4d649870c0a43b1806reed@google.com    }
604de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
605de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    static void generate_image_from_picture(GM* gm, const ConfigData& gRec,
606c938c48bf426c322e2cb45b4bfaf8a2ee5a2a3c5junov@chromium.org                                            SkPicture* pict, SkBitmap* bitmap,
6075a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                                            SkScalar scale = SK_Scalar1,
6085a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                                            bool tile = false) {
609de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkISize size = gm->getISize();
610de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        setup_bitmap(gRec, size, bitmap);
6115a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com
6125a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com        if (tile) {
6135a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            // Generate the result image by rendering to tiles and accumulating
6145a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            // the results in 'bitmap'
6155a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com
616041e2dbc0614e3341e1349fd0e962744a45b6194skia.committer@gmail.com            // This 16x16 tiling matches the settings applied to 'pict' in
6175a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            // 'generate_new_picture'
6185a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            SkISize tileSize = SkISize::Make(16, 16);
6195a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com
6205a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            SkBitmap tileBM;
6215a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            setup_bitmap(gRec, tileSize, &tileBM);
6225a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            SkCanvas tileCanvas(tileBM);
6235a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            installFilter(&tileCanvas);
6245a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com
6255a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            SkCanvas bmpCanvas(*bitmap);
6265a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            SkPaint bmpPaint;
6275a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            bmpPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
6285a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com
6295a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            for (int yTile = 0; yTile < (size.height()+15)/16; ++yTile) {
6305a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                for (int xTile = 0; xTile < (size.width()+15)/16; ++xTile) {
6315a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                    int saveCount = tileCanvas.save();
6325a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                    SkMatrix mat(tileCanvas.getTotalMatrix());
633041e2dbc0614e3341e1349fd0e962744a45b6194skia.committer@gmail.com                    mat.postTranslate(SkIntToScalar(-xTile*tileSize.width()),
6345a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                                      SkIntToScalar(-yTile*tileSize.height()));
6355a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                    tileCanvas.setMatrix(mat);
636c5ba71d2e5cd426def66fa49dcf003e5b2c98dc7robertphillips                    pict->playback(&tileCanvas);
6375a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                    tileCanvas.flush();
6385a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                    tileCanvas.restoreToCount(saveCount);
639041e2dbc0614e3341e1349fd0e962744a45b6194skia.committer@gmail.com                    bmpCanvas.drawBitmap(tileBM,
640041e2dbc0614e3341e1349fd0e962744a45b6194skia.committer@gmail.com                                         SkIntToScalar(xTile * tileSize.width()),
6415a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                                         SkIntToScalar(yTile * tileSize.height()),
6425a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                                         &bmpPaint);
6435a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com                }
6445a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            }
6455a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com        } else {
6465a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            SkCanvas canvas(*bitmap);
6475a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            installFilter(&canvas);
6485a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            canvas.scale(scale, scale);
6499b14f26d0f3a974f3dd626c8354e1db1cfcd322frobertphillips            canvas.drawPicture(pict);
6505a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com            complete_bitmap(bitmap);
6515a7d029fd6fcdf161b58c09887c6f745c37f1855robertphillips@google.com        }
652b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
653de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
6545e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org    static bool generate_pdf(GM* gm, SkDynamicMemoryWStream& pdf) {
655de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com#ifdef SK_SUPPORT_PDF
656de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkMatrix initialTransform = gm->getInitialTransform();
6575e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org        if (FLAGS_useDocumentInsteadOfDevice) {
6585e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            SkISize pageISize = gm->getISize();
65973a7ea3ae0d37ee28f90d6b38c49fda052638253edisonn@google.com            SkAutoTUnref<SkDocument> pdfDoc(
66073a7ea3ae0d37ee28f90d6b38c49fda052638253edisonn@google.com                    SkDocument::CreatePDF(&pdf, NULL,
66173a7ea3ae0d37ee28f90d6b38c49fda052638253edisonn@google.com                                          encode_to_dct_data,
66273a7ea3ae0d37ee28f90d6b38c49fda052638253edisonn@google.com                                          SkIntToScalar(FLAGS_pdfRasterDpi)));
663de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
6645e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            if (!pdfDoc.get()) {
6655e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                return false;
6665e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            }
667de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
6685e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            SkCanvas* canvas = NULL;
6695e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            canvas = pdfDoc->beginPage(SkIntToScalar(pageISize.width()),
6705e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                                       SkIntToScalar(pageISize.height()));
6715e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            canvas->concat(initialTransform);
6725e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org
6735e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            invokeGM(gm, canvas, true, false);
6745e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org
6755e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            return pdfDoc->close();
6765e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org        } else {
6775e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            SkISize pageSize = gm->getISize();
6785e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            SkPDFDevice* dev = NULL;
6795e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            if (initialTransform.isIdentity()) {
6805e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                dev = new SkPDFDevice(pageSize, pageSize, initialTransform);
6815e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            } else {
6825e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                SkRect content = SkRect::MakeWH(SkIntToScalar(pageSize.width()),
6835e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                                                SkIntToScalar(pageSize.height()));
6845e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                initialTransform.mapRect(&content);
6855e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                content.intersect(0, 0, SkIntToScalar(pageSize.width()),
6865e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                                  SkIntToScalar(pageSize.height()));
6875e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                SkISize contentSize =
6885e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                    SkISize::Make(SkScalarRoundToInt(content.width()),
6895e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                                  SkScalarRoundToInt(content.height()));
6905e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                dev = new SkPDFDevice(pageSize, contentSize, initialTransform);
6915e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            }
6925e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            dev->setDCTEncoder(encode_to_dct_data);
69373a7ea3ae0d37ee28f90d6b38c49fda052638253edisonn@google.com            dev->setRasterDpi(SkIntToScalar(FLAGS_pdfRasterDpi));
6945e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            SkAutoUnref aur(dev);
6955e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            SkCanvas c(dev);
6965e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            invokeGM(gm, &c, true, false);
6975e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            SkPDFDocument doc;
6985e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            doc.appendPage(dev);
6995e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            doc.emitPDF(&pdf);
7005e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org        }
7015e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org#endif  // SK_SUPPORT_PDF
7025e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org        return true; // Do not report failure if pdf is not supported.
7039875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com    }
704de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
705de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    static void generate_xps(GM* gm, SkDynamicMemoryWStream& xps) {
706de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com#ifdef SK_SUPPORT_XPS
707de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkISize size = gm->getISize();
708de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
709de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkSize trimSize = SkSize::Make(SkIntToScalar(size.width()),
710de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com                                       SkIntToScalar(size.height()));
711de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        static const SkScalar inchesPerMeter = SkScalarDiv(10000, 254);
712de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        static const SkScalar upm = 72 * inchesPerMeter;
713de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkVector unitsPerMeter = SkPoint::Make(upm, upm);
714de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        static const SkScalar ppm = 200 * inchesPerMeter;
715de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkVector pixelsPerMeter = SkPoint::Make(ppm, ppm);
716de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
717de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkXPSDevice* dev = new SkXPSDevice();
718de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkAutoUnref aur(dev);
719de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
720de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkCanvas c(dev);
721de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        dev->beginPortfolio(&xps);
722de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        dev->beginSheet(unitsPerMeter, pixelsPerMeter, trimSize);
723aef73617d1bcede80145a13639668f958a3863d8reed@google.com        invokeGM(gm, &c, false, false);
724de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        dev->endSheet();
725de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        dev->endPortfolio();
726de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
727de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com#endif
7289875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com    }
7299875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com
73037269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    /**
73184a1802b072d76657261eada3018955283ab29d2epoger@google.com     * Log more detail about the mistmatch between expectedBitmap and
73284a1802b072d76657261eada3018955283ab29d2epoger@google.com     * actualBitmap.
73384a1802b072d76657261eada3018955283ab29d2epoger@google.com     */
73484a1802b072d76657261eada3018955283ab29d2epoger@google.com    void report_bitmap_diffs(const SkBitmap& expectedBitmap, const SkBitmap& actualBitmap,
73584a1802b072d76657261eada3018955283ab29d2epoger@google.com                             const char *testName) {
73684a1802b072d76657261eada3018955283ab29d2epoger@google.com        const int expectedWidth = expectedBitmap.width();
73784a1802b072d76657261eada3018955283ab29d2epoger@google.com        const int expectedHeight = expectedBitmap.height();
73884a1802b072d76657261eada3018955283ab29d2epoger@google.com        const int width = actualBitmap.width();
73984a1802b072d76657261eada3018955283ab29d2epoger@google.com        const int height = actualBitmap.height();
74084a1802b072d76657261eada3018955283ab29d2epoger@google.com        if ((expectedWidth != width) || (expectedHeight != height)) {
7412b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org            SkDebugf("---- %s: dimension mismatch -- expected [%d %d], actual [%d %d]\n",
7422b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                     testName, expectedWidth, expectedHeight, width, height);
74384a1802b072d76657261eada3018955283ab29d2epoger@google.com            return;
74484a1802b072d76657261eada3018955283ab29d2epoger@google.com        }
74584a1802b072d76657261eada3018955283ab29d2epoger@google.com
74628fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.org        if ((kN32_SkColorType != expectedBitmap.colorType()) ||
74728fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.org            (kN32_SkColorType != actualBitmap.colorType())) {
7482b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org            SkDebugf("---- %s: not computing max per-channel pixel mismatch because non-8888\n",
7492b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                     testName);
75084a1802b072d76657261eada3018955283ab29d2epoger@google.com            return;
75184a1802b072d76657261eada3018955283ab29d2epoger@google.com        }
75284a1802b072d76657261eada3018955283ab29d2epoger@google.com
75384a1802b072d76657261eada3018955283ab29d2epoger@google.com        SkAutoLockPixels alp0(expectedBitmap);
75484a1802b072d76657261eada3018955283ab29d2epoger@google.com        SkAutoLockPixels alp1(actualBitmap);
75584a1802b072d76657261eada3018955283ab29d2epoger@google.com        int errR = 0;
75684a1802b072d76657261eada3018955283ab29d2epoger@google.com        int errG = 0;
75784a1802b072d76657261eada3018955283ab29d2epoger@google.com        int errB = 0;
75884a1802b072d76657261eada3018955283ab29d2epoger@google.com        int errA = 0;
75984a1802b072d76657261eada3018955283ab29d2epoger@google.com        int differingPixels = 0;
76084a1802b072d76657261eada3018955283ab29d2epoger@google.com
76184a1802b072d76657261eada3018955283ab29d2epoger@google.com        for (int y = 0; y < height; ++y) {
76284a1802b072d76657261eada3018955283ab29d2epoger@google.com            const SkPMColor* expectedPixelPtr = expectedBitmap.getAddr32(0, y);
76384a1802b072d76657261eada3018955283ab29d2epoger@google.com            const SkPMColor* actualPixelPtr = actualBitmap.getAddr32(0, y);
76484a1802b072d76657261eada3018955283ab29d2epoger@google.com            for (int x = 0; x < width; ++x) {
76584a1802b072d76657261eada3018955283ab29d2epoger@google.com                SkPMColor expectedPixel = *expectedPixelPtr++;
76684a1802b072d76657261eada3018955283ab29d2epoger@google.com                SkPMColor actualPixel = *actualPixelPtr++;
76784a1802b072d76657261eada3018955283ab29d2epoger@google.com                if (expectedPixel != actualPixel) {
76884a1802b072d76657261eada3018955283ab29d2epoger@google.com                    differingPixels++;
76984a1802b072d76657261eada3018955283ab29d2epoger@google.com                    errR = SkMax32(errR, SkAbs32((int)SkGetPackedR32(expectedPixel) -
77084a1802b072d76657261eada3018955283ab29d2epoger@google.com                                                 (int)SkGetPackedR32(actualPixel)));
77184a1802b072d76657261eada3018955283ab29d2epoger@google.com                    errG = SkMax32(errG, SkAbs32((int)SkGetPackedG32(expectedPixel) -
77284a1802b072d76657261eada3018955283ab29d2epoger@google.com                                                 (int)SkGetPackedG32(actualPixel)));
77384a1802b072d76657261eada3018955283ab29d2epoger@google.com                    errB = SkMax32(errB, SkAbs32((int)SkGetPackedB32(expectedPixel) -
77484a1802b072d76657261eada3018955283ab29d2epoger@google.com                                                 (int)SkGetPackedB32(actualPixel)));
77584a1802b072d76657261eada3018955283ab29d2epoger@google.com                    errA = SkMax32(errA, SkAbs32((int)SkGetPackedA32(expectedPixel) -
77684a1802b072d76657261eada3018955283ab29d2epoger@google.com                                                 (int)SkGetPackedA32(actualPixel)));
77784a1802b072d76657261eada3018955283ab29d2epoger@google.com                }
77884a1802b072d76657261eada3018955283ab29d2epoger@google.com            }
77984a1802b072d76657261eada3018955283ab29d2epoger@google.com        }
7802b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org        SkDebugf("---- %s: %d (of %d) differing pixels, "
7812b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                 "max per-channel mismatch R=%d G=%d B=%d A=%d\n",
7822b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                 testName, differingPixels, width*height, errR, errG, errB, errA);
78384a1802b072d76657261eada3018955283ab29d2epoger@google.com    }
78484a1802b072d76657261eada3018955283ab29d2epoger@google.com
78584a1802b072d76657261eada3018955283ab29d2epoger@google.com    /**
786908f5836626d792c5e33ad93f44c6a418a0cc8f5epoger@google.com     * Compares actual hash digest to expectations, returning the set of errors
7876f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com     * (if any) that we saw along the way.
78837269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     *
78937269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * If fMismatchPath has been set, and there are pixel diffs, then the
79037269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * actual bitmap will be written out to a file within fMismatchPath.
7915f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com     * And similarly for fMissingExpectationsPath...
79237269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     *
79337269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * @param expectations what expectations to compare actualBitmap against
7946f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com     * @param actualBitmapAndDigest the SkBitmap we actually generated, and its GmResultDigest
795659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     * @param shortName name of test, e.g. "selftest1"
796659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     * @param configName name of config, e.g. "8888"
79737269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * @param renderModeDescriptor e.g., "-rtree", "-deferred"
79837269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * @param addToJsonSummary whether to add these results (both actual and
799caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com     *        expected) to the JSON summary. Regardless of this setting, if
800caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com     *        we find an image mismatch in this test, we will write these
801caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com     *        results to the JSON summary.  (This is so that we will always
802caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com     *        report errors across rendering modes, such as pipe vs tiled.
803caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com     *        See https://codereview.chromium.org/13650002/ )
80437269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     */
8056f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com    ErrorCombination compare_to_expectations(Expectations expectations,
8066f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                                             const BitmapAndDigest& actualBitmapAndDigest,
807659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                                             const char *shortName, const char *configName,
808659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                                             const char *renderModeDescriptor,
809caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com                                             bool addToJsonSummary) {
8106f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com        ErrorCombination errors;
811659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        SkString shortNamePlusConfig = make_shortname_plus_config(shortName, configName);
812659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        SkString completeNameString(shortNamePlusConfig);
81337269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        completeNameString.append(renderModeDescriptor);
814ce057fec926f8ec1d60ed4722d1e51a0086e7976epoger@google.com        completeNameString.append(".");
815ce057fec926f8ec1d60ed4722d1e51a0086e7976epoger@google.com        completeNameString.append(kPNG_FileExtension);
81637269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        const char* completeName = completeNameString.c_str();
81737269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com
81837269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        if (expectations.empty()) {
8196f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            errors.add(kMissingExpectations_ErrorType);
8205f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com
8215f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com            // Write out the "actuals" for any tests without expectations, if we have
8225f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com            // been directed to do so.
8235f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com            if (fMissingExpectationsPath) {
8245f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com                SkString path = make_bitmap_filename(fMissingExpectationsPath, shortName,
8255f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com                                                     configName, renderModeDescriptor,
8265f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com                                                     actualBitmapAndDigest.fDigest);
8275f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com                write_bitmap(path, actualBitmapAndDigest.fBitmap);
8285f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com            }
8295f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com
8306f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com        } else if (!expectations.match(actualBitmapAndDigest.fDigest)) {
831caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com            addToJsonSummary = true;
832caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com            // The error mode we record depends on whether this was running
833caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com            // in a non-standard renderMode.
834caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com            if ('\0' == *renderModeDescriptor) {
835caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com                errors.add(kExpectationsMismatch_ErrorType);
836caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com            } else {
837caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com                errors.add(kRenderModeMismatch_ErrorType);
838caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com            }
83984a1802b072d76657261eada3018955283ab29d2epoger@google.com
84084a1802b072d76657261eada3018955283ab29d2epoger@google.com            // Write out the "actuals" for any mismatches, if we have
84184a1802b072d76657261eada3018955283ab29d2epoger@google.com            // been directed to do so.
84237269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com            if (fMismatchPath) {
8436f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                SkString path = make_bitmap_filename(fMismatchPath, shortName, configName,
8446f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                                                     renderModeDescriptor,
8456f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                                                     actualBitmapAndDigest.fDigest);
8466f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                write_bitmap(path, actualBitmapAndDigest.fBitmap);
847de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            }
84884a1802b072d76657261eada3018955283ab29d2epoger@google.com
84984a1802b072d76657261eada3018955283ab29d2epoger@google.com            // If we have access to a single expected bitmap, log more
85084a1802b072d76657261eada3018955283ab29d2epoger@google.com            // detail about the mismatch.
85184a1802b072d76657261eada3018955283ab29d2epoger@google.com            const SkBitmap *expectedBitmapPtr = expectations.asBitmap();
85249f085dddff10473b6ebf832a974288300224e60bsalomon            if (expectedBitmapPtr) {
8536f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                report_bitmap_diffs(*expectedBitmapPtr, actualBitmapAndDigest.fBitmap,
8546f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com                                    completeName);
85584a1802b072d76657261eada3018955283ab29d2epoger@google.com            }
856ee8a8e3931c1d3f39755ee8beaf0c7cb1ba91888epoger@google.com        }
85737269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com
85837269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        if (addToJsonSummary) {
8596f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com            add_actual_results_to_json_summary(completeName, actualBitmapAndDigest.fDigest, errors,
86037269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com                                               expectations.ignoreFailure());
86137269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com            add_expected_results_to_json_summary(completeName, expectations);
86237269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        }
86337269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com
8646f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com        return errors;
86537269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    }
866ee8a8e3931c1d3f39755ee8beaf0c7cb1ba91888epoger@google.com
86737269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    /**
8681ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com     * Add this result to the appropriate JSON collection of actual results (but just ONE),
86976c913db20de7ae1baa49ae66e943bf7f40781ccepoger@google.com     * depending on errors encountered.
87037269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     */
87137269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    void add_actual_results_to_json_summary(const char testName[],
872d4993ff3605102036f83d5834d9a022d780e5488epoger@google.com                                            const GmResultDigest &actualResultDigest,
87376c913db20de7ae1baa49ae66e943bf7f40781ccepoger@google.com                                            ErrorCombination errors,
87437269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com                                            bool ignoreFailure) {
875d4993ff3605102036f83d5834d9a022d780e5488epoger@google.com        Json::Value jsonActualResults = actualResultDigest.asJsonTypeValuePair();
8761ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com        Json::Value *resultCollection = NULL;
8771ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com
87876c913db20de7ae1baa49ae66e943bf7f40781ccepoger@google.com        if (errors.isEmpty()) {
8791ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            resultCollection = &this->fJsonActualResults_Succeeded;
8801ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com        } else if (errors.includes(kRenderModeMismatch_ErrorType)) {
8811ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            resultCollection = &this->fJsonActualResults_Failed;
8821ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com        } else if (errors.includes(kExpectationsMismatch_ErrorType)) {
88337269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com            if (ignoreFailure) {
8841ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com                resultCollection = &this->fJsonActualResults_FailureIgnored;
885ee8a8e3931c1d3f39755ee8beaf0c7cb1ba91888epoger@google.com            } else {
8861ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com                resultCollection = &this->fJsonActualResults_Failed;
887ee8a8e3931c1d3f39755ee8beaf0c7cb1ba91888epoger@google.com            }
8881ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com        } else if (errors.includes(kMissingExpectations_ErrorType)) {
8891ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            // TODO: What about the case where there IS an expected
8901ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            // image hash digest, but that gm test doesn't actually
8911ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            // run?  For now, those cases will always be ignored,
8921ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            // because gm only looks at expectations that correspond
8931ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            // to gm tests that were actually run.
8941ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            //
8951ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            // Once we have the ability to express expectations as a
8961ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            // JSON file, we should fix this (and add a test case for
8971ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            // which an expectation is given but the test is never
8981ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            // run).
8991ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            resultCollection = &this->fJsonActualResults_NoComparison;
9001ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com        }
9011ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com
9021ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com        // If none of the above cases match, we don't add it to ANY tally of actual results.
9031ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com        if (resultCollection) {
9041ddfbc201a5445d2da17d2a087a1ec88cc980ffbepoger@google.com            (*resultCollection)[testName] = jsonActualResults;
905de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        }
90637269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    }
907ee8a8e3931c1d3f39755ee8beaf0c7cb1ba91888epoger@google.com
90837269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    /**
90937269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * Add this test to the JSON collection of expected results.
91037269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     */
91137269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    void add_expected_results_to_json_summary(const char testName[],
91237269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com                                              Expectations expectations) {
91376c913db20de7ae1baa49ae66e943bf7f40781ccepoger@google.com        this->fJsonExpectedResults[testName] = expectations.asJsonValue();
9149ef89ce8b7eb270b37941553099654129f2e0a04epoger@google.com    }
915a243b22422c82def12282f2ea713121716e30621epoger@google.com
91637269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    /**
91737269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * Compare actualBitmap to expectations stored in this->fExpectationsSource.
91837269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     *
91937269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * @param gm which test generated the actualBitmap
92037269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * @param gRec
9218fc3766c58ec879389c3c83c96a73e50ff59eb78vandebo@chromium.org     * @param configName The config name to look for in the expectation file.
922e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com     * @param actualBitmapAndDigest ptr to bitmap generated by this run, or NULL
923e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com     *        if we don't have a usable bitmap representation
92437269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     */
9256f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com    ErrorCombination compare_test_results_to_stored_expectations(
9268fc3766c58ec879389c3c83c96a73e50ff59eb78vandebo@chromium.org        GM* gm, const ConfigData& gRec, const char* configName,
9270dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        const BitmapAndDigest* actualBitmapAndDigest) {
928659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        ErrorCombination errors;
929e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com
930e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com        if (NULL == actualBitmapAndDigest) {
931e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com            // Note that we intentionally skipped validating the results for
932e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com            // this test, because we don't know how to generate an SkBitmap
933e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com            // version of the output.
9340dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            errors.add(ErrorCombination(kIntentionallySkipped_ErrorType));
935c824c83ec1402e68e827e8920278a86793a12c0cepoger@google.com        } else if (!(gRec.fFlags & kWrite_ConfigFlag)) {
936c824c83ec1402e68e827e8920278a86793a12c0cepoger@google.com            // We don't record the results for this test or compare them
937c824c83ec1402e68e827e8920278a86793a12c0cepoger@google.com            // against any expectations, because the output image isn't
938c824c83ec1402e68e827e8920278a86793a12c0cepoger@google.com            // meaningful.
939c824c83ec1402e68e827e8920278a86793a12c0cepoger@google.com            // See https://code.google.com/p/skia/issues/detail?id=1410 ('some
940c824c83ec1402e68e827e8920278a86793a12c0cepoger@google.com            // GM result images not available for download from Google Storage')
9410dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            errors.add(ErrorCombination(kIntentionallySkipped_ErrorType));
942e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com        } else {
943e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com            ExpectationsSource *expectationsSource = this->fExpectationsSource.get();
94438aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org            SkString nameWithExtension = make_shortname_plus_config(gm->getName(), configName);
9450dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            nameWithExtension.append(".");
9460dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            nameWithExtension.append(kPNG_FileExtension);
9470dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
948e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com            if (expectationsSource && (gRec.fFlags & kRead_ConfigFlag)) {
949e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                /*
950e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 * Get the expected results for this test, as one or more allowed
951e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 * hash digests. The current implementation of expectationsSource
952e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 * get this by computing the hash digest of a single PNG file on disk.
953e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 *
954e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 * TODO(epoger): This relies on the fact that
955e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 * force_all_opaque() was called on the bitmap before it
956e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 * was written to disk as a PNG in the first place. If
957e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 * not, the hash digest returned here may not match the
958e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 * hash digest of actualBitmap, which *has* been run through
959e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 * force_all_opaque().
960e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 * See comments above complete_bitmap() for more detail.
961e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                 */
962e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                Expectations expectations = expectationsSource->get(nameWithExtension.c_str());
96338aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                if (this->ShouldIgnoreTest(gm->getName())) {
964defc487aa9e6bef16f76846f286f8a1343524d45epoger@google.com                    expectations.setIgnoreFailure(true);
965defc487aa9e6bef16f76846f286f8a1343524d45epoger@google.com                }
966e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                errors.add(compare_to_expectations(expectations, *actualBitmapAndDigest,
96738aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                                                   gm->getName(), configName, "", true));
968e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com            } else {
969e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                // If we are running without expectations, we still want to
970e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                // record the actual results.
971e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                add_actual_results_to_json_summary(nameWithExtension.c_str(),
972e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                                                   actualBitmapAndDigest->fDigest,
973e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                                                   ErrorCombination(kMissingExpectations_ErrorType),
974e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com                                                   false);
9750dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                errors.add(ErrorCombination(kMissingExpectations_ErrorType));
976e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com            }
977de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        }
9786f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com        return errors;
9799284ccd8bc4e3dfed83485d9fa94ebd69c4b9741epoger@google.com    }
9809875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com
98137269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    /**
98237269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * Compare actualBitmap to referenceBitmap.
98337269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     *
984659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     * @param shortName test name, e.g. "selftest1"
985659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com     * @param configName configuration name, e.g. "8888"
98637269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * @param renderModeDescriptor
98737269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * @param actualBitmap actual bitmap generated by this run
98837269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     * @param referenceBitmap bitmap we expected to be generated
98937269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com     */
9906f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com    ErrorCombination compare_test_results_to_reference_bitmap(
991659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        const char *shortName, const char *configName, const char *renderModeDescriptor,
99237269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        SkBitmap& actualBitmap, const SkBitmap* referenceBitmap) {
99337269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com
99437269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        SkASSERT(referenceBitmap);
99584a1802b072d76657261eada3018955283ab29d2epoger@google.com        Expectations expectations(*referenceBitmap);
9966f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com        BitmapAndDigest actualBitmapAndDigest(actualBitmap);
9970dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
9980dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        // TODO: Eliminate RecordTestResults from here.
9990dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        // Results recording code for the test_drawing path has been refactored so that
10000dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        // RecordTestResults is only called once, at the topmost level. However, the
10010dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        // other paths have not yet been refactored, and RecordTestResults has been added
10020dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        // here to maintain proper behavior for calls not coming from the test_drawing path.
10030dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        ErrorCombination errors;
10040dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        errors.add(compare_to_expectations(expectations, actualBitmapAndDigest, shortName,
10050dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                                           configName, renderModeDescriptor, false));
10060dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        SkString shortNamePlusConfig = make_shortname_plus_config(shortName, configName);
10070dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        RecordTestResults(errors, shortNamePlusConfig, renderModeDescriptor);
10080dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
10090dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        return errors;
101037269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    }
101137269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com
101220bd04e365ca8f5cdb37068fedd696e783d1a775junov@chromium.org    static SkPicture* generate_new_picture(GM* gm, BbhType bbhType, uint32_t recordFlags,
101320bd04e365ca8f5cdb37068fedd696e783d1a775junov@chromium.org                                           SkScalar scale = SK_Scalar1) {
1014a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips        SkScalar width = SkScalarMul(SkIntToScalar(gm->getISize().width()), scale);
1015a8d7f0b13cd4c6d773fcf055fe17db75d260fa05robertphillips        SkScalar height = SkScalarMul(SkIntToScalar(gm->getISize().height()), scale);
1016d8b279926e5d57b2377dac52c3358266733f5820skia.committer@gmail.com
10175fb2ce38b3dcb8e60e9e112df23c9d42456d7069commit-bot@chromium.org        SkAutoTDelete<SkBBHFactory> factory;
10183cb834bd27a16cc60ff30adae96659558c2dc91fjunov@chromium.org        if (kTileGrid_BbhType == bbhType) {
10195fb2ce38b3dcb8e60e9e112df23c9d42456d7069commit-bot@chromium.org            SkTileGridFactory::TileGridInfo info;
102029b19e53cfac5af4f9bd5d361436d1097f349a34junov@chromium.org            info.fMargin.setEmpty();
102129b19e53cfac5af4f9bd5d361436d1097f349a34junov@chromium.org            info.fOffset.setZero();
102229b19e53cfac5af4f9bd5d361436d1097f349a34junov@chromium.org            info.fTileInterval.set(16, 16);
10235fb2ce38b3dcb8e60e9e112df23c9d42456d7069commit-bot@chromium.org            factory.reset(SkNEW_ARGS(SkTileGridFactory, (info)));
1024d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org        } else if (kRTree_BbhType == bbhType) {
10255fb2ce38b3dcb8e60e9e112df23c9d42456d7069commit-bot@chromium.org            factory.reset(SkNEW(SkRTreeFactory));
102620bd04e365ca8f5cdb37068fedd696e783d1a775junov@chromium.org        }
10275fb2ce38b3dcb8e60e9e112df23c9d42456d7069commit-bot@chromium.org        SkPictureRecorder recorder;
10285fb2ce38b3dcb8e60e9e112df23c9d42456d7069commit-bot@chromium.org        SkCanvas* cv = recorder.beginRecording(width, height, factory.get(), recordFlags);
1029c938c48bf426c322e2cb45b4bfaf8a2ee5a2a3c5junov@chromium.org        cv->scale(scale, scale);
1030aef73617d1bcede80145a13639668f958a3863d8reed@google.com        invokeGM(gm, cv, false, false);
103184b18c7e3e042bf206e1ace3d1b6ea5bb929fe51robertphillips@google.com        return recorder.endRecording();
1032de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    }
10339875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com
1034de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    static SkPicture* stream_to_new_picture(const SkPicture& src) {
1035de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkDynamicMemoryWStream storage;
1036672588b684d484dce6ae251e9e163e4a46924322reed@google.com        src.serialize(&storage, NULL);
1037c29f3d8c6dec938fd0599db30cae590fcaa2108bbungeman@google.com        SkAutoTUnref<SkStreamAsset> pictReadback(storage.detachAsStream());
1038805df1aa1ddba876e6d18f9d2207d04c09415f9dcommit-bot@chromium.org        SkPicture* retval = SkPicture::CreateFromStream(pictReadback,
1039805df1aa1ddba876e6d18f9d2207d04c09415f9dcommit-bot@chromium.org                                                        &SkImageDecoder::DecodeMemory);
1040de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        return retval;
1041de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    }
10429875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com
1043de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com    // Test: draw into a bitmap or pdf.
104415655b241bfe1d77032e88115f119684722c10e0epoger@google.com    // Depending on flags, possibly compare to an expected image.
10450dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    // If writePath is not NULL, also write images (or documents) to the specified path.
10460dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    ErrorCombination test_drawing(GM* gm, const ConfigData& gRec,
10470dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                                  const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
10486f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                                  const char writePath [],
1049123ac1d4eab757052407064623643fdc59f85363bsalomon@google.com                                  GrSurface* gpuTarget,
10506f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                                  SkBitmap* bitmap) {
10510dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        ErrorCombination errors;
1052de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        SkDynamicMemoryWStream document;
10530dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        SkString path;
1054de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com
1055de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        if (gRec.fBackend == kRaster_Backend ||
1056de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            gRec.fBackend == kGPU_Backend) {
1057de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            // Early exit if we can't generate the image.
10580dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            errors.add(generate_image(gm, gRec, gpuTarget, bitmap, false));
10596f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            if (!errors.isEmpty()) {
106037269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com                // TODO: Add a test to exercise what the stdout and
106137269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com                // JSON look like if we get an "early error" while
106237269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com                // trying to generate the image.
1063de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com                return errors;
1064de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            }
10650dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            BitmapAndDigest bitmapAndDigest(*bitmap);
10660dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            errors.add(compare_test_results_to_stored_expectations(
10678fc3766c58ec879389c3c83c96a73e50ff59eb78vandebo@chromium.org                           gm, gRec, gRec.fName, &bitmapAndDigest));
10680dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
10690dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
107038aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                path = make_bitmap_filename(writePath, gm->getName(), gRec.fName,
10710dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                                            "", bitmapAndDigest.fDigest);
10720dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                errors.add(write_bitmap(path, bitmapAndDigest.fBitmap));
10730dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            }
1074de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        } else if (gRec.fBackend == kPDF_Backend) {
10755e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            if (!generate_pdf(gm, document)) {
10765e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                errors.add(kGeneratePdfFailed_ErrorType);
10775e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org            } else {
10785e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream());
10795e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
108038aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                    path = make_filename(writePath, gm->getName(), gRec.fName, "", "pdf");
10815e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                    errors.add(write_document(path, documentStream));
10825e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                }
10838fc3766c58ec879389c3c83c96a73e50ff59eb78vandebo@chromium.org
10845e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                if (!(gm->getFlags() & GM::kSkipPDFRasterization_Flag)) {
10855e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                    for (int i = 0; i < pdfRasterizers.count(); i++) {
10865e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                        SkBitmap pdfBitmap;
1087969967ed68762ab047645a26eab0565eb760709dvandebo@chromium.org                        documentStream->rewind();
10885e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                        bool success = (*pdfRasterizers[i]->fRasterizerFunction)(
10895e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                                documentStream.get(), &pdfBitmap);
10905e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                        if (!success) {
10912b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                            SkDebugf("FAILED to render PDF for %s using renderer %s\n",
10922b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                                     gm->getName(),
10932b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                                     pdfRasterizers[i]->fName);
10945e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                            continue;
10955e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                        }
10965e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org
10975e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                        SkString configName(gRec.fName);
10985e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                        configName.append("-");
10995e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                        configName.append(pdfRasterizers[i]->fName);
11005e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org
11015e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                        BitmapAndDigest bitmapAndDigest(pdfBitmap);
11025e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                        errors.add(compare_test_results_to_stored_expectations(
11035e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                                   gm, gRec, configName.c_str(), &bitmapAndDigest));
11045e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org
11055e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                        if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
110638aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                            path = make_bitmap_filename(writePath, gm->getName(),
11075e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                                                        configName.c_str(),
11085e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                                                        "", bitmapAndDigest.fDigest);
11095e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                            errors.add(write_bitmap(path, bitmapAndDigest.fBitmap));
11105e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                        }
11110dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                    }
11125e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                } else {
11135e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.org                    errors.add(kIntentionallySkipped_ErrorType);
11140dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                }
11150dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            }
1116de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        } else if (gRec.fBackend == kXPS_Backend) {
1117de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            generate_xps(gm, document);
11180dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            SkAutoTUnref<SkStreamAsset> documentStream(document.detachAsStream());
11190dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
11200dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            errors.add(compare_test_results_to_stored_expectations(
11218fc3766c58ec879389c3c83c96a73e50ff59eb78vandebo@chromium.org                           gm, gRec, gRec.fName, NULL));
1122e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com
11230dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            if (writePath && (gRec.fFlags & kWrite_ConfigFlag)) {
112438aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                path = make_filename(writePath, gm->getName(), gRec.fName, "", "xps");
11250dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                errors.add(write_document(path, documentStream));
11260dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            }
1127e33e137623d56a9d61f00ef74c6ad2e998613062epoger@google.com        } else {
11280dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            SkASSERT(false);
1129de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        }
11300dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        return errors;
11319875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com    }
11329875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.com
11336f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com    ErrorCombination test_deferred_drawing(GM* gm,
11346f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                                           const ConfigData& gRec,
11356f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                                           const SkBitmap& referenceBitmap,
1136123ac1d4eab757052407064623643fdc59f85363bsalomon@google.com                                           GrSurface* gpuTarget) {
1137de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        if (gRec.fBackend == kRaster_Backend ||
1138de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            gRec.fBackend == kGPU_Backend) {
1139c8263e704135436f71a585801966294d6deadeebepoger@google.com            const char renderModeDescriptor[] = "-deferred";
1140de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            SkBitmap bitmap;
1141de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            // Early exit if we can't generate the image, but this is
1142de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            // expected in some cases, so don't report a test failure.
1143123ac1d4eab757052407064623643fdc59f85363bsalomon@google.com            ErrorCombination errors = generate_image(gm, gRec, gpuTarget, &bitmap, true);
11446f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            // TODO(epoger): This logic is the opposite of what is
11456f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            // described above... if we succeeded in generating the
11466f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            // -deferred image, we exit early!  We should fix this
11476f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            // ASAP, because it is hiding -deferred errors... but for
11486f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            // now, I'm leaving the logic as it is so that the
11496f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            // refactoring change
11506f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            // https://codereview.chromium.org/12992003/ is unblocked.
11516f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            //
11526f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            // Filed as https://code.google.com/p/skia/issues/detail?id=1180
11536f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            // ('image-surface gm test is failing in "deferred" mode,
11546f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            // and gm is not reporting the failure')
11556f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com            if (errors.isEmpty()) {
1156310478e72c63e639373465216271b81f1e4a9136epoger@google.com                // TODO(epoger): Report this as a new ErrorType,
1157310478e72c63e639373465216271b81f1e4a9136epoger@google.com                // something like kImageGeneration_ErrorType?
11586f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                return kEmpty_ErrorCombination;
1159de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            }
116037269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com            return compare_test_results_to_reference_bitmap(
116138aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                gm->getName(), gRec.fName, renderModeDescriptor, bitmap, &referenceBitmap);
11624370aedf7f55af74e9ebb4ad1c2e010c08236dfajunov@google.com        }
11636f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com        return kEmpty_ErrorCombination;
11644370aedf7f55af74e9ebb4ad1c2e010c08236dfajunov@google.com    }
11654370aedf7f55af74e9ebb4ad1c2e010c08236dfajunov@google.com
1166caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com    ErrorCombination test_pipe_playback(GM* gm, const ConfigData& gRec,
1167caac3db48f74676889006b5520b766b572b5e9b2epoger@google.com                                        const SkBitmap& referenceBitmap, bool simulateFailure) {
116838aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org        const SkString shortNamePlusConfig = make_shortname_plus_config(gm->getName(),
1169659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                                                                        gRec.fName);
11706f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com        ErrorCombination errors;
1171de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
1172c8263e704135436f71a585801966294d6deadeebepoger@google.com            SkString renderModeDescriptor("-pipe");
1173c8263e704135436f71a585801966294d6deadeebepoger@google.com            renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1174c8263e704135436f71a585801966294d6deadeebepoger@google.com
1175805df1aa1ddba876e6d18f9d2207d04c09415f9dcommit-bot@chromium.org            if (gm->getFlags() & GM::kSkipPipe_Flag
1176805df1aa1ddba876e6d18f9d2207d04c09415f9dcommit-bot@chromium.org                || (gPipeWritingFlagCombos[i].flags == SkGPipeWriter::kCrossProcess_Flag
1177805df1aa1ddba876e6d18f9d2207d04c09415f9dcommit-bot@chromium.org                    && gm->getFlags() & GM::kSkipPipeCrossProcess_Flag)) {
1178659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1179c8263e704135436f71a585801966294d6deadeebepoger@google.com                                  renderModeDescriptor.c_str());
1180c8263e704135436f71a585801966294d6deadeebepoger@google.com                errors.add(kIntentionallySkipped_ErrorType);
1181c8263e704135436f71a585801966294d6deadeebepoger@google.com            } else {
1182c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkBitmap bitmap;
1183c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkISize size = gm->getISize();
1184c8263e704135436f71a585801966294d6deadeebepoger@google.com                setup_bitmap(gRec, size, &bitmap);
1185c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkCanvas canvas(bitmap);
1186c8263e704135436f71a585801966294d6deadeebepoger@google.com                installFilter(&canvas);
118774b7ffda687c66d46ac3cfa4f2baedd4c62e3fbescroggo@google.com                // Pass a decoding function so the factory GM (which has an SkBitmap
118874b7ffda687c66d46ac3cfa4f2baedd4c62e3fbescroggo@google.com                // with encoded data) will not fail playback.
118974b7ffda687c66d46ac3cfa4f2baedd4c62e3fbescroggo@google.com                PipeController pipeController(&canvas, &SkImageDecoder::DecodeMemory);
1190c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkGPipeWriter writer;
1191c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
1192aef2d3b14eab4f86e7d0efef952e64b3546630acscroggo@google.com                                                             gPipeWritingFlagCombos[i].flags,
1193aef2d3b14eab4f86e7d0efef952e64b3546630acscroggo@google.com                                                             size.width(), size.height());
1194c8263e704135436f71a585801966294d6deadeebepoger@google.com                if (!simulateFailure) {
1195c8263e704135436f71a585801966294d6deadeebepoger@google.com                    invokeGM(gm, pipeCanvas, false, false);
1196c8263e704135436f71a585801966294d6deadeebepoger@google.com                }
1197c8263e704135436f71a585801966294d6deadeebepoger@google.com                complete_bitmap(&bitmap);
1198c8263e704135436f71a585801966294d6deadeebepoger@google.com                writer.endRecording();
1199c8263e704135436f71a585801966294d6deadeebepoger@google.com                errors.add(compare_test_results_to_reference_bitmap(
120038aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                    gm->getName(), gRec.fName, renderModeDescriptor.c_str(), bitmap,
1201659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                    &referenceBitmap));
1202c8263e704135436f71a585801966294d6deadeebepoger@google.com                if (!errors.isEmpty()) {
1203c8263e704135436f71a585801966294d6deadeebepoger@google.com                    break;
1204c8263e704135436f71a585801966294d6deadeebepoger@google.com                }
1205de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            }
1206565254bc9343d0befdfbbb97a3dc6d44c6e18658scroggo@google.com        }
1207de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        return errors;
1208565254bc9343d0befdfbbb97a3dc6d44c6e18658scroggo@google.com    }
12095af9b2032b552516c9223d9fb22185b022b13c62scroggo@google.com
12106f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com    ErrorCombination test_tiled_pipe_playback(GM* gm, const ConfigData& gRec,
12116f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                                              const SkBitmap& referenceBitmap) {
121238aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org        const SkString shortNamePlusConfig = make_shortname_plus_config(gm->getName(),
1213659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                                                                        gRec.fName);
12146f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com        ErrorCombination errors;
1215de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        for (size_t i = 0; i < SK_ARRAY_COUNT(gPipeWritingFlagCombos); ++i) {
1216c8263e704135436f71a585801966294d6deadeebepoger@google.com            SkString renderModeDescriptor("-tiled pipe");
1217c8263e704135436f71a585801966294d6deadeebepoger@google.com            renderModeDescriptor.append(gPipeWritingFlagCombos[i].name);
1218c8263e704135436f71a585801966294d6deadeebepoger@google.com
1219c8263e704135436f71a585801966294d6deadeebepoger@google.com            if ((gm->getFlags() & GM::kSkipPipe_Flag) ||
1220c8263e704135436f71a585801966294d6deadeebepoger@google.com                (gm->getFlags() & GM::kSkipTiled_Flag)) {
1221659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1222c8263e704135436f71a585801966294d6deadeebepoger@google.com                                  renderModeDescriptor.c_str());
1223c8263e704135436f71a585801966294d6deadeebepoger@google.com                errors.add(kIntentionallySkipped_ErrorType);
1224c8263e704135436f71a585801966294d6deadeebepoger@google.com            } else {
1225c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkBitmap bitmap;
1226c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkISize size = gm->getISize();
1227c8263e704135436f71a585801966294d6deadeebepoger@google.com                setup_bitmap(gRec, size, &bitmap);
1228c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkCanvas canvas(bitmap);
1229c8263e704135436f71a585801966294d6deadeebepoger@google.com                installFilter(&canvas);
123074b7ffda687c66d46ac3cfa4f2baedd4c62e3fbescroggo@google.com                TiledPipeController pipeController(bitmap, &SkImageDecoder::DecodeMemory);
1231c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkGPipeWriter writer;
1232c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkCanvas* pipeCanvas = writer.startRecording(&pipeController,
1233aef2d3b14eab4f86e7d0efef952e64b3546630acscroggo@google.com                                                             gPipeWritingFlagCombos[i].flags,
1234aef2d3b14eab4f86e7d0efef952e64b3546630acscroggo@google.com                                                             size.width(), size.height());
1235c8263e704135436f71a585801966294d6deadeebepoger@google.com                invokeGM(gm, pipeCanvas, false, false);
1236c8263e704135436f71a585801966294d6deadeebepoger@google.com                complete_bitmap(&bitmap);
1237c8263e704135436f71a585801966294d6deadeebepoger@google.com                writer.endRecording();
123838aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                errors.add(compare_test_results_to_reference_bitmap(gm->getName(), gRec.fName,
1239c8263e704135436f71a585801966294d6deadeebepoger@google.com                                                                    renderModeDescriptor.c_str(),
1240c8263e704135436f71a585801966294d6deadeebepoger@google.com                                                                    bitmap, &referenceBitmap));
1241c8263e704135436f71a585801966294d6deadeebepoger@google.com                if (!errors.isEmpty()) {
1242c8263e704135436f71a585801966294d6deadeebepoger@google.com                    break;
1243c8263e704135436f71a585801966294d6deadeebepoger@google.com                }
1244de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com            }
1245565254bc9343d0befdfbbb97a3dc6d44c6e18658scroggo@google.com        }
1246de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com        return errors;
1247565254bc9343d0befdfbbb97a3dc6d44c6e18658scroggo@google.com    }
1248e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com
1249e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com    //
1250e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com    // member variables.
1251e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com    // They are public for now, to allow easier setting by tool_main().
1252e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com    //
1253e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com
12546f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com    bool fUseFileHierarchy, fWriteChecksumBasedFilenames;
12555079d2c2a0dc0deacecccc6ef9d1c45c6c7982e4epoger@google.com    ErrorCombination fIgnorableErrorTypes;
12563e62ebf93b43139202ce803245411467c7ed13ffcommit-bot@chromium.org    SkTArray<SkString> fIgnorableTestNames;
1257e8ebeb1f8fde6525bbab988c6090a5d3ab19855bepoger@google.com
125895146ebc43175ae0c1cd3a116509d92aa1a445abjunov@chromium.org    const char* fMismatchPath;
12595f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com    const char* fMissingExpectationsPath;
126095146ebc43175ae0c1cd3a116509d92aa1a445abjunov@chromium.org
1261310478e72c63e639373465216271b81f1e4a9136epoger@google.com    // collection of tests that have failed with each ErrorType
1262310478e72c63e639373465216271b81f1e4a9136epoger@google.com    SkTArray<SkString> fFailedTests[kLast_ErrorType+1];
126385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org    SkTArray<SkString> fTestsSkippedOnAllRenderModes;
1264310478e72c63e639373465216271b81f1e4a9136epoger@google.com    int fTestsRun;
1265310478e72c63e639373465216271b81f1e4a9136epoger@google.com    SkTDict<int> fRenderModesEncountered;
126657f7abc8659f17e58fc2d1410117033ad524f9d3epoger@google.com
1267908f5836626d792c5e33ad93f44c6a418a0cc8f5epoger@google.com    // Where to read expectations (expected image hash digests, etc.) from.
126837269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    // If unset, we don't do comparisons.
126937269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    SkAutoTUnref<ExpectationsSource> fExpectationsSource;
127037269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com
127137269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com    // JSON summaries that we generate as we go (just for output).
1272ee8a8e3931c1d3f39755ee8beaf0c7cb1ba91888epoger@google.com    Json::Value fJsonExpectedResults;
1273ee8a8e3931c1d3f39755ee8beaf0c7cb1ba91888epoger@google.com    Json::Value fJsonActualResults_Failed;
1274ee8a8e3931c1d3f39755ee8beaf0c7cb1ba91888epoger@google.com    Json::Value fJsonActualResults_FailureIgnored;
12759c56a8dea6587242671fb36db3940277db6018dbepoger@google.com    Json::Value fJsonActualResults_NoComparison;
1276ee8a8e3931c1d3f39755ee8beaf0c7cb1ba91888epoger@google.com    Json::Value fJsonActualResults_Succeeded;
1277de96163a80167636d95837f9ee6a2e98baf9d350epoger@google.com}; // end of GMMain class definition
127872c9672ce274a3b6cb40800d66374edf25b157a3scroggo@google.com
1279cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
1280cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.comstatic const GLContextType kDontCare_GLContextType = GrContextFactory::kNative_GLContextType;
1281cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#else
1282cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.comstatic const GLContextType kDontCare_GLContextType = 0;
1283cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
12847361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com
12859875dd14af6d768da8d1a4be58b98fc91ceca0ddtomhudson@google.comstatic const ConfigData gRec[] = {
1286ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType,     kRaster_Backend, kDontCare_GLContextType,                  0, kRW_ConfigFlag,    "8888",         true },
1287ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kRGB_565_SkColorType, kRaster_Backend, kDontCare_GLContextType,                  0, kRW_ConfigFlag,    "565",          true },
12884c75f241cb75f202cfa51569ba8dc4749a28a9f7bsalomon@google.com#if SK_SUPPORT_GPU
1289ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kGPU_Backend,    GrContextFactory::kNative_GLContextType,  0, kRW_ConfigFlag,    "gpu",          true },
1290ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kGPU_Backend,    GrContextFactory::kNative_GLContextType, 16, kRW_ConfigFlag,    "msaa16",       false},
1291ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kGPU_Backend,    GrContextFactory::kNative_GLContextType,  4, kRW_ConfigFlag,    "msaa4",        false},
1292ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kGPU_Backend,    GrContextFactory::kNVPR_GLContextType,    4, kRW_ConfigFlag,    "nvprmsaa4",   true },
1293ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kGPU_Backend,    GrContextFactory::kNVPR_GLContextType,   16, kRW_ConfigFlag,    "nvprmsaa16",  false},
1294c824c83ec1402e68e827e8920278a86793a12c0cepoger@google.com    /* The gpudebug context does not generate meaningful images, so don't record
1295c824c83ec1402e68e827e8920278a86793a12c0cepoger@google.com     * the images it generates!  We only run it to look for asserts. */
1296ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kGPU_Backend,    GrContextFactory::kDebug_GLContextType,   0, kNone_ConfigFlag,  "gpudebug",     kDebugOnly},
1297d6543e5edae5d2fe9f364a563f41c1654b14e5f4robertphillips@google.com    /* The gpunull context does the least amount of work possible and doesn't
1298d6543e5edae5d2fe9f364a563f41c1654b14e5f4robertphillips@google.com       generate meaninful images, so don't record them!. It can be run to
1299d6543e5edae5d2fe9f364a563f41c1654b14e5f4robertphillips@google.com       isolate the CPU-side processing expense from the GPU-side.
1300d6543e5edae5d2fe9f364a563f41c1654b14e5f4robertphillips@google.com      */
1301ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kGPU_Backend,    GrContextFactory::kNull_GLContextType,    0, kNone_ConfigFlag,  "gpunull",      kDebugOnly},
1302cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_ANGLE
1303ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kGPU_Backend,    GrContextFactory::kANGLE_GLContextType,   0, kRW_ConfigFlag,    "angle",        true },
1304ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kGPU_Backend,    GrContextFactory::kANGLE_GLContextType,  16, kRW_ConfigFlag,    "anglemsaa16",  true },
1305cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif // SK_ANGLE
1306cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#ifdef SK_MESA
1307ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kGPU_Backend,    GrContextFactory::kMESA_GLContextType,    0, kRW_ConfigFlag,    "mesa",         true },
1308cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif // SK_MESA
13094c75f241cb75f202cfa51569ba8dc4749a28a9f7bsalomon@google.com#endif // SK_SUPPORT_GPU
1310b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#ifdef SK_SUPPORT_XPS
13117361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com    /* At present we have no way of comparing XPS files (either natively or by converting to PNG). */
1312ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kXPS_Backend,    kDontCare_GLContextType,                  0, kWrite_ConfigFlag, "xps",          true },
1313a73e860923885ae05ae794b40e7e3fe0b1bd8478robertphillips@google.com#endif // SK_SUPPORT_XPS
13147361f54294d65a5c42ce5cf1cd56d0fd7122e268bsalomon@google.com#ifdef SK_SUPPORT_PDF
1315ddd014e65264e3f18b2d4305dd2fbc81549f5180reed    { kN32_SkColorType, kPDF_Backend,    kDontCare_GLContextType,                  0, kRW_ConfigFlag,    "pdf",          true },
1316cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif // SK_SUPPORT_PDF
131700dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com};
131800dae86f5872b60927b28a32b375bc01cd7c61c9reed@android.com
13195d20caea15b574c9027ea4e695a03c4402e1d832bungeman@google.comstatic bool SkNoRasterizePDF(SkStream*, SkBitmap*) { return false; }
13205d20caea15b574c9027ea4e695a03c4402e1d832bungeman@google.com
13210dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.orgstatic const PDFRasterizerData kPDFRasterizers[] = {
13220dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org#ifdef SK_BUILD_FOR_MAC
13230dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    { &SkPDFDocumentToBitmap, "mac",     true },
13240dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org#endif
13250dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org#ifdef SK_BUILD_POPPLER
13260dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    { &SkPopplerRasterizePDF, "poppler", true },
13270dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org#endif
1328ffd178c84d85a12016d844d1b9b3fe2172e32a74commit-bot@chromium.org#ifdef SK_BUILD_NATIVE_PDF_RENDERER
1329ffd178c84d85a12016d844d1b9b3fe2172e32a74commit-bot@chromium.org    { &SkNativeRasterizePDF,  "native",  true },
1330ffd178c84d85a12016d844d1b9b3fe2172e32a74commit-bot@chromium.org#endif  // SK_BUILD_NATIVE_PDF_RENDERER
13315d20caea15b574c9027ea4e695a03c4402e1d832bungeman@google.com    // The following exists so that this array is never zero length.
13325d20caea15b574c9027ea4e695a03c4402e1d832bungeman@google.com    { &SkNoRasterizePDF,      "none",    false},
13330dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org};
13340dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
1335dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.comstatic const char kDefaultsConfigStr[] = "defaults";
1336dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.comstatic const char kExcludeConfigChar = '~';
133780549fcdd50269d7e069d6db02b395fca128056ckkinnunen#if SK_SUPPORT_GPU
133880549fcdd50269d7e069d6db02b395fca128056ckkinnunenstatic const char kGpuAPINameGL[] = "gl";
133980549fcdd50269d7e069d6db02b395fca128056ckkinnunenstatic const char kGpuAPINameGLES[] = "gles";
134080549fcdd50269d7e069d6db02b395fca128056ckkinnunen#endif
1341dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com
134209fd4d216963c498c24ba355accce9337a5029a8scroggo@google.comstatic SkString configUsage() {
13430f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com    SkString result;
13440f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com    result.appendf("Space delimited list of which configs to run. Possible options: [");
13455867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.com    for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1346dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com        SkASSERT(gRec[i].fName != kDefaultsConfigStr);
13475867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.com        if (i > 0) {
13480f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com            result.append("|");
13495867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.com        }
135009fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com        result.appendf("%s", gRec[i].fName);
13515867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.com    }
13520f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com    result.append("]\n");
13530f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com    result.appendf("The default value is: \"");
1354dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com    SkString firstDefault;
1355dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com    SkString allButFirstDefaults;
1356dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com    SkString nonDefault;
13570f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com    for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
13580f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com        if (gRec[i].fRunByDefault) {
13590f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com            if (i > 0) {
13600f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com                result.append(" ");
13610f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com            }
1362dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            result.append(gRec[i].fName);
1363dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            if (firstDefault.isEmpty()) {
1364dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                firstDefault = gRec[i].fName;
1365dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            } else {
1366dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                if (!allButFirstDefaults.isEmpty()) {
1367dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                    allButFirstDefaults.append(" ");
1368dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                }
1369dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                allButFirstDefaults.append(gRec[i].fName);
1370dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            }
1371dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com        } else {
1372dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            nonDefault = gRec[i].fName;
13730f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com        }
13740f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.com    }
1375dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com    result.append("\"\n");
1376dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com    result.appendf("\"%s\" evaluates to the default set of configs.\n", kDefaultsConfigStr);
1377dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com    result.appendf("Prepending \"%c\" on a config name excludes it from the set of configs to run.\n"
1378dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                   "Exclusions always override inclusions regardless of order.\n",
1379dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                   kExcludeConfigChar);
1380dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com    result.appendf("E.g. \"--config %s %c%s %s\" will run these configs:\n\t%s %s",
1381dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                   kDefaultsConfigStr,
1382dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                   kExcludeConfigChar,
1383dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                   firstDefault.c_str(),
1384dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                   nonDefault.c_str(),
1385dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                   allButFirstDefaults.c_str(),
1386dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                   nonDefault.c_str());
138709fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com    return result;
138809fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com}
138909fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com
13900dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.orgstatic SkString pdfRasterizerUsage() {
13910dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    SkString result;
13920dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    result.appendf("Space delimited list of which PDF rasterizers to run. Possible options: [");
13930dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    // For this (and further) loops through kPDFRasterizers, there is a typecast to int to avoid
13940dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    // the compiler giving an "comparison of unsigned expression < 0 is always false" warning
13950dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    // and turning it into a build-breaking error.
13960dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
13970dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        if (i > 0) {
13980dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            result.append(" ");
13990dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        }
14000dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        result.append(kPDFRasterizers[i].fName);
14010dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    }
14020dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    result.append("]\n");
14030dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    result.append("The default value is: \"");
14040dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
14050dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        if (kPDFRasterizers[i].fRunByDefault) {
14060dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            if (i > 0) {
14070dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                result.append(" ");
14080dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            }
14090dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            result.append(kPDFRasterizers[i].fName);
14100dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        }
14110dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    }
14120dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    result.append("\"");
14130dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    return result;
14140dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org}
14150dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
14166f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com// Macro magic to convert a numeric preprocessor token into a string.
14176f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com// Adapted from http://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string
14186f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com// This should probably be moved into one of our common headers...
14196f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com#define TOSTRING_INTERNAL(x) #x
14206f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com#define TOSTRING(x) TOSTRING_INTERNAL(x)
14216f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com
14227d519305bb52eadf6b8265c6720642193791a364scroggo@google.com// Alphabetized ignoring "no" prefix ("readPath", "noreplay", "resourcePath").
14230f567c6cb6944d0fc1ce18214c3459ba6b23fdf0scroggo@google.comDEFINE_string(config, "", configUsage().c_str());
142469456186f723b260be6cfa2ac2aa245f860841cbbsalomonDEFINE_bool(cpu, true, "Allows non-GPU configs to be run. Applied after --config.");
1425f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.orgDEFINE_string(pdfRasterizers, "default", pdfRasterizerUsage().c_str());
14265e49738c99f573b2e5ba342f6ec8bfa3bcf125e4epoger@google.comDEFINE_bool(deferred, false, "Exercise the deferred rendering test pass.");
1427b17ccc992da94e55cef07cedf5491911d78dfa3fcommit-bot@chromium.orgDEFINE_bool(dryRun, false, "Don't actually run the tests, just print what would have been done.");
142809fd4d216963c498c24ba355accce9337a5029a8scroggo@google.comDEFINE_string(excludeConfig, "", "Space delimited list of configs to skip.");
142909fd4d216963c498c24ba355accce9337a5029a8scroggo@google.comDEFINE_bool(forceBWtext, false, "Disable text anti-aliasing.");
14300b735631b7d22316693629a4110c7e95b2a7633escroggo@google.com#if SK_SUPPORT_GPU
143180549fcdd50269d7e069d6db02b395fca128056ckkinnunenDEFINE_string(gpuAPI, "", "Force use of specific gpu API.  Using \"gl\" "
143280549fcdd50269d7e069d6db02b395fca128056ckkinnunen              "forces OpenGL API. Using \"gles\" forces OpenGL ES API. "
143380549fcdd50269d7e069d6db02b395fca128056ckkinnunen              "Defaults to empty string, which selects the API native to the "
143480549fcdd50269d7e069d6db02b395fca128056ckkinnunen              "system.");
143509fd4d216963c498c24ba355accce9337a5029a8scroggo@google.comDEFINE_string(gpuCacheSize, "", "<bytes> <count>: Limit the gpu cache to byte size or "
14366f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com              "object count. " TOSTRING(DEFAULT_CACHE_VALUE) " for either value means "
14376f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com              "use the default. 0 for either disables the cache.");
143869456186f723b260be6cfa2ac2aa245f860841cbbsalomonDEFINE_bool(gpu, true, "Allows GPU configs to be run. Applied after --config.");
143912b3544028e74712c6c095ed3a2e8a78de6b2ed8krajcevskiDEFINE_bool(gpuCompressAlphaMasks, false, "Compress masks generated from falling back to "
144012b3544028e74712c6c095ed3a2e8a78de6b2ed8krajcevski                                          "software path rendering.");
14410b735631b7d22316693629a4110c7e95b2a7633escroggo@google.com#endif
144209fd4d216963c498c24ba355accce9337a5029a8scroggo@google.comDEFINE_bool(hierarchy, false, "Whether to use multilevel directory structure "
144309fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com            "when reading/writing files.");
14445079d2c2a0dc0deacecccc6ef9d1c45c6c7982e4epoger@google.comDEFINE_string(ignoreErrorTypes, kDefaultIgnorableErrorTypes.asString(" ").c_str(),
14455079d2c2a0dc0deacecccc6ef9d1c45c6c7982e4epoger@google.com              "Space-separated list of ErrorTypes that should be ignored. If any *other* error "
14465079d2c2a0dc0deacecccc6ef9d1c45c6c7982e4epoger@google.com              "types are encountered, the tool will exit with a nonzero return value.");
1447f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.comDEFINE_string(ignoreFailuresFile, "", "Path to file containing a list of tests for which we "
1448f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com              "should ignore failures.\n"
1449f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com              "The file should list one test per line, except for comment lines starting with #");
14506dda827913a3dbdb84934f4f1d79c0b702169e5ccommit-bot@chromium.orgDEFINE_bool2(leaks, l, false, "show leaked ref cnt'd objects.");
1451512c9b65fc9fd6d298892719aeda2a8bcd377ac5caryclark@google.comDEFINE_string(match, "", "[~][^]substring[$] [...] of test name to run.\n"
1452512c9b65fc9fd6d298892719aeda2a8bcd377ac5caryclark@google.com              "Multiple matches may be separated by spaces.\n"
1453512c9b65fc9fd6d298892719aeda2a8bcd377ac5caryclark@google.com              "~ causes a matching test to always be skipped\n"
1454512c9b65fc9fd6d298892719aeda2a8bcd377ac5caryclark@google.com              "^ requires the start of the test to match\n"
1455512c9b65fc9fd6d298892719aeda2a8bcd377ac5caryclark@google.com              "$ requires the end of the test to match\n"
1456512c9b65fc9fd6d298892719aeda2a8bcd377ac5caryclark@google.com              "^ and $ requires an exact match\n"
1457512c9b65fc9fd6d298892719aeda2a8bcd377ac5caryclark@google.com              "If a test does not match any list entry,\n"
1458512c9b65fc9fd6d298892719aeda2a8bcd377ac5caryclark@google.com              "it is skipped unless some list entry starts with ~");
14595f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.comDEFINE_string(missingExpectationsPath, "", "Write images for tests without expectations "
14605f99545de67ee729b4b17f1aae56f8c9dcf460f9epoger@google.com              "into this directory.");
146109fd4d216963c498c24ba355accce9337a5029a8scroggo@google.comDEFINE_string(mismatchPath, "", "Write images for tests that failed due to "
146209fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com              "pixel mismatches into this directory.");
146309fd4d216963c498c24ba355accce9337a5029a8scroggo@google.comDEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which "
146409fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com              "testIndex %% divisor == remainder.");
14655e49738c99f573b2e5ba342f6ec8bfa3bcf125e4epoger@google.comDEFINE_bool(pipe, false, "Exercise the SkGPipe replay test pass.");
146609fd4d216963c498c24ba355accce9337a5029a8scroggo@google.comDEFINE_string2(readPath, r, "", "Read reference images from this dir, and report "
146709fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com               "any differences between those and the newly generated ones.");
14685e49738c99f573b2e5ba342f6ec8bfa3bcf125e4epoger@google.comDEFINE_bool(replay, false, "Exercise the SkPicture replay test pass.");
1469ac8f3a4f133d059c74ea20c106b963235ee6e48edjsollen@google.com#if SK_SUPPORT_GPU
1470ac8f3a4f133d059c74ea20c106b963235ee6e48edjsollen@google.comDEFINE_bool(resetGpuContext, false, "Reset the GrContext prior to running each GM.");
1471ac8f3a4f133d059c74ea20c106b963235ee6e48edjsollen@google.com#endif
14725e49738c99f573b2e5ba342f6ec8bfa3bcf125e4epoger@google.comDEFINE_bool(rtree, false, "Exercise the R-Tree variant of SkPicture test pass.");
14735e49738c99f573b2e5ba342f6ec8bfa3bcf125e4epoger@google.comDEFINE_bool(serialize, false, "Exercise the SkPicture serialization & deserialization test pass.");
1474caac3db48f74676889006b5520b766b572b5e9b2epoger@google.comDEFINE_bool(simulatePipePlaybackFailure, false, "Simulate a rendering failure in pipe mode only.");
147509fd4d216963c498c24ba355accce9337a5029a8scroggo@google.comDEFINE_bool(tiledPipe, false, "Exercise tiled SkGPipe replay.");
14765e49738c99f573b2e5ba342f6ec8bfa3bcf125e4epoger@google.comDEFINE_bool(tileGrid, false, "Exercise the tile grid variant of SkPicture.");
147709fd4d216963c498c24ba355accce9337a5029a8scroggo@google.comDEFINE_string(tileGridReplayScales, "", "Space separated list of floating-point scale "
147809fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com              "factors to be used for tileGrid playback testing. Default value: 1.0");
1479b0f8b430de74a51f35dbfbff5284a04f8d70ec48epoger@google.comDEFINE_bool2(verbose, v, false, "Give more detail (e.g. list all GMs run, more info about "
1480b0f8b430de74a51f35dbfbff5284a04f8d70ec48epoger@google.com             "each test).");
14816f7f14d061b045a58a888ee5450b4596797dc453epoger@google.comDEFINE_bool(writeChecksumBasedFilenames, false, "When writing out actual images, use checksum-"
14826f7f14d061b045a58a888ee5450b4596797dc453epoger@google.com            "based filenames, as rebaseline.py will use when downloading them from Google Storage");
14836f7f14d061b045a58a888ee5450b4596797dc453epoger@google.comDEFINE_string(writeJsonSummaryPath, "", "Write a JSON-formatted result summary to this file.");
148409fd4d216963c498c24ba355accce9337a5029a8scroggo@google.comDEFINE_string2(writePath, w, "",  "Write rendered images into this directory.");
1485604e0c249ef0e11a1b8bc41195081363fd86d892scroggo@google.comDEFINE_string2(writePicturePath, p, "", "Write .skp files into this directory.");
1486d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.comDEFINE_int32(pdfJpegQuality, -1, "Encodes images in JPEG at quality level N, "
1487d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com             "which can be in range 0-100). N = -1 will disable JPEG compression. "
1488d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com             "Default is N = 100, maximum quality.");
1489f4f9df4c193620167dc6f202f1b72245f4a260cdcommit-bot@chromium.org// TODO(edisonn): pass a matrix instead of forcePerspectiveMatrix
1490f4f9df4c193620167dc6f202f1b72245f4a260cdcommit-bot@chromium.org// Either the 9 numbers defining the matrix
1491f4f9df4c193620167dc6f202f1b72245f4a260cdcommit-bot@chromium.org// or probably more readable would be to replace it with a set of a few predicates
1492f4f9df4c193620167dc6f202f1b72245f4a260cdcommit-bot@chromium.org// Like --prerotate 100 200 10 --posttranslate 10, 10
1493f4f9df4c193620167dc6f202f1b72245f4a260cdcommit-bot@chromium.org// Probably define spacial names like centerx, centery, top, bottom, left, right
1494f4f9df4c193620167dc6f202f1b72245f4a260cdcommit-bot@chromium.org// then we can write something reabable like --rotate centerx centery 90
1495f4f9df4c193620167dc6f202f1b72245f4a260cdcommit-bot@chromium.orgDEFINE_bool(forcePerspectiveMatrix, false, "Force a perspective matrix.");
14965e00989a283111cef05bed8102e45c16651e43e4commit-bot@chromium.orgDEFINE_bool(useDocumentInsteadOfDevice, false, "Use SkDocument::CreateFoo instead of SkFooDevice.");
149773a7ea3ae0d37ee28f90d6b38c49fda052638253edisonn@google.comDEFINE_int32(pdfRasterDpi, 72, "Scale at which at which the non suported "
149873a7ea3ae0d37ee28f90d6b38c49fda052638253edisonn@google.com             "features in PDF are rasterized. Must be be in range 0-10000. "
149973a7ea3ae0d37ee28f90d6b38c49fda052638253edisonn@google.com             "Default is 72. N = 0 will disable rasterizing features like "
150073a7ea3ae0d37ee28f90d6b38c49fda052638253edisonn@google.com             "text shadows or perspective bitmaps.");
1501672588b684d484dce6ae251e9e163e4a46924322reed@google.comstatic SkData* encode_to_dct_data(size_t*, const SkBitmap& bitmap) {
1502d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com    // Filter output of warnings that JPEG is not available for the image.
1503608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org    if (bitmap.width() >= 65500 || bitmap.height() >= 65500) return NULL;
1504608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org    if (FLAGS_pdfJpegQuality == -1) return NULL;
1505d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com
1506608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org    SkBitmap bm = bitmap;
1507d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com#if defined(SK_BUILD_FOR_MAC)
1508d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com    // Workaround bug #1043 where bitmaps with referenced pixels cause
1509d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com    // CGImageDestinationFinalize to crash
1510d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com    SkBitmap copy;
1511d5f032d46baa42f836d06d7372fba8f022a20dfacommit-bot@chromium.org    bitmap.deepCopyTo(&copy);
1512608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org    bm = copy;
1513d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com#endif
1514d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com
1515608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org    SkPixelRef* pr = bm.pixelRef();
1516608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org    if (pr != NULL) {
1517608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org        SkData* data = pr->refEncodedData();
1518608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org        if (data != NULL) {
1519608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org            return data;
1520608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org        }
1521608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org    }
1522608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org
1523608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org    return SkImageEncoder::EncodeData(bm,
1524608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org                                      SkImageEncoder::kJPEG_Type,
1525608ea6508ae2e9ea05ea863ba50fc27d44d2eae9commit-bot@chromium.org                                      FLAGS_pdfJpegQuality);
1526d9dfa18372119c8e1318125d2075fa80e0819094edisonn@google.com}
15277d519305bb52eadf6b8265c6720642193791a364scroggo@google.com
15285867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.comstatic int findConfig(const char config[]) {
15295867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.com    for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
15305867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.com        if (!strcmp(config, gRec[i].fName)) {
153109fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com            return (int) i;
15325867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.com        }
15335867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.com    }
15345867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.com    return -1;
15355867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.com}
15365867c0f03262a3ce11c6790d5bd75ce0ebcb197cscroggo@google.com
15370dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.orgstatic const PDFRasterizerData* findPDFRasterizer(const char rasterizer[]) {
15380dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); i++) {
15390dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        if (!strcmp(rasterizer, kPDFRasterizers[i].fName)) {
15400dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            return &kPDFRasterizers[i];
15410dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        }
15420dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    }
15430dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    return NULL;
15440dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org}
15450dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
1546fb2cd425c2271c3d42c9667753b33ae71f05ef1breed@google.comtemplate <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
1547fb2cd425c2271c3d42c9667753b33ae71f05ef1breed@google.com    int index = array->find(value);
1548fb2cd425c2271c3d42c9667753b33ae71f05ef1breed@google.com    if (index < 0) {
1549fb2cd425c2271c3d42c9667753b33ae71f05ef1breed@google.com        *array->append() = value;
1550fb2cd425c2271c3d42c9667753b33ae71f05ef1breed@google.com    }
1551fb2cd425c2271c3d42c9667753b33ae71f05ef1breed@google.com}
1552fb2cd425c2271c3d42c9667753b33ae71f05ef1breed@google.com
155380724dfeb320d1152128cd33636c9024952432d3epoger@google.com/**
155480724dfeb320d1152128cd33636c9024952432d3epoger@google.com * Run this test in a number of different drawing modes (pipe,
155580724dfeb320d1152128cd33636c9024952432d3epoger@google.com * deferred, tiled, etc.), confirming that the resulting bitmaps all
155680724dfeb320d1152128cd33636c9024952432d3epoger@google.com * *exactly* match comparisonBitmap.
155780724dfeb320d1152128cd33636c9024952432d3epoger@google.com *
155880724dfeb320d1152128cd33636c9024952432d3epoger@google.com * Returns all errors encountered while doing so.
155980724dfeb320d1152128cd33636c9024952432d3epoger@google.com */
15606f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.comErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
15616f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                                    const SkBitmap &comparisonBitmap,
15626f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                                    const SkTDArray<SkScalar> &tileGridReplayScales);
15636f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.comErrorCombination run_multiple_modes(GMMain &gmmain, GM *gm, const ConfigData &compareConfig,
15646f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                                    const SkBitmap &comparisonBitmap,
15656f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com                                    const SkTDArray<SkScalar> &tileGridReplayScales) {
15666f6568b27eae62fea23ab8192c6da02ab892bb5eepoger@google.com    ErrorCombination errorsForAllModes;
156780724dfeb320d1152128cd33636c9024952432d3epoger@google.com    uint32_t gmFlags = gm->getFlags();
156838aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org    const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->getName(),
1569659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                                                                           compareConfig.fName);
1570c8263e704135436f71a585801966294d6deadeebepoger@google.com
1571c8263e704135436f71a585801966294d6deadeebepoger@google.com    SkPicture* pict = gmmain.generate_new_picture(gm, kNone_BbhType, 0);
1572c8263e704135436f71a585801966294d6deadeebepoger@google.com    SkAutoUnref aur(pict);
1573c8263e704135436f71a585801966294d6deadeebepoger@google.com    if (FLAGS_replay) {
1574c8263e704135436f71a585801966294d6deadeebepoger@google.com        const char renderModeDescriptor[] = "-replay";
1575c8263e704135436f71a585801966294d6deadeebepoger@google.com        if (gmFlags & GM::kSkipPicture_Flag) {
1576659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com            gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1577659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                                     renderModeDescriptor);
1578c8263e704135436f71a585801966294d6deadeebepoger@google.com            errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1579c8263e704135436f71a585801966294d6deadeebepoger@google.com        } else {
158080724dfeb320d1152128cd33636c9024952432d3epoger@google.com            SkBitmap bitmap;
158180724dfeb320d1152128cd33636c9024952432d3epoger@google.com            gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
1582c8263e704135436f71a585801966294d6deadeebepoger@google.com            errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
158338aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                gm->getName(), compareConfig.fName, renderModeDescriptor, bitmap,
1584659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                &comparisonBitmap));
158580724dfeb320d1152128cd33636c9024952432d3epoger@google.com        }
1586c8263e704135436f71a585801966294d6deadeebepoger@google.com    }
158780724dfeb320d1152128cd33636c9024952432d3epoger@google.com
1588c8263e704135436f71a585801966294d6deadeebepoger@google.com    if (FLAGS_serialize) {
1589c8263e704135436f71a585801966294d6deadeebepoger@google.com        const char renderModeDescriptor[] = "-serialize";
1590c8263e704135436f71a585801966294d6deadeebepoger@google.com        if (gmFlags & GM::kSkipPicture_Flag) {
1591659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com            gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1592659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                                     renderModeDescriptor);
1593c8263e704135436f71a585801966294d6deadeebepoger@google.com            errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1594c8263e704135436f71a585801966294d6deadeebepoger@google.com        } else {
159580724dfeb320d1152128cd33636c9024952432d3epoger@google.com            SkPicture* repict = gmmain.stream_to_new_picture(*pict);
159680724dfeb320d1152128cd33636c9024952432d3epoger@google.com            SkAutoUnref aurr(repict);
159780724dfeb320d1152128cd33636c9024952432d3epoger@google.com            SkBitmap bitmap;
159880724dfeb320d1152128cd33636c9024952432d3epoger@google.com            gmmain.generate_image_from_picture(gm, compareConfig, repict, &bitmap);
1599c8263e704135436f71a585801966294d6deadeebepoger@google.com            errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
160038aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                gm->getName(), compareConfig.fName, renderModeDescriptor, bitmap,
1601659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                &comparisonBitmap));
160280724dfeb320d1152128cd33636c9024952432d3epoger@google.com        }
160380724dfeb320d1152128cd33636c9024952432d3epoger@google.com    }
160480724dfeb320d1152128cd33636c9024952432d3epoger@google.com
1605c8263e704135436f71a585801966294d6deadeebepoger@google.com    if ((1 == FLAGS_writePicturePath.count()) &&
1606c8263e704135436f71a585801966294d6deadeebepoger@google.com        !(gmFlags & GM::kSkipPicture_Flag)) {
1607c8263e704135436f71a585801966294d6deadeebepoger@google.com        const char* pictureSuffix = "skp";
1608659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        // TODO(epoger): Make sure this still works even though the
1609659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        // filename now contains the config name (it used to contain
1610659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        // just the shortName).  I think this is actually an
1611659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        // *improvement*, because now runs with different configs will
1612659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        // write out their SkPictures to separate files rather than
1613659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        // overwriting each other.  But we should make sure it doesn't
1614659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com        // break anybody.
161538aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org        SkString path = gmmain.make_filename(FLAGS_writePicturePath[0], gm->getName(),
1616659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                                             compareConfig.fName, "", pictureSuffix);
1617c8263e704135436f71a585801966294d6deadeebepoger@google.com        SkFILEWStream stream(path.c_str());
1618c8263e704135436f71a585801966294d6deadeebepoger@google.com        pict->serialize(&stream);
161980724dfeb320d1152128cd33636c9024952432d3epoger@google.com    }
162080724dfeb320d1152128cd33636c9024952432d3epoger@google.com
1621c8263e704135436f71a585801966294d6deadeebepoger@google.com    if (FLAGS_rtree) {
1622c8263e704135436f71a585801966294d6deadeebepoger@google.com        const char renderModeDescriptor[] = "-rtree";
1623d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org        if ((gmFlags & GM::kSkipPicture_Flag) || (gmFlags & GM::kSkipTiled_Flag)) {
1624d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org            gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1625d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org                                     renderModeDescriptor);
1626d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org            errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1627d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org        } else {
1628d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org            SkPicture* pict = gmmain.generate_new_picture(gm, kRTree_BbhType, 0);
1629d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org            SkAutoUnref aur(pict);
1630d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org            SkBitmap bitmap;
1631d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org            gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap);
1632d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org            errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
1633d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org                gm->getName(), compareConfig.fName, renderModeDescriptor, bitmap,
1634d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org                &comparisonBitmap));
1635d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org        }
1636d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org    }
1637d393b17cf3427bd6f6255f8670067d9aa529e409commit-bot@chromium.org
1638c8263e704135436f71a585801966294d6deadeebepoger@google.com    if (FLAGS_tileGrid) {
1639c8263e704135436f71a585801966294d6deadeebepoger@google.com        for(int scaleIndex = 0; scaleIndex < tileGridReplayScales.count(); ++scaleIndex) {
1640c8263e704135436f71a585801966294d6deadeebepoger@google.com            SkScalar replayScale = tileGridReplayScales[scaleIndex];
1641c8263e704135436f71a585801966294d6deadeebepoger@google.com            SkString renderModeDescriptor("-tilegrid");
1642c8263e704135436f71a585801966294d6deadeebepoger@google.com            if (SK_Scalar1 != replayScale) {
1643c8263e704135436f71a585801966294d6deadeebepoger@google.com                renderModeDescriptor += "-scale-";
1644c8263e704135436f71a585801966294d6deadeebepoger@google.com                renderModeDescriptor.appendScalar(replayScale);
1645c8263e704135436f71a585801966294d6deadeebepoger@google.com            }
164680724dfeb320d1152128cd33636c9024952432d3epoger@google.com
1647c8263e704135436f71a585801966294d6deadeebepoger@google.com            if ((gmFlags & GM::kSkipPicture_Flag) ||
1648d4d44f08d73933dd64fffa75042aa19d45b772casenorblanco@chromium.org                (gmFlags & GM::kSkipTiled_Flag) ||
1649c8263e704135436f71a585801966294d6deadeebepoger@google.com                ((gmFlags & GM::kSkipScaledReplay_Flag) && replayScale != 1)) {
1650659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                gmmain.RecordTestResults(kIntentionallySkipped_ErrorType, shortNamePlusConfig,
1651c8263e704135436f71a585801966294d6deadeebepoger@google.com                                         renderModeDescriptor.c_str());
1652c8263e704135436f71a585801966294d6deadeebepoger@google.com                errorsForAllModes.add(kIntentionallySkipped_ErrorType);
1653c8263e704135436f71a585801966294d6deadeebepoger@google.com            } else {
1654c8263e704135436f71a585801966294d6deadeebepoger@google.com                // We record with the reciprocal scale to obtain a replay
1655c8263e704135436f71a585801966294d6deadeebepoger@google.com                // result that can be validated against comparisonBitmap.
1656c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkScalar recordScale = SkScalarInvert(replayScale);
1657c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkPicture* pict = gmmain.generate_new_picture(
16589f1c241e0d8a756fca1ec2dacb565eec83166d5frobertphillips                    gm, kTileGrid_BbhType, 0, recordScale);
1659c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkAutoUnref aur(pict);
1660c8263e704135436f71a585801966294d6deadeebepoger@google.com                SkBitmap bitmap;
1661c8263e704135436f71a585801966294d6deadeebepoger@google.com                // We cannot yet pass 'true' to generate_image_from_picture to
1662c8263e704135436f71a585801966294d6deadeebepoger@google.com                // perform actual tiled rendering (see Issue 1198 -
1663c8263e704135436f71a585801966294d6deadeebepoger@google.com                // https://code.google.com/p/skia/issues/detail?id=1198)
1664c8263e704135436f71a585801966294d6deadeebepoger@google.com                gmmain.generate_image_from_picture(gm, compareConfig, pict, &bitmap,
1665c8263e704135436f71a585801966294d6deadeebepoger@google.com                                                   replayScale /*, true */);
1666c8263e704135436f71a585801966294d6deadeebepoger@google.com                errorsForAllModes.add(gmmain.compare_test_results_to_reference_bitmap(
166738aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org                    gm->getName(), compareConfig.fName, renderModeDescriptor.c_str(), bitmap,
1668659c8c06cdc6c825004515240f81a8dafb2b4b0fepoger@google.com                    &comparisonBitmap));
1669c8263e704135436f71a585801966294d6deadeebepoger@google.com            }
167080724dfeb320d1152128cd33636c9024952432d3epoger@google.com        }
1671c8263e704135436f71a585801966294d6deadeebepoger@google.com    }
167280724dfeb320d1152128cd33636c9024952432d3epoger@google.com
1673c8263e704135436f71a585801966294d6deadeebepoger@google.com    // run the pipe centric GM steps
1674c8263e704135436f71a585801966294d6deadeebepoger@google.com    if (FLAGS_pipe) {
1675c8263e704135436f71a585801966294d6deadeebepoger@google.com        errorsForAllModes.add(gmmain.test_pipe_playback(gm, compareConfig, comparisonBitmap,
1676c8263e704135436f71a585801966294d6deadeebepoger@google.com                                                        FLAGS_simulatePipePlaybackFailure));
1677c8263e704135436f71a585801966294d6deadeebepoger@google.com        if (FLAGS_tiledPipe) {
1678c8263e704135436f71a585801966294d6deadeebepoger@google.com            errorsForAllModes.add(gmmain.test_tiled_pipe_playback(gm, compareConfig,
1679c8263e704135436f71a585801966294d6deadeebepoger@google.com                                                                  comparisonBitmap));
168080724dfeb320d1152128cd33636c9024952432d3epoger@google.com        }
168180724dfeb320d1152128cd33636c9024952432d3epoger@google.com    }
168280724dfeb320d1152128cd33636c9024952432d3epoger@google.com    return errorsForAllModes;
168380724dfeb320d1152128cd33636c9024952432d3epoger@google.com}
168480724dfeb320d1152128cd33636c9024952432d3epoger@google.com
168585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
168685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org/**
168785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org * Run this test in a number of different configs (8888, 565, PDF,
168885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org * etc.), confirming that the resulting bitmaps match expectations
168985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org * (which may be different for each config).
169085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org *
169185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org * Returns all errors encountered while doing so.
169285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org */
169385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.orgErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm,
169485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                      const SkTDArray<size_t> &configs,
169585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                      const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
169685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                      const SkTDArray<SkScalar> &tileGridReplayScales,
169780549fcdd50269d7e069d6db02b395fca128056ckkinnunen                                      GrContextFactory *grFactory,
169880549fcdd50269d7e069d6db02b395fca128056ckkinnunen                                      GrGLStandard gpuAPI);
169985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.orgErrorCombination run_multiple_configs(GMMain &gmmain, GM *gm,
170085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                      const SkTDArray<size_t> &configs,
170185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                      const SkTDArray<const PDFRasterizerData*> &pdfRasterizers,
170285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                      const SkTDArray<SkScalar> &tileGridReplayScales,
170380549fcdd50269d7e069d6db02b395fca128056ckkinnunen                                      GrContextFactory *grFactory,
170480549fcdd50269d7e069d6db02b395fca128056ckkinnunen                                      GrGLStandard gpuAPI) {
170585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org    const char renderModeDescriptor[] = "";
170685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org    ErrorCombination errorsForAllConfigs;
170785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org    uint32_t gmFlags = gm->getFlags();
170885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
170985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org    for (int i = 0; i < configs.count(); i++) {
171085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        ConfigData config = gRec[configs[i]];
171138aeb0fd7a2bdab5e44531d96045dffe25c8e2b0commit-bot@chromium.org        const SkString shortNamePlusConfig = gmmain.make_shortname_plus_config(gm->getName(),
171285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                                                               config.fName);
171385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
171485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        // Skip any tests that we don't even need to try.
171585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        // If any of these were skipped on a per-GM basis, record them as
171685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        // kIntentionallySkipped.
171785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        if (kPDF_Backend == config.fBackend) {
171885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            if (gmFlags & GM::kSkipPDF_Flag) {
171985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                gmmain.RecordSkippedTest(shortNamePlusConfig,
172085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                         renderModeDescriptor,
172185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                         config.fBackend);
172285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
172385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                continue;
172485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            }
172585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        }
172685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        if ((gmFlags & GM::kSkip565_Flag) &&
172785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            (kRaster_Backend == config.fBackend) &&
1728ddd014e65264e3f18b2d4305dd2fbc81549f5180reed            (kRGB_565_SkColorType == config.fColorType)) {
172985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            gmmain.RecordSkippedTest(shortNamePlusConfig,
173085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                     renderModeDescriptor,
173185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                     config.fBackend);
173285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
173385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            continue;
173485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        }
173585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        if (((gmFlags & GM::kSkipGPU_Flag) && kGPU_Backend == config.fBackend) ||
173685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            ((gmFlags & GM::kGPUOnly_Flag) && kGPU_Backend != config.fBackend)) {
173785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            gmmain.RecordSkippedTest(shortNamePlusConfig,
173885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                     renderModeDescriptor,
173985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                     config.fBackend);
174085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            errorsForAllConfigs.add(kIntentionallySkipped_ErrorType);
174185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            continue;
174285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        }
174385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
174485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        // Now we know that we want to run this test and record its
174585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        // success or failure.
174685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        ErrorCombination errorsForThisConfig;
174785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        GrSurface* gpuTarget = NULL;
174885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org#if SK_SUPPORT_GPU
174985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        SkAutoTUnref<GrSurface> auGpuTarget;
175085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
175185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            if (FLAGS_resetGpuContext) {
175285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                grFactory->destroyContexts();
175385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            }
175480549fcdd50269d7e069d6db02b395fca128056ckkinnunen            GrContext* gr = grFactory->get(config.fGLContextType, gpuAPI);
175585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            bool grSuccess = false;
175685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            if (gr) {
175785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                // create a render target to back the device
175885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                GrTextureDesc desc;
175985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                desc.fConfig = kSkia8888_GrPixelConfig;
176085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                desc.fFlags = kRenderTarget_GrTextureFlagBit;
176185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                desc.fWidth = gm->getISize().width();
176285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                desc.fHeight = gm->getISize().height();
176385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                desc.fSampleCnt = config.fSampleCnt;
176485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                auGpuTarget.reset(gr->createUncachedTexture(desc, NULL, 0));
176549f085dddff10473b6ebf832a974288300224e60bsalomon                if (auGpuTarget) {
176685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                    gpuTarget = auGpuTarget;
176785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                    grSuccess = true;
176885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                    // Set the user specified cache limits if non-default.
176985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                    size_t bytes;
177085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                    int count;
177195c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org                    gr->getResourceCacheLimits(&count, &bytes);
177285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                    if (DEFAULT_CACHE_VALUE != gGpuCacheSizeBytes) {
177385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                        bytes = static_cast<size_t>(gGpuCacheSizeBytes);
177485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                    }
177585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                    if (DEFAULT_CACHE_VALUE != gGpuCacheSizeCount) {
177685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                        count = gGpuCacheSizeCount;
177785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                    }
177895c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org                    gr->setResourceCacheLimits(count, bytes);
177985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                }
178085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            }
178185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            if (!grSuccess) {
178285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                errorsForThisConfig.add(kNoGpuContext_ErrorType);
178385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            }
178485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        }
178585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org#endif
178685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
178785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        SkBitmap comparisonBitmap;
178885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
178985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        const char* writePath;
179085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        if (FLAGS_writePath.count() == 1) {
179185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            writePath = FLAGS_writePath[0];
179285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        } else {
179385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            writePath = NULL;
179485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        }
179585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
179685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        if (errorsForThisConfig.isEmpty()) {
179785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            errorsForThisConfig.add(gmmain.test_drawing(gm, config, pdfRasterizers,
179885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                                        writePath, gpuTarget,
179985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                                        &comparisonBitmap));
180085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            gmmain.RecordTestResults(errorsForThisConfig, shortNamePlusConfig, "");
180185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        }
180285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
180385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        // TODO: run only if gmmain.test_drawing succeeded.
180485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        if (kRaster_Backend == config.fBackend) {
180585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            run_multiple_modes(gmmain, gm, config, comparisonBitmap, tileGridReplayScales);
180685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        }
180785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
180885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        if (FLAGS_deferred && errorsForThisConfig.isEmpty() &&
180985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            (kGPU_Backend == config.fBackend || kRaster_Backend == config.fBackend)) {
181085195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org            errorsForThisConfig.add(gmmain.test_deferred_drawing(gm, config, comparisonBitmap,
181185195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org                                                                 gpuTarget));
181285195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        }
181385195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
181485195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org        errorsForAllConfigs.add(errorsForThisConfig);
181585195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org    }
181685195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org    return errorsForAllConfigs;
181785195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org}
181885195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
181985195486939b19a88b25b3ba0a1ad6640980fb5acommit-bot@chromium.org
1820310478e72c63e639373465216271b81f1e4a9136epoger@google.com/**
1821f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com * Read individual lines from a file, pushing them into the given array.
1822f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com *
1823f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com * @param filename path to the file to read
1824f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com * @param lines array of strings to add the lines to
1825f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com * @returns true if able to read lines from the file
1826f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com */
1827f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.comstatic bool read_lines_from_file(const char* filename, SkTArray<SkString> &lines) {
1828f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    SkAutoTUnref<SkStream> streamWrapper(SkStream::NewFromFile(filename));
1829f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    SkStream *stream = streamWrapper.get();
1830f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    if (!stream) {
18312b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org        SkDebugf("unable to read file '%s'\n", filename);
1832f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        return false;
1833f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    }
1834f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com
1835f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    char c;
1836f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    SkString line;
1837f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    while (1 == stream->read(&c, 1)) {
1838f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        // If we hit either CR or LF, we've completed a line.
1839f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        //
1840f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        // TODO: If the file uses both CR and LF, this will return an extra blank
1841f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        // line for each line of the file.  Which is OK for current purposes...
1842f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        //
1843f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        // TODO: Does this properly handle unicode?  It doesn't matter for
1844f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        // current purposes...
1845f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        if ((c == 0x0d) || (c == 0x0a)) {
1846f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com            lines.push_back(line);
1847f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com            line.reset();
1848f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        } else {
1849f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com            line.append(&c, 1);
1850f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        }
1851f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    }
1852f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    lines.push_back(line);
1853f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    return true;
1854f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com}
1855f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com
1856f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com/**
1857310478e72c63e639373465216271b81f1e4a9136epoger@google.com * Return a list of all entries in an array of strings as a single string
1858310478e72c63e639373465216271b81f1e4a9136epoger@google.com * of this form:
1859310478e72c63e639373465216271b81f1e4a9136epoger@google.com * "item1", "item2", "item3"
1860310478e72c63e639373465216271b81f1e4a9136epoger@google.com */
1861310478e72c63e639373465216271b81f1e4a9136epoger@google.comSkString list_all(const SkTArray<SkString> &stringArray);
1862310478e72c63e639373465216271b81f1e4a9136epoger@google.comSkString list_all(const SkTArray<SkString> &stringArray) {
1863310478e72c63e639373465216271b81f1e4a9136epoger@google.com    SkString total;
1864310478e72c63e639373465216271b81f1e4a9136epoger@google.com    for (int i = 0; i < stringArray.count(); i++) {
1865310478e72c63e639373465216271b81f1e4a9136epoger@google.com        if (i > 0) {
1866310478e72c63e639373465216271b81f1e4a9136epoger@google.com            total.append(", ");
1867310478e72c63e639373465216271b81f1e4a9136epoger@google.com        }
1868310478e72c63e639373465216271b81f1e4a9136epoger@google.com        total.append("\"");
1869310478e72c63e639373465216271b81f1e4a9136epoger@google.com        total.append(stringArray[i]);
1870310478e72c63e639373465216271b81f1e4a9136epoger@google.com        total.append("\"");
1871310478e72c63e639373465216271b81f1e4a9136epoger@google.com    }
1872310478e72c63e639373465216271b81f1e4a9136epoger@google.com    return total;
1873310478e72c63e639373465216271b81f1e4a9136epoger@google.com}
1874310478e72c63e639373465216271b81f1e4a9136epoger@google.com
1875310478e72c63e639373465216271b81f1e4a9136epoger@google.com/**
1876310478e72c63e639373465216271b81f1e4a9136epoger@google.com * Return a list of configuration names, as a single string of this form:
1877310478e72c63e639373465216271b81f1e4a9136epoger@google.com * "item1", "item2", "item3"
1878310478e72c63e639373465216271b81f1e4a9136epoger@google.com *
1879310478e72c63e639373465216271b81f1e4a9136epoger@google.com * @param configs configurations, as a list of indices into gRec
1880310478e72c63e639373465216271b81f1e4a9136epoger@google.com */
1881310478e72c63e639373465216271b81f1e4a9136epoger@google.comSkString list_all_config_names(const SkTDArray<size_t> &configs);
1882310478e72c63e639373465216271b81f1e4a9136epoger@google.comSkString list_all_config_names(const SkTDArray<size_t> &configs) {
1883310478e72c63e639373465216271b81f1e4a9136epoger@google.com    SkString total;
1884310478e72c63e639373465216271b81f1e4a9136epoger@google.com    for (int i = 0; i < configs.count(); i++) {
1885310478e72c63e639373465216271b81f1e4a9136epoger@google.com        if (i > 0) {
1886310478e72c63e639373465216271b81f1e4a9136epoger@google.com            total.append(", ");
1887310478e72c63e639373465216271b81f1e4a9136epoger@google.com        }
1888310478e72c63e639373465216271b81f1e4a9136epoger@google.com        total.append("\"");
1889310478e72c63e639373465216271b81f1e4a9136epoger@google.com        total.append(gRec[configs[i]].fName);
1890310478e72c63e639373465216271b81f1e4a9136epoger@google.com        total.append("\"");
1891310478e72c63e639373465216271b81f1e4a9136epoger@google.com    }
1892310478e72c63e639373465216271b81f1e4a9136epoger@google.com    return total;
1893310478e72c63e639373465216271b81f1e4a9136epoger@google.com}
1894310478e72c63e639373465216271b81f1e4a9136epoger@google.com
18950dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.orgstatic bool prepare_subdirectories(const char *root, bool useFileHierarchy,
18960dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                                   const SkTDArray<size_t> &configs,
18970dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                                   const SkTDArray<const PDFRasterizerData*>& pdfRasterizers) {
1898fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com    if (!sk_mkdir(root)) {
1899fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com        return false;
1900fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com    }
1901fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com    if (useFileHierarchy) {
1902fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com        for (int i = 0; i < configs.count(); i++) {
1903fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com            ConfigData config = gRec[configs[i]];
1904fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com            SkString subdir;
1905fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com            subdir.appendf("%s%c%s", root, SkPATH_SEPARATOR, config.fName);
1906fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com            if (!sk_mkdir(subdir.c_str())) {
1907fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com                return false;
1908fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com            }
19090dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
19100dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            if (config.fBackend == kPDF_Backend) {
19110dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                for (int j = 0; j < pdfRasterizers.count(); j++) {
19120dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                    SkString pdfSubdir = subdir;
19138fc3766c58ec879389c3c83c96a73e50ff59eb78vandebo@chromium.org                    pdfSubdir.appendf("-%s", pdfRasterizers[j]->fName);
19140dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                    if (!sk_mkdir(pdfSubdir.c_str())) {
19150dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                        return false;
19160dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                    }
19170dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                }
19180dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            }
1919fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com        }
1920fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com    }
1921fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com    return true;
1922fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com}
1923fdea3259d01572fdc09fec5ae84258a6e162da65epoger@google.com
1924ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.orgstatic bool parse_flags_configs(SkTDArray<size_t>* outConfigs,
192580549fcdd50269d7e069d6db02b395fca128056ckkinnunen                         GrContextFactory* grFactory, GrGLStandard gpuAPI) {
1926fb2cd425c2271c3d42c9667753b33ae71f05ef1breed@google.com    SkTDArray<size_t> excludeConfigs;
192709fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com
192809fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com    for (int i = 0; i < FLAGS_config.count(); i++) {
1929dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com        const char* config = FLAGS_config[i];
1930dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com        bool exclude = false;
1931dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com        if (*config == kExcludeConfigChar) {
1932dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            exclude = true;
1933dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            config += 1;
1934dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com        }
1935dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com        int index = findConfig(config);
193609fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com        if (index >= 0) {
1937dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            if (exclude) {
1938dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                *excludeConfigs.append() = index;
1939dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            } else {
1940ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                appendUnique<size_t>(outConfigs, index);
1941dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            }
1942dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com        } else if (0 == strcmp(kDefaultsConfigStr, config)) {
1943ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            if (exclude) {
19442b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                SkDebugf("%c%s is not allowed.\n",
19452b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                         kExcludeConfigChar, kDefaultsConfigStr);
1946ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                return false;
1947ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            }
1948dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            for (size_t c = 0; c < SK_ARRAY_COUNT(gRec); ++c) {
1949dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                if (gRec[c].fRunByDefault) {
1950ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                    appendUnique<size_t>(outConfigs, c);
1951dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com                }
1952dd8e353bef35b2656b83499289b409af3b0e3f10bsalomon@google.com            }
195309fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com        } else {
19542b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org            SkDebugf("unrecognized config %s\n", config);
1955ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            return false;
195609fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com        }
195709fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com    }
19587d519305bb52eadf6b8265c6720642193791a364scroggo@google.com
195909fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com    for (int i = 0; i < FLAGS_excludeConfig.count(); i++) {
196009fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com        int index = findConfig(FLAGS_excludeConfig[i]);
196109fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com        if (index >= 0) {
196209fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com            *excludeConfigs.append() = index;
19630b735631b7d22316693629a4110c7e95b2a7633escroggo@google.com        } else {
19642b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org            SkDebugf("unrecognized excludeConfig %s\n", FLAGS_excludeConfig[i]);
1965ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            return false;
196609fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com        }
196709fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com    }
196809fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com
1969ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    if (outConfigs->count() == 0) {
1970ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        // if no config is specified by user, add the defaults
1971ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1972ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            if (gRec[i].fRunByDefault) {
1973ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                *outConfigs->append() = i;
1974ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            }
1975ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        }
1976ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    }
1977ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    // now remove any explicitly excluded configs
1978ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    for (int i = 0; i < excludeConfigs.count(); ++i) {
1979ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        int index = outConfigs->find(excludeConfigs[i]);
1980ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        if (index >= 0) {
1981ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            outConfigs->remove(index);
1982ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            // now assert that there was only one copy in configs[]
1983ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            SkASSERT(outConfigs->find(excludeConfigs[i]) < 0);
1984ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        }
1985ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    }
1986fb2cd425c2271c3d42c9667753b33ae71f05ef1breed@google.com
1987ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    for (int i = 0; i < outConfigs->count(); ++i) {
1988ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        size_t index = (*outConfigs)[i];
1989ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        if (kGPU_Backend == gRec[index].fBackend) {
199069456186f723b260be6cfa2ac2aa245f860841cbbsalomon#if SK_SUPPORT_GPU
199169456186f723b260be6cfa2ac2aa245f860841cbbsalomon            if (!FLAGS_gpu) {
199269456186f723b260be6cfa2ac2aa245f860841cbbsalomon                outConfigs->remove(i);
199369456186f723b260be6cfa2ac2aa245f860841cbbsalomon                --i;
199469456186f723b260be6cfa2ac2aa245f860841cbbsalomon                continue;
199569456186f723b260be6cfa2ac2aa245f860841cbbsalomon            }
199669456186f723b260be6cfa2ac2aa245f860841cbbsalomon#endif
199769456186f723b260be6cfa2ac2aa245f860841cbbsalomon        } else if (!FLAGS_cpu) {
199869456186f723b260be6cfa2ac2aa245f860841cbbsalomon            outConfigs->remove(i);
199969456186f723b260be6cfa2ac2aa245f860841cbbsalomon            --i;
200069456186f723b260be6cfa2ac2aa245f860841cbbsalomon            continue;
200169456186f723b260be6cfa2ac2aa245f860841cbbsalomon        }
200269456186f723b260be6cfa2ac2aa245f860841cbbsalomon#if SK_SUPPORT_GPU
200369456186f723b260be6cfa2ac2aa245f860841cbbsalomon        SkASSERT(grFactory != NULL);
200469456186f723b260be6cfa2ac2aa245f860841cbbsalomon        if (kGPU_Backend == gRec[index].fBackend) {
200580549fcdd50269d7e069d6db02b395fca128056ckkinnunen            GrContext* ctx = grFactory->get(gRec[index].fGLContextType, gpuAPI);
2006ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            if (NULL == ctx) {
20072b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                SkDebugf("GrContext could not be created for config %s. Config will be skipped.\n",
20082b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                         gRec[index].fName);
2009ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                outConfigs->remove(i);
2010ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                --i;
2011ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                continue;
2012ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            }
2013ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            if (gRec[index].fSampleCnt > ctx->getMaxSampleCount()) {
20142b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                SkDebugf("Sample count (%d) of config %s is not supported."
20152b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                         " Config will be skipped.\n",
20162b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                         gRec[index].fSampleCnt, gRec[index].fName);
2017ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                outConfigs->remove(i);
2018ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                --i;
2019ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            }
20204c75f241cb75f202cfa51569ba8dc4749a28a9f7bsalomon@google.com        }
2021ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org#endif
202269456186f723b260be6cfa2ac2aa245f860841cbbsalomon    }
20234c75f241cb75f202cfa51569ba8dc4749a28a9f7bsalomon@google.com
2024ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    if (outConfigs->isEmpty()) {
20252b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org        SkDebugf("No configs to run.");
2026ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        return false;
2027ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    }
2028ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org
2029ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    // now show the user the set of configs that will be run.
2030ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    SkString configStr("These configs will be run:");
2031ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    // show the user the config that will run.
2032ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    for (int i = 0; i < outConfigs->count(); ++i) {
2033ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        configStr.appendf(" %s", gRec[(*outConfigs)[i]].fName);
2034ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    }
20352b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org    SkDebugf("%s\n", configStr.c_str());
2036ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org
2037ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    return true;
2038ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org}
2039ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org
20400dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.orgstatic bool parse_flags_pdf_rasterizers(const SkTDArray<size_t>& configs,
20410dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                                        SkTDArray<const PDFRasterizerData*>* outRasterizers) {
20420dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    // No need to run this check (and display the PDF rasterizers message)
20430dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    // if no PDF backends are in the configs.
20440dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    bool configHasPDF = false;
20450dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    for (int i = 0; i < configs.count(); i++) {
20460dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        if (gRec[configs[i]].fBackend == kPDF_Backend) {
20470dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            configHasPDF = true;
20480dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            break;
20490dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        }
20500dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    }
20510dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    if (!configHasPDF) {
20520dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        return true;
20530dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    }
20540dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
2055f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org    if (FLAGS_pdfRasterizers.count() == 1 &&
2056f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org            !strcmp(FLAGS_pdfRasterizers[0], "default")) {
20570dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        for (int i = 0; i < (int)SK_ARRAY_COUNT(kPDFRasterizers); ++i) {
20580dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            if (kPDFRasterizers[i].fRunByDefault) {
20590dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org                *outRasterizers->append() = &kPDFRasterizers[i];
20600dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org            }
20610dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        }
2062f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org    } else {
2063f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org        for (int i = 0; i < FLAGS_pdfRasterizers.count(); i++) {
2064f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org            const char* rasterizer = FLAGS_pdfRasterizers[i];
2065f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org            const PDFRasterizerData* rasterizerPtr =
2066f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org                    findPDFRasterizer(rasterizer);
2067f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org            if (rasterizerPtr == NULL) {
20682b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                SkDebugf("unrecognized rasterizer %s\n", rasterizer);
2069f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org                return false;
2070f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org            }
2071f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org            appendUnique<const PDFRasterizerData*>(outRasterizers,
2072f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org                                                   rasterizerPtr);
2073f8afb2bd5853f9d66448ba1f23251f8c93340bf6vandebo@chromium.org        }
20740dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    }
20750dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
20760dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    // now show the user the set of configs that will be run.
20770dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    SkString configStr("These PDF rasterizers will be run:");
20780dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    // show the user the config that will run.
20790dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    for (int i = 0; i < outRasterizers->count(); ++i) {
20800dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org        configStr.appendf(" %s", (*outRasterizers)[i]->fName);
20810dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    }
20822b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org    SkDebugf("%s\n", configStr.c_str());
20830dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
20840dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org    return true;
20850dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org}
20860dcbece326ce2fa1f9046aa69a21bb99de08714avandebo@chromium.org
2087ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.orgstatic bool parse_flags_ignore_error_types(ErrorCombination* outErrorTypes) {
20889677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org    if (FLAGS_ignoreErrorTypes.count() > 0) {
2089ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        *outErrorTypes = ErrorCombination();
20909677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org        for (int i = 0; i < FLAGS_ignoreErrorTypes.count(); i++) {
20919677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org            ErrorType type;
20929677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org            const char *name = FLAGS_ignoreErrorTypes[i];
20939677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org            if (!getErrorTypeByName(name, &type)) {
20942b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                SkDebugf("cannot find ErrorType with name '%s'\n", name);
2095ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                return false;
20969677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org            } else {
2097ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                outErrorTypes->add(type);
20989677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org            }
20999677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org        }
21009677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org    }
2101ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    return true;
2102ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org}
2103ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org
21043a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com/**
21053e62ebf93b43139202ce803245411467c7ed13ffcommit-bot@chromium.org * Replace contents of ignoreTestNames with a list of test names, indicating
21063a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com * which tests' failures should be ignored.
21073a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com */
21083e62ebf93b43139202ce803245411467c7ed13ffcommit-bot@chromium.orgstatic bool parse_flags_ignore_tests(SkTArray<SkString> &ignoreTestNames) {
21093e62ebf93b43139202ce803245411467c7ed13ffcommit-bot@chromium.org    ignoreTestNames.reset();
2110f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com
2111f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    // Parse --ignoreFailuresFile
2112f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    for (int i = 0; i < FLAGS_ignoreFailuresFile.count(); i++) {
2113f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        SkTArray<SkString> linesFromFile;
2114f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        if (!read_lines_from_file(FLAGS_ignoreFailuresFile[i], linesFromFile)) {
2115f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com            return false;
2116f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        } else {
2117f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com            for (int j = 0; j < linesFromFile.count(); j++) {
2118f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com                SkString thisLine = linesFromFile[j];
2119f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com                if (thisLine.isEmpty() || thisLine.startsWith('#')) {
2120f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com                    // skip this line
2121f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com                } else {
21223e62ebf93b43139202ce803245411467c7ed13ffcommit-bot@chromium.org                    ignoreTestNames.push_back(thisLine);
2123f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com                }
2124f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com            }
2125f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com        }
2126f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com    }
2127f711f320a34216fc9f64839b90b99a1492c54e09epoger@google.com
21283a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com    return true;
21293a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com}
21303a882dda0787e6957cfb37f6b5a68bcd31ce5cf7epoger@google.com
2131ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.orgstatic bool parse_flags_modulo(int* moduloRemainder, int* moduloDivisor) {
2132ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    if (FLAGS_modulo.count() == 2) {
2133ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        *moduloRemainder = atoi(FLAGS_modulo[0]);
2134ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        *moduloDivisor = atoi(FLAGS_modulo[1]);
2135ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        if (*moduloRemainder < 0 || *moduloDivisor <= 0 ||
2136ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                *moduloRemainder >= *moduloDivisor) {
21372b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org            SkDebugf("invalid modulo values.");
2138ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            return false;
2139ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        }
2140ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    }
2141ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    return true;
2142ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org}
21439677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org
21449677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org#if SK_SUPPORT_GPU
2145ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.orgstatic bool parse_flags_gpu_cache(int* sizeBytes, int* sizeCount) {
21469677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org    if (FLAGS_gpuCacheSize.count() > 0) {
21479677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org        if (FLAGS_gpuCacheSize.count() != 2) {
21482b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org            SkDebugf("--gpuCacheSize requires two arguments\n");
2149ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            return false;
21509677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org        }
2151ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        *sizeBytes = atoi(FLAGS_gpuCacheSize[0]);
2152ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        *sizeCount = atoi(FLAGS_gpuCacheSize[1]);
21539677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org    } else {
2154ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        *sizeBytes = DEFAULT_CACHE_VALUE;
2155ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        *sizeCount = DEFAULT_CACHE_VALUE;
21569677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org    }
2157ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    return true;
2158ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org}
215980549fcdd50269d7e069d6db02b395fca128056ckkinnunen
216080549fcdd50269d7e069d6db02b395fca128056ckkinnunenstatic bool parse_flags_gl_standard(GrGLStandard* gpuAPI) {
216180549fcdd50269d7e069d6db02b395fca128056ckkinnunen    if (0 == FLAGS_gpuAPI.count()) {
216280549fcdd50269d7e069d6db02b395fca128056ckkinnunen        *gpuAPI = kNone_GrGLStandard;
216380549fcdd50269d7e069d6db02b395fca128056ckkinnunen        return true;
216480549fcdd50269d7e069d6db02b395fca128056ckkinnunen    }
216580549fcdd50269d7e069d6db02b395fca128056ckkinnunen    if (1 == FLAGS_gpuAPI.count()) {
216680549fcdd50269d7e069d6db02b395fca128056ckkinnunen        if (FLAGS_gpuAPI.contains(kGpuAPINameGL)) {
216780549fcdd50269d7e069d6db02b395fca128056ckkinnunen            *gpuAPI = kGL_GrGLStandard;
216880549fcdd50269d7e069d6db02b395fca128056ckkinnunen            return true;
216980549fcdd50269d7e069d6db02b395fca128056ckkinnunen        }
217080549fcdd50269d7e069d6db02b395fca128056ckkinnunen        if (FLAGS_gpuAPI.contains(kGpuAPINameGLES)) {
217180549fcdd50269d7e069d6db02b395fca128056ckkinnunen            *gpuAPI = kGLES_GrGLStandard;
217280549fcdd50269d7e069d6db02b395fca128056ckkinnunen            return true;
217380549fcdd50269d7e069d6db02b395fca128056ckkinnunen        }
217480549fcdd50269d7e069d6db02b395fca128056ckkinnunen    }
217580549fcdd50269d7e069d6db02b395fca128056ckkinnunen    SkDebugf("--gpuAPI invalid api value");
217680549fcdd50269d7e069d6db02b395fca128056ckkinnunen    return false;
217780549fcdd50269d7e069d6db02b395fca128056ckkinnunen}
21789677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org#endif
21799677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org
2180ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.orgstatic bool parse_flags_tile_grid_replay_scales(SkTDArray<SkScalar>* outScales) {
2181ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    *outScales->append() = SK_Scalar1; // By default only test at scale 1.0
21829677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org    if (FLAGS_tileGridReplayScales.count() > 0) {
2183ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        outScales->reset();
21849677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org        for (int i = 0; i < FLAGS_tileGridReplayScales.count(); i++) {
21859677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org            double val = atof(FLAGS_tileGridReplayScales[i]);
21869677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org            if (0 < val) {
2187ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org                *outScales->append() = SkDoubleToScalar(val);
21889677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org            }
21899677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org        }
2190ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        if (0 == outScales->count()) {
21919677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org            // Should have at least one scale
21922b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org            SkDebugf("--tileGridReplayScales requires at least one scale.\n");
2193ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            return false;
2194c5205629da1e6e4aca83a2a1b3ce08f4bee360fdscroggo@google.com        }
2195c5205629da1e6e4aca83a2a1b3ce08f4bee360fdscroggo@google.com    }
2196ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    return true;
2197ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org}
21989677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org
2199ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.orgstatic bool parse_flags_gmmain_paths(GMMain* gmmain) {
2200ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    gmmain->fUseFileHierarchy = FLAGS_hierarchy;
2201ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    gmmain->fWriteChecksumBasedFilenames = FLAGS_writeChecksumBasedFilenames;
22029677a84b4055ab084b2aae6adeab31ab1c7a95b7commit-bot@chromium.org
2203ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    if (FLAGS_mismatchPath.count() == 1) {
2204ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        gmmain->fMismatchPath = FLAGS_mismatchPath[0];
2205c5205629da1e6e4aca83a2a1b3ce08f4bee360fdscroggo@google.com    }
2206c5205629da1e6e4aca83a2a1b3ce08f4bee360fdscroggo@google.com
2207ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    if (FLAGS_missingExpectationsPath.count() == 1) {
2208ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        gmmain->fMissingExpectationsPath = FLAGS_missingExpectationsPath[0];
220909fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com    }
22108570b5c8695052378491b0c61e745d736fe85c8drobertphillips@google.com
221109fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com    if (FLAGS_readPath.count() == 1) {
221209fd4d216963c498c24ba355accce9337a5029a8scroggo@google.com        const char* readPath = FLAGS_readPath[0];
221337269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        if (!sk_exists(readPath)) {
22142b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org            SkDebugf("readPath %s does not exist!\n", readPath);
2215ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            return false;
221637269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        }
221737269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        if (sk_isdir(readPath)) {
2218b0f8b430de74a51f35dbfbff5284a04f8d70ec48epoger@google.com            if (FLAGS_verbose) {
22192b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                SkDebugf("reading from %s\n", readPath);
2220b0f8b430de74a51f35dbfbff5284a04f8d70ec48epoger@google.com            }
2221ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            gmmain->fExpectationsSource.reset(SkNEW_ARGS(
2222b0f8b430de74a51f35dbfbff5284a04f8d70ec48epoger@google.com                IndividualImageExpectationsSource, (readPath)));
222337269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        } else {
2224b0f8b430de74a51f35dbfbff5284a04f8d70ec48epoger@google.com            if (FLAGS_verbose) {
22252b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org                SkDebugf("reading expectations from JSON summary file %s\n", readPath);
2226b0f8b430de74a51f35dbfbff5284a04f8d70ec48epoger@google.com            }
2227ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org            gmmain->fExpectationsSource.reset(SkNEW_ARGS(JsonExpectationsSource, (readPath)));
222837269607334b99bf814c7dc6b426745d9b7c7e3fepoger@google.com        }
2229f32f6e8323d201ede639ba66e1614783428a166dchudy@google.com    }
2230ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    return true;
2231ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org}
2232ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org
2233ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.orgstatic bool parse_flags_jpeg_quality() {
2234ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    if (FLAGS_pdfJpegQuality < -1 || FLAGS_pdfJpegQuality > 100) {
22352b3a204bf68c9e41e62e0f2db5dfe263ea811424commit-bot@chromium.org        SkDebugf("%s\n", "pdfJpegQuality must be in [-1 .. 100] range.");
2236ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org        return false;
2237ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    }
2238ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org    return true;
2239ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org}
2240ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org
2241ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.orgint tool_main(int argc, char** argv);
2242ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.orgint tool_main(int argc, char** argv) {
224330e6e2af14e84216b1c113fd7500d0822bc81daamtklein    SetupCrashHandler();
2244ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org
22456dda827913a3dbdb84934f4f1d79c0b702169e5ccommit-bot@chromium.org    SkString usage;
22466dda827913a3dbdb84934f4f1d79c0b702169e5ccommit-bot@chromium.org    usage.printf("Run the golden master tests.\n");
22476dda827913a3dbdb84934f4f1d79c0b702169e5ccommit-bot@chromium.org    SkCommandLineFlags::SetUsage(usage.c_str());
22486dda827913a3dbdb84934f4f1d79c0b702169e5ccommit-bot@chromium.org    SkCommandLineFlags::Parse(argc, argv);
22496dda827913a3dbdb84934f4f1d79c0b702169e5ccommit-bot@chromium.org
2250ab882bf205affcd29f62f764681bfdf09fde004ecommit-bot@chromium.org#if SK_ENABLE_INST_COUNT
22516dda827913a3dbdb84934f4f1d79c0b702169e5ccommit-bot@chromium.org    if (FLAGS_leaks) {
22526dda827913a3dbdb84934f4f1d79c0b702169e5ccommit-bot@chromium.org        gPrintInstCount = true;
22536dda827913a3dbdb84934f4f1d79c0b702169e5ccommit-bot@chromium.org    }
2254