Use mipmapping/protected to lookup scratch textures.

Include protected in scratch key.

Change-Id: I0dfc58c9f54e8279a39adf4bad808b7e0e72cba2
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/241397
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/GrResourceProvider.cpp b/src/gpu/GrResourceProvider.cpp
index 8408873..82a0d12 100644
--- a/src/gpu/GrResourceProvider.cpp
+++ b/src/gpu/GrResourceProvider.cpp
@@ -124,9 +124,10 @@
                                                      GrRenderable renderable,
                                                      int renderTargetSampleCnt,
                                                      SkBudgeted budgeted,
+                                                     GrMipMapped mipMapped,
                                                      GrProtected isProtected) {
     sk_sp<GrTexture> tex(this->refScratchTexture(desc, format, renderable, renderTargetSampleCnt,
-                                                 isProtected));
+                                                 mipMapped, isProtected));
     if (tex && SkBudgeted::kNo == budgeted) {
         tex->resourcePriv().makeUnbudgeted();
     }
@@ -223,9 +224,9 @@
 
     // Compressed textures are read-only so they don't support re-use for scratch.
     // TODO: Support GrMipMapped::kYes in scratch texture lookup here.
-    if (!GrPixelConfigIsCompressed(desc.fConfig) && mipMapped == GrMipMapped::kNo) {
+    if (!GrPixelConfigIsCompressed(desc.fConfig)) {
         sk_sp<GrTexture> tex = this->getExactScratch(
-                desc, format, renderable, renderTargetSampleCnt, budgeted, isProtected);
+                desc, format, renderable, renderTargetSampleCnt, budgeted, mipMapped, isProtected);
         if (tex) {
             return tex;
         }
@@ -288,7 +289,7 @@
     copyDesc.fHeight = MakeApprox(desc.fHeight);
 
     if (auto tex = this->refScratchTexture(copyDesc, format, renderable, renderTargetSampleCnt,
-                                           isProtected)) {
+                                           GrMipMapped::kNo, isProtected)) {
         return tex;
     }
 
@@ -300,6 +301,7 @@
                                                        const GrBackendFormat& format,
                                                        GrRenderable renderable,
                                                        int renderTargetSampleCnt,
+                                                       GrMipMapped mipMapped,
                                                        GrProtected isProtected) {
     ASSERT_SINGLE_OWNER
     SkASSERT(!this->isAbandoned());
@@ -311,7 +313,8 @@
     // to fall back to making a new texture.
     if (fGpu->caps()->reuseScratchTextures() || renderable == GrRenderable::kYes) {
         GrScratchKey key;
-        GrTexturePriv::ComputeScratchKey(desc, renderable, renderTargetSampleCnt, &key);
+        GrTexturePriv::ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, renderable,
+                                         renderTargetSampleCnt, mipMapped, isProtected, &key);
         GrGpuResource* resource = fCache->findAndRefScratchResource(key);
         if (resource) {
             fGpu->stats()->incNumScratchTexturesReused();
diff --git a/src/gpu/GrResourceProvider.h b/src/gpu/GrResourceProvider.h
index 78ebe4e..9f9aebe 100644
--- a/src/gpu/GrResourceProvider.h
+++ b/src/gpu/GrResourceProvider.h
@@ -284,22 +284,24 @@
 
     // Attempts to find a resource in the cache that exactly matches the GrSurfaceDesc. Failing that
     // it returns null. If non-null, the resulting texture is always budgeted.
-    sk_sp<GrTexture> refScratchTexture(const GrSurfaceDesc& desc,
-                                       const GrBackendFormat& format,
-                                       GrRenderable renderable,
+    sk_sp<GrTexture> refScratchTexture(const GrSurfaceDesc&,
+                                       const GrBackendFormat&,
+                                       GrRenderable,
                                        int renderTargetSampleCnt,
-                                       GrProtected isProtected);
+                                       GrMipMapped,
+                                       GrProtected);
 
     /*
      * Try to find an existing scratch texture that exactly matches 'desc'. If successful
      * update the budgeting accordingly.
      */
-    sk_sp<GrTexture> getExactScratch(const GrSurfaceDesc& desc,
-                                     const GrBackendFormat& format,
-                                     GrRenderable renderable,
+    sk_sp<GrTexture> getExactScratch(const GrSurfaceDesc&,
+                                     const GrBackendFormat&,
+                                     GrRenderable,
                                      int renderTargetSampleCnt,
-                                     SkBudgeted budgeted,
-                                     GrProtected isProtected);
+                                     SkBudgeted,
+                                     GrMipMapped,
+                                     GrProtected);
 
     GrResourceCache* cache() { return fCache; }
     const GrResourceCache* cache() const { return fCache; }
diff --git a/src/gpu/GrSurfaceProxy.cpp b/src/gpu/GrSurfaceProxy.cpp
index 03c7e61..2abefc8 100644
--- a/src/gpu/GrSurfaceProxy.cpp
+++ b/src/gpu/GrSurfaceProxy.cpp
@@ -281,7 +281,7 @@
     int height = this->worstCaseHeight();
 
     GrTexturePriv::ComputeScratchKey(this->config(), width, height, renderable, sampleCount,
-                                     mipMapped, key);
+                                     mipMapped, fIsProtected, key);
 }
 
 void GrSurfaceProxy::setLastRenderTask(GrRenderTask* renderTask) {
diff --git a/src/gpu/GrTexture.cpp b/src/gpu/GrTexture.cpp
index 371aa8e..18c4ec2 100644
--- a/src/gpu/GrTexture.cpp
+++ b/src/gpu/GrTexture.cpp
@@ -86,13 +86,20 @@
             sampleCount = rt->numSamples();
             renderable = GrRenderable::kYes;
         }
+        auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
         GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), renderable,
-                                         sampleCount, this->texturePriv().mipMapped(), key);
+                                         sampleCount, this->texturePriv().mipMapped(), isProtected,
+                                         key);
     }
 }
 
-void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height,
-                                      GrRenderable renderable, int sampleCnt, GrMipMapped mipMapped,
+void GrTexturePriv::ComputeScratchKey(GrPixelConfig config,
+                                      int width,
+                                      int height,
+                                      GrRenderable renderable,
+                                      int sampleCnt,
+                                      GrMipMapped mipMapped,
+                                      GrProtected isProtected,
                                       GrScratchKey* key) {
     static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
     SkASSERT(width > 0);
@@ -102,22 +109,18 @@
 
     // make sure desc.fConfig fits in 5 bits
     SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
-    SkASSERT(static_cast<int>(config) < (1 << 5));
-    SkASSERT(sampleCnt < (1 << 8));
-    SkASSERT(static_cast<int>(mipMapped) <= 1);
+    SkASSERT(static_cast<uint32_t>(config) < (1 << 5));
+    SkASSERT(static_cast<uint32_t>(mipMapped) <= 1);
+    SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
+    SkASSERT(static_cast<uint32_t>(renderable) <= 1);
+    SkASSERT(static_cast<uint32_t>(sampleCnt) < (1 << (32 - 8)));
 
     GrScratchKey::Builder builder(key, kType, 3);
     builder[0] = width;
     builder[1] = height;
-    builder[2] = config
-               | (static_cast<uint32_t>(mipMapped) << 5)
-               | (sampleCnt << 6)
-               | (static_cast<uint32_t>(renderable) << 14);
-}
-
-void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrRenderable renderable,
-                                      int sampleCnt, GrScratchKey* key) {
-    // Note: the fOrigin field is not used in the scratch key
-    return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, renderable, sampleCnt,
-                             GrMipMapped::kNo, key);
+    builder[2] = (static_cast<uint32_t>(config)      << 0)
+               | (static_cast<uint32_t>(mipMapped)   << 5)
+               | (static_cast<uint32_t>(isProtected) << 6)
+               | (static_cast<uint32_t>(renderable)  << 7)
+               | (static_cast<uint32_t>(sampleCnt)   << 8);
 }
diff --git a/src/gpu/GrTexturePriv.h b/src/gpu/GrTexturePriv.h
index 9476c78..4ce3e05 100644
--- a/src/gpu/GrTexturePriv.h
+++ b/src/gpu/GrTexturePriv.h
@@ -53,9 +53,14 @@
                                              : GrSamplerState::Filter::kMipMap;
     }
 
-    static void ComputeScratchKey(const GrSurfaceDesc&, GrRenderable, int sampleCnt, GrScratchKey*);
-    static void ComputeScratchKey(GrPixelConfig config, int width, int height, GrRenderable,
-                                  int sampleCnt, GrMipMapped, GrScratchKey* key);
+    static void ComputeScratchKey(GrPixelConfig config,
+                                  int width,
+                                  int height,
+                                  GrRenderable,
+                                  int sampleCnt,
+                                  GrMipMapped,
+                                  GrProtected,
+                                  GrScratchKey* key);
 
 private:
     GrTexturePriv(GrTexture* texture) : fTexture(texture) { }
diff --git a/src/gpu/mock/GrMockTexture.h b/src/gpu/mock/GrMockTexture.h
index 88b922f..edb231e 100644
--- a/src/gpu/mock/GrMockTexture.h
+++ b/src/gpu/mock/GrMockTexture.h
@@ -195,11 +195,8 @@
                                       this->texturePriv().mipMapped());
     }
 
-    void computeScratchKey(GrScratchKey* key) const override {
-        GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
-                                         GrRenderable::kYes, this->numSamples(),
-                                         this->texturePriv().mipMapped(), key);
-    }
+    // This avoids an inherits via dominance warning on MSVC.
+    void computeScratchKey(GrScratchKey* key) const override { GrTexture::computeScratchKey(key); }
 };
 
 #endif