Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
shadowutils.cpp
Go to the documentation of this file.
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"
17#include "include/core/SkRect.h"
23
24#include <initializer_list>
25
26using namespace skia_private;
27
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),
36 SkShadowUtils::DrawShadow(canvas, path, SkPoint3{ 0, 0, height}, lightPos, lightR,
37 ambientColor, spotColor, flags);
38}
39
40static constexpr int kW = 800;
41static constexpr int kH = 960;
42
48
49void 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));
56 paths.push_back(SkPath::Circle(25, 25, 25));
57 paths.push_back(SkPathBuilder().cubicTo(100, 50, 20, 100, 0, 0).detach());
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) {
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
133 paint.setAntiAlias(true);
134 if (kDebugColorNoOccluders == mode) {
135 // Draw the path outline in green on top of the ambient and spot shadows.
137 paint.setColor(SK_ColorCYAN);
138 } else {
139 paint.setColor(SK_ColorGREEN);
140 }
142 paint.setStrokeWidth(0);
143 } else {
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
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);
199 paint.setStrokeWidth(0);
200 } else {
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);
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
226DEF_SIMPLE_GM(shadow_utils, canvas, kW, kH) {
228}
229
230DEF_SIMPLE_GM(shadow_utils_occl, canvas, kW, kH) {
232}
233
234DEF_SIMPLE_GM(shadow_utils_gray, canvas, kW, kH) {
235 draw_paths(canvas, kGrayscale);
236}
237
240
241DEF_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),
248
249 SkPaint redPaint;
250 redPaint.setColor(SK_ColorRED);
251
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
263DEF_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
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,
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,
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,
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,
329
330 canvas->drawPath(rrect, paint);
331 canvas->restore();
332 }
333
334}
SkColor4f color
constexpr SkColor SK_ColorLTGRAY
Definition SkColor.h:118
#define SkColorGetR(color)
Definition SkColor.h:65
#define SkColorGetG(color)
Definition SkColor.h:69
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
#define SkColorGetA(color)
Definition SkColor.h:61
#define SkColorGetB(color)
Definition SkColor.h:73
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
@ kDirectionalLight_ShadowFlag
@ kTransparentOccluder_ShadowFlag
@ kNone_ShadowFlag
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
constexpr int kPad
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void rotate(SkScalar degrees)
SkMatrix getTotalMatrix() const
int save()
Definition SkCanvas.cpp:451
void drawPath(const SkPath &path, const SkPaint &paint)
void concat(const SkMatrix &matrix)
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
static sk_sp< SkColorFilter > MakeGaussian()
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static constexpr int kMPersp1
input y perspective factor
Definition SkMatrix.h:360
bool invert(SkMatrix *inverse) const
Definition SkMatrix.h:1206
void mapXY(SkScalar x, SkScalar y, SkPoint *result) const
Definition SkMatrix.cpp:777
static constexpr int kMPersp2
perspective bias
Definition SkMatrix.h:361
SkMatrix & reset()
Definition SkMatrix.cpp:49
void setColor(SkColor color)
Definition SkPaint.cpp:119
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
static SkPath RRect(const SkRRect &, SkPathDirection dir=SkPathDirection::kCW)
Definition SkPath.cpp:3534
static SkPath Rect(const SkRect &, SkPathDirection=SkPathDirection::kCW, unsigned startIndex=0)
Definition SkPath.cpp:3518
static SkPath Circle(SkScalar center_x, SkScalar center_y, SkScalar radius, SkPathDirection dir=SkPathDirection::kCW)
Definition SkPath.cpp:3530
static SkPath Oval(const SkRect &, SkPathDirection=SkPathDirection::kCW)
Definition SkPath.cpp:3522
void setNinePatch(const SkRect &rect, SkScalar leftRad, SkScalar topRad, SkScalar rightRad, SkScalar bottomRad)
Definition SkRRect.cpp:115
static void DrawShadow(SkCanvas *canvas, const SkPath &path, const SkPoint3 &zPlaneParams, const SkPoint3 &lightPos, SkScalar lightRadius, SkColor ambientColor, SkColor spotColor, uint32_t flags=SkShadowFlags::kNone_ShadowFlag)
T * append()
Definition SkTDArray.h:191
const Paint & paint
float SkScalar
Definition extension.cpp:12
FlutterSemanticsFlag flags
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
double x
SkScalar w
int32_t height
void draw_shadow(SkCanvas *canvas, const SkPath &path, SkScalar height, SkColor color, SkPoint3 lightPos, SkScalar lightR, bool isAmbient, uint32_t flags)
static constexpr int kH
ShadowMode
@ kDebugColorOccluders
@ kGrayscale
@ kDebugColorNoOccluders
void draw_paths(SkCanvas *canvas, ShadowMode mode)
static constexpr int kW
SkScalar fX
Definition SkPoint3.h:16
SkScalar fY
Definition SkPoint3.h:16
float fX
x-axis value
float fY
y-axis value
constexpr float centerX() const
Definition SkRect.h:776
constexpr float height() const
Definition SkRect.h:769
constexpr float centerY() const
Definition SkRect.h:785
constexpr float width() const
Definition SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646
constexpr size_t kHeight