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 | |
| 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 10 | #include "SkCanvasPriv.h" |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 11 | |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 12 | |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 13 | // This GM tests out the deprecated Android-specific unclipped saveLayer "feature". |
| 14 | // In particular, it attempts to compare the performance of unclipped saveLayers with alternatives. |
| 15 | |
| 16 | static void save_layer_unclipped(SkCanvas* canvas, |
| 17 | SkScalar l, SkScalar t, SkScalar r, SkScalar b) { |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 18 | SkRect rect = SkRect::MakeLTRB(l, t, r, b); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 19 | canvas->saveLayer({ &rect, nullptr, nullptr, nullptr, nullptr, |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 20 | (SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag }); |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | static void do_draw(SkCanvas* canvas) { |
| 24 | SkPaint paint; |
| 25 | SkRandom rand; |
| 26 | |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 27 | for (int i = 0; i < 20; ++i) { |
Robert Phillips | 52b346e | 2017-01-19 08:37:31 -0500 | [diff] [blame] | 28 | paint.setColor(sk_tool_utils::color_to_565(rand.nextU() | (0xFF << 24))); |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 29 | canvas->drawRect({ 15, 15, 290, 40 }, paint); |
| 30 | canvas->translate(0, 30); |
| 31 | } |
| 32 | } |
| 33 | |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 34 | class UnclippedSaveLayerGM : public skiagm::GM { |
| 35 | public: |
| 36 | enum class Mode { |
| 37 | kClipped, |
| 38 | kUnclipped |
| 39 | }; |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 40 | |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 41 | UnclippedSaveLayerGM(Mode mode) : fMode(mode) { this->setBGColor(SK_ColorWHITE); } |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 42 | |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 43 | protected: |
| 44 | bool runAsBench() const override { return true; } |
| 45 | |
| 46 | SkString onShortName() override { |
| 47 | if (Mode::kClipped == fMode) { |
| 48 | return SkString("savelayer_unclipped"); |
| 49 | } else { |
| 50 | SkASSERT(Mode::kUnclipped == fMode); |
| 51 | return SkString("savelayer_clipped"); |
| 52 | } |
Mike Reed | 113fd34 | 2017-01-14 13:45:02 -0500 | [diff] [blame] | 53 | } |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 54 | |
| 55 | SkISize onISize() override { return SkISize::Make(320, 640); } |
| 56 | |
| 57 | void onDraw(SkCanvas* canvas) override { |
| 58 | const SkScalar L = 10; |
| 59 | const SkScalar T = 10; |
| 60 | const SkScalar R = 310; |
| 61 | const SkScalar B = 630; |
| 62 | |
| 63 | canvas->clipRect({ L, T, R, B }); |
| 64 | |
| 65 | for (int i = 0; i < 100; ++i) { |
| 66 | SkAutoCanvasRestore acr(canvas, true); |
| 67 | if (Mode::kClipped == fMode) { |
| 68 | save_layer_unclipped(canvas, L, T, R, T + 20); |
| 69 | save_layer_unclipped(canvas, L, B - 20, R, B); |
| 70 | } else { |
| 71 | SkASSERT(Mode::kUnclipped == fMode); |
| 72 | canvas->saveLayer({ L, T, R, B }, nullptr); |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 73 | } |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 74 | |
| 75 | do_draw(canvas); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | Mode fMode; |
| 81 | |
| 82 | typedef skiagm::GM INHERITED; |
| 83 | }; |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 84 | DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kClipped);) |
| 85 | DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kUnclipped);) |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 86 | |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 87 | DEF_SIMPLE_GM(picture_savelayer, canvas, 320, 640) { |
| 88 | SkPaint paint1, paint2, paint3; |
| 89 | paint1.setAlpha(0x7f); |
| 90 | paint2.setAlpha(0x3f); |
| 91 | paint3.setColor(0xFFFF0000); |
| 92 | SkRect rect1{40, 5, 80, 70}, rect2{5, 40, 70, 80}, rect3{10, 10, 70, 70}; |
| 93 | // In the future, we might also test the clipped case by allowing i = 0 |
| 94 | for(int i = 1; i < 2; ++i) { |
| 95 | canvas->translate(100 * i, 0); |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 96 | auto flag = i ? |
| 97 | (SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag : 0; |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 98 | canvas->saveLayer({ &rect1, &paint1, nullptr, nullptr, nullptr, flag}); |
| 99 | canvas->saveLayer({ &rect2, &paint2, nullptr, nullptr, nullptr, flag}); |
Yuqian Li | d196cbe | 2017-02-23 10:28:33 -0500 | [diff] [blame] | 100 | canvas->drawRect(rect3, paint3); |
| 101 | canvas->restore(); |
| 102 | canvas->restore(); |
| 103 | } |
| 104 | }; |
| 105 | |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 106 | #include "Resources.h" |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 107 | |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 108 | // Test kInitWithPrevious_SaveLayerFlag by drawing an image, save a layer with the flag, which |
| 109 | // should seed the layer with the image (from below). Then we punch a hole in the layer and |
| 110 | // restore with kPlus mode, which should show the mandrill super-bright on the outside, but |
| 111 | // normal where we punched the hole. |
| 112 | DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 113 | canvas->drawImage(GetResourceAsImage("images/mandrill_256.png"), 0, 0, nullptr); |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 114 | |
| 115 | SkCanvas::SaveLayerRec rec; |
| 116 | SkPaint paint; |
| 117 | paint.setBlendMode(SkBlendMode::kPlus); |
| 118 | rec.fSaveLayerFlags = SkCanvas::kInitWithPrevious_SaveLayerFlag; |
| 119 | rec.fPaint = &paint; |
| 120 | canvas->saveLayer(rec); |
| 121 | paint.setBlendMode(SkBlendMode::kClear); |
| 122 | canvas->drawCircle(128, 128, 96, paint); |
| 123 | canvas->restore(); |
| 124 | }; |
Robert Phillips | 42f3094 | 2017-01-17 10:48:52 -0500 | [diff] [blame] | 125 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 126 | #include "SkBlurImageFilter.h" |
| 127 | #include "SkGradientShader.h" |
| 128 | #include "SkPicture.h" |
| 129 | #include "SkPictureRecorder.h" |
| 130 | #include "SkSurface.h" |
| 131 | |
| 132 | static void draw_mask(SkCanvas* canvas, int size) { |
| 133 | const SkScalar cx = size * SK_ScalarHalf, |
| 134 | cy = cx; |
| 135 | const SkColor colors[] = { 0x00000000, 0xffff0000, 0x00000000, 0xffff0000, 0x00000000, |
| 136 | 0xffff0000, 0x00000000, 0xffff0000, 0x00000000 }; |
| 137 | |
| 138 | SkPaint paint; |
| 139 | paint.setAntiAlias(true); |
| 140 | paint.setShader(SkGradientShader::MakeSweep(cx, cy, colors, nullptr, SK_ARRAY_COUNT(colors))); |
| 141 | canvas->drawPaint(paint); |
| 142 | |
| 143 | paint.setShader(SkGradientShader::MakeRadial({cx, cy}, size / 4, colors, nullptr, 2, |
| 144 | SkShader::kClamp_TileMode)); |
| 145 | canvas->drawCircle(cx, cy, size / 4, paint); |
| 146 | } |
| 147 | |
| 148 | DEF_SIMPLE_GM(savelayer_clipmask, canvas, 1200, 1200) { |
| 149 | static constexpr int kSize = 100; |
| 150 | static constexpr SkRect kLayerBounds = { kSize * 0.25f, kSize * 0.25f, |
| 151 | kSize * 0.75f, kSize * 0.75f }; |
| 152 | static constexpr struct { |
| 153 | const SkRect* bounds; |
| 154 | const SkScalar matrix[9]; |
| 155 | } kConfigs[] = { |
| 156 | { nullptr, { 1 , 0 , 0, 0 , 1 , 0, 0, 0, 1 } }, |
| 157 | { nullptr, { 2 , 0 , 0, 0 , 2 , 0, 0, 0, 1 } }, |
| 158 | { nullptr, { 2 , 0 , -50, 0 , 2 , -50, 0, 0, 1 } }, |
| 159 | { nullptr, { 0.707f, -0.707f, 50, 0.707f, 0.707f, -20, 0, 0, 1 } }, |
| 160 | { nullptr, { 0.5f , 0 , 25, 0 , 0.5f , 25, 0, 0, 1 } }, |
| 161 | |
| 162 | { &kLayerBounds, { 1 , 0 , 0, 0 , 1 , 0, 0, 0, 1 } }, |
| 163 | { &kLayerBounds, { 2 , 0 , 0, 0 , 2 , 0, 0, 0, 1 } }, |
| 164 | { &kLayerBounds, { 2 , 0 , -50, 0 , 2 , -50, 0, 0, 1 } }, |
| 165 | { &kLayerBounds, { 0.707f, -0.707f, 50, 0.707f, 0.707f, -20, 0, 0, 1 } }, |
| 166 | { &kLayerBounds, { 0.5f , 0 , 25, 0 , 0.5f , 25, 0, 0, 1 } }, |
| 167 | }; |
| 168 | |
| 169 | using MaskMakerFunc = sk_sp<SkImage> (*)(int size); |
| 170 | static const MaskMakerFunc kMaskMakers[] = { |
| 171 | [](int size) -> sk_sp<SkImage> { |
| 172 | auto surf = SkSurface::MakeRaster(SkImageInfo::MakeA8(size, size)); |
| 173 | draw_mask(surf->getCanvas(), size); |
| 174 | return surf->makeImageSnapshot(); |
| 175 | }, |
| 176 | |
| 177 | [](int size) -> sk_sp<SkImage> { |
| 178 | auto surf = SkSurface::MakeRasterN32Premul(size, size); |
| 179 | draw_mask(surf->getCanvas(), size); |
| 180 | return surf->makeImageSnapshot(); |
| 181 | }, |
| 182 | |
| 183 | [](int size) -> sk_sp<SkImage> { |
| 184 | SkPictureRecorder recorder; |
| 185 | draw_mask(recorder.beginRecording(size, size), size); |
| 186 | return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), |
| 187 | SkISize::Make(size, size), |
| 188 | nullptr, nullptr, |
| 189 | SkImage::BitDepth::kU8, |
| 190 | SkColorSpace::MakeSRGB()); |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | using PaintMakerFunc = SkPaint (*)(); |
| 195 | static const PaintMakerFunc kPaintMakers[] = { |
| 196 | []() -> SkPaint { return SkPaint(); }, |
| 197 | []() -> SkPaint { |
| 198 | SkPaint p; p.setImageFilter(SkBlurImageFilter::Make(2, 2, nullptr)); return p; |
| 199 | }, |
| 200 | []() -> SkPaint { SkPaint p; p.setBlendMode(SkBlendMode::kSrcOut); return p; }, |
| 201 | }; |
| 202 | |
| 203 | canvas->drawColor(0xffcccccc); |
| 204 | |
| 205 | SkMatrix clipMatrix; |
| 206 | SkCanvas::SaveLayerRec rec; |
| 207 | rec.fClipMatrix = &clipMatrix; |
| 208 | |
| 209 | for (const auto& paintMaker : kPaintMakers) { |
| 210 | auto layerPaint = paintMaker(); |
| 211 | rec.fPaint = &layerPaint; |
| 212 | |
| 213 | for (const auto& maskMaker : kMaskMakers) { |
Mike Klein | b34ab04 | 2017-05-01 21:34:14 +0000 | [diff] [blame] | 214 | sk_sp<SkImage> mask = maskMaker(kSize); |
| 215 | rec.fClipMask = mask.get(); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 216 | |
| 217 | canvas->save(); |
| 218 | for (const auto cfg : kConfigs) { |
| 219 | rec.fBounds = cfg.bounds; |
| 220 | clipMatrix.set9(cfg.matrix); |
| 221 | canvas->saveLayer(rec); |
| 222 | |
| 223 | SkPaint paint; |
| 224 | paint.setColor(0xff0000ff); |
| 225 | canvas->drawRect(SkRect::MakeWH(50, 50), paint); |
| 226 | paint.setColor(0xffff0000); |
| 227 | canvas->drawRect(SkRect::MakeXYWH(50, 0, 50, 50), paint); |
| 228 | paint.setColor(0xff00ff00); |
| 229 | canvas->drawRect(SkRect::MakeXYWH(50, 50, 50, 50), paint); |
| 230 | paint.setColor(0xffffff00); |
| 231 | canvas->drawRect(SkRect::MakeXYWH(0, 50, 50, 50), paint); |
| 232 | |
| 233 | canvas->restore(); |
| 234 | canvas->translate(120, 0); |
| 235 | } |
| 236 | canvas->restore(); |
| 237 | canvas->translate(0, 120); |
| 238 | } |
| 239 | } |
| 240 | } |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 241 | |
| 242 | DEF_SIMPLE_GM(savelayer_coverage, canvas, 500, 500) { |
| 243 | canvas->saveLayer(nullptr, nullptr); |
| 244 | |
| 245 | SkRect r = { 0, 0, 200, 200 }; |
| 246 | SkPaint layerPaint; |
| 247 | layerPaint.setBlendMode(SkBlendMode::kModulate); |
| 248 | |
| 249 | auto image = GetResourceAsImage("images/mandrill_128.png"); |
| 250 | |
| 251 | auto proc = [layerPaint](SkCanvas* canvas, SkCanvas::SaveLayerRec& rec) { |
| 252 | SkPaint paint; |
| 253 | paint.setColor(SK_ColorRED); |
| 254 | |
| 255 | canvas->saveLayer(rec); |
| 256 | canvas->drawCircle(100, 100, 50, paint); |
| 257 | paint.setColor(0x8800FF00); |
| 258 | canvas->drawRect({10, 90, 190, 110}, paint); |
| 259 | canvas->restore(); |
| 260 | }; |
| 261 | |
| 262 | const int yflags[] = { 0, SkCanvas::kInitWithPrevious_SaveLayerFlag }; |
| 263 | for (int y = 0; y <= 1; ++y) { |
| 264 | const int xflags[] = { 0, SkCanvas::kMaskAgainstCoverage_EXPERIMENTAL_DONT_USE_SaveLayerFlag }; |
| 265 | for (int x = 0; x <= 1; ++x) { |
| 266 | canvas->save(); |
| 267 | canvas->translate(x * 200.f, y * 200.f); |
| 268 | |
| 269 | SkCanvas::SaveLayerRec rec(&r, &layerPaint, yflags[y] | xflags[x]); |
| 270 | canvas->drawImageRect(image, r, nullptr); |
| 271 | proc(canvas, rec); |
| 272 | |
| 273 | canvas->restore(); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | canvas->restore(); |
| 278 | } |
| 279 | |