xref: /aosp_15_r20/external/skia/gm/savelayer.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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/gm.h"
9 #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/SkRSXform.h"
24 #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"
35 #include "include/core/SkVertices.h"
36 #include "include/effects/SkGradientShader.h"
37 #include "include/effects/SkImageFilters.h"
38 #include "include/effects/SkShaderMaskFilter.h"
39 #include "src/base/SkRandom.h"
40 #include "src/core/SkCanvasPriv.h"
41 #include "tools/DecodeUtils.h"
42 #include "tools/Resources.h"
43 #include "tools/ToolUtils.h"
44 #include "tools/fonts/FontToolUtils.h"
45 
46 #include <string.h>
47 #include <initializer_list>
48 
49 // 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.
53 DEF_SIMPLE_GM(savelayer_initfromprev, canvas, 256, 256) {
54     canvas->drawImage(ToolUtils::GetResourceAsImage("images/mandrill_256.png"), 0, 0);
55 
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 };
66 
draw_cell(SkCanvas * canvas,sk_sp<SkTextBlob> blob,SkColor c,SkScalar w,SkScalar h,bool useDrawBehind)67 static void draw_cell(SkCanvas* canvas, sk_sp<SkTextBlob> blob, SkColor c, SkScalar w, SkScalar h,
68                       bool useDrawBehind) {
69     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 };
93     auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
94     p.setShader(sh);
95     p.setBlendMode(SkBlendMode::kDstIn);
96 
97     if (useDrawBehind) {
98         SkCanvasPriv::DrawBehind(canvas, p);
99     } else {
100         canvas->drawRect(r, p);
101     }
102 
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);
111     p.setAlphaf(0.25f);
112 }
113 
draw_list(SkCanvas * canvas,sk_sp<SkTextBlob> blob,bool useDrawBehind)114 static void draw_list(SkCanvas* canvas, sk_sp<SkTextBlob> blob, bool useDrawBehind) {
115     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;
123         draw_cell(canvas, blob, c, w, h, useDrawBehind);
124         canvas->translate(0, h);
125     }
126 }
127 
128 DEF_SIMPLE_GM(save_behind, canvas, 830, 670) {
129     SkFont font = ToolUtils::DefaultPortableFont();
130     font.setSize(30);
131 
132     const char text[] = "This is a very long line of text";
133     auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
134 
135     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     }
147 }
148 
149 #include "include/effects/SkGradientShader.h"
150 
151 DEF_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(),
158                                                 colors, nullptr, std::size(colors)));
159 
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 }
177 
draw_atlas(SkCanvas * canvas,SkImage * image)178 static 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 
draw_vertices(SkCanvas * canvas,SkImage * image)195 static 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 
draw_points(SkCanvas * canvas,SkImage * image)206 static 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 
draw_image_set(SkCanvas * canvas,SkImage * image)216 static 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   */
235 DEF_SIMPLE_GM(skbug_14554, canvas, 310, 630) {
236     sk_sp<SkImage> image = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
237     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 }
263