Revert "Drop unused shader support for sample variables"

This restores support for sample variables and improves on the
original API.

A test will come once we restore support for sample locations.

Bug: skia:
Change-Id: I350cc08477c532a13ff7acdd8055c0aa65c51d26
Reviewed-on: https://skia-review.googlesource.com/c/195720
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
diff --git a/src/gpu/glsl/GrGLSLFragmentShaderBuilder.cpp b/src/gpu/glsl/GrGLSLFragmentShaderBuilder.cpp
index 511df10..361221a 100644
--- a/src/gpu/glsl/GrGLSLFragmentShaderBuilder.cpp
+++ b/src/gpu/glsl/GrGLSLFragmentShaderBuilder.cpp
@@ -72,6 +72,7 @@
     , fHasCustomColorOutput(false)
     , fCustomColorOutputIndex(-1)
     , fHasSecondaryOutput(false)
+    , fHasInitializedSampleMask(false)
     , fForceHighPrecision(false) {
     fSubstageIndices.push_back(0);
 #ifdef SK_DEBUG
@@ -92,6 +93,28 @@
     return coords2D;
 }
 
+void GrGLSLFragmentShaderBuilder::maskOffMultisampleCoverage(const char* mask, Scope scope) {
+    const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps();
+    if (!shaderCaps.sampleVariablesSupport()) {
+        SkDEBUGFAIL("Attempted to mask sample coverage without support.");
+        return;
+    }
+    if (const char* extension = shaderCaps.sampleVariablesExtensionString()) {
+        this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension);
+    }
+
+    if (!fHasInitializedSampleMask && Scope::kTopLevel == scope) {
+        this->codeAppendf("gl_SampleMask[0] = (%s);", mask);
+        fHasInitializedSampleMask = true;
+        return;
+    }
+    if (!fHasInitializedSampleMask) {
+        this->codePrependf("gl_SampleMask[0] = ~0;");
+        fHasInitializedSampleMask = true;
+    }
+    this->codeAppendf("gl_SampleMask[0] &= (%s);", mask);
+}
+
 const char* GrGLSLFragmentShaderBuilder::dstColor() {
     SkDEBUGCODE(fHasReadDstColor = true;)
 
diff --git a/src/gpu/glsl/GrGLSLFragmentShaderBuilder.h b/src/gpu/glsl/GrGLSLFragmentShaderBuilder.h
index 0e0794f..02a933e 100644
--- a/src/gpu/glsl/GrGLSLFragmentShaderBuilder.h
+++ b/src/gpu/glsl/GrGLSLFragmentShaderBuilder.h
@@ -47,14 +47,23 @@
     /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
     GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
 
-    enum Coordinates {
-        kSkiaDevice_Coordinates,
-        kGLSLWindow_Coordinates,
-
-        kLast_Coordinates = kGLSLWindow_Coordinates
+    enum class Scope : bool {
+        kTopLevel,
+        kInsideLoopOrBranch
     };
 
     /**
+     * Subtracts multisample coverage by AND-ing the sample mask with the provided "mask".
+     * Sample N corresponds to bit "1 << N".
+     *
+     * If the given scope is "kTopLevel" and the sample mask has not yet been modified, this method
+     * assigns the sample mask in place rather than pre-initializing it to ~0 then AND-ing it.
+     *
+     * Requires MSAA and GLSL support for sample variables.
+     */
+    virtual void maskOffMultisampleCoverage(const char* mask, Scope) = 0;
+
+    /**
      * Fragment procs with child procs should call these functions before/after calling emitCode
      * on a child proc.
      */
@@ -102,6 +111,7 @@
     virtual SkString ensureCoords2D(const GrShaderVar&) override;
 
     // GrGLSLFPFragmentBuilder interface.
+    void maskOffMultisampleCoverage(const char* mask, Scope) override;
     const SkString& getMangleString() const override { return fMangleString; }
     void onBeforeChildProcEmitCode() override;
     void onAfterChildProcEmitCode() override;
@@ -163,6 +173,7 @@
     bool fHasCustomColorOutput;
     int fCustomColorOutputIndex;
     bool fHasSecondaryOutput;
+    bool fHasInitializedSampleMask;
     bool fForceHighPrecision;
 
 #ifdef SK_DEBUG
diff --git a/src/gpu/glsl/GrGLSLShaderBuilder.h b/src/gpu/glsl/GrGLSLShaderBuilder.h
index 855011f..bbd3f28 100644
--- a/src/gpu/glsl/GrGLSLShaderBuilder.h
+++ b/src/gpu/glsl/GrGLSLShaderBuilder.h
@@ -164,7 +164,8 @@
         kBlendFuncExtended_GLSLPrivateFeature,
         kFramebufferFetch_GLSLPrivateFeature,
         kNoPerspectiveInterpolation_GLSLPrivateFeature,
-        kLastGLSLPrivateFeature = kNoPerspectiveInterpolation_GLSLPrivateFeature
+        kSampleVariables_GLSLPrivateFeature,
+        kLastGLSLPrivateFeature = kSampleVariables_GLSLPrivateFeature
     };
 
     /*