From 7b6945bc4e639d7cc4a49b84d492690f8e865566 Mon Sep 17 00:00:00 2001 From: reed Date: Thu, 24 Sep 2015 00:50:58 -0700 Subject: remove unused (by the outside) SkImage::newSurface, and simplify newImage -> newSubset BUG=skia: Review URL: https://codereview.chromium.org/1364443002 --- src/image/SkImage.cpp | 65 ++++++++++++++++++----------------------- src/image/SkImage_Base.h | 6 +--- src/image/SkImage_Generator.cpp | 25 +++++++++++----- src/image/SkImage_Gpu.cpp | 39 +++++++++++++------------ src/image/SkImage_Gpu.h | 6 ++-- src/image/SkImage_Raster.cpp | 20 +++++++++---- src/image/SkSurface_Gpu.cpp | 4 +-- 7 files changed, 85 insertions(+), 80 deletions(-) (limited to 'src/image') diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp index 6f4655758e..032be19493 100644 --- a/src/image/SkImage.cpp +++ b/src/image/SkImage.cpp @@ -126,21 +126,15 @@ SkImage* SkImage::NewFromEncoded(SkData* encoded, const SkIRect* subset) { return generator ? SkImage::NewFromGenerator(generator, subset) : nullptr; } -SkSurface* SkImage::newSurface(const SkImageInfo& info, const SkSurfaceProps* props) const { - if (nullptr == props) { - props = &as_IB(this)->props(); - } - return as_IB(this)->onNewSurface(info, *props); -} - const char* SkImage::toString(SkString* str) const { str->appendf("image: (id:%d (%d, %d) %s)", this->uniqueID(), this->width(), this->height(), this->isOpaque() ? "opaque" : ""); return str->c_str(); } -SkImage* SkImage::newImage(int newWidth, int newHeight, const SkIRect* subset, - SkFilterQuality quality) const { +#ifdef SK_SUPPORT_LEGACY_NEWIMAGE +SkImage* SkImage::newImage(int newWidth, int newHeight, const SkIRect* subset) const { +#if 0 if (newWidth <= 0 || newHeight <= 0) { return nullptr; } @@ -161,6 +155,30 @@ SkImage* SkImage::newImage(int newWidth, int newHeight, const SkIRect* subset, } return as_IB(this)->onNewImage(newWidth, newHeight, subset, quality); +#else + SkASSERT(subset); + SkASSERT(subset->width() == newWidth); + SkASSERT(subset->height() == newHeight); + return this->newSubset(*subset); +#endif +} +#endif + +SkImage* SkImage::newSubset(const SkIRect& subset) const { + if (subset.isEmpty()) { + return nullptr; + } + + const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); + if (!bounds.contains(subset)) { + return nullptr; + } + + // optimization : return self if the subset == our bounds + if (bounds == subset) { + return SkRef(const_cast(this)); + } + return as_IB(this)->onNewSubset(subset); } #if SK_SUPPORT_GPU @@ -244,33 +262,6 @@ bool SkImage_Base::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, siz return true; } -SkImage* SkImage_Base::onNewImage(int newWidth, int newHeight, const SkIRect* subset, - SkFilterQuality quality) const { - const bool opaque = this->isOpaque(); - const SkImageInfo info = SkImageInfo::Make(newWidth, newHeight, kN32_SkColorType, - opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); - SkAutoTUnref surface(this->newSurface(info, nullptr)); - if (!surface.get()) { - return nullptr; - } - - SkRect src; - if (subset) { - src.set(*subset); - } else { - src = SkRect::MakeIWH(this->width(), this->height()); - } - - surface->getCanvas()->scale(newWidth / src.width(), newHeight / src.height()); - surface->getCanvas()->translate(-src.x(), -src.y()); - - SkPaint paint; - paint.setXfermodeMode(SkXfermode::kSrc_Mode); - paint.setFilterQuality(quality); - surface->getCanvas()->drawImage(this, 0, 0, &paint); - return surface->newImageSnapshot(); -} - /////////////////////////////////////////////////////////////////////////////////////////////////// bool SkImage::peekPixels(SkPixmap* pmap) const { @@ -311,7 +302,7 @@ SkImage* SkImage::NewFromBitmap(const SkBitmap& bm) { } const SkImageInfo info = bm.info(); return new SkImage_Gpu(info.width(), info.height(), bm.getGenerationID(), info.alphaType(), - tex, 0, SkSurface::kNo_Budgeted); + tex, SkSurface::kNo_Budgeted); } #endif diff --git a/src/image/SkImage_Base.h b/src/image/SkImage_Base.h index d0cb8d5840..bbcf182ab8 100644 --- a/src/image/SkImage_Base.h +++ b/src/image/SkImage_Base.h @@ -37,8 +37,6 @@ public: const SkSurfaceProps& props() const { return fProps; } - virtual SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const = 0; - virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const { return nullptr; } @@ -60,9 +58,7 @@ public: SkShader::TileMode, const SkMatrix* localMatrix) const { return nullptr; } - // newWidth > 0, newHeight > 0, subset either nullptr or a proper subset of this bounds - virtual SkImage* onNewImage(int newWidth, int newHeight, const SkIRect* subset, - SkFilterQuality) const; + virtual SkImage* onNewSubset(const SkIRect&) const = 0; virtual SkData* onRefEncoded() const { return nullptr; } virtual bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const; diff --git a/src/image/SkImage_Generator.cpp b/src/image/SkImage_Generator.cpp index 4981f37c08..765234fa0c 100644 --- a/src/image/SkImage_Generator.cpp +++ b/src/image/SkImage_Generator.cpp @@ -7,6 +7,7 @@ #include "SkImage_Base.h" #include "SkBitmap.h" +#include "SkCanvas.h" #include "SkData.h" #include "SkImageCacherator.h" #include "SkImagePriv.h" @@ -20,12 +21,11 @@ public: , fCache(cache) // take ownership {} - SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const override; bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY) const override; const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const override; SkData* onRefEncoded() const override; bool isOpaque() const override { return fCache->info().isOpaque(); } - + SkImage* onNewSubset(const SkIRect&) const override; bool getROPixels(SkBitmap*) const override; GrTexture* asTextureRef(GrContext*, SkImageUsageType) const override; @@ -54,11 +54,6 @@ SkShader* SkImage_Generator::onNewShader(SkShader::TileMode tileX, SkShader::Til return nullptr; } -SkSurface* SkImage_Generator::onNewSurface(const SkImageInfo& info, - const SkSurfaceProps& props) const { - return SkSurface::NewRaster(info, &props); -} - bool SkImage_Generator::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, int srcX, int srcY) const { SkBitmap bm; @@ -84,6 +79,22 @@ GrTexture* SkImage_Generator::asTextureRef(GrContext* ctx, SkImageUsageType usag return fCache->lockAsTexture(ctx, usage, this); } +SkImage* SkImage_Generator::onNewSubset(const SkIRect& subset) const { + // TODO: make this lazy, by wrapping the subset inside a new generator or something + // For now, we do effectively what we did before, make it a raster + + const SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), + this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType); + SkAutoTUnref surface(SkSurface::NewRaster(info)); + if (!surface) { + return nullptr; + } + surface->getCanvas()->clear(0); + surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToScalar(-subset.y()), + nullptr); + return surface->newImageSnapshot(); +} + SkImage* SkImage::NewFromGenerator(SkImageGenerator* generator, const SkIRect* subset) { SkImageCacherator* cache = SkImageCacherator::NewFromGenerator(generator, subset); if (!cache) { diff --git a/src/image/SkImage_Gpu.cpp b/src/image/SkImage_Gpu.cpp index 163e62e02d..1a63a0d6ba 100644 --- a/src/image/SkImage_Gpu.cpp +++ b/src/image/SkImage_Gpu.cpp @@ -15,10 +15,9 @@ #include "SkPixelRef.h" SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrTexture* tex, - int sampleCountForNewSurfaces, SkSurface::Budgeted budgeted) + SkSurface::Budgeted budgeted) : INHERITED(w, h, uniqueID, nullptr) , fTexture(SkRef(tex)) - , fSampleCountForNewSurfaces(sampleCountForNewSurfaces) , fAlphaType(at) , fBudgeted(budgeted) , fAddedRasterVersionToCache(false) @@ -30,19 +29,6 @@ SkImage_Gpu::~SkImage_Gpu() { } } -SkSurface* SkImage_Gpu::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const { - GrTexture* tex = this->getTexture(); - SkASSERT(tex); - GrContext* ctx = tex->getContext(); - if (!ctx) { - // the texture may have been abandoned, so we have to check - return nullptr; - } - // TODO: Change signature of onNewSurface to take a budgeted param. - const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; - return SkSurface::NewRenderTarget(ctx, budgeted, info, fSampleCountForNewSurfaces, &props); -} - extern void SkTextureImageApplyBudgetedDecision(SkImage* image) { if (as_IB(image)->getTexture()) { ((SkImage_Gpu*)image)->applyBudgetDecision(); @@ -136,6 +122,22 @@ bool SkImage_Gpu::onReadPixels(const SkImageInfo& info, void* pixels, size_t row return true; } +SkImage* SkImage_Gpu::onNewSubset(const SkIRect& subset) const { + GrContext* ctx = fTexture->getContext(); + GrSurfaceDesc desc = fTexture->desc(); + desc.fWidth = subset.width(); + desc.fHeight = subset.height(); + + GrTexture* subTx = ctx->textureProvider()->createTexture(desc, + SkSurface::kYes_Budgeted == fBudgeted); + if (!subTx) { + return nullptr; + } + ctx->copySurface(subTx, fTexture, subset, SkIPoint::Make(0, 0)); + return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, fAlphaType, subTx, + fBudgeted); +} + /////////////////////////////////////////////////////////////////////////////////////////////////// static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextureDesc& desc, @@ -154,7 +156,7 @@ static SkImage* new_wrapped_texture_common(GrContext* ctx, const GrBackendTextur } const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; - return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, 0, budgeted); + return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, tex, budgeted); } SkImage* SkImage::NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc, SkAlphaType at, @@ -186,8 +188,7 @@ SkImage* SkImage::NewFromTextureCopy(GrContext* ctx, const GrBackendTextureDesc& } const SkSurface::Budgeted budgeted = SkSurface::kYes_Budgeted; - const int sampleCount = 0; // todo: make this an explicit parameter to newSurface()? - return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst, sampleCount, + return new SkImage_Gpu(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, at, dst, budgeted); } @@ -267,7 +268,7 @@ SkImage* SkImage::NewFromYUVTexturesCopy(GrContext* ctx , SkYUVColorSpace colorS drawContext->drawRect(dst->asRenderTarget(), GrClip::WideOpen(), paint, SkMatrix::I(), rect); ctx->flushSurfaceWrites(dst); return new SkImage_Gpu(dstDesc.fWidth, dstDesc.fHeight, kNeedNewImageUniqueID, - kOpaque_SkAlphaType, dst, 0, budgeted); + kOpaque_SkAlphaType, dst, budgeted); } /////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/image/SkImage_Gpu.h b/src/image/SkImage_Gpu.h index d168fb6853..5e847dd381 100644 --- a/src/image/SkImage_Gpu.h +++ b/src/image/SkImage_Gpu.h @@ -22,8 +22,7 @@ public: * An "image" can be a subset/window into a larger texture, so we explicit take the * width and height. */ - SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType, GrTexture*, - int sampleCountForNewSurfaces, SkSurface::Budgeted); + SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType, GrTexture*, SkSurface::Budgeted); ~SkImage_Gpu() override; void applyBudgetDecision() const { @@ -38,19 +37,18 @@ public: bool getROPixels(SkBitmap*) const override; GrTexture* asTextureRef(GrContext* ctx, SkImageUsageType usage) const override; + SkImage* onNewSubset(const SkIRect&) const override; GrTexture* peekTexture() const override { return fTexture; } SkShader* onNewShader(SkShader::TileMode, SkShader::TileMode, const SkMatrix* localMatrix) const override; bool isOpaque() const override; - SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const override; bool onReadPixels(const SkImageInfo&, void* dstPixels, size_t dstRowBytes, int srcX, int srcY) const override; private: SkAutoTUnref fTexture; - const int fSampleCountForNewSurfaces; // 0 if we don't know const SkAlphaType fAlphaType; const SkSurface::Budgeted fBudgeted; mutable SkAtomic fAddedRasterVersionToCache; diff --git a/src/image/SkImage_Raster.cpp b/src/image/SkImage_Raster.cpp index 67498f0e11..30ea7cc1a4 100644 --- a/src/image/SkImage_Raster.cpp +++ b/src/image/SkImage_Raster.cpp @@ -67,12 +67,12 @@ public: SkImage_Raster(const SkImageInfo&, SkData*, size_t rb, SkColorTable*, const SkSurfaceProps*); virtual ~SkImage_Raster(); - SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) const override; bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY) const override; const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const override; SkData* onRefEncoded() const override; bool getROPixels(SkBitmap*) const override; GrTexture* asTextureRef(GrContext*, SkImageUsageType) const override; + SkImage* onNewSubset(const SkIRect&) const override; // exposed for SkSurface_Raster via SkNewImageFromPixelRef SkImage_Raster(const SkImageInfo&, SkPixelRef*, const SkIPoint& pixelRefOrigin, size_t rowBytes, @@ -149,10 +149,6 @@ SkShader* SkImage_Raster::onNewShader(SkShader::TileMode tileX, SkShader::TileMo return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix); } -SkSurface* SkImage_Raster::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) const { - return SkSurface::NewRaster(info, &props); -} - bool SkImage_Raster::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, int srcX, int srcY) const { SkBitmap shallowCopy(fBitmap); @@ -212,6 +208,20 @@ GrTexture* SkImage_Raster::asTextureRef(GrContext* ctx, SkImageUsageType usage) return nullptr; } +SkImage* SkImage_Raster::onNewSubset(const SkIRect& subset) const { + // TODO : could consider heurist of sharing pixels, if subset is pretty close to complete + + SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), fBitmap.alphaType()); + SkAutoTUnref surface(SkSurface::NewRaster(info)); + if (!surface) { + return nullptr; + } + surface->getCanvas()->clear(0); + surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToScalar(-subset.y()), + nullptr); + return surface->newImageSnapshot(); +} + /////////////////////////////////////////////////////////////////////////////// SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, size_t rowBytes, diff --git a/src/image/SkSurface_Gpu.cpp b/src/image/SkSurface_Gpu.cpp index c0c6b6eed0..b705f4733a 100644 --- a/src/image/SkSurface_Gpu.cpp +++ b/src/image/SkSurface_Gpu.cpp @@ -16,7 +16,6 @@ #if SK_SUPPORT_GPU -/////////////////////////////////////////////////////////////////////////////// SkSurface_Gpu::SkSurface_Gpu(SkGpuDevice* device) : INHERITED(device->width(), device->height(), &device->surfaceProps()) , fDevice(SkRef(device)) { @@ -80,12 +79,11 @@ SkSurface* SkSurface_Gpu::onNewSurface(const SkImageInfo& info) { SkImage* SkSurface_Gpu::onNewImageSnapshot(Budgeted budgeted) { const SkImageInfo info = fDevice->imageInfo(); - const int sampleCount = fDevice->accessRenderTarget()->numColorSamples(); SkImage* image = nullptr; GrTexture* tex = fDevice->accessRenderTarget()->asTexture(); if (tex) { image = new SkImage_Gpu(info.width(), info.height(), kNeedNewImageUniqueID, - info.alphaType(), tex, sampleCount, budgeted); + info.alphaType(), tex, budgeted); } if (image) { as_IB(image)->initWithProps(this->props()); -- cgit v1.2.3