From 3e7ecdc1e7b54dbb8c83c779a7037514b50480c0 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Thu, 16 Feb 2017 17:30:57 -0800 Subject: [PATCH] Physical model layer that can draw shadows for a Material widget (flutter/engine#3424) --- engine/src/flutter/flow/BUILD.gn | 2 + .../flow/layers/physical_model_layer.cc | 60 +++++++++++++++++++ .../flow/layers/physical_model_layer.h | 33 ++++++++++ engine/src/flutter/lib/ui/compositing.dart | 12 ++++ .../lib/ui/compositing/scene_builder.cc | 16 +++++ .../lib/ui/compositing/scene_builder.h | 2 + 6 files changed, 125 insertions(+) create mode 100644 engine/src/flutter/flow/layers/physical_model_layer.cc create mode 100644 engine/src/flutter/flow/layers/physical_model_layer.h diff --git a/engine/src/flutter/flow/BUILD.gn b/engine/src/flutter/flow/BUILD.gn index 5d3d34cac5..e0961b8562 100644 --- a/engine/src/flutter/flow/BUILD.gn +++ b/engine/src/flutter/flow/BUILD.gn @@ -28,6 +28,8 @@ source_set("flow") { "layers/opacity_layer.h", "layers/performance_overlay_layer.cc", "layers/performance_overlay_layer.h", + "layers/physical_model_layer.cc", + "layers/physical_model_layer.h", "layers/picture_layer.cc", "layers/picture_layer.h", "layers/shader_mask_layer.cc", diff --git a/engine/src/flutter/flow/layers/physical_model_layer.cc b/engine/src/flutter/flow/layers/physical_model_layer.cc new file mode 100644 index 0000000000..0392677b5f --- /dev/null +++ b/engine/src/flutter/flow/layers/physical_model_layer.cc @@ -0,0 +1,60 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/flow/layers/physical_model_layer.h" + +#include "third_party/skia/include/utils/SkShadowUtils.h" + +namespace flow { + +PhysicalModelLayer::PhysicalModelLayer() + : rrect_(SkRRect::MakeEmpty()) {} + +PhysicalModelLayer::~PhysicalModelLayer() {} + +void PhysicalModelLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { + PrerollChildren(context, matrix); + if (!context->child_paint_bounds.intersect(rrect_.getBounds())) + context->child_paint_bounds.setEmpty(); + + // Add some margin to the paint bounds to leave space for the shadow. + // The margin is hardcoded to an arbitrary maximum for now because Skia + // doesn't provide a way to calculate it. + SkRect bounds(rrect_.getBounds()); + bounds.outset(50.0, 50.0); + set_paint_bounds(bounds); +} + +void PhysicalModelLayer::Paint(PaintContext& context) { + TRACE_EVENT0("flutter", "PhysicalModelLayer::Paint"); + // TODO: need to support this layer on Fuchsia + FTL_DCHECK(!needs_system_composite()); + + SkPath path; + path.addRRect(rrect_); + + if (elevation_ != 0) { + SkShadowUtils::DrawShadow(&context.canvas, path, + elevation_ * 4, + SkPoint3::Make(0.0f, -700.0f, 2800.0f), + 2800.0f, + 0.25f, 0.25f, + SK_ColorBLACK); + } + + SkPaint paint; + paint.setColor(SkColorSetA(color_, 0xFF)); + context.canvas.drawPath(path, paint); + + SkAutoCanvasRestore save(&context.canvas, false); + if (rrect_.isRect()) { + context.canvas.save(); + } else { + context.canvas.saveLayer(&rrect_.getBounds(), nullptr); + } + context.canvas.clipRRect(rrect_, true); + PaintChildren(context); +} + +} // namespace flow diff --git a/engine/src/flutter/flow/layers/physical_model_layer.h b/engine/src/flutter/flow/layers/physical_model_layer.h new file mode 100644 index 0000000000..64b4915818 --- /dev/null +++ b/engine/src/flutter/flow/layers/physical_model_layer.h @@ -0,0 +1,33 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_FLOW_LAYERS_PHYSICAL_MODEL_LAYER_H_ +#define FLUTTER_FLOW_LAYERS_PHYSICAL_MODEL_LAYER_H_ + +#include "flutter/flow/layers/container_layer.h" + +namespace flow { + +class PhysicalModelLayer : public ContainerLayer { + public: + PhysicalModelLayer(); + ~PhysicalModelLayer() override; + + void set_rrect(const SkRRect& rrect) { rrect_ = rrect; } + void set_elevation(int elevation) { elevation_ = elevation; } + void set_color(SkColor color) { color_ = color; } + + protected: + void Preroll(PrerollContext* context, const SkMatrix& matrix) override; + void Paint(PaintContext& context) override; + + private: + SkRRect rrect_; + int elevation_; + SkColor color_; +}; + +} // namespace flow + +#endif // FLUTTER_FLOW_LAYERS_PHYSICAL_MODEL_LAYER_H_ diff --git a/engine/src/flutter/lib/ui/compositing.dart b/engine/src/flutter/lib/ui/compositing.dart index d9756a26b0..a37af9c984 100644 --- a/engine/src/flutter/lib/ui/compositing.dart +++ b/engine/src/flutter/lib/ui/compositing.dart @@ -127,6 +127,18 @@ class SceneBuilder extends NativeFieldWrapperClass2 { double maskRectBottom, int blendMode) native "SceneBuilder_pushShaderMask"; + /// Pushes a physical model operation onto the operation stack. + /// + /// Rasterization will be clipped to the given shape. + /// + /// See [pop] for details about the operation stack. + void pushPhysicalModel({ RRect rrect, int elevation, Color color }) { + _pushPhysicalModel(rrect._value, elevation, color.value); + } + void _pushPhysicalModel(Float32List rrect, + int elevation, + int color) native "SceneBuilder_pushPhysicalModel"; + /// Ends the effect of the most recently pushed operation. /// /// Internally the scene builder maintains a stack of operations. Each of the diff --git a/engine/src/flutter/lib/ui/compositing/scene_builder.cc b/engine/src/flutter/lib/ui/compositing/scene_builder.cc index 1ef3c2994e..13f2fc29f1 100644 --- a/engine/src/flutter/lib/ui/compositing/scene_builder.cc +++ b/engine/src/flutter/lib/ui/compositing/scene_builder.cc @@ -12,6 +12,7 @@ #include "flutter/flow/layers/container_layer.h" #include "flutter/flow/layers/opacity_layer.h" #include "flutter/flow/layers/performance_overlay_layer.h" +#include "flutter/flow/layers/physical_model_layer.h" #include "flutter/flow/layers/picture_layer.h" #include "flutter/flow/layers/shader_mask_layer.h" #include "flutter/flow/layers/transform_layer.h" @@ -45,6 +46,7 @@ IMPLEMENT_WRAPPERTYPEINFO(ui, SceneBuilder); V(SceneBuilder, pushColorFilter) \ V(SceneBuilder, pushBackdropFilter) \ V(SceneBuilder, pushShaderMask) \ + V(SceneBuilder, pushPhysicalModel) \ V(SceneBuilder, pop) \ V(SceneBuilder, addPicture) \ V(SceneBuilder, addChildScene) \ @@ -152,6 +154,20 @@ void SceneBuilder::pushShaderMask(Shader* shader, addLayer(std::move(layer), m_cullRects.top()); } +void SceneBuilder::pushPhysicalModel(const RRect& rrect, + int elevation, + int color) { + SkRect cullRect; + if (!cullRect.intersect(rrect.sk_rrect.rect(), m_cullRects.top())) + cullRect = SkRect::MakeEmpty(); + + std::unique_ptr layer(new flow::PhysicalModelLayer()); + layer->set_rrect(rrect.sk_rrect); + layer->set_elevation(elevation); + layer->set_color(color); + addLayer(std::move(layer), cullRect); +} + void SceneBuilder::addLayer(std::unique_ptr layer, const SkRect& cullRect) { FTL_DCHECK(layer); diff --git a/engine/src/flutter/lib/ui/compositing/scene_builder.h b/engine/src/flutter/lib/ui/compositing/scene_builder.h index 80d49489fc..0166800cf5 100644 --- a/engine/src/flutter/lib/ui/compositing/scene_builder.h +++ b/engine/src/flutter/lib/ui/compositing/scene_builder.h @@ -46,6 +46,8 @@ class SceneBuilder : public ftl::RefCountedThreadSafe, double maskRectTop, double maskRectBottom, int blendMode); + void pushPhysicalModel(const RRect& rrect, int elevation, int color); + void pop(); void addPerformanceOverlay(uint64_t enabledOptions,