Physical model layer that can draw shadows for a Material widget (flutter/engine#3424)

This commit is contained in:
Jason Simmons
2017-02-16 17:30:57 -08:00
committed by GitHub
parent 7e31feadc0
commit 3e7ecdc1e7
6 changed files with 125 additions and 0 deletions

View File

@@ -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",

View File

@@ -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

View File

@@ -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_

View File

@@ -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

View File

@@ -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<flow::PhysicalModelLayer> 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<flow::ContainerLayer> layer,
const SkRect& cullRect) {
FTL_DCHECK(layer);

View File

@@ -46,6 +46,8 @@ class SceneBuilder : public ftl::RefCountedThreadSafe<SceneBuilder>,
double maskRectTop,
double maskRectBottom,
int blendMode);
void pushPhysicalModel(const RRect& rrect, int elevation, int color);
void pop();
void addPerformanceOverlay(uint64_t enabledOptions,