Re-arrange GrResourceProvider's texture creation methods
authorRobert Phillips <robertphillips@google.com>
Mon, 17 Apr 2017 16:57:27 +0000 (12:57 -0400)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Mon, 17 Apr 2017 18:11:03 +0000 (18:11 +0000)
This is all in service of removing the last raw writeSurfacePixels (that we can do anything about)

Change-Id: Ic84f677b2440b20e5fcc23e787584db0cfaecd01
Reviewed-on: https://skia-review.googlesource.com/13584
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
src/gpu/GrResourceProvider.cpp
src/gpu/GrResourceProvider.h
src/gpu/GrSurfaceProxy.cpp
src/gpu/SkGr.cpp

index d2ec204d6c78cd5e48e6b11fa226fdaa25c3012b..6299264e21f065d491030ed900d151e69a3154be 100644 (file)
@@ -22,6 +22,7 @@
 #include "GrSurfaceProxyPriv.h"
 #include "GrTexturePriv.h"
 #include "../private/GrSingleOwner.h"
+#include "SkGr.h"
 #include "SkMathPriv.h"
 
 GR_DECLARE_STATIC_UNIQUE_KEY(gQuadIndexBufferKey);
@@ -54,7 +55,6 @@ sk_sp<GrTextureProxy> GrResourceProvider::createMipMappedTexture(
                                                       SkBudgeted budgeted,
                                                       const GrMipLevel* texels,
                                                       int mipLevelCount,
-                                                      uint32_t flags,
                                                       SkDestinationSurfaceColorMode mipColorMode) {
     ASSERT_SINGLE_OWNER
 
@@ -63,17 +63,17 @@ sk_sp<GrTextureProxy> GrResourceProvider::createMipMappedTexture(
             return nullptr;
         }
         return GrSurfaceProxy::MakeDeferred(this, desc, budgeted, nullptr, 0);
+    } else if (1 == mipLevelCount) {
+        if (!texels) {
+            return nullptr;
+        }
+        return this->createTextureProxy(desc, budgeted, texels[0]);
     }
 
     if (this->isAbandoned()) {
         return nullptr;
     }
 
-    for (int i = 0; i < mipLevelCount; ++i) {
-        if (!texels[i].fPixels) {
-            return nullptr;
-        }
-    }
     if (mipLevelCount > 1 && GrPixelConfigIsSint(desc.fConfig)) {
         return nullptr;
     }
@@ -81,28 +81,13 @@ sk_sp<GrTextureProxy> GrResourceProvider::createMipMappedTexture(
         !fGpu->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
         return nullptr;
     }
-    if (!GrPixelConfigIsCompressed(desc.fConfig)) {
-        if (mipLevelCount < 2) {
-            flags |= kExact_Flag | kNoCreate_Flag;
-            sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
-            if (tex) {
-                sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(tex);
-
-                if (fGpu->getContext()->contextPriv().writeSurfacePixels(
-                                proxy.get(), nullptr, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
-                                nullptr, texels[0].fPixels, texels[0].fRowBytes)) {
-                    if (SkBudgeted::kNo == budgeted) {
-                        tex->resourcePriv().makeUnbudgeted();
-                    }
-                    tex->texturePriv().setMipColorMode(mipColorMode);
-                    return proxy;
-                }
-            }
-        }
-    }
 
     SkTArray<GrMipLevel> texelsShallowCopy(mipLevelCount);
     for (int i = 0; i < mipLevelCount; ++i) {
+        if (!texels[i].fPixels) {
+            return nullptr;
+        }
+
         texelsShallowCopy.push_back(texels[i]);
     }
     sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texelsShallowCopy));
@@ -113,6 +98,60 @@ sk_sp<GrTextureProxy> GrResourceProvider::createMipMappedTexture(
     return GrSurfaceProxy::MakeWrapped(std::move(tex));
 }
 
+sk_sp<GrTexture> GrResourceProvider::getExactScratch(const GrSurfaceDesc& desc,
+                                                     SkBudgeted budgeted, uint32_t flags) {
+
+    flags |= kExact_Flag | kNoCreate_Flag;
+    sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
+    if (tex && SkBudgeted::kNo == budgeted) {
+        tex->resourcePriv().makeUnbudgeted();
+    }
+
+    return tex;
+}
+
+static bool make_info(int w, int h, GrPixelConfig config, SkImageInfo* ii) {
+    SkColorType colorType;
+    if (!GrPixelConfigToColorType(config, &colorType)) {
+        return false;
+    }
+
+    *ii = SkImageInfo::Make(w, h, colorType, kUnknown_SkAlphaType, nullptr);
+    return true;
+}
+
+sk_sp<GrTextureProxy> GrResourceProvider::createTextureProxy(const GrSurfaceDesc& desc,
+                                                             SkBudgeted budgeted,
+                                                             const GrMipLevel& mipLevel) {
+    if (!mipLevel.fPixels) {
+        return nullptr;
+    }
+
+    GrContext* context = fGpu->getContext();
+
+    if (!GrPixelConfigIsCompressed(desc.fConfig)) {
+        SkImageInfo srcInfo;
+
+        if (make_info(desc.fWidth, desc.fHeight, desc.fConfig, &srcInfo)) {
+            sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, 0);
+            sk_sp<GrSurfaceContext> sContext =
+                                context->contextPriv().makeWrappedSurfaceContext(std::move(tex));
+            if (sContext) {
+                if (sContext->writePixels(srcInfo, mipLevel.fPixels, mipLevel.fRowBytes, 0, 0)) {
+                    return sContext->asTextureProxyRef();
+                }
+            }
+        }
+    }
+
+    SkTArray<GrMipLevel> texels(1);
+    texels.push_back(mipLevel);
+
+    sk_sp<GrTexture> tex(fGpu->createTexture(desc, budgeted, texels));
+    return GrSurfaceProxy::MakeWrapped(std::move(tex));
+}
+
+
 sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
                                                    uint32_t flags) {
     ASSERT_SINGLE_OWNER
@@ -127,12 +166,8 @@ sk_sp<GrTexture> GrResourceProvider::createTexture(const GrSurfaceDesc& desc, Sk
     }
 
     if (!GrPixelConfigIsCompressed(desc.fConfig)) {
-        flags |= kExact_Flag | kNoCreate_Flag;
-        sk_sp<GrTexture> tex(this->refScratchTexture(desc, flags));
+        sk_sp<GrTexture> tex = this->getExactScratch(desc, budgeted, flags);
         if (tex) {
-            if (SkBudgeted::kNo == budgeted) {
-                tex->resourcePriv().makeUnbudgeted();
-            }
             return tex;
         }
     }
index 91dd09f01d7a14c055b399c28f46ef2cd6bd6314..676d82febc63599044fb4926f62539aff556c093 100644 (file)
@@ -49,7 +49,6 @@ public:
      */
     sk_sp<GrTextureProxy> createMipMappedTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
                                                  const GrMipLevel* texels, int mipLevelCount,
-                                                 uint32_t flags = 0,
                                                  SkDestinationSurfaceColorMode mipColorMode =
                                                         SkDestinationSurfaceColorMode::kLegacy);
 
@@ -71,8 +70,9 @@ public:
 
     /** Create an exact fit texture with no initial data to upload.
      */
-    sk_sp<GrTexture> createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
-                                   uint32_t flags = 0);
+    sk_sp<GrTexture> createTexture(const GrSurfaceDesc&, SkBudgeted, uint32_t flags = 0);
+
+    sk_sp<GrTextureProxy> createTextureProxy(const GrSurfaceDesc&, SkBudgeted, const GrMipLevel&);
 
     ///////////////////////////////////////////////////////////////////////////
     // Wrapped Backend Surfaces
@@ -245,6 +245,12 @@ private:
 
     GrTexture* refScratchTexture(const GrSurfaceDesc&, uint32_t scratchTextureFlags);
 
+    /*
+     * Try to find an existing scratch texture that exactly matches 'desc'. If successful
+     * update the budgeting accordingly.
+     */
+    sk_sp<GrTexture> getExactScratch(const GrSurfaceDesc&, SkBudgeted, uint32_t flags);
+
     GrResourceCache* cache() { return fCache; }
     const GrResourceCache* cache() const { return fCache; }
 
index c9a36289d8697df0e3e73170d744057abb5f6625..3ddee5830896af832ce84b1a3e844e68b22aa581 100644 (file)
@@ -147,8 +147,6 @@ sk_sp<GrTextureProxy> GrSurfaceProxy::MakeWrapped(sk_sp<GrTexture> tex) {
     }
 }
 
-#include "GrResourceProvider.h"
-
 sk_sp<GrTextureProxy> GrSurfaceProxy::MakeDeferred(GrResourceProvider* resourceProvider,
                                                    const GrSurfaceDesc& desc,
                                                    SkBackingFit fit,
@@ -232,16 +230,9 @@ sk_sp<GrTextureProxy> GrSurfaceProxy::MakeDeferred(GrResourceProvider* resourceP
                                                    const void* srcData,
                                                    size_t rowBytes) {
     if (srcData) {
-        GrMipLevel tempTexels;
-        GrMipLevel* texels = nullptr;
-        int levelCount = 0;
-        if (srcData) {
-            tempTexels.fPixels = srcData;
-            tempTexels.fRowBytes = rowBytes;
-            texels = &tempTexels;
-            levelCount = 1;
-        }
-        return resourceProvider->createMipMappedTexture(desc, budgeted, texels, levelCount);
+        GrMipLevel mipLevel = { srcData, rowBytes };
+
+        return resourceProvider->createTextureProxy(desc, budgeted, mipLevel);
     }
 
     return GrSurfaceProxy::MakeDeferred(resourceProvider, desc, SkBackingFit::kExact, budgeted);
index e4b31558ae9f9d65f74c2354092155a15f3c276e..35030289d398b5d751be529bc3b00b252ac8655b 100644 (file)
@@ -245,7 +245,7 @@ sk_sp<GrTextureProxy> GrGenerateMipMapsAndUploadToTextureProxy(GrContext* ctx,
                                                            SkBudgeted::kYes,
                                                            texels.get(),
                                                            mipLevelCount,
-                                                           0, colorMode);
+                                                           colorMode);
 }
 
 sk_sp<GrTextureProxy> GrUploadMipMapToTextureProxy(GrContext* ctx, const SkImageInfo& info,
@@ -259,7 +259,7 @@ sk_sp<GrTextureProxy> GrUploadMipMapToTextureProxy(GrContext* ctx, const SkImage
     const GrCaps* caps = ctx->caps();
     return ctx->resourceProvider()->createMipMappedTexture(GrImageInfoToSurfaceDesc(info, *caps),
                                                            SkBudgeted::kYes, texels,
-                                                           mipLevelCount, 0, colorMode);
+                                                           mipLevelCount, colorMode);
 }
 
 sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext* ctx,