move setshader to sk_sp, re-using SK_SUPPORT_LEGACY_CREATESHADER_PTR
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1829303002
Review URL: https://codereview.chromium.org/1829303002
diff --git a/src/animator/SkDrawPaint.cpp b/src/animator/SkDrawPaint.cpp
index 5f6efd8..fc1a413 100644
--- a/src/animator/SkDrawPaint.cpp
+++ b/src/animator/SkDrawPaint.cpp
@@ -237,7 +237,7 @@
if (shader == nullptr)
paint->setShader(nullptr);
else if (shader != (SkDrawShader*) -1)
- SkSafeUnref(paint->setShader(shader->getShader()));
+ paint->setShader(sk_ref_sp(shader->getShader()));
if (strikeThru != -1)
paint->setStrikeThruText(SkToBool(strikeThru));
if (strokeCap != -1)
diff --git a/src/c/sk_paint.cpp b/src/c/sk_paint.cpp
index dd0733f..b48e28f 100644
--- a/src/c/sk_paint.cpp
+++ b/src/c/sk_paint.cpp
@@ -6,6 +6,7 @@
*/
#include "SkPaint.h"
+#include "SkShader.h"
#include "sk_paint.h"
#include "sk_types_priv.h"
@@ -63,7 +64,7 @@
}
void sk_paint_set_shader(sk_paint_t* cpaint, sk_shader_t* cshader) {
- AsPaint(cpaint)->setShader(AsShader(cshader));
+ AsPaint(cpaint)->setShader(sk_ref_sp(AsShader(cshader)));
}
void sk_paint_set_maskfilter(sk_paint_t* cpaint, sk_maskfilter_t* cfilter) {
diff --git a/src/core/SkBlitter.cpp b/src/core/SkBlitter.cpp
index 1556216..d8e81d5 100644
--- a/src/core/SkBlitter.cpp
+++ b/src/core/SkBlitter.cpp
@@ -809,16 +809,16 @@
SkShader* shader = origPaint.getShader();
SkColorFilter* cf = origPaint.getColorFilter();
SkXfermode* mode = origPaint.getXfermode();
- Sk3DShader* shader3D = nullptr;
+ sk_sp<Sk3DShader> shader3D;
SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
if (origPaint.getMaskFilter() != nullptr &&
origPaint.getMaskFilter()->getFormat() == SkMask::k3D_Format) {
- shader3D = new Sk3DShader(sk_sp<SkShader>(SkSafeRef(shader)));
+ shader3D = sk_make_sp<Sk3DShader>(sk_ref_sp(shader));
// we know we haven't initialized lazyPaint yet, so just do it
- paint.writable()->setShader(shader3D)->unref();
- shader = shader3D;
+ paint.writable()->setShader(shader3D);
+ shader = shader3D.get();
}
if (mode) {
@@ -843,7 +843,8 @@
*/
if (SkXfermode::IsMode(mode, SkXfermode::kClear_Mode)) {
SkPaint* p = paint.writable();
- shader = p->setShader(nullptr);
+ p->setShader(nullptr);
+ shader = nullptr;
p->setColorFilter(nullptr);
cf = nullptr;
mode = p->setXfermodeMode(SkXfermode::kSrc_Mode);
@@ -853,9 +854,9 @@
if (nullptr == shader) {
if (mode) {
// xfermodes (and filters) require shaders for our current blitters
- shader = new SkColorShader(paint->getColor());
- paint.writable()->setShader(shader)->unref();
+ paint.writable()->setShader(SkShader::MakeColorShader(paint->getColor()));
paint.writable()->setAlpha(0xFF);
+ shader = paint->getShader();
} else if (cf) {
// if no shader && no xfermode, we just apply the colorfilter to
// our color and move on.
diff --git a/src/core/SkLightingShader.cpp b/src/core/SkLightingShader.cpp
index 02f75f3..d8e88c9 100644
--- a/src/core/SkLightingShader.cpp
+++ b/src/core/SkLightingShader.cpp
@@ -700,10 +700,10 @@
return bm.width() > kMaxSize || bm.height() > kMaxSize;
}
-SkShader* SkLightingShader::Create(const SkBitmap& diffuse, const SkBitmap& normal,
- const Lights* lights,
- const SkVector& invNormRotation,
- const SkMatrix* diffLocalM, const SkMatrix* normLocalM) {
+sk_sp<SkShader> SkLightingShader::Make(const SkBitmap& diffuse, const SkBitmap& normal,
+ const Lights* lights,
+ const SkVector& invNormRotation,
+ const SkMatrix* diffLocalM, const SkMatrix* normLocalM) {
if (diffuse.isNull() || bitmap_is_too_big(diffuse) ||
normal.isNull() || bitmap_is_too_big(normal) ||
diffuse.width() != normal.width() ||
@@ -713,8 +713,8 @@
SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1));
- return new SkLightingShaderImpl(diffuse, normal, lights, invNormRotation, diffLocalM,
- normLocalM);
+ return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, lights, invNormRotation, diffLocalM,
+ normLocalM);
}
///////////////////////////////////////////////////////////////////////////////
diff --git a/src/core/SkLightingShader.h b/src/core/SkLightingShader.h
index e918f3b..30ce655 100644
--- a/src/core/SkLightingShader.h
+++ b/src/core/SkLightingShader.h
@@ -1,4 +1,3 @@
-
/*
* Copyright 2015 Google Inc.
*
@@ -6,7 +5,6 @@
* found in the LICENSE file.
*/
-
#ifndef SkLightingShader_DEFINED
#define SkLightingShader_DEFINED
@@ -90,9 +88,9 @@
The +Z axis is thus encoded in RGB as (127, 127, 255) while the -Z axis is
(127, 127, 0).
*/
- static SkShader* Create(const SkBitmap& diffuse, const SkBitmap& normal,
- const Lights* lights, const SkVector& invNormRotation,
- const SkMatrix* diffLocalMatrix, const SkMatrix* normLocalMatrix);
+ static sk_sp<SkShader> Make(const SkBitmap& diffuse, const SkBitmap& normal,
+ const Lights* lights, const SkVector& invNormRotation,
+ const SkMatrix* diffLocalMatrix, const SkMatrix* normLocalMatrix);
SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
};
diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp
index 2ced3f6..6a54b9a 100644
--- a/src/core/SkPaint.cpp
+++ b/src/core/SkPaint.cpp
@@ -374,7 +374,9 @@
SET_PTR(Rasterizer)
#endif
SET_PTR(ImageFilter)
+#ifdef SK_SUPPORT_LEGACY_CREATESHADER_PTR
SET_PTR(Shader)
+#endif
#ifdef SK_SUPPORT_LEGACY_COLORFILTER_PTR
SET_PTR(ColorFilter)
#endif
diff --git a/src/effects/SkLayerDrawLooper.cpp b/src/effects/SkLayerDrawLooper.cpp
index 8510767..526fb4d 100644
--- a/src/effects/SkLayerDrawLooper.cpp
+++ b/src/effects/SkLayerDrawLooper.cpp
@@ -99,7 +99,7 @@
dst->setMaskFilter(src.getMaskFilter());
}
if (bits & kShader_Bit) {
- dst->setShader(src.getShader());
+ dst->setShader(sk_ref_sp(src.getShader()));
}
if (bits & kColorFilter_Bit) {
dst->setColorFilter(sk_ref_sp(src.getColorFilter()));
diff --git a/src/effects/SkPerlinNoiseShader.cpp b/src/effects/SkPerlinNoiseShader.cpp
index ef52875..72b7e02 100644
--- a/src/effects/SkPerlinNoiseShader.cpp
+++ b/src/effects/SkPerlinNoiseShader.cpp
@@ -249,18 +249,21 @@
#endif
};
-SkShader* SkPerlinNoiseShader::CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
- int numOctaves, SkScalar seed,
- const SkISize* tileSize) {
- return new SkPerlinNoiseShader(kFractalNoise_Type, baseFrequencyX, baseFrequencyY, numOctaves,
- seed, tileSize);
+sk_sp<SkShader> SkPerlinNoiseShader::MakeFractalNoise(SkScalar baseFrequencyX,
+ SkScalar baseFrequencyY,
+ int numOctaves, SkScalar seed,
+ const SkISize* tileSize) {
+ return sk_sp<SkShader>(new SkPerlinNoiseShader(kFractalNoise_Type, baseFrequencyX,
+ baseFrequencyY, numOctaves,
+ seed, tileSize));
}
-SkShader* SkPerlinNoiseShader::CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
- int numOctaves, SkScalar seed,
- const SkISize* tileSize) {
- return new SkPerlinNoiseShader(kTurbulence_Type, baseFrequencyX, baseFrequencyY, numOctaves,
- seed, tileSize);
+sk_sp<SkShader> SkPerlinNoiseShader::MakeTurbulence(SkScalar baseFrequencyX,
+ SkScalar baseFrequencyY,
+ int numOctaves, SkScalar seed,
+ const SkISize* tileSize) {
+ return sk_sp<SkShader>(new SkPerlinNoiseShader(kTurbulence_Type, baseFrequencyX, baseFrequencyY,
+ numOctaves, seed, tileSize));
}
SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type,
@@ -295,9 +298,11 @@
switch (type) {
case kFractalNoise_Type:
- return SkPerlinNoiseShader::CreateFractalNoise(freqX, freqY, octaves, seed, &tileSize);
+ return SkPerlinNoiseShader::MakeFractalNoise(freqX, freqY, octaves, seed,
+ &tileSize).release();
case kTurbulence_Type:
- return SkPerlinNoiseShader::CreateTubulence(freqX, freqY, octaves, seed, &tileSize);
+ return SkPerlinNoiseShader::MakeTurbulence(freqX, freqY, octaves, seed,
+ &tileSize).release();
default:
return nullptr;
}
@@ -580,11 +585,11 @@
SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f,
0.99f);
- SkAutoTUnref<SkShader> shader(d->fRandom->nextBool() ?
- SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
- stitchTiles ? &tileSize : nullptr) :
- SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
- stitchTiles ? &tileSize : nullptr));
+ sk_sp<SkShader> shader(d->fRandom->nextBool() ?
+ SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
+ stitchTiles ? &tileSize : nullptr) :
+ SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
+ stitchTiles ? &tileSize : nullptr));
return shader->asFragmentProcessor(d->fContext,
GrTest::TestMatrix(d->fRandom), nullptr,
diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp
index 0054143..b6a5b51 100644
--- a/src/pdf/SkPDFDevice.cpp
+++ b/src/pdf/SkPDFDevice.cpp
@@ -1983,7 +1983,7 @@
// PDF treats a shader as a color, so we only set one or the other.
sk_sp<SkPDFObject> pdfShader;
- const SkShader* shader = paint.getShader();
+ SkShader* shader = paint.getShader();
SkColor color = paint.getColor();
if (shader) {
// PDF positions patterns relative to the initial transform, so
@@ -2005,7 +2005,7 @@
SkScalar rasterScale =
SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE;
pdfShader.reset(SkPDFShader::GetPDFShader(
- fDocument, fRasterDpi, *shader, transform, bounds, rasterScale));
+ fDocument, fRasterDpi, shader, transform, bounds, rasterScale));
if (pdfShader.get()) {
// pdfShader has been canonicalized so we can directly compare
diff --git a/src/pdf/SkPDFShader.cpp b/src/pdf/SkPDFShader.cpp
index 7fb1f89..eaac849 100644
--- a/src/pdf/SkPDFShader.cpp
+++ b/src/pdf/SkPDFShader.cpp
@@ -434,7 +434,7 @@
uint32_t fPixelGeneration;
SkShader::TileMode fImageTileModes[2];
- State(const SkShader& shader, const SkMatrix& canvasTransform,
+ State(SkShader* shader, const SkMatrix& canvasTransform,
const SkIRect& bbox, SkScalar rasterScale);
bool operator==(const State& b) const;
@@ -515,7 +515,7 @@
// static
SkPDFObject* SkPDFShader::GetPDFShader(SkPDFDocument* doc,
SkScalar dpi,
- const SkShader& shader,
+ SkShader* shader,
const SkMatrix& matrix,
const SkIRect& surfaceBBox,
SkScalar rasterScale) {
@@ -1091,7 +1091,7 @@
return true;
}
-SkPDFShader::State::State(const SkShader& shader, const SkMatrix& canvasTransform,
+SkPDFShader::State::State(SkShader* shader, const SkMatrix& canvasTransform,
const SkIRect& bbox, SkScalar rasterScale)
: fCanvasTransform(canvasTransform),
fBBox(bbox),
@@ -1099,14 +1099,14 @@
fInfo.fColorCount = 0;
fInfo.fColors = nullptr;
fInfo.fColorOffsets = nullptr;
- fShaderTransform = shader.getLocalMatrix();
+ fShaderTransform = shader->getLocalMatrix();
fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode;
- fType = shader.asAGradient(&fInfo);
+ fType = shader->asAGradient(&fInfo);
if (fType == SkShader::kNone_GradientType) {
SkMatrix matrix;
- if (shader.isABitmap(&fImage, &matrix, fImageTileModes)) {
+ if (shader->isABitmap(&fImage, &matrix, fImageTileModes)) {
SkASSERT(matrix.isIdentity());
} else {
// Generic fallback for unsupported shaders:
@@ -1139,7 +1139,7 @@
fImage.eraseColor(SK_ColorTRANSPARENT);
SkPaint p;
- p.setShader(const_cast<SkShader*>(&shader));
+ p.setShader(sk_ref_sp(shader));
SkCanvas canvas(fImage);
canvas.scale(scale.width(), scale.height());
@@ -1152,7 +1152,7 @@
fPixelGeneration = fImage.getGenerationID();
} else {
AllocateGradientInfoStorage();
- shader.asAGradient(&fInfo);
+ shader->asAGradient(&fInfo);
}
}
diff --git a/src/pdf/SkPDFShader.h b/src/pdf/SkPDFShader.h
index a646aef..999dc02 100644
--- a/src/pdf/SkPDFShader.h
+++ b/src/pdf/SkPDFShader.h
@@ -45,7 +45,7 @@
*/
static SkPDFObject* GetPDFShader(SkPDFDocument* doc,
SkScalar dpi,
- const SkShader& shader,
+ SkShader* shader,
const SkMatrix& matrix,
const SkIRect& surfaceBBox,
SkScalar rasterScale);
diff --git a/src/utils/SkLua.cpp b/src/utils/SkLua.cpp
index c25bbf4..01bf507 100644
--- a/src/utils/SkLua.cpp
+++ b/src/utils/SkLua.cpp
@@ -1146,7 +1146,7 @@
static int lpaint_setShader(lua_State* L) {
SkPaint* paint = get_obj<SkPaint>(L, 1);
- paint->setShader(get_ref<SkShader>(L, 2));
+ paint->setShader(sk_ref_sp(get_ref<SkShader>(L, 2)));
return 0;
}