From 538f1a36e3cd0327ee2639693143179ec0359151 Mon Sep 17 00:00:00 2001 From: Robert Phillips Date: Wed, 8 Mar 2017 14:32:55 -0500 Subject: [PATCH] Switch GrYUVProvider over to GrTextureProxies 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 Commit-Queue: Robert Phillips --- src/core/SkImageCacherator.cpp | 10 ++++---- src/gpu/GrTextureToYUVPlanes.cpp | 49 +++++++++++++++++++++++----------------- src/gpu/GrTextureToYUVPlanes.h | 6 +++-- src/gpu/GrYUVProvider.cpp | 8 +++---- src/gpu/GrYUVProvider.h | 4 ++-- src/image/SkImage.cpp | 21 ++++++----------- src/image/SkImage_Base.h | 3 +++ src/image/SkImage_Gpu.cpp | 13 +++++++++++ src/image/SkImage_Gpu.h | 4 ++++ 9 files changed, 71 insertions(+), 47 deletions(-) diff --git a/src/core/SkImageCacherator.cpp b/src/core/SkImageCacherator.cpp index 196723c..1d2f0d1 100644 --- a/src/core/SkImageCacherator.cpp +++ b/src/core/SkImageCacherator.cpp @@ -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 tex = provider.refAsTexture(ctx, desc, true); - if (tex) { + if (sk_sp 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); + } } } diff --git a/src/gpu/GrTextureToYUVPlanes.cpp b/src/gpu/GrTextureToYUVPlanes.cpp index 683bd63..7d01626 100644 --- a/src/gpu/GrTextureToYUVPlanes.cpp +++ b/src/gpu/GrTextureToYUVPlanes.cpp @@ -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 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 fp( - GrSimpleTextureEffect::Make(src, nullptr, SkMatrix::MakeScale(xScale, yScale), filter)); + sk_sp 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 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 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; } } diff --git a/src/gpu/GrTextureToYUVPlanes.h b/src/gpu/GrTextureToYUVPlanes.h index 67b6fce..1dcbea3 100644 --- a/src/gpu/GrTextureToYUVPlanes.h +++ b/src/gpu/GrTextureToYUVPlanes.h @@ -11,9 +11,11 @@ #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, + const SkISize[3], void* const planes[3], const size_t rowBytes[3], SkYUVColorSpace); #endif diff --git a/src/gpu/GrYUVProvider.cpp b/src/gpu/GrYUVProvider.cpp index ee2ad63..8d921a4 100644 --- a/src/gpu/GrYUVProvider.cpp +++ b/src/gpu/GrYUVProvider.cpp @@ -84,9 +84,9 @@ bool YUVScoper::init(GrYUVProvider* provider, SkYUVPlanesCache::Info* yuvInfo, v return true; } -sk_sp GrYUVProvider::refAsTexture(GrContext* ctx, - const GrSurfaceDesc& desc, - bool useCache) { +sk_sp GrYUVProvider::refAsTextureProxy(GrContext* ctx, + const GrSurfaceDesc& desc, + bool useCache) { SkYUVPlanesCache::Info yuvInfo; void* planes[3]; YUVScoper scoper; @@ -159,5 +159,5 @@ sk_sp GrYUVProvider::refAsTexture(GrContext* ctx, renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), r); - return renderTargetContext->asTexture(); + return renderTargetContext->asTextureProxyRef(); } diff --git a/src/gpu/GrYUVProvider.h b/src/gpu/GrYUVProvider.h index c32af15..ef3ae6d 100644 --- a/src/gpu/GrYUVProvider.h +++ b/src/gpu/GrYUVProvider.h @@ -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 refAsTexture(GrContext*, const GrSurfaceDesc&, bool useCache); + sk_sp refAsTextureProxy(GrContext*, const GrSurfaceDesc&, bool useCache); virtual uint32_t onGetID() = 0; diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp index a131de1..d76276b 100644 --- a/src/image/SkImage.cpp +++ b/src/image/SkImage.cpp @@ -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); } /////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/image/SkImage_Base.h b/src/image/SkImage_Base.h index 8049194..cc53358 100644 --- a/src/image/SkImage_Base.h +++ b/src/image/SkImage_Base.h @@ -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; diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp index bac4b40..9ca790c 100644 --- a/src/image/SkImage_Gpu.cpp +++ b/src/image/SkImage_Gpu.cpp @@ -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 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())) { diff --git a/src/image/SkImage_Gpu.h b/src/image/SkImage_Gpu.h index c918b5c..419d4d1 100644 --- a/src/image/SkImage_Gpu.h +++ b/src/image/SkImage_Gpu.h @@ -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; -- 2.7.4