Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlendMode.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkColorSpace.h" |
| 13 | #include "include/core/SkFont.h" |
| 14 | #include "include/core/SkImage.h" |
| 15 | #include "include/core/SkImageFilter.h" |
| 16 | #include "include/core/SkImageInfo.h" |
| 17 | #include "include/core/SkMaskFilter.h" |
| 18 | #include "include/core/SkMatrix.h" |
| 19 | #include "include/core/SkPaint.h" |
| 20 | #include "include/core/SkPicture.h" |
| 21 | #include "include/core/SkPictureRecorder.h" |
| 22 | #include "include/core/SkPoint.h" |
| 23 | #include "include/core/SkRect.h" |
| 24 | #include "include/core/SkRefCnt.h" |
| 25 | #include "include/core/SkScalar.h" |
| 26 | #include "include/core/SkShader.h" |
| 27 | #include "include/core/SkSize.h" |
| 28 | #include "include/core/SkString.h" |
| 29 | #include "include/core/SkSurface.h" |
| 30 | #include "include/core/SkTextBlob.h" |
| 31 | #include "include/core/SkTileMode.h" |
| 32 | #include "include/core/SkTypeface.h" |
| 33 | #include "include/core/SkTypes.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 34 | #include "include/effects/SkGradientShader.h" |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 35 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "include/effects/SkShaderMaskFilter.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 37 | #include "include/utils/SkRandom.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 38 | #include "src/core/SkCanvasPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 39 | #include "tools/Resources.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 40 | #include "tools/ToolUtils.h" |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 41 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 42 | #include <string.h> |
| 43 | #include <initializer_list> |
| 44 | |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 45 | // This GM tests out the deprecated Android-specific unclipped saveLayer "feature". |
| 46 | // In particular, it attempts to compare the performance of unclipped saveLayers with alternatives. |
| 47 | |
| 48 | static void save_layer_unclipped(SkCanvas* canvas, |
| 49 | SkScalar l, SkScalar t, SkScalar r, SkScalar b) { |
Mike Reed | 18a8df6 | 2019-09-03 15:44:47 -0400 | [diff] [blame] | 50 | SkPaint paint; |
| 51 | paint.setAlphaf(0.25f); |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 52 | SkRect rect = SkRect::MakeLTRB(l, t, r, b); |
Mike Reed | 18a8df6 | 2019-09-03 15:44:47 -0400 | [diff] [blame] | 53 | canvas->saveLayer({ &rect, &paint, nullptr, nullptr, nullptr, |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 54 | (SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag }); |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static void do_draw(SkCanvas* canvas) { |
| 58 | SkPaint paint; |
Mike Reed | 18a8df6 | 2019-09-03 15:44:47 -0400 | [diff] [blame] | 59 | paint.setColor(0xFFFF0000); |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 60 | |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 61 | for (int i = 0; i < 20; ++i) { |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 62 | canvas->drawRect({ 15, 15, 290, 40 }, paint); |
| 63 | canvas->translate(0, 30); |
| 64 | } |
| 65 | } |
| 66 | |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 67 | class UnclippedSaveLayerGM : public skiagm::GM { |
| 68 | public: |
Mike Reed | 18a8df6 | 2019-09-03 15:44:47 -0400 | [diff] [blame] | 69 | UnclippedSaveLayerGM() { this->setBGColor(SK_ColorWHITE); } |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 70 | |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 71 | protected: |
| 72 | bool runAsBench() const override { return true; } |
| 73 | |
| 74 | SkString onShortName() override { |
Mike Reed | 18a8df6 | 2019-09-03 15:44:47 -0400 | [diff] [blame] | 75 | return SkString("savelayer_unclipped"); |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 76 | } |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 77 | |
| 78 | SkISize onISize() override { return SkISize::Make(320, 640); } |
| 79 | |
| 80 | void onDraw(SkCanvas* canvas) override { |
| 81 | const SkScalar L = 10; |
| 82 | const SkScalar T = 10; |
| 83 | const SkScalar R = 310; |
| 84 | const SkScalar B = 630; |
| 85 | |
| 86 | canvas->clipRect({ L, T, R, B }); |
| 87 | |
Mike Reed | 18a8df6 | 2019-09-03 15:44:47 -0400 | [diff] [blame] | 88 | SkAutoCanvasRestore acr(canvas, true); |
| 89 | save_layer_unclipped(canvas, L, T, R, T + 100); |
| 90 | save_layer_unclipped(canvas, L, B - 100, R, B); |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 91 | |
Mike Reed | 18a8df6 | 2019-09-03 15:44:47 -0400 | [diff] [blame] | 92 | do_draw(canvas); |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private: |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 96 | typedef skiagm::GM INHERITED; |
| 97 | }; |
Mike Reed | 18a8df6 | 2019-09-03 15:44:47 -0400 | [diff] [blame] | 98 | DEF_GM(return new UnclippedSaveLayerGM;) |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 99 | |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 100 | DEF_SIMPLE_GM(picture_savelayer, canvas, 320, 640) { |
| 101 | SkPaint paint1, paint2, paint3; |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 102 | paint1.setAlphaf(0.5f); |
| 103 | paint2.setAlphaf(0.25f); |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 104 | paint3.setColor(0xFFFF0000); |
| 105 | SkRect rect1{40, 5, 80, 70}, rect2{5, 40, 70, 80}, rect3{10, 10, 70, 70}; |
| 106 | // In the future, we might also test the clipped case by allowing i = 0 |
| 107 | for(int i = 1; i < 2; ++i) { |
| 108 | canvas->translate(100 * i, 0); |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 109 | auto flag = i ? |
| 110 | (SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag : 0; |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 111 | canvas->saveLayer({ &rect1, &paint1, nullptr, nullptr, nullptr, flag}); |
| 112 | canvas->saveLayer({ &rect2, &paint2, nullptr, nullptr, nullptr, flag}); |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 113 | canvas->drawRect(rect3, paint3); |
| 114 | canvas->restore(); |
| 115 | canvas->restore(); |
| 116 | } |
| 117 | }; |
| 118 | |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 119 | // Test kInitWithPrevious_SaveLayerFlag by drawing an image, save a layer with the flag, which |
| 120 | // should seed the layer with the image (from below). Then we punch a hole in the layer and |
| 121 | // restore with kPlus mode, which should show the mandrill super-bright on the outside, but |
| 122 | // normal where we punched the hole. |
| 123 | DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 124 | canvas->drawImage(GetResourceAsImage("images/mandrill_256.png"), 0, 0, nullptr); |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 125 | |
| 126 | SkCanvas::SaveLayerRec rec; |
| 127 | SkPaint paint; |
| 128 | paint.setBlendMode(SkBlendMode::kPlus); |
| 129 | rec.fSaveLayerFlags = SkCanvas::kInitWithPrevious_SaveLayerFlag; |
| 130 | rec.fPaint = &paint; |
| 131 | canvas->saveLayer(rec); |
| 132 | paint.setBlendMode(SkBlendMode::kClear); |
| 133 | canvas->drawCircle(128, 128, 96, paint); |
| 134 | canvas->restore(); |
| 135 | }; |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 136 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 137 | static void draw_mask(SkCanvas* canvas, int size) { |
| 138 | const SkScalar cx = size * SK_ScalarHalf, |
| 139 | cy = cx; |
| 140 | const SkColor colors[] = { 0x00000000, 0xffff0000, 0x00000000, 0xffff0000, 0x00000000, |
| 141 | 0xffff0000, 0x00000000, 0xffff0000, 0x00000000 }; |
| 142 | |
| 143 | SkPaint paint; |
| 144 | paint.setAntiAlias(true); |
| 145 | paint.setShader(SkGradientShader::MakeSweep(cx, cy, colors, nullptr, SK_ARRAY_COUNT(colors))); |
| 146 | canvas->drawPaint(paint); |
| 147 | |
| 148 | paint.setShader(SkGradientShader::MakeRadial({cx, cy}, size / 4, colors, nullptr, 2, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 149 | SkTileMode::kClamp)); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 150 | canvas->drawCircle(cx, cy, size / 4, paint); |
| 151 | } |
| 152 | |
| 153 | DEF_SIMPLE_GM(savelayer_clipmask, canvas, 1200, 1200) { |
| 154 | static constexpr int kSize = 100; |
| 155 | static constexpr SkRect kLayerBounds = { kSize * 0.25f, kSize * 0.25f, |
| 156 | kSize * 0.75f, kSize * 0.75f }; |
| 157 | static constexpr struct { |
| 158 | const SkRect* bounds; |
| 159 | const SkScalar matrix[9]; |
| 160 | } kConfigs[] = { |
| 161 | { nullptr, { 1 , 0 , 0, 0 , 1 , 0, 0, 0, 1 } }, |
| 162 | { nullptr, { 2 , 0 , 0, 0 , 2 , 0, 0, 0, 1 } }, |
| 163 | { nullptr, { 2 , 0 , -50, 0 , 2 , -50, 0, 0, 1 } }, |
| 164 | { nullptr, { 0.707f, -0.707f, 50, 0.707f, 0.707f, -20, 0, 0, 1 } }, |
| 165 | { nullptr, { 0.5f , 0 , 25, 0 , 0.5f , 25, 0, 0, 1 } }, |
| 166 | |
| 167 | { &kLayerBounds, { 1 , 0 , 0, 0 , 1 , 0, 0, 0, 1 } }, |
| 168 | { &kLayerBounds, { 2 , 0 , 0, 0 , 2 , 0, 0, 0, 1 } }, |
| 169 | { &kLayerBounds, { 2 , 0 , -50, 0 , 2 , -50, 0, 0, 1 } }, |
| 170 | { &kLayerBounds, { 0.707f, -0.707f, 50, 0.707f, 0.707f, -20, 0, 0, 1 } }, |
| 171 | { &kLayerBounds, { 0.5f , 0 , 25, 0 , 0.5f , 25, 0, 0, 1 } }, |
| 172 | }; |
| 173 | |
| 174 | using MaskMakerFunc = sk_sp<SkImage> (*)(int size); |
| 175 | static const MaskMakerFunc kMaskMakers[] = { |
| 176 | [](int size) -> sk_sp<SkImage> { |
| 177 | auto surf = SkSurface::MakeRaster(SkImageInfo::MakeA8(size, size)); |
| 178 | draw_mask(surf->getCanvas(), size); |
| 179 | return surf->makeImageSnapshot(); |
| 180 | }, |
| 181 | |
| 182 | [](int size) -> sk_sp<SkImage> { |
| 183 | auto surf = SkSurface::MakeRasterN32Premul(size, size); |
| 184 | draw_mask(surf->getCanvas(), size); |
| 185 | return surf->makeImageSnapshot(); |
| 186 | }, |
| 187 | |
| 188 | [](int size) -> sk_sp<SkImage> { |
| 189 | SkPictureRecorder recorder; |
| 190 | draw_mask(recorder.beginRecording(size, size), size); |
| 191 | return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), |
| 192 | SkISize::Make(size, size), |
| 193 | nullptr, nullptr, |
| 194 | SkImage::BitDepth::kU8, |
| 195 | SkColorSpace::MakeSRGB()); |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | using PaintMakerFunc = SkPaint (*)(); |
| 200 | static const PaintMakerFunc kPaintMakers[] = { |
| 201 | []() -> SkPaint { return SkPaint(); }, |
| 202 | []() -> SkPaint { |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 203 | SkPaint p; p.setImageFilter(SkImageFilters::Blur(2, 2, nullptr)); return p; |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 204 | }, |
| 205 | []() -> SkPaint { SkPaint p; p.setBlendMode(SkBlendMode::kSrcOut); return p; }, |
| 206 | }; |
| 207 | |
| 208 | canvas->drawColor(0xffcccccc); |
| 209 | |
| 210 | SkMatrix clipMatrix; |
| 211 | SkCanvas::SaveLayerRec rec; |
| 212 | rec.fClipMatrix = &clipMatrix; |
| 213 | |
| 214 | for (const auto& paintMaker : kPaintMakers) { |
| 215 | auto layerPaint = paintMaker(); |
| 216 | rec.fPaint = &layerPaint; |
| 217 | |
| 218 | for (const auto& maskMaker : kMaskMakers) { |
Mike Klein | b34ab04 | 2017-05-01 21:34:14 +0000 | [diff] [blame] | 219 | sk_sp<SkImage> mask = maskMaker(kSize); |
| 220 | rec.fClipMask = mask.get(); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 221 | |
| 222 | canvas->save(); |
| 223 | for (const auto cfg : kConfigs) { |
| 224 | rec.fBounds = cfg.bounds; |
| 225 | clipMatrix.set9(cfg.matrix); |
| 226 | canvas->saveLayer(rec); |
| 227 | |
| 228 | SkPaint paint; |
| 229 | paint.setColor(0xff0000ff); |
| 230 | canvas->drawRect(SkRect::MakeWH(50, 50), paint); |
| 231 | paint.setColor(0xffff0000); |
| 232 | canvas->drawRect(SkRect::MakeXYWH(50, 0, 50, 50), paint); |
| 233 | paint.setColor(0xff00ff00); |
| 234 | canvas->drawRect(SkRect::MakeXYWH(50, 50, 50, 50), paint); |
| 235 | paint.setColor(0xffffff00); |
| 236 | canvas->drawRect(SkRect::MakeXYWH(0, 50, 50, 50), paint); |
| 237 | |
| 238 | canvas->restore(); |
| 239 | canvas->translate(120, 0); |
| 240 | } |
| 241 | canvas->restore(); |
| 242 | canvas->translate(0, 120); |
| 243 | } |
| 244 | } |
| 245 | } |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 246 | |
| 247 | DEF_SIMPLE_GM(savelayer_coverage, canvas, 500, 500) { |
| 248 | canvas->saveLayer(nullptr, nullptr); |
| 249 | |
| 250 | SkRect r = { 0, 0, 200, 200 }; |
| 251 | SkPaint layerPaint; |
| 252 | layerPaint.setBlendMode(SkBlendMode::kModulate); |
| 253 | |
| 254 | auto image = GetResourceAsImage("images/mandrill_128.png"); |
| 255 | |
| 256 | auto proc = [layerPaint](SkCanvas* canvas, SkCanvas::SaveLayerRec& rec) { |
| 257 | SkPaint paint; |
| 258 | paint.setColor(SK_ColorRED); |
| 259 | |
| 260 | canvas->saveLayer(rec); |
| 261 | canvas->drawCircle(100, 100, 50, paint); |
| 262 | paint.setColor(0x8800FF00); |
| 263 | canvas->drawRect({10, 90, 190, 110}, paint); |
| 264 | canvas->restore(); |
| 265 | }; |
| 266 | |
| 267 | const int yflags[] = { 0, SkCanvas::kInitWithPrevious_SaveLayerFlag }; |
| 268 | for (int y = 0; y <= 1; ++y) { |
| 269 | const int xflags[] = { 0, SkCanvas::kMaskAgainstCoverage_EXPERIMENTAL_DONT_USE_SaveLayerFlag }; |
| 270 | for (int x = 0; x <= 1; ++x) { |
| 271 | canvas->save(); |
| 272 | canvas->translate(x * 200.f, y * 200.f); |
| 273 | |
| 274 | SkCanvas::SaveLayerRec rec(&r, &layerPaint, yflags[y] | xflags[x]); |
| 275 | canvas->drawImageRect(image, r, nullptr); |
| 276 | proc(canvas, rec); |
| 277 | |
| 278 | canvas->restore(); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | canvas->restore(); |
| 283 | } |
| 284 | |
Michael Ludwig | beb7cd2 | 2019-04-08 11:11:42 -0400 | [diff] [blame] | 285 | DEF_SIMPLE_GM(savelayer_clipmask_maskfilter, canvas, 500, 500) { |
| 286 | // Offscreen surface for making the clip mask and mask filter images |
| 287 | auto surf = SkSurface::MakeRaster(SkImageInfo::MakeA8(100, 100)); |
| 288 | SkPaint maskPaint; |
| 289 | maskPaint.setColor(SK_ColorWHITE); |
| 290 | maskPaint.setAntiAlias(true); |
| 291 | |
| 292 | // Draw a centered circle for the mask filter |
| 293 | surf->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 294 | surf->getCanvas()->drawCircle(50.f, 50.f, 50.f, maskPaint); |
| 295 | auto maskFilterImage = surf->makeImageSnapshot(); |
| 296 | sk_sp<SkMaskFilter> maskFilter = SkShaderMaskFilter::Make(maskFilterImage->makeShader()); |
| 297 | |
| 298 | // Cut out a cross for the clip mask |
| 299 | surf->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 300 | surf->getCanvas()->drawRect(SkRect::MakeLTRB(0.f, 0.f, 40.f, 40.f), maskPaint); |
| 301 | surf->getCanvas()->drawRect(SkRect::MakeLTRB(60.f, 0.f, 100.f, 40.f), maskPaint); |
| 302 | surf->getCanvas()->drawRect(SkRect::MakeLTRB(0.f, 60.f, 40.f, 100.f), maskPaint); |
| 303 | surf->getCanvas()->drawRect(SkRect::MakeLTRB(60.f, 60.f, 100.f, 100.f), maskPaint); |
| 304 | auto clipMaskImage = surf->makeImageSnapshot(); |
| 305 | SkMatrix clipMatrix = SkMatrix::I(); |
| 306 | SkRect clipBounds = SkRect::MakeWH(100, 100); |
| 307 | |
| 308 | // On the main canvas, save a 100x100 layer three times, applying clip mask, mask filter, or |
| 309 | // both, translating across the GM for each configuration. |
| 310 | canvas->clear(SK_ColorGRAY); |
| 311 | |
| 312 | canvas->translate(25.f, 0.f); |
| 313 | |
| 314 | // Clip mask only |
| 315 | SkCanvas::SaveLayerRec rec; |
| 316 | rec.fBounds = &clipBounds; |
| 317 | rec.fClipMask = clipMaskImage.get(); |
| 318 | rec.fClipMatrix = &clipMatrix; |
| 319 | canvas->saveLayer(rec); |
| 320 | canvas->clear(SK_ColorWHITE); |
| 321 | canvas->restore(); |
| 322 | |
| 323 | canvas->translate(125.f, 0.f); |
| 324 | |
| 325 | // Mask filter only |
| 326 | maskPaint.setMaskFilter(maskFilter); |
| 327 | rec.fClipMask = nullptr; |
| 328 | rec.fPaint = &maskPaint; |
| 329 | canvas->saveLayer(rec); |
| 330 | canvas->clear(SK_ColorWHITE); |
| 331 | canvas->restore(); |
| 332 | |
| 333 | canvas->translate(125.f, 0.f); |
| 334 | |
| 335 | // Both |
| 336 | rec.fClipMask = clipMaskImage.get(); |
| 337 | canvas->saveLayer(rec); |
| 338 | canvas->clear(SK_ColorWHITE); |
| 339 | canvas->restore(); |
| 340 | } |
| 341 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 342 | static void draw_cell(SkCanvas* canvas, sk_sp<SkTextBlob> blob, SkColor c, SkScalar w, SkScalar h, |
| 343 | bool useDrawBehind) { |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 344 | SkRect r = SkRect::MakeWH(w, h); |
| 345 | SkPaint p; |
| 346 | p.setColor(c); |
| 347 | p.setBlendMode(SkBlendMode::kSrc); |
| 348 | canvas->drawRect(r, p); |
| 349 | p.setBlendMode(SkBlendMode::kSrcOver); |
| 350 | |
| 351 | const SkScalar margin = 80; |
| 352 | r.fLeft = w - margin; |
| 353 | |
| 354 | // save the behind image |
| 355 | SkDEBUGCODE(int sc0 =) canvas->getSaveCount(); |
| 356 | SkDEBUGCODE(int sc1 =) SkCanvasPriv::SaveBehind(canvas, &r); |
| 357 | SkDEBUGCODE(int sc2 =) canvas->getSaveCount(); |
| 358 | SkASSERT(sc0 == sc1); |
| 359 | SkASSERT(sc0 + 1 == sc2); |
| 360 | |
| 361 | // draw the foreground (including over the 'behind' section) |
| 362 | p.setColor(SK_ColorBLACK); |
| 363 | canvas->drawTextBlob(blob, 10, 30, p); |
| 364 | |
| 365 | // draw the treatment |
| 366 | const SkPoint pts[] = { {r.fLeft,0}, {r.fRight, 0} }; |
| 367 | const SkColor colors[] = { 0x88000000, 0x0 }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 368 | auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 369 | p.setShader(sh); |
| 370 | p.setBlendMode(SkBlendMode::kDstIn); |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 371 | |
| 372 | if (useDrawBehind) { |
| 373 | SkCanvasPriv::DrawBehind(canvas, p); |
| 374 | } else { |
| 375 | canvas->drawRect(r, p); |
| 376 | } |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 377 | |
| 378 | // this should restore the behind image |
| 379 | canvas->restore(); |
| 380 | SkDEBUGCODE(int sc3 =) canvas->getSaveCount(); |
| 381 | SkASSERT(sc3 == sc0); |
| 382 | |
| 383 | // just outline where we expect the treatment to appear |
| 384 | p.reset(); |
| 385 | p.setStyle(SkPaint::kStroke_Style); |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 386 | p.setAlphaf(0.25f); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 387 | } |
| 388 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 389 | static void draw_list(SkCanvas* canvas, sk_sp<SkTextBlob> blob, bool useDrawBehind) { |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 390 | SkAutoCanvasRestore acr(canvas, true); |
| 391 | |
| 392 | SkRandom rand; |
| 393 | SkScalar w = 400; |
| 394 | SkScalar h = 40; |
| 395 | for (int i = 0; i < 8; ++i) { |
| 396 | SkColor c = rand.nextU(); // ensure we're opaque |
| 397 | c = (c & 0xFFFFFF) | 0x80000000; |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 398 | draw_cell(canvas, blob, c, w, h, useDrawBehind); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 399 | canvas->translate(0, h); |
| 400 | } |
| 401 | } |
| 402 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 403 | DEF_SIMPLE_GM(save_behind, canvas, 830, 670) { |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 404 | SkFont font; |
Mike Reed | 2ce3640 | 2019-04-21 18:15:23 -0400 | [diff] [blame] | 405 | font.setTypeface(ToolUtils::create_portable_typeface()); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 406 | font.setSize(30); |
Mike Reed | 2ce3640 | 2019-04-21 18:15:23 -0400 | [diff] [blame] | 407 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 408 | const char text[] = "This is a very long line of text"; |
| 409 | auto blob = SkTextBlob::MakeFromText(text, strlen(text), font); |
| 410 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 411 | for (bool useDrawBehind : {false, true}) { |
| 412 | canvas->save(); |
| 413 | |
| 414 | draw_list(canvas, blob, useDrawBehind); |
| 415 | canvas->translate(0, 350); |
| 416 | canvas->saveLayer({0, 0, 400, 320}, nullptr); |
| 417 | draw_list(canvas, blob, useDrawBehind); |
| 418 | canvas->restore(); |
| 419 | |
| 420 | canvas->restore(); |
| 421 | canvas->translate(430, 0); |
| 422 | } |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 423 | } |
Mike Reed | 1f3548c | 2019-07-12 12:53:11 -0400 | [diff] [blame] | 424 | |
| 425 | #include "include/effects/SkGradientShader.h" |
| 426 | |
| 427 | DEF_SIMPLE_GM(savelayer_f16, canvas, 900, 300) { |
| 428 | int n = 15; |
| 429 | SkRect r{0, 0, 300, 300}; |
| 430 | SkPaint paint; |
| 431 | |
| 432 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED }; |
| 433 | paint.setShader(SkGradientShader::MakeSweep(r.centerX(), r.centerY(), |
| 434 | colors, nullptr, SK_ARRAY_COUNT(colors))); |
| 435 | |
| 436 | canvas->drawOval(r, paint); |
| 437 | |
| 438 | paint.setAlphaf(1.0f/n); |
| 439 | paint.setBlendMode(SkBlendMode::kPlus); |
| 440 | |
| 441 | for (auto flags : {0, (int)SkCanvas::kF16ColorType}) { |
| 442 | canvas->translate(r.width(), 0); |
| 443 | |
| 444 | SkCanvas::SaveLayerRec rec; |
| 445 | rec.fSaveLayerFlags = flags; |
| 446 | canvas->saveLayer(rec); |
| 447 | for (int i = 0; i < n; ++i) { |
| 448 | canvas->drawOval(r, paint); |
| 449 | } |
| 450 | canvas->restore(); |
| 451 | } |
| 452 | } |