[Impeller] Dont blow away coverage hint on advanced blends. (flutter/engine#46219)

Advnaced blends are given a fairly accurate coverage hint based on the entity size. We were blowing this value away when applying the stencil coverage, resulting in oversized offscreen textures. This should improve the android advanced blend benchmark
This commit is contained in:
Jonah Williams
2023-09-25 16:08:56 -07:00
committed by GitHub
parent 11ab13e1a5
commit 0872e4b5f1
4 changed files with 104 additions and 5 deletions

View File

@@ -162,7 +162,8 @@ static std::unique_ptr<PipelineT> CreateDefaultPipeline(
ContentContext::ContentContext(
std::shared_ptr<Context> context,
std::shared_ptr<TypographerContext> typographer_context)
std::shared_ptr<TypographerContext> typographer_context,
std::shared_ptr<RenderTargetAllocator> render_target_allocator)
: context_(std::move(context)),
lazy_glyph_atlas_(
std::make_shared<LazyGlyphAtlas>(std::move(typographer_context))),
@@ -170,8 +171,10 @@ ContentContext::ContentContext(
#if IMPELLER_ENABLE_3D
scene_context_(std::make_shared<scene::SceneContext>(context_)),
#endif // IMPELLER_ENABLE_3D
render_target_cache_(std::make_shared<RenderTargetCache>(
context_->GetResourceAllocator())) {
render_target_cache_(render_target_allocator == nullptr
? std::make_shared<RenderTargetCache>(
context_->GetResourceAllocator())
: std::move(render_target_allocator)) {
if (!context_ || !context_->IsValid()) {
return;
}

View File

@@ -343,7 +343,8 @@ class ContentContext {
public:
explicit ContentContext(
std::shared_ptr<Context> context,
std::shared_ptr<TypographerContext> typographer_context);
std::shared_ptr<TypographerContext> typographer_context,
std::shared_ptr<RenderTargetAllocator> render_target_allocator = nullptr);
~ContentContext();

View File

@@ -783,7 +783,16 @@ bool EntityPass::OnRender(
// rendered output will actually be used, and so we set this to the current
// stencil coverage (which is the max clip bounds). The contents may
// optionally use this hint to avoid unnecessary rendering work.
element_entity.GetContents()->SetCoverageHint(current_stencil_coverage);
if (element_entity.GetContents()->GetCoverageHint().has_value()) {
// If the element already has a coverage hint (because its an advanced
// blend), then we need to intersect the stencil coverage hint with the
// existing coverage hint.
element_entity.GetContents()->SetCoverageHint(
current_stencil_coverage->Intersection(
element_entity.GetContents()->GetCoverageHint().value()));
} else {
element_entity.GetContents()->SetCoverageHint(current_stencil_coverage);
}
switch (stencil_coverage.type) {
case Contents::StencilCoverage::Type::kNoChange:

View File

@@ -7,12 +7,14 @@
#include <memory>
#include <optional>
#include <unordered_map>
#include <utility>
#include <vector>
#include "flutter/testing/testing.h"
#include "fml/logging.h"
#include "fml/time/time_point.h"
#include "gtest/gtest.h"
#include "impeller/core/texture_descriptor.h"
#include "impeller/entity/contents/atlas_contents.h"
#include "impeller/entity/contents/clip_contents.h"
#include "impeller/entity/contents/conical_gradient_contents.h"
@@ -42,6 +44,7 @@
#include "impeller/geometry/geometry_asserts.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/geometry/sigma.h"
#include "impeller/geometry/vector.h"
#include "impeller/playground/playground.h"
#include "impeller/playground/widgets.h"
#include "impeller/renderer/command.h"
@@ -2441,6 +2444,89 @@ TEST_P(EntityTest, TextContentsCeilsGlyphScaleToDecimal) {
ASSERT_EQ(TextFrame::RoundScaledFontSize(0.0f, 12), 0.0f);
}
class TestRenderTargetAllocator : public RenderTargetAllocator {
public:
explicit TestRenderTargetAllocator(std::shared_ptr<Allocator> allocator)
: RenderTargetAllocator(std::move(allocator)) {}
~TestRenderTargetAllocator() = default;
std::shared_ptr<Texture> CreateTexture(
const TextureDescriptor& desc) override {
allocated_.push_back(desc);
return RenderTargetAllocator::CreateTexture(desc);
}
void Start() override { RenderTargetAllocator::Start(); }
void End() override { RenderTargetAllocator::End(); }
std::vector<TextureDescriptor> GetDescriptors() const { return allocated_; }
private:
std::vector<TextureDescriptor> allocated_;
};
TEST_P(EntityTest, AdvancedBlendCoverageHintIsNotResetByEntityPass) {
if (GetContext()->GetCapabilities()->SupportsFramebufferFetch()) {
GTEST_SKIP() << "Backends that support framebuffer fetch dont use coverage "
"for advanced blends.";
}
auto contents = std::make_shared<SolidColorContents>();
contents->SetGeometry(Geometry::MakeRect({100, 100, 100, 100}));
contents->SetColor(Color::Red());
Entity entity;
entity.SetTransformation(Matrix::MakeScale(Vector3(2, 2, 1)));
entity.SetBlendMode(BlendMode::kColorBurn);
entity.SetContents(contents);
auto coverage = entity.GetCoverage();
EXPECT_TRUE(coverage.has_value());
auto pass = std::make_unique<EntityPass>();
auto test_allocator = std::make_shared<TestRenderTargetAllocator>(
GetContext()->GetResourceAllocator());
auto stencil_config = RenderTarget::AttachmentConfig{
.storage_mode = StorageMode::kDevicePrivate,
.load_action = LoadAction::kClear,
.store_action = StoreAction::kDontCare,
.clear_color = Color::BlackTransparent()};
auto rt = RenderTarget::CreateOffscreen(
*GetContext(), *test_allocator, ISize::MakeWH(1000, 1000), "Offscreen",
RenderTarget::kDefaultColorAttachmentConfig, stencil_config);
auto content_context = ContentContext(
GetContext(), TypographerContextSkia::Make(), test_allocator);
pass->AddEntity(entity);
EXPECT_TRUE(pass->Render(content_context, rt));
if (test_allocator->GetDescriptors().size() == 6u) {
EXPECT_EQ(test_allocator->GetDescriptors()[0].size, ISize(1000, 1000));
EXPECT_EQ(test_allocator->GetDescriptors()[1].size, ISize(1000, 1000));
EXPECT_EQ(test_allocator->GetDescriptors()[2].size, ISize(200, 200));
EXPECT_EQ(test_allocator->GetDescriptors()[3].size, ISize(200, 200));
EXPECT_EQ(test_allocator->GetDescriptors()[4].size, ISize(200, 200));
EXPECT_EQ(test_allocator->GetDescriptors()[5].size, ISize(200, 200));
} else if (test_allocator->GetDescriptors().size() == 9u) {
// Onscreen render target.
EXPECT_EQ(test_allocator->GetDescriptors()[0].size, ISize(1000, 1000));
EXPECT_EQ(test_allocator->GetDescriptors()[1].size, ISize(1000, 1000));
EXPECT_EQ(test_allocator->GetDescriptors()[2].size, ISize(1000, 1000));
EXPECT_EQ(test_allocator->GetDescriptors()[3].size, ISize(1000, 1000));
EXPECT_EQ(test_allocator->GetDescriptors()[4].size, ISize(1000, 1000));
EXPECT_EQ(test_allocator->GetDescriptors()[5].size, ISize(200, 200));
EXPECT_EQ(test_allocator->GetDescriptors()[5].size, ISize(200, 200));
EXPECT_EQ(test_allocator->GetDescriptors()[6].size, ISize(200, 200));
EXPECT_EQ(test_allocator->GetDescriptors()[7].size, ISize(200, 200));
} else {
EXPECT_TRUE(false);
}
}
} // namespace testing
} // namespace impeller