Factory methods for heap-allocated SkColorFilter 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 SkColorFilter 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/175293002

git-svn-id: http://skia.googlecode.com/svn/trunk@13539 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
commit-bot@chromium.org 2014-02-21 18:46:30 +00:00
parent a3baf3be0e
commit 727a352f74
9 changed files with 26 additions and 16 deletions

View File

@ -33,7 +33,7 @@ protected:
0, 1, 0, 0, amount255,
0, 0, 1, 0, amount255,
0, 0, 0, 1, 0 };
SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
return SkColorFilterImageFilter::Create(filter, input);
}
@ -44,7 +44,7 @@ protected:
matrix[1] = matrix[6] = matrix[11] = 0.7152f;
matrix[2] = matrix[7] = matrix[12] = 0.0722f;
matrix[18] = 1.0f;
SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
return SkColorFilterImageFilter::Create(filter, input);
}

View File

@ -28,7 +28,7 @@ static SkImageFilter* make_brightness(float amount, SkImageFilter* input = NULL)
0, 1, 0, 0, amount255,
0, 0, 1, 0, amount255,
0, 0, 0, 1, 0 };
SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
return SkColorFilterImageFilter::Create(filter, input);
}
@ -39,7 +39,7 @@ static SkImageFilter* make_grayscale(SkImageFilter* input = NULL) {
matrix[1] = matrix[6] = matrix[11] = 0.7152f;
matrix[2] = matrix[7] = matrix[12] = 0.0722f;
matrix[18] = 1.0f;
SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
return SkColorFilterImageFilter::Create(filter, input);
}

View File

@ -29,11 +29,11 @@ private:
};
static void setColorMatrix(SkPaint* paint, const SkColorMatrix& matrix) {
paint->setColorFilter(SkNEW_ARGS(SkColorMatrixFilter, (matrix)))->unref();
paint->setColorFilter(SkColorMatrixFilter::Create(matrix))->unref();
}
static void setArray(SkPaint* paint, const SkScalar array[]) {
paint->setColorFilter(SkNEW_ARGS(SkColorMatrixFilter, (array)))->unref();
paint->setColorFilter(SkColorMatrixFilter::Create(array))->unref();
}
namespace skiagm {

View File

@ -142,7 +142,7 @@ protected:
0, 0, SK_Scalar1, 0, 0,
0, 0, 0, 0.5f, 0 };
SkAutoTUnref<SkColorFilter> matrixFilter(new SkColorMatrixFilter(matrix));
SkAutoTUnref<SkColorFilter> matrixFilter(SkColorMatrixFilter::Create(matrix));
SkAutoTUnref<SkImageFilter> colorMorph(SkColorFilterImageFilter::Create(matrixFilter, morph));
SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
SkAutoTUnref<SkImageFilter> blendColor(new SkXfermodeImageFilter(mode, colorMorph));
@ -157,8 +157,8 @@ protected:
0, SK_Scalar1, 0, 0, 0,
0, 0, SK_Scalar1, 0, 0,
0, 0, 0, 0.5f, 0 };
SkColorMatrixFilter matrixCF(matrix);
SkAutoTUnref<SkImageFilter> matrixFilter(SkColorFilterImageFilter::Create(&matrixCF));
SkAutoTUnref<SkColorMatrixFilter> matrixCF(SkColorMatrixFilter::Create(matrix));
SkAutoTUnref<SkImageFilter> matrixFilter(SkColorFilterImageFilter::Create(matrixCF));
SimpleOffsetFilter offsetFilter(SkIntToScalar(10), SkIntToScalar(10), matrixFilter);
SkAutoTUnref<SkXfermode> arith(SkArithmeticMode::Create(0, SK_Scalar1, SK_Scalar1, 0));

View File

@ -13,8 +13,12 @@
class SK_API SkColorMatrixFilter : public SkColorFilter {
public:
explicit SkColorMatrixFilter(const SkColorMatrix&);
SkColorMatrixFilter(const SkScalar array[20]);
static SkColorMatrixFilter* Create(const SkColorMatrix& cm) {
return SkNEW_ARGS(SkColorMatrixFilter, (cm));
}
static SkColorMatrixFilter* Create(const SkScalar array[20]) {
return SkNEW_ARGS(SkColorMatrixFilter, (array));
}
// overrides from SkColorFilter
virtual void filterSpan(const SkPMColor src[], int count, SkPMColor[]) const SK_OVERRIDE;
@ -38,6 +42,12 @@ protected:
SkColorMatrixFilter(SkReadBuffer& buffer);
virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
#ifdef SK_SUPPORT_LEGACY_PUBLICEFFECTCONSTRUCTORS
public:
#endif
explicit SkColorMatrixFilter(const SkColorMatrix&);
SkColorMatrixFilter(const SkScalar array[20]);
private:
SkColorMatrix fMatrix;

View File

@ -69,7 +69,7 @@ SkColorFilterImageFilter* SkColorFilterImageFilter::Create(SkColorFilter* cf,
if (inputColorFilter->asColorMatrix(inputMatrix) && !matrix_needs_clamping(inputMatrix)) {
SkScalar combinedMatrix[20];
mult_color_matrix(inputMatrix, colorMatrix, combinedMatrix);
SkAutoTUnref<SkColorFilter> newCF(SkNEW_ARGS(SkColorMatrixFilter, (combinedMatrix)));
SkAutoTUnref<SkColorFilter> newCF(SkColorMatrixFilter::Create(combinedMatrix));
return SkNEW_ARGS(SkColorFilterImageFilter, (newCF, input->getInput(0), cropRect));
}
}

View File

@ -557,7 +557,7 @@ SkColorFilter* SkColorFilter::CreateLightingFilter(SkColor mul, SkColor add) {
SkIntToScalar(SkColorGetG(add)),
SkIntToScalar(SkColorGetB(add)),
0);
return SkNEW_ARGS(SkColorMatrixFilter, (matrix));
return SkColorMatrixFilter::Create(matrix);
}
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkColorFilter)

View File

@ -263,7 +263,7 @@ void forceLinking() {
GrConfigConversionEffect::kNone_PMConversion,
SkMatrix::I());
SkScalar matrix[20];
SkColorMatrixFilter cmf(matrix);
SkAutoTUnref<SkColorMatrixFilter> cmf(SkColorMatrixFilter::Create(matrix));
}
#endif

View File

@ -108,7 +108,7 @@ static SkImageFilter* make_scale(float amount, SkImageFilter* input = NULL) {
0, s, 0, 0, 0,
0, 0, s, 0, 0,
0, 0, 0, s, 0 };
SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
return SkColorFilterImageFilter::Create(filter, input);
}
@ -119,7 +119,7 @@ static SkImageFilter* make_grayscale(SkImageFilter* input = NULL, const SkImageF
matrix[1] = matrix[6] = matrix[11] = 0.7152f;
matrix[2] = matrix[7] = matrix[12] = 0.0722f;
matrix[18] = 1.0f;
SkAutoTUnref<SkColorFilter> filter(new SkColorMatrixFilter(matrix));
SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
return SkColorFilterImageFilter::Create(filter, input, cropRect);
}