blob: dd7ff7b54e6f514ad205e20862a94b9b75277a5d [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"
Mike Reed113fd342017-01-14 13:45:02 -050010
Yuqian Lid196cbe2017-02-23 10:28:33 -050011static const uint32_t SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag = 1U << 31;
12
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,
20 SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag });
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);
96 auto flag = i ? SkCanvas_kDontClipToLayer_PrivateSaveLayerFlag : 0;
Florin Malita53f77bd2017-04-28 13:48:37 -040097 canvas->saveLayer({ &rect1, &paint1, nullptr, nullptr, nullptr, flag});
98 canvas->saveLayer({ &rect2, &paint2, nullptr, nullptr, nullptr, flag});
Yuqian Lid196cbe2017-02-23 10:28:33 -050099 canvas->drawRect(rect3, paint3);
100 canvas->restore();
101 canvas->restore();
102 }
103};
104
Mike Reedc61abee2017-02-28 17:45:27 -0500105#include "Resources.h"
Robert Phillips42f30942017-01-17 10:48:52 -0500106
Mike Reedc61abee2017-02-28 17:45:27 -0500107// 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.
111DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) {
112 canvas->drawImage(GetResourceAsImage("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};
Robert Phillips42f30942017-01-17 10:48:52 -0500124
Florin Malita53f77bd2017-04-28 13:48:37 -0400125#include "SkBlurImageFilter.h"
126#include "SkGradientShader.h"
127#include "SkPicture.h"
128#include "SkPictureRecorder.h"
129#include "SkSurface.h"
130
131static 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
147DEF_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[] = {
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
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
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[] = {
195 []() -> SkPaint { return SkPaint(); },
196 []() -> SkPaint {
197 SkPaint p; p.setImageFilter(SkBlurImageFilter::Make(2, 2, nullptr)); return p;
198 },
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) {
Mike Kleinb34ab042017-05-01 21:34:14 +0000213 sk_sp<SkImage> mask = maskMaker(kSize);
214 rec.fClipMask = mask.get();
Florin Malita53f77bd2017-04-28 13:48:37 -0400215
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}