Debian Codegraph @

tools/viewer/AndroidShadowsSlide.cpp

raw
1
2 /*
3 * Copyright 2016 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkColorFilter.h"
10 #include "include/core/SkPath.h"
11 #include "include/core/SkPathBuilder.h"
12 #include "include/core/SkPoint3.h"
13 #include "include/core/SkRRect.h"
14 #include "include/pathops/SkPathOps.h"
15 #include "include/utils/SkCamera.h"
16 #include "include/utils/SkShadowUtils.h"
17 #include "src/base/SkUTF.h"
18 #include "src/core/SkBlurMask.h"
19 #include "tools/ToolUtils.h"
20 #include "tools/timer/TimeUtils.h"
21 #include "tools/viewer/Slide.h"
22
23 ////////////////////////////////////////////////////////////////////////////
24
25 class ShadowsSlide : public Slide {
26 SkPath fRectPath;
27 SkPath fRRPath;
28 SkPath fCirclePath;
29 SkPath fFunkyRRPath;
30 SkPath fCubicPath;
31 SkPath fStarPath;
32 SkPath fSquareRRectPath;
33 SkPath fWideRectPath;
34 SkPath fWideOvalPath;
35 SkPath fNotchPath;
36 SkPath fTabPath;
37
38 SkPoint3 fLightPos;
39 SkScalar fZDelta = 0;
40 SkScalar fAnimTranslate = 0;
41 SkScalar fAnimAngle = 0;
42 SkScalar fAnimAlpha = 1;
43
44 bool fShowAmbient = true;
45 bool fShowSpot = true;
46 bool fUseAlt = false;
47 bool fUseBlur = true;
48 bool fShowObject = true;
49 bool fIgnoreShadowAlpha = false;
50 bool fDoAlphaAnimation = false;
51
52 public:
53 ShadowsSlide() { fName = "AndroidShadows"; }
54
55 void load(SkScalar w, SkScalar h) override {
56 fCirclePath = SkPath::Circle(0, 0, 50);
57 fRectPath = SkPath::Rect(SkRect::MakeXYWH(-100, -50, 200, 100));
58 fRRPath = SkPath::RRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-100, -50, 200, 100), 4, 4));
59 fFunkyRRPath = SkPath::RRect(SkRect::MakeXYWH(-50, -50, SK_Scalar1 * 100, SK_Scalar1 * 100),
60 40 * SK_Scalar1, 20 * SK_Scalar1,
61 SkPathDirection::kCW);
62 fCubicPath = SkPathBuilder()
63 .cubicTo(100, 50, 20, 100, 0, 0)
64 .detach();
65 fStarPath = SkPathBuilder()
66 .moveTo(0.0f, -50.0f)
67 .lineTo(14.43f, -25.0f)
68 .lineTo(43.30f, -25.0f)
69 .lineTo(28.86f, 0.0f)
70 .lineTo(43.30f, 25.0f)
71 .lineTo(14.43f, 25.0f)
72 .lineTo(0.0f, 50.0f)
73 .lineTo(-14.43f, 25.0f)
74 .lineTo(-43.30f, 25.0f)
75 .lineTo(-28.86f, 0.0f)
76 .lineTo(-43.30f, -25.0f)
77 .lineTo(-14.43f, -25.0f)
78 .detach();
79 fSquareRRectPath = SkPath::RRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-50, -50, 100, 100),
80 10, 10));
81 fWideRectPath = SkPath::Rect(SkRect::MakeXYWH(0, 0, 630, 70));
82 fWideOvalPath = SkPath::Oval(SkRect::MakeXYWH(0, 0, 630, 70));
83
84 fNotchPath = SkPathBuilder()
85 .moveTo(0, 80)
86 .arcTo(SkRect::MakeLTRB(-20, 80, 20, 120), -90, -90, false)
87 .lineTo(-75, 100)
88 .lineTo(-75, -100)
89 .lineTo(75, -100)
90 .lineTo(75, 100)
91 .arcTo(SkRect::MakeLTRB(-20, 80, 20, 120), 0, -90, false)
92 .detach();
93
94 fTabPath = SkPathBuilder()
95 .moveTo(-75, -100)
96 .lineTo(75, -100)
97 .lineTo(75, 100)
98 .arcTo(SkRect::MakeLTRB(-20, 80, 20, 120), 0, 90, false)
99 .arcTo(SkRect::MakeLTRB(-20, 80, 20, 120), 90, 90, false)
100 .lineTo(-75, 100)
101 .detach();
102
103 fLightPos = SkPoint3::Make(350, 0, 600);
104 }
105
106 bool onChar(SkUnichar uni) override {
107 bool handled = false;
108 switch (uni) {
109 case 'W':
110 fShowAmbient = !fShowAmbient;
111 handled = true;
112 break;
113 case 'S':
114 fShowSpot = !fShowSpot;
115 handled = true;
116 break;
117 case 'T':
118 fUseAlt = !fUseAlt;
119 handled = true;
120 break;
121 case 'B':
122 fUseBlur = !fUseBlur;
123 handled = true;
124 break;
125 case 'O':
126 fShowObject = !fShowObject;
127 handled = true;
128 break;
129 case 'N':
130 fDoAlphaAnimation = !fDoAlphaAnimation;
131 if (!fDoAlphaAnimation) {
132 fAnimAlpha = 1;
133 }
134 handled = true;
135 break;
136 case '>':
137 fZDelta += 0.5f;
138 handled = true;
139 break;
140 case '<':
141 fZDelta -= 0.5f;
142 handled = true;
143 break;
144 case '?':
145 fIgnoreShadowAlpha = !fIgnoreShadowAlpha;
146 handled = true;
147 break;
148 default:
149 break;
150 }
151 if (handled) {
152 return true;
153 }
154 return false;
155 }
156
157
158 bool animate(double nanos) override {
159 fAnimTranslate = TimeUtils::PingPong(1e-9 * nanos, 30, 0, 125, -125);
160 fAnimAngle = TimeUtils::PingPong(1e-9 * nanos, 15, 0, 0, 20);
161 if (fDoAlphaAnimation) {
162 fAnimAlpha = TimeUtils::PingPong(1e-9 * nanos, 5, 0, 1, 0);
163 }
164 return true;
165 }
166
167 void draw(SkCanvas* canvas) override {
168 canvas->drawColor(0xFFDDDDDD);
169
170 const SkScalar kLightWidth = 800;
171 const SkScalar kAmbientAlpha = 0.039f;
172 const SkScalar kSpotAlpha = 0.19f;
173
174 SkPaint paint;
175 paint.setAntiAlias(true);
176
177 SkPoint3 lightPos = fLightPos;
178 SkPoint3 zPlaneParams = SkPoint3::Make(0, 0, 0);
179
180 paint.setColor(SK_ColorWHITE);
181 canvas->translate(200, 90);
182 zPlaneParams.fZ = std::max(1.0f, 2 + fZDelta);
183 this->drawShadowedPath(canvas, fRRPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
184 lightPos, kLightWidth, fAnimAlpha*kSpotAlpha);
185
186 paint.setColor(SK_ColorRED);
187 canvas->translate(250, 0);
188 zPlaneParams.fZ = std::max(1.0f, 8 + fZDelta);
189 this->drawShadowedPath(canvas, fRectPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
190 lightPos, kLightWidth, fAnimAlpha*kSpotAlpha);
191
192 paint.setColor(SK_ColorBLUE);
193 canvas->translate(-250, 110);
194 zPlaneParams.fZ = std::max(1.0f, 12 + fZDelta);
195 this->drawShadowedPath(canvas, fCirclePath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
196 lightPos, kLightWidth, fAnimAlpha*0.5f);
197
198 paint.setColor(SK_ColorGREEN);
199 canvas->translate(250, 0);
200 zPlaneParams.fZ = std::max(1.0f, 64 + fZDelta);
201 this->drawShadowedPath(canvas, fRRPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
202 lightPos, kLightWidth, fAnimAlpha*kSpotAlpha);
203
204 paint.setColor(SK_ColorYELLOW);
205 canvas->translate(-250, 110);
206 zPlaneParams.fZ = std::max(1.0f, 8 + fZDelta);
207 this->drawShadowedPath(canvas, fFunkyRRPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
208 lightPos, kLightWidth, fAnimAlpha*kSpotAlpha);
209
210 paint.setColor(SK_ColorCYAN);
211 canvas->translate(250, 0);
212 zPlaneParams.fZ = std::max(1.0f, 16 + fZDelta);
213 this->drawShadowedPath(canvas, fCubicPath, zPlaneParams, paint, fAnimAlpha*kAmbientAlpha,
214 lightPos, kLightWidth, fAnimAlpha*kSpotAlpha);
215
216 paint.setColor(SK_ColorWHITE);
217 canvas->translate(250, -180);
218 zPlaneParams.fZ = std::max(1.0f, 8 + fZDelta);
219 this->drawShadowedPath(canvas, fStarPath, zPlaneParams, paint,
220 kAmbientAlpha, lightPos, kLightWidth, kSpotAlpha);
221
222 paint.setColor(SK_ColorWHITE);
223 canvas->translate(150, 0);
224 zPlaneParams.fZ = std::max(1.0f, 2 + fZDelta);
225 this->drawShadowedPath(canvas, fNotchPath, zPlaneParams, paint,
226 kAmbientAlpha, lightPos, kLightWidth, kSpotAlpha);
227
228 paint.setColor(SK_ColorWHITE);
229 canvas->translate(200, 0);
230 zPlaneParams.fZ = std::max(1.0f, 16 + fZDelta);
231 this->drawShadowedPath(canvas, fTabPath, zPlaneParams, paint,
232 kAmbientAlpha, lightPos, kLightWidth, kSpotAlpha);
233
234 // circular reveal
235 SkPath tmpClipPath = SkPath::Circle(fAnimTranslate, 0, 60);
236 SkPath tmpPath = Op(fSquareRRectPath, tmpClipPath, kIntersect_SkPathOp).value_or(SkPath());
237
238 paint.setColor(SK_ColorMAGENTA);
239 canvas->translate(-725, 240);
240 zPlaneParams.fZ = std::max(1.0f, 32 + fZDelta);
241 this->drawShadowedPath(canvas, tmpPath, zPlaneParams, paint, .1f,
242 lightPos, kLightWidth, .5f);
243
244 // path ops bug
245 SkPath tmpClipPathBug = SkPath::Circle(88.0344925f, 0, 60);
246 if (auto res = Op(fSquareRRectPath, tmpClipPathBug, kIntersect_SkPathOp)) {
247 tmpPath = *res;
248 }
249
250 canvas->translate(250, 0);
251 zPlaneParams.fZ = std::max(1.0f, 32 + fZDelta);
252 this->drawShadowedPath(canvas, tmpPath, zPlaneParams, paint, .1f,
253 lightPos, kLightWidth, .5f);
254
255 // perspective paths
256 SkPoint pivot = SkPoint::Make(fWideRectPath.getBounds().width()/2,
257 fWideRectPath.getBounds().height()/2);
258 SkPoint translate = SkPoint::Make(100, 450);
259 paint.setColor(SK_ColorWHITE);
260 Sk3DView view;
261 view.save();
262 view.rotateX(fAnimAngle);
263 SkMatrix persp;
264 view.getMatrix(&persp);
265 persp.preTranslate(-pivot.fX, -pivot.fY);
266 persp.postTranslate(pivot.fX + translate.fX, pivot.fY + translate.fY);
267 canvas->setMatrix(persp);
268 SkScalar radians = SkDegreesToRadians(fAnimAngle);
269 zPlaneParams = SkPoint3::Make(0,
270 SkScalarSin(radians),
271 std::max(1.0f, 16 + fZDelta) - SkScalarSin(radians)*pivot.fY);
272 this->drawShadowedPath(canvas, fWideRectPath, zPlaneParams, paint, .1f,
273 lightPos, kLightWidth, .5f);
274
275 pivot = SkPoint::Make(fWideOvalPath.getBounds().width() / 2,
276 fWideOvalPath.getBounds().height() / 2);
277 translate = SkPoint::Make(100, 600);
278 view.restore();
279 view.save();
280 view.rotateY(fAnimAngle);
281 view.getMatrix(&persp);
282 persp.preTranslate(-pivot.fX, -pivot.fY);
283 persp.postTranslate(pivot.fX + translate.fX, pivot.fY + translate.fY);
284 canvas->setMatrix(persp);
285 zPlaneParams = SkPoint3::Make(-SkScalarSin(radians),
286 0,
287 std::max(1.0f, 32 + fZDelta) + SkScalarSin(radians)*pivot.fX);
288 this->drawShadowedPath(canvas, fWideOvalPath, zPlaneParams, paint, .1f,
289 lightPos, kLightWidth, .5f);
290
291 pivot = SkPoint::Make(fStarPath.getBounds().width() / 2,
292 fStarPath.getBounds().height() / 2);
293 translate = SkPoint::Make(700, 250);
294 view.restore();
295 view.rotateY(fAnimAngle);
296 view.getMatrix(&persp);
297 persp.preTranslate(-pivot.fX, -pivot.fY);
298 persp.postTranslate(pivot.fX + translate.fX, pivot.fY + translate.fY);
299 canvas->setMatrix(persp);
300 zPlaneParams = SkPoint3::Make(-SkScalarSin(radians),
301 0,
302 std::max(1.0f, 8 + fZDelta) + SkScalarSin(radians)*pivot.fX);
303 this->drawShadowedPath(canvas, fStarPath, zPlaneParams, paint, .1f,
304 lightPos, kLightWidth, .5f);
305 }
306
307 private:
308 void drawShadowedPath(SkCanvas* canvas, const SkPath& path,
309 const SkPoint3& zPlaneParams,
310 const SkPaint& paint, SkScalar ambientAlpha,
311 const SkPoint3& lightPos, SkScalar lightWidth, SkScalar spotAlpha) {
312 if (fIgnoreShadowAlpha) {
313 ambientAlpha = 1;
314 spotAlpha = 1;
315 }
316 if (!fShowAmbient) {
317 ambientAlpha = 0;
318 }
319 if (!fShowSpot) {
320 spotAlpha = 0;
321 }
322 uint32_t flags = 0;
323 if (fUseAlt) {
324 flags |= SkShadowFlags::kGeometricOnly_ShadowFlag;
325 }
326 if (fUseBlur) {
327 flags |= SkShadowFlags::kConcaveBlurOnly_ShadowFlag;
328 }
329
330 SkColor ambientColor = SkColorSetARGB(ambientAlpha * 255, 0, 0, 0);
331 SkColor spotColor = SkColorSetARGB(spotAlpha * 255, 0, 0, 0);
332 SkShadowUtils::DrawShadow(canvas, path, zPlaneParams, lightPos, lightWidth,
333 ambientColor, spotColor, flags);
334
335 if (fShowObject) {
336 canvas->drawPath(path, paint);
337 } else {
338 SkPaint strokePaint;
339
340 strokePaint.setColor(paint.getColor());
341 strokePaint.setStyle(SkPaint::kStroke_Style);
342
343 canvas->drawPath(path, strokePaint);
344 }
345 }
346 };
347
348 //////////////////////////////////////////////////////////////////////////////
349
350 DEF_SLIDE( return new ShadowsSlide(); )