Skip to content

Commit

Permalink
Untie paths from entities (flutter#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdero authored and dnfield committed Apr 27, 2022
1 parent df261f9 commit c5474f6
Show file tree
Hide file tree
Showing 27 changed files with 171 additions and 175 deletions.
13 changes: 4 additions & 9 deletions impeller/aiks/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "flutter/fml/logging.h"
#include "impeller/aiks/paint_pass_delegate.h"
#include "impeller/entity/contents/clear_contents.h"
#include "impeller/entity/contents/clip_contents.h"
#include "impeller/entity/contents/text_contents.h"
#include "impeller/entity/contents/texture_contents.h"
Expand Down Expand Up @@ -111,10 +110,9 @@ void Canvas::RestoreToCount(size_t count) {
void Canvas::DrawPath(Path path, Paint paint) {
Entity entity;
entity.SetTransformation(GetCurrentTransformation());
entity.SetPath(std::move(path));
entity.SetStencilDepth(GetStencilDepth());
entity.SetBlendMode(paint.blend_mode);
entity.SetContents(paint.WithFilters(paint.CreateContentsForEntity()));
entity.SetContents(paint.WithFilters(paint.CreateContentsForEntity(std::move(path))));

GetCurrentPass().AddEntity(std::move(entity));
}
Expand All @@ -124,8 +122,7 @@ void Canvas::DrawPaint(Paint paint) {
entity.SetTransformation(GetCurrentTransformation());
entity.SetStencilDepth(GetStencilDepth());
entity.SetBlendMode(paint.blend_mode);
entity.SetContents(
std::make_shared<ClearContents>(paint.CreateContentsForEntity()));
entity.SetContents(paint.CreateContentsForEntity({}, true));

GetCurrentPass().AddEntity(std::move(entity));
}
Expand Down Expand Up @@ -158,11 +155,11 @@ void Canvas::SaveLayer(Paint paint, std::optional<Rect> bounds) {

void Canvas::ClipPath(Path path, Entity::ClipOperation clip_op) {
auto contents = std::make_shared<ClipContents>();
contents->SetPath(std::move(path));
contents->SetClipOperation(clip_op);

Entity entity;
entity.SetTransformation(GetCurrentTransformation());
entity.SetPath(std::move(path));
entity.SetContents(std::move(contents));
entity.SetStencilDepth(GetStencilDepth());
entity.SetAddsToCoverage(false);
Expand All @@ -178,7 +175,6 @@ void Canvas::RestoreClip() {
entity.SetTransformation(GetCurrentTransformation());
// This path is empty because ClipRestoreContents just generates a quad that
// takes up the full render target.
entity.SetPath({});
entity.SetContents(std::make_shared<ClipRestoreContents>());
entity.SetStencilDepth(GetStencilDepth());
entity.SetAddsToCoverage(false);
Expand Down Expand Up @@ -234,12 +230,12 @@ void Canvas::DrawImageRect(std::shared_ptr<Image> image,
}

auto contents = std::make_shared<TextureContents>();
contents->SetPath(PathBuilder{}.AddRect(dest).TakePath());
contents->SetTexture(image->GetTexture());
contents->SetSourceRect(source);
contents->SetSamplerDescriptor(std::move(sampler));

Entity entity;
entity.SetPath(PathBuilder{}.AddRect(dest).TakePath());
entity.SetBlendMode(paint.blend_mode);
entity.SetStencilDepth(GetStencilDepth());
entity.SetContents(paint.WithFilters(contents, false));
Expand Down Expand Up @@ -293,7 +289,6 @@ void Canvas::DrawTextFrame(TextFrame text_frame, Point position, Paint paint) {
Entity entity;
entity.SetTransformation(GetCurrentTransformation() *
Matrix::MakeTranslation(position));
entity.SetPath({});
entity.SetStencilDepth(GetStencilDepth());
entity.SetBlendMode(paint.blend_mode);
entity.SetContents(paint.WithFilters(std::move(text_contents), true));
Expand Down
6 changes: 5 additions & 1 deletion impeller/aiks/paint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@

namespace impeller {

std::shared_ptr<Contents> Paint::CreateContentsForEntity() const {
std::shared_ptr<Contents> Paint::CreateContentsForEntity(Path path,
bool cover) const {
if (contents) {
return contents;
}

switch (style) {
case Style::kFill: {
auto solid_color = std::make_shared<SolidColorContents>();
solid_color->SetPath(std::move(path));
solid_color->SetColor(color);
solid_color->SetCover(cover);
return solid_color;
}
case Style::kStroke: {
auto solid_stroke = std::make_shared<SolidStrokeContents>();
solid_stroke->SetPath(std::move(path));
solid_stroke->SetColor(color);
solid_stroke->SetStrokeSize(stroke_width);
solid_stroke->SetStrokeMiter(stroke_miter);
Expand Down
5 changes: 3 additions & 2 deletions impeller/aiks/paint.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ struct Paint {
std::optional<MaskBlur> mask_blur;
std::shared_ptr<Contents> contents;

std::shared_ptr<Contents> CreateContentsForEntity() const;

/// @brief Wrap this paint's configured filters to the given contents.
/// @param[in] input The contents to wrap with paint's filters.
/// @param[in] is_solid_color Affects mask blurring behavior. If false, use
Expand All @@ -52,6 +50,9 @@ struct Paint {
std::shared_ptr<Contents> WithFilters(
std::shared_ptr<Contents> input,
std::optional<bool> is_solid_color = std::nullopt) const;

std::shared_ptr<Contents> CreateContentsForEntity(Path path = {},
bool cover = false) const;
};

} // namespace impeller
4 changes: 4 additions & 0 deletions impeller/aiks/paint_pass_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "impeller/entity/contents/contents.h"
#include "impeller/entity/contents/texture_contents.h"
#include "impeller/geometry/path_builder.h"

namespace impeller {

Expand Down Expand Up @@ -34,6 +35,9 @@ bool PaintPassDelegate::CanCollapseIntoParentPass() {
std::shared_ptr<Contents> PaintPassDelegate::CreateContentsForSubpassTarget(
std::shared_ptr<Texture> target) {
auto contents = std::make_shared<TextureContents>();
contents->SetPath(PathBuilder{}
.AddRect(Rect::MakeSize(Size(target->GetSize())))
.TakePath());
contents->SetTexture(target);
contents->SetSourceRect(Rect::MakeSize(Size(target->GetSize())));
contents->SetOpacity(paint_.color.alpha);
Expand Down
2 changes: 0 additions & 2 deletions impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ impeller_shaders("entity_shaders") {

impeller_component("entity") {
sources = [
"contents/clear_contents.cc",
"contents/clear_contents.h",
"contents/clip_contents.cc",
"contents/clip_contents.h",
"contents/content_context.cc",
Expand Down
34 changes: 0 additions & 34 deletions impeller/entity/contents/clear_contents.cc

This file was deleted.

33 changes: 0 additions & 33 deletions impeller/entity/contents/clear_contents.h

This file was deleted.

16 changes: 15 additions & 1 deletion impeller/entity/contents/clip_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <optional>
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/formats.h"
#include "impeller/renderer/vertex_buffer_builder.h"
Expand All @@ -23,10 +24,18 @@ ClipContents::ClipContents() = default;

ClipContents::~ClipContents() = default;

void ClipContents::SetPath(Path path) {
path_ = std::move(path);
}

void ClipContents::SetClipOperation(Entity::ClipOperation clip_op) {
clip_op_ = clip_op;
}

std::optional<Rect> ClipContents::GetCoverage(const Entity& entity) const {
return path_.GetTransformedBoundingBox(entity.GetTransformation());
};

bool ClipContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
Expand Down Expand Up @@ -77,7 +86,7 @@ bool ClipContents::Render(const ContentContext& renderer,

cmd.pipeline = renderer.GetClipPipeline(options);
cmd.BindVertices(SolidColorContents::CreateSolidFillVertices(
entity.GetPath(), pass.GetTransientsBuffer()));
path_, pass.GetTransientsBuffer()));

info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
entity.GetTransformation();
Expand All @@ -95,6 +104,11 @@ ClipRestoreContents::ClipRestoreContents() = default;

ClipRestoreContents::~ClipRestoreContents() = default;

std::optional<Rect> ClipRestoreContents::GetCoverage(
const Entity& entity) const {
return std::nullopt;
};

bool ClipRestoreContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
Expand Down
9 changes: 9 additions & 0 deletions impeller/entity/contents/clip_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ class ClipContents final : public Contents {

~ClipContents();

void SetPath(Path path);

void SetClipOperation(Entity::ClipOperation clip_op);

// |Contents|
std::optional<Rect> GetCoverage(const Entity& entity) const override;

// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const override;

private:
Path path_;
Entity::ClipOperation clip_op_ = Entity::ClipOperation::kIntersect;

FML_DISALLOW_COPY_AND_ASSIGN(ClipContents);
Expand All @@ -39,6 +45,9 @@ class ClipRestoreContents final : public Contents {

~ClipRestoreContents();

// |Contents|
std::optional<Rect> GetCoverage(const Entity& entity) const override;

// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
Expand Down
5 changes: 0 additions & 5 deletions impeller/entity/contents/contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ Contents::Contents() = default;

Contents::~Contents() = default;

std::optional<Rect> Contents::GetCoverage(const Entity& entity) const {
return entity.GetPathCoverage();
}

std::optional<Snapshot> Contents::RenderToSnapshot(
const ContentContext& renderer,
const Entity& entity) const {
Expand All @@ -46,7 +42,6 @@ std::optional<Snapshot> Contents::RenderToSnapshot(
[&contents = *this, &entity, &bounds](const ContentContext& renderer,
RenderPass& pass) -> bool {
Entity sub_entity;
sub_entity.SetPath(entity.GetPath());
sub_entity.SetBlendMode(Entity::BlendMode::kSourceOver);
sub_entity.SetTransformation(
Matrix::MakeTranslation(Vector3(-bounds->origin)) *
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/contents/contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Contents {
RenderPass& pass) const = 0;

/// @brief Get the screen space bounding rectangle that this contents affects.
virtual std::optional<Rect> GetCoverage(const Entity& entity) const;
virtual std::optional<Rect> GetCoverage(const Entity& entity) const = 0;

/// @brief Render this contents to a snapshot, respecting the entity's
/// transform, path, stencil depth, and blend mode.
Expand Down
3 changes: 2 additions & 1 deletion impeller/entity/contents/filters/filter_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ bool FilterContents::Render(const ContentContext& renderer,
// Draw the result texture, respecting the transform and clip stack.

auto contents = std::make_shared<TextureContents>();
contents->SetPath(
PathBuilder{}.AddRect(filter_coverage.value()).GetCurrentPath());
contents->SetTexture(snapshot.texture);
contents->SetSourceRect(Rect::MakeSize(Size(snapshot.texture->GetSize())));

Entity e;
e.SetPath(PathBuilder{}.AddRect(filter_coverage.value()).GetCurrentPath());
e.SetBlendMode(entity.GetBlendMode());
e.SetStencilDepth(entity.GetStencilDepth());
return contents->Render(renderer, e, pass);
Expand Down
14 changes: 2 additions & 12 deletions impeller/entity/contents/filters/filter_input.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,13 @@ std::optional<Snapshot> TextureFilterInput::GetSnapshot(

std::optional<Rect> TextureFilterInput::GetCoverage(
const Entity& entity) const {
auto path_bounds = entity.GetPath().GetBoundingBox();
if (!path_bounds.has_value()) {
return std::nullopt;
}
return Rect::MakeSize(Size(texture_->GetSize()))
.TransformBounds(GetTransform(entity));
}

Matrix TextureFilterInput::GetLocalTransform(const Entity& entity) const {
// Compute the local transform such that the texture will cover the entity
// path bounding box.
auto path_bounds = entity.GetPath().GetBoundingBox();
if (!path_bounds.has_value()) {
return Matrix();
}
return Matrix::MakeTranslation(path_bounds->origin) *
Matrix::MakeScale(Vector2(path_bounds->size) / texture_->GetSize());
// Compute the local transform such that the texture is centered.
return Matrix::MakeTranslation(-Point(texture_->GetSize()) / 2);
}

} // namespace impeller
Loading

0 comments on commit c5474f6

Please sign in to comment.