Image filters: Replace all use of tryAllocPixels() with createDevice().

In order to have a central pinch point for bitmap allocation, change all
filters to use Proxy::createDevice() instead of allocating memory
directly with SkBitmap::tryAllocPixels().

This will aid in moving filter backing stores and caches to
discardable memory.

BUG=skia:

Review URL: https://codereview.chromium.org/1414843003
This commit is contained in:
senorblanco 2015-10-20 10:17:34 -07:00 committed by Commit bot
parent 0bccd8749b
commit 1d3ff43495
7 changed files with 57 additions and 21 deletions

View File

@ -7,6 +7,7 @@
#include "SkAlphaThresholdFilter.h"
#include "SkBitmap.h"
#include "SkDevice.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkRegion.h"
@ -303,7 +304,7 @@ void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const {
buffer.writeRegion(fRegion);
}
bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy*, const SkBitmap& src,
bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy* proxy, const SkBitmap& src,
const Context& ctx, SkBitmap* dst,
SkIPoint* offset) const {
SkASSERT(src.colorType() == kN32_SkColorType);
@ -323,9 +324,12 @@ bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy*, const SkBitmap& src,
return false;
}
if (!dst->tryAllocPixels(src.info())) {
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(src.width(), src.height()));
if (!device) {
return false;
}
*dst = device->accessBitmap(false);
SkAutoLockPixels alp_dst(*dst);
U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF);
U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF);

View File

@ -8,6 +8,7 @@
#include "SkBitmap.h"
#include "SkBlurImageFilter.h"
#include "SkColorPriv.h"
#include "SkDevice.h"
#include "SkGpuBlurUtils.h"
#include "SkOpts.h"
#include "SkReadBuffer.h"
@ -90,9 +91,12 @@ bool SkBlurImageFilter::onFilterImage(Proxy* proxy,
return false;
}
if (!dst->tryAllocPixels(src.info().makeWH(srcBounds.width(), srcBounds.height()))) {
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(srcBounds.width(), srcBounds.height()));
if (!device) {
return false;
}
*dst = device->accessBitmap(false);
SkAutoLockPixels alp_dst(*dst);
dst->getBounds(&dstBounds);
SkVector sigma = mapSigma(fSigma, ctx.ctm());
@ -113,10 +117,12 @@ bool SkBlurImageFilter::onFilterImage(Proxy* proxy,
return true;
}
SkBitmap temp;
if (!temp.tryAllocPixels(dst->info())) {
SkAutoTUnref<SkBaseDevice> tempDevice(proxy->createDevice(dst->width(), dst->height()));
if (!tempDevice) {
return false;
}
SkBitmap temp = tempDevice->accessBitmap(false);
SkAutoLockPixels alpTemp(temp);
offset->fX = srcBounds.fLeft;
offset->fY = srcBounds.fTop;

View File

@ -6,6 +6,7 @@
*/
#include "SkDisplacementMapEffect.h"
#include "SkDevice.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkUnPreMultiply.h"
@ -239,9 +240,12 @@ bool SkDisplacementMapEffect::onFilterImage(Proxy* proxy,
return false;
}
if (!dst->tryAllocPixels(color.info().makeWH(bounds.width(), bounds.height()))) {
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height()));
if (!device) {
return false;
}
*dst = device->accessBitmap(false);
SkAutoLockPixels alp_dst(*dst);
SkVector scale = SkVector::Make(fScale, fScale);
ctx.ctm().mapVectors(&scale, 1);

View File

@ -8,6 +8,7 @@
#include "SkLightingImageFilter.h"
#include "SkBitmap.h"
#include "SkColorPriv.h"
#include "SkDevice.h"
#include "SkPoint3.h"
#include "SkReadBuffer.h"
#include "SkTypes.h"
@ -1190,9 +1191,12 @@ bool SkDiffuseLightingImageFilter::onFilterImage(Proxy* proxy,
return false;
}
if (!dst->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height()));
if (!device) {
return false;
}
*dst = device->accessBitmap(false);
SkAutoLockPixels alp_dst(*dst);
SkMatrix matrix(ctx.ctm());
matrix.postTranslate(SkIntToScalar(-srcOffset.x()), SkIntToScalar(-srcOffset.y()));
@ -1331,9 +1335,12 @@ bool SkSpecularLightingImageFilter::onFilterImage(Proxy* proxy,
return false;
}
if (!dst->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height()));
if (!device) {
return false;
}
*dst = device->accessBitmap(false);
SkAutoLockPixels alp_dst(*dst);
SpecularLightingType lightingType(fKS, fShininess);
offset->fX = bounds.left();

View File

@ -8,6 +8,7 @@
#include "SkBitmap.h"
#include "SkMagnifierImageFilter.h"
#include "SkColorPriv.h"
#include "SkDevice.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkValidationUtils.h"
@ -300,7 +301,7 @@ void SkMagnifierImageFilter::flatten(SkWriteBuffer& buffer) const {
buffer.writeScalar(fInset);
}
bool SkMagnifierImageFilter::onFilterImage(Proxy*, const SkBitmap& src,
bool SkMagnifierImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& src,
const Context&, SkBitmap* dst,
SkIPoint* offset) const {
if ((src.colorType() != kN32_SkColorType) ||
@ -315,9 +316,12 @@ bool SkMagnifierImageFilter::onFilterImage(Proxy*, const SkBitmap& src,
return false;
}
if (!dst->tryAllocPixels(src.info())) {
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(src.width(), src.height()));
if (!device) {
return false;
}
*dst = device->accessBitmap(false);
SkAutoLockPixels alp_dst(*dst);
SkScalar inv_inset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1;

View File

@ -8,6 +8,7 @@
#include "SkMatrixConvolutionImageFilter.h"
#include "SkBitmap.h"
#include "SkColorPriv.h"
#include "SkDevice.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkRect.h"
@ -241,16 +242,18 @@ void SkMatrixConvolutionImageFilter::filterBorderPixels(const SkBitmap& src,
// FIXME: This should be refactored to SkImageFilterUtils for
// use by other filters. For now, we assume the input is always
// premultiplied and unpremultiply it
static SkBitmap unpremultiplyBitmap(const SkBitmap& src)
static SkBitmap unpremultiplyBitmap(SkImageFilter::Proxy* proxy, const SkBitmap& src)
{
SkAutoLockPixels alp(src);
if (!src.getPixels()) {
return SkBitmap();
}
SkBitmap result;
if (!result.tryAllocPixels(src.info())) {
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(src.width(), src.height()));
if (!device) {
return SkBitmap();
}
SkBitmap result = device->accessBitmap(false);
SkAutoLockPixels alp_result(result);
for (int y = 0; y < src.height(); ++y) {
const uint32_t* srcRow = src.getAddr32(0, y);
uint32_t* dstRow = result.getAddr32(0, y);
@ -282,7 +285,7 @@ bool SkMatrixConvolutionImageFilter::onFilterImage(Proxy* proxy,
}
if (!fConvolveAlpha && !src.isOpaque()) {
src = unpremultiplyBitmap(src);
src = unpremultiplyBitmap(proxy, src);
}
SkAutoLockPixels alp(src);
@ -290,9 +293,12 @@ bool SkMatrixConvolutionImageFilter::onFilterImage(Proxy* proxy,
return false;
}
if (!result->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height()));
if (!device) {
return false;
}
*result = device->accessBitmap(false);
SkAutoLockPixels alp_result(*result);
offset->fX = bounds.fLeft;
offset->fY = bounds.fTop;

View File

@ -8,6 +8,7 @@
#include "SkMorphologyImageFilter.h"
#include "SkBitmap.h"
#include "SkColorPriv.h"
#include "SkDevice.h"
#include "SkOpts.h"
#include "SkReadBuffer.h"
#include "SkRect.h"
@ -76,10 +77,6 @@ bool SkMorphologyImageFilter::filterImageGeneric(SkMorphologyImageFilter::Proc p
return false;
}
if (!dst->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
return false;
}
SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()),
SkIntToScalar(this->radius().height()));
ctx.ctm().mapVectors(&radius, 1);
@ -100,12 +97,20 @@ bool SkMorphologyImageFilter::filterImageGeneric(SkMorphologyImageFilter::Proc p
return true;
}
SkBitmap temp;
if (!temp.tryAllocPixels(dst->info())) {
SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height()));
if (!device) {
return false;
}
*dst = device->accessBitmap(false);
SkAutoLockPixels alp_dst(*dst);
if (width > 0 && height > 0) {
SkAutoTUnref<SkBaseDevice> tempDevice(proxy->createDevice(dst->width(), dst->height()));
if (!tempDevice) {
return false;
}
SkBitmap temp = tempDevice->accessBitmap(false);
SkAutoLockPixels alp_temp(temp);
callProcX(procX, src, &temp, width, srcBounds);
SkIRect tmpBounds = SkIRect::MakeWH(srcBounds.width(), srcBounds.height());
callProcY(procY, temp, dst, height, tmpBounds);