From c6f28f7f0e788dc43cad4494e277e9ad7b958b6e Mon Sep 17 00:00:00 2001 From: reed Date: Mon, 14 Mar 2016 12:22:10 -0700 Subject: [PATCH] Reland of "Finish conversion to sk_sp (patchset #2 id:20001 of https://codereview.chromium.org/1803763002/ )" This reverts commit 106e10ddff5d473dc81dd1ce8ade615585ea609b. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1803783002 CQ_EXTRA_TRYBOTS=client.skia.compile:Build-Ubuntu-GCC-x86_64-Release-CMake-Trybot,Build-Mac-Clang-x86_64-Release-CMake-Trybot TBR= Review URL: https://codereview.chromium.org/1803783002 --- bench/AlternatingColorPatternBench.cpp | 21 +++---- bench/ColorCubeBench.cpp | 8 +-- bench/GameBench.cpp | 6 +- bench/GradientBench.cpp | 88 ++++++++++++++--------------- bench/ImageFilterCollapse.cpp | 6 +- bench/PatchBench.cpp | 9 ++- bench/PatchGridBench.cpp | 9 ++- bench/RectBench.cpp | 6 +- bench/RepeatTileBench.cpp | 7 +-- bench/TileBench.cpp | 3 +- cmake/example.cpp | 5 +- example/HelloWorld.cpp | 16 ++---- gyp/skia_for_android_framework_defines.gypi | 1 + include/core/SkShader.h | 2 - samplecode/SampleClamp.cpp | 11 +--- 15 files changed, 81 insertions(+), 117 deletions(-) diff --git a/bench/AlternatingColorPatternBench.cpp b/bench/AlternatingColorPatternBench.cpp index b7285d6..147dc36 100644 --- a/bench/AlternatingColorPatternBench.cpp +++ b/bench/AlternatingColorPatternBench.cpp @@ -51,11 +51,11 @@ static void makebm(SkBitmap* bm, int w, int h) { SkPaint paint; - paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos, - SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode))->unref(); + paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos, SK_ARRAY_COUNT(kColors0), + SkShader::kClamp_TileMode)); canvas.drawPaint(paint); - paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos, - SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref(); + paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos, SK_ARRAY_COUNT(kColors1), + SkShader::kClamp_TileMode)); canvas.drawPaint(paint); } @@ -80,12 +80,12 @@ public: NY = 5, NUM_DRAWS = NX * NY, }; - SkShader* fBmShader; + sk_sp fBmShader; SkPath fPaths[NUM_DRAWS]; SkRect fRects[NUM_DRAWS]; SkColor fColors[NUM_DRAWS]; - SkShader* fShaders[NUM_DRAWS]; + sk_sp fShaders[NUM_DRAWS]; SkString fName; ColorPatternData fPattern1; @@ -94,8 +94,7 @@ public: SkBitmap fBmp; - AlternatingColorPatternBench(ColorPattern pattern1, ColorPattern pattern2, DrawType drawType) - : fBmShader(nullptr) { + AlternatingColorPatternBench(ColorPattern pattern1, ColorPattern pattern2, DrawType drawType) { fPattern1 = gColorPatterns[pattern1]; fPattern2 = gColorPatterns[pattern2]; fName.printf("colorPattern_%s_%s_%s", @@ -104,10 +103,6 @@ public: fDrawType = drawType; } - virtual ~AlternatingColorPatternBench() { - SkSafeUnref(fBmShader); - } - protected: const char* onGetName() override { return fName.c_str(); @@ -117,7 +112,7 @@ protected: int w = 40; int h = 40; makebm(&fBmp, w, h); - fBmShader = SkShader::CreateBitmapShader(fBmp, + fBmShader = SkShader::MakeBitmapShader(fBmp, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); int offset = 2; diff --git a/bench/ColorCubeBench.cpp b/bench/ColorCubeBench.cpp index 6c5a16d..9f6bd00 100644 --- a/bench/ColorCubeBench.cpp +++ b/bench/ColorCubeBench.cpp @@ -48,13 +48,13 @@ protected: } private: - static SkShader* MakeLinear(const SkISize& size) { + static sk_sp MakeLinear(const SkISize& size) { const SkPoint pts[2] = { { 0, 0 }, { SkIntToScalar(size.width()), SkIntToScalar(size.height()) } }; static const SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE }; - return SkGradientShader::CreateLinear( + return SkGradientShader::MakeLinear( pts, colors, nullptr, 2, SkShader::kRepeat_TileMode, 0, &SkMatrix::I()); } @@ -64,11 +64,9 @@ private: canvas.clear(0x00000000); SkPaint paint; paint.setAntiAlias(true); - SkShader* shader = MakeLinear(fSize); - paint.setShader(shader); + paint.setShader(MakeLinear(fSize)); SkRect r = { 0, 0, SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height()) }; canvas.drawRect(r, paint); - shader->unref(); } void makeCubeData() { diff --git a/bench/GameBench.cpp b/bench/GameBench.cpp index 823f5b0..84da73e 100644 --- a/bench/GameBench.cpp +++ b/bench/GameBench.cpp @@ -139,9 +139,9 @@ protected: SkPaint p2; // for drawVertices path p2.setColor(0xFF000000); p2.setFilterQuality(kLow_SkFilterQuality); - p2.setShader(SkShader::CreateBitmapShader(fAtlas, - SkShader::kClamp_TileMode, - SkShader::kClamp_TileMode))->unref(); + p2.setShader(SkShader::MakeBitmapShader(fAtlas, + SkShader::kClamp_TileMode, + SkShader::kClamp_TileMode)); for (int i = 0; i < loops; ++i, ++fNumSaved) { if (0 == i % kNumBeforeClear) { diff --git a/bench/GradientBench.cpp b/bench/GradientBench.cpp index 73014f7..21ebdce 100644 --- a/bench/GradientBench.cpp +++ b/bench/GradientBench.cpp @@ -47,89 +47,87 @@ static const GradData gGradData[] = { }; /// Ignores scale -static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, - SkShader::TileMode tm, float scale, bool force4f) { +static sk_sp MakeLinear(const SkPoint pts[2], const GradData& data, + SkShader::TileMode tm, float scale, bool force4f) { const uint32_t flags = force4f ? SkLinearGradient::kForce4fContext_PrivateFlag : 0; - return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, - data.fCount, tm, flags, nullptr); + return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, + data.fCount, tm, flags, nullptr); } -static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, - SkShader::TileMode tm, float scale, bool force4f) { +static sk_sp MakeRadial(const SkPoint pts[2], const GradData& data, + SkShader::TileMode tm, float scale, bool force4f) { SkPoint center; center.set(SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY)); - return SkGradientShader::CreateRadial(center, center.fX * scale, - data.fColors, - data.fPos, data.fCount, tm); + return SkGradientShader::MakeRadial(center, center.fX * scale, data.fColors, + data.fPos, data.fCount, tm); } /// Ignores scale -static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, - SkShader::TileMode tm, float scale, bool force4f) { +static sk_sp MakeSweep(const SkPoint pts[2], const GradData& data, + SkShader::TileMode tm, float scale, bool force4f) { SkPoint center; center.set(SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY)); - return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, - data.fPos, data.fCount); + return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount); } /// Ignores scale -static SkShader* MakeConical(const SkPoint pts[2], const GradData& data, - SkShader::TileMode tm, float scale, bool force4f) { +static sk_sp MakeConical(const SkPoint pts[2], const GradData& data, + SkShader::TileMode tm, float scale, bool force4f) { 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::CreateTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, - center0, (pts[1].fX - pts[0].fX) / 2, - data.fColors, data.fPos, data.fCount, tm); + 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); } /// Ignores scale -static SkShader* MakeConicalZeroRad(const SkPoint pts[2], const GradData& data, - SkShader::TileMode tm, float scale, bool force4f) { +static sk_sp MakeConicalZeroRad(const SkPoint pts[2], const GradData& data, + SkShader::TileMode tm, float scale, bool force4f) { 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::CreateTwoPointConical(center1, 0.0, - center0, (pts[1].fX - pts[0].fX) / 2, - data.fColors, data.fPos, data.fCount, tm); + return SkGradientShader::MakeTwoPointConical(center1, 0.0, + center0, (pts[1].fX - pts[0].fX) / 2, + data.fColors, data.fPos, data.fCount, tm); } /// Ignores scale -static SkShader* MakeConicalOutside(const SkPoint pts[2], const GradData& data, - SkShader::TileMode tm, float scale, bool force4f) { +static sk_sp MakeConicalOutside(const SkPoint pts[2], const GradData& data, + SkShader::TileMode tm, float scale, bool force4f) { 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::CreateTwoPointConical(center0, radius0, - center1, radius1, - data.fColors, data.fPos, - data.fCount, tm); + return SkGradientShader::MakeTwoPointConical(center0, radius0, + center1, radius1, + data.fColors, data.fPos, + data.fCount, tm); } /// Ignores scale -static SkShader* MakeConicalOutsideZeroRad(const SkPoint pts[2], const GradData& data, - SkShader::TileMode tm, float scale, bool force4f) { +static sk_sp MakeConicalOutsideZeroRad(const SkPoint pts[2], const GradData& data, + SkShader::TileMode tm, float scale, bool force4f) { 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::CreateTwoPointConical(center0, 0.0, - center1, radius1, - data.fColors, data.fPos, - data.fCount, tm); + return SkGradientShader::MakeTwoPointConical(center0, 0.0, + center1, radius1, + data.fColors, data.fPos, + data.fCount, tm); } -typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, - SkShader::TileMode tm, float scale, bool force4f); +typedef sk_sp (*GradMaker)(const SkPoint pts[2], const GradData& data, + SkShader::TileMode tm, float scale, bool force4f); static const struct { GradMaker fMaker; @@ -213,9 +211,8 @@ public: fName.append("_4f"); } - SkAutoTUnref shader(MakeShader(gradType, data, tm, scale, force4f)); this->setupPaint(&fPaint); - fPaint.setShader(shader); + fPaint.setShader(MakeShader(gradType, data, tm, scale, force4f)); } GradientBench(GradType gradType, GradData data, bool dither, bool force4f = false) @@ -229,10 +226,8 @@ public: fName.appendf("_dither"); } - SkAutoTUnref shader( - MakeShader(gradType, data, SkShader::kClamp_TileMode, 1.0f, force4f)); this->setupPaint(&fPaint); - fPaint.setShader(shader); + fPaint.setShader(MakeShader(gradType, data, SkShader::kClamp_TileMode, 1.0f, force4f)); fPaint.setDither(dither); } @@ -263,8 +258,8 @@ protected: private: typedef Benchmark INHERITED; - SkShader* MakeShader(GradType gradType, GradData data, - SkShader::TileMode tm, float scale, bool force4f) { + sk_sp MakeShader(GradType gradType, GradData data, + SkShader::TileMode tm, float scale, bool force4f) { const SkPoint pts[2] = { { 0, 0 }, { SkIntToScalar(kSize), SkIntToScalar(kSize) } @@ -383,10 +378,9 @@ protected: SK_ColorBLACK, SkColorSetARGB(alpha, gray, gray, gray), SK_ColorWHITE }; - SkShader* s = SkGradientShader::CreateLinear(pts, colors, nullptr, + paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), - SkShader::kClamp_TileMode); - paint.setShader(s)->unref(); + SkShader::kClamp_TileMode)); canvas->drawRect(r, paint); } } diff --git a/bench/ImageFilterCollapse.cpp b/bench/ImageFilterCollapse.cpp index 2a2fa3a..bd3f80f 100644 --- a/bench/ImageFilterCollapse.cpp +++ b/bench/ImageFilterCollapse.cpp @@ -66,10 +66,8 @@ private: SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN, SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE }; - SkAutoTUnref shader(SkGradientShader::CreateLinear( - pts, colors, nullptr, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode - )); - paint.setShader(shader); + paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), + SkShader::kClamp_TileMode)); canvas.drawPaint(paint); } }; diff --git a/bench/PatchBench.cpp b/bench/PatchBench.cpp index 6f51b45..5d7451a 100644 --- a/bench/PatchBench.cpp +++ b/bench/PatchBench.cpp @@ -69,16 +69,15 @@ public: } // override this method to change the shader - virtual SkShader* createShader() { + virtual sk_sp createShader() { const SkColor colors[] = { SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW, }; const SkPoint pts[] = { { 200.f / 4.f, 0.f }, { 3.f * 200.f / 4, 200.f } }; - return SkGradientShader::CreateLinear(pts, colors, nullptr, - SK_ARRAY_COUNT(colors), - SkShader::kMirror_TileMode); + return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), + SkShader::kMirror_TileMode); } protected: @@ -115,7 +114,7 @@ protected: switch (fVertexMode) { case kTexCoords_VertexMode: case kBoth_VertexMode: - fPaint.setShader(this->createShader())->unref(); + fPaint.setShader(this->createShader()); break; default: fPaint.setShader(nullptr); diff --git a/bench/PatchGridBench.cpp b/bench/PatchGridBench.cpp index ef78532..83e2932 100644 --- a/bench/PatchGridBench.cpp +++ b/bench/PatchGridBench.cpp @@ -150,16 +150,15 @@ public: } // override this method to change the shader - SkShader* createShader() { + sk_sp createShader() { const SkColor colors[] = { SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW, }; const SkPoint pts[] = { { 200.f / 4.f, 0.f }, { 3.f * 200.f / 4, 200.f } }; - return SkGradientShader::CreateLinear(pts, colors, nullptr, - SK_ARRAY_COUNT(colors), - SkShader::kMirror_TileMode); + return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), + SkShader::kMirror_TileMode); } protected: @@ -205,7 +204,7 @@ protected: switch (fVertexMode) { case kTexCoords_VertexMode: case kBoth_VertexMode: - fPaint.setShader(createShader())->unref(); + fPaint.setShader(createShader()); break; default: fPaint.setShader(nullptr); diff --git a/bench/RectBench.cpp b/bench/RectBench.cpp index a700b36..5bd5640 100644 --- a/bench/RectBench.cpp +++ b/bench/RectBench.cpp @@ -238,10 +238,8 @@ protected: srcBM.allocN32Pixels(10, 1); srcBM.eraseColor(0xFF00FF00); - SkShader* s; - s = SkShader::CreateBitmapShader(srcBM, SkShader::kClamp_TileMode, - SkShader::kClamp_TileMode); - paint.setShader(s)->unref(); + paint.setShader(SkShader::MakeBitmapShader(srcBM, SkShader::kClamp_TileMode, + SkShader::kClamp_TileMode)); } for (int loop = 0; loop < loops; loop++) { for (size_t i = 0; i < sizes; i++) { diff --git a/bench/RepeatTileBench.cpp b/bench/RepeatTileBench.cpp index 39830bf..452c2c0 100644 --- a/bench/RepeatTileBench.cpp +++ b/bench/RepeatTileBench.cpp @@ -120,10 +120,9 @@ protected: fBitmap = tmp; } - SkShader* s = SkShader::CreateBitmapShader(fBitmap, - SkShader::kRepeat_TileMode, - SkShader::kRepeat_TileMode); - fPaint.setShader(s)->unref(); + fPaint.setShader(SkShader::MakeBitmapShader(fBitmap, + SkShader::kRepeat_TileMode, + SkShader::kRepeat_TileMode)); } diff --git a/bench/TileBench.cpp b/bench/TileBench.cpp index 6b91ada..47e79c7 100644 --- a/bench/TileBench.cpp +++ b/bench/TileBench.cpp @@ -53,8 +53,7 @@ public: create_gradient(&bm); - SkShader* s = SkShader::CreateBitmapShader(bm, xTile, yTile); - fPaint.setShader(s)->unref(); + fPaint.setShader(SkShader::MakeBitmapShader(bm, xTile, yTile)); fName.printf("constXTile_"); diff --git a/cmake/example.cpp b/cmake/example.cpp index 247deaf..93611d7 100644 --- a/cmake/example.cpp +++ b/cmake/example.cpp @@ -67,14 +67,11 @@ int main(int, char**) { // Create a left-to-right green-to-purple gradient shader. SkPoint pts[] = { {0,0}, {320,240} }; SkColor colors[] = { 0xFF00FF00, 0xFFFF00FF }; - std::shared_ptr shader = adopt( - SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kRepeat_TileMode)); - // Our text will draw with this paint: size 24, antialiased, with the shader. SkPaint paint; paint.setTextSize(24); paint.setAntiAlias(true); - paint.setShader(shader.get()); + paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kRepeat_TileMode)); // Draw to the surface via its SkCanvas. SkCanvas* canvas = surface->getCanvas(); // We don't manage this pointer's lifetime. diff --git a/example/HelloWorld.cpp b/example/HelloWorld.cpp index e13cb3b..e30c467 100644 --- a/example/HelloWorld.cpp +++ b/example/HelloWorld.cpp @@ -93,10 +93,7 @@ void HelloWorldWindow::drawContents(SkCanvas* canvas) { paint.setColor(SK_ColorRED); // Draw a rectangle with red paint - SkRect rect = { - 10, 10, - 128, 128 - }; + SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128); canvas->drawRect(rect, paint); // Set up a linear gradient and draw a circle @@ -107,18 +104,15 @@ void HelloWorldWindow::drawContents(SkCanvas* canvas) { }; SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK}; - SkShader* shader = SkGradientShader::CreateLinear( - linearPoints, linearColors, NULL, 2, - SkShader::kMirror_TileMode); - SkAutoUnref shader_deleter(shader); - - paint.setShader(shader); + paint.setShader(SkGradientShader::MakeLinear( + linearPoints, linearColors, nullptr, 2, + SkShader::kMirror_TileMode)); paint.setFlags(SkPaint::kAntiAlias_Flag); canvas->drawCircle(200, 200, 64, paint); // Detach shader - paint.setShader(NULL); + paint.setShader(nullptr); } // Draw a message with a nice black paint. diff --git a/gyp/skia_for_android_framework_defines.gypi b/gyp/skia_for_android_framework_defines.gypi index 8e7fff4..10995f4 100644 --- a/gyp/skia_for_android_framework_defines.gypi +++ b/gyp/skia_for_android_framework_defines.gypi @@ -19,6 +19,7 @@ 'SK_IGNORE_LINEONLY_AA_CONVEX_PATH_OPTS', 'SK_SUPPORT_LEGACY_GRADIENT_DITHERING', 'SK_SUPPORT_LEGACY_DRAWFILTER', + 'SK_SUPPORT_LEGACY_CREATESHADER_PTR', ], }, } diff --git a/include/core/SkShader.h b/include/core/SkShader.h index 158d526..8002f63 100644 --- a/include/core/SkShader.h +++ b/include/core/SkShader.h @@ -23,8 +23,6 @@ class SkXfermode; class GrContext; class GrFragmentProcessor; -#define SK_SUPPORT_LEGACY_CREATESHADER_PTR - /** \class SkShader * * Shaders specify the source color(s) for what is being drawn. If a paint diff --git a/samplecode/SampleClamp.cpp b/samplecode/SampleClamp.cpp index e319a56..9739053 100644 --- a/samplecode/SampleClamp.cpp +++ b/samplecode/SampleClamp.cpp @@ -13,25 +13,20 @@ #include "SkGradientShader.h" #include "SkPicture.h" -static SkShader* make_linear() { +static sk_sp make_linear() { SkPoint pts[] = { 0, 0, SK_Scalar1/500, SK_Scalar1/500 }; SkColor colors[] = { SK_ColorRED, SK_ColorBLUE }; - return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, - SkShader::kClamp_TileMode); + return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode); } class ClampView : public SampleView { - SkShader* fGrad; + sk_sp fGrad; public: ClampView() { fGrad = make_linear(); } - virtual ~ClampView() { - fGrad->unref(); - } - protected: // overrides from SkEventSink virtual bool onQuery(SkEvent* evt) { -- 2.7.4