blob: 90ba253c21729f6cfdbc2f1b461de126e498c6b9 [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"
Brian Osmana39421e2023-07-05 12:42:01 -040023#include "include/core/SkRSXform.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "include/core/SkRect.h"
25#include "include/core/SkRefCnt.h"
26#include "include/core/SkScalar.h"
27#include "include/core/SkShader.h"
28#include "include/core/SkSize.h"
29#include "include/core/SkString.h"
30#include "include/core/SkSurface.h"
31#include "include/core/SkTextBlob.h"
32#include "include/core/SkTileMode.h"
33#include "include/core/SkTypeface.h"
34#include "include/core/SkTypes.h"
Brian Osmana39421e2023-07-05 12:42:01 -040035#include "include/core/SkVertices.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040036#include "include/effects/SkGradientShader.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040037#include "include/effects/SkImageFilters.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "include/effects/SkShaderMaskFilter.h"
Kevin Lubick0d4d1142023-02-13 09:13:10 -050039#include "src/base/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/core/SkCanvasPriv.h"
Kevin Lubick8b741882023-10-06 11:41:38 -040041#include "tools/DecodeUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040042#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040044#include "tools/fonts/FontToolUtils.h"
Yuqian Lid196cbe2017-02-23 10:28:33 -050045
Ben Wagner7fde8e12019-05-01 17:28:53 -040046#include <string.h>
47#include <initializer_list>
48
Mike Reedc61abee2017-02-28 17:45:27 -050049// Test kInitWithPrevious_SaveLayerFlag by drawing an image, save a layer with the flag, which
50// should seed the layer with the image (from below). Then we punch a hole in the layer and
51// restore with kPlus mode, which should show the mandrill super-bright on the outside, but
52// normal where we punched the hole.
53DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) {
Kevin Lubick8b741882023-10-06 11:41:38 -040054 canvas->drawImage(ToolUtils::GetResourceAsImage("images/mandrill_256.png"), 0, 0);
Mike Reedc61abee2017-02-28 17:45:27 -050055
56 SkCanvas::SaveLayerRec rec;
57 SkPaint paint;
58 paint.setBlendMode(SkBlendMode::kPlus);
59 rec.fSaveLayerFlags = SkCanvas::kInitWithPrevious_SaveLayerFlag;
60 rec.fPaint = &paint;
61 canvas->saveLayer(rec);
62 paint.setBlendMode(SkBlendMode::kClear);
63 canvas->drawCircle(128, 128, 96, paint);
64 canvas->restore();
65};
Robert Phillips42f30942017-01-17 10:48:52 -050066
Mike Reedd5674082019-04-19 15:00:47 -040067static void draw_cell(SkCanvas* canvas, sk_sp<SkTextBlob> blob, SkColor c, SkScalar w, SkScalar h,
68 bool useDrawBehind) {
Mike Reed148b7fd2018-12-18 17:38:18 -050069 SkRect r = SkRect::MakeWH(w, h);
70 SkPaint p;
71 p.setColor(c);
72 p.setBlendMode(SkBlendMode::kSrc);
73 canvas->drawRect(r, p);
74 p.setBlendMode(SkBlendMode::kSrcOver);
75
76 const SkScalar margin = 80;
77 r.fLeft = w - margin;
78
79 // save the behind image
80 SkDEBUGCODE(int sc0 =) canvas->getSaveCount();
81 SkDEBUGCODE(int sc1 =) SkCanvasPriv::SaveBehind(canvas, &r);
82 SkDEBUGCODE(int sc2 =) canvas->getSaveCount();
83 SkASSERT(sc0 == sc1);
84 SkASSERT(sc0 + 1 == sc2);
85
86 // draw the foreground (including over the 'behind' section)
87 p.setColor(SK_ColorBLACK);
88 canvas->drawTextBlob(blob, 10, 30, p);
89
90 // draw the treatment
91 const SkPoint pts[] = { {r.fLeft,0}, {r.fRight, 0} };
92 const SkColor colors[] = { 0x88000000, 0x0 };
Mike Reedfae8fce2019-04-03 10:27:45 -040093 auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
Mike Reed148b7fd2018-12-18 17:38:18 -050094 p.setShader(sh);
95 p.setBlendMode(SkBlendMode::kDstIn);
Mike Reedd5674082019-04-19 15:00:47 -040096
97 if (useDrawBehind) {
98 SkCanvasPriv::DrawBehind(canvas, p);
99 } else {
100 canvas->drawRect(r, p);
101 }
Mike Reed148b7fd2018-12-18 17:38:18 -0500102
103 // this should restore the behind image
104 canvas->restore();
105 SkDEBUGCODE(int sc3 =) canvas->getSaveCount();
106 SkASSERT(sc3 == sc0);
107
108 // just outline where we expect the treatment to appear
109 p.reset();
110 p.setStyle(SkPaint::kStroke_Style);
Mike Reed9407e242019-02-15 16:13:57 -0500111 p.setAlphaf(0.25f);
Mike Reed148b7fd2018-12-18 17:38:18 -0500112}
113
Mike Reedd5674082019-04-19 15:00:47 -0400114static void draw_list(SkCanvas* canvas, sk_sp<SkTextBlob> blob, bool useDrawBehind) {
Mike Reed148b7fd2018-12-18 17:38:18 -0500115 SkAutoCanvasRestore acr(canvas, true);
116
117 SkRandom rand;
118 SkScalar w = 400;
119 SkScalar h = 40;
120 for (int i = 0; i < 8; ++i) {
121 SkColor c = rand.nextU(); // ensure we're opaque
122 c = (c & 0xFFFFFF) | 0x80000000;
Mike Reedd5674082019-04-19 15:00:47 -0400123 draw_cell(canvas, blob, c, w, h, useDrawBehind);
Mike Reed148b7fd2018-12-18 17:38:18 -0500124 canvas->translate(0, h);
125 }
126}
127
Mike Reedd5674082019-04-19 15:00:47 -0400128DEF_SIMPLE_GM(save_behind, canvas, 830, 670) {
Kevin Lubickbca43ec2023-10-30 10:11:22 -0400129 SkFont font = ToolUtils::DefaultPortableFont();
Mike Reed148b7fd2018-12-18 17:38:18 -0500130 font.setSize(30);
Mike Reed2ce36402019-04-21 18:15:23 -0400131
Mike Reed148b7fd2018-12-18 17:38:18 -0500132 const char text[] = "This is a very long line of text";
133 auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
134
Mike Reedd5674082019-04-19 15:00:47 -0400135 for (bool useDrawBehind : {false, true}) {
136 canvas->save();
137
138 draw_list(canvas, blob, useDrawBehind);
139 canvas->translate(0, 350);
140 canvas->saveLayer({0, 0, 400, 320}, nullptr);
141 draw_list(canvas, blob, useDrawBehind);
142 canvas->restore();
143
144 canvas->restore();
145 canvas->translate(430, 0);
146 }
Mike Reed148b7fd2018-12-18 17:38:18 -0500147}
Mike Reed1f3548c2019-07-12 12:53:11 -0400148
149#include "include/effects/SkGradientShader.h"
150
151DEF_SIMPLE_GM(savelayer_f16, canvas, 900, 300) {
152 int n = 15;
153 SkRect r{0, 0, 300, 300};
154 SkPaint paint;
155
156 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
157 paint.setShader(SkGradientShader::MakeSweep(r.centerX(), r.centerY(),
Herb Derbyc37b3862022-06-21 09:49:17 -0400158 colors, nullptr, std::size(colors)));
Mike Reed1f3548c2019-07-12 12:53:11 -0400159
160 canvas->drawOval(r, paint);
161
162 paint.setAlphaf(1.0f/n);
163 paint.setBlendMode(SkBlendMode::kPlus);
164
165 for (auto flags : {0, (int)SkCanvas::kF16ColorType}) {
166 canvas->translate(r.width(), 0);
167
168 SkCanvas::SaveLayerRec rec;
169 rec.fSaveLayerFlags = flags;
170 canvas->saveLayer(rec);
171 for (int i = 0; i < n; ++i) {
172 canvas->drawOval(r, paint);
173 }
174 canvas->restore();
175 }
176}
Brian Osmana39421e2023-07-05 12:42:01 -0400177
178static void draw_atlas(SkCanvas* canvas, SkImage* image) {
179 SkRSXform xforms[] = {{1, 0, 0, 0}, {1, 0, 50, 50}};
180 SkRect tex[] = {{0, 0, 100, 100}, {0, 0, 100, 100}};
181 SkColor colors[] = {0xffffffff, 0xffffffff};
182 SkPaint paint;
183
184 canvas->drawAtlas(image,
185 xforms,
186 tex,
187 colors,
188 2,
189 SkBlendMode::kSrcIn,
190 SkFilterMode::kNearest,
191 nullptr,
192 &paint);
193}
194
195static void draw_vertices(SkCanvas* canvas, SkImage* image) {
196 SkPoint pts[] = {{0, 0}, {0, 100}, {100, 100}, {100, 0}, {100, 100}, {0, 100}};
197 sk_sp<SkVertices> verts =
198 SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 6, pts, nullptr, nullptr);
199
200 SkPaint paint;
201 paint.setShader(image->makeShader(SkFilterMode::kNearest));
202
203 canvas->drawVertices(verts, SkBlendMode::kSrc, paint);
204}
205
206static void draw_points(SkCanvas* canvas, SkImage* image) {
207 SkPoint pts[] = {{50, 50}, {75, 75}};
208 SkPaint paint;
209 paint.setShader(image->makeShader(SkFilterMode::kNearest));
210 paint.setStrokeWidth(100);
211 paint.setStrokeCap(SkPaint::kSquare_Cap);
212
213 canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, pts, paint);
214}
215
216static void draw_image_set(SkCanvas* canvas, SkImage* image) {
217 SkRect r = SkRect::MakeWH(100, 100);
218 SkCanvas::ImageSetEntry entries[] = {
219 SkCanvas::ImageSetEntry(sk_ref_sp(image), r, r, 1.0f, SkCanvas::kNone_QuadAAFlags),
220 SkCanvas::ImageSetEntry(
221 sk_ref_sp(image), r, r.makeOffset(50, 50), 1.0f, SkCanvas::kNone_QuadAAFlags),
222 };
223
224 SkPaint paint;
225 canvas->experimental_DrawEdgeAAImageSet(
226 entries, 2, nullptr, nullptr, SkFilterMode::kNearest, &paint);
227}
228
229/*
230 Picture optimization looks for single drawing operations inside a saveLayer with alpha. It tries
231 to push the alpha into the drawing operation itself. That's only valid if the draw logically
232 touches each pixel once. A handful of draws do not behave like that. They instead act like
233 repeated, independent draws. This GM tests this with several operations.
234 */
235DEF_SIMPLE_GM(skbug_14554, canvas, 310, 630) {
Kevin Lubick8b741882023-10-06 11:41:38 -0400236 sk_sp<SkImage> image = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
Brian Osmana39421e2023-07-05 12:42:01 -0400237 SkPictureRecorder rec;
238
239 using DrawProc = void(*)(SkCanvas*, SkImage*);
240
241 for (DrawProc proc : {draw_atlas, draw_vertices, draw_points, draw_image_set}) {
242 canvas->save();
243 for (bool injectExtraOp : {false, true}) {
244 auto c = rec.beginRecording(SkRect::MakeWH(150, 150));
245 c->saveLayerAlphaf(nullptr, 0.6f);
246 proc(c, image.get());
247 // For the second draw of each test-case, we inject an extra (useless) operation, which
248 // inhibits the optimization and produces the correct result.
249 if (injectExtraOp) {
250 c->translate(1, 0);
251 }
252 c->restore();
253
254 auto pic = rec.finishRecordingAsPicture();
255
256 canvas->drawPicture(pic);
257 canvas->translate(160, 0);
258 }
259 canvas->restore();
260 canvas->translate(0, 160);
261 }
262}