• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
9 #include "sk_tool_utils.h"
10 
11 static const uint32_t SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag = 1U << 31;
12 
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 
save_layer_unclipped(SkCanvas * canvas,SkScalar l,SkScalar t,SkScalar r,SkScalar b)16 static void save_layer_unclipped(SkCanvas* canvas,
17                                  SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
18     SkRect rect = SkRect::MakeLTRB(l, t, r, b);
19     canvas->saveLayer({ &rect, nullptr, nullptr, nullptr, nullptr,
20                         SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag });
21 }
22 
do_draw(SkCanvas * canvas)23 static void do_draw(SkCanvas* canvas) {
24     SkPaint paint;
25     SkRandom rand;
26 
27     for (int i = 0; i < 20; ++i) {
28         paint.setColor(sk_tool_utils::color_to_565(rand.nextU() | (0xFF << 24)));
29         canvas->drawRect({ 15, 15, 290, 40 }, paint);
30         canvas->translate(0, 30);
31     }
32 }
33 
34 class UnclippedSaveLayerGM : public skiagm::GM {
35 public:
36     enum class Mode {
37         kClipped,
38         kUnclipped
39     };
40 
UnclippedSaveLayerGM(Mode mode)41     UnclippedSaveLayerGM(Mode mode) : fMode(mode) { this->setBGColor(SK_ColorWHITE); }
42 
43 protected:
runAsBench() const44     bool runAsBench() const override { return true; }
45 
onShortName()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         }
53     }
54 
onISize()55     SkISize onISize() override { return SkISize::Make(320, 640); }
56 
onDraw(SkCanvas * canvas)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);
73             }
74 
75             do_draw(canvas);
76         }
77     }
78 
79 private:
80     Mode fMode;
81 
82     typedef skiagm::GM INHERITED;
83 };
84 DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kClipped);)
DEF_GM(return new UnclippedSaveLayerGM (UnclippedSaveLayerGM::Mode::kUnclipped);)85 DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kUnclipped);)
86 
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);
96         auto flag = i ? SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag : 0;
97         canvas->saveLayer({ &rect1, &paint1, nullptr, nullptr, nullptr, flag});
98         canvas->saveLayer({ &rect2, &paint2, nullptr, nullptr, nullptr, flag});
99         canvas->drawRect(rect3, paint3);
100         canvas->restore();
101         canvas->restore();
102     }
103 };
104 
105 #include "Resources.h"
106 
107 // Test kInitWithPrevious_SaveLayerFlag by drawing an image, save a layer with the flag, which
108 // should seed the layer with the image (from below). Then we punch a hole in the layer and
109 // restore with kPlus mode, which should show the mandrill super-bright on the outside, but
110 // normal where we punched the hole.
111 DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) {
112     canvas->drawImage(GetResourceAsImage("images/mandrill_256.png"), 0, 0, nullptr);
113 
114     SkCanvas::SaveLayerRec rec;
115     SkPaint paint;
116     paint.setBlendMode(SkBlendMode::kPlus);
117     rec.fSaveLayerFlags = SkCanvas::kInitWithPrevious_SaveLayerFlag;
118     rec.fPaint = &paint;
119     canvas->saveLayer(rec);
120     paint.setBlendMode(SkBlendMode::kClear);
121     canvas->drawCircle(128, 128, 96, paint);
122     canvas->restore();
123 };
124 
125 #include "SkBlurImageFilter.h"
126 #include "SkGradientShader.h"
127 #include "SkPicture.h"
128 #include "SkPictureRecorder.h"
129 #include "SkSurface.h"
130 
draw_mask(SkCanvas * canvas,int size)131 static void draw_mask(SkCanvas* canvas, int size) {
132     const SkScalar cx = size * SK_ScalarHalf,
133                    cy = cx;
134     const SkColor colors[] = { 0x00000000, 0xffff0000, 0x00000000, 0xffff0000, 0x00000000,
135                                0xffff0000, 0x00000000, 0xffff0000, 0x00000000 };
136 
137     SkPaint paint;
138     paint.setAntiAlias(true);
139     paint.setShader(SkGradientShader::MakeSweep(cx, cy, colors, nullptr, SK_ARRAY_COUNT(colors)));
140     canvas->drawPaint(paint);
141 
142     paint.setShader(SkGradientShader::MakeRadial({cx, cy}, size / 4, colors, nullptr, 2,
143                                                  SkShader::kClamp_TileMode));
144     canvas->drawCircle(cx, cy, size / 4, paint);
145 }
146 
147 DEF_SIMPLE_GM(savelayer_clipmask, canvas, 1200, 1200) {
148     static constexpr int kSize = 100;
149     static constexpr SkRect kLayerBounds = { kSize * 0.25f, kSize * 0.25f,
150                                              kSize * 0.75f, kSize * 0.75f };
151     static constexpr struct {
152         const SkRect* bounds;
153         const SkScalar matrix[9];
154     } kConfigs[] = {
155         { nullptr, { 1     ,  0     ,   0,   0     , 1     ,   0,   0, 0, 1 } },
156         { nullptr, { 2     ,  0     ,   0,   0     , 2     ,   0,   0, 0, 1 } },
157         { nullptr, { 2     ,  0     , -50,   0     , 2     , -50,   0, 0, 1 } },
158         { nullptr, { 0.707f, -0.707f,  50,   0.707f, 0.707f, -20,   0, 0, 1 } },
159         { nullptr, { 0.5f  ,  0     ,  25,   0     , 0.5f  ,  25,   0, 0, 1 } },
160 
161         { &kLayerBounds, { 1     ,  0     ,   0,   0     , 1     ,   0,   0, 0, 1 } },
162         { &kLayerBounds, { 2     ,  0     ,   0,   0     , 2     ,   0,   0, 0, 1 } },
163         { &kLayerBounds, { 2     ,  0     , -50,   0     , 2     , -50,   0, 0, 1 } },
164         { &kLayerBounds, { 0.707f, -0.707f,  50,   0.707f, 0.707f, -20,   0, 0, 1 } },
165         { &kLayerBounds, { 0.5f  ,  0     ,  25,   0     , 0.5f  ,  25,   0, 0, 1 } },
166     };
167 
168     using MaskMakerFunc = sk_sp<SkImage> (*)(int size);
169     static const MaskMakerFunc kMaskMakers[] = {
__anon99bcef050202() 170         [](int size) -> sk_sp<SkImage> {
171             auto surf = SkSurface::MakeRaster(SkImageInfo::MakeA8(size, size));
172             draw_mask(surf->getCanvas(), size);
173             return surf->makeImageSnapshot();
174         },
175 
__anon99bcef050302() 176         [](int size) -> sk_sp<SkImage> {
177             auto surf = SkSurface::MakeRasterN32Premul(size, size);
178             draw_mask(surf->getCanvas(), size);
179             return surf->makeImageSnapshot();
180         },
181 
__anon99bcef050402() 182         [](int size) -> sk_sp<SkImage> {
183             SkPictureRecorder recorder;
184             draw_mask(recorder.beginRecording(size, size), size);
185             return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
186                                             SkISize::Make(size, size),
187                                             nullptr, nullptr,
188                                             SkImage::BitDepth::kU8,
189                                             SkColorSpace::MakeSRGB());
190         }
191     };
192 
193     using PaintMakerFunc = SkPaint (*)();
194     static const PaintMakerFunc kPaintMakers[] = {
__anon99bcef050502() 195         []() -> SkPaint { return SkPaint(); },
__anon99bcef050602() 196         []() -> SkPaint {
197             SkPaint p; p.setImageFilter(SkBlurImageFilter::Make(2, 2, nullptr)); return p;
198         },
__anon99bcef050702() 199         []() -> SkPaint { SkPaint p; p.setBlendMode(SkBlendMode::kSrcOut); return p; },
200     };
201 
202     canvas->drawColor(0xffcccccc);
203 
204     SkMatrix clipMatrix;
205     SkCanvas::SaveLayerRec rec;
206     rec.fClipMatrix = &clipMatrix;
207 
208     for (const auto& paintMaker : kPaintMakers) {
209         auto layerPaint = paintMaker();
210         rec.fPaint = &layerPaint;
211 
212         for (const auto& maskMaker : kMaskMakers) {
213             sk_sp<SkImage> mask = maskMaker(kSize);
214             rec.fClipMask = mask.get();
215 
216             canvas->save();
217             for (const auto cfg : kConfigs) {
218                 rec.fBounds = cfg.bounds;
219                 clipMatrix.set9(cfg.matrix);
220                 canvas->saveLayer(rec);
221 
222                 SkPaint paint;
223                 paint.setColor(0xff0000ff);
224                 canvas->drawRect(SkRect::MakeWH(50, 50), paint);
225                 paint.setColor(0xffff0000);
226                 canvas->drawRect(SkRect::MakeXYWH(50, 0, 50, 50), paint);
227                 paint.setColor(0xff00ff00);
228                 canvas->drawRect(SkRect::MakeXYWH(50, 50, 50, 50), paint);
229                 paint.setColor(0xffffff00);
230                 canvas->drawRect(SkRect::MakeXYWH(0, 50, 50, 50), paint);
231 
232                 canvas->restore();
233                 canvas->translate(120, 0);
234             }
235             canvas->restore();
236             canvas->translate(0, 120);
237         }
238     }
239 }
240