blob: ade68082facdac3deb5e417bb3b5af44c45667cd [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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 Wagner7fde8e12019-05-01 17:28:53 -040034#include "include/effects/SkGradientShader.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040035#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "include/effects/SkShaderMaskFilter.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040037#include "include/utils/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/core/SkCanvasPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040039#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "tools/ToolUtils.h"
Yuqian Lid196cbe2017-02-23 10:28:33 -050041
Ben Wagner7fde8e12019-05-01 17:28:53 -040042#include <string.h>
43#include <initializer_list>
44
Robert Phillips42f30942017-01-17 10:48:52 -050045// 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
48static void save_layer_unclipped(SkCanvas* canvas,
49 SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
Mike Reed18a8df62019-09-03 15:44:47 -040050 SkPaint paint;
51 paint.setAlphaf(0.25f);
Mike Reed113fd342017-01-14 13:45:02 -050052 SkRect rect = SkRect::MakeLTRB(l, t, r, b);
Mike Reed18a8df62019-09-03 15:44:47 -040053 canvas->saveLayer({ &rect, &paint, nullptr, nullptr, nullptr,
Cary Clark7eddfb82018-03-13 14:41:10 -040054 (SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag });
Mike Reed113fd342017-01-14 13:45:02 -050055}
56
57static void do_draw(SkCanvas* canvas) {
58 SkPaint paint;
Mike Reed18a8df62019-09-03 15:44:47 -040059 paint.setColor(0xFFFF0000);
Mike Reed113fd342017-01-14 13:45:02 -050060
Mike Reed113fd342017-01-14 13:45:02 -050061 for (int i = 0; i < 20; ++i) {
Mike Reed113fd342017-01-14 13:45:02 -050062 canvas->drawRect({ 15, 15, 290, 40 }, paint);
63 canvas->translate(0, 30);
64 }
65}
66
Robert Phillips42f30942017-01-17 10:48:52 -050067class UnclippedSaveLayerGM : public skiagm::GM {
68public:
Mike Reed18a8df62019-09-03 15:44:47 -040069 UnclippedSaveLayerGM() { this->setBGColor(SK_ColorWHITE); }
Mike Reed113fd342017-01-14 13:45:02 -050070
Robert Phillips42f30942017-01-17 10:48:52 -050071protected:
72 bool runAsBench() const override { return true; }
73
74 SkString onShortName() override {
Mike Reed18a8df62019-09-03 15:44:47 -040075 return SkString("savelayer_unclipped");
Mike Reed113fd342017-01-14 13:45:02 -050076 }
Robert Phillips42f30942017-01-17 10:48:52 -050077
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 Reed18a8df62019-09-03 15:44:47 -040088 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 Phillips42f30942017-01-17 10:48:52 -050091
Mike Reed18a8df62019-09-03 15:44:47 -040092 do_draw(canvas);
Robert Phillips42f30942017-01-17 10:48:52 -050093 }
94
95private:
Robert Phillips42f30942017-01-17 10:48:52 -050096 typedef skiagm::GM INHERITED;
97};
Mike Reed18a8df62019-09-03 15:44:47 -040098DEF_GM(return new UnclippedSaveLayerGM;)
Robert Phillips42f30942017-01-17 10:48:52 -050099
Yuqian Lid196cbe2017-02-23 10:28:33 -0500100DEF_SIMPLE_GM(picture_savelayer, canvas, 320, 640) {
101 SkPaint paint1, paint2, paint3;
Mike Reed9407e242019-02-15 16:13:57 -0500102 paint1.setAlphaf(0.5f);
103 paint2.setAlphaf(0.25f);
Yuqian Lid196cbe2017-02-23 10:28:33 -0500104 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 Clark7eddfb82018-03-13 14:41:10 -0400109 auto flag = i ?
110 (SkCanvas::SaveLayerFlags) SkCanvasPriv::kDontClipToLayer_SaveLayerFlag : 0;
Florin Malita53f77bd2017-04-28 13:48:37 -0400111 canvas->saveLayer({ &rect1, &paint1, nullptr, nullptr, nullptr, flag});
112 canvas->saveLayer({ &rect2, &paint2, nullptr, nullptr, nullptr, flag});
Yuqian Lid196cbe2017-02-23 10:28:33 -0500113 canvas->drawRect(rect3, paint3);
114 canvas->restore();
115 canvas->restore();
116 }
117};
118
Mike Reedc61abee2017-02-28 17:45:27 -0500119// 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.
123DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) {
Hal Canaryc465d132017-12-08 10:21:31 -0500124 canvas->drawImage(GetResourceAsImage("images/mandrill_256.png"), 0, 0, nullptr);
Mike Reedc61abee2017-02-28 17:45:27 -0500125
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 Phillips42f30942017-01-17 10:48:52 -0500136
Florin Malita53f77bd2017-04-28 13:48:37 -0400137static 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 Reedfae8fce2019-04-03 10:27:45 -0400149 SkTileMode::kClamp));
Florin Malita53f77bd2017-04-28 13:48:37 -0400150 canvas->drawCircle(cx, cy, size / 4, paint);
151}
152
153DEF_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 Ludwig898bbfa2019-08-02 15:21:23 -0400203 SkPaint p; p.setImageFilter(SkImageFilters::Blur(2, 2, nullptr)); return p;
Florin Malita53f77bd2017-04-28 13:48:37 -0400204 },
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 Kleinb34ab042017-05-01 21:34:14 +0000219 sk_sp<SkImage> mask = maskMaker(kSize);
220 rec.fClipMask = mask.get();
Florin Malita53f77bd2017-04-28 13:48:37 -0400221
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 Reed910ca0f2018-04-25 13:04:05 -0400246
247DEF_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 Ludwigbeb7cd22019-04-08 11:11:42 -0400285DEF_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 Reedd5674082019-04-19 15:00:47 -0400342static void draw_cell(SkCanvas* canvas, sk_sp<SkTextBlob> blob, SkColor c, SkScalar w, SkScalar h,
343 bool useDrawBehind) {
Mike Reed148b7fd2018-12-18 17:38:18 -0500344 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 Reedfae8fce2019-04-03 10:27:45 -0400368 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
Mike Reed148b7fd2018-12-18 17:38:18 -0500369 p.setShader(sh);
370 p.setBlendMode(SkBlendMode::kDstIn);
Mike Reedd5674082019-04-19 15:00:47 -0400371
372 if (useDrawBehind) {
373 SkCanvasPriv::DrawBehind(canvas, p);
374 } else {
375 canvas->drawRect(r, p);
376 }
Mike Reed148b7fd2018-12-18 17:38:18 -0500377
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 Reed9407e242019-02-15 16:13:57 -0500386 p.setAlphaf(0.25f);
Mike Reed148b7fd2018-12-18 17:38:18 -0500387}
388
Mike Reedd5674082019-04-19 15:00:47 -0400389static void draw_list(SkCanvas* canvas, sk_sp<SkTextBlob> blob, bool useDrawBehind) {
Mike Reed148b7fd2018-12-18 17:38:18 -0500390 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 Reedd5674082019-04-19 15:00:47 -0400398 draw_cell(canvas, blob, c, w, h, useDrawBehind);
Mike Reed148b7fd2018-12-18 17:38:18 -0500399 canvas->translate(0, h);
400 }
401}
402
Mike Reedd5674082019-04-19 15:00:47 -0400403DEF_SIMPLE_GM(save_behind, canvas, 830, 670) {
Mike Reed148b7fd2018-12-18 17:38:18 -0500404 SkFont font;
Mike Reed2ce36402019-04-21 18:15:23 -0400405 font.setTypeface(ToolUtils::create_portable_typeface());
Mike Reed148b7fd2018-12-18 17:38:18 -0500406 font.setSize(30);
Mike Reed2ce36402019-04-21 18:15:23 -0400407
Mike Reed148b7fd2018-12-18 17:38:18 -0500408 const char text[] = "This is a very long line of text";
409 auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
410
Mike Reedd5674082019-04-19 15:00:47 -0400411 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 Reed148b7fd2018-12-18 17:38:18 -0500423}
Mike Reed1f3548c2019-07-12 12:53:11 -0400424
425#include "include/effects/SkGradientShader.h"
426
427DEF_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}