Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Unified Diff: src/core/SkImageFilter.cpp

Issue 920513003: Make filters use SkImage instead of SkBitmap Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/core/SkImageFilter.cpp
diff --git a/src/core/SkImageFilter.cpp b/src/core/SkImageFilter.cpp
index 34b1b9bc3e88557043c494dd1a80d5068b1132e5..fb553c1686230f022091e36575e0d388d7d9558b 100644
--- a/src/core/SkImageFilter.cpp
+++ b/src/core/SkImageFilter.cpp
@@ -10,10 +10,14 @@
#include "SkBitmap.h"
#include "SkChecksum.h"
#include "SkDevice.h"
+#include "SkImage.h"
+#include "SkImagePriv.h"
+#include "SkImage_Base.h"
#include "SkLazyPtr.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkRect.h"
+#include "SkSurface.h"
#include "SkTDynamicHash.h"
#include "SkTInternalLList.h"
#include "SkValidationUtils.h"
@@ -162,13 +166,13 @@ void SkImageFilter::flatten(SkWriteBuffer& buffer) const {
buffer.writeUInt(fUniqueID);
}
-bool SkImageFilter::filterImage(Proxy* proxy, const SkBitmap& src,
+bool SkImageFilter::filterImage(Proxy* proxy, SkImage& src,
const Context& context,
- SkBitmap* result, SkIPoint* offset) const {
+ SkAutoTUnref<SkImage>& result, SkIPoint* offset) const {
SkASSERT(result);
SkASSERT(offset);
- uint32_t srcGenID = fUsesSrcInput ? src.getGenerationID() : 0;
- Cache::Key key(fUniqueID, context.ctm(), context.clipBounds(), srcGenID);
+ uint32_t srcUniqueID = fUsesSrcInput ? src.uniqueID() : 0;
+ Cache::Key key(fUniqueID, context.ctm(), context.clipBounds(), srcUniqueID);
if (context.cache()) {
if (context.cache()->get(key, result, offset)) {
return true;
@@ -217,8 +221,8 @@ void SkImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const {
}
}
-bool SkImageFilter::onFilterImage(Proxy*, const SkBitmap&, const Context&,
- SkBitmap*, SkIPoint*) const {
+bool SkImageFilter::onFilterImage(Proxy*, SkImage&, const Context&,
+ SkAutoTUnref<SkImage>&, SkIPoint*) const {
return false;
}
@@ -226,21 +230,27 @@ bool SkImageFilter::canFilterImageGPU() const {
return this->asFragmentProcessor(NULL, NULL, SkMatrix::I(), SkIRect());
}
-bool SkImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx,
- SkBitmap* result, SkIPoint* offset) const {
+bool SkImageFilter::filterImageGPU(Proxy* proxy, SkImage& src, const Context& ctx,
+ SkAutoTUnref<SkImage>& result, SkIPoint* offset) const {
#if SK_SUPPORT_GPU
- SkBitmap input = src;
- SkASSERT(fInputCount == 1);
+ SkAutoTUnref<SkImage> filtered(&src);
SkIPoint srcOffset = SkIPoint::Make(0, 0);
+
+ SkASSERT(fInputCount == 1);
if (this->getInput(0) &&
- !this->getInput(0)->getInputResultGPU(proxy, src, ctx, &input, &srcOffset)) {
+ !this->getInput(0)->getInputResultGPU(proxy, src, ctx, filtered, &srcOffset)) {
return false;
}
- GrTexture* srcTexture = input.getTexture();
+
+ SkAutoTUnref<SkImage> cropped;
SkIRect bounds;
- if (!this->applyCropRect(ctx, proxy, input, &srcOffset, &bounds, &input)) {
+ if (!this->applyCropRect(ctx, proxy, *filtered, &srcOffset, &bounds, cropped)) {
return false;
}
+ filtered.reset(NULL);
+
+ GrTexture* srcTexture = cropped->getTexture();
+
SkRect srcRect = SkRect::Make(bounds);
SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height());
GrContext* context = srcTexture->getContext();
@@ -270,17 +280,18 @@ bool SkImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Cont
paint.addColorProcessor(fp)->unref();
context->drawNonAARectToRect(paint, SkMatrix::I(), dstRect, srcRect);
- WrapTexture(dst, bounds.width(), bounds.height(), result);
+ if (!WrapTexture(dst, bounds.width(), bounds.height(), result)) {
+ return false;
+ }
return true;
}
#endif
return false;
}
-bool SkImageFilter::applyCropRect(const Context& ctx, const SkBitmap& src,
+bool SkImageFilter::applyCropRect(const Context& ctx, SkImage& src,
const SkIPoint& srcOffset, SkIRect* bounds) const {
- SkIRect srcBounds;
- src.getBounds(&srcBounds);
+ SkIRect srcBounds = SkIRect::MakeWH(src.width(), src.height());
srcBounds.offset(srcOffset);
SkRect cropRect;
ctx.ctm().mapRect(&cropRect, fCropRect.rect());
@@ -297,10 +308,10 @@ bool SkImageFilter::applyCropRect(const Context& ctx, const SkBitmap& src,
return true;
}
-bool SkImageFilter::applyCropRect(const Context& ctx, Proxy* proxy, const SkBitmap& src,
- SkIPoint* srcOffset, SkIRect* bounds, SkBitmap* dst) const {
- SkIRect srcBounds;
- src.getBounds(&srcBounds);
+bool SkImageFilter::applyCropRect(const Context& ctx, Proxy* proxy, SkImage& src,
+ SkIPoint* srcOffset, SkIRect* bounds,
+ SkAutoTUnref<SkImage>& dst) const {
+ SkIRect srcBounds = SkIRect::MakeWH(src.width(), src.height());
srcBounds.offset(*srcOffset);
SkRect cropRect;
ctx.ctm().mapRect(&cropRect, fCropRect.rect());
@@ -315,19 +326,19 @@ bool SkImageFilter::applyCropRect(const Context& ctx, Proxy* proxy, const SkBitm
return false;
}
if (srcBounds.contains(*bounds)) {
- *dst = src;
+ dst.reset(&src);
return true;
} else {
- SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds->width(), bounds->height()));
- if (!device) {
+ SkAutoTUnref<SkSurface> surface(proxy->createSurface(bounds->width(), bounds->height()));
+ if (!surface) {
return false;
}
- SkCanvas canvas(device);
- canvas.clear(0x00000000);
- canvas.drawBitmap(src, srcOffset->x() - bounds->x(), srcOffset->y() - bounds->y());
+ SkCanvas* canvas = surface->getCanvas();
+ canvas->clear(0x00000000);
+ canvas->drawImage(&src, srcOffset->x() - bounds->x(), srcOffset->y() - bounds->y());
*srcOffset = SkIPoint::Make(bounds->x(), bounds->y());
- *dst = device->accessBitmap(false);
- return true;
+ dst.reset(surface->newImageSnapshot(SkSurface::kYes_Budgeted));
+ return dst != NULL;
}
}
@@ -369,15 +380,25 @@ bool SkImageFilter::asColorFilter(SkColorFilter**) const {
#if SK_SUPPORT_GPU
-void SkImageFilter::WrapTexture(GrTexture* texture, int width, int height, SkBitmap* result) {
+bool SkImageFilter::WrapTexture(GrTexture* texture, int width, int height,
+ SkAutoTUnref<SkImage>& result) {
+ // TODO: currently we go texture->bitmap->surface, when we should go rendertarget->surface.
+ SkBitmap bitmap;
SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
- result->setInfo(info);
- result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
+ bitmap.setInfo(info);
+ bitmap.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
+ SkImage* image = SkNewImageFromBitmapTexture(bitmap, texture->desc().fSampleCnt,
+ SkSurface::kYes_Budgeted);
+ if (NULL == image) {
+ return false;
+ }
+ result.reset(image);
+ return true;
}
bool SkImageFilter::getInputResultGPU(SkImageFilter::Proxy* proxy,
- const SkBitmap& src, const Context& ctx,
- SkBitmap* result, SkIPoint* offset) const {
+ SkImage& src, const Context& ctx,
+ SkAutoTUnref<SkImage>& result, SkIPoint* offset) const {
// Ensure that GrContext calls under filterImage and filterImageGPU below will see an identity
// matrix with no clip and that the matrix, clip, and render target set before this function was
// called are restored before we return to the caller.
@@ -388,12 +409,17 @@ bool SkImageFilter::getInputResultGPU(SkImageFilter::Proxy* proxy,
} else {
if (this->filterImage(proxy, src, ctx, result, offset)) {
if (!result->getTexture()) {
- const SkImageInfo info = result->info();
- if (kUnknown_SkColorType == info.colorType()) {
+ SkBitmap bitmap;
+ if (!as_IB(result)->getROPixels(&bitmap)) {
+ return false;
+ }
+ if (kUnknown_SkColorType == bitmap.colorType()) {
+ return false;
+ }
+ SkAutoTUnref<GrTexture> resultTex(GrRefCachedBitmapTexture(context, bitmap, NULL));
+ if (!WrapTexture(resultTex, bitmap.width(), bitmap.height(), result)) {
return false;
}
- SkAutoTUnref<GrTexture> resultTex(GrRefCachedBitmapTexture(context, *result, NULL));
- result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, resultTex)))->unref();
}
return true;
} else {
@@ -419,10 +445,10 @@ public:
}
}
struct Value {
- Value(const Key& key, const SkBitmap& bitmap, const SkIPoint& offset)
- : fKey(key), fBitmap(bitmap), fOffset(offset) {}
+ Value(const Key& key, SkImage& image, const SkIPoint& offset)
+ : fKey(key), fImage(SkRef(&image)), fOffset(offset) {}
Key fKey;
- SkBitmap fBitmap;
+ SkAutoTUnref<SkImage> fImage;
SkIPoint fOffset;
static const Key& GetKey(const Value& v) {
return v.fKey;
@@ -432,10 +458,10 @@ public:
}
SK_DECLARE_INTERNAL_LLIST_INTERFACE(Value);
};
- bool get(const Key& key, SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE {
+ bool get(const Key& key, SkAutoTUnref<SkImage>& result, SkIPoint* offset) const SK_OVERRIDE {
SkAutoMutexAcquire mutex(fMutex);
if (Value* v = fLookup.find(key)) {
- *result = v->fBitmap;
+ result.reset(SkRef(v->fImage.get()));
*offset = v->fOffset;
if (v != fLRU.head()) {
fLRU.remove(v);
@@ -445,15 +471,15 @@ public:
}
return false;
}
- void set(const Key& key, const SkBitmap& result, const SkIPoint& offset) SK_OVERRIDE {
+ void set(const Key& key, SkImage& image, const SkIPoint& offset) SK_OVERRIDE {
SkAutoMutexAcquire mutex(fMutex);
if (Value* v = fLookup.find(key)) {
removeInternal(v);
}
- Value* v = new Value(key, result, offset);
+ Value* v = new Value(key, image, offset);
fLookup.add(v);
fLRU.addToHead(v);
- fCurrentBytes += result.getSize();
+ fCurrentBytes += as_IB(&image)->getSize();
while (fCurrentBytes > fMaxBytes) {
Value* tail = fLRU.tail();
SkASSERT(tail);
@@ -465,7 +491,7 @@ public:
}
private:
void removeInternal(Value* v) {
- fCurrentBytes -= v->fBitmap.getSize();
+ fCurrentBytes -= as_IB(v->fImage)->getSize();
fLRU.remove(v);
fLookup.remove(v->fKey);
delete v;

Powered by Google App Engine
This is Rietveld 408576698