From f1b4382421f24563f1df07942a98a234220e687e Mon Sep 17 00:00:00 2001 From: Brian Osman Date: Thu, 20 Apr 2017 13:43:23 -0400 Subject: Rename SkImage_Generator to SkImage_Lazy This removes a long-standing source of confusion: SkImage_Generator was an image that wrapped an SkImageGenerator (with an SkImageCacherator stuck in the middle). We could choose to rename either one, but SkImageGenerator is public, so take the easy road and rename the private image subclass. Given the existence of SkImage::isLazyGenerated, this name seems appropriate. Bug: skia: Change-Id: I061ece94f48538efb1dc5548010f6ca7d438a69b Reviewed-on: https://skia-review.googlesource.com/13979 Reviewed-by: Matt Sarett Commit-Queue: Brian Osman --- gm/imagemasksubset.cpp | 2 +- gn/core.gni | 2 +- src/core/SkImageCacherator.h | 2 +- src/image/SkImage_Generator.cpp | 133 ---------------------------------------- src/image/SkImage_Lazy.cpp | 133 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 136 deletions(-) delete mode 100644 src/image/SkImage_Generator.cpp create mode 100644 src/image/SkImage_Lazy.cpp diff --git a/gm/imagemasksubset.cpp b/gm/imagemasksubset.cpp index 49ff3c5405..cdd85d1449 100644 --- a/gm/imagemasksubset.cpp +++ b/gm/imagemasksubset.cpp @@ -63,7 +63,7 @@ const MakerT makers[] = { return make_mask(surface ? surface : SkSurface::MakeRaster(info)); }, - // SkImage_Generator + // SkImage_Lazy [](SkCanvas*, const SkImageInfo& info) -> sk_sp { return SkImage::MakeFromGenerator(skstd::make_unique(info)); }, diff --git a/gn/core.gni b/gn/core.gni index 797a350286..21f7ee8355 100644 --- a/gn/core.gni +++ b/gn/core.gni @@ -374,9 +374,9 @@ skia_core_sources = [ "$_src/core/SkShadowShader.h", "$_src/image/SkImage.cpp", - "$_src/image/SkImage_Generator.cpp", # "$_src/image/SkImage_Gpu.cpp", + "$_src/image/SkImage_Lazy.cpp", "$_src/image/SkImage_Raster.cpp", "$_src/image/SkImageShader.cpp", "$_src/image/SkImageShader.h", diff --git a/src/core/SkImageCacherator.h b/src/core/SkImageCacherator.h index ed3a4d95d6..df5e2f6b65 100644 --- a/src/core/SkImageCacherator.h +++ b/src/core/SkImageCacherator.h @@ -162,7 +162,7 @@ private: friend class GrImageTextureMaker; friend class SkImage; - friend class SkImage_Generator; + friend class SkImage_Lazy; }; #endif diff --git a/src/image/SkImage_Generator.cpp b/src/image/SkImage_Generator.cpp deleted file mode 100644 index 51fb281c26..0000000000 --- a/src/image/SkImage_Generator.cpp +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright 2015 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include "SkImage_Base.h" -#include "SkBitmap.h" -#include "SkData.h" -#include "SkImageCacherator.h" -#include "SkImagePriv.h" -#include "SkPixelRef.h" - -class SkImage_Generator : public SkImage_Base { -public: - SkImage_Generator(SkImageCacherator::Validator* validator) - : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID) - , fCache(validator) - {} - - virtual SkImageInfo onImageInfo() const override { - return fCache.info(); - } - SkAlphaType onAlphaType() const override { - return fCache.info().alphaType(); - } - - bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, - CachingHint) const override; -#if SK_SUPPORT_GPU - sk_sp asTextureProxyRef(GrContext*, const GrSamplerParams&, - SkColorSpace*, sk_sp*, - SkScalar scaleAdjust[2]) const override; -#endif - SkImageCacherator* peekCacherator() const override { return &fCache; } - SkData* onRefEncoded(GrContext*) const override; - sk_sp onMakeSubset(const SkIRect&) const override; - bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace, CachingHint) const override; - bool onIsLazyGenerated() const override { return true; } - sk_sp onMakeColorSpace(sk_sp) const override; - -private: - mutable SkImageCacherator fCache; - - typedef SkImage_Base INHERITED; -}; - -/////////////////////////////////////////////////////////////////////////////// - -bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, - int srcX, int srcY, CachingHint chint) const { - SkColorSpace* dstColorSpace = dstInfo.colorSpace(); - SkBitmap bm; - if (kDisallow_CachingHint == chint) { - SkImageCacherator::CachedFormat cacheFormat = fCache.chooseCacheFormat(dstColorSpace); - if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) { - return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); - } else { - // Try passing the caller's buffer directly down to the generator. If this fails we - // may still succeed in the general case, as the generator may prefer some other - // config, which we could then convert via SkBitmap::readPixels. - if (fCache.directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY, - SkTransferFunctionBehavior::kRespect)) { - return true; - } - // else fall through - } - } - - if (this->getROPixels(&bm, dstColorSpace, chint)) { - return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); - } - return false; -} - -SkData* SkImage_Generator::onRefEncoded(GrContext* ctx) const { - return fCache.refEncoded(ctx); -} - -bool SkImage_Generator::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace, - CachingHint chint) const { - return fCache.lockAsBitmap(nullptr, bitmap, this, dstColorSpace, chint); -} - -#if SK_SUPPORT_GPU -sk_sp SkImage_Generator::asTextureProxyRef(GrContext* context, - const GrSamplerParams& params, - SkColorSpace* dstColorSpace, - sk_sp* texColorSpace, - SkScalar scaleAdjust[2]) const { - return fCache.lockAsTextureProxy(context, params, dstColorSpace, - texColorSpace, this, scaleAdjust); -} -#endif - -sk_sp SkImage_Generator::onMakeSubset(const SkIRect& subset) const { - SkASSERT(fCache.info().bounds().contains(subset)); - SkASSERT(fCache.info().bounds() != subset); - - const SkIRect generatorSubset = subset.makeOffset(fCache.fOrigin.x(), fCache.fOrigin.y()); - SkImageCacherator::Validator validator(fCache.fSharedGenerator, &generatorSubset); - return validator ? sk_sp(new SkImage_Generator(&validator)) : nullptr; -} - -sk_sp SkImage_Generator::onMakeColorSpace(sk_sp target) const { - SkBitmap dst; - SkImageInfo dstInfo = fCache.info().makeColorSpace(target); - if (kIndex_8_SkColorType == dstInfo.colorType() || - kGray_8_SkColorType == dstInfo.colorType() || - kRGB_565_SkColorType == dstInfo.colorType()) { - dstInfo = dstInfo.makeColorType(kN32_SkColorType); - } - dst.allocPixels(dstInfo); - - // Use kIgnore for transfer function behavior. This is used by the SkColorSpaceXformCanvas, - // which wants to pre-xform the inputs but ignore the transfer function on blends. - if (!fCache.directGeneratePixels(dstInfo, dst.getPixels(), dst.rowBytes(), 0, 0, - SkTransferFunctionBehavior::kIgnore)) { - return nullptr; - } - - dst.setImmutable(); - return SkImage::MakeFromBitmap(dst); -} - -sk_sp SkImage::MakeFromGenerator(std::unique_ptr generator, - const SkIRect* subset) { - SkImageCacherator::Validator validator( - SkImageCacherator::SharedGenerator::Make(std::move(generator)), subset); - - return validator ? sk_make_sp(&validator) : nullptr; -} diff --git a/src/image/SkImage_Lazy.cpp b/src/image/SkImage_Lazy.cpp new file mode 100644 index 0000000000..b1f243b151 --- /dev/null +++ b/src/image/SkImage_Lazy.cpp @@ -0,0 +1,133 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkImage_Base.h" +#include "SkBitmap.h" +#include "SkData.h" +#include "SkImageCacherator.h" +#include "SkImagePriv.h" +#include "SkPixelRef.h" + +class SkImage_Lazy : public SkImage_Base { +public: + SkImage_Lazy(SkImageCacherator::Validator* validator) + : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID) + , fCache(validator) + {} + + virtual SkImageInfo onImageInfo() const override { + return fCache.info(); + } + SkAlphaType onAlphaType() const override { + return fCache.info().alphaType(); + } + + bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, + CachingHint) const override; +#if SK_SUPPORT_GPU + sk_sp asTextureProxyRef(GrContext*, const GrSamplerParams&, + SkColorSpace*, sk_sp*, + SkScalar scaleAdjust[2]) const override; +#endif + SkImageCacherator* peekCacherator() const override { return &fCache; } + SkData* onRefEncoded(GrContext*) const override; + sk_sp onMakeSubset(const SkIRect&) const override; + bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace, CachingHint) const override; + bool onIsLazyGenerated() const override { return true; } + sk_sp onMakeColorSpace(sk_sp) const override; + +private: + mutable SkImageCacherator fCache; + + typedef SkImage_Base INHERITED; +}; + +/////////////////////////////////////////////////////////////////////////////// + +bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, + int srcX, int srcY, CachingHint chint) const { + SkColorSpace* dstColorSpace = dstInfo.colorSpace(); + SkBitmap bm; + if (kDisallow_CachingHint == chint) { + SkImageCacherator::CachedFormat cacheFormat = fCache.chooseCacheFormat(dstColorSpace); + if (fCache.lockAsBitmapOnlyIfAlreadyCached(&bm, cacheFormat)) { + return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); + } else { + // Try passing the caller's buffer directly down to the generator. If this fails we + // may still succeed in the general case, as the generator may prefer some other + // config, which we could then convert via SkBitmap::readPixels. + if (fCache.directGeneratePixels(dstInfo, dstPixels, dstRB, srcX, srcY, + SkTransferFunctionBehavior::kRespect)) { + return true; + } + // else fall through + } + } + + if (this->getROPixels(&bm, dstColorSpace, chint)) { + return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY); + } + return false; +} + +SkData* SkImage_Lazy::onRefEncoded(GrContext* ctx) const { + return fCache.refEncoded(ctx); +} + +bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkColorSpace* dstColorSpace, + CachingHint chint) const { + return fCache.lockAsBitmap(nullptr, bitmap, this, dstColorSpace, chint); +} + +#if SK_SUPPORT_GPU +sk_sp SkImage_Lazy::asTextureProxyRef(GrContext* context, + const GrSamplerParams& params, + SkColorSpace* dstColorSpace, + sk_sp* texColorSpace, + SkScalar scaleAdjust[2]) const { + return fCache.lockAsTextureProxy(context, params, dstColorSpace, + texColorSpace, this, scaleAdjust); +} +#endif + +sk_sp SkImage_Lazy::onMakeSubset(const SkIRect& subset) const { + SkASSERT(fCache.info().bounds().contains(subset)); + SkASSERT(fCache.info().bounds() != subset); + + const SkIRect generatorSubset = subset.makeOffset(fCache.fOrigin.x(), fCache.fOrigin.y()); + SkImageCacherator::Validator validator(fCache.fSharedGenerator, &generatorSubset); + return validator ? sk_sp(new SkImage_Lazy(&validator)) : nullptr; +} + +sk_sp SkImage_Lazy::onMakeColorSpace(sk_sp target) const { + SkBitmap dst; + SkImageInfo dstInfo = fCache.info().makeColorSpace(target); + if (kIndex_8_SkColorType == dstInfo.colorType() || + kGray_8_SkColorType == dstInfo.colorType() || + kRGB_565_SkColorType == dstInfo.colorType()) { + dstInfo = dstInfo.makeColorType(kN32_SkColorType); + } + dst.allocPixels(dstInfo); + + // Use kIgnore for transfer function behavior. This is used by the SkColorSpaceXformCanvas, + // which wants to pre-xform the inputs but ignore the transfer function on blends. + if (!fCache.directGeneratePixels(dstInfo, dst.getPixels(), dst.rowBytes(), 0, 0, + SkTransferFunctionBehavior::kIgnore)) { + return nullptr; + } + + dst.setImmutable(); + return SkImage::MakeFromBitmap(dst); +} + +sk_sp SkImage::MakeFromGenerator(std::unique_ptr generator, + const SkIRect* subset) { + SkImageCacherator::Validator validator( + SkImageCacherator::SharedGenerator::Make(std::move(generator)), subset); + + return validator ? sk_make_sp(&validator) : nullptr; +} -- cgit v1.2.3