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