Chromium Code Reviews| Index: src/core/SkSpecialImage.cpp |
| diff --git a/src/core/SkSpecialImage.cpp b/src/core/SkSpecialImage.cpp |
| index 4fed2ac24a0afdcd866e5234de79aa31f8fbdc45..0bbbcea22b72f006bc605fddde5aa3c6dd1c5b8c 100644 |
| --- a/src/core/SkSpecialImage.cpp |
| +++ b/src/core/SkSpecialImage.cpp |
| @@ -12,7 +12,7 @@ |
| /////////////////////////////////////////////////////////////////////////////// |
| class SkSpecialImage_Base : public SkSpecialImage { |
| public: |
| - SkSpecialImage_Base(const SkIRect& subset) : INHERITED(subset) { } |
| + SkSpecialImage_Base(const SkIRect& subset, uint32_t uniqueID) : INHERITED(subset, uniqueID) {} |
| virtual ~SkSpecialImage_Base() { } |
| virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0; |
| @@ -21,6 +21,9 @@ public: |
| virtual GrTexture* onPeekTexture() const { return nullptr; } |
| + // Delete this entry point ASAP |
| + virtual bool getBitmap(SkBitmap* result) const = 0; |
| + |
| virtual SkSpecialSurface* onNewSurface(const SkImageInfo& info) const { return nullptr; } |
| private: |
| @@ -48,22 +51,91 @@ SkSpecialSurface* SkSpecialImage::newSurface(const SkImageInfo& info) const { |
| return as_IB(this)->onNewSurface(info); |
| } |
| +#if SK_SUPPORT_GPU |
| +#include "SkGr.h" |
| +#include "SkGrPixelRef.h" |
| +#endif |
| + |
| +SkSpecialImage* SkSpecialImage::internal_fromBM(const SkBitmap& src) { |
| + // Need to test offset case! |
| + if (src.getTexture()) { |
| + return SkSpecialImage::NewFromGpu(src.bounds(), src.getGenerationID(), src.getTexture()); |
| + } |
| + |
| + return SkSpecialImage::NewFromRaster(src.bounds(), src); |
| +} |
| + |
| +bool SkSpecialImage::internal_getBM(SkBitmap* result) { |
| + const SkSpecialImage_Base* ib = as_IB(this); |
| + |
| + // TODO: need to test offset case! |
| + return ib->getBitmap(result); |
| +} |
| + |
| +#include "SkBitmapDevice.h" |
| +#if SK_SUPPORT_GPU |
| +#include "SkGpuDevice.h" |
| +#endif |
| + |
| +SkImageFilter::Proxy* SkSpecialImage::internal_getProxy() { |
| + if (fProxy) { |
| + SkASSERT(fDevice); |
| + return fProxy; |
| + } |
| + |
| + const SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16); |
| + const SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); |
| + |
| +#if SK_SUPPORT_GPU |
| + if (this->peekTexture()) { |
| + GrContext* context = this->peekTexture()->getContext(); |
| + |
| + // Youch! |
| + fDevice.reset(SkGpuDevice::Create(context, SkSurface::kYes_Budgeted, info, 0, &props, |
|
Stephen White
2016/02/17 17:00:02
This doesn't make sense to me. Why do we need to h
robertphillips
2016/02/17 19:10:28
Done.
|
| + SkGpuDevice::kUninit_InitContents)); |
| + |
| + fProxy = new SkImageFilter::DeviceProxy(fDevice); |
| + return fProxy; |
| + } |
| +#endif |
| + |
| + fDevice.reset(SkBitmapDevice::Create(info, props)); |
| + |
| + fProxy = new SkImageFilter::DeviceProxy(fDevice); |
| + return fProxy; |
| +} |
| + |
| /////////////////////////////////////////////////////////////////////////////// |
| #include "SkImage.h" |
| #if SK_SUPPORT_GPU |
| -#include "SkGr.h" |
| #include "SkGrPriv.h" |
| #endif |
| class SkSpecialImage_Image : public SkSpecialImage_Base { |
| public: |
| SkSpecialImage_Image(const SkIRect& subset, const SkImage* image) |
| - : INHERITED(subset) |
| + : INHERITED(subset, image->uniqueID()) |
| , fImage(SkRef(image)) { |
| } |
| ~SkSpecialImage_Image() override { } |
| + bool isOpaque() const override { return fImage->isOpaque(); } |
| + |
| + size_t getSize() const override { |
| + if (fImage->getTexture()) { |
| + return fImage->getTexture()->gpuMemorySize(); |
| + } else { |
| + SkImageInfo info; |
| + size_t rowBytes; |
| + |
| + if (fImage->peekPixels(&info, &rowBytes)) { |
| + return info.height() * 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()); |
| @@ -77,6 +149,10 @@ public: |
| GrTexture* onPeekTexture() const override { return fImage->getTexture(); } |
| + bool getBitmap(SkBitmap* result) const override { |
| + return false; |
| + } |
| + |
| SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
| #if SK_SUPPORT_GPU |
| GrTexture* texture = fImage->getTexture(); |
| @@ -118,7 +194,7 @@ SkSpecialImage* SkSpecialImage::NewFromImage(const SkIRect& subset, const SkImag |
| class SkSpecialImage_Raster : public SkSpecialImage_Base { |
| public: |
| SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm) |
| - : INHERITED(subset) |
| + : INHERITED(subset, bm.getGenerationID()) |
| , fBitmap(bm) { |
| if (bm.pixelRef()->isPreLocked()) { |
| // we only preemptively lock if there is no chance of triggering something expensive |
| @@ -129,6 +205,10 @@ public: |
| ~SkSpecialImage_Raster() override { } |
| + 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()); |
| @@ -152,6 +232,11 @@ public: |
| return false; |
| } |
| + bool getBitmap(SkBitmap* result) const override { |
| + *result = fBitmap; |
| + return true; |
| + } |
| + |
| SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
| return SkSpecialSurface::NewRaster(info, nullptr); |
| } |
| @@ -173,23 +258,29 @@ SkSpecialImage* SkSpecialImage::NewFromRaster(const SkIRect& subset, const SkBit |
| #include "GrTexture.h" |
| class SkSpecialImage_Gpu : public SkSpecialImage_Base { |
| -public: |
| - SkSpecialImage_Gpu(const SkIRect& subset, GrTexture* tex) |
| - : INHERITED(subset) |
| - , fTexture(SkRef(tex)) { |
| +public: |
| + SkSpecialImage_Gpu(const SkIRect& subset, uint32_t uniqueID, GrTexture* tex, SkAlphaType at) |
| + : INHERITED(subset, uniqueID) |
| + , fTexture(SkRef(tex)) |
| + , fAlphaType(at) { |
| } |
| ~SkSpecialImage_Gpu() override { } |
| + 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()); |
| SkBitmap bm; |
| - static const bool kUnknownOpacity = false; |
| GrWrapTextureInBitmap(fTexture, |
| - fTexture->width(), fTexture->height(), kUnknownOpacity, &bm); |
| + fTexture->width(), fTexture->height(), this->isOpaque(), &bm); |
| canvas->drawBitmapRect(bm, this->subset(), |
| dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| @@ -197,6 +288,18 @@ public: |
| GrTexture* onPeekTexture() const override { return fTexture; } |
| + bool getBitmap(SkBitmap* result) const override { |
| + const SkImageInfo info = GrMakeInfoFromTexture(fTexture, |
| + this->width(), this->height(), |
| + this->isOpaque()); |
| + if (!result->setInfo(info)) { |
| + return false; |
| + } |
| + |
| + result->setPixelRef(new SkGrPixelRef(info, fTexture))->unref(); |
| + return true; |
| + } |
| + |
| SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { |
| GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); |
| desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| @@ -206,18 +309,25 @@ public: |
| private: |
| SkAutoTUnref<GrTexture> fTexture; |
| + const SkAlphaType fAlphaType; |
| typedef SkSpecialImage_Base INHERITED; |
| }; |
| -SkSpecialImage* SkSpecialImage::NewFromGpu(const SkIRect& subset, GrTexture* tex) { |
| +SkSpecialImage* SkSpecialImage::NewFromGpu(const SkIRect& subset, |
| + uint32_t uniqueID, |
| + GrTexture* tex, |
| + SkAlphaType at) { |
| SkASSERT(rect_fits(subset, tex->width(), tex->height())); |
| - return new SkSpecialImage_Gpu(subset, tex); |
| + return new SkSpecialImage_Gpu(subset, uniqueID, tex, at); |
| } |
| #else |
| -SkSpecialImage* SkSpecialImage::NewFromGpu(const SkIRect& subset, GrTexture* tex) { |
| +SkSpecialImage* SkSpecialImage::NewFromGpu(const SkIRect& subset, |
| + uint32_t uniqueID, |
| + GrTexture* tex, |
| + SkAlphaType at) { |
| return nullptr; |
| } |