1/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SK_COMMON_FLAGS_CONFIG_H
9#define SK_COMMON_FLAGS_CONFIG_H
10
11#include "tools/flags/CommandLineFlags.h"
12#include "tools/gpu/GrContextFactory.h"
13
14DECLARE_string(config);
15
16class SkCommandLineConfigGpu;
17class SkCommandLineConfigGraphite;
18class SkCommandLineConfigSvg;
19
20// SkCommandLineConfig represents a Skia rendering configuration string.
21// The string has following form:
22// tag:
23//   [via-]*backend
24// where 'backend' consists of chars excluding hyphen
25// and each 'via' consists of chars excluding hyphen.
26class SkCommandLineConfig {
27public:
28    SkCommandLineConfig(const SkString&           tag,
29                        const SkString&           backend,
30                        const SkTArray<SkString>& viaParts);
31    virtual ~SkCommandLineConfig();
32    virtual const SkCommandLineConfigGpu* asConfigGpu() const { return nullptr; }
33    virtual const SkCommandLineConfigGraphite* asConfigGraphite() const { return nullptr; }
34    virtual const SkCommandLineConfigSvg* asConfigSvg() const { return nullptr; }
35    const SkString&                       getTag() const { return fTag; }
36    const SkString&                       getBackend() const { return fBackend; }
37    sk_sp<SkColorSpace>                   refColorSpace() const { return fColorSpace; }
38    const SkTArray<SkString>&             getViaParts() const { return fViaParts; }
39
40private:
41    SkString            fTag;
42    SkString            fBackend;
43    sk_sp<SkColorSpace> fColorSpace;
44    SkTArray<SkString>  fViaParts;
45};
46
47// SkCommandLineConfigGpu is a SkCommandLineConfig that extracts information out of the backend
48// part of the tag. It is constructed tags that have:
49// * backends of form "gpu[option=value,option2=value,...]"
50// * backends that represent a shorthand of above (such as "glmsaa16" representing
51// "gpu(api=gl,samples=16)")
52class SkCommandLineConfigGpu : public SkCommandLineConfig {
53public:
54    enum class SurfType { kDefault, kBackendTexture, kBackendRenderTarget };
55    typedef sk_gpu_test::GrContextFactory::ContextType      ContextType;
56    typedef sk_gpu_test::GrContextFactory::ContextOverrides ContextOverrides;
57
58    SkCommandLineConfigGpu(const SkString&           tag,
59                           const SkTArray<SkString>& viaParts,
60                           ContextType               contextType,
61                           bool                      fakeGLESVer2,
62                           uint32_t                  surfaceFlags,
63                           int                       samples,
64                           SkColorType               colorType,
65                           SkAlphaType               alphaType,
66                           bool                      useStencilBuffers,
67                           bool                      testThreading,
68                           int                       testPersistentCache,
69                           bool                      testPrecompile,
70                           bool                      useDDLSink,
71                           bool                      OOPRish,
72                           bool                      reducedShaders,
73                           SurfType);
74
75    const SkCommandLineConfigGpu* asConfigGpu() const override { return this; }
76    ContextType                   getContextType() const { return fContextType; }
77    ContextOverrides              getContextOverrides() const { return fContextOverrides; }
78    uint32_t      getSurfaceFlags() const { return fSurfaceFlags; }
79    int           getSamples() const { return fSamples; }
80    SkColorType   getColorType() const { return fColorType; }
81    SkAlphaType   getAlphaType() const { return fAlphaType; }
82    bool          getTestThreading() const { return fTestThreading; }
83    int           getTestPersistentCache() const { return fTestPersistentCache; }
84    bool          getTestPrecompile() const { return fTestPrecompile; }
85    bool          getUseDDLSink() const { return fUseDDLSink; }
86    bool          getOOPRish() const { return fOOPRish; }
87    bool          getReducedShaders() const { return fReducedShaders; }
88    SurfType      getSurfType() const { return fSurfType; }
89
90private:
91    ContextType         fContextType;
92    ContextOverrides    fContextOverrides;
93    uint32_t            fSurfaceFlags;
94    int                 fSamples;
95    SkColorType         fColorType;
96    SkAlphaType         fAlphaType;
97    bool                fTestThreading;
98    int                 fTestPersistentCache;
99    bool                fTestPrecompile;
100    bool                fUseDDLSink;
101    bool                fOOPRish;
102    bool                fReducedShaders;
103    SurfType            fSurfType;
104};
105
106#ifdef SK_GRAPHITE_ENABLED
107
108#include "tools/graphite/ContextFactory.h"
109
110class SkCommandLineConfigGraphite : public SkCommandLineConfig {
111public:
112    using ContextType = skiatest::graphite::ContextFactory::ContextType;
113
114    SkCommandLineConfigGraphite(const SkString&           tag,
115                                const SkTArray<SkString>& viaParts,
116                                ContextType               contextType,
117                                SkColorType               colorType,
118                                SkAlphaType               alphaType,
119                                bool                      testPrecompile)
120            : SkCommandLineConfig(tag, SkString("graphite"), viaParts)
121            , fContextType(contextType)
122            , fColorType(colorType)
123            , fAlphaType(alphaType)
124            , fTestPrecompile(testPrecompile) {
125    }
126    const SkCommandLineConfigGraphite* asConfigGraphite() const override { return this; }
127
128    ContextType getContextType() const { return fContextType; }
129    SkColorType getColorType() const { return fColorType; }
130    SkAlphaType getAlphaType() const { return fAlphaType; }
131    bool getTestPrecompile() const { return fTestPrecompile; }
132
133private:
134    ContextType         fContextType;
135    SkColorType         fColorType;
136    SkAlphaType         fAlphaType;
137    bool                fTestPrecompile;
138};
139
140#endif // SK_GRAPHITE_ENABLED
141
142// SkCommandLineConfigSvg is a SkCommandLineConfig that extracts information out of the backend
143// part of the tag. It is constructed tags that have:
144// * backends of form "svg[option=value,option2=value,...]"
145class SkCommandLineConfigSvg : public SkCommandLineConfig {
146public:
147    SkCommandLineConfigSvg(const SkString& tag, const SkTArray<SkString>& viaParts, int pageIndex);
148    const SkCommandLineConfigSvg* asConfigSvg() const override { return this; }
149
150    int getPageIndex() const { return fPageIndex; }
151
152private:
153    int fPageIndex;
154};
155
156typedef SkTArray<std::unique_ptr<SkCommandLineConfig>, true> SkCommandLineConfigArray;
157void ParseConfigs(const CommandLineFlags::StringArray& configList,
158                  SkCommandLineConfigArray*            outResult);
159
160#endif
161