Move SkSL caching out of GR_TEST_UTILS, trim persistent cache options

Combines the two boolean options into a single tri-state enum. Old GLSL
option is still present (temporarily) until Chrome is switched over.

Also add a type tag for cached program binaries, so we can safely
detect cache entries of the wrong type.

Change-Id: I0ddeefa9180b27bc2c46e2e7b77e6c9cdf4a730a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/238856
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
diff --git a/tools/viewer/Viewer.cpp b/tools/viewer/Viewer.cpp
index 27c03d2..dd64e84 100644
--- a/tools/viewer/Viewer.cpp
+++ b/tools/viewer/Viewer.cpp
@@ -305,7 +305,8 @@
     displayParams.fMSAASampleCount = FLAGS_msaa;
     SetCtxOptionsFromCommonFlags(&displayParams.fGrContextOptions);
     displayParams.fGrContextOptions.fPersistentCache = &fPersistentCache;
-    displayParams.fGrContextOptions.fDisallowGLSLBinaryCaching = true;
+    displayParams.fGrContextOptions.fShaderCacheStrategy =
+            GrContextOptions::ShaderCacheStrategy::kBackendSource;
     displayParams.fGrContextOptions.fShaderErrorHandler = &gShaderErrorHandler;
     displayParams.fGrContextOptions.fSuppressPrints = true;
     displayParams.fGrContextOptions.fInternalMultisampleCount = FLAGS_internalSamples;
@@ -2013,8 +2014,11 @@
             // HACK: If we get here when SKSL caching isn't enabled, and we're on a backend other
             // than GL, we need to force it on. Just do that on the first frame after the backend
             // switch, then resume normal operation.
-            if (!backendIsGL && !params.fGrContextOptions.fCacheSKSL) {
-                params.fGrContextOptions.fCacheSKSL = true;
+            if (!backendIsGL &&
+                params.fGrContextOptions.fShaderCacheStrategy !=
+                        GrContextOptions::ShaderCacheStrategy::kSkSL) {
+                params.fGrContextOptions.fShaderCacheStrategy =
+                        GrContextOptions::ShaderCacheStrategy::kSkSL;
                 paramsChanged = true;
                 fPersistentCache.reset();
             } else if (ImGui::CollapsingHeader("Shaders")) {
@@ -2033,8 +2037,11 @@
                             entry.fKeyString.appendf("%02x", digest.data[i]);
                         }
 
-                        entry.fShaderType = GrPersistentCacheUtils::UnpackCachedShaders(
-                                data.get(), entry.fShader, entry.fInputs, kGrShaderTypeCount);
+                        SkReader32 reader(data->data(), data->size());
+                        entry.fShaderType = reader.readU32();
+                        GrPersistentCacheUtils::UnpackCachedShaders(&reader, entry.fShader,
+                                                                    entry.fInputs,
+                                                                    kGrShaderTypeCount);
                     };
                     fCachedGLSL.reset();
                     fPersistentCache.foreach(collectShaders);
@@ -2047,7 +2054,12 @@
                 bool doSave = ImGui::Button("Save");
                 if (backendIsGL) {
                     ImGui::SameLine();
-                    if (ImGui::Checkbox("SkSL", &params.fGrContextOptions.fCacheSKSL)) {
+                    bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
+                                GrContextOptions::ShaderCacheStrategy::kSkSL;
+                    if (ImGui::Checkbox("SkSL", &sksl)) {
+                        params.fGrContextOptions.fShaderCacheStrategy = sksl
+                                ? GrContextOptions::ShaderCacheStrategy::kSkSL
+                                : GrContextOptions::ShaderCacheStrategy::kBackendSource;
                         paramsChanged = true;
                         doLoad = true;
                         fDeferredActions.push_back([=]() { fPersistentCache.reset(); });
@@ -2083,7 +2095,8 @@
                 if (doSave) {
                     // The hovered item (if any) gets a special shader to make it identifiable
                     auto shaderCaps = ctx->priv().caps()->shaderCaps();
-                    bool sksl = params.fGrContextOptions.fCacheSKSL;
+                    bool sksl = params.fGrContextOptions.fShaderCacheStrategy ==
+                                GrContextOptions::ShaderCacheStrategy::kSkSL;
 
                     SkSL::String highlight;
                     if (!sksl) {