blob: dbb667699a94961d08162f9206991f86e353db54 [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
Mike Reedc61abee2017-02-28 17:45:27 -050045// Test kInitWithPrevious_SaveLayerFlag by drawing an image, save a layer with the flag, which
46// should seed the layer with the image (from below). Then we punch a hole in the layer and
47// restore with kPlus mode, which should show the mandrill super-bright on the outside, but
48// normal where we punched the hole.
49DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) {
Mike Reed8d29ab62021-01-23 18:10:39 -050050 canvas->drawImage(GetResourceAsImage("images/mandrill_256.png"), 0, 0);
Mike Reedc61abee2017-02-28 17:45:27 -050051
52 SkCanvas::SaveLayerRec rec;
53 SkPaint paint;
54 paint.setBlendMode(SkBlendMode::kPlus);
55 rec.fSaveLayerFlags = SkCanvas::kInitWithPrevious_SaveLayerFlag;
56 rec.fPaint = &paint;
57 canvas->saveLayer(rec);
58 paint.setBlendMode(SkBlendMode::kClear);
59 canvas->drawCircle(128, 128, 96, paint);
60 canvas->restore();
61};
Robert Phillips42f30942017-01-17 10:48:52 -050062
Mike Reed910ca0f2018-04-25 13:04:05 -040063DEF_SIMPLE_GM(savelayer_coverage, canvas, 500, 500) {
64 canvas->saveLayer(nullptr, nullptr);
65
66 SkRect r = { 0, 0, 200, 200 };
67 SkPaint layerPaint;
68 layerPaint.setBlendMode(SkBlendMode::kModulate);
69
70 auto image = GetResourceAsImage("images/mandrill_128.png");
71
72 auto proc = [layerPaint](SkCanvas* canvas, SkCanvas::SaveLayerRec& rec) {
73 SkPaint paint;
74 paint.setColor(SK_ColorRED);
75
76 canvas->saveLayer(rec);
77 canvas->drawCircle(100, 100, 50, paint);
78 paint.setColor(0x8800FF00);
79 canvas->drawRect({10, 90, 190, 110}, paint);
80 canvas->restore();
81 };
82
83 const int yflags[] = { 0, SkCanvas::kInitWithPrevious_SaveLayerFlag };
84 for (int y = 0; y <= 1; ++y) {
85 const int xflags[] = { 0, SkCanvas::kMaskAgainstCoverage_EXPERIMENTAL_DONT_USE_SaveLayerFlag };
86 for (int x = 0; x <= 1; ++x) {
87 canvas->save();
88 canvas->translate(x * 200.f, y * 200.f);
89
90 SkCanvas::SaveLayerRec rec(&r, &layerPaint, yflags[y] | xflags[x]);
Mike Reed8d29ab62021-01-23 18:10:39 -050091 canvas->drawImageRect(image, r, SkSamplingOptions(), nullptr);
Mike Reed910ca0f2018-04-25 13:04:05 -040092 proc(canvas, rec);
93
94 canvas->restore();
95 }
96 }
97
98 canvas->restore();
99}
100
Mike Reedd5674082019-04-19 15:00:47 -0400101static void draw_cell(SkCanvas* canvas, sk_sp<SkTextBlob> blob, SkColor c, SkScalar w, SkScalar h,
102 bool useDrawBehind) {
Mike Reed148b7fd2018-12-18 17:38:18 -0500103 SkRect r = SkRect::MakeWH(w, h);
104 SkPaint p;
105 p.setColor(c);
106 p.setBlendMode(SkBlendMode::kSrc);
107 canvas->drawRect(r, p);
108 p.setBlendMode(SkBlendMode::kSrcOver);
109
110 const SkScalar margin = 80;
111 r.fLeft = w - margin;
112
113 // save the behind image
114 SkDEBUGCODE(int sc0 =) canvas->getSaveCount();
115 SkDEBUGCODE(int sc1 =) SkCanvasPriv::SaveBehind(canvas, &r);
116 SkDEBUGCODE(int sc2 =) canvas->getSaveCount();
117 SkASSERT(sc0 == sc1);
118 SkASSERT(sc0 + 1 == sc2);
119
120 // draw the foreground (including over the 'behind' section)
121 p.setColor(SK_ColorBLACK);
122 canvas->drawTextBlob(blob, 10, 30, p);
123
124 // draw the treatment
125 const SkPoint pts[] = { {r.fLeft,0}, {r.fRight, 0} };
126 const SkColor colors[] = { 0x88000000, 0x0 };
Mike Reedfae8fce2019-04-03 10:27:45 -0400127 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
Mike Reed148b7fd2018-12-18 17:38:18 -0500128 p.setShader(sh);
129 p.setBlendMode(SkBlendMode::kDstIn);
Mike Reedd5674082019-04-19 15:00:47 -0400130
131 if (useDrawBehind) {
132 SkCanvasPriv::DrawBehind(canvas, p);
133 } else {
134 canvas->drawRect(r, p);
135 }
Mike Reed148b7fd2018-12-18 17:38:18 -0500136
137 // this should restore the behind image
138 canvas->restore();
139 SkDEBUGCODE(int sc3 =) canvas->getSaveCount();
140 SkASSERT(sc3 == sc0);
141
142 // just outline where we expect the treatment to appear
143 p.reset();
144 p.setStyle(SkPaint::kStroke_Style);
Mike Reed9407e242019-02-15 16:13:57 -0500145 p.setAlphaf(0.25f);
Mike Reed148b7fd2018-12-18 17:38:18 -0500146}
147
Mike Reedd5674082019-04-19 15:00:47 -0400148static void draw_list(SkCanvas* canvas, sk_sp<SkTextBlob> blob, bool useDrawBehind) {
Mike Reed148b7fd2018-12-18 17:38:18 -0500149 SkAutoCanvasRestore acr(canvas, true);
150
151 SkRandom rand;
152 SkScalar w = 400;
153 SkScalar h = 40;
154 for (int i = 0; i < 8; ++i) {
155 SkColor c = rand.nextU(); // ensure we're opaque
156 c = (c & 0xFFFFFF) | 0x80000000;
Mike Reedd5674082019-04-19 15:00:47 -0400157 draw_cell(canvas, blob, c, w, h, useDrawBehind);
Mike Reed148b7fd2018-12-18 17:38:18 -0500158 canvas->translate(0, h);
159 }
160}
161
Mike Reedd5674082019-04-19 15:00:47 -0400162DEF_SIMPLE_GM(save_behind, canvas, 830, 670) {
Mike Reed148b7fd2018-12-18 17:38:18 -0500163 SkFont font;
Mike Reed2ce36402019-04-21 18:15:23 -0400164 font.setTypeface(ToolUtils::create_portable_typeface());
Mike Reed148b7fd2018-12-18 17:38:18 -0500165 font.setSize(30);
Mike Reed2ce36402019-04-21 18:15:23 -0400166
Mike Reed148b7fd2018-12-18 17:38:18 -0500167 const char text[] = "This is a very long line of text";
168 auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
169
Mike Reedd5674082019-04-19 15:00:47 -0400170 for (bool useDrawBehind : {false, true}) {
171 canvas->save();
172
173 draw_list(canvas, blob, useDrawBehind);
174 canvas->translate(0, 350);
175 canvas->saveLayer({0, 0, 400, 320}, nullptr);
176 draw_list(canvas, blob, useDrawBehind);
177 canvas->restore();
178
179 canvas->restore();
180 canvas->translate(430, 0);
181 }
Mike Reed148b7fd2018-12-18 17:38:18 -0500182}
Mike Reed1f3548c2019-07-12 12:53:11 -0400183
184#include "include/effects/SkGradientShader.h"
185
186DEF_SIMPLE_GM(savelayer_f16, canvas, 900, 300) {
187 int n = 15;
188 SkRect r{0, 0, 300, 300};
189 SkPaint paint;
190
191 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
192 paint.setShader(SkGradientShader::MakeSweep(r.centerX(), r.centerY(),
193 colors, nullptr, SK_ARRAY_COUNT(colors)));
194
195 canvas->drawOval(r, paint);
196
197 paint.setAlphaf(1.0f/n);
198 paint.setBlendMode(SkBlendMode::kPlus);
199
200 for (auto flags : {0, (int)SkCanvas::kF16ColorType}) {
201 canvas->translate(r.width(), 0);
202
203 SkCanvas::SaveLayerRec rec;
204 rec.fSaveLayerFlags = flags;
205 canvas->saveLayer(rec);
206 for (int i = 0; i < n; ++i) {
207 canvas->drawOval(r, paint);
208 }
209 canvas->restore();
210 }
211}