Factory methods for heap-allocated SkPathEffect and SkXfermode 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 SkPathEffect, SkXfermode and
their subclasses non-public and instead provides factory methods for
creating these objects on the heap. We temporarily keep the constructors
of the following classes public to not break Chrome/Blink:
SkXfermode
SkCornerPathEffect
SkDashPathEffect
BUG=skia:2187
R=scroggo@google.com, reed@google.com, mtklein@google.com, bungeman@google.com
Author: dominikg@chromium.org
Review URL: https://codereview.chromium.org/166583002
git-svn-id: http://skia.googlecode.com/svn/trunk@13519 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/samplecode/SampleAll.cpp b/samplecode/SampleAll.cpp
index 6410670..bb5aadd 100644
--- a/samplecode/SampleAll.cpp
+++ b/samplecode/SampleAll.cpp
@@ -144,7 +144,7 @@
static void r5(SkLayerRasterizer* rast, SkPaint& p) {
rast->addLayer(p);
- p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref();
+ p.setPathEffect(SkDiscretePathEffect::Create(SK_Scalar1*4, SK_Scalar1*3))->unref();
p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
rast->addLayer(p);
}
@@ -217,7 +217,7 @@
SkMatrix lattice;
lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
lattice.postRotate(SkIntToScalar(30), 0, 0);
- p.setPathEffect(new SkLine2DPathEffect(SK_Scalar1*2, lattice))->unref();
+ p.setPathEffect(SkLine2DPathEffect::Create(SK_Scalar1*2, lattice))->unref();
p.setXfermodeMode(SkXfermode::kClear_Mode);
rast->addLayer(p);
@@ -550,11 +550,11 @@
path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
path.close();
path.offset(SkIntToScalar(-6), 0);
- SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12),
+ SkPathEffect* outer = SkPath1DPathEffect::Create(path, SkIntToScalar(12),
gPhase, SkPath1DPathEffect::kRotate_Style);
- SkPathEffect* inner = new SkDiscretePathEffect(SkIntToScalar(2),
+ SkPathEffect* inner = SkDiscretePathEffect::Create(SkIntToScalar(2),
SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2));
- SkPathEffect* result = new SkComposePathEffect(outer, inner);
+ SkPathEffect* result = SkComposePathEffect::Create(outer, inner);
outer->unref();
inner->unref();
return result;
@@ -627,12 +627,12 @@
canvas->restore();
if (1) {
- SkAvoidXfermode mode(SK_ColorWHITE, 0xFF,
- SkAvoidXfermode::kTargetColor_Mode);
+ SkAutoTUnref<SkAvoidXfermode> mode(SkAvoidXfermode::Create(SK_ColorWHITE, 0xFF,
+ SkAvoidXfermode::kTargetColor_Mode));
SkPaint paint;
x += SkIntToScalar(20);
SkRect r = { x, 0, x + SkIntToScalar(360), SkIntToScalar(700) };
- paint.setXfermode(&mode);
+ paint.setXfermode(mode);
paint.setColor(SK_ColorGREEN);
paint.setAntiAlias(true);
canvas->drawOval(r, paint);
diff --git a/samplecode/SampleAvoid.cpp b/samplecode/SampleAvoid.cpp
index 879481b..aa7ca0e 100644
--- a/samplecode/SampleAvoid.cpp
+++ b/samplecode/SampleAvoid.cpp
@@ -78,9 +78,9 @@
frameP.setStyle(SkPaint::kStroke_Style);
for (size_t i = 0; i < SK_ARRAY_COUNT(gData); i++) {
- SkAvoidXfermode mode(SK_ColorGREEN, gData[i].fTolerance,
- gData[i].fMode);
- paint.setXfermode(&mode);
+ SkAutoTUnref<SkAvoidXfermode> mode(SkAvoidXfermode::Create(
+ SK_ColorGREEN, gData[i].fTolerance, gData[i].fMode));
+ paint.setXfermode(mode);
int div = 3;
SkRect rr = { 0, 0, r.width()/div, r.height()/div };
rr.offset(r.width()/4 - rr.width()/2, r.height()/4 - rr.height()/2);
diff --git a/samplecode/SamplePathEffects.cpp b/samplecode/SamplePathEffects.cpp
index ac77f21..2a1a49b 100644
--- a/samplecode/SamplePathEffects.cpp
+++ b/samplecode/SamplePathEffects.cpp
@@ -29,7 +29,7 @@
static SkPathEffect* make_pe(int flags) {
if (flags == 1)
- return new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
+ return SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
SkPath path;
path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
@@ -38,14 +38,14 @@
path.close();
path.offset(SkIntToScalar(-6), 0);
- SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kRotate_Style);
+ SkPathEffect* outer = SkPath1DPathEffect::Create(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kRotate_Style);
if (flags == 2)
return outer;
- SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
+ SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
- SkPathEffect* pe = new SkComposePathEffect(outer, inner);
+ SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
outer->unref();
inner->unref();
return pe;
@@ -59,10 +59,11 @@
path.close();
path.offset(SkIntToScalar(-6), 0);
- SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style);
- SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
+ SkPathEffect* outer = SkPath1DPathEffect::Create(
+ path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style);
+ SkPathEffect* inner = SkCornerPathEffect::Create(SkIntToScalar(CORNER_RADIUS));
- SkPathEffect* pe = new SkComposePathEffect(outer, inner);
+ SkPathEffect* pe = SkComposePathEffect::Create(outer, inner);
outer->unref();
inner->unref();
return pe;
diff --git a/samplecode/SamplePathUtils.cpp b/samplecode/SamplePathUtils.cpp
index 09ab773..503a6ea 100644
--- a/samplecode/SamplePathUtils.cpp
+++ b/samplecode/SamplePathUtils.cpp
@@ -65,14 +65,14 @@
virtual void onDrawContent(SkCanvas* canvas) {
SkScalar intervals[8] = { .5f, .3f, .5f, .3f, .5f, .3f, .5f, .3f };
- SkDashPathEffect dash(intervals, 2, fPhase);
- SkCornerPathEffect corner(.25f);
- SkComposePathEffect compose(&dash, &corner);
+ SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, fPhase));
+ SkAutoTUnref<SkCornerPathEffect> corner(SkCornerPathEffect::Create(.25f));
+ SkAutoTUnref<SkComposePathEffect> compose(SkComposePathEffect::Create(dash, corner));
SkPaint outlinePaint;
outlinePaint.setAntiAlias(true); // dashed paint for bitmap
outlinePaint.setStyle(SkPaint::kStroke_Style);
- outlinePaint.setPathEffect(&compose);
+ outlinePaint.setPathEffect(compose);
canvas->scale(10.0f, 10.0f); // scales up
diff --git a/samplecode/SampleSlides.cpp b/samplecode/SampleSlides.cpp
index 26cfc7d..914283a 100644
--- a/samplecode/SampleSlides.cpp
+++ b/samplecode/SampleSlides.cpp
@@ -31,10 +31,10 @@
static void compose_pe(SkPaint* paint) {
SkPathEffect* pe = paint->getPathEffect();
- SkPathEffect* corner = new SkCornerPathEffect(25);
+ SkPathEffect* corner = SkCornerPathEffect::Create(25);
SkPathEffect* compose;
if (pe) {
- compose = new SkComposePathEffect(pe, corner);
+ compose = SkComposePathEffect::Create(pe, corner);
corner->unref();
} else {
compose = corner;
@@ -59,8 +59,8 @@
static void dash_pe(SkPaint* paint) {
SkScalar inter[] = { 20, 10, 10, 10 };
paint->setStrokeWidth(12);
- paint->setPathEffect(new SkDashPathEffect(inter, SK_ARRAY_COUNT(inter),
- 0))->unref();
+ paint->setPathEffect(SkDashPathEffect::Create(inter, SK_ARRAY_COUNT(inter),
+ 0))->unref();
compose_pe(paint);
}
@@ -83,8 +83,8 @@
path.offset(SkIntToScalar(-6), 0);
scale(&path, 1.5f);
- paint->setPathEffect(new SkPath1DPathEffect(path, SkIntToScalar(21), 0,
- SkPath1DPathEffect::kRotate_Style))->unref();
+ paint->setPathEffect(SkPath1DPathEffect::Create(path, SkIntToScalar(21), 0,
+ SkPath1DPathEffect::kRotate_Style))->unref();
compose_pe(paint);
}
@@ -97,7 +97,7 @@
}
static void discrete_pe(SkPaint* paint) {
- paint->setPathEffect(new SkDiscretePathEffect(10, 4))->unref();
+ paint->setPathEffect(SkDiscretePathEffect::Create(10, 4))->unref();
}
static SkPathEffect* MakeTileEffect() {
@@ -107,7 +107,7 @@
SkPath path;
path.addCircle(0, 0, SkIntToScalar(5));
- return new SkPath2DPathEffect(m, path);
+ return SkPath2DPathEffect::Create(m, path);
}
static void tile_pe(SkPaint* paint) {
@@ -547,7 +547,7 @@
{
rast->addLayer(p);
- p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref();
+ p.setPathEffect(SkDiscretePathEffect::Create(SK_Scalar1*4, SK_Scalar1*3))->unref();
p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
rast->addLayer(p);
}
@@ -569,7 +569,7 @@
static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
SkPath path;
path.addCircle(0, 0, radius);
- return new SkPath2DPathEffect(matrix, path);
+ return SkPath2DPathEffect::Create(matrix, path);
}
static void r7(SkLayerRasterizer* rast, SkPaint& p)
@@ -606,7 +606,7 @@
SkMatrix lattice;
lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
lattice.postRotate(SkIntToScalar(30), 0, 0);
- p.setPathEffect(new SkLine2DPathEffect(SK_Scalar1*2, lattice))->unref();
+ p.setPathEffect(SkLine2DPathEffect::Create(SK_Scalar1*2, lattice))->unref();
p.setXfermodeMode(SkXfermode::kClear_Mode);
rast->addLayer(p);