* 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 "src/image/SkImage_Lazy.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkData.h"
#include "include/core/SkImageGenerator.h"
#include "src/core/SkBitmapCache.h"
#include "src/core/SkCachedData.h"
#include "src/core/SkImagePriv.h"
#include "src/core/SkNextID.h"
#if SK_SUPPORT_GPU
#include "include/private/GrRecordingContext.h"
#include "include/private/GrResourceKey.h"
#include "src/gpu/GrCaps.h"
#include "src/gpu/GrGpuResourcePriv.h"
#include "src/gpu/GrImageTextureMaker.h"
#include "src/gpu/GrProxyProvider.h"
#include "src/gpu/GrRecordingContextPriv.h"
#include "src/gpu/GrSamplerState.h"
#include "src/gpu/GrYUVProvider.h"
#include "src/gpu/SkGr.h"
#endif
class SharedGenerator final : public SkNVRefCnt<SharedGenerator> {
public:
static sk_sp<SharedGenerator> Make(std::unique_ptr<SkImageGenerator> gen) {
return gen ? sk_sp<SharedGenerator>(new SharedGenerator(std::move(gen))) : nullptr;
}
const SkImageInfo& getInfo() { return fGenerator->getInfo(); }
private:
explicit SharedGenerator(std::unique_ptr<SkImageGenerator> gen)
: fGenerator(std::move(gen)) {
SkASSERT(fGenerator);
}
friend class ScopedGenerator;
friend class SkImage_Lazy;
std::unique_ptr<SkImageGenerator> fGenerator;
SkMutex fMutex;
};
SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkIRect* subset,
const SkColorType* colorType, sk_sp<SkColorSpace> colorSpace)
: fSharedGenerator(std::move(gen)) {
if (!fSharedGenerator) {
return;
}
const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo();
if (info.isEmpty()) {
fSharedGenerator.reset();
return;
}
fUniqueID = fSharedGenerator->fGenerator->uniqueID();
const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height());
if (subset) {
if (!bounds.contains(*subset)) {
fSharedGenerator.reset();
return;
}
if (*subset != bounds) {
fUniqueID = SkNextID::ImageID();
}
} else {
subset = &bounds;
}
fInfo = info.makeWH(subset->width(), subset->height());
fOrigin = SkIPoint::Make(subset->x(), subset->y());
if (colorType || colorSpace) {
if (colorType) {
fInfo = fInfo.makeColorType(*colorType);
}
if (colorSpace) {
fInfo = fInfo.makeColorSpace(colorSpace);
}
fUniqueID = SkNextID::ImageID();
}
}
class SkImage_Lazy::ScopedGenerator {
public:
ScopedGenerator(const sk_sp<SharedGenerator>& gen)
: fSharedGenerator(gen)
, fAutoAquire(gen->fMutex) {}
SkImageGenerator* operator->() const {
fSharedGenerator->fMutex.assertHeld();
return fSharedGenerator->fGenerator.get();
}
operator SkImageGenerator*() const {
fSharedGenerator->fMutex.assertHeld();
return fSharedGenerator->fGenerator.get();
}
private:
const sk_sp<SharedGenerator>& fSharedGenerator;
SkAutoMutexExclusive fAutoAquire;
};
SkImage_Lazy::SkImage_Lazy(Validator* validator)
: INHERITED(validator->fInfo, validator->fUniqueID)
, fSharedGenerator(std::move(validator->fSharedGenerator))
, fOrigin(validator->fOrigin) {
SkASSERT(fSharedGenerator);
fUniqueID = validator->fUniqueID;
}
SkImage_Lazy::~SkImage_Lazy() {
#if SK_SUPPORT_GPU
for (int i = 0; i < fUniqueKeyInvalidatedMessages.count(); ++i) {
SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(*fUniqueKeyInvalidatedMessages[i]);
}
fUniqueKeyInvalidatedMessages.deleteAll();
#endif
}
static bool generate_pixels(SkImageGenerator* gen, const SkPixmap& pmap, int originX, int originY) {
const int genW = gen->getInfo().width();
const int genH = gen->getInfo().height();
const SkIRect srcR = SkIRect::MakeWH(genW, genH);
const SkIRect dstR = SkIRect::MakeXYWH(originX, originY, pmap.width(), pmap.height());
if (!srcR.contains(dstR)) {
return false;
}
SkBitmap full;
SkPixmap fullPM;
const SkPixmap* dstPM = &pmap;
if (srcR != dstR) {
if (!full.tryAllocPixels(pmap.info().makeWH(genW, genH))) {
return false;
}
if (!full.peekPixels(&fullPM)) {
return false;
}
dstPM = &fullPM;
}
if (!gen->getPixels(dstPM->info(), dstPM->writable_addr(), dstPM->rowBytes())) {
return false;
}
if (srcR != dstR) {
if (!full.readPixels(pmap, originX, originY)) {
return false;
}
}
return true;
}
bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkImage::CachingHint chint) const {
auto check_output_bitmap = [bitmap]() {
SkASSERT(bitmap->isImmutable());
SkASSERT(bitmap->getPixels());
(void)bitmap;
};
auto desc = SkBitmapCacheDesc::Make(this);
if (SkBitmapCache::Find(desc, bitmap)) {
check_output_bitmap();
return true;
}
if (SkImage::kAllow_CachingHint == chint) {
SkPixmap pmap;
SkBitmapCache::RecPtr cacheRec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
if (!cacheRec ||
!generate_pixels(ScopedGenerator(fSharedGenerator), pmap,
fOrigin.x(), fOrigin.y())) {
return false;
}
SkBitmapCache::Add(std::move(cacheRec), bitmap);
this->notifyAddedToRasterCache();
} else {
if (!bitmap->tryAllocPixels(this->imageInfo()) ||
!generate_pixels(ScopedGenerator(fSharedGenerator), bitmap->pixmap(), fOrigin.x(),
fOrigin.y())) {
return false;
}
bitmap->setImmutable();
}
check_output_bitmap();
return true;
}
bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
int srcX, int srcY, CachingHint chint) const {
SkBitmap bm;
if (this->getROPixels(&bm, chint)) {
return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
}
return false;
}
sk_sp<SkData> SkImage_Lazy::onRefEncoded() const {
ScopedGenerator generator(fSharedGenerator);
return generator->refEncodedData();
}
bool SkImage_Lazy::onIsValid(GrContext* context) const {
ScopedGenerator generator(fSharedGenerator);
return generator->isValid(context);
}
#if SK_SUPPORT_GPU
sk_sp<GrTextureProxy> SkImage_Lazy::asTextureProxyRef(GrRecordingContext* context,
const GrSamplerState& params,
SkScalar scaleAdjust[2]) const {
if (!context) {
return nullptr;
}
GrImageTextureMaker textureMaker(context, this, kAllow_CachingHint);
return textureMaker.refTextureProxyForParams(params, scaleAdjust);
}
#endif
sk_sp<SkImage> SkImage_Lazy::onMakeSubset(GrRecordingContext* context,
const SkIRect& subset) const {
SkASSERT(this->bounds().contains(subset));
SkASSERT(this->bounds() != subset);
const SkIRect generatorSubset = subset.makeOffset(fOrigin.x(), fOrigin.y());
const SkColorType colorType = this->colorType();
Validator validator(fSharedGenerator, &generatorSubset, &colorType, this->refColorSpace());
return validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
}
sk_sp<SkImage> SkImage_Lazy::onMakeColorTypeAndColorSpace(GrRecordingContext*,
SkColorType targetCT,
sk_sp<SkColorSpace> targetCS) const {
SkAutoMutexExclusive autoAquire(fOnMakeColorTypeAndSpaceMutex);
if (fOnMakeColorTypeAndSpaceResult &&
targetCT == fOnMakeColorTypeAndSpaceResult->colorType() &&
SkColorSpace::Equals(targetCS.get(), fOnMakeColorTypeAndSpaceResult->colorSpace())) {
return fOnMakeColorTypeAndSpaceResult;
}
const SkIRect generatorSubset =
SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), this->width(), this->height());
Validator validator(fSharedGenerator, &generatorSubset, &targetCT, targetCS);
sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
if (result) {
fOnMakeColorTypeAndSpaceResult = result;
}
return result;
}
sk_sp<SkImage> SkImage_Lazy::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const {
SkBitmap bitmap;
if (bitmap.tryAllocPixels(this->imageInfo().makeColorSpace(std::move(newCS)))) {
SkPixmap pixmap = bitmap.pixmap();
pixmap.setColorSpace(this->refColorSpace());
if (generate_pixels(ScopedGenerator(fSharedGenerator), pixmap, fOrigin.x(), fOrigin.y())) {
bitmap.setImmutable();
return SkImage::MakeFromBitmap(bitmap);
}
}
return nullptr;
}
sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,
const SkIRect* subset) {
SkImage_Lazy::Validator
validator(SharedGenerator::Make(std::move(generator)), subset, nullptr, nullptr);
return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
}
sk_sp<SkImage> SkImage::DecodeToRaster(const void* encoded, size_t length, const SkIRect* subset) {
auto gen = SkImageGenerator::MakeFromEncoded(SkData::MakeWithoutCopy(encoded, length));
if (!gen) {
return nullptr;
}
SkImageInfo info = gen->getInfo();
if (info.isEmpty()) {
return nullptr;
}
SkIPoint origin = {0, 0};
if (subset) {
if (!SkIRect::MakeWH(info.width(), info.height()).contains(*subset)) {
return nullptr;
}
info = info.makeWH(subset->width(), subset->height());
origin = {subset->x(), subset->y()};
}
size_t rb = info.minRowBytes();
if (rb == 0) {
return nullptr;
}
size_t size = info.computeByteSize(rb);
if (size == SIZE_MAX) {
return nullptr;
}
auto data = SkData::MakeUninitialized(size);
SkPixmap pmap(info, data->writable_data(), rb);
if (!generate_pixels(gen.get(), pmap, origin.x(), origin.y())) {
return nullptr;
}
return SkImage::MakeRasterData(info, data, rb);
}
#if SK_SUPPORT_GPU
void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey,
GrUniqueKey* cacheKey) const {
SkASSERT(!cacheKey->isValid());
if (origKey.isValid()) {
static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 0, "Image");
}
}
class Generator_GrYUVProvider : public GrYUVProvider {
public:
Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
private:
uint32_t onGetID() const override { return fGen->uniqueID(); }
bool onQueryYUVA8(SkYUVASizeInfo* sizeInfo,
SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
SkYUVColorSpace* colorSpace) const override {
return fGen->queryYUVA8(sizeInfo, yuvaIndices, colorSpace);
}
bool onGetYUVA8Planes(const SkYUVASizeInfo& sizeInfo,
const SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
void* planes[]) override {
return fGen->getYUVA8Planes(sizeInfo, yuvaIndices, planes);
}
SkImageGenerator* fGen;
typedef GrYUVProvider INHERITED;
};
static void set_key_on_proxy(GrProxyProvider* proxyProvider,
GrTextureProxy* proxy, GrTextureProxy* originalProxy,
const GrUniqueKey& key) {
if (key.isValid()) {
if (originalProxy && originalProxy->getUniqueKey().isValid()) {
SkASSERT(originalProxy->getUniqueKey() == key);
SkASSERT(GrMipMapped::kYes == proxy->mipMapped() &&
GrMipMapped::kNo == originalProxy->mipMapped());
SkASSERT(originalProxy->getUniqueKey() == key);
proxyProvider->removeUniqueKeyFromProxy(originalProxy);
}
proxyProvider->assignUniqueKeyToProxy(key, proxy);
}
}
sk_sp<SkCachedData> SkImage_Lazy::getPlanes(SkYUVASizeInfo* yuvaSizeInfo,
SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
SkYUVColorSpace* yuvColorSpace,
const void* planes[SkYUVASizeInfo::kMaxCount]) {
ScopedGenerator generator(fSharedGenerator);
Generator_GrYUVProvider provider(generator);
sk_sp<SkCachedData> data = provider.getPlanes(yuvaSizeInfo, yuvaIndices, yuvColorSpace, planes);
if (!data) {
return nullptr;
}
return data;
}
* We have 4 ways to try to return a texture (in sorted order)
*
* 1. Check the cache for a pre-existing one
* 2. Ask the generator to natively create one
* 3. Ask the generator to return YUV planes, which the GPU can convert
* 4. Ask the generator to return RGB(A) data, which the GPU can convert
*/
sk_sp<GrTextureProxy> SkImage_Lazy::lockTextureProxy(
GrRecordingContext* ctx,
const GrUniqueKey& origKey,
SkImage::CachingHint chint,
bool willBeMipped,
GrTextureMaker::AllowedTexGenType genType) const {
enum LockTexturePath {
kFailure_LockTexturePath,
kPreExisting_LockTexturePath,
kNative_LockTexturePath,
kCompressed_LockTexturePath,
kYUV_LockTexturePath,
kRGBA_LockTexturePath,
};
enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
GrUniqueKey key;
this->makeCacheKeyFromOrigKey(origKey, &key);
GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
sk_sp<GrTextureProxy> proxy;
if (key.isValid()) {
auto ct = SkColorTypeToGrColorType(this->colorType());
proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, ct, kTopLeft_GrSurfaceOrigin);
if (proxy) {
SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath,
kLockTexturePathCount);
if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
return proxy;
}
}
}
if (!proxy) {
ScopedGenerator generator(fSharedGenerator);
if (GrTextureMaker::AllowedTexGenType::kCheap == genType &&
SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) {
return nullptr;
}
if ((proxy = generator->generateTexture(ctx, this->imageInfo(), fOrigin, willBeMipped))) {
SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath,
kLockTexturePathCount);
set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
for (size_t i = 0; i < fUniqueKeyInvalidatedMessages.size(); i++) {
auto message = fUniqueKeyInvalidatedMessages[i];
if (key == message->key()) {
delete message;
fUniqueKeyInvalidatedMessages[i] =
new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
return proxy;
}
}
*fUniqueKeyInvalidatedMessages.append() =
new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
return proxy;
}
}
}
if (!proxy && !willBeMipped && !ctx->priv().options().fDisableGpuYUVConversion) {
const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(this->imageInfo());
SkColorType colorType = this->colorType();
ScopedGenerator generator(fSharedGenerator);
Generator_GrYUVProvider provider(generator);
SkColorSpace* generatorColorSpace = fSharedGenerator->fGenerator->getInfo().colorSpace();
SkColorSpace* thisColorSpace = this->colorSpace();
proxy = provider.refAsTextureProxy(ctx, desc, SkColorTypeToGrColorType(colorType),
generatorColorSpace, thisColorSpace);
if (proxy) {
SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath,
kLockTexturePathCount);
set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
*fUniqueKeyInvalidatedMessages.append() =
new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
return proxy;
}
}
SkBitmap bitmap;
if (!proxy && this->getROPixels(&bitmap, chint)) {
proxy = proxyProvider->createProxyFromBitmap(bitmap, willBeMipped ? GrMipMapped::kYes
: GrMipMapped::kNo);
if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath,
kLockTexturePathCount);
set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
*fUniqueKeyInvalidatedMessages.append() =
new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
return proxy;
}
}
if (proxy) {
SkASSERT(willBeMipped);
SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
*fUniqueKeyInvalidatedMessages.append() =
new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(ctx, proxy.get())) {
set_key_on_proxy(proxyProvider, mippedProxy.get(), proxy.get(), key);
return mippedProxy;
}
return proxy;
}
SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath,
kLockTexturePathCount);
return nullptr;
}
sk_sp<SkImage> SkImage::DecodeToTexture(GrContext* ctx, const void* encoded, size_t length,
const SkIRect* subset) {
auto img = MakeFromEncoded(SkData::MakeWithoutCopy(encoded, length), subset);
if (!img) {
return nullptr;
}
return img->makeTextureImage(ctx);
}
#endif