Switch GrYUVProvider over to GrTextureProxies
authorRobert Phillips <robertphillips@google.com>
Wed, 8 Mar 2017 19:32:55 +0000 (14:32 -0500)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Wed, 8 Mar 2017 22:37:42 +0000 (22:37 +0000)
This is split out of: https://skia-review.googlesource.com/c/8823/ (Remove GrFragmentProcessor-derived class' GrTexture-based ctors)

Change-Id: I302e6b4c1ffed449a990288ec06f2dfdcdadf1f8
Reviewed-on: https://skia-review.googlesource.com/9448
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>

src/core/SkImageCacherator.cpp
src/gpu/GrTextureToYUVPlanes.cpp
src/gpu/GrTextureToYUVPlanes.h
src/gpu/GrYUVProvider.cpp
src/gpu/GrYUVProvider.h
src/image/SkImage.cpp
src/image/SkImage_Base.h
src/image/SkImage_Gpu.cpp
src/image/SkImage_Gpu.h

index 196723c..1d2f0d1 100644 (file)
@@ -557,7 +557,7 @@ GrTexture* SkImageCacherator::lockTexture(GrContext* ctx, const GrUniqueKey& ori
                                      kLockTexturePathCount);
             GrTexture* tex2 = proxy->instantiate(ctx->resourceProvider());
             if (tex2) {
-                return set_key_and_return(ctx->resourceProvider(), SkSafeRef(tex2), key);
+                return set_key_and_return(ctx->resourceProvider(), SkRef(tex2), key);
             }
         }
     }
@@ -581,11 +581,13 @@ GrTexture* SkImageCacherator::lockTexture(GrContext* ctx, const GrUniqueKey& ori
     if (!ctx->contextPriv().disableGpuYUVConversion()) {
         ScopedGenerator generator(fSharedGenerator);
         Generator_GrYUVProvider provider(generator);
-        sk_sp<GrTexture> tex = provider.refAsTexture(ctx, desc, true);
-        if (tex) {
+        if (sk_sp<GrTextureProxy> proxy = provider.refAsTextureProxy(ctx, desc, true)) {
             SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath,
                                      kLockTexturePathCount);
-            return set_key_and_return(ctx->resourceProvider(), tex.release(), key);
+            GrTexture* tex2 = proxy->instantiate(ctx->resourceProvider());
+            if (tex2) {
+                return set_key_and_return(ctx->resourceProvider(), SkRef(tex2), key);
+            }
         }
     }
 
index 683bd63..7d01626 100644 (file)
@@ -19,8 +19,9 @@ namespace {
                                                       SkYUVColorSpace colorSpace);
 };
 
-static bool convert_texture(GrTexture* src, GrRenderTargetContext* dst, int dstW, int dstH,
-                            SkYUVColorSpace colorSpace, MakeFPProc proc) {
+static bool convert_proxy(GrContext* context, sk_sp<GrTextureProxy> src,
+                          GrRenderTargetContext* dst, int dstW, int dstH,
+                          SkYUVColorSpace colorSpace, MakeFPProc proc) {
 
     SkScalar xScale = SkIntToScalar(src->width()) / dstW;
     SkScalar yScale = SkIntToScalar(src->height()) / dstH;
@@ -31,8 +32,9 @@ static bool convert_texture(GrTexture* src, GrRenderTargetContext* dst, int dstW
         filter = GrSamplerParams::kBilerp_FilterMode;
     }
 
-    sk_sp<GrFragmentProcessor> fp(
-            GrSimpleTextureEffect::Make(src, nullptr, SkMatrix::MakeScale(xScale, yScale), filter));
+    sk_sp<GrFragmentProcessor> fp(GrSimpleTextureEffect::Make(context, std::move(src), nullptr,
+                                                              SkMatrix::MakeScale(xScale, yScale),
+                                                              filter));
     if (!fp) {
         return false;
     }
@@ -48,9 +50,14 @@ static bool convert_texture(GrTexture* src, GrRenderTargetContext* dst, int dstW
     return true;
 }
 
-bool GrTextureToYUVPlanes(GrTexture* texture, const SkISize sizes[3], void* const planes[3],
+bool GrTextureToYUVPlanes(GrContext* context, sk_sp<GrTextureProxy> proxy,
+                          const SkISize sizes[3], void* const planes[3],
                           const size_t rowBytes[3], SkYUVColorSpace colorSpace) {
-    if (GrContext* context = texture->getContext()) {
+    if (!context) {
+        return false;
+    }
+
+    {
         // Depending on the relative sizes of the y, u, and v planes we may do 1 to 3 draws/
         // readbacks.
         sk_sp<GrRenderTargetContext> yuvRenderTargetContext;
@@ -114,34 +121,34 @@ bool GrTextureToYUVPlanes(GrTexture* texture, const SkISize sizes[3], void* cons
 
         // Do all the draws before any readback.
         if (yuvRenderTargetContext) {
-            if (!convert_texture(texture, yuvRenderTargetContext.get(),
-                                 sizes[0].fWidth, sizes[0].fHeight,
-                                 colorSpace, GrYUVEffect::MakeRGBToYUV)) {
+            if (!convert_proxy(context, std::move(proxy), yuvRenderTargetContext.get(),
+                               sizes[0].fWidth, sizes[0].fHeight,
+                               colorSpace, GrYUVEffect::MakeRGBToYUV)) {
                 return false;
             }
         } else {
             SkASSERT(yRenderTargetContext);
-            if (!convert_texture(texture, yRenderTargetContext.get(),
-                                 sizes[0].fWidth, sizes[0].fHeight,
-                                 colorSpace, GrYUVEffect::MakeRGBToY)) {
+            if (!convert_proxy(context, proxy, yRenderTargetContext.get(),
+                               sizes[0].fWidth, sizes[0].fHeight,
+                               colorSpace, GrYUVEffect::MakeRGBToY)) {
                 return false;
             }
             if (uvRenderTargetContext) {
-                if (!convert_texture(texture, uvRenderTargetContext.get(),
-                                     sizes[1].fWidth, sizes[1].fHeight,
-                                     colorSpace,  GrYUVEffect::MakeRGBToUV)) {
+                if (!convert_proxy(context, std::move(proxy), uvRenderTargetContext.get(),
+                                   sizes[1].fWidth, sizes[1].fHeight,
+                                   colorSpace,  GrYUVEffect::MakeRGBToUV)) {
                     return false;
                 }
             } else {
                 SkASSERT(uRenderTargetContext && vRenderTargetContext);
-                if (!convert_texture(texture, uRenderTargetContext.get(),
-                                     sizes[1].fWidth, sizes[1].fHeight,
-                                     colorSpace, GrYUVEffect::MakeRGBToU)) {
+                if (!convert_proxy(context, proxy, uRenderTargetContext.get(),
+                                   sizes[1].fWidth, sizes[1].fHeight,
+                                   colorSpace, GrYUVEffect::MakeRGBToU)) {
                     return false;
                 }
-                if (!convert_texture(texture, vRenderTargetContext.get(),
-                                     sizes[2].fWidth, sizes[2].fHeight,
-                                     colorSpace, GrYUVEffect::MakeRGBToV)) {
+                if (!convert_proxy(context, std::move(proxy), vRenderTargetContext.get(),
+                                   sizes[2].fWidth, sizes[2].fHeight,
+                                   colorSpace, GrYUVEffect::MakeRGBToV)) {
                     return false;
                 }
             }
index 67b6fce..1dcbea3 100644 (file)
 #include "SkImageInfo.h"
 #include "SkSize.h"
 
-class GrTexture;
+class GrContext;
+class GrTextureProxy;
 
-bool GrTextureToYUVPlanes(GrTexture* texture, const SkISize[3], void* const planes[3],
+bool GrTextureToYUVPlanes(GrContext*, sk_sp<GrTextureProxy>,
+                          const SkISize[3], void* const planes[3],
                           const size_t rowBytes[3], SkYUVColorSpace);
 
 #endif
index ee2ad63..8d921a4 100644 (file)
@@ -84,9 +84,9 @@ bool YUVScoper::init(GrYUVProvider* provider, SkYUVPlanesCache::Info* yuvInfo, v
     return true;
 }
 
-sk_sp<GrTexture> GrYUVProvider::refAsTexture(GrContext* ctx,
-                                             const GrSurfaceDesc& desc,
-                                             bool useCache) {
+sk_sp<GrTextureProxy> GrYUVProvider::refAsTextureProxy(GrContext* ctx,
+                                                       const GrSurfaceDesc& desc,
+                                                       bool useCache) {
     SkYUVPlanesCache::Info yuvInfo;
     void* planes[3];
     YUVScoper scoper;
@@ -159,5 +159,5 @@ sk_sp<GrTexture> GrYUVProvider::refAsTexture(GrContext* ctx,
 
     renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), r);
 
-    return renderTargetContext->asTexture();
+    return renderTargetContext->asTextureProxyRef();
 }
index c32af15..ef3ae6d 100644 (file)
@@ -28,14 +28,14 @@ public:
     virtual ~GrYUVProvider() {}
 
     /**
-     *  On success, this returns a texture that has converted the YUV data from the provider
+     *  On success, this returns a texture proxy that has converted the YUV data from the provider
      *  into a form that is supported by the GPU (typically transformed into RGB). If useCache
      *  is true, then the texture will automatically have a key added, so it can be retrieved
      *  from the cache (assuming it is requested by a provider w/ the same genID).
      *
      *  On failure (e.g. the provider had no data), this returns NULL.
      */
-    sk_sp<GrTexture> refAsTexture(GrContext*, const GrSurfaceDesc&, bool useCache);
+    sk_sp<GrTextureProxy> refAsTextureProxy(GrContext*, const GrSurfaceDesc&, bool useCache);
 
     virtual uint32_t onGetID() = 0;
 
index a131de1..d76276b 100644 (file)
@@ -21,6 +21,7 @@
 #include "SkPicture.h"
 #include "SkPixelRef.h"
 #include "SkPixelSerializer.h"
+#include "SkRGBAToYUV.h"
 #include "SkReadPixelsRec.h"
 #include "SkSpecialImage.h"
 #include "SkStream.h"
@@ -211,28 +212,20 @@ SkImage_Base::~SkImage_Base() {
     }
 }
 
+bool SkImage_Base::onReadYUV8Planes(const SkISize sizes[3], void* const planes[3],
+                                    const size_t rowBytes[3], SkYUVColorSpace colorSpace) const {
+    return SkRGBAToYUV(this, sizes, planes, rowBytes, colorSpace);
+}
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const {
     return this->readPixels(pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX, srcY, chint);
 }
 
-#if SK_SUPPORT_GPU
-#include "GrTextureToYUVPlanes.h"
-#endif
-
-#include "SkRGBAToYUV.h"
-
 bool SkImage::readYUV8Planes(const SkISize sizes[3], void* const planes[3],
                              const size_t rowBytes[3], SkYUVColorSpace colorSpace) const {
-#if SK_SUPPORT_GPU
-    if (GrTexture* texture = as_IB(this)->peekTexture()) {
-        if (GrTextureToYUVPlanes(texture, sizes, planes, rowBytes, colorSpace)) {
-            return true;
-        }
-    }
-#endif
-    return SkRGBAToYUV(this, sizes, planes, rowBytes, colorSpace);
+    return as_IB(this)->onReadYUV8Planes(sizes, planes, rowBytes, colorSpace);
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
index 8049194..cc53358 100644 (file)
@@ -41,6 +41,9 @@ public:
 
     virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
 
+    virtual bool onReadYUV8Planes(const SkISize sizes[3], void* const planes[3],
+                                  const size_t rowBytes[3], SkYUVColorSpace colorSpace) const;
+
     virtual bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
                               int srcX, int srcY, CachingHint) const = 0;
 
index bac4b40..9ca790c 100644 (file)
@@ -22,6 +22,7 @@
 #include "GrTextureAdjuster.h"
 #include "GrTexturePriv.h"
 #include "GrTextureProxy.h"
+#include "GrTextureToYUVPlanes.h"
 #include "effects/GrYUVEffect.h"
 #include "SkCanvas.h"
 #include "SkCrossContextImageData.h"
@@ -134,6 +135,18 @@ static void apply_premul(const SkImageInfo& info, void* pixels, size_t rowBytes)
     }
 }
 
+bool SkImage_Gpu::onReadYUV8Planes(const SkISize sizes[3], void* const planes[3],
+                                   const size_t rowBytes[3], SkYUVColorSpace colorSpace) const {
+    if (sk_sp<GrTextureProxy> proxy = as_IB(this)->asTextureProxyRef()) {
+        if (GrTextureToYUVPlanes(fTexture->getContext(), std::move(proxy), sizes, planes,
+                                 rowBytes, colorSpace)) {
+            return true;
+        }
+    }
+
+    return INHERITED::onReadYUV8Planes(sizes, planes, rowBytes, colorSpace);
+}
+
 bool SkImage_Gpu::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
                                int srcX, int srcY, CachingHint) const {
     if (!SkImageInfoValidConversion(dstInfo, this->onImageInfo())) {
index c918b5c..419d4d1 100644 (file)
@@ -49,6 +49,10 @@ public:
         *uniqueID = this->uniqueID();
         return fTexture;
     }
+
+    bool onReadYUV8Planes(const SkISize sizes[3], void* const planes[3],
+                          const size_t rowBytes[3], SkYUVColorSpace colorSpace) const override;
+
     bool onReadPixels(const SkImageInfo&, void* dstPixels, size_t dstRowBytes,
                       int srcX, int srcY, CachingHint) const override;