1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2014 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/core/SkExecutor.h"
9cb93a386Sopenharmony_ci#include "include/gpu/GrContextOptions.h"
10cb93a386Sopenharmony_ci#include "tools/flags/CommonFlags.h"
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ciDEFINE_int(gpuThreads,
13cb93a386Sopenharmony_ci             2,
14cb93a386Sopenharmony_ci             "Create this many extra threads to assist with GPU work, "
15cb93a386Sopenharmony_ci             "including software path rendering. Defaults to two.");
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_cinamespace CommonFlags {
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_cistatic DEFINE_bool(cachePathMasks, true,
20cb93a386Sopenharmony_ci                   "Allows path mask textures to be cached in GPU configs.");
21cb93a386Sopenharmony_cistatic DEFINE_bool(allPathsVolatile, false,
22cb93a386Sopenharmony_ci                   "Causes all GPU paths to be processed as if 'setIsVolatile' had been called.");
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_cistatic DEFINE_bool(hwtess, false, "Enables support for tessellation shaders (if hw allows.).");
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_cistatic DEFINE_int(maxTessellationSegments, 0,
27cb93a386Sopenharmony_ci                  "Overrides the max number of tessellation segments supported by the caps.");
28cb93a386Sopenharmony_ci
29cb93a386Sopenharmony_cistatic DEFINE_bool(alwaysHwTess, false,
30cb93a386Sopenharmony_ci        "Always try to use hardware tessellation, regardless of how small a path may be.");
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_cistatic DEFINE_string(pr, "",
33cb93a386Sopenharmony_ci              "Set of enabled gpu path renderers. Defined as a list of: "
34cb93a386Sopenharmony_ci              "[~]none [~]dashline [~]aahairline [~]aaconvex [~]aalinearizing [~]small [~]tri "
35cb93a386Sopenharmony_ci              "[~]atlas [~]tess [~]all");
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_cistatic DEFINE_int(internalSamples, -1,
38cb93a386Sopenharmony_ci        "Number of samples for internal draws that use MSAA, or default value if negative.");
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_cistatic DEFINE_int(maxAtlasSize, -1,
41cb93a386Sopenharmony_ci        "Maximum width and height of internal texture atlases, or default value if negative.");
42cb93a386Sopenharmony_ci
43cb93a386Sopenharmony_cistatic DEFINE_bool(disableDriverCorrectnessWorkarounds, false,
44cb93a386Sopenharmony_ci                   "Disables all GPU driver correctness workarounds");
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_cistatic DEFINE_bool(dontReduceOpsTaskSplitting, false,
47cb93a386Sopenharmony_ci                   "Don't reorder tasks to reduce render passes");
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_cistatic DEFINE_int(gpuResourceCacheLimit, -1,
50cb93a386Sopenharmony_ci                  "Maximum number of bytes to use for budgeted GPU resources. "
51cb93a386Sopenharmony_ci                  "Default is -1, which means GrResourceCache::kDefaultMaxSize.");
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_cistatic GpuPathRenderers get_named_pathrenderers_flags(const char* name) {
54cb93a386Sopenharmony_ci    if (!strcmp(name, "none")) {
55cb93a386Sopenharmony_ci        return GpuPathRenderers::kNone;
56cb93a386Sopenharmony_ci    } else if (!strcmp(name, "dashline")) {
57cb93a386Sopenharmony_ci        return GpuPathRenderers::kDashLine;
58cb93a386Sopenharmony_ci    } else if (!strcmp(name, "aahairline")) {
59cb93a386Sopenharmony_ci        return GpuPathRenderers::kAAHairline;
60cb93a386Sopenharmony_ci    } else if (!strcmp(name, "aaconvex")) {
61cb93a386Sopenharmony_ci        return GpuPathRenderers::kAAConvex;
62cb93a386Sopenharmony_ci    } else if (!strcmp(name, "aalinearizing")) {
63cb93a386Sopenharmony_ci        return GpuPathRenderers::kAALinearizing;
64cb93a386Sopenharmony_ci    } else if (!strcmp(name, "small")) {
65cb93a386Sopenharmony_ci        return GpuPathRenderers::kSmall;
66cb93a386Sopenharmony_ci    } else if (!strcmp(name, "tri")) {
67cb93a386Sopenharmony_ci        return GpuPathRenderers::kTriangulating;
68cb93a386Sopenharmony_ci    } else if (!strcmp(name, "atlas")) {
69cb93a386Sopenharmony_ci        return GpuPathRenderers::kAtlas;
70cb93a386Sopenharmony_ci    } else if (!strcmp(name, "tess")) {
71cb93a386Sopenharmony_ci        return GpuPathRenderers::kTessellation;
72cb93a386Sopenharmony_ci    } else if (!strcmp(name, "default")) {
73cb93a386Sopenharmony_ci        return GpuPathRenderers::kDefault;
74cb93a386Sopenharmony_ci    }
75cb93a386Sopenharmony_ci    SK_ABORT("error: unknown named path renderer \"%s\"\n", name);
76cb93a386Sopenharmony_ci}
77cb93a386Sopenharmony_ci
78cb93a386Sopenharmony_cistatic GpuPathRenderers collect_gpu_path_renderers_from_flags() {
79cb93a386Sopenharmony_ci    if (FLAGS_pr.isEmpty()) {
80cb93a386Sopenharmony_ci        return GpuPathRenderers::kDefault;
81cb93a386Sopenharmony_ci    }
82cb93a386Sopenharmony_ci
83cb93a386Sopenharmony_ci    GpuPathRenderers gpuPathRenderers = ('~' == FLAGS_pr[0][0])
84cb93a386Sopenharmony_ci            ? GpuPathRenderers::kDefault
85cb93a386Sopenharmony_ci            : GpuPathRenderers::kNone;
86cb93a386Sopenharmony_ci
87cb93a386Sopenharmony_ci    for (int i = 0; i < FLAGS_pr.count(); ++i) {
88cb93a386Sopenharmony_ci        const char* name = FLAGS_pr[i];
89cb93a386Sopenharmony_ci        if (name[0] == '~') {
90cb93a386Sopenharmony_ci            gpuPathRenderers &= ~get_named_pathrenderers_flags(&name[1]);
91cb93a386Sopenharmony_ci        } else {
92cb93a386Sopenharmony_ci            gpuPathRenderers |= get_named_pathrenderers_flags(name);
93cb93a386Sopenharmony_ci        }
94cb93a386Sopenharmony_ci    }
95cb93a386Sopenharmony_ci    return gpuPathRenderers;
96cb93a386Sopenharmony_ci}
97cb93a386Sopenharmony_ci
98cb93a386Sopenharmony_civoid SetCtxOptions(GrContextOptions* ctxOptions) {
99cb93a386Sopenharmony_ci    static std::unique_ptr<SkExecutor> gGpuExecutor = (0 != FLAGS_gpuThreads)
100cb93a386Sopenharmony_ci        ? SkExecutor::MakeFIFOThreadPool(FLAGS_gpuThreads)
101cb93a386Sopenharmony_ci        : nullptr;
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_ci    ctxOptions->fExecutor                            = gGpuExecutor.get();
104cb93a386Sopenharmony_ci    ctxOptions->fAllowPathMaskCaching                = FLAGS_cachePathMasks;
105cb93a386Sopenharmony_ci    ctxOptions->fAllPathsVolatile                    = FLAGS_allPathsVolatile;
106cb93a386Sopenharmony_ci    ctxOptions->fEnableExperimentalHardwareTessellation = FLAGS_hwtess;
107cb93a386Sopenharmony_ci    ctxOptions->fMaxTessellationSegmentsOverride     = FLAGS_maxTessellationSegments;
108cb93a386Sopenharmony_ci    ctxOptions->fAlwaysPreferHardwareTessellation    = FLAGS_alwaysHwTess;
109cb93a386Sopenharmony_ci    ctxOptions->fGpuPathRenderers                    = collect_gpu_path_renderers_from_flags();
110cb93a386Sopenharmony_ci    ctxOptions->fDisableDriverCorrectnessWorkarounds = FLAGS_disableDriverCorrectnessWorkarounds;
111cb93a386Sopenharmony_ci    ctxOptions->fResourceCacheLimitOverride          = FLAGS_gpuResourceCacheLimit;
112cb93a386Sopenharmony_ci
113cb93a386Sopenharmony_ci    if (FLAGS_internalSamples >= 0) {
114cb93a386Sopenharmony_ci        ctxOptions->fInternalMultisampleCount = FLAGS_internalSamples;
115cb93a386Sopenharmony_ci    }
116cb93a386Sopenharmony_ci    if (FLAGS_maxAtlasSize >= 0) {
117cb93a386Sopenharmony_ci        ctxOptions->fMaxTextureAtlasSize = FLAGS_maxAtlasSize;
118cb93a386Sopenharmony_ci    }
119cb93a386Sopenharmony_ci
120cb93a386Sopenharmony_ci    if (FLAGS_dontReduceOpsTaskSplitting) {
121cb93a386Sopenharmony_ci        ctxOptions->fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
122cb93a386Sopenharmony_ci    } else {
123cb93a386Sopenharmony_ci        ctxOptions->fReduceOpsTaskSplitting = GrContextOptions::Enable::kYes;
124cb93a386Sopenharmony_ci    }
125cb93a386Sopenharmony_ci}
126cb93a386Sopenharmony_ci
127cb93a386Sopenharmony_ci}  // namespace CommonFlags
128