Skip to content

Commit

Permalink
Revive always using SkPath for physical models (flutter#4537)
Browse files Browse the repository at this point in the history
  • Loading branch information
amirh authored Jan 11, 2018
1 parent f5dc4ec commit 05fe72d
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 224 deletions.
4 changes: 2 additions & 2 deletions flow/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,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/physical_shape_layer.cc",
"layers/physical_shape_layer.h",
"layers/picture_layer.cc",
"layers/picture_layer.h",
"layers/shader_mask_layer.cc",
Expand Down
24 changes: 4 additions & 20 deletions flow/layers/default_layer_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "flutter/flow/layers/layer_tree.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/physical_shape_layer.h"
#include "flutter/flow/layers/picture_layer.h"
#include "flutter/flow/layers/shader_mask_layer.h"
#include "flutter/flow/layers/texture_layer.h"
Expand Down Expand Up @@ -106,32 +106,16 @@ void DefaultLayerBuilder::PushShaderMask(sk_sp<SkShader> shader,
PushLayer(std::move(layer), cull_rects_.top());
}

void DefaultLayerBuilder::PushPhysicalModel(const SkRRect& sk_rrect,
double elevation,
SkColor color,
SkScalar device_pixel_ratio) {
SkRect cullRect;
if (!cullRect.intersect(sk_rrect.rect(), cull_rects_.top())) {
cullRect = SkRect::MakeEmpty();
}
auto layer = std::make_unique<flow::PhysicalModelLayer>();
layer->set_shape(std::make_unique<PhysicalLayerRRect>(sk_rrect));
layer->set_elevation(elevation);
layer->set_color(color);
layer->set_device_pixel_ratio(device_pixel_ratio);
PushLayer(std::move(layer), cullRect);
}

void DefaultLayerBuilder::PushPhysicalModel(const SkPath& sk_path,
void DefaultLayerBuilder::PushPhysicalShape(const SkPath& sk_path,
double elevation,
SkColor color,
SkScalar device_pixel_ratio) {
SkRect cullRect;
if (!cullRect.intersect(sk_path.getBounds(), cull_rects_.top())) {
cullRect = SkRect::MakeEmpty();
}
auto layer = std::make_unique<flow::PhysicalModelLayer>();
layer->set_shape(std::make_unique<PhysicalLayerPath>(sk_path));
auto layer = std::make_unique<flow::PhysicalShapeLayer>();
layer->set_path(sk_path);
layer->set_elevation(elevation);
layer->set_color(color);
layer->set_device_pixel_ratio(device_pixel_ratio);
Expand Down
8 changes: 1 addition & 7 deletions flow/layers/default_layer_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ class DefaultLayerBuilder final : public LayerBuilder {
SkBlendMode blend_mode) override;

// |flow::LayerBuilder|
void PushPhysicalModel(const SkRRect& rect,
double elevation,
SkColor color,
SkScalar device_pixel_ratio) override;

// |flow::LayerBuilder|
void PushPhysicalModel(const SkPath& path,
void PushPhysicalShape(const SkPath& path,
double elevation,
SkColor color,
SkScalar device_pixel_ratio) override;
Expand Down
7 changes: 1 addition & 6 deletions flow/layers/layer_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ class LayerBuilder {
const SkRect& rect,
SkBlendMode blend_mode) = 0;

virtual void PushPhysicalModel(const SkRRect& rect,
double elevation,
SkColor color,
SkScalar device_pixel_ratio) = 0;

virtual void PushPhysicalModel(const SkPath& path,
virtual void PushPhysicalShape(const SkPath& path,
double elevation,
SkColor color,
SkScalar device_pixel_ratio) = 0;
Expand Down
131 changes: 0 additions & 131 deletions flow/layers/physical_model_layer.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
// 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 "flutter/flow/layers/physical_shape_layer.h"

#include "flutter/flow/paint_utils.h"
#include "third_party/skia/include/utils/SkShadowUtils.h"

namespace flow {

PhysicalModelLayer::PhysicalModelLayer() = default;
PhysicalShapeLayer::PhysicalShapeLayer() : isRect_(false) {}

PhysicalModelLayer::~PhysicalModelLayer() = default;
PhysicalShapeLayer::~PhysicalShapeLayer() = default;

void PhysicalModelLayer::Preroll(PrerollContext* context,
void PhysicalShapeLayer::Preroll(PrerollContext* context,
const SkMatrix& matrix) {
SkRect child_paint_bounds;
PrerollChildren(context, matrix, &child_paint_bounds);

if (elevation_ == 0) {
set_paint_bounds(shape_->getBounds());
set_paint_bounds(path_.getBounds());
} else {
#if defined(OS_FUCHSIA)
// Let the system compositor draw all shadows for us.
Expand All @@ -29,7 +29,7 @@ void PhysicalModelLayer::Preroll(PrerollContext* context,
// The margin is hardcoded to an arbitrary maximum for now because Skia
// doesn't provide a way to calculate it. We fill this whole region
// and clip children to it so we don't need to join the child paint bounds.
SkRect bounds(shape_->getBounds());
SkRect bounds(path_.getBounds());
bounds.outset(20.0, 20.0);
set_paint_bounds(bounds);
#endif // defined(OS_FUCHSIA)
Expand All @@ -38,11 +38,10 @@ void PhysicalModelLayer::Preroll(PrerollContext* context,

#if defined(OS_FUCHSIA)

void PhysicalModelLayer::UpdateScene(SceneUpdateContext& context) {
void PhysicalShapeLayer::UpdateScene(SceneUpdateContext& context) {
FXL_DCHECK(needs_system_composite());

SceneUpdateContext::Frame frame(context, shape_->getFrameRRect(), color_,
elevation_);
SceneUpdateContext::Frame frame(context, frameRRect_, color_, elevation_);
for (auto& layer : layers()) {
if (layer->needs_painting()) {
frame.AddPaintedLayer(layer.get());
Expand All @@ -54,34 +53,32 @@ void PhysicalModelLayer::UpdateScene(SceneUpdateContext& context) {

#endif // defined(OS_FUCHSIA)

void PhysicalModelLayer::Paint(PaintContext& context) const {
TRACE_EVENT0("flutter", "PhysicalModelLayer::Paint");
void PhysicalShapeLayer::Paint(PaintContext& context) const {
TRACE_EVENT0("flutter", "PhysicalShapeLayer::Paint");
FXL_DCHECK(needs_painting());

SkPath path = shape_->getPath();

if (elevation_ != 0) {
DrawShadow(&context.canvas, path, SK_ColorBLACK, elevation_,
DrawShadow(&context.canvas, path_, SK_ColorBLACK, elevation_,
SkColorGetA(color_) != 0xff, device_pixel_ratio_);
}

SkPaint paint;
paint.setColor(color_);
context.canvas.drawPath(path, paint);
context.canvas.drawPath(path_, paint);

SkAutoCanvasRestore save(&context.canvas, false);
if (shape_->isRect()) {
if (isRect_) {
context.canvas.save();
} else {
context.canvas.saveLayer(&shape_->getBounds(), nullptr);
context.canvas.saveLayer(path_.getBounds(), nullptr);
}
shape_->clipCanvas(context.canvas);
context.canvas.clipPath(path_, true);
PaintChildren(context);
if (context.checkerboard_offscreen_layers && !shape_->isRect())
DrawCheckerboard(&context.canvas, shape_->getBounds());
if (context.checkerboard_offscreen_layers && !isRect_)
DrawCheckerboard(&context.canvas, path_.getBounds());
}

void PhysicalModelLayer::DrawShadow(SkCanvas* canvas,
void PhysicalShapeLayer::DrawShadow(SkCanvas* canvas,
const SkPath& path,
SkColor color,
float elevation,
Expand All @@ -98,10 +95,4 @@ void PhysicalModelLayer::DrawShadow(SkCanvas* canvas,
dpr * 800.0f, 0.039f, 0.25f, color, flags);
}

SkPath PhysicalLayerRRect::getPath() const {
SkPath path;
path.addRRect(rrect_);
return path;
}

} // namespace flow
59 changes: 59 additions & 0 deletions flow/layers/physical_shape_layer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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_SHAPE_LAYER_H_
#define FLUTTER_FLOW_LAYERS_PHYSICAL_SHAPE_LAYER_H_

#include "flutter/flow/layers/container_layer.h"

namespace flow {

class PhysicalShapeLayer : public ContainerLayer {
public:
PhysicalShapeLayer();
~PhysicalShapeLayer() override;

void set_path(const SkPath& path) {
path_ = path;
isRect_ = false;
SkRect rect;
if (path.isRect(&rect)) {
isRect_ = true;
frameRRect_ = SkRRect::MakeRect(rect);
} else if (path.isRRect(&frameRRect_)) {
isRect_ = frameRRect_.isRect();
}
}

void set_elevation(float elevation) { elevation_ = elevation; }
void set_color(SkColor color) { color_ = color; }
void set_device_pixel_ratio(SkScalar dpr) { device_pixel_ratio_ = dpr; }

static void DrawShadow(SkCanvas* canvas,
const SkPath& path,
SkColor color,
float elevation,
bool transparentOccluder,
SkScalar dpr);

void Preroll(PrerollContext* context, const SkMatrix& matrix) override;

void Paint(PaintContext& context) const override;

#if defined(OS_FUCHSIA)
void UpdateScene(SceneUpdateContext& context) override;
#endif // defined(OS_FUCHSIA)

private:
float elevation_;
SkColor color_;
SkScalar device_pixel_ratio_;
SkPath path_;
bool isRect_;
SkRRect frameRRect_;
};

} // namespace flow

#endif // FLUTTER_FLOW_LAYERS_PHYSICAL_SHAPE_LAYER_H_
Loading

0 comments on commit 05fe72d

Please sign in to comment.