Skip to content

Commit 6b9eda4

Browse files
Canvas.drawShadow API based on SkShadowUtils::drawShadow (flutter#3486)
1 parent 26a6615 commit 6b9eda4

File tree

5 files changed

+52
-11
lines changed

5 files changed

+52
-11
lines changed

flow/layers/physical_model_layer.cc

+17-10
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,8 @@ void PhysicalModelLayer::Paint(PaintContext& context) {
5050
path.addRRect(rrect_);
5151

5252
if (elevation_ != 0) {
53-
SkShadowFlags flags = SkColorGetA(color_) == 0xff ?
54-
SkShadowFlags::kNone_ShadowFlag :
55-
SkShadowFlags::kTransparentOccluder_ShadowFlag;
56-
SkShadowUtils::DrawShadow(&context.canvas, path,
57-
elevation_ * 4,
58-
SkPoint3::Make(0.0f, -700.0f, 2800.0f),
59-
2800.0f,
60-
0.25f, 0.25f,
61-
SK_ColorBLACK,
62-
flags);
53+
DrawShadow(&context.canvas, path, SK_ColorBLACK, elevation_,
54+
SkColorGetA(color_) != 0xff);
6355
}
6456

6557
if (needs_system_composite())
@@ -79,4 +71,19 @@ void PhysicalModelLayer::Paint(PaintContext& context) {
7971
PaintChildren(context);
8072
}
8173

74+
void PhysicalModelLayer::DrawShadow(SkCanvas* canvas, const SkPath& path,
75+
SkColor color, int elevation,
76+
bool transparentOccluder) {
77+
SkShadowFlags flags = transparentOccluder ?
78+
SkShadowFlags::kTransparentOccluder_ShadowFlag :
79+
SkShadowFlags::kNone_ShadowFlag;
80+
SkShadowUtils::DrawShadow(canvas, path,
81+
elevation * 4,
82+
SkPoint3::Make(0.0f, -700.0f, 2800.0f),
83+
2800.0f,
84+
0.25f, 0.25f,
85+
color,
86+
flags);
87+
}
88+
8289
} // namespace flow

flow/layers/physical_model_layer.h

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class PhysicalModelLayer : public ContainerLayer {
1818
void set_elevation(int elevation) { elevation_ = elevation; }
1919
void set_color(SkColor color) { color_ = color; }
2020

21+
static void DrawShadow(SkCanvas* canvas, const SkPath& path,
22+
SkColor color, int elevation, bool transparentOccluder);
23+
2124
protected:
2225
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
2326
void Paint(PaintContext& context) override;

lib/ui/painting.dart

+12
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,18 @@ class Canvas extends NativeFieldWrapperClass2 {
15481548
Int32List colors,
15491549
int blendMode,
15501550
Float32List cullRect) native "Canvas_drawAtlas";
1551+
1552+
/// Draws a shadow for a [Path] representing the given material elevation.
1553+
///
1554+
/// transparentOccluder should be true if the occluding object is not opaque.
1555+
void drawShadow(Path path, Color color, int elevation, bool transparentOccluder) {
1556+
_drawShadow(path, color.value, elevation, transparentOccluder);
1557+
}
1558+
1559+
void _drawShadow(Path path,
1560+
int color,
1561+
int elevation,
1562+
bool transparentOccluder) native "Canvas_drawShadow";
15511563
}
15521564

15531565
/// An object representing a sequence of recorded graphical operations.

lib/ui/painting/canvas.cc

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <math.h>
88

9+
#include "flutter/flow/layers/physical_model_layer.h"
910
#include "flutter/lib/ui/painting/image.h"
1011
#include "flutter/lib/ui/painting/matrix.h"
1112
#include "lib/tonic/converter/dart_converter.h"
@@ -53,7 +54,8 @@ IMPLEMENT_WRAPPERTYPEINFO(ui, Canvas);
5354
V(Canvas, drawPicture) \
5455
V(Canvas, drawPoints) \
5556
V(Canvas, drawVertices) \
56-
V(Canvas, drawAtlas)
57+
V(Canvas, drawAtlas) \
58+
V(Canvas, drawShadow)
5759

5860
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
5961

@@ -395,6 +397,17 @@ void Canvas::drawAtlas(const Paint& paint,
395397
paint.paint());
396398
}
397399

400+
void Canvas::drawShadow(const CanvasPath* path,
401+
SkColor color,
402+
int elevation,
403+
bool transparentOccluder) {
404+
flow::PhysicalModelLayer::DrawShadow(canvas_,
405+
path->path(),
406+
color,
407+
elevation,
408+
transparentOccluder);
409+
}
410+
398411
void Canvas::Clear() {
399412
canvas_ = nullptr;
400413
}

lib/ui/painting/canvas.h

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "lib/tonic/typed_data/float64_list.h"
1616
#include "lib/tonic/typed_data/int32_list.h"
1717
#include "third_party/skia/include/core/SkCanvas.h"
18+
#include "third_party/skia/include/utils/SkShadowUtils.h"
1819

1920
namespace tonic {
2021
class DartLibraryNatives;
@@ -159,6 +160,11 @@ class Canvas : public ftl::RefCountedThreadSafe<Canvas>,
159160
SkBlendMode blend_mode,
160161
const tonic::Float32List& cull_rect);
161162

163+
void drawShadow(const CanvasPath* path,
164+
SkColor color,
165+
int elevation,
166+
bool transparentOccluder);
167+
162168
SkCanvas* canvas() const { return canvas_; }
163169
void Clear();
164170
bool IsRecording() const;

0 commit comments

Comments
 (0)