Add ContextOptions field to SkCommandLineConfigGpu

Reduces duplicate code by computing the ContextOptions in one spot.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2337163002

Review-Url: https://codereview.chromium.org/2337163002
diff --git a/bench/nanobench.cpp b/bench/nanobench.cpp
index ae415fa..238e8f6 100644
--- a/bench/nanobench.cpp
+++ b/bench/nanobench.cpp
@@ -390,7 +390,7 @@
 
 #if SK_SUPPORT_GPU
 #define kBogusContextType GrContextFactory::kNativeGL_ContextType
-#define kBogusContextOptions GrContextFactory::kNone_ContextOptions
+#define kBogusContextOptions GrContextFactory::ContextOptions::kNone
 #else
 #define kBogusContextType 0
 #define kBogusContextOptions 0
@@ -403,21 +403,8 @@
         if (!FLAGS_gpu)
             return;
 
-        auto ctxOptions = GrContextFactory::kNone_ContextOptions;
-        if (gpuConfig->getUseNVPR()) {
-            ctxOptions = static_cast<GrContextFactory::ContextOptions>(
-                ctxOptions | GrContextFactory::kEnableNVPR_ContextOptions);
-        }
-        if (gpuConfig->getUseInstanced()) {
-            ctxOptions = static_cast<GrContextFactory::ContextOptions>(
-                ctxOptions | GrContextFactory::kUseInstanced_ContextOptions);
-        }
-        if (SkColorAndColorSpaceAreGammaCorrect(gpuConfig->getColorType(),
-                                                gpuConfig->getColorSpace())) {
-            ctxOptions = static_cast<GrContextFactory::ContextOptions>(
-                ctxOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions);
-        }
         const auto ctxType = gpuConfig->getContextType();
+        const auto ctxOptions = gpuConfig->getContextOptions();
         const auto sampleCount = gpuConfig->getSamples();
 
         if (const GrContext* ctx = gGrFactory->get(ctxType, ctxOptions)) {
diff --git a/dm/DM.cpp b/dm/DM.cpp
index 1598bcd..fc332a2 100644
--- a/dm/DM.cpp
+++ b/dm/DM.cpp
@@ -825,21 +825,7 @@
     if (gpu_supported()) {
         if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
             GrContextFactory::ContextType contextType = gpuConfig->getContextType();
-            GrContextFactory::ContextOptions contextOptions =
-                    GrContextFactory::kNone_ContextOptions;
-            if (gpuConfig->getUseNVPR()) {
-                contextOptions = static_cast<GrContextFactory::ContextOptions>(
-                    contextOptions | GrContextFactory::kEnableNVPR_ContextOptions);
-            }
-            if (gpuConfig->getUseInstanced()) {
-                contextOptions = static_cast<GrContextFactory::ContextOptions>(
-                    contextOptions | GrContextFactory::kUseInstanced_ContextOptions);
-            }
-            if (SkColorAndColorSpaceAreGammaCorrect(gpuConfig->getColorType(),
-                                                    gpuConfig->getColorSpace())) {
-                contextOptions = static_cast<GrContextFactory::ContextOptions>(
-                    contextOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions);
-            }
+            GrContextFactory::ContextOptions contextOptions = gpuConfig->getContextOptions();
             GrContextFactory testFactory;
             if (!testFactory.get(contextType, contextOptions)) {
                 info("WARNING: can not create GPU context for config '%s'. "
@@ -1429,7 +1415,7 @@
             (*test)(reporter, ctxInfo);
         }
         ctxInfo = factory->getContextInfo(contextType,
-                                          GrContextFactory::kEnableNVPR_ContextOptions);
+                                          GrContextFactory::ContextOptions::kEnableNVPR);
         if (ctxInfo.grContext()) {
             (*test)(reporter, ctxInfo);
         }
diff --git a/tests/GrContextFactoryTest.cpp b/tests/GrContextFactoryTest.cpp
index 3b12b83..9b600fa 100644
--- a/tests/GrContextFactoryTest.cpp
+++ b/tests/GrContextFactoryTest.cpp
@@ -23,7 +23,7 @@
     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
         GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
         GrContext* context = testFactory.get(ctxType,
-                                             GrContextFactory::kEnableNVPR_ContextOptions);
+                                             GrContextFactory::ContextOptions::kEnableNVPR);
         if (!context) {
             continue;
         }
@@ -57,7 +57,7 @@
     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
         GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
         GrContext* context =
-            testFactory.get(ctxType, GrContextFactory::kRequireSRGBSupport_ContextOptions);
+            testFactory.get(ctxType, GrContextFactory::ContextOptions::kRequireSRGBSupport);
 
         if (context) {
             REPORTER_ASSERT(reporter, context->caps()->srgbSupport());
diff --git a/tools/flags/SkCommonFlagsConfig.cpp b/tools/flags/SkCommonFlagsConfig.cpp
index 447c56d..1d38b56 100644
--- a/tools/flags/SkCommonFlagsConfig.cpp
+++ b/tools/flags/SkCommonFlagsConfig.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "SkCommonFlagsConfig.h"
+#include "SkImageInfo.h"
 
 #include <stdlib.h>
 
@@ -208,12 +209,20 @@
     sk_sp<SkColorSpace> colorSpace)
         : SkCommandLineConfig(tag, SkString("gpu"), viaParts)
         , fContextType(contextType)
-        , fUseNVPR(useNVPR)
-        , fUseInstanced(useInstanced)
+        , fContextOptions(ContextOptions::kNone)
         , fUseDIText(useDIText)
         , fSamples(samples)
         , fColorType(colorType)
         , fColorSpace(std::move(colorSpace)) {
+    if (useNVPR) {
+        fContextOptions |= ContextOptions::kEnableNVPR;
+    }
+    if (useInstanced) {
+        fContextOptions |= ContextOptions::kUseInstanced;
+    }
+    if (SkColorAndColorSpaceAreGammaCorrect(colorType, colorSpace.get())) {
+        fContextOptions |= ContextOptions::kRequireSRGBSupport;
+    }
 }
 static bool parse_option_int(const SkString& value, int* outInt) {
     if (value.isEmpty()) {
diff --git a/tools/flags/SkCommonFlagsConfig.h b/tools/flags/SkCommonFlagsConfig.h
index 641c68b..e817339 100644
--- a/tools/flags/SkCommonFlagsConfig.h
+++ b/tools/flags/SkCommonFlagsConfig.h
@@ -51,13 +51,15 @@
 class SkCommandLineConfigGpu : public SkCommandLineConfig {
   public:
     typedef sk_gpu_test::GrContextFactory::ContextType ContextType;
+    typedef sk_gpu_test::GrContextFactory::ContextOptions ContextOptions;
     SkCommandLineConfigGpu(const SkString& tag, const SkTArray<SkString>& viaParts,
                            ContextType contextType, bool useNVPR, bool useInstanced, bool useDIText,
                            int samples, SkColorType colorType, sk_sp<SkColorSpace> colorSpace);
     const SkCommandLineConfigGpu* asConfigGpu() const override { return this; }
     ContextType getContextType() const { return fContextType; }
-    bool getUseNVPR() const { return fUseNVPR; }
-    bool getUseInstanced() const { return fUseInstanced; }
+    ContextOptions getContextOptions() const { return fContextOptions; }
+    bool getUseNVPR() const { return fContextOptions & ContextOptions::kEnableNVPR; }
+    bool getUseInstanced() const { return fContextOptions & ContextOptions::kUseInstanced; }
     bool getUseDIText() const { return fUseDIText; }
     int getSamples() const { return fSamples; }
     SkColorType getColorType() const { return fColorType; }
@@ -65,8 +67,7 @@
 
   private:
     ContextType fContextType;
-    bool fUseNVPR;
-    bool fUseInstanced;
+    ContextOptions fContextOptions;
     bool fUseDIText;
     int fSamples;
     SkColorType fColorType;
diff --git a/tools/gpu/GrContextFactory.cpp b/tools/gpu/GrContextFactory.cpp
index 2407da9..c40764b 100644
--- a/tools/gpu/GrContextFactory.cpp
+++ b/tools/gpu/GrContextFactory.cpp
@@ -31,7 +31,7 @@
 
 GrContextFactory::GrContextFactory(const GrContextOptions& opts)
     : fGlobalOptions(opts) {
-    // In this factory, instanced rendering is specified with kUseInstanced_ContextOptions.
+    // In this factory, instanced rendering is specified with ContextOptions::kUseInstanced.
     SkASSERT(!fGlobalOptions.fEnableInstancedRendering);
 }
 
@@ -139,7 +139,7 @@
                     break;
 #endif
                 case kNullGL_ContextType:
-                    glCtx = CreateNullGLTestContext(kEnableNVPR_ContextOptions & options);
+                    glCtx = CreateNullGLTestContext(ContextOptions::kEnableNVPR & options);
                     break;
                 case kDebugGL_ContextType:
                     glCtx = CreateDebugGLTestContext();
@@ -154,7 +154,7 @@
             glInterface.reset(SkRef(glCtx->gl()));
             // Block NVPR from non-NVPR types. We don't block NVPR from contexts that will use
             // instanced rendering because that would prevent us from testing mixed samples.
-            if (!((kEnableNVPR_ContextOptions | kUseInstanced_ContextOptions) & options)) {
+            if (!((ContextOptions::kEnableNVPR | ContextOptions::kUseInstanced) & options)) {
                 glInterface.reset(GrGLInterfaceRemoveNVPR(glInterface.get()));
                 if (!glInterface) {
                     return ContextInfo();
@@ -166,7 +166,7 @@
 #ifdef SK_VULKAN
         case kVulkan_GrBackend:
             SkASSERT(kVulkan_ContextType == type);
-            if (kEnableNVPR_ContextOptions & options) {
+            if (ContextOptions::kEnableNVPR & options) {
                 return ContextInfo();
             }
             testCtx.reset(CreatePlatformVkTestContext());
@@ -192,24 +192,24 @@
     testCtx->makeCurrent();
     SkASSERT(testCtx && testCtx->backend() == backend);
     GrContextOptions grOptions = fGlobalOptions;
-    if (kUseInstanced_ContextOptions & options) {
+    if (ContextOptions::kUseInstanced & options) {
         grOptions.fEnableInstancedRendering = true;
     }
     grCtx.reset(GrContext::Create(backend, backendContext, grOptions));
     if (!grCtx.get()) {
         return ContextInfo();
     }
-    if (kEnableNVPR_ContextOptions & options) {
+    if (ContextOptions::kEnableNVPR & options) {
         if (!grCtx->caps()->shaderCaps()->pathRenderingSupport()) {
             return ContextInfo();
         }
     }
-    if (kUseInstanced_ContextOptions & options) {
+    if (ContextOptions::kUseInstanced & options) {
         if (GrCaps::InstancedSupport::kNone == grCtx->caps()->instancedSupport()) {
             return ContextInfo();
         }
     }
-    if (kRequireSRGBSupport_ContextOptions & options) {
+    if (ContextOptions::kRequireSRGBSupport & options) {
         if (!grCtx->caps()->srgbSupport()) {
             return ContextInfo();
         }
diff --git a/tools/gpu/GrContextFactory.h b/tools/gpu/GrContextFactory.h
index f70ebea..d5e87a4 100644
--- a/tools/gpu/GrContextFactory.h
+++ b/tools/gpu/GrContextFactory.h
@@ -89,11 +89,11 @@
      * Options for GL context creation. For historical and testing reasons the options will default
      * to not using GL_NV_path_rendering extension  even when the driver supports it.
      */
-    enum ContextOptions {
-        kNone_ContextOptions                = 0x0,
-        kEnableNVPR_ContextOptions          = 0x1,
-        kUseInstanced_ContextOptions        = 0x2,
-        kRequireSRGBSupport_ContextOptions  = 0x4,
+    enum class ContextOptions {
+        kNone                = 0x0,
+        kEnableNVPR          = 0x1,
+        kUseInstanced        = 0x2,
+        kRequireSRGBSupport  = 0x4,
     };
 
     static ContextType NativeContextTypeForBackend(GrBackend backend) {
@@ -163,11 +163,11 @@
      * Get a context initialized with a type of GL context. It also makes the GL context current.
      */
     ContextInfo getContextInfo(ContextType type,
-                               ContextOptions options = kNone_ContextOptions);
+                               ContextOptions options = ContextOptions::kNone);
     /**
      * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
      */
-    GrContext* get(ContextType type, ContextOptions options = kNone_ContextOptions) {
+    GrContext* get(ContextType type, ContextOptions options = ContextOptions::kNone) {
         return this->getContextInfo(type, options).grContext();
     }
     const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
@@ -186,4 +186,7 @@
     const GrContextOptions          fGlobalOptions;
 };
 }  // namespace sk_gpu_test
+
+GR_MAKE_BITFIELD_CLASS_OPS(sk_gpu_test::GrContextFactory::ContextOptions);
+
 #endif
diff --git a/tools/skiaserve/Request.cpp b/tools/skiaserve/Request.cpp
index ea2d17a..ef1f757 100644
--- a/tools/skiaserve/Request.cpp
+++ b/tools/skiaserve/Request.cpp
@@ -72,10 +72,10 @@
 #if SK_SUPPORT_GPU
     GrContextFactory* factory = fContextFactory;
     GLTestContext* gl = factory->getContextInfo(GrContextFactory::kNativeGL_ContextType,
-                                                GrContextFactory::kNone_ContextOptions).glContext();
+                                               GrContextFactory::ContextOptions::kNone).glContext();
     if (!gl) {
         gl = factory->getContextInfo(GrContextFactory::kMESA_ContextType,
-                                     GrContextFactory::kNone_ContextOptions).glContext();
+                                     GrContextFactory::ContextOptions::kNone).glContext();
     }
     if (gl) {
         gl->makeCurrent();
@@ -123,10 +123,10 @@
 GrContext* Request::getContext() {
 #if SK_SUPPORT_GPU
     GrContext* result = fContextFactory->get(GrContextFactory::kNativeGL_ContextType,
-                                             GrContextFactory::kNone_ContextOptions);
+                                             GrContextFactory::ContextOptions::kNone);
     if (!result) {
         result = fContextFactory->get(GrContextFactory::kMESA_ContextType,
-                                      GrContextFactory::kNone_ContextOptions);
+                                      GrContextFactory::ContextOptions::kNone);
     }
     return result;
 #else