Skip to content

Commit

Permalink
Make flow layers' attributes immutable (#9176)
Browse files Browse the repository at this point in the history
For #33807

We still need to make layers' children immutable for full immutability.
That will require us to change the SceneBuilder API to build the layer
bottom up instead of top down (post-order traversal instead of pre-order
traversal).
  • Loading branch information
liyuqian committed Jun 10, 2019
1 parent 259d334 commit b622d43
Show file tree
Hide file tree
Showing 27 changed files with 129 additions and 151 deletions.
3 changes: 2 additions & 1 deletion flow/layers/backdrop_filter_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

namespace flutter {

BackdropFilterLayer::BackdropFilterLayer() = default;
BackdropFilterLayer::BackdropFilterLayer(sk_sp<SkImageFilter> filter)
: filter_(std::move(filter)) {}

BackdropFilterLayer::~BackdropFilterLayer() = default;

Expand Down
4 changes: 1 addition & 3 deletions flow/layers/backdrop_filter_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ namespace flutter {

class BackdropFilterLayer : public ContainerLayer {
public:
BackdropFilterLayer();
BackdropFilterLayer(sk_sp<SkImageFilter> filter);
~BackdropFilterLayer() override;

void set_filter(sk_sp<SkImageFilter> filter) { filter_ = std::move(filter); }

void Paint(PaintContext& context) const override;

private:
Expand Down
4 changes: 2 additions & 2 deletions flow/layers/clip_path_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace flutter {

ClipPathLayer::ClipPathLayer(Clip clip_behavior)
: clip_behavior_(clip_behavior) {
ClipPathLayer::ClipPathLayer(const SkPath& clip_path, Clip clip_behavior)
: clip_path_(clip_path), clip_behavior_(clip_behavior) {
FML_DCHECK(clip_behavior != Clip::none);
}

Expand Down
4 changes: 1 addition & 3 deletions flow/layers/clip_path_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ namespace flutter {

class ClipPathLayer : public ContainerLayer {
public:
ClipPathLayer(Clip clip_behavior = Clip::antiAlias);
ClipPathLayer(const SkPath& clip_path, Clip clip_behavior = Clip::antiAlias);
~ClipPathLayer() override;

void set_clip_path(const SkPath& clip_path) { clip_path_ = clip_path; }

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

void Paint(PaintContext& context) const override;
Expand Down
4 changes: 2 additions & 2 deletions flow/layers/clip_rect_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace flutter {

ClipRectLayer::ClipRectLayer(Clip clip_behavior)
: clip_behavior_(clip_behavior) {
ClipRectLayer::ClipRectLayer(const SkRect& clip_rect, Clip clip_behavior)
: clip_rect_(clip_rect), clip_behavior_(clip_behavior) {
FML_DCHECK(clip_behavior != Clip::none);
}

Expand Down
4 changes: 1 addition & 3 deletions flow/layers/clip_rect_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ namespace flutter {

class ClipRectLayer : public ContainerLayer {
public:
ClipRectLayer(Clip clip_behavior);
ClipRectLayer(const SkRect& clip_rect, Clip clip_behavior);
~ClipRectLayer() override;

void set_clip_rect(const SkRect& clip_rect) { clip_rect_ = clip_rect; }

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

Expand Down
4 changes: 2 additions & 2 deletions flow/layers/clip_rrect_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace flutter {

ClipRRectLayer::ClipRRectLayer(Clip clip_behavior)
: clip_behavior_(clip_behavior) {
ClipRRectLayer::ClipRRectLayer(const SkRRect& clip_rrect, Clip clip_behavior)
: clip_rrect_(clip_rrect), clip_behavior_(clip_behavior) {
FML_DCHECK(clip_behavior != Clip::none);
}

Expand Down
4 changes: 1 addition & 3 deletions flow/layers/clip_rrect_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ namespace flutter {

class ClipRRectLayer : public ContainerLayer {
public:
ClipRRectLayer(Clip clip_behavior);
ClipRRectLayer(const SkRRect& clip_rrect, Clip clip_behavior);
~ClipRRectLayer() override;

void set_clip_rrect(const SkRRect& clip_rrect) { clip_rrect_ = clip_rrect; }

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

void Paint(PaintContext& context) const override;
Expand Down
3 changes: 2 additions & 1 deletion flow/layers/color_filter_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

namespace flutter {

ColorFilterLayer::ColorFilterLayer() = default;
ColorFilterLayer::ColorFilterLayer(SkColor color, SkBlendMode blend_mode)
: color_(color), blend_mode_(blend_mode) {}

ColorFilterLayer::~ColorFilterLayer() = default;

Expand Down
6 changes: 1 addition & 5 deletions flow/layers/color_filter_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ namespace flutter {

class ColorFilterLayer : public ContainerLayer {
public:
ColorFilterLayer();
ColorFilterLayer(SkColor color, SkBlendMode blend_mode);
~ColorFilterLayer() override;

void set_color(SkColor color) { color_ = color; }

void set_blend_mode(SkBlendMode blend_mode) { blend_mode_ = blend_mode; }

void Paint(PaintContext& context) const override;

private:
Expand Down
7 changes: 3 additions & 4 deletions flow/layers/opacity_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

namespace flutter {

OpacityLayer::OpacityLayer() = default;
OpacityLayer::OpacityLayer(int alpha, const SkPoint& offset)
: alpha_(alpha), offset_(offset) {}

OpacityLayer::~OpacityLayer() = default;

Expand All @@ -19,14 +20,12 @@ void OpacityLayer::EnsureSingleChild() {
return;
}

auto new_child = std::make_shared<flutter::TransformLayer>();

// Be careful: SkMatrix's default constructor doesn't initialize the matrix to
// identity. Hence we have to explicitly call SkMatrix::setIdentity.
SkMatrix identity;
identity.setIdentity();
auto new_child = std::make_shared<flutter::TransformLayer>(identity);

new_child->set_transform(identity);
for (auto& child : layers()) {
new_child->Add(child);
}
Expand Down
5 changes: 1 addition & 4 deletions flow/layers/opacity_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ namespace flutter {
// |EnsureSingleChild| will assert if there are no children.
class OpacityLayer : public ContainerLayer {
public:
OpacityLayer();
OpacityLayer(int alpha, const SkPoint& offset);
~OpacityLayer() override;

void set_alpha(int alpha) { alpha_ = alpha; }
void set_offset(const SkPoint& offset) { offset_ = offset; }

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

void Paint(PaintContext& context) const override;
Expand Down
25 changes: 17 additions & 8 deletions flow/layers/physical_shape_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ namespace flutter {
const SkScalar kLightHeight = 600;
const SkScalar kLightRadius = 800;

PhysicalShapeLayer::PhysicalShapeLayer(Clip clip_behavior)
: isRect_(false), clip_behavior_(clip_behavior) {}

PhysicalShapeLayer::~PhysicalShapeLayer() = default;

void PhysicalShapeLayer::set_path(const SkPath& path) {
path_ = path;
isRect_ = false;
PhysicalShapeLayer::PhysicalShapeLayer(SkColor color,
SkColor shadow_color,
SkScalar device_pixel_ratio,
float viewport_depth,
float elevation,
const SkPath& path,
Clip clip_behavior)
: color_(color),
shadow_color_(shadow_color),
device_pixel_ratio_(device_pixel_ratio),
viewport_depth_(viewport_depth),
elevation_(elevation),
path_(path),
isRect_(false),
clip_behavior_(clip_behavior) {
SkRect rect;
if (path.isRect(&rect)) {
isRect_ = true;
Expand All @@ -41,6 +48,8 @@ void PhysicalShapeLayer::set_path(const SkPath& path) {
}
}

PhysicalShapeLayer::~PhysicalShapeLayer() = default;

void PhysicalShapeLayer::Preroll(PrerollContext* context,
const SkMatrix& matrix) {
context->total_elevation += elevation_;
Expand Down
26 changes: 10 additions & 16 deletions flow/layers/physical_shape_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@ namespace flutter {

class PhysicalShapeLayer : public ContainerLayer {
public:
PhysicalShapeLayer(Clip clip_behavior);
PhysicalShapeLayer(SkColor color,
SkColor shadow_color,
SkScalar device_pixel_ratio,
float viewport_depth,
float elevation,
const SkPath& path,
Clip clip_behavior);
~PhysicalShapeLayer() override;

void set_path(const SkPath& path);

void set_color(SkColor color) { color_ = color; }
void set_shadow_color(SkColor shadow_color) { shadow_color_ = shadow_color; }
void set_device_pixel_ratio(SkScalar dpr) { device_pixel_ratio_ = dpr; }
void set_viewport_depth(float depth) { viewport_depth_ = depth; }

// Sets the elevation. This needs to be set before preroll because it's then
// cached by any children of this layer. Setting it after preroll will break
// their elevation calculations.
void set_elevation(float elevation) { elevation_ = elevation; }

static void DrawShadow(SkCanvas* canvas,
const SkPath& path,
SkColor color,
Expand All @@ -42,12 +36,12 @@ class PhysicalShapeLayer : public ContainerLayer {
#endif // defined(OS_FUCHSIA)

private:
float elevation_ = 0.0f;
float total_elevation_ = 0.0f;
float viewport_depth_;
SkColor color_;
SkColor shadow_color_;
SkScalar device_pixel_ratio_;
float viewport_depth_;
float elevation_ = 0.0f;
float total_elevation_ = 0.0f;
SkPath path_;
bool isRect_;
SkRRect frameRRect_;
Expand Down
10 changes: 8 additions & 2 deletions flow/layers/physical_shape_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ namespace flutter {
TEST(PhysicalShapeLayer, TotalElevation) {
std::shared_ptr<PhysicalShapeLayer> layers[4];

SkColor dummy_color = 0;
SkPath dummy_path;
for (int i = 0; i < 4; i += 1) {
layers[i] = std::make_shared<PhysicalShapeLayer>(Clip::none);
layers[i]->set_elevation((float)(i + 1));
layers[i] =
std::make_shared<PhysicalShapeLayer>(dummy_color, dummy_color,
1.0f, // pixel ratio,
1.0f, // depth
(float)(i + 1), // elevation
dummy_path, Clip::none);
}

layers[0]->Add(layers[1]);
Expand Down
9 changes: 8 additions & 1 deletion flow/layers/picture_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

namespace flutter {

PictureLayer::PictureLayer() = default;
PictureLayer::PictureLayer(const SkPoint& offset,
SkiaGPUObject<SkPicture> picture,
bool is_complex,
bool will_change)
: offset_(offset),
picture_(std::move(picture)),
is_complex_(is_complex),
will_change_(will_change) {}

PictureLayer::~PictureLayer() = default;

Expand Down
13 changes: 4 additions & 9 deletions flow/layers/picture_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@ namespace flutter {

class PictureLayer : public Layer {
public:
PictureLayer();
PictureLayer(const SkPoint& offset,
SkiaGPUObject<SkPicture> picture,
bool is_complex,
bool will_change);
~PictureLayer() override;

void set_offset(const SkPoint& offset) { offset_ = offset; }
void set_picture(SkiaGPUObject<SkPicture> picture) {
picture_ = std::move(picture);
}

void set_is_complex(bool value) { is_complex_ = value; }
void set_will_change(bool value) { will_change_ = value; }

SkPicture* picture() const { return picture_.get().get(); }

void Preroll(PrerollContext* frame, const SkMatrix& matrix) override;
Expand Down
5 changes: 4 additions & 1 deletion flow/layers/platform_view_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

namespace flutter {

PlatformViewLayer::PlatformViewLayer() = default;
PlatformViewLayer::PlatformViewLayer(const SkPoint& offset,
const SkSize& size,
int64_t view_id)
: offset_(offset), size_(size), view_id_(view_id) {}

PlatformViewLayer::~PlatformViewLayer() = default;

Expand Down
6 changes: 1 addition & 5 deletions flow/layers/platform_view_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@ namespace flutter {

class PlatformViewLayer : public Layer {
public:
PlatformViewLayer();
PlatformViewLayer(const SkPoint& offset, const SkSize& size, int64_t view_id);
~PlatformViewLayer() override;

void set_offset(const SkPoint& offset) { offset_ = offset; }
void set_size(const SkSize& size) { size_ = size; }
void set_view_id(int64_t view_id) { view_id_ = view_id; }

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

Expand Down
5 changes: 4 additions & 1 deletion flow/layers/shader_mask_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

namespace flutter {

ShaderMaskLayer::ShaderMaskLayer() = default;
ShaderMaskLayer::ShaderMaskLayer(sk_sp<SkShader> shader,
const SkRect& mask_rect,
SkBlendMode blend_mode)
: shader_(shader), mask_rect_(mask_rect), blend_mode_(blend_mode) {}

ShaderMaskLayer::~ShaderMaskLayer() = default;

Expand Down
10 changes: 3 additions & 7 deletions flow/layers/shader_mask_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ namespace flutter {

class ShaderMaskLayer : public ContainerLayer {
public:
ShaderMaskLayer();
ShaderMaskLayer(sk_sp<SkShader> shader,
const SkRect& mask_rect,
SkBlendMode blend_mode);
~ShaderMaskLayer() override;

void set_shader(sk_sp<SkShader> shader) { shader_ = shader; }

void set_mask_rect(const SkRect& mask_rect) { mask_rect_ = mask_rect; }

void set_blend_mode(SkBlendMode blend_mode) { blend_mode_ = blend_mode; }

void Paint(PaintContext& context) const override;

private:
Expand Down
6 changes: 5 additions & 1 deletion flow/layers/texture_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

namespace flutter {

TextureLayer::TextureLayer() = default;
TextureLayer::TextureLayer(const SkPoint& offset,
const SkSize& size,
int64_t texture_id,
bool freeze)
: offset_(offset), size_(size), texture_id_(texture_id), freeze_(freeze) {}

TextureLayer::~TextureLayer() = default;

Expand Down
10 changes: 4 additions & 6 deletions flow/layers/texture_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ namespace flutter {

class TextureLayer : public Layer {
public:
TextureLayer();
TextureLayer(const SkPoint& offset,
const SkSize& size,
int64_t texture_id,
bool freeze);
~TextureLayer() override;

void set_offset(const SkPoint& offset) { offset_ = offset; }
void set_size(const SkSize& size) { size_ = size; }
void set_texture_id(int64_t texture_id) { texture_id_ = texture_id; }
void set_freeze(bool freeze) { freeze_ = freeze; }

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

Expand Down
Loading

0 comments on commit b622d43

Please sign in to comment.