diff options
author | Robert Phillips <robertphillips@google.com> | 2017-03-03 11:10:18 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-03-03 17:40:52 +0000 |
commit | 4447b64a88ea141161fca772c2fec28b6141bbc3 (patch) | |
tree | fce75d30623892dfe7271acdd671623240302e10 | |
parent | 3c6d5b0daa8c9ff1137bb2ef9b00a712bb3a47c2 (diff) |
Switch SkImageGenerator over to generating GrTextureProxies
It does not seem irrational for generateTexture to always receive a valid GrContext. lockAsBitmap can do as it pleases.
This is split out of: https://skia-review.googlesource.com/c/8823/ (Remove GrFragmentProcessor-derived class' GrTexture-based ctors)
Change-Id: I8aebc813a8a3a7d694b7369c2c9810e2164fe16e
Reviewed-on: https://skia-review.googlesource.com/9191
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
-rw-r--r-- | gm/image_pict.cpp | 25 | ||||
-rw-r--r-- | include/core/SkImageGenerator.h | 21 | ||||
-rw-r--r-- | src/core/SkImageCacherator.cpp | 32 | ||||
-rw-r--r-- | src/core/SkImageCacherator.h | 2 | ||||
-rw-r--r-- | src/core/SkImageGenerator.cpp | 13 | ||||
-rw-r--r-- | src/core/SkPictureImageGenerator.cpp | 9 | ||||
-rw-r--r-- | src/core/SkPictureImageGenerator.h | 3 | ||||
-rw-r--r-- | src/image/SkImage_Generator.cpp | 2 |
8 files changed, 64 insertions, 43 deletions
diff --git a/gm/image_pict.cpp b/gm/image_pict.cpp index 387ceb8d41..1f273dc615 100644 --- a/gm/image_pict.cpp +++ b/gm/image_pict.cpp @@ -231,11 +231,10 @@ public: } } protected: - GrTexture* onGenerateTexture(GrContext* ctx, const SkImageInfo& info, - const SkIPoint& origin) override { - if (ctx) { - SkASSERT(ctx == fCtx.get()); - } + sk_sp<GrTextureProxy> onGenerateTexture(GrContext* ctx, const SkImageInfo& info, + const SkIPoint& origin) override { + SkASSERT(ctx); + SkASSERT(ctx == fCtx.get()); if (!fProxy) { return nullptr; @@ -243,7 +242,7 @@ protected: if (origin.fX == 0 && origin.fY == 0 && info.width() == fProxy->width() && info.height() == fProxy->height()) { - return SkSafeRef(fProxy->instantiate(fCtx->textureProvider())->asTexture()); + return fProxy; } // need to copy the subset into a new texture @@ -266,17 +265,14 @@ protected: return nullptr; } - GrSurface* dstSurf = dstContext->asSurfaceProxy()->instantiate(fCtx->textureProvider()); - if (!dstSurf) { - return nullptr; - } - - return SkRef(dstSurf->asTexture()); + return dstContext->asTextureProxyRef(); } + private: sk_sp<GrContext> fCtx; - sk_sp<GrSurfaceProxy> fProxy; + sk_sp<GrTextureProxy> fProxy; }; + static std::unique_ptr<SkImageGenerator> make_tex_generator(GrContext* ctx, sk_sp<SkPicture> pic) { const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); @@ -340,7 +336,8 @@ protected: static void draw_as_bitmap(SkCanvas* canvas, SkImageCacherator* cache, SkScalar x, SkScalar y) { SkBitmap bitmap; - cache->lockAsBitmap(&bitmap, nullptr, canvas->imageInfo().colorSpace()); + cache->lockAsBitmap(canvas->getGrContext(), &bitmap, nullptr, + canvas->imageInfo().colorSpace()); canvas->drawBitmap(bitmap, x, y); } diff --git a/include/core/SkImageGenerator.h b/include/core/SkImageGenerator.h index b193f48f01..6263dee574 100644 --- a/include/core/SkImageGenerator.h +++ b/include/core/SkImageGenerator.h @@ -16,7 +16,7 @@ class GrContext; class GrContextThreadSafeProxy; -class GrTexture; +class GrTextureProxy; class GrSamplerParams; class SkBitmap; class SkData; @@ -112,6 +112,7 @@ public: */ bool getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]); +#if SK_SUPPORT_GPU /** * If the generator can natively/efficiently return its pixels as a GPU image (backed by a * texture) this will return that image. If not, this will return NULL. @@ -130,16 +131,13 @@ public: * * Regarding the GrContext parameter: * - * The caller may pass NULL for the context. In that case the generator may assume that its - * internal context is current. If it has no internal context, then it should just return - * null. - * - * If the caller passes a non-null context, then the generator should only succeed if: - * - it has no intrinsic context, and will use the caller's + * It must be non-NULL. The generator should only succeed if: * - its internal context is the same * - it can somehow convert its texture into one that is valid for the provided context. */ - GrTexture* generateTexture(GrContext*, const SkImageInfo& info, const SkIPoint& origin); + sk_sp<GrTextureProxy> generateTexture(GrContext*, const SkImageInfo& info, + const SkIPoint& origin); +#endif /** * If the default image decoder system can interpret the specified (encoded) data, then @@ -179,9 +177,10 @@ protected: return false; } - virtual GrTexture* onGenerateTexture(GrContext*, const SkImageInfo&, const SkIPoint&) { - return nullptr; - } +#if SK_SUPPORT_GPU + virtual sk_sp<GrTextureProxy> onGenerateTexture(GrContext*, const SkImageInfo&, + const SkIPoint&); +#endif private: const SkImageInfo fInfo; diff --git a/src/core/SkImageCacherator.cpp b/src/core/SkImageCacherator.cpp index 17cac1b4ee..c33a008baa 100644 --- a/src/core/SkImageCacherator.cpp +++ b/src/core/SkImageCacherator.cpp @@ -208,7 +208,7 @@ bool SkImageCacherator::tryLockAsBitmap(SkBitmap* bitmap, const SkImage* client, return true; } -bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap, const SkImage* client, +bool SkImageCacherator::lockAsBitmap(GrContext* context, SkBitmap* bitmap, const SkImage* client, SkColorSpace* dstColorSpace, SkImage::CachingHint chint) { CachedFormat format = this->chooseCacheFormat(dstColorSpace); @@ -223,14 +223,19 @@ bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap, const SkImage* client, } #if SK_SUPPORT_GPU + if (!context) { + bitmap->reset(); + return false; + } + // Try to get a texture and read it back to raster (and then cache that with our ID) - sk_sp<GrTexture> tex; + sk_sp<GrTextureProxy> proxy; { ScopedGenerator generator(fSharedGenerator); - tex.reset(generator->generateTexture(nullptr, cacheInfo, fOrigin)); + proxy = generator->generateTexture(context, cacheInfo, fOrigin); } - if (!tex) { + if (!proxy) { bitmap->reset(); return false; } @@ -240,9 +245,15 @@ bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap, const SkImage* client, return false; } - if (!tex->readPixels(fInfo.colorSpace(), 0, 0, bitmap->width(), bitmap->height(), - SkImageInfo2GrPixelConfig(cacheInfo, *tex->getContext()->caps()), - cacheInfo.colorSpace(), bitmap->getPixels(), bitmap->rowBytes())) { + sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext( + proxy, + fInfo.refColorSpace())); // src colorSpace + if (!sContext) { + bitmap->reset(); + return false; + } + + if (!sContext->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { bitmap->reset(); return false; } @@ -542,10 +553,13 @@ GrTexture* SkImageCacherator::lockTexture(GrContext* ctx, const GrUniqueKey& ori // 2. Ask the generator to natively create one { ScopedGenerator generator(fSharedGenerator); - if (GrTexture* tex = generator->generateTexture(ctx, cacheInfo, fOrigin)) { + if (sk_sp<GrTextureProxy> proxy = generator->generateTexture(ctx, cacheInfo, fOrigin)) { SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath, kLockTexturePathCount); - return set_key_and_return(ctx->textureProvider(), tex, key); + GrTexture* tex2 = proxy->instantiate(ctx->textureProvider()); + if (tex2) { + return set_key_and_return(ctx->textureProvider(), SkSafeRef(tex2), key); + } } } diff --git a/src/core/SkImageCacherator.h b/src/core/SkImageCacherator.h index 97339360bd..38d14d06ec 100644 --- a/src/core/SkImageCacherator.h +++ b/src/core/SkImageCacherator.h @@ -48,7 +48,7 @@ public: * If not NULL, the client will be notified (->notifyAddedToCache()) when resources are * added to the cache on its behalf. */ - bool lockAsBitmap(SkBitmap*, const SkImage* client, SkColorSpace* dstColorSpace, + bool lockAsBitmap(GrContext*, SkBitmap*, const SkImage* client, SkColorSpace* dstColorSpace, SkImage::CachingHint = SkImage::kAllow_CachingHint); /** diff --git a/src/core/SkImageGenerator.cpp b/src/core/SkImageGenerator.cpp index 77f2c38618..18a6ce2669 100644 --- a/src/core/SkImageGenerator.cpp +++ b/src/core/SkImageGenerator.cpp @@ -77,8 +77,11 @@ bool SkImageGenerator::getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes return this->onGetYUV8Planes(sizeInfo, planes); } -GrTexture* SkImageGenerator::generateTexture(GrContext* ctx, const SkImageInfo& info, - const SkIPoint& origin) { +#if SK_SUPPORT_GPU +#include "GrTextureProxy.h" + +sk_sp<GrTextureProxy> SkImageGenerator::generateTexture(GrContext* ctx, const SkImageInfo& info, + const SkIPoint& origin) { SkIRect srcRect = SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height()); if (!SkIRect::MakeWH(fInfo.width(), fInfo.height()).contains(srcRect)) { return nullptr; @@ -86,6 +89,12 @@ GrTexture* SkImageGenerator::generateTexture(GrContext* ctx, const SkImageInfo& return this->onGenerateTexture(ctx, info, origin); } +sk_sp<GrTextureProxy> SkImageGenerator::onGenerateTexture(GrContext*, const SkImageInfo&, + const SkIPoint&) { + return nullptr; +} +#endif + ///////////////////////////////////////////////////////////////////////////////////////////// SkData* SkImageGenerator::onRefEncodedData(GrContext* ctx) { diff --git a/src/core/SkPictureImageGenerator.cpp b/src/core/SkPictureImageGenerator.cpp index 44a6b82895..a1d919af34 100644 --- a/src/core/SkPictureImageGenerator.cpp +++ b/src/core/SkPictureImageGenerator.cpp @@ -94,10 +94,11 @@ SkImageGenerator::MakeFromPicture(const SkISize& size, sk_sp<SkPicture> picture, /////////////////////////////////////////////////////////////////////////////////////////////////// #if SK_SUPPORT_GPU -#include "GrTexture.h" +sk_sp<GrTextureProxy> SkPictureImageGenerator::onGenerateTexture(GrContext* ctx, + const SkImageInfo& info, + const SkIPoint& origin) { + SkASSERT(ctx); -GrTexture* SkPictureImageGenerator::onGenerateTexture(GrContext* ctx, const SkImageInfo& info, - const SkIPoint& origin) { // // TODO: respect the usage, by possibly creating a different (pow2) surface // @@ -114,6 +115,6 @@ GrTexture* SkPictureImageGenerator::onGenerateTexture(GrContext* ctx, const SkIm if (!image) { return nullptr; } - return SkSafeRef(as_IB(image)->peekTexture()); + return as_IB(image)->asTextureProxyRef(); } #endif diff --git a/src/core/SkPictureImageGenerator.h b/src/core/SkPictureImageGenerator.h index ec63e30fe3..83872608a5 100644 --- a/src/core/SkPictureImageGenerator.h +++ b/src/core/SkPictureImageGenerator.h @@ -21,7 +21,8 @@ protected: int* ctableCount) override; #if SK_SUPPORT_GPU - GrTexture* onGenerateTexture(GrContext*, const SkImageInfo&, const SkIPoint&) override; + sk_sp<GrTextureProxy> onGenerateTexture(GrContext*, const SkImageInfo&, + const SkIPoint&) override; #endif private: diff --git a/src/image/SkImage_Generator.cpp b/src/image/SkImage_Generator.cpp index c38b7e4937..14516e4d5c 100644 --- a/src/image/SkImage_Generator.cpp +++ b/src/image/SkImage_Generator.cpp @@ -74,7 +74,7 @@ SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const { bool SkImage_Generator::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace, CachingHint chint) const { - return fCache.lockAsBitmap(bitmap, this, dstColorSpace, chint); + return fCache.lockAsBitmap(nullptr, bitmap, this, dstColorSpace, chint); } GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, const GrSamplerParams& params, |