提交 6b9eda4a 编写于 作者: J Jason Simmons 提交者: GitHub

Canvas.drawShadow API based on SkShadowUtils::drawShadow (#3486)

上级 26a6615d
...@@ -50,16 +50,8 @@ void PhysicalModelLayer::Paint(PaintContext& context) { ...@@ -50,16 +50,8 @@ void PhysicalModelLayer::Paint(PaintContext& context) {
path.addRRect(rrect_); path.addRRect(rrect_);
if (elevation_ != 0) { if (elevation_ != 0) {
SkShadowFlags flags = SkColorGetA(color_) == 0xff ? DrawShadow(&context.canvas, path, SK_ColorBLACK, elevation_,
SkShadowFlags::kNone_ShadowFlag : SkColorGetA(color_) != 0xff);
SkShadowFlags::kTransparentOccluder_ShadowFlag;
SkShadowUtils::DrawShadow(&context.canvas, path,
elevation_ * 4,
SkPoint3::Make(0.0f, -700.0f, 2800.0f),
2800.0f,
0.25f, 0.25f,
SK_ColorBLACK,
flags);
} }
if (needs_system_composite()) if (needs_system_composite())
...@@ -79,4 +71,19 @@ void PhysicalModelLayer::Paint(PaintContext& context) { ...@@ -79,4 +71,19 @@ void PhysicalModelLayer::Paint(PaintContext& context) {
PaintChildren(context); PaintChildren(context);
} }
void PhysicalModelLayer::DrawShadow(SkCanvas* canvas, const SkPath& path,
SkColor color, int elevation,
bool transparentOccluder) {
SkShadowFlags flags = transparentOccluder ?
SkShadowFlags::kTransparentOccluder_ShadowFlag :
SkShadowFlags::kNone_ShadowFlag;
SkShadowUtils::DrawShadow(canvas, path,
elevation * 4,
SkPoint3::Make(0.0f, -700.0f, 2800.0f),
2800.0f,
0.25f, 0.25f,
color,
flags);
}
} // namespace flow } // namespace flow
...@@ -18,6 +18,9 @@ class PhysicalModelLayer : public ContainerLayer { ...@@ -18,6 +18,9 @@ class PhysicalModelLayer : public ContainerLayer {
void set_elevation(int elevation) { elevation_ = elevation; } void set_elevation(int elevation) { elevation_ = elevation; }
void set_color(SkColor color) { color_ = color; } void set_color(SkColor color) { color_ = color; }
static void DrawShadow(SkCanvas* canvas, const SkPath& path,
SkColor color, int elevation, bool transparentOccluder);
protected: protected:
void Preroll(PrerollContext* context, const SkMatrix& matrix) override; void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
void Paint(PaintContext& context) override; void Paint(PaintContext& context) override;
......
...@@ -1548,6 +1548,18 @@ class Canvas extends NativeFieldWrapperClass2 { ...@@ -1548,6 +1548,18 @@ class Canvas extends NativeFieldWrapperClass2 {
Int32List colors, Int32List colors,
int blendMode, int blendMode,
Float32List cullRect) native "Canvas_drawAtlas"; Float32List cullRect) native "Canvas_drawAtlas";
/// Draws a shadow for a [Path] representing the given material elevation.
///
/// transparentOccluder should be true if the occluding object is not opaque.
void drawShadow(Path path, Color color, int elevation, bool transparentOccluder) {
_drawShadow(path, color.value, elevation, transparentOccluder);
}
void _drawShadow(Path path,
int color,
int elevation,
bool transparentOccluder) native "Canvas_drawShadow";
} }
/// An object representing a sequence of recorded graphical operations. /// An object representing a sequence of recorded graphical operations.
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <math.h> #include <math.h>
#include "flutter/flow/layers/physical_model_layer.h"
#include "flutter/lib/ui/painting/image.h" #include "flutter/lib/ui/painting/image.h"
#include "flutter/lib/ui/painting/matrix.h" #include "flutter/lib/ui/painting/matrix.h"
#include "lib/tonic/converter/dart_converter.h" #include "lib/tonic/converter/dart_converter.h"
...@@ -53,7 +54,8 @@ IMPLEMENT_WRAPPERTYPEINFO(ui, Canvas); ...@@ -53,7 +54,8 @@ IMPLEMENT_WRAPPERTYPEINFO(ui, Canvas);
V(Canvas, drawPicture) \ V(Canvas, drawPicture) \
V(Canvas, drawPoints) \ V(Canvas, drawPoints) \
V(Canvas, drawVertices) \ V(Canvas, drawVertices) \
V(Canvas, drawAtlas) V(Canvas, drawAtlas) \
V(Canvas, drawShadow)
FOR_EACH_BINDING(DART_NATIVE_CALLBACK) FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
...@@ -395,6 +397,17 @@ void Canvas::drawAtlas(const Paint& paint, ...@@ -395,6 +397,17 @@ void Canvas::drawAtlas(const Paint& paint,
paint.paint()); paint.paint());
} }
void Canvas::drawShadow(const CanvasPath* path,
SkColor color,
int elevation,
bool transparentOccluder) {
flow::PhysicalModelLayer::DrawShadow(canvas_,
path->path(),
color,
elevation,
transparentOccluder);
}
void Canvas::Clear() { void Canvas::Clear() {
canvas_ = nullptr; canvas_ = nullptr;
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "lib/tonic/typed_data/float64_list.h" #include "lib/tonic/typed_data/float64_list.h"
#include "lib/tonic/typed_data/int32_list.h" #include "lib/tonic/typed_data/int32_list.h"
#include "third_party/skia/include/core/SkCanvas.h" #include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/utils/SkShadowUtils.h"
namespace tonic { namespace tonic {
class DartLibraryNatives; class DartLibraryNatives;
...@@ -159,6 +160,11 @@ class Canvas : public ftl::RefCountedThreadSafe<Canvas>, ...@@ -159,6 +160,11 @@ class Canvas : public ftl::RefCountedThreadSafe<Canvas>,
SkBlendMode blend_mode, SkBlendMode blend_mode,
const tonic::Float32List& cull_rect); const tonic::Float32List& cull_rect);
void drawShadow(const CanvasPath* path,
SkColor color,
int elevation,
bool transparentOccluder);
SkCanvas* canvas() const { return canvas_; } SkCanvas* canvas() const { return canvas_; }
void Clear(); void Clear();
bool IsRecording() const; bool IsRecording() const;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册