Skip to content

Commit

Permalink
Make flow::LayerBuilder virtual and plug in the default layer builder. (
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored Oct 11, 2017
1 parent 199620a commit 0b8efb6
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 234 deletions.
2 changes: 2 additions & 0 deletions flow/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ source_set("flow") {
"layers/color_filter_layer.h",
"layers/container_layer.cc",
"layers/container_layer.h",
"layers/default_layer_builder.cc",
"layers/default_layer_builder.h",
"layers/layer.cc",
"layers/layer.h",
"layers/layer_builder.cc",
Expand Down
6 changes: 6 additions & 0 deletions flow/debug_print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ std::ostream& operator<<(std::ostream& os, const SkRect& r) {
return os;
}

std::ostream& operator<<(std::ostream& os, const SkRRect& r) {
os << "LTRB: " << r.rect().fLeft << ", " << r.rect().fTop << ", "
<< r.rect().fRight << ", " << r.rect().fBottom;
return os;
}

std::ostream& operator<<(std::ostream& os, const SkPoint& r) {
os << "XY: " << r.fX << ", " << r.fY;
return os;
Expand Down
2 changes: 2 additions & 0 deletions flow/debug_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "third_party/skia/include/core/SkMatrix.h"
#include "third_party/skia/include/core/SkMatrix44.h"
#include "third_party/skia/include/core/SkPoint3.h"
#include "third_party/skia/include/core/SkRRect.h"

#define DEF_PRINTER(x) std::ostream& operator<<(std::ostream&, const x&);

Expand All @@ -21,6 +22,7 @@ DEF_PRINTER(SkMatrix44);
DEF_PRINTER(SkVector3);
DEF_PRINTER(SkVector4);
DEF_PRINTER(SkRect);
DEF_PRINTER(SkRRect);
DEF_PRINTER(SkPoint);

#endif // FLUTTER_FLOW_DEBUG_PRINT_H_
210 changes: 210 additions & 0 deletions flow/layers/default_layer_builder.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// Copyright 2017 The Flutter 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/default_layer_builder.h"

#include "flutter/flow/layers/backdrop_filter_layer.h"
#include "flutter/flow/layers/clip_path_layer.h"
#include "flutter/flow/layers/clip_rect_layer.h"
#include "flutter/flow/layers/clip_rrect_layer.h"
#include "flutter/flow/layers/color_filter_layer.h"
#include "flutter/flow/layers/container_layer.h"
#include "flutter/flow/layers/layer.h"
#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/picture_layer.h"
#include "flutter/flow/layers/shader_mask_layer.h"
#include "flutter/flow/layers/transform_layer.h"

#if defined(OS_FUCHSIA)
#include "flutter/flow/layers/child_scene_layer.h"
#endif // defined(OS_FUCHSIA)

namespace flow {

DefaultLayerBuilder::DefaultLayerBuilder() {
cull_rects_.push(SkRect::MakeLargest());
}

DefaultLayerBuilder::~DefaultLayerBuilder() = default;

void DefaultLayerBuilder::PushTransform(const SkMatrix& sk_matrix) {
SkMatrix inverse_sk_matrix;
SkRect cullRect;
if (sk_matrix.invert(&inverse_sk_matrix)) {
inverse_sk_matrix.mapRect(&cullRect, cull_rects_.top());
} else {
cullRect = SkRect::MakeLargest();
}

auto layer = std::make_unique<flow::TransformLayer>();
layer->set_transform(sk_matrix);
PushLayer(std::move(layer), cullRect);
}

void DefaultLayerBuilder::PushClipRect(const SkRect& clipRect) {
SkRect cullRect;
if (!cullRect.intersect(clipRect, cull_rects_.top())) {
cullRect = SkRect::MakeEmpty();
}
auto layer = std::make_unique<flow::ClipRectLayer>();
layer->set_clip_rect(clipRect);
PushLayer(std::move(layer), cullRect);
}

void DefaultLayerBuilder::PushClipRoundedRect(const SkRRect& rrect) {
SkRect cullRect;
if (!cullRect.intersect(rrect.rect(), cull_rects_.top())) {
cullRect = SkRect::MakeEmpty();
}
auto layer = std::make_unique<flow::ClipRRectLayer>();
layer->set_clip_rrect(rrect);
PushLayer(std::move(layer), cullRect);
}

void DefaultLayerBuilder::PushClipPath(const SkPath& path) {
SkRect cullRect;
if (!cullRect.intersect(path.getBounds(), cull_rects_.top())) {
cullRect = SkRect::MakeEmpty();
}
auto layer = std::make_unique<flow::ClipPathLayer>();
layer->set_clip_path(path);
PushLayer(std::move(layer), cullRect);
}

void DefaultLayerBuilder::PushOpacity(int alpha) {
auto layer = std::make_unique<flow::OpacityLayer>();
layer->set_alpha(alpha);
PushLayer(std::move(layer), cull_rects_.top());
}

void DefaultLayerBuilder::PushColorFilter(SkColor color,
SkBlendMode blend_mode) {
auto layer = std::make_unique<flow::ColorFilterLayer>();
layer->set_color(color);
layer->set_blend_mode(blend_mode);
PushLayer(std::move(layer), cull_rects_.top());
}

void DefaultLayerBuilder::PushBackdropFilter(sk_sp<SkImageFilter> filter) {
auto layer = std::make_unique<flow::BackdropFilterLayer>();
layer->set_filter(filter);
PushLayer(std::move(layer), cull_rects_.top());
}

void DefaultLayerBuilder::PushShaderMask(sk_sp<SkShader> shader,
const SkRect& rect,
SkBlendMode blend_mode) {
auto layer = std::make_unique<flow::ShaderMaskLayer>();
layer->set_shader(shader);
layer->set_mask_rect(rect);
layer->set_blend_mode(blend_mode);
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_rrect(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::PushPerformanceOverlay(uint64_t enabled_options,
const SkRect& rect) {
if (!current_layer_) {
return;
}
auto layer = std::make_unique<flow::PerformanceOverlayLayer>(enabled_options);
layer->set_paint_bounds(rect);
current_layer_->Add(std::move(layer));
}

void DefaultLayerBuilder::PushPicture(const SkPoint& offset,
sk_sp<SkPicture> picture,
bool picture_is_complex,
bool picture_will_change) {
if (!current_layer_) {
return;
}
SkRect pictureRect = picture->cullRect();
pictureRect.offset(offset.x(), offset.y());
if (!SkRect::Intersects(pictureRect, cull_rects_.top())) {
return;
}
auto layer = std::make_unique<flow::PictureLayer>();
layer->set_offset(offset);
layer->set_picture(picture);
layer->set_is_complex(picture_is_complex);
layer->set_will_change(picture_will_change);
current_layer_->Add(std::move(layer));
}

#if defined(OS_FUCHSIA)
void DefaultLayerBuilder::PushChildScene(
const SkPoint& offset,
const SkSize& size,
fxl::RefPtr<flow::ExportNodeHolder> export_token_holder,
bool hit_testable) {
if (!current_layer_) {
return;
}
SkRect sceneRect =
SkRect::MakeXYWH(offset.x(), offset.y(), size.width(), size.height());
if (!SkRect::Intersects(sceneRect, cull_rects_.top())) {
return;
}
auto layer = std::make_unique<flow::ChildSceneLayer>();
layer->set_offset(offset);
layer->set_size(size);
layer->set_export_node_holder(std::move(export_token_holder));
layer->set_hit_testable(hit_testable);
current_layer_->Add(std::move(layer));
}
#endif // defined(OS_FUCHSIA)

void DefaultLayerBuilder::Pop() {
if (!current_layer_) {
return;
}
cull_rects_.pop();
current_layer_ = current_layer_->parent();
}

std::unique_ptr<flow::Layer> DefaultLayerBuilder::TakeLayer() {
return std::move(root_layer_);
}

void DefaultLayerBuilder::PushLayer(std::unique_ptr<flow::ContainerLayer> layer,
const SkRect& cullRect) {
FXL_DCHECK(layer);

cull_rects_.push(cullRect);

if (!root_layer_) {
root_layer_ = std::move(layer);
current_layer_ = root_layer_.get();
return;
}

if (!current_layer_) {
return;
}

flow::ContainerLayer* newLayer = layer.get();
current_layer_->Add(std::move(layer));
current_layer_ = newLayer;
}

} // namespace flow
93 changes: 93 additions & 0 deletions flow/layers/default_layer_builder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2017 The Flutter 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_DEFAULT_LAYER_BUILDER_H_
#define FLUTTER_FLOW_LAYERS_DEFAULT_LAYER_BUILDER_H_

#include <stack>

#include "flutter/flow/layers/container_layer.h"
#include "flutter/flow/layers/layer_builder.h"
#include "garnet/public/lib/fxl/macros.h"

namespace flow {

class DefaultLayerBuilder final : public LayerBuilder {
public:
DefaultLayerBuilder();

// |flow::LayerBuilder|
~DefaultLayerBuilder() override;

// |flow::LayerBuilder|
void PushTransform(const SkMatrix& matrix) override;

// |flow::LayerBuilder|
void PushClipRect(const SkRect& rect) override;

// |flow::LayerBuilder|
void PushClipRoundedRect(const SkRRect& rect) override;

// |flow::LayerBuilder|
void PushClipPath(const SkPath& path) override;

// |flow::LayerBuilder|
void PushOpacity(int alpha) override;

// |flow::LayerBuilder|
void PushColorFilter(SkColor color, SkBlendMode blend_mode) override;

// |flow::LayerBuilder|
void PushBackdropFilter(sk_sp<SkImageFilter> filter) override;

// |flow::LayerBuilder|
void PushShaderMask(sk_sp<SkShader> shader,
const SkRect& rect,
SkBlendMode blend_mode) override;

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

// |flow::LayerBuilder|
void PushPerformanceOverlay(uint64_t enabled_options,
const SkRect& rect) override;

// |flow::LayerBuilder|
void PushPicture(const SkPoint& offset,
sk_sp<SkPicture> picture,
bool picture_is_complex,
bool picture_will_change) override;

#if defined(OS_FUCHSIA)
// |flow::LayerBuilder|
void PushChildScene(const SkPoint& offset,
const SkSize& size,
fxl::RefPtr<flow::ExportNodeHolder> export_token_holder,
bool hit_testable) override;
#endif // defined(OS_FUCHSIA)

// |flow::LayerBuilder|
void Pop() override;

// |flow::LayerBuilder|
std::unique_ptr<flow::Layer> TakeLayer() override;

private:
std::unique_ptr<flow::ContainerLayer> root_layer_;
flow::ContainerLayer* current_layer_ = nullptr;

std::stack<SkRect> cull_rects_;

void PushLayer(std::unique_ptr<flow::ContainerLayer> layer,
const SkRect& cullRect);

FXL_DISALLOW_COPY_AND_ASSIGN(DefaultLayerBuilder);
};

} // namespace flow

#endif // FLUTTER_FLOW_LAYERS_DEFAULT_LAYER_BUILDER_H_
Loading

0 comments on commit 0b8efb6

Please sign in to comment.