Factory methods for heap-allocated SkMaskFilter objects.

This is part of an effort to ensure that all SkPaint effects can only be
allocated on the heap.

This patch makes the constructors of SkMaskFilter and its subclasses non-public
and instead provides factory methods for  creating these objects on the heap. We
temporarily keep constructor of publicly visible classes public behind a flag.

BUG=skia:2187
R=scroggo@google.com, mtklein@google.com, reed@google.com

Author: dominikg@chromium.org

Review URL: https://codereview.chromium.org/173633003

git-svn-id: http://skia.googlecode.com/svn/trunk@13527 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
commit-bot@chromium.org 2014-02-21 10:13:32 +00:00
parent 0b45dc45d6
commit 7c9d0f3104
10 changed files with 48 additions and 28 deletions

View File

@ -83,7 +83,7 @@ protected:
return;
}
fMaskFilter.reset(SkNEW(SkStippleMaskFilter));
fMaskFilter.reset(SkStippleMaskFilter::Create());
}
virtual void onDraw(SkCanvas* canvas) {

View File

@ -40,8 +40,6 @@ class SK_API SkMaskFilter : public SkFlattenable {
public:
SK_DECLARE_INST_COUNT(SkMaskFilter)
SkMaskFilter() {}
/** Returns the format of the resulting mask that this subclass will return
when its filterMask() method is called.
*/
@ -136,6 +134,7 @@ public:
SK_DEFINE_FLATTENABLE_TYPE(SkMaskFilter)
protected:
SkMaskFilter() {}
// empty for now, but lets get our subclass to remember to init us for the future
SkMaskFilter(SkReadBuffer& buffer) : INHERITED(buffer) {}

View File

@ -23,10 +23,9 @@ public:
uint8_t fSpecular; // exponent, 4.4 right now
};
SkEmbossMaskFilter(SkScalar blurSigma, const Light& light);
SK_ATTR_DEPRECATED("use sigma version")
SkEmbossMaskFilter(const Light& light, SkScalar blurRadius);
static SkEmbossMaskFilter* Create(SkScalar blurSigma, const Light& light) {
return SkNEW_ARGS(SkEmbossMaskFilter, (blurSigma, light));
}
// overrides from SkMaskFilter
// This method is not exported to java.
@ -42,6 +41,11 @@ protected:
SkEmbossMaskFilter(SkReadBuffer&);
virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
#ifdef SK_SUPPORT_LEGACY_PUBLICEFFECTCONSTRUCTORS
public:
#endif
SkEmbossMaskFilter(SkScalar blurSigma, const Light& light);
private:
Light fLight;
SkScalar fBlurSigma;

View File

@ -12,9 +12,6 @@
class SK_API SkKernel33ProcMaskFilter : public SkMaskFilter {
public:
SkKernel33ProcMaskFilter(unsigned percent256 = 256)
: fPercent256(percent256) {}
virtual uint8_t computeValue(uint8_t* const* srcRows) const = 0;
virtual SkMask::Format getFormat() const SK_OVERRIDE;
@ -24,6 +21,8 @@ public:
SkDEVCODE(virtual void toString(SkString* str) const SK_OVERRIDE;)
protected:
SkKernel33ProcMaskFilter(unsigned percent256 = 256)
: fPercent256(percent256) {}
SkKernel33ProcMaskFilter(SkReadBuffer& rb);
virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
@ -37,10 +36,8 @@ private:
class SK_API SkKernel33MaskFilter : public SkKernel33ProcMaskFilter {
public:
SkKernel33MaskFilter(const int coeff[3][3], int shift, int percent256 = 256)
: SkKernel33ProcMaskFilter(percent256) {
memcpy(fKernel, coeff, 9 * sizeof(int));
fShift = shift;
static SkKernel33MaskFilter* Create(const int coeff[3][3], int shift, int percent256 = 256) {
return SkNEW_ARGS(SkKernel33MaskFilter, (coeff, shift, percent256));
}
// override from SkKernel33ProcMaskFilter
@ -49,6 +46,16 @@ public:
SkDEVCODE(virtual void toString(SkString* str) const SK_OVERRIDE;)
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkKernel33MaskFilter)
protected:
#ifdef SK_SUPPORT_LEGACY_PUBLICEFFECTCONSTRUCTORS
public:
#endif
SkKernel33MaskFilter(const int coeff[3][3], int shift, int percent256 = 256)
: SkKernel33ProcMaskFilter(percent256) {
memcpy(fKernel, coeff, 9 * sizeof(int));
fShift = shift;
}
private:
int fKernel[3][3];
int fShift;

View File

@ -15,7 +15,8 @@
*/
class SK_API SkStippleMaskFilter : public SkMaskFilter {
public:
SkStippleMaskFilter() : INHERITED() {
static SkStippleMaskFilter* Create() {
return SkNEW(SkStippleMaskFilter);
}
virtual bool filterMask(SkMask* dst, const SkMask& src,
@ -35,6 +36,12 @@ protected:
: SkMaskFilter(buffer) {
}
#ifdef SK_SUPPORT_LEGACY_PUBLICEFFECTCONSTRUCTORS
public:
#endif
SkStippleMaskFilter() : INHERITED() {
}
private:
typedef SkMaskFilter INHERITED;
};

View File

@ -18,8 +18,6 @@
*/
class SK_API SkTableMaskFilter : public SkMaskFilter {
public:
SkTableMaskFilter();
SkTableMaskFilter(const uint8_t table[256]);
virtual ~SkTableMaskFilter();
/** Utility that sets the gamma table
@ -31,6 +29,10 @@ public:
*/
static void MakeClipTable(uint8_t table[256], uint8_t min, uint8_t max);
static SkTableMaskFilter* Create(const uint8_t table[256]) {
return SkNEW_ARGS(SkTableMaskFilter, (table));
}
static SkTableMaskFilter* CreateGamma(SkScalar gamma) {
uint8_t table[256];
MakeGammaTable(table, gamma);
@ -54,6 +56,12 @@ protected:
SkTableMaskFilter(SkReadBuffer& rb);
virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
#ifdef SK_SUPPORT_LEGACY_PUBLICEFFECTCONSTRUCTORS
public:
#endif
SkTableMaskFilter();
SkTableMaskFilter(const uint8_t table[256]);
private:
uint8_t fTable[256];

View File

@ -388,7 +388,7 @@ protected:
light.fAmbient = 0x48;
light.fSpecular = 0x80;
SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(12)/5);
SkEmbossMaskFilter* embossFilter = new SkEmbossMaskFilter(sigma, light);
SkEmbossMaskFilter* embossFilter = SkEmbossMaskFilter::Create(sigma, light);
SkXfermode* xfermode = SkXfermode::Create(SkXfermode::kXor_Mode);
SkColorFilter* lightingFilter = SkColorFilter::CreateLightingFilter(

View File

@ -53,7 +53,7 @@ protected:
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(SkIntToScalar(10));
paint.setMaskFilter(new SkEmbossMaskFilter(
paint.setMaskFilter(SkEmbossMaskFilter::Create(
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)), fLight))->unref();
paint.setShader(new SkColorShader(SK_ColorBLUE))->unref();
paint.setDither(true);

View File

@ -49,7 +49,7 @@ SkMaskFilter* SkBlurMaskFilter::CreateEmboss(SkScalar blurSigma, const SkScalar
light.fAmbient = SkToU8(am);
light.fSpecular = SkToU8(sp);
return SkNEW_ARGS(SkEmbossMaskFilter, (blurSigma, light));
return SkEmbossMaskFilter::Create(blurSigma, light);
}
///////////////////////////////////////////////////////////////////////////////
@ -68,13 +68,6 @@ SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light)
normalize(fLight.fDirection);
}
SkEmbossMaskFilter::SkEmbossMaskFilter(const Light& light, SkScalar blurRadius)
: fLight(light) {
normalize(fLight.fDirection);
fBlurSigma = SkBlurMask::ConvertRadiusToSigma(blurRadius);
}
SkMask::Format SkEmbossMaskFilter::getFormat() const {
return SkMask::k3D_Format;
}

View File

@ -234,6 +234,7 @@ static const char* computeAnimatorState(int enabled, int focused, SkButtonWidget
return "enabled";
}
#include "SkBlurMask.h"
#include "SkBlurMaskFilter.h"
#include "SkEmbossMaskFilter.h"
@ -255,7 +256,8 @@ static void create_emboss(SkPaint* paint, SkScalar radius, bool focus, bool pres
if (focus)
light.fDirection[2] += SK_Scalar1/4;
paint->setMaskFilter(new SkEmbossMaskFilter(light, radius))->unref();
SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius);
paint->setMaskFilter(new SkEmbossMaskFilter(sigma, light))->unref();
}
void SkPushButtonWidget::onDraw(SkCanvas* canvas)