blob: 32f95bfc7d995a7c790ad8d32155f7d21acac22e [file] [log] [blame]
Mike Reed113fd342017-01-14 13:45:02 -05001/*
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 Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
Cary Clark7eddfb82018-03-13 14:41:10 -040010#include "SkCanvasPriv.h"
Mike Reed113fd342017-01-14 13:45:02 -050011
Yuqian Lid196cbe2017-02-23 10:28:33 -050012
Robert Phillips42f30942017-01-17 10:48:52 -050013// 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
16static void save_layer_unclipped(SkCanvas* canvas,
17 SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
Mike Reed113fd342017-01-14 13:45:02 -050018 SkRect rect = SkRect::MakeLTRB(l, t, r, b);
Florin Malita53f77bd2017-04-28 13:48:37 -040019 canvas->saveLayer({ &rect, nullptr, nullptr, nullptr, nullptr,
Cary Clark7eddfb82018-03-13 14:41:10 -040020 (SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag });
Mike Reed113fd342017-01-14 13:45:02 -050021}
22
23static void do_draw(SkCanvas* canvas) {
24 SkPaint paint;
25 SkRandom rand;
26
Mike Reed113fd342017-01-14 13:45:02 -050027 for (int i = 0; i < 20; ++i) {
Robert Phillips52b346e2017-01-19 08:37:31 -050028 paint.setColor(sk_tool_utils::color_to_565(rand.nextU() | (0xFF << 24)));
Mike Reed113fd342017-01-14 13:45:02 -050029 canvas->drawRect({ 15, 15, 290, 40 }, paint);
30 canvas->translate(0, 30);
31 }
32}
33
Robert Phillips42f30942017-01-17 10:48:52 -050034class UnclippedSaveLayerGM : public skiagm::GM {
35public:
36 enum class Mode {
37 kClipped,
38 kUnclipped
39 };
Mike Reed113fd342017-01-14 13:45:02 -050040
Robert Phillips42f30942017-01-17 10:48:52 -050041 UnclippedSaveLayerGM(Mode mode) : fMode(mode) { this->setBGColor(SK_ColorWHITE); }
Mike Reed113fd342017-01-14 13:45:02 -050042
Robert Phillips42f30942017-01-17 10:48:52 -050043protected:
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 Reed113fd342017-01-14 13:45:02 -050053 }
Robert Phillips42f30942017-01-17 10:48:52 -050054
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 Lid196cbe2017-02-23 10:28:33 -050073 }
Robert Phillips42f30942017-01-17 10:48:52 -050074
75 do_draw(canvas);
76 }
77 }
78
79private:
80 Mode fMode;
81
82 typedef skiagm::GM INHERITED;
83};
Mike Reedc61abee2017-02-28 17:45:27 -050084DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kClipped);)
85DEF_GM(return new UnclippedSaveLayerGM(UnclippedSaveLayerGM::Mode::kUnclipped);)
Robert Phillips42f30942017-01-17 10:48:52 -050086
Yuqian Lid196cbe2017-02-23 10:28:33 -050087DEF_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 Clark7eddfb82018-03-13 14:41:10 -040096 auto flag = i ?
97 (SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag : 0;
Florin Malita53f77bd2017-04-28 13:48:37 -040098 canvas->saveLayer({ &rect1, &paint1, nullptr, nullptr, nullptr, flag});
99 canvas->saveLayer({ &rect2, &paint2, nullptr, nullptr, nullptr, flag});
Yuqian Lid196cbe2017-02-23 10:28:33 -0500100 canvas->drawRect(rect3, paint3);
101 canvas->restore();
102 canvas->restore();
103 }
104};
105
Mike Reedc61abee2017-02-28 17:45:27 -0500106#include "Resources.h"
Robert Phillips42f30942017-01-17 10:48:52 -0500107
Mike Reedc61abee2017-02-28 17:45:27 -0500108// 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.
112DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) {
Hal Canaryc465d132017-12-08 10:21:31 -0500113 canvas->drawImage(GetResourceAsImage("images/mandrill_256.png"), 0, 0, nullptr);
Mike Reedc61abee2017-02-28 17:45:27 -0500114
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 Phillips42f30942017-01-17 10:48:52 -0500125
Florin Malita53f77bd2017-04-28 13:48:37 -0400126#include "SkBlurImageFilter.h"
127#include "SkGradientShader.h"
128#include "SkPicture.h"
129#include "SkPictureRecorder.h"
130#include "SkSurface.h"
131
132static 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
148DEF_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 Kleinb34ab042017-05-01 21:34:14 +0000214 sk_sp<SkImage> mask = maskMaker(kSize);
215 rec.fClipMask = mask.get();
Florin Malita53f77bd2017-04-28 13:48:37 -0400216
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}