/* * Copyright 2016 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file */ #include "SkSpecialImage.h" #if SK_SUPPORT_GPU #include "GrTexture.h" #include "GrTextureParams.h" #include "SkGr.h" #endif #include "SkBitmapCache.h" #include "SkCanvas.h" #include "SkImage_Base.h" #include "SkSpecialSurface.h" #include "SkSurfacePriv.h" // Currently the raster imagefilters can only handle certain imageinfos. Call this to know if // a given info is supported. static bool valid_for_imagefilters(const SkImageInfo& info) { // no support for other swizzles/depths yet return info.colorType() == kN32_SkColorType; } /////////////////////////////////////////////////////////////////////////////// class SkSpecialImage_Base : public SkSpecialImage { public: SkSpecialImage_Base(const SkIRect& subset, uint32_t uniqueID, const SkSurfaceProps* props) : INHERITED(subset, uniqueID, props) { } ~SkSpecialImage_Base() override { } virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0; virtual bool onGetROPixels(SkBitmap*) const = 0; virtual GrTexture* onPeekTexture() const { return nullptr; } #if SK_SUPPORT_GPU virtual sk_sp onAsTextureRef(GrContext* context) const = 0; #endif // Delete this entry point ASAP (see skbug.com/4965) virtual bool getBitmapDeprecated(SkBitmap* result) const = 0; virtual sk_sp onMakeSubset(const SkIRect& subset) const = 0; virtual sk_sp onMakeSurface(const SkImageInfo& info) const = 0; virtual sk_sp onMakeTightSubset(const SkIRect& subset) const = 0; virtual sk_sp onMakeTightSurface(const SkImageInfo& info) const = 0; private: typedef SkSpecialImage INHERITED; }; /////////////////////////////////////////////////////////////////////////////// static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) { return static_cast(image); } SkSpecialImage::SkSpecialImage(const SkIRect& subset, uint32_t uniqueID, const SkSurfaceProps* props) : fProps(SkSurfacePropsCopyOrDefault(props)) , fSubset(subset) , fUniqueID(kNeedNewImageUniqueID_SpecialImage == uniqueID ? SkNextID::ImageID() : uniqueID) { } sk_sp SkSpecialImage::makeTextureImage(GrContext* context) { #if SK_SUPPORT_GPU if (!context) { return nullptr; } if (GrTexture* peek = as_SIB(this)->onPeekTexture()) { return peek->getContext() == context ? sk_sp(SkRef(this)) : nullptr; } SkBitmap bmp; // At this point, we are definitely not texture-backed, so we must be raster or generator // backed. If we remove the special-wrapping-an-image subclass, we may be able to assert that // we are strictly raster-backed (i.e. generator images become raster when they are specialized) // in which case getROPixels could turn into peekPixels... if (!this->getROPixels(&bmp)) { return nullptr; } if (bmp.empty()) { return SkSpecialImage::MakeFromRaster(SkIRect::MakeEmpty(), bmp, &this->props()); } sk_sp resultTex(GrRefCachedBitmapTexture(context, bmp, GrTextureParams::ClampNoFilter(), SkSourceGammaTreatment::kRespect)); if (!resultTex) { return nullptr; } SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(resultTex->width(), resultTex->height()), this->uniqueID(), resultTex, &this->props(), at); #else return nullptr; #endif } void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const { return as_SIB(this)->onDraw(canvas, x, y, paint); } bool SkSpecialImage::getROPixels(SkBitmap* bm) const { return as_SIB(this)->onGetROPixels(bm); } bool SkSpecialImage::isTextureBacked() const { #if SK_SUPPORT_GPU return as_SIB(this)->onPeekTexture() && as_SIB(this)->onPeekTexture()->getContext(); #else return false; #endif } GrContext* SkSpecialImage::getContext() const { #if SK_SUPPORT_GPU GrTexture* texture = as_SIB(this)->onPeekTexture(); if (texture) { return texture->getContext(); } #endif return nullptr; } #if SK_SUPPORT_GPU sk_sp SkSpecialImage::asTextureRef(GrContext* context) const { return as_SIB(this)->onAsTextureRef(context); } #endif sk_sp SkSpecialImage::makeSurface(const SkImageInfo& info) const { return as_SIB(this)->onMakeSurface(info); } sk_sp SkSpecialImage::makeTightSurface(const SkImageInfo& info) const { return as_SIB(this)->onMakeTightSurface(info); } sk_sp SkSpecialImage::makeSubset(const SkIRect& subset) const { return as_SIB(this)->onMakeSubset(subset); } sk_sp SkSpecialImage::makeTightSubset(const SkIRect& subset) const { return as_SIB(this)->onMakeTightSubset(subset); } #if SK_SUPPORT_GPU #include "SkGr.h" #include "SkGrPixelRef.h" #endif /////////////////////////////////////////////////////////////////////////////// #include "SkImage.h" #if SK_SUPPORT_GPU #include "GrContext.h" #include "SkGrPriv.h" #endif class SkSpecialImage_Image : public SkSpecialImage_Base { public: SkSpecialImage_Image(const SkIRect& subset, sk_sp image, const SkSurfaceProps* props) : INHERITED(subset, image->uniqueID(), props) , fImage(image) { } ~SkSpecialImage_Image() override { } bool isOpaque() const override { return fImage->isOpaque(); } size_t getSize() const override { #if SK_SUPPORT_GPU if (GrTexture* texture = as_IB(fImage.get())->peekTexture()) { return texture->gpuMemorySize(); } else #endif { SkPixmap pm; if (fImage->peekPixels(&pm)) { return pm.height() * pm.rowBytes(); } } return 0; } void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset().height()); canvas->drawImageRect(fImage.get(), this->subset(), dst, paint, SkCanvas::kStrict_SrcRectConstraint); } bool onGetROPixels(SkBitmap* bm) const override { return as_IB(fImage)->getROPixels(bm); } GrTexture* onPeekTexture() const override { return as_IB(fImage)->peekTexture(); } #if SK_SUPPORT_GPU sk_sp onAsTextureRef(GrContext* context) const override { return sk_sp(as_IB(fImage)->asTextureRef(context, GrTextureParams::ClampNoFilter(), SkSourceGammaTreatment::kRespect)); } #endif bool getBitmapDeprecated(SkBitmap* result) const override { #if SK_SUPPORT_GPU if (GrTexture* texture = as_IB(fImage.get())->peekTexture()) { const SkImageInfo info = GrMakeInfoFromTexture(texture, fImage->width(), fImage->height(), fImage->isOpaque()); if (!result->setInfo(info)) { return false; } result->setPixelRef(new SkGrPixelRef(info, texture))->unref(); return true; } #endif return as_IB(fImage.get())->asBitmapForImageFilters(result); } sk_sp onMakeSurface(const SkImageInfo& info) const override { #if SK_SUPPORT_GPU GrTexture* texture = as_IB(fImage.get())->peekTexture(); if (texture) { GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *texture->getContext()->caps()); return SkSpecialSurface::MakeRenderTarget(texture->getContext(), info.width(), info.height(), config); } #endif return SkSpecialSurface::MakeRaster(info, nullptr); } sk_sp onMakeSubset(const SkIRect& subset) const override { sk_sp subsetImg(fImage->makeSubset(subset)); if (!subsetImg) { return nullptr; } return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(subset.width(), subset.height()), subsetImg, &this->props()); } sk_sp onMakeTightSubset(const SkIRect& subset) const override { return fImage->makeSubset(subset); } sk_sp onMakeTightSurface(const SkImageInfo& info) const override { #if SK_SUPPORT_GPU GrTexture* texture = as_IB(fImage.get())->peekTexture(); if (texture) { return SkSurface::MakeRenderTarget(texture->getContext(), SkBudgeted::kYes, info); } #endif return SkSurface::MakeRaster(info, nullptr); } private: sk_sp fImage; typedef SkSpecialImage_Base INHERITED; }; #ifdef SK_DEBUG static bool rect_fits(const SkIRect& rect, int width, int height) { if (0 == width && 0 == height) { SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == rect.fBottom); return true; } return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && rect.fRight >= 0 && rect.fRight <= width && rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && rect.fBottom >= 0 && rect.fBottom <= height; } #endif sk_sp SkSpecialImage::MakeFromImage(const SkIRect& subset, sk_sp image, const SkSurfaceProps* props) { SkASSERT(rect_fits(subset, image->width(), image->height())); if (valid_for_imagefilters(as_IB(image.get())->onImageInfo())) { return sk_make_sp(subset, image, props); } else { return nullptr; } } /////////////////////////////////////////////////////////////////////////////// #include "SkBitmap.h" #include "SkImageInfo.h" #include "SkPixelRef.h" class SkSpecialImage_Raster : public SkSpecialImage_Base { public: SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSurfaceProps* props) : INHERITED(subset, bm.getGenerationID(), props) , fBitmap(bm) { if (bm.pixelRef() && bm.pixelRef()->isPreLocked()) { // we only preemptively lock if there is no chance of triggering something expensive // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. fBitmap.lockPixels(); } } bool isOpaque() const override { return fBitmap.isOpaque(); } size_t getSize() const override { return fBitmap.getSize(); } void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset().height()); canvas->drawBitmapRect(fBitmap, this->subset(), dst, paint, SkCanvas::kStrict_SrcRectConstraint); } bool onGetROPixels(SkBitmap* bm) const override { *bm = fBitmap; return true; } #if SK_SUPPORT_GPU sk_sp onAsTextureRef(GrContext* context) const override { if (context) { return sk_ref_sp(GrRefCachedBitmapTexture(context, fBitmap, GrTextureParams::ClampNoFilter(), SkSourceGammaTreatment::kRespect)); } return nullptr; } #endif bool getBitmapDeprecated(SkBitmap* result) const override { *result = fBitmap; return true; } sk_sp onMakeSurface(const SkImageInfo& info) const override { return SkSpecialSurface::MakeRaster(info, nullptr); } sk_sp onMakeSubset(const SkIRect& subset) const override { SkBitmap subsetBM; if (!fBitmap.extractSubset(&subsetBM, subset)) { return nullptr; } return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(subset.width(), subset.height()), subsetBM, &this->props()); } sk_sp onMakeTightSubset(const SkIRect& subset) const override { SkBitmap subsetBM; if (!fBitmap.extractSubset(&subsetBM, subset)) { return nullptr; } return SkImage::MakeFromBitmap(subsetBM); } sk_sp onMakeTightSurface(const SkImageInfo& info) const override { return SkSurface::MakeRaster(info); } private: SkBitmap fBitmap; typedef SkSpecialImage_Base INHERITED; }; sk_sp SkSpecialImage::MakeFromRaster(const SkIRect& subset, const SkBitmap& bm, const SkSurfaceProps* props) { SkASSERT(nullptr == bm.getTexture()); SkASSERT(rect_fits(subset, bm.width(), bm.height())); const SkBitmap* srcBM = &bm; SkBitmap tmpStorage; // ImageFilters only handle N32 at the moment, so force our src to be that if (!valid_for_imagefilters(bm.info())) { if (!bm.copyTo(&tmpStorage, kN32_SkColorType)) { return nullptr; } srcBM = &tmpStorage; } return sk_make_sp(subset, *srcBM, props); } #if SK_SUPPORT_GPU /////////////////////////////////////////////////////////////////////////////// #include "GrTexture.h" #include "SkImage_Gpu.h" class SkSpecialImage_Gpu : public SkSpecialImage_Base { public: SkSpecialImage_Gpu(const SkIRect& subset, uint32_t uniqueID, sk_sp tex, SkAlphaType at, const SkSurfaceProps* props) : INHERITED(subset, uniqueID, props) , fTexture(std::move(tex)) , fAlphaType(at) , fAddedRasterVersionToCache(false) { } ~SkSpecialImage_Gpu() override { if (fAddedRasterVersionToCache.load()) { SkNotifyBitmapGenIDIsStale(this->uniqueID()); } } bool isOpaque() const override { return GrPixelConfigIsOpaque(fTexture->config()) || fAlphaType == kOpaque_SkAlphaType; } size_t getSize() const override { return fTexture->gpuMemorySize(); } void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { SkRect dst = SkRect::MakeXYWH(x, y, this->subset().width(), this->subset().height()); auto img = sk_sp(new SkImage_Gpu(fTexture->width(), fTexture->height(), this->uniqueID(), fAlphaType, fTexture.get(), SkBudgeted::kNo)); canvas->drawImageRect(img, this->subset(), dst, paint, SkCanvas::kStrict_SrcRectConstraint); } GrTexture* onPeekTexture() const override { return fTexture.get(); } sk_sp onAsTextureRef(GrContext*) const override { return fTexture; } bool onGetROPixels(SkBitmap* dst) const override { if (SkBitmapCache::Find(this->uniqueID(), dst)) { SkASSERT(dst->getGenerationID() == this->uniqueID()); SkASSERT(dst->isImmutable()); SkASSERT(dst->getPixels()); return true; } SkImageInfo info = SkImageInfo::MakeN32(this->width(), this->height(), this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType); if (!dst->tryAllocPixels(info)) { return false; } if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, dst->getPixels(), dst->rowBytes())) { return false; } dst->pixelRef()->setImmutableWithID(this->uniqueID()); SkBitmapCache::Add(this->uniqueID(), *dst); fAddedRasterVersionToCache.store(true); return true; } bool getBitmapDeprecated(SkBitmap* result) const override { const SkImageInfo info = GrMakeInfoFromTexture(fTexture.get(), this->width(), this->height(), this->isOpaque()); if (!result->setInfo(info)) { return false; } const SkImageInfo prInfo = info.makeWH(fTexture->width(), fTexture->height()); SkAutoTUnref pixelRef(new SkGrPixelRef(prInfo, fTexture.get())); result->setPixelRef(pixelRef, this->subset().fLeft, this->subset().fTop); return true; } sk_sp onMakeSurface(const SkImageInfo& info) const override { if (!fTexture->getContext()) { return nullptr; } GrPixelConfig config = SkImageInfo2GrPixelConfig(info, *fTexture->getContext()->caps()); return SkSpecialSurface::MakeRenderTarget(fTexture->getContext(), info.width(), info.height(), config); } sk_sp onMakeSubset(const SkIRect& subset) const override { return SkSpecialImage::MakeFromGpu(subset, this->uniqueID(), fTexture, &this->props(), fAlphaType); } sk_sp onMakeTightSubset(const SkIRect& subset) const override { if (0 == subset.fLeft && 0 == subset.fTop && fTexture->width() == subset.width() && fTexture->height() == subset.height()) { // The existing GrTexture is already tight so reuse it in the SkImage return sk_make_sp(fTexture->width(), fTexture->height(), kNeedNewImageUniqueID, fAlphaType, fTexture.get(), SkBudgeted::kYes); } GrContext* ctx = fTexture->getContext(); GrSurfaceDesc desc = fTexture->desc(); desc.fWidth = subset.width(); desc.fHeight = subset.height(); sk_sp subTx(ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes)); if (!subTx) { return nullptr; } ctx->copySurface(subTx.get(), fTexture.get(), subset, SkIPoint::Make(0, 0)); return sk_make_sp(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, fAlphaType, subTx.get(), SkBudgeted::kYes); } sk_sp onMakeTightSurface(const SkImageInfo& info) const override { return SkSurface::MakeRenderTarget(fTexture->getContext(), SkBudgeted::kYes, info); } private: sk_sp fTexture; const SkAlphaType fAlphaType; mutable SkAtomic fAddedRasterVersionToCache; typedef SkSpecialImage_Base INHERITED; }; sk_sp SkSpecialImage::MakeFromGpu(const SkIRect& subset, uint32_t uniqueID, sk_sp tex, const SkSurfaceProps* props, SkAlphaType at) { SkASSERT(rect_fits(subset, tex->width(), tex->height())); return sk_make_sp(subset, uniqueID, std::move(tex), at, props); } #endif