[Impeller] Limit coverage of all subpasses to the root texture (flutter/engine#34246)
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "impeller/aiks/picture.h"
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "impeller/base/validation.h"
|
||||
#include "impeller/entity/entity.h"
|
||||
@@ -14,7 +15,7 @@
|
||||
namespace impeller {
|
||||
|
||||
std::optional<Snapshot> Picture::Snapshot(AiksContext& context) {
|
||||
auto coverage = pass->GetElementsCoverage();
|
||||
auto coverage = pass->GetElementsCoverage(std::nullopt);
|
||||
if (!coverage.has_value() || coverage->IsEmpty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
@@ -64,16 +64,21 @@ const std::shared_ptr<LazyGlyphAtlas>& EntityPass::GetLazyGlyphAtlas() const {
|
||||
return lazy_glyph_atlas_;
|
||||
}
|
||||
|
||||
std::optional<Rect> EntityPass::GetElementsCoverage() const {
|
||||
std::optional<Rect> EntityPass::GetElementsCoverage(
|
||||
std::optional<Rect> coverage_clip) const {
|
||||
std::optional<Rect> result;
|
||||
for (const auto& element : elements_) {
|
||||
std::optional<Rect> coverage;
|
||||
|
||||
if (auto entity = std::get_if<Entity>(&element)) {
|
||||
coverage = entity->GetCoverage();
|
||||
|
||||
if (coverage.has_value() && coverage_clip.has_value()) {
|
||||
coverage = coverage->Intersection(coverage_clip.value());
|
||||
}
|
||||
} else if (auto subpass =
|
||||
std::get_if<std::unique_ptr<EntityPass>>(&element)) {
|
||||
coverage = GetSubpassCoverage(*subpass->get());
|
||||
coverage = GetSubpassCoverage(*subpass->get(), coverage_clip);
|
||||
} else {
|
||||
FML_UNREACHABLE();
|
||||
}
|
||||
@@ -91,8 +96,9 @@ std::optional<Rect> EntityPass::GetElementsCoverage() const {
|
||||
}
|
||||
|
||||
std::optional<Rect> EntityPass::GetSubpassCoverage(
|
||||
const EntityPass& subpass) const {
|
||||
auto entities_coverage = subpass.GetElementsCoverage();
|
||||
const EntityPass& subpass,
|
||||
std::optional<Rect> coverage_clip) const {
|
||||
auto entities_coverage = subpass.GetElementsCoverage(coverage_clip);
|
||||
// The entities don't cover anything. There is nothing to do.
|
||||
if (!entities_coverage.has_value()) {
|
||||
return std::nullopt;
|
||||
@@ -143,7 +149,8 @@ bool EntityPass::Render(ContentContext& renderer,
|
||||
"EntityPass", //
|
||||
StorageMode::kDevicePrivate, LoadAction::kClear, StoreAction::kStore,
|
||||
StorageMode::kDevicePrivate, LoadAction::kClear, StoreAction::kStore);
|
||||
if (!OnRender(renderer, offscreen_target, Point(), Point(), 0)) {
|
||||
if (!OnRender(renderer, offscreen_target.GetRenderTargetSize(),
|
||||
offscreen_target, Point(), Point(), 0)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -178,13 +185,15 @@ bool EntityPass::Render(ContentContext& renderer,
|
||||
return true;
|
||||
}
|
||||
|
||||
return OnRender(renderer, render_target, Point(), Point(), 0);
|
||||
return OnRender(renderer, render_target.GetRenderTargetSize(), render_target,
|
||||
Point(), Point(), 0);
|
||||
}
|
||||
|
||||
EntityPass::EntityResult EntityPass::GetEntityForElement(
|
||||
const EntityPass::Element& element,
|
||||
ContentContext& renderer,
|
||||
InlinePassContext& pass_context,
|
||||
ISize root_pass_size,
|
||||
Point position,
|
||||
uint32_t pass_depth,
|
||||
size_t stencil_depth_floor) const {
|
||||
@@ -221,8 +230,9 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
|
||||
if (subpass->delegate_->CanCollapseIntoParentPass() &&
|
||||
!subpass->backdrop_filter_proc_.has_value()) {
|
||||
// Directly render into the parent target and move on.
|
||||
if (!subpass->OnRender(renderer, pass_context.GetRenderTarget(), position,
|
||||
position, stencil_depth_floor)) {
|
||||
if (!subpass->OnRender(renderer, root_pass_size,
|
||||
pass_context.GetRenderTarget(), position, position,
|
||||
stencil_depth_floor)) {
|
||||
return EntityPass::EntityResult::Failure();
|
||||
}
|
||||
return EntityPass::EntityResult::Skip();
|
||||
@@ -241,7 +251,8 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
|
||||
pass_context.EndPass();
|
||||
}
|
||||
|
||||
auto subpass_coverage = GetSubpassCoverage(*subpass);
|
||||
auto subpass_coverage =
|
||||
GetSubpassCoverage(*subpass, Rect::MakeSize(Size(root_pass_size)));
|
||||
|
||||
if (backdrop_contents) {
|
||||
auto backdrop_coverage = backdrop_contents->GetCoverage(Entity{});
|
||||
@@ -256,6 +267,11 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
|
||||
}
|
||||
}
|
||||
|
||||
if (subpass_coverage.has_value()) {
|
||||
subpass_coverage =
|
||||
subpass_coverage->Intersection(Rect::MakeSize(Size(root_pass_size)));
|
||||
}
|
||||
|
||||
if (!subpass_coverage.has_value()) {
|
||||
return EntityPass::EntityResult::Skip();
|
||||
}
|
||||
@@ -304,9 +320,9 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
|
||||
|
||||
// Stencil textures aren't shared between EntityPasses (as much of the
|
||||
// time they are transient).
|
||||
if (!subpass->OnRender(renderer, subpass_target, subpass_coverage->origin,
|
||||
position, ++pass_depth, subpass->stencil_depth_,
|
||||
backdrop_contents)) {
|
||||
if (!subpass->OnRender(renderer, root_pass_size, subpass_target,
|
||||
subpass_coverage->origin, position, ++pass_depth,
|
||||
subpass->stencil_depth_, backdrop_contents)) {
|
||||
return EntityPass::EntityResult::Failure();
|
||||
}
|
||||
|
||||
@@ -327,6 +343,7 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
|
||||
}
|
||||
|
||||
bool EntityPass::OnRender(ContentContext& renderer,
|
||||
ISize root_pass_size,
|
||||
RenderTarget render_target,
|
||||
Point position,
|
||||
Point parent_position,
|
||||
@@ -368,8 +385,8 @@ bool EntityPass::OnRender(ContentContext& renderer,
|
||||
|
||||
for (const auto& element : elements_) {
|
||||
EntityResult result =
|
||||
GetEntityForElement(element, renderer, pass_context, position,
|
||||
pass_depth, stencil_depth_floor);
|
||||
GetEntityForElement(element, renderer, pass_context, root_pass_size,
|
||||
position, pass_depth, stencil_depth_floor);
|
||||
|
||||
switch (result.status) {
|
||||
case EntityResult::kSuccess:
|
||||
|
||||
@@ -60,9 +60,12 @@ class EntityPass {
|
||||
|
||||
void SetBackdropFilter(std::optional<BackdropFilterProc> proc);
|
||||
|
||||
std::optional<Rect> GetSubpassCoverage(const EntityPass& subpass) const;
|
||||
std::optional<Rect> GetSubpassCoverage(
|
||||
const EntityPass& subpass,
|
||||
std::optional<Rect> coverage_clip) const;
|
||||
|
||||
std::optional<Rect> GetElementsCoverage() const;
|
||||
std::optional<Rect> GetElementsCoverage(
|
||||
std::optional<Rect> coverage_clip) const;
|
||||
|
||||
private:
|
||||
struct EntityResult {
|
||||
@@ -91,11 +94,13 @@ class EntityPass {
|
||||
EntityResult GetEntityForElement(const EntityPass::Element& element,
|
||||
ContentContext& renderer,
|
||||
InlinePassContext& pass_context,
|
||||
ISize root_pass_size,
|
||||
Point position,
|
||||
uint32_t pass_depth,
|
||||
size_t stencil_depth_floor) const;
|
||||
|
||||
bool OnRender(ContentContext& renderer,
|
||||
ISize root_pass_size,
|
||||
RenderTarget render_target,
|
||||
Point position,
|
||||
Point parent_position,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "flutter/testing/testing.h"
|
||||
#include "impeller/entity/contents/filters/blend_filter_contents.h"
|
||||
@@ -63,36 +64,31 @@ class TestPassDelegate final : public EntityPassDelegate {
|
||||
const std::optional<Rect> coverage_;
|
||||
};
|
||||
|
||||
TEST_P(EntityTest, EntityPassSubpassCoverageIsCorrect) {
|
||||
auto CreatePassWithRectPath(Rect rect, std::optional<Rect> bounds_hint) {
|
||||
auto subpass = std::make_unique<EntityPass>();
|
||||
Entity entity;
|
||||
entity.SetContents(SolidColorContents::Make(
|
||||
PathBuilder{}.AddRect(rect).TakePath(), Color::Red()));
|
||||
subpass->AddEntity(entity);
|
||||
subpass->SetDelegate(std::make_unique<TestPassDelegate>(bounds_hint));
|
||||
return subpass;
|
||||
}
|
||||
|
||||
TEST_P(EntityTest, EntityPassCoverageRespectsDelegateBoundsHint) {
|
||||
EntityPass pass;
|
||||
|
||||
auto subpass0 = std::make_unique<EntityPass>();
|
||||
{
|
||||
Entity entity;
|
||||
entity.SetContents(SolidColorContents::Make(
|
||||
PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 100, 100)).TakePath(),
|
||||
Color::Red()));
|
||||
subpass0->AddEntity(entity);
|
||||
subpass0->SetDelegate(
|
||||
std::make_unique<TestPassDelegate>(Rect::MakeLTRB(50, 50, 150, 150)));
|
||||
}
|
||||
auto subpass0 = CreatePassWithRectPath(Rect::MakeLTRB(0, 0, 100, 100),
|
||||
Rect::MakeLTRB(50, 50, 150, 150));
|
||||
auto subpass1 = CreatePassWithRectPath(Rect::MakeLTRB(500, 500, 1000, 1000),
|
||||
Rect::MakeLTRB(800, 800, 900, 900));
|
||||
|
||||
auto subpass1 = std::make_unique<EntityPass>();
|
||||
{
|
||||
Entity entity;
|
||||
entity.SetContents(SolidColorContents::Make(
|
||||
PathBuilder{}.AddRect(Rect::MakeLTRB(500, 500, 1000, 1000)).TakePath(),
|
||||
Color::Red()));
|
||||
subpass1->AddEntity(entity);
|
||||
subpass1->SetDelegate(
|
||||
std::make_unique<TestPassDelegate>(Rect::MakeLTRB(800, 800, 900, 900)));
|
||||
}
|
||||
|
||||
auto subpass0_coverage = pass.GetSubpassCoverage(*subpass0.get());
|
||||
auto subpass0_coverage =
|
||||
pass.GetSubpassCoverage(*subpass0.get(), std::nullopt);
|
||||
ASSERT_TRUE(subpass0_coverage.has_value());
|
||||
ASSERT_RECT_NEAR(subpass0_coverage.value(), Rect::MakeLTRB(50, 50, 100, 100));
|
||||
|
||||
auto subpass1_coverage = pass.GetSubpassCoverage(*subpass1.get());
|
||||
auto subpass1_coverage =
|
||||
pass.GetSubpassCoverage(*subpass1.get(), std::nullopt);
|
||||
ASSERT_TRUE(subpass1_coverage.has_value());
|
||||
ASSERT_RECT_NEAR(subpass1_coverage.value(),
|
||||
Rect::MakeLTRB(800, 800, 900, 900));
|
||||
@@ -100,11 +96,41 @@ TEST_P(EntityTest, EntityPassSubpassCoverageIsCorrect) {
|
||||
pass.AddSubpass(std::move(subpass0));
|
||||
pass.AddSubpass(std::move(subpass1));
|
||||
|
||||
auto coverage = pass.GetElementsCoverage();
|
||||
auto coverage = pass.GetElementsCoverage(std::nullopt);
|
||||
ASSERT_TRUE(coverage.has_value());
|
||||
ASSERT_RECT_NEAR(coverage.value(), Rect::MakeLTRB(50, 50, 900, 900));
|
||||
}
|
||||
|
||||
TEST_P(EntityTest, EntityPassCoverageRespectsCoverageLimit) {
|
||||
// Rect is drawn entirely in negative area.
|
||||
auto pass = CreatePassWithRectPath(Rect::MakeLTRB(-200, -200, -100, -100),
|
||||
std::nullopt);
|
||||
|
||||
// Without coverage limit.
|
||||
{
|
||||
auto pass_coverage = pass->GetElementsCoverage(std::nullopt);
|
||||
ASSERT_TRUE(pass_coverage.has_value());
|
||||
ASSERT_RECT_NEAR(pass_coverage.value(),
|
||||
Rect::MakeLTRB(-200, -200, -100, -100));
|
||||
}
|
||||
|
||||
// With limit that doesn't overlap.
|
||||
{
|
||||
auto pass_coverage =
|
||||
pass->GetElementsCoverage(Rect::MakeLTRB(0, 0, 100, 100));
|
||||
ASSERT_FALSE(pass_coverage.has_value());
|
||||
}
|
||||
|
||||
// With limit that partially overlaps.
|
||||
{
|
||||
auto pass_coverage =
|
||||
pass->GetElementsCoverage(Rect::MakeLTRB(-150, -150, 0, 0));
|
||||
ASSERT_TRUE(pass_coverage.has_value());
|
||||
ASSERT_RECT_NEAR(pass_coverage.value(),
|
||||
Rect::MakeLTRB(-150, -150, -100, -100));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(EntityTest, CanDrawRect) {
|
||||
Entity entity;
|
||||
entity.SetTransformation(Matrix::MakeScale(GetContentScale()));
|
||||
|
||||
Reference in New Issue
Block a user