Index: gm/gradients.cpp |
diff --git a/gm/gradients.cpp b/gm/gradients.cpp |
index 29d0f31e960fe54a52c38738f6d3d6ae502269e1..eeb5bcb9ff86ccba7e8116b6e501667b12bff702 100644 |
--- a/gm/gradients.cpp |
+++ b/gm/gradients.cpp |
@@ -40,57 +40,59 @@ |
{ 4, gColorClamp, gPosClamp } |
}; |
-static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, |
- SkShader::TileMode tm, const SkMatrix& localMatrix) { |
- return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm, 0, |
- &localMatrix); |
-} |
- |
-static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, |
- SkShader::TileMode tm, const SkMatrix& localMatrix) { |
+static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
+ SkShader::TileMode tm, const SkMatrix& localMatrix) { |
+ return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
+ data.fCount, tm, 0, &localMatrix); |
+} |
+ |
+static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
+ SkShader::TileMode tm, const SkMatrix& localMatrix) { |
SkPoint center; |
center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
SkScalarAve(pts[0].fY, pts[1].fY)); |
- return SkGradientShader::MakeRadial(center, center.fX, data.fColors, data.fPos, data.fCount, |
- tm, 0, &localMatrix); |
-} |
- |
-static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, |
- SkShader::TileMode, const SkMatrix& localMatrix) { |
+ return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
+ data.fPos, data.fCount, tm, 0, &localMatrix); |
+} |
+ |
+static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
+ SkShader::TileMode, const SkMatrix& localMatrix) { |
SkPoint center; |
center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
SkScalarAve(pts[0].fY, pts[1].fY)); |
- return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount, |
- 0, &localMatrix); |
-} |
- |
-static sk_sp<SkShader> Make2Radial(const SkPoint pts[2], const GradData& data, |
- SkShader::TileMode tm, const SkMatrix& localMatrix) { |
+ return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
+ data.fPos, data.fCount, 0, &localMatrix); |
+} |
+ |
+static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
+ SkShader::TileMode tm, const SkMatrix& localMatrix) { |
SkPoint center0, center1; |
center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
SkScalarAve(pts[0].fY, pts[1].fY)); |
center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
- return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, |
- center0, (pts[1].fX - pts[0].fX) / 2, |
- data.fColors, data.fPos, data.fCount, tm, |
- 0, &localMatrix); |
-} |
- |
-static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, |
+ return SkGradientShader::CreateTwoPointConical( |
+ center1, (pts[1].fX - pts[0].fX) / 7, |
+ center0, (pts[1].fX - pts[0].fX) / 2, |
+ data.fColors, data.fPos, data.fCount, tm, |
+ 0, &localMatrix); |
+} |
+ |
+static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, |
SkShader::TileMode tm, const SkMatrix& localMatrix) { |
SkPoint center0, center1; |
SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
- return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radius0, |
- data.fColors, data.fPos, |
- data.fCount, tm, 0, &localMatrix); |
-} |
- |
-typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, |
- SkShader::TileMode tm, const SkMatrix& localMatrix); |
+ return SkGradientShader::CreateTwoPointConical(center1, radius1, |
+ center0, radius0, |
+ data.fColors, data.fPos, |
+ data.fCount, tm, 0, &localMatrix); |
+} |
+ |
+typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
+ SkShader::TileMode tm, const SkMatrix& localMatrix); |
static const GradMaker gGradMakers[] = { |
MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical |
}; |
@@ -134,8 +136,11 @@ |
scale.postTranslate(25.f, 25.f); |
} |
- paint.setShader(gGradMakers[j](pts, gGradData[i], tm, scale)); |
+ SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, scale); |
+ |
+ paint.setShader(shader); |
canvas->drawRect(r, paint); |
+ shader->unref(); |
canvas->translate(0, SkIntToScalar(120)); |
} |
canvas->restore(); |
@@ -191,8 +196,11 @@ |
perspective.setPerspY(SkIntToScalar(i+1) / 500); |
perspective.setSkewX(SkIntToScalar(i+1) / 10); |
- paint.setShader(gGradMakers[j](pts, gGradData[i], tm, perspective)); |
+ SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, perspective); |
+ |
+ paint.setShader(shader); |
canvas->drawRect(r, paint); |
+ shader->unref(); |
canvas->translate(0, SkIntToScalar(120)); |
} |
canvas->restore(); |
@@ -279,11 +287,12 @@ |
SkPoint c1; |
c1.iset(0, 25); |
SkScalar r1 = SkIntToScalar(150); |
+ SkShader* s = SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, colors, |
+ pos, SK_ARRAY_COUNT(pos), |
+ SkShader::kClamp_TileMode); |
SkPaint paint; |
- paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors, |
- pos, SK_ARRAY_COUNT(pos), |
- SkShader::kClamp_TileMode)); |
paint.setDither(fDither); |
+ paint.setShader(s)->unref(); |
canvas->drawPaint(paint); |
} |
@@ -320,13 +329,13 @@ |
SkScalar r0 = 20; |
SkPoint c1 = { 200, 25 }; |
SkScalar r1 = 10; |
- |
+ SkShader* s = SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, colors, |
+ pos, SK_ARRAY_COUNT(pos), |
+ SkShader::kClamp_TileMode); |
SkPaint paint; |
paint.setColor(SK_ColorYELLOW); |
canvas->drawRect(SkRect::MakeWH(100, 150), paint); |
- paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors, pos, |
- SK_ARRAY_COUNT(pos), |
- SkShader::kClamp_TileMode)); |
+ paint.setShader(s)->unref(); |
canvas->drawRect(SkRect::MakeWH(100, 150), paint); |
} |
@@ -359,11 +368,13 @@ |
SkPoint center; |
center.iset(0, 300); |
canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
- paint.setShader(SkGradientShader::MakeRadial( |
+ SkShader* shader = SkGradientShader::CreateRadial( |
SkPoint(center), |
SkIntToScalar(200), gColors, nullptr, 5, |
- SkShader::kClamp_TileMode)); |
+ SkShader::kClamp_TileMode); |
+ paint.setShader(shader); |
canvas->drawRect(r, paint); |
+ shader->unref(); |
} |
private: |
@@ -402,9 +413,11 @@ |
const SkScalar pos[] = { 0.0f, |
0.35f, |
1.0f }; |
- paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, pos, |
- SK_ARRAY_COUNT(pos), |
- SkShader::kClamp_TileMode)); |
+ SkShader* shader = |
+ SkGradientShader::CreateRadial(center, radius, colors, |
+ pos, SK_ARRAY_COUNT(pos), |
+ SkShader::kClamp_TileMode); |
+ paint.setShader(shader)->unref(); |
SkRect r = { |
0, 0, SkIntToScalar(dim.width()), SkIntToScalar(dim.height()) |
}; |
@@ -451,19 +464,25 @@ |
const uint32_t flags[] = { 0, SkGradientShader::kInterpolateColorsInPremul_Flag }; |
for (size_t i = 0; i < SK_ARRAY_COUNT(flags); i++) { |
- paint1.setShader(SkGradientShader::MakeSweep(cx, cy, sweep_colors, |
- nullptr, SK_ARRAY_COUNT(sweep_colors), |
- flags[i], nullptr)); |
- paint2.setShader(SkGradientShader::MakeRadial(center, radius, colors1, |
- nullptr, SK_ARRAY_COUNT(colors1), |
- SkShader::kClamp_TileMode, |
- flags[i], nullptr)); |
- paint3.setShader(SkGradientShader::MakeRadial(center, radius, colors2, |
- nullptr, SK_ARRAY_COUNT(colors2), |
- SkShader::kClamp_TileMode, |
- flags[i], nullptr)); |
+ SkAutoTUnref<SkShader> sweep( |
+ SkGradientShader::CreateSweep(cx, cy, sweep_colors, |
+ nullptr, SK_ARRAY_COUNT(sweep_colors), |
+ flags[i], nullptr)); |
+ SkAutoTUnref<SkShader> radial1( |
+ SkGradientShader::CreateRadial(center, radius, colors1, |
+ nullptr, SK_ARRAY_COUNT(colors1), |
+ SkShader::kClamp_TileMode, |
+ flags[i], nullptr)); |
+ SkAutoTUnref<SkShader> radial2( |
+ SkGradientShader::CreateRadial(center, radius, colors2, |
+ nullptr, SK_ARRAY_COUNT(colors2), |
+ SkShader::kClamp_TileMode, |
+ flags[i], nullptr)); |
+ paint1.setShader(sweep); |
paint1.setDither(fDither); |
+ paint2.setShader(radial1); |
paint2.setDither(fDither); |
+ paint3.setShader(radial2); |
paint3.setDither(fDither); |
canvas->drawCircle(cx, cy, radius, paint1); |
@@ -500,8 +519,8 @@ |
const SkPoint center = { 0, 0 }; |
const SkScalar kRadius = 3000; |
const SkColor gColors[] = { 0xFFFFFFFF, 0xFF000000 }; |
- fShader = SkGradientShader::MakeRadial(center, kRadius, gColors, nullptr, 2, |
- SkShader::kClamp_TileMode); |
+ fShader.reset(SkGradientShader::CreateRadial(center, kRadius, gColors, nullptr, 2, |
+ SkShader::kClamp_TileMode)); |
} |
void onDraw(SkCanvas* canvas) override { |
@@ -512,7 +531,7 @@ |
} |
private: |
- sk_sp<SkShader> fShader; |
+ SkAutoTUnref<SkShader> fShader; |
bool fDither; |
typedef GM INHERITED; |
@@ -537,8 +556,8 @@ |
const SkColor colors[] = { SK_ColorRED, SK_ColorRED, SK_ColorWHITE, SK_ColorWHITE, |
SK_ColorRED }; |
const SkScalar pos[] = { 0, .4f, .4f, .8f, .8f, 1 }; |
- fShader = SkGradientShader::MakeRadial(center, kRadius, colors, pos, |
- SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode); |
+ fShader.reset(SkGradientShader::CreateRadial(center, kRadius, colors, pos, |
+ SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode)); |
} |
void onDraw(SkCanvas* canvas) override { |
@@ -550,7 +569,7 @@ |
} |
private: |
- sk_sp<SkShader> fShader; |
+ SkAutoTUnref<SkShader> fShader; |
bool fDither; |
typedef GM INHERITED; |
@@ -585,8 +604,8 @@ |
for (int inner = 0; inner < (int) SK_ARRAY_COUNT(unitPos); ++inner) { |
pos[inner] = unitPos[inner] / (kMinWidth + index * kWidthBump); |
} |
- fShader[index] = SkGradientShader::MakeLinear(pts, colors, pos, |
- SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode); |
+ fShader[index].reset(SkGradientShader::CreateLinear(pts, colors, pos, |
+ SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode)); |
} |
} |
@@ -602,7 +621,7 @@ |
} |
private: |
- sk_sp<SkShader> fShader[100]; |
+ SkAutoTUnref<SkShader> fShader[100]; |
bool fDither; |
typedef GM INHERITED; |
@@ -654,12 +673,13 @@ |
SkPaint paint; |
for (unsigned i = 0; i < SK_ARRAY_COUNT(configs); ++i) { |
SkAutoCanvasRestore acr(canvas, true); |
- paint.setShader(SkGradientShader::MakeLinear(configs[i].pts, colors, configs[i].pos, |
- kStopCount, SkShader::kClamp_TileMode, |
- fFlags, nullptr)); |
+ SkAutoTUnref<SkShader> gradient( |
+ SkGradientShader::CreateLinear(configs[i].pts, colors, configs[i].pos, kStopCount, |
+ SkShader::kClamp_TileMode, fFlags, nullptr)); |
canvas->translate(kRectSize * ((i % 4) * 1.5f + 0.25f), |
kRectSize * ((i / 4) * 1.5f + 0.25f)); |
+ paint.setShader(gradient); |
canvas->drawRect(SkRect::MakeWH(kRectSize, kRectSize), paint); |
} |
} |
@@ -684,27 +704,27 @@ |
#define SIZE 121 |
-static sk_sp<SkShader> make_linear(const GradRun& run, SkShader::TileMode mode) { |
+static SkShader* make_linear(const GradRun& run, SkShader::TileMode mode) { |
const SkPoint pts[] { { 30, 30 }, { SIZE - 30, SIZE - 30 } }; |
- return SkGradientShader::MakeLinear(pts, run.fColors, run.fPos, run.fCount, mode); |
-} |
- |
-static sk_sp<SkShader> make_radial(const GradRun& run, SkShader::TileMode mode) { |
+ return SkGradientShader::CreateLinear(pts, run.fColors, run.fPos, run.fCount, mode); |
+} |
+ |
+static SkShader* make_radial(const GradRun& run, SkShader::TileMode mode) { |
const SkScalar half = SIZE * 0.5f; |
- return SkGradientShader::MakeRadial({half,half}, half - 10, run.fColors, run.fPos, |
- run.fCount, mode); |
-} |
- |
-static sk_sp<SkShader> make_conical(const GradRun& run, SkShader::TileMode mode) { |
+ return SkGradientShader::CreateRadial({half,half}, half - 10, |
+ run.fColors, run.fPos, run.fCount, mode); |
+} |
+ |
+static SkShader* make_conical(const GradRun& run, SkShader::TileMode mode) { |
const SkScalar half = SIZE * 0.5f; |
const SkPoint center { half, half }; |
- return SkGradientShader::MakeTwoPointConical(center, 20, center, half - 10, |
- run.fColors, run.fPos, run.fCount, mode); |
-} |
- |
-static sk_sp<SkShader> make_sweep(const GradRun& run, SkShader::TileMode) { |
+ return SkGradientShader::CreateTwoPointConical(center, 20, center, half - 10, |
+ run.fColors, run.fPos, run.fCount, mode); |
+} |
+ |
+static SkShader* make_sweep(const GradRun& run, SkShader::TileMode) { |
const SkScalar half = SIZE * 0.5f; |
- return SkGradientShader::MakeSweep(half, half, run.fColors, run.fPos, run.fCount); |
+ return SkGradientShader::CreateSweep(half, half, run.fColors, run.fPos, run.fCount); |
} |
/* |
@@ -742,7 +762,7 @@ |
4, |
}, |
}; |
- sk_sp<SkShader> (*factories[])(const GradRun&, SkShader::TileMode) { |
+ SkShader* (*factories[])(const GradRun&, SkShader::TileMode) { |
make_linear, make_radial, make_conical, make_sweep |
}; |
@@ -757,7 +777,7 @@ |
canvas->translate(0, dy); |
SkAutoCanvasRestore acr(canvas, true); |
for (const auto& run : runs) { |
- paint.setShader(factory(run, mode)); |
+ paint.setShader(factory(run, mode))->unref(); |
canvas->drawRect(rect, paint); |
canvas->translate(dx, 0); |
} |
@@ -779,9 +799,11 @@ |
} |
} |
+ SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear( |
+ pts, colors, nullptr, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, flags, nullptr)); |
+ |
SkPaint p; |
- p.setShader(SkGradientShader::MakeLinear( |
- pts, colors, nullptr, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, flags, nullptr)); |
+ p.setShader(shader); |
canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p); |
} |