Untie paths from entities (flutter/engine#145)

This commit is contained in:
Brandon DeRosier
2022-04-21 15:18:47 -07:00
committed by Dan Field
parent fa9bf454e3
commit cc598ce74a
27 changed files with 171 additions and 175 deletions

View File

@@ -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"
@@ -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));
}
@@ -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));
}
@@ -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);
@@ -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);
@@ -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));
@@ -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));

View File

@@ -8,7 +8,8 @@
namespace impeller {
std::shared_ptr<Contents> Paint::CreateContentsForEntity() const {
std::shared_ptr<Contents> Paint::CreateContentsForEntity(Path path,
bool cover) const {
if (contents) {
return contents;
}
@@ -16,11 +17,14 @@ std::shared_ptr<Contents> Paint::CreateContentsForEntity() const {
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);

View File

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

View File

@@ -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 {
@@ -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);

View File

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

View File

@@ -1,34 +0,0 @@
// Copyright 2013 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 "impeller/entity/contents/clear_contents.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/render_pass.h"
namespace impeller {
ClearContents::ClearContents(std::shared_ptr<Contents> contents)
: contents_(std::move(contents)) {}
ClearContents::~ClearContents() = default;
// |Contents|
bool ClearContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
if (contents_ == nullptr) {
return false;
}
// Instead of an entity that doesn't know its size because the render target
// size was unknown to it at construction time, create a copy but substitute
// the contents with the replacements.
Entity clear_entity = entity;
clear_entity.SetPath(
PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath());
return contents_->Render(renderer, clear_entity, pass);
}
} // namespace impeller

View File

@@ -1,33 +0,0 @@
// Copyright 2013 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.
#pragma once
#include "flutter/fml/macros.h"
#include "impeller/entity/contents/contents.h"
namespace impeller {
//------------------------------------------------------------------------------
/// @brief Disregard existing contents on the render target and use the
/// provided filter to render to the entire target.
///
class ClearContents final : public Contents {
public:
ClearContents(std::shared_ptr<Contents> contents);
~ClearContents();
// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const override;
private:
std::shared_ptr<Contents> contents_;
FML_DISALLOW_COPY_AND_ASSIGN(ClearContents);
};
} // namespace impeller

View File

@@ -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"
@@ -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 {
@@ -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();
@@ -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 {

View File

@@ -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);
@@ -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,

View File

@@ -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 {
@@ -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)) *

View File

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

View File

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

View File

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

View File

@@ -15,6 +15,10 @@ LinearGradientContents::LinearGradientContents() = default;
LinearGradientContents::~LinearGradientContents() = default;
void LinearGradientContents::SetPath(Path path) {
path_ = std::move(path);
}
void LinearGradientContents::SetEndPoints(Point start_point, Point end_point) {
start_point_ = start_point;
end_point_ = end_point;
@@ -34,6 +38,11 @@ const std::vector<Color>& LinearGradientContents::GetColors() const {
return colors_;
}
std::optional<Rect> LinearGradientContents::GetCoverage(
const Entity& entity) const {
return path_.GetTransformedBoundingBox(entity.GetTransformation());
};
bool LinearGradientContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
@@ -42,13 +51,13 @@ bool LinearGradientContents::Render(const ContentContext& renderer,
auto vertices_builder = VertexBufferBuilder<VS::PerVertexData>();
{
auto result = Tessellator{}.Tessellate(entity.GetPath().GetFillType(),
entity.GetPath().CreatePolyline(),
[&vertices_builder](Point point) {
VS::PerVertexData vtx;
vtx.vertices = point;
vertices_builder.AppendVertex(vtx);
});
auto result =
Tessellator{}.Tessellate(path_.GetFillType(), path_.CreatePolyline(),
[&vertices_builder](Point point) {
VS::PerVertexData vtx;
vtx.vertices = point;
vertices_builder.AppendVertex(vtx);
});
if (!result) {
return false;
}

View File

@@ -11,6 +11,7 @@
#include "flutter/fml/macros.h"
#include "impeller/entity/contents/contents.h"
#include "impeller/geometry/color.h"
#include "impeller/geometry/path.h"
#include "impeller/geometry/point.h"
namespace impeller {
@@ -21,6 +22,11 @@ class LinearGradientContents final : public Contents {
~LinearGradientContents() override;
void SetPath(Path path);
// |Contents|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
@@ -33,6 +39,7 @@ class LinearGradientContents final : public Contents {
const std::vector<Color>& GetColors() const;
private:
Path path_;
Point start_point_;
Point end_point_;
std::vector<Color> colors_;

View File

@@ -7,6 +7,7 @@
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/entity.h"
#include "impeller/geometry/path.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/tessellator/tessellator.h"
@@ -24,6 +25,19 @@ const Color& SolidColorContents::GetColor() const {
return color_;
}
void SolidColorContents::SetPath(Path path) {
path_ = std::move(path);
}
void SolidColorContents::SetCover(bool cover) {
cover_ = cover;
}
std::optional<Rect> SolidColorContents::GetCoverage(
const Entity& entity) const {
return path_.GetTransformedBoundingBox(entity.GetTransformation());
};
VertexBuffer SolidColorContents::CreateSolidFillVertices(const Path& path,
HostBuffer& buffer) {
using VS = SolidFillPipeline::VertexShader;
@@ -57,8 +71,12 @@ bool SolidColorContents::Render(const ContentContext& renderer,
cmd.pipeline =
renderer.GetSolidFillPipeline(OptionsFromPassAndEntity(pass, entity));
cmd.stencil_reference = entity.GetStencilDepth();
cmd.BindVertices(
CreateSolidFillVertices(entity.GetPath(), pass.GetTransientsBuffer()));
cmd.BindVertices(CreateSolidFillVertices(
cover_
? PathBuilder{}.AddRect(Size(pass.GetRenderTargetSize())).TakePath()
: path_,
pass.GetTransientsBuffer()));
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
@@ -75,8 +93,10 @@ bool SolidColorContents::Render(const ContentContext& renderer,
return true;
}
std::unique_ptr<SolidColorContents> SolidColorContents::Make(Color color) {
std::unique_ptr<SolidColorContents> SolidColorContents::Make(Path path,
Color color) {
auto contents = std::make_unique<SolidColorContents>();
contents->SetPath(std::move(path));
contents->SetColor(color);
return contents;
}

View File

@@ -11,6 +11,7 @@
#include "flutter/fml/macros.h"
#include "impeller/entity/contents/contents.h"
#include "impeller/geometry/color.h"
#include "impeller/geometry/path.h"
namespace impeller {
@@ -24,21 +25,31 @@ class SolidColorContents final : public Contents {
~SolidColorContents() override;
static std::unique_ptr<SolidColorContents> Make(Color color);
static std::unique_ptr<SolidColorContents> Make(Path path, Color color);
static VertexBuffer CreateSolidFillVertices(const Path& path,
HostBuffer& buffer);
void SetPath(Path path);
void SetCover(bool cover);
void SetColor(Color color);
const Color& GetColor() const;
// |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_;
bool cover_ = false;
Color color_;
FML_DISALLOW_COPY_AND_ASSIGN(SolidColorContents);

View File

@@ -29,12 +29,17 @@ const Color& SolidStrokeContents::GetColor() const {
return color_;
}
void SolidStrokeContents::SetPath(Path path) {
path_ = std::move(path);
}
std::optional<Rect> SolidStrokeContents::GetCoverage(
const Entity& entity) const {
auto path_coverage = entity.GetPathCoverage();
if (!path_coverage.has_value()) {
auto path_bounds = path_.GetBoundingBox();
if (!path_bounds.has_value()) {
return std::nullopt;
}
auto path_coverage = path_bounds->TransformBounds(entity.GetTransformation());
Scalar max_radius = 0.5;
if (cap_ == Cap::kSquare) {
@@ -46,9 +51,9 @@ std::optional<Rect> SolidStrokeContents::GetCoverage(
Vector2 max_radius_xy = entity.GetTransformation().TransformDirection(
Vector2(max_radius, max_radius) * stroke_size_);
return Rect(path_coverage->origin - max_radius_xy,
Size(path_coverage->size.width + max_radius_xy.x * 2,
path_coverage->size.height + max_radius_xy.y * 2));
return Rect(path_coverage.origin - max_radius_xy,
Size(path_coverage.size.width + max_radius_xy.x * 2,
path_coverage.size.height + max_radius_xy.y * 2));
}
static VertexBuffer CreateSolidStrokeVertices(
@@ -197,7 +202,7 @@ bool SolidStrokeContents::Render(const ContentContext& renderer,
5.0 / (stroke_size_ * entity.GetTransformation().GetMaxBasisLength()),
0.0, 0.0);
cmd.BindVertices(CreateSolidStrokeVertices(
entity.GetPath(), pass.GetTransientsBuffer(), cap_proc_, join_proc_,
path_, pass.GetTransientsBuffer(), cap_proc_, join_proc_,
miter_limit_, smoothing));
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
VS::BindStrokeInfo(cmd,

View File

@@ -48,6 +48,8 @@ class SolidStrokeContents final : public Contents {
~SolidStrokeContents() override;
void SetPath(Path path);
void SetColor(Color color);
const Color& GetColor() const;
@@ -77,6 +79,8 @@ class SolidStrokeContents final : public Contents {
RenderPass& pass) const override;
private:
Path path_;
Color color_;
Scalar stroke_size_ = 0.0;
Scalar miter_limit_ = 4.0;

View File

@@ -18,6 +18,10 @@ TextureContents::TextureContents() = default;
TextureContents::~TextureContents() = default;
void TextureContents::SetPath(Path path) {
path_ = std::move(path);
}
void TextureContents::SetTexture(std::shared_ptr<Texture> texture) {
texture_ = std::move(texture);
}
@@ -30,6 +34,10 @@ void TextureContents::SetOpacity(Scalar opacity) {
opacity_ = opacity;
}
std::optional<Rect> TextureContents::GetCoverage(const Entity& entity) const {
return path_.GetTransformedBoundingBox(entity.GetTransformation());
};
bool TextureContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
@@ -40,7 +48,7 @@ bool TextureContents::Render(const ContentContext& renderer,
using VS = TextureFillVertexShader;
using FS = TextureFillFragmentShader;
const auto coverage_rect = entity.GetPath().GetBoundingBox();
const auto coverage_rect = path_.GetBoundingBox();
if (!coverage_rect.has_value()) {
return true;
@@ -62,7 +70,7 @@ bool TextureContents::Render(const ContentContext& renderer,
VertexBufferBuilder<VS::PerVertexData> vertex_builder;
{
const auto tess_result = Tessellator{}.Tessellate(
entity.GetPath().GetFillType(), entity.GetPath().CreatePolyline(),
path_.GetFillType(), path_.CreatePolyline(),
[this, &vertex_builder, &coverage_rect, &texture_size](Point vtx) {
VS::PerVertexData data;
data.vertices = vtx;

View File

@@ -10,6 +10,7 @@
#include "flutter/fml/macros.h"
#include "impeller/entity/contents/contents.h"
#include "impeller/geometry/path.h"
#include "impeller/renderer/sampler_descriptor.h"
namespace impeller {
@@ -22,6 +23,8 @@ class TextureContents final : public Contents {
~TextureContents() override;
void SetPath(Path path);
void SetTexture(std::shared_ptr<Texture> texture);
std::shared_ptr<Texture> GetTexture() const;
@@ -36,12 +39,17 @@ class TextureContents final : public Contents {
void SetOpacity(Scalar opacity);
// |Contents|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const override;
public:
Path path_;
std::shared_ptr<Texture> texture_;
SamplerDescriptor sampler_descriptor_ = {};
Rect source_rect_;

View File

@@ -24,22 +24,6 @@ void Entity::SetTransformation(const Matrix& transformation) {
transformation_ = transformation;
}
const Path& Entity::GetPath() const {
return path_;
}
void Entity::SetPath(Path path) {
path_ = std::move(path);
}
std::optional<Rect> Entity::GetPathCoverage() const {
auto bounds = GetPath().GetBoundingBox();
if (!bounds.has_value()) {
return std::nullopt;
}
return bounds->TransformBounds(GetTransformation());
}
void Entity::SetAddsToCoverage(bool adds) {
adds_to_coverage_ = adds;
}

View File

@@ -61,12 +61,6 @@ class Entity {
void SetTransformation(const Matrix& transformation);
const Path& GetPath() const;
void SetPath(Path path);
std::optional<Rect> GetPathCoverage() const;
void SetAddsToCoverage(bool adds);
bool AddsToCoverage() const;
@@ -93,7 +87,6 @@ class Entity {
Matrix transformation_;
std::shared_ptr<Contents> contents_;
BlendMode blend_mode_ = BlendMode::kSourceOver;
Path path_;
uint32_t stencil_depth_ = 0u;
bool adds_to_coverage_ = true;
};

View File

@@ -159,7 +159,6 @@ bool EntityPass::Render(ContentContext& renderer,
}
const auto subpass_coverage = GetSubpassCoverage(*subpass);
if (!subpass_coverage.has_value()) {
continue;
}
@@ -227,9 +226,6 @@ bool EntityPass::Render(ContentContext& renderer,
}
Entity entity;
entity.SetPath(PathBuilder{}
.AddRect(Rect::MakeSize(subpass_coverage->size))
.TakePath());
entity.SetContents(std::move(offscreen_texture_contents));
entity.SetStencilDepth(stencil_depth_);
entity.SetBlendMode(subpass->blend_mode_);

View File

@@ -33,8 +33,8 @@ TEST_P(EntityTest, CanCreateEntity) {
TEST_P(EntityTest, CanDrawRect) {
Entity entity;
entity.SetPath(PathBuilder{}.AddRect({100, 100, 100, 100}).TakePath());
entity.SetContents(SolidColorContents::Make(Color::Red()));
entity.SetContents(SolidColorContents::Make(
PathBuilder{}.AddRect({100, 100, 100, 100}).TakePath(), Color::Red()));
ASSERT_TRUE(OpenPlaygroundHere(entity));
}
@@ -49,8 +49,8 @@ TEST_P(EntityTest, ThreeStrokesInOnePath) {
.TakePath();
Entity entity;
entity.SetPath(path);
auto contents = std::make_unique<SolidStrokeContents>();
contents->SetPath(std::move(path));
contents->SetColor(Color::Red());
contents->SetStrokeSize(5.0);
entity.SetContents(std::move(contents));
@@ -79,8 +79,8 @@ TEST_P(EntityTest, TriangleInsideASquare) {
.TakePath();
Entity entity;
entity.SetPath(path);
auto contents = std::make_unique<SolidStrokeContents>();
contents->SetPath(std::move(path));
contents->SetColor(Color::Red());
contents->SetStrokeSize(20.0);
entity.SetContents(std::move(contents));
@@ -124,6 +124,7 @@ TEST_P(EntityTest, StrokeCapAndJoinTest) {
Path path, SolidStrokeContents::Cap cap,
SolidStrokeContents::Join join) {
auto contents = std::make_unique<SolidStrokeContents>();
contents->SetPath(path);
contents->SetColor(Color::Red());
contents->SetStrokeSize(width);
contents->SetStrokeCap(cap);
@@ -131,16 +132,15 @@ TEST_P(EntityTest, StrokeCapAndJoinTest) {
contents->SetStrokeMiter(miter_limit);
Entity entity;
entity.SetPath(path);
entity.SetContents(std::move(contents));
auto coverage = entity.GetCoverage();
if (coverage.has_value()) {
auto bounds_contents = std::make_unique<SolidColorContents>();
bounds_contents->SetPath(
PathBuilder{}.AddRect(entity.GetCoverage().value()).TakePath());
bounds_contents->SetColor(Color::Green().WithAlpha(0.5));
Entity bounds_entity;
bounds_entity.SetPath(
PathBuilder{}.AddRect(entity.GetCoverage().value()).TakePath());
bounds_entity.SetContents(std::move(bounds_contents));
bounds_entity.Render(context, pass);
}
@@ -242,8 +242,7 @@ TEST_P(EntityTest, CubicCurveTest) {
.Close()
.TakePath();
Entity entity;
entity.SetPath(path);
entity.SetContents(SolidColorContents::Make(Color::Red()));
entity.SetContents(SolidColorContents::Make(path, Color::Red()));
ASSERT_TRUE(OpenPlaygroundHere(entity));
}
@@ -470,8 +469,7 @@ TEST_P(EntityTest, CubicCurveAndOverlapTest) {
.Close()
.TakePath();
Entity entity;
entity.SetPath(path);
entity.SetContents(SolidColorContents::Make(Color::Red()));
entity.SetContents(SolidColorContents::Make(path, Color::Red()));
ASSERT_TRUE(OpenPlaygroundHere(entity));
}
@@ -656,10 +654,9 @@ TEST_P(EntityTest, BezierCircleScaled) {
{97.32499434685802, 34.81799797758954})
.Close()
.TakePath();
entity.SetPath(path);
entity.SetTransformation(
Matrix::MakeScale({20.0, 20.0, 1.0}).Translate({-80, -15, 0}));
entity.SetContents(SolidColorContents::Make(Color::Red()));
entity.SetContents(SolidColorContents::Make(path, Color::Red()));
ASSERT_TRUE(OpenPlaygroundHere(entity));
}
@@ -682,7 +679,8 @@ TEST_P(EntityTest, Filters) {
{fi_bridge, FilterInput::Make(blend0), fi_bridge, fi_bridge});
Entity entity;
entity.SetPath(PathBuilder{}.AddRect({100, 100, 300, 300}).TakePath());
entity.SetTransformation(Matrix::MakeTranslation({500, 300}) *
Matrix::MakeScale(Vector2{0.5, 0.5}));
entity.SetContents(blend1);
return entity.Render(context, pass);
};
@@ -717,7 +715,7 @@ TEST_P(EntityTest, GaussianBlurFilter) {
static Color bounds_color(0, 1, 0, 0.1);
static float offset[2] = {500, 400};
static float rotation = 0;
static float scale[2] = {0.8, 0.8};
static float scale[2] = {0.5, 0.5};
static float skew[2] = {0, 0};
ImGui::Begin("Controls");
@@ -752,8 +750,6 @@ TEST_P(EntityTest, GaussianBlurFilter) {
FilterContents::Sigma{blur_amount[1]},
blur_styles[selected_blur_style]);
ISize input_size = boston->GetSize();
auto rect = Rect(-Point(input_size) / 2, Size(input_size));
auto ctm = Matrix::MakeTranslation(Vector3(offset[0], offset[1])) *
Matrix::MakeRotationZ(Radians(rotation)) *
Matrix::MakeScale(Vector2(scale[0], scale[1])) *
@@ -762,7 +758,6 @@ TEST_P(EntityTest, GaussianBlurFilter) {
auto target_contents = selected_blur_type == 0 ? blur : mask_blur;
Entity entity;
entity.SetPath(PathBuilder{}.AddRect(rect).TakePath());
entity.SetContents(target_contents);
entity.SetTransformation(ctm);
@@ -771,19 +766,23 @@ TEST_P(EntityTest, GaussianBlurFilter) {
// Renders a red "cover" rectangle that shows the original position of the
// unfiltered input.
Entity cover_entity;
cover_entity.SetPath(PathBuilder{}.AddRect(rect).TakePath());
cover_entity.SetContents(SolidColorContents::Make(cover_color));
cover_entity.SetContents(
SolidColorContents::Make(PathBuilder{}
.AddRect(Rect(-Point(bridge->GetSize()) / 2,
Size(bridge->GetSize())))
.TakePath(),
cover_color));
cover_entity.SetTransformation(ctm);
cover_entity.Render(context, pass);
// Renders a green bounding rect of the target filter.
Entity bounds_entity;
bounds_entity.SetPath(
bounds_entity.SetContents(SolidColorContents::Make(
PathBuilder{}
.AddRect(target_contents->GetCoverage(entity).value())
.TakePath());
bounds_entity.SetContents(SolidColorContents::Make(bounds_color));
.TakePath(),
bounds_color));
bounds_entity.SetTransformation(Matrix());
bounds_entity.Render(context, pass);
@@ -803,19 +802,17 @@ TEST_P(EntityTest, SetBlendMode) {
TEST_P(EntityTest, ContentsGetBoundsForEmptyPathReturnsNullopt) {
Entity entity;
entity.SetContents(std::make_shared<SolidColorContents>());
entity.SetPath({});
ASSERT_FALSE(entity.GetCoverage().has_value());
ASSERT_FALSE(entity.GetPathCoverage().has_value());
}
TEST_P(EntityTest, SolidStrokeCoverageIsCorrect) {
{
Entity entity;
auto contents = std::make_unique<SolidStrokeContents>();
contents->SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath());
contents->SetStrokeCap(SolidStrokeContents::Cap::kButt);
contents->SetStrokeJoin(SolidStrokeContents::Join::kBevel);
contents->SetStrokeSize(4);
entity.SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath());
entity.SetContents(std::move(contents));
auto actual = entity.GetCoverage();
auto expected = Rect::MakeLTRB(-2, -2, 12, 12);
@@ -827,10 +824,10 @@ TEST_P(EntityTest, SolidStrokeCoverageIsCorrect) {
{
Entity entity;
auto contents = std::make_unique<SolidStrokeContents>();
contents->SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath());
contents->SetStrokeCap(SolidStrokeContents::Cap::kSquare);
contents->SetStrokeJoin(SolidStrokeContents::Join::kBevel);
contents->SetStrokeSize(4);
entity.SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath());
entity.SetContents(std::move(contents));
auto actual = entity.GetCoverage();
auto expected =
@@ -843,11 +840,11 @@ TEST_P(EntityTest, SolidStrokeCoverageIsCorrect) {
{
Entity entity;
auto contents = std::make_unique<SolidStrokeContents>();
contents->SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath());
contents->SetStrokeCap(SolidStrokeContents::Cap::kSquare);
contents->SetStrokeJoin(SolidStrokeContents::Join::kMiter);
contents->SetStrokeSize(4);
contents->SetStrokeMiter(2);
entity.SetPath(PathBuilder{}.AddLine({0, 0}, {10, 10}).TakePath());
entity.SetContents(std::move(contents));
auto actual = entity.GetCoverage();
auto expected = Rect::MakeLTRB(-4, -4, 14, 14);

View File

@@ -286,6 +286,14 @@ std::optional<Rect> Path::GetBoundingBox() const {
return Rect{min.x, min.y, difference.x, difference.y};
}
std::optional<Rect> Path::GetTransformedBoundingBox(const Matrix& transform) const {
auto bounds = GetBoundingBox();
if (!bounds.has_value()) {
return std::nullopt;
}
return bounds->TransformBounds(transform);
}
std::optional<std::pair<Point, Point>> Path::GetMinMaxCoveragePoints() const {
if (linears_.empty() && quads_.empty() && cubics_.empty()) {
return std::nullopt;

View File

@@ -121,6 +121,8 @@ class Path {
std::optional<Rect> GetBoundingBox() const;
std::optional<Rect> GetTransformedBoundingBox(const Matrix& transform) const;
std::optional<std::pair<Point, Point>> GetMinMaxCoveragePoints() const;
private: