Flutter Engine
The Flutter Engine
MaterialShadowsSlide.cpp
Go to the documentation of this file.
1
2/*
3 * Copyright 2020 Google LLC
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
10#include "include/core/SkPath.h"
15#include "src/base/SkUTF.h"
16#include "src/core/SkBlurMask.h"
17#include "tools/ToolUtils.h"
19#include "tools/viewer/Slide.h"
20
21////////////////////////////////////////////////////////////////////////////
22
24 SkPath fCirclePath;
25 SkPath fCapsulePath;
26 SkPath fLargeRRPath;
27 SkPath fSmallRRPath;
28
29 SkPoint3 fLightPos;
30
31public:
32 MaterialShadowsSlide() { fName = "MaterialShadows"; }
33
34 void load(SkScalar w, SkScalar h) override {
35 fCirclePath.addCircle(0, 0, 56/2);
36 fCapsulePath.addRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-64, -24, 128, 48), 24, 24));
37 fLargeRRPath.addRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-64, -64, 128, 128), 4, 4));
38 fSmallRRPath.addRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(-40, -40, 80, 80), 4, 4));
39
40 fLightPos = SkPoint3::Make(0, -700, 700);
41 }
42
43 void drawShadowedPath(SkCanvas* canvas, const SkPath& path,
44 const SkPoint3& zPlaneParams,
45 const SkPaint& paint, SkScalar ambientAlpha,
46 const SkPoint3& lightPos, SkScalar lightRadius, SkScalar spotAlpha) {
47 uint32_t flags = 0;
49
50 SkColor ambientColor = SkColorSetARGB(ambientAlpha * 255, 0, 0, 0);
51 SkColor spotColor = SkColorSetARGB(spotAlpha * 255, 0, 0, 0);
52 SkShadowUtils::DrawShadow(canvas, path, zPlaneParams, lightPos, lightRadius,
53 ambientColor, spotColor, flags);
54
55 canvas->drawPath(path, paint);
56 }
57
58 void draw(SkCanvas* canvas) override {
59 canvas->drawColor(0xFFFFFFFF);
60
61 const SkScalar kLightRadius = 1.1f;
62 const SkScalar kAmbientAlpha = 0.05f;
63 const SkScalar kSpotAlpha = 0.35f;
64
65 const SkScalar elevations[] = { 1, 3, 6, 8, 12, 24 };
66
68 paint.setAntiAlias(true);
69
70 SkPoint3 lightPos = fLightPos;
71 SkPoint3 zPlaneParams = SkPoint3::Make(0, 0, 0);
72
73 paint.setColor(SK_ColorWHITE);
74 canvas->save();
75 canvas->translate(80, 80);
76 for (unsigned int i = 0; i < std::size(elevations); ++i) {
77 zPlaneParams.fZ = elevations[i];
78 this->drawShadowedPath(canvas, fCirclePath, zPlaneParams, paint, kAmbientAlpha,
79 lightPos, kLightRadius, kSpotAlpha);
80 canvas->translate(80, 0);
81 }
82 canvas->restore();
83
84 canvas->save();
85 canvas->translate(120, 175);
86 for (unsigned int i = 0; i < std::size(elevations); ++i) {
87 zPlaneParams.fZ = elevations[i];
88 this->drawShadowedPath(canvas, fCapsulePath, zPlaneParams, paint, kAmbientAlpha,
89 lightPos, kLightRadius, kSpotAlpha);
90 canvas->translate(160, 0);
91 }
92 canvas->restore();
93
94 canvas->save();
95 canvas->translate(120, 320);
96 for (unsigned int i = 0; i < std::size(elevations); ++i) {
97 zPlaneParams.fZ = elevations[i];
98 this->drawShadowedPath(canvas, fLargeRRPath, zPlaneParams, paint, kAmbientAlpha,
99 lightPos, kLightRadius, kSpotAlpha);
100 canvas->translate(160, 0);
101 }
102 canvas->restore();
103
104 canvas->save();
105 canvas->translate(100, 475);
106 for (unsigned int i = 0; i < std::size(elevations); ++i) {
107 zPlaneParams.fZ = elevations[i];
108 this->drawShadowedPath(canvas, fSmallRRPath, zPlaneParams, paint, kAmbientAlpha,
109 lightPos, kLightRadius, kSpotAlpha);
110 canvas->translate(160, 0);
111 }
112 canvas->restore();
113
114 canvas->save();
115 canvas->translate(100, 600);
116 for (unsigned int i = 0; i < std::size(elevations); ++i) {
117 canvas->save();
118 zPlaneParams.fZ = elevations[i];
119 canvas->rotate(10);
120 this->drawShadowedPath(canvas, fSmallRRPath, zPlaneParams, paint, kAmbientAlpha,
121 lightPos, kLightRadius, kSpotAlpha);
122 canvas->restore();
123 canvas->translate(160, 0);
124 }
125 canvas->restore();
126
127 canvas->save();
128 canvas->translate(100, 725);
129 for (unsigned int i = 0; i < std::size(elevations); ++i) {
130 canvas->save();
131 zPlaneParams.fZ = elevations[i];
132 canvas->rotate(45);
133 this->drawShadowedPath(canvas, fSmallRRPath, zPlaneParams, paint, kAmbientAlpha,
134 lightPos, kLightRadius, kSpotAlpha);
135 canvas->restore();
136 canvas->translate(160, 0);
137 }
138 canvas->restore();
139
140 }
141};
142
143//////////////////////////////////////////////////////////////////////////////
144
145DEF_SLIDE( return new MaterialShadowsSlide(); )
uint32_t SkColor
Definition: SkColor.h:37
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
@ kDirectionalLight_ShadowFlag
Definition: SkShadowUtils.h:31
#define DEF_SLIDE(code)
Definition: Slide.h:25
void load(SkScalar w, SkScalar h) override
void drawShadowedPath(SkCanvas *canvas, const SkPath &path, const SkPoint3 &zPlaneParams, const SkPaint &paint, SkScalar ambientAlpha, const SkPoint3 &lightPos, SkScalar lightRadius, SkScalar spotAlpha)
void draw(SkCanvas *canvas) override
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
void rotate(SkScalar degrees)
Definition: SkCanvas.cpp:1300
int save()
Definition: SkCanvas.cpp:447
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
Definition: SkPath.h:59
SkPath & addCircle(SkScalar x, SkScalar y, SkScalar radius, SkPathDirection dir=SkPathDirection::kCW)
Definition: SkPath.cpp:1213
SkPath & addRRect(const SkRRect &rrect, SkPathDirection dir=SkPathDirection::kCW)
Definition: SkPath.cpp:1000
static SkRRect MakeRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Definition: SkRRect.h:180
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)
Definition: Slide.h:29
SkString fName
Definition: Slide.h:54
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
FlutterSemanticsFlag flags
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
SkScalar w
SkScalar h
static SkPoint3 Make(SkScalar x, SkScalar y, SkScalar z)
Definition: SkPoint3.h:18
SkScalar fZ
Definition: SkPoint3.h:16
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659