Update GrTextureMaker to handle mips when copying for npot

Bug: skia:
Change-Id: I4b5ea3b8fadf207aef521404c4e9205df23ef3c8
Reviewed-on: https://skia-review.googlesource.com/65900
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
diff --git a/src/gpu/GrTextureMaker.cpp b/src/gpu/GrTextureMaker.cpp
index a60a745..e234180 100644
--- a/src/gpu/GrTextureMaker.cpp
+++ b/src/gpu/GrTextureMaker.cpp
@@ -45,25 +45,35 @@
     GrSurfaceOrigin origOrigin;
     GrUniqueKey copyKey;
     this->makeCopyKey(copyParams, &copyKey, dstColorSpace);
+    sk_sp<GrTextureProxy> cachedProxy;
     if (copyKey.isValid()) {
         if (original) {
             origOrigin = original->origin();
         } else {
             origOrigin = kTopLeft_GrSurfaceOrigin;
         }
-        sk_sp<GrTextureProxy> result(fContext->resourceProvider()->findOrCreateProxyByUniqueKey(
-                                                                            copyKey, origOrigin));
-        if (result) {
-            return result;
+        cachedProxy = fContext->resourceProvider()->findOrCreateProxyByUniqueKey(copyKey,
+                                                                                 origOrigin);
+        if (cachedProxy && (!willBeMipped || GrMipMapped::kYes == cachedProxy->mipMapped())) {
+            return cachedProxy;
         }
     }
 
     sk_sp<GrTextureProxy> result;
     if (original) {
-        result = CopyOnGpu(fContext, std::move(original), nullptr, copyParams, willBeMipped);
+        result = std::move(original);
+    } else if (cachedProxy) {
+        result = cachedProxy;
     } else {
-        result = this->generateTextureProxyForParams(copyParams, willBeMipped, dstColorSpace);
+        // Since we will be copying this texture there is no reason to make it mipped
+        result = this->refOriginalTextureProxy(false, dstColorSpace,
+                                               AllowedTexGenType::kAny);
     }
+    if (!result) {
+        return nullptr;
+    }
+
+    result = CopyOnGpu(fContext, std::move(result), nullptr, copyParams, willBeMipped);
 
     if (!result) {
         return nullptr;
@@ -71,6 +81,14 @@
 
     if (copyKey.isValid()) {
         SkASSERT(result->origin() == origOrigin);
+        if (cachedProxy) {
+            SkASSERT(GrMipMapped::kYes == result->mipMapped() &&
+                     GrMipMapped::kNo == cachedProxy->mipMapped());
+            // If we had a cachedProxy, that means there already is a proxy in the cache which
+            // matches the key, but it does not have mip levels and we require them. Thus we must
+            // remove the unique key from that proxy.
+            fContext->resourceProvider()->removeUniqueKeyFromProxy(copyKey, cachedProxy.get());
+        }
         fContext->resourceProvider()->assignUniqueKeyToProxy(copyKey, result.get());
         this->didCacheCopy(copyKey);
     }