Revert "Revert "Revert "Revert "Revert "Revert "Redefine the meaning of sample counts in GPU backend.""""""

This reverts commit 3a2cc2c2ec124de36d2544b2a523ef1dd317ca32.

Fix code with samplecnt=0 that slipped in between trybots/CQ and landing of previous version

Change-Id: Iab19f2e8d1e9901601c8c76244d7a88c5d707fab
Reviewed-on: https://skia-review.googlesource.com/103181
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/include/gpu/GrCaps.h b/include/gpu/GrCaps.h
index f4d6e06..0f03f16 100644
--- a/include/gpu/GrCaps.h
+++ b/include/gpu/GrCaps.h
@@ -134,10 +134,6 @@
 
     int maxRasterSamples() const { return fMaxRasterSamples; }
 
-    // Find a sample count greater than or equal to the requested count which is supported for a
-    // color buffer of the given config. If MSAA is not support for the config we will return 0.
-    virtual int getSampleCount(int requestedCount, GrPixelConfig config) const = 0;
-
     int maxWindowRectangles() const { return fMaxWindowRectangles; }
 
     // A tuned, platform-specific value for the maximum number of analytic fragment processors we
@@ -145,9 +141,32 @@
     int maxClipAnalyticFPs() const { return fMaxClipAnalyticFPs; }
 
     virtual bool isConfigTexturable(GrPixelConfig) const = 0;
-    virtual bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const = 0;
+
     // Returns whether a texture of the given config can be copied to a texture of the same config.
-    virtual bool isConfigCopyable(GrPixelConfig config) const = 0;
+    virtual bool isConfigCopyable(GrPixelConfig) const = 0;
+
+    // Returns the maximum supported sample count for a config. 0 means the config is not renderable
+    // 1 means the config is renderable but doesn't support MSAA.
+    virtual int maxRenderTargetSampleCount(GrPixelConfig) const = 0;
+
+    bool isConfigRenderable(GrPixelConfig config) const {
+        return this->maxRenderTargetSampleCount(config) > 0;
+    }
+
+    // TODO: Remove this after Flutter updated to no longer use it.
+    bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
+        return this->maxRenderTargetSampleCount(config) > (withMSAA ? 1 : 0);
+    }
+
+    // Find a sample count greater than or equal to the requested count which is supported for a
+    // color buffer of the given config or 0 if no such sample count is supported. If the requested
+    // sample count is 1 then 1 will be returned if non-MSAA rendering is supported, otherwise 0.
+    // For historical reasons requestedCount==0 is handled identically to requestedCount==1.
+    virtual int getRenderTargetSampleCount(int requestedCount, GrPixelConfig) const = 0;
+    // TODO: Remove. Legacy name used by Chrome.
+    int getSampleCount(int requestedCount, GrPixelConfig config) const {
+        return this->getRenderTargetSampleCount(requestedCount, config);
+    }
 
     bool suppressPrints() const { return fSuppressPrints; }
 
@@ -178,6 +197,8 @@
     virtual bool initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
                                     bool* rectsMustMatch, bool* disallowSubrect) const = 0;
 
+    bool validateSurfaceDesc(const GrSurfaceDesc&, GrMipMapped) const;
+
     /**
      * Returns true if the GrBackendTexutre can we used with the supplied SkColorType. If it is
      * compatible, the passed in GrPixelConfig will be set to a config that matches the backend
diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h
index f85efc4..d0ca90e 100644
--- a/include/gpu/GrContext.h
+++ b/include/gpu/GrContext.h
@@ -216,6 +216,26 @@
     /** Access the context capabilities */
     const GrCaps* caps() const { return fCaps.get(); }
 
+    /**
+     * Can a SkImage be created with the given color type.
+     */
+    bool colorTypeSupportedAsImage(SkColorType) const;
+
+    /**
+     * Can a SkSurface be created with the given color type. To check whether MSAA is supported
+     * use maxSurfaceSampleCountForColorType().
+     */
+    bool colorTypeSupportedAsSurface(SkColorType colorType) const {
+        return this->maxSurfaceSampleCountForColorType(colorType) > 0;
+    }
+
+    /**
+     * Gets the maximum supported sample count for a color type. 1 is returned if only non-MSAA
+     * rendering is supported for the color type. 0 is returned if rendering to this color type
+     * is not supported at all.
+     */
+    int maxSurfaceSampleCountForColorType(SkColorType) const;
+
     /*
      * Create a new render target context backed by a deferred-style
      * GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
@@ -226,7 +246,7 @@
                                                  int width, int height,
                                                  GrPixelConfig config,
                                                  sk_sp<SkColorSpace> colorSpace,
-                                                 int sampleCnt = 0,
+                                                 int sampleCnt = 1,
                                                  GrMipMapped = GrMipMapped::kNo,
                                                  GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
                                                  const SkSurfaceProps* surfaceProps = nullptr,
@@ -242,7 +262,7 @@
                                                  int width, int height,
                                                  GrPixelConfig config,
                                                  sk_sp<SkColorSpace> colorSpace,
-                                                 int sampleCnt = 0,
+                                                 int sampleCnt = 1,
                                                  GrMipMapped = GrMipMapped::kNo,
                                                  GrSurfaceOrigin origin = kBottomLeft_GrSurfaceOrigin,
                                                  const SkSurfaceProps* surfaceProps = nullptr,
diff --git a/include/gpu/GrRenderTarget.h b/include/gpu/GrRenderTarget.h
index 4eee7f6..0aad636 100644
--- a/include/gpu/GrRenderTarget.h
+++ b/include/gpu/GrRenderTarget.h
@@ -33,10 +33,11 @@
     const GrRenderTarget* asRenderTarget() const  override { return this; }
 
     // GrRenderTarget
-    bool isStencilBufferMultisampled() const { return fSampleCnt > 0; }
+    bool isStencilBufferMultisampled() const { return fSampleCnt > 1; }
 
     GrFSAAType fsaaType() const {
-        if (!fSampleCnt) {
+        SkASSERT(fSampleCnt >= 1);
+        if (fSampleCnt <= 1) {
             SkASSERT(!(fFlags & GrRenderTargetFlags::kMixedSampled));
             return GrFSAAType::kNone;
         }
@@ -45,15 +46,15 @@
     }
 
     /**
-     * Returns the number of samples/pixel in the stencil buffer (Zero if non-MSAA).
+     * Returns the number of samples/pixel in the stencil buffer (One if non-MSAA).
      */
     int numStencilSamples() const { return fSampleCnt; }
 
     /**
-     * Returns the number of samples/pixel in the color buffer (Zero if non-MSAA or mixed sampled).
+     * Returns the number of samples/pixel in the color buffer (One if non-MSAA or mixed sampled).
      */
     int numColorSamples() const {
-        return GrFSAAType::kMixedSamples == this->fsaaType() ? 0 : fSampleCnt;
+        return GrFSAAType::kMixedSamples == this->fsaaType() ? 1 : fSampleCnt;
     }
 
     /**
diff --git a/include/gpu/GrTypes.h b/include/gpu/GrTypes.h
index 3081543..ab21fba 100644
--- a/include/gpu/GrTypes.h
+++ b/include/gpu/GrTypes.h
@@ -402,13 +402,12 @@
  */
 struct GrSurfaceDesc {
     GrSurfaceDesc()
-        : fFlags(kNone_GrSurfaceFlags)
-        , fOrigin(kTopLeft_GrSurfaceOrigin)
-        , fWidth(0)
-        , fHeight(0)
-        , fConfig(kUnknown_GrPixelConfig)
-        , fSampleCnt(0) {
-    }
+            : fFlags(kNone_GrSurfaceFlags)
+            , fOrigin(kTopLeft_GrSurfaceOrigin)
+            , fWidth(0)
+            , fHeight(0)
+            , fConfig(kUnknown_GrPixelConfig)
+            , fSampleCnt(1) {}
 
     GrSurfaceFlags         fFlags;  //!< bitfield of TextureFlags
     GrSurfaceOrigin        fOrigin; //!< origin of the texture
@@ -422,11 +421,11 @@
     GrPixelConfig          fConfig;
 
     /**
-     * The number of samples per pixel or 0 to disable full scene AA. This only
+     * The number of samples per pixel. Zero is treated equivalently to 1. This only
      * applies if the kRenderTarget_GrSurfaceFlag is set. The actual number
      * of samples may not exactly match the request. The request will be rounded
-     * up to the next supported sample count, or down if it is larger than the
-     * max supported count.
+     * up to the next supported sample count. A value larger than the largest
+     * supported sample count will fail.
      */
     int                    fSampleCnt;
 };
diff --git a/include/gpu/mock/GrMockTypes.h b/include/gpu/mock/GrMockTypes.h
index cf30a33..0954c5e 100644
--- a/include/gpu/mock/GrMockTypes.h
+++ b/include/gpu/mock/GrMockTypes.h
@@ -22,8 +22,9 @@
  */
 struct GrMockOptions {
     GrMockOptions() {
+        using Renderability = ConfigOptions::Renderability;
         // By default RGBA_8888 is textureable and renderable and A8 and RGB565 are texturable.
-        fConfigOptions[kRGBA_8888_GrPixelConfig].fRenderable[0] = true;
+        fConfigOptions[kRGBA_8888_GrPixelConfig].fRenderability = Renderability::kNonMSAA;
         fConfigOptions[kRGBA_8888_GrPixelConfig].fTexturable = true;
         fConfigOptions[kAlpha_8_GrPixelConfig].fTexturable = true;
         fConfigOptions[kAlpha_8_as_Alpha_GrPixelConfig].fTexturable = true;
@@ -32,8 +33,8 @@
     }
 
     struct ConfigOptions {
-        /** The first value is for non-MSAA rendering, the second for MSAA. */
-        bool fRenderable[2] = {false, false};
+        enum Renderability { kNo, kNonMSAA, kMSAA };
+        Renderability fRenderability;
         bool fTexturable = false;
     };