Move external texture extension logic into SkSL

Automatically enable the extension if any external samplers are used.

Bug: skia:7713
Change-Id: I1de2faddd7c8eda864873941b90d7a4c4606497a
Reviewed-on: https://skia-review.googlesource.com/121797
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/src/gpu/gl/GrGLGpu.cpp b/src/gpu/gl/GrGLGpu.cpp
index 85c9fb2..8a5c235 100644
--- a/src/gpu/gl/GrGLGpu.cpp
+++ b/src/gpu/gl/GrGLGpu.cpp
@@ -3568,10 +3568,6 @@
             fshaderTxt.appendf("#extension %s : require\n", extension);
         }
     }
-    if (samplerType == kTextureExternalSampler_GrSLType) {
-        fshaderTxt.appendf("#extension %s : require\n",
-                           shaderCaps->externalTextureExtensionString());
-    }
     vTexCoord.setTypeModifier(GrShaderVar::kIn_TypeModifier);
     vTexCoord.appendDecl(shaderCaps, &fshaderTxt);
     fshaderTxt.append(";");
diff --git a/src/gpu/glsl/GrGLSLProgramBuilder.cpp b/src/gpu/glsl/GrGLSLProgramBuilder.cpp
index 914a958..91ac5c3 100644
--- a/src/gpu/glsl/GrGLSLProgramBuilder.cpp
+++ b/src/gpu/glsl/GrGLSLProgramBuilder.cpp
@@ -257,15 +257,6 @@
         const GrResourceIOProcessor::TextureSampler& sampler = processor.textureSampler(t);
         name.printf("TextureSampler_%d", outTexSamplerHandles->count());
         GrSLType samplerType = sampler.peekTexture()->texturePriv().samplerType();
-        if (kTextureExternalSampler_GrSLType == samplerType) {
-            const char* externalFeatureString =
-                    this->shaderCaps()->externalTextureExtensionString();
-            // We shouldn't ever create a GrGLTexture that requires external sampler type
-            SkASSERT(externalFeatureString);
-            this->addFeature(sampler.visibility(),
-                             1 << GrGLSLShaderBuilder::kExternalTexture_GLSLPrivateFeature,
-                             externalFeatureString);
-        }
         outTexSamplerHandles->emplace_back(this->emitSampler(
                 samplerType, sampler.peekTexture()->config(), name.c_str(), sampler.visibility()));
     }
diff --git a/src/gpu/glsl/GrGLSLShaderBuilder.h b/src/gpu/glsl/GrGLSLShaderBuilder.h
index cfddbff..b13773b 100644
--- a/src/gpu/glsl/GrGLSLShaderBuilder.h
+++ b/src/gpu/glsl/GrGLSLShaderBuilder.h
@@ -169,7 +169,6 @@
         kFragCoordConventions_GLSLPrivateFeature,
         kBlendEquationAdvanced_GLSLPrivateFeature,
         kBlendFuncExtended_GLSLPrivateFeature,
-        kExternalTexture_GLSLPrivateFeature,
         kTexelBuffer_GLSLPrivateFeature,
         kFramebufferFetch_GLSLPrivateFeature,
         kNoPerspectiveInterpolation_GLSLPrivateFeature,
diff --git a/src/sksl/SkSLGLSLCodeGenerator.cpp b/src/sksl/SkSLGLSLCodeGenerator.cpp
index 0ee0afa..bcf45ba 100644
--- a/src/sksl/SkSLGLSLCodeGenerator.cpp
+++ b/src/sksl/SkSLGLSLCodeGenerator.cpp
@@ -1083,6 +1083,14 @@
             }
             fFoundImageDecl = true;
         }
+        if (!fFoundExternalSamplerDecl && var.fVar->fType == *fContext.fSamplerExternalOES_Type) {
+            if (fProgram.fSettings.fCaps->externalTextureExtensionString()) {
+                fHeader.writeText("#extension ");
+                fHeader.writeText(fProgram.fSettings.fCaps->externalTextureExtensionString());
+                fHeader.writeText(" : require\n");
+            }
+            fFoundExternalSamplerDecl = true;
+        }
     }
     if (wroteType) {
         this->write(";");
diff --git a/src/sksl/SkSLGLSLCodeGenerator.h b/src/sksl/SkSLGLSLCodeGenerator.h
index 2b308e3..362ae7c 100644
--- a/src/sksl/SkSLGLSLCodeGenerator.h
+++ b/src/sksl/SkSLGLSLCodeGenerator.h
@@ -209,6 +209,7 @@
     // true if we have run into usages of dFdx / dFdy
     bool fFoundDerivatives = false;
     bool fFoundImageDecl = false;
+    bool fFoundExternalSamplerDecl = false;
     bool fFoundGSInvocations = false;
     bool fSetupFragPositionGlobal = false;
     bool fSetupFragPositionLocal = false;
diff --git a/src/sksl/SkSLUtil.h b/src/sksl/SkSLUtil.h
index 78a8073..dc87246 100644
--- a/src/sksl/SkSLUtil.h
+++ b/src/sksl/SkSLUtil.h
@@ -151,6 +151,10 @@
         return nullptr;
     }
 
+    const char* externalTextureExtensionString() const {
+        return nullptr;
+    }
+
     const char* versionDeclString() const {
         return "";
     }