xref: /aosp_15_r20/external/skia/gm/shadowutils.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/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPathBuilder.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkPoint3.h"
16 #include "include/core/SkRRect.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkTypes.h"
20 #include "include/private/base/SkTArray.h"
21 #include "include/private/base/SkTDArray.h"
22 #include "include/utils/SkShadowUtils.h"
23 
24 #include <initializer_list>
25 
26 using namespace skia_private;
27 
draw_shadow(SkCanvas * canvas,const SkPath & path,SkScalar height,SkColor color,SkPoint3 lightPos,SkScalar lightR,bool isAmbient,uint32_t flags)28 void draw_shadow(SkCanvas* canvas, const SkPath& path, SkScalar height, SkColor color,
29                  SkPoint3 lightPos, SkScalar lightR, bool isAmbient, uint32_t flags) {
30     SkScalar ambientAlpha = isAmbient ? .5f : 0.f;
31     SkScalar spotAlpha = isAmbient ? 0.f : .5f;
32     SkColor ambientColor = SkColorSetARGB(ambientAlpha*SkColorGetA(color), SkColorGetR(color),
33                                           SkColorGetG(color), SkColorGetB(color));
34     SkColor spotColor = SkColorSetARGB(spotAlpha*SkColorGetA(color), SkColorGetR(color),
35                                        SkColorGetG(color), SkColorGetB(color));
36     SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, height}, lightPos, lightR,
37                               ambientColor, spotColor, flags);
38 }
39 
40 static constexpr int kW = 800;
41 static constexpr int kH = 960;
42 
43 enum ShadowMode {
44     kDebugColorNoOccluders,
45     kDebugColorOccluders,
46     kGrayscale
47 };
48 
draw_paths(SkCanvas * canvas,ShadowMode mode)49 void draw_paths(SkCanvas* canvas, ShadowMode mode) {
50     TArray<SkPath> paths;
51     paths.push_back(SkPath::RRect(SkRect::MakeWH(50, 50), 10, 10.00002f));
52     SkRRect oddRRect;
53     oddRRect.setNinePatch(SkRect::MakeWH(50, 50), 9, 13, 6, 16);
54     paths.push_back(SkPath::RRect(oddRRect));
55     paths.push_back(SkPath::Rect(SkRect::MakeWH(50, 50)));
56     paths.push_back(SkPath::Circle(25, 25, 25));
57     paths.push_back(SkPathBuilder().cubicTo(100, 50, 20, 100, 0, 0).detach());
58     paths.push_back(SkPath::Oval(SkRect::MakeWH(20, 60)));
59 
60     // star
61     TArray<SkPath> concavePaths;
62     concavePaths.push_back().moveTo(0.0f, -33.3333f);
63     concavePaths.back().lineTo(9.62f, -16.6667f);
64     concavePaths.back().lineTo(28.867f, -16.6667f);
65     concavePaths.back().lineTo(19.24f, 0.0f);
66     concavePaths.back().lineTo(28.867f, 16.6667f);
67     concavePaths.back().lineTo(9.62f, 16.6667f);
68     concavePaths.back().lineTo(0.0f, 33.3333f);
69     concavePaths.back().lineTo(-9.62f, 16.6667f);
70     concavePaths.back().lineTo(-28.867f, 16.6667f);
71     concavePaths.back().lineTo(-19.24f, 0.0f);
72     concavePaths.back().lineTo(-28.867f, -16.6667f);
73     concavePaths.back().lineTo(-9.62f, -16.6667f);
74     concavePaths.back().close();
75 
76     // dumbbell
77     concavePaths.push_back().moveTo(50, 0);
78     concavePaths.back().cubicTo(100, 25, 60, 50, 50, 0);
79     concavePaths.back().cubicTo(0, -25, 40, -50, 50, 0);
80 
81     static constexpr SkScalar kPad = 15.f;
82     static constexpr SkScalar kLightR = 100.f;
83     static constexpr SkScalar kHeight = 50.f;
84 
85     // transform light position relative to canvas to handle tiling
86     SkPoint lightXY = canvas->getTotalMatrix().mapXY(250, 400);
87     SkPoint3 lightPos = { lightXY.fX, lightXY.fY, 500 };
88 
89     canvas->translate(3 * kPad, 3 * kPad);
90     canvas->save();
91     SkScalar x = 0;
92     SkScalar dy = 0;
93     SkTDArray<SkMatrix> matrices;
94     matrices.append()->reset();
95     matrices.append()->setRotate(33.f, 25.f, 25.f).postScale(1.2f, 0.8f, 25.f, 25.f);
96     for (auto& m : matrices) {
97         for (int flags : { kNone_ShadowFlag, kTransparentOccluder_ShadowFlag }) {
98             int pathCounter = 0;
99             for (const auto& path : paths) {
100                 SkRect postMBounds = path.getBounds();
101                 m.mapRect(&postMBounds);
102                 SkScalar w = postMBounds.width() + kHeight;
103                 SkScalar dx = w + kPad;
104                 if (x + dx > kW - 3 * kPad) {
105                     canvas->restore();
106                     canvas->translate(0, dy);
107                     canvas->save();
108                     x = 0;
109                     dy = 0;
110                 }
111 
112                 canvas->save();
113                 canvas->concat(m);
114 
115                 // flip a couple of paths to test 180° rotation
116                 if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) {
117                     canvas->save();
118                     canvas->rotate(180, 25, 25);
119                 }
120                 if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) {
121                     draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR,
122                                 true, flags);
123                     draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR,
124                                 false, flags);
125                 } else if (kGrayscale == mode) {
126                     SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0);
127                     SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0);
128                     SkShadowUtils::DrawShadow(canvas, path, SkPoint3{0, 0, kHeight}, lightPos,
129                                               kLightR, ambientColor, spotColor, flags);
130                 }
131 
132                 SkPaint paint;
133                 paint.setAntiAlias(true);
134                 if (kDebugColorNoOccluders == mode) {
135                     // Draw the path outline in green on top of the ambient and spot shadows.
136                     if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
137                         paint.setColor(SK_ColorCYAN);
138                     } else {
139                         paint.setColor(SK_ColorGREEN);
140                     }
141                     paint.setStyle(SkPaint::kStroke_Style);
142                     paint.setStrokeWidth(0);
143                 } else {
144                     paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE);
145                     if (SkToBool(flags & kTransparentOccluder_ShadowFlag)) {
146                         paint.setAlphaf(0.5f);
147                     }
148                     paint.setStyle(SkPaint::kFill_Style);
149                 }
150                 canvas->drawPath(path, paint);
151                 if (kTransparentOccluder_ShadowFlag == flags && 0 == pathCounter % 3) {
152                     canvas->restore();
153                 }
154                 canvas->restore();
155 
156                 canvas->translate(dx, 0);
157                 x += dx;
158                 dy = std::max(dy, postMBounds.height() + kPad + kHeight);
159                 ++pathCounter;
160             }
161         }
162     }
163 
164     // concave paths
165     canvas->restore();
166     canvas->translate(kPad, dy);
167     canvas->save();
168     x = kPad;
169     dy = 0;
170     for (auto& m : matrices) {
171         // for the concave paths we are not clipping, so transparent and opaque are the same
172         for (const auto& path : concavePaths) {
173             SkRect postMBounds = path.getBounds();
174             m.mapRect(&postMBounds);
175             SkScalar w = postMBounds.width() + kHeight;
176             SkScalar dx = w + kPad;
177 
178             canvas->save();
179             canvas->concat(m);
180 
181             if (kDebugColorNoOccluders == mode || kDebugColorOccluders == mode) {
182                 draw_shadow(canvas, path, kHeight, SK_ColorRED, lightPos, kLightR,
183                             true, kNone_ShadowFlag);
184                 draw_shadow(canvas, path, kHeight, SK_ColorBLUE, lightPos, kLightR,
185                             false, kNone_ShadowFlag);
186             } else if (kGrayscale == mode) {
187                 SkColor ambientColor = SkColorSetARGB(0.1f * 255, 0, 0, 0);
188                 SkColor spotColor = SkColorSetARGB(0.25f * 255, 0, 0, 0);
189                 SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, kHeight }, lightPos,
190                                           kLightR, ambientColor, spotColor, kNone_ShadowFlag);
191             }
192 
193             SkPaint paint;
194             paint.setAntiAlias(true);
195             if (kDebugColorNoOccluders == mode) {
196                 // Draw the path outline in green on top of the ambient and spot shadows.
197                 paint.setColor(SK_ColorGREEN);
198                 paint.setStyle(SkPaint::kStroke_Style);
199                 paint.setStrokeWidth(0);
200             } else {
201                 paint.setColor(kDebugColorOccluders == mode ? SK_ColorLTGRAY : SK_ColorWHITE);
202                 paint.setStyle(SkPaint::kFill_Style);
203             }
204             canvas->drawPath(path, paint);
205             canvas->restore();
206 
207             canvas->translate(dx, 0);
208             x += dx;
209             dy = std::max(dy, postMBounds.height() + kPad + kHeight);
210         }
211     }
212 
213     // Show where the light is in x,y as a circle (specified in device space).
214     SkMatrix invCanvasM = canvas->getTotalMatrix();
215     if (invCanvasM.invert(&invCanvasM)) {
216         canvas->save();
217         canvas->concat(invCanvasM);
218         SkPaint paint;
219         paint.setColor(SK_ColorBLACK);
220         paint.setAntiAlias(true);
221         canvas->drawCircle(lightPos.fX, lightPos.fY, kLightR / 10.f, paint);
222         canvas->restore();
223     }
224 }
225 
DEF_SIMPLE_GM(shadow_utils,canvas,kW,kH)226 DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) {
227     draw_paths(canvas, kDebugColorNoOccluders);
228 }
229 
DEF_SIMPLE_GM(shadow_utils_occl,canvas,kW,kH)230 DEF_SIMPLE_GM(shadow_utils_occl, canvas, kW, kH) {
231     draw_paths(canvas, kDebugColorOccluders);
232 }
233 
DEF_SIMPLE_GM(shadow_utils_gray,canvas,kW,kH)234 DEF_SIMPLE_GM(shadow_utils_gray, canvas, kW, kH) {
235     draw_paths(canvas, kGrayscale);
236 }
237 
238 #include "include/effects/SkGradientShader.h"
239 #include "src/core/SkColorFilterPriv.h"
240 
241 DEF_SIMPLE_GM(shadow_utils_gaussian_colorfilter, canvas, 512, 256) {
242     const SkRect r = SkRect::MakeWH(256, 256);
243 
244     const SkColor colors[] = { 0, 0xFF000000 };
245     auto sh = SkGradientShader::MakeRadial({r.centerX(), r.centerY()}, r.width(),
246                                            colors, nullptr, std::size(colors),
247                                            SkTileMode::kClamp);
248 
249     SkPaint redPaint;
250     redPaint.setColor(SK_ColorRED);
251 
252     SkPaint paint;
253     paint.setShader(sh);
254     canvas->drawRect(r, redPaint);
255     canvas->drawRect(r, paint);
256 
257     canvas->translate(256, 0);
258     paint.setColorFilter(SkColorFilterPriv::MakeGaussian());
259     canvas->drawRect(r, redPaint);
260     canvas->drawRect(r, paint);
261 }
262 
263 DEF_SIMPLE_GM(shadow_utils_directional, canvas, 256, 384) {
264     static constexpr SkScalar kLightR = 1.f;
265     static constexpr SkScalar kHeight = 12.f;
266 
267     SkPath rrect(SkPath::RRect(SkRect::MakeLTRB(-25, -25, 25, 25), 10, 10));
268     SkPoint3 lightPos = { -45, -45, 77.9422863406f };
269 
270     SkColor ambientColor = SkColorSetARGB(0.02f * 255, 0, 0, 0);
271     SkColor spotColor = SkColorSetARGB(0.35f * 255, 0, 0, 0);
272 
273     SkPaint paint;
274     paint.setAntiAlias(true);
275     paint.setColor(SK_ColorWHITE);
276     paint.setStyle(SkPaint::kFill_Style);
277 
278     // translation
279     canvas->save();
280     canvas->translate(35, 35);
281     for (int i = 0; i < 3; ++i) {
282         SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos,
283                                   kLightR, ambientColor, spotColor,
284                                   kDirectionalLight_ShadowFlag);
285         canvas->drawPath(rrect, paint);
286         canvas->translate(80, 0);
287     }
288     canvas->restore();
289 
290     // rotation
291     for (int i = 0; i < 3; ++i) {
292         canvas->save();
293         canvas->translate(35 + 80*i, 105);
294         canvas->rotate(20.f*(i + 1));
295         SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos,
296                                   kLightR, ambientColor, spotColor,
297                                   kDirectionalLight_ShadowFlag);
298 
299         canvas->drawPath(rrect, paint);
300         canvas->restore();
301     }
302 
303     // scale
304     for (int i = 0; i < 3; ++i) {
305         canvas->save();
306         SkScalar scaleFactor = std::pow(2.0, -i);
307         canvas->translate(35 + 80*i, 185);
308         canvas->scale(scaleFactor, scaleFactor);
309         SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos,
310                                   kLightR, ambientColor, spotColor,
311                                   kDirectionalLight_ShadowFlag);
312 
313         canvas->drawPath(rrect, paint);
314         canvas->restore();
315     }
316 
317     // perspective
318     for (int i = 0; i < 3; ++i) {
319         canvas->save();
320         SkMatrix mat;
321         mat.reset();
322         mat[SkMatrix::kMPersp1] = 0.005f;
323         mat[SkMatrix::kMPersp2] = 1.005f;
324         canvas->translate(35 + 80*i, 265);
325         canvas->concat(mat);
326         SkShadowUtils::DrawShadow(canvas, rrect, SkPoint3{ 0, 0, kHeight }, lightPos,
327                                   kLightR, ambientColor, spotColor,
328                                   kDirectionalLight_ShadowFlag);
329 
330         canvas->drawPath(rrect, paint);
331         canvas->restore();
332     }
333 
334 }
335