From 5a9a981edf54d203e06adab0909da03343a1c596 Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Tue, 1 Aug 2017 16:38:08 -0400 Subject: Tiling support for SkSweepGradient Expand the sweep gradient definition to include a color stop angular range ([0, 360] by default). Color stop positions in [0,1] are mapped to this range, and drawing outside is controlled by a tile mode param. This is closer to the CSS gradients spec and allows us to use fewer color stops in Blink conic gradients. Impl-wise, the remapping is effected after t calculation, and before tiling. Change-Id: I5d71be01d134404d6eb9d7e2a904ec636b39f855 Reviewed-on: https://skia-review.googlesource.com/27704 Commit-Queue: Florin Malita Reviewed-by: Brian Salomon Reviewed-by: Mike Klein --- gm/gradients.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'gm/gradients.cpp') diff --git a/gm/gradients.cpp b/gm/gradients.cpp index 9abd6f0e9a..85ac6906da 100644 --- a/gm/gradients.cpp +++ b/gm/gradients.cpp @@ -1033,3 +1033,41 @@ DEF_SIMPLE_GM(fancy_gradients, canvas, 800, 300) { SkBlendMode::kExclusion); }); } + +DEF_SIMPLE_GM(sweep_tiling, canvas, 512, 512) { + static constexpr SkScalar size = 160; + static constexpr SkColor colors[] = { SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN }; + static constexpr SkScalar pos[] = { 0, .25f, .50f }; + static_assert(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(pos), "size mismatch"); + + static constexpr SkShader::TileMode modes[] = { SkShader::kClamp_TileMode, + SkShader::kRepeat_TileMode, + SkShader::kMirror_TileMode }; + + static const struct { + SkScalar start, end; + } angles[] = { + { -330, -270 }, + { 30, 90 }, + { 390, 450 }, + }; + + SkPaint p; + const SkRect r = SkRect::MakeWH(size, size); + + for (auto mode : modes) { + { + SkAutoCanvasRestore acr(canvas, true); + + for (auto angle : angles) { + p.setShader(SkGradientShader::MakeSweep(size / 2, size / 2, colors, pos, + SK_ARRAY_COUNT(colors), mode, + angle.start, angle.end, 0, nullptr)); + + canvas->drawRect(r, p); + canvas->translate(size * 1.1f, 0); + } + } + canvas->translate(0, size * 1.1f); + } +} -- cgit v1.2.3