Skip to content

Commit b3ec9da

Browse files
committed
[Impeller] Pixel align blend subpass textures
Advanced and pipeline blends that take the offscreen path derive three things from the blend coverage, and they do not agree: * the texture coordinates, from the raw float coverage; * the subpass render target extent, from `ISize(coverage.GetSize())`, whose converting constructor truncates rather than rounds; * the composite translation, from the fractional coverage origin, drawn with the default nearest sampler. An integer quad therefore displays a float sized region, scaling the contents by trunc(w)/w. The factor changes whenever the coverage crosses a pixel boundary, so animated content is resampled differently every frame. Snap the coverage to the device pixel grid once with `Rect::RoundOut` and derive all three from that single integral rect. `Canvas::SaveLayer` already does this for save layer subpasses, for the same nearest sampling reason; the blend filter path never got the same treatment. Only configurations without framebuffer fetch take this path: the iOS simulator, PowerVR, and Adreno on GLES. Devices with framebuffer fetch blend in pass via `ApplyFramebufferBlend`, which is exact. Adds a regression test per offscreen path, asserting the composited entity lands on the pixel grid when the input coverage is fractional. Both fail without this change. Fixes flutter#192980
1 parent c3edad8 commit b3ec9da

2 files changed

Lines changed: 112 additions & 8 deletions

File tree

‎engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,17 @@ static std::optional<Entity> AdvancedBlend(
116116
return std::nullopt;
117117
}
118118

119+
// Snap to the pixel grid so the texture coordinates, the subpass extent and
120+
// the composite translation all describe the same rect. Canvas::SaveLayer
121+
// aligns subpass textures for the same reason.
122+
const Rect snapped_coverage = Rect::RoundOut(coverage);
123+
119124
auto dst_snapshot =
120125
inputs[0]->GetSnapshot("AdvancedBlend(Dst)", renderer, entity);
121126
if (!dst_snapshot.has_value()) {
122127
return std::nullopt;
123128
}
124-
auto maybe_dst_uvs = dst_snapshot->GetCoverageUVs(coverage);
129+
auto maybe_dst_uvs = dst_snapshot->GetCoverageUVs(snapped_coverage);
125130
if (!maybe_dst_uvs.has_value()) {
126131
return std::nullopt;
127132
}
@@ -138,7 +143,7 @@ static std::optional<Entity> AdvancedBlend(
138143
}
139144
return Entity::FromSnapshot(dst_snapshot.value(), entity.GetBlendMode());
140145
}
141-
auto maybe_src_uvs = src_snapshot->GetCoverageUVs(coverage);
146+
auto maybe_src_uvs = src_snapshot->GetCoverageUVs(snapped_coverage);
142147
if (!maybe_src_uvs.has_value()) {
143148
if (!dst_snapshot.has_value()) {
144149
return std::nullopt;
@@ -148,13 +153,13 @@ static std::optional<Entity> AdvancedBlend(
148153
src_uvs = maybe_src_uvs.value();
149154
}
150155

151-
Rect subpass_coverage = coverage;
156+
Rect subpass_coverage = snapped_coverage;
152157
if (entity.GetContents()) {
153158
auto coverage_hint = entity.GetContents()->GetCoverageHint();
154159

155160
if (coverage_hint.has_value()) {
156161
auto maybe_subpass_coverage =
157-
subpass_coverage.Intersection(*coverage_hint);
162+
subpass_coverage.Intersection(Rect::RoundOut(*coverage_hint));
158163
if (!maybe_subpass_coverage.has_value()) {
159164
return std::nullopt; // Nothing to render.
160165
}
@@ -171,7 +176,8 @@ static std::optional<Entity> AdvancedBlend(
171176
RenderPass& pass) {
172177
auto& data_host_buffer = renderer.GetTransientsDataBuffer();
173178

174-
auto size = pass.GetRenderTargetSize();
179+
// The quad spans the rect the texture coordinates were derived from.
180+
Size size = snapped_coverage.GetSize();
175181

176182
std::array<typename VS::PerVertexData, 4> vertices = {
177183
typename VS::PerVertexData{Point(0, 0), dst_uvs[0], src_uvs[0]},
@@ -227,7 +233,7 @@ static std::optional<Entity> AdvancedBlend(
227233
FS::BindBlendInfo(pass, blend_uniform);
228234

229235
frame_info.mvp = pass.GetOrthographicTransform() *
230-
Matrix::MakeTranslation(coverage.GetOrigin() -
236+
Matrix::MakeTranslation(snapped_coverage.GetOrigin() -
231237
subpass_coverage.GetOrigin());
232238

233239
auto uniform_view = data_host_buffer.EmplaceUniform(frame_info);
@@ -515,13 +521,14 @@ static std::optional<Entity> PipelineBlend(
515521
return std::nullopt; // Nothing to render.
516522
}
517523

518-
Rect subpass_coverage = coverage;
524+
// Snap to the pixel grid, as in AdvancedBlend above.
525+
Rect subpass_coverage = Rect::RoundOut(coverage);
519526
if (entity.GetContents()) {
520527
auto coverage_hint = entity.GetContents()->GetCoverageHint();
521528

522529
if (coverage_hint.has_value()) {
523530
auto maybe_subpass_coverage =
524-
subpass_coverage.Intersection(*coverage_hint);
531+
subpass_coverage.Intersection(Rect::RoundOut(*coverage_hint));
525532
if (!maybe_subpass_coverage.has_value()) {
526533
return std::nullopt; // Nothing to render.
527534
}

‎engine/src/flutter/impeller/entity/entity_unittests.cc‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#include <algorithm>
6+
#include <cmath>
67
#include <cstring>
78
#include <memory>
89
#include <optional>
@@ -11,6 +12,7 @@
1112

1213
#include "flutter/display_list/geometry/dl_path_builder.h"
1314
#include "flutter/display_list/testing/dl_test_snippets.h"
15+
#include "flutter/fml/closure.h"
1416
#include "fml/logging.h"
1517
#include "gtest/gtest.h"
1618
#include "impeller/core/device_buffer.h"
@@ -3002,6 +3004,101 @@ TEST_P(EntityTest, RoundSuperellipseGetPositionBufferFlushes) {
30023004
EXPECT_TRUE(device_buffer->flush_called());
30033005
}
30043006

3007+
// A fractional blend coverage used to truncate into the subpass extent while
3008+
// the texture coordinates and the composite translation stayed fractional, so
3009+
// the subpass displayed a region wider than itself.
3010+
//
3011+
// Regression test for https://github.com/flutter/flutter/issues/192980.
3012+
TEST_P(EntityTest, PipelineBlendSubpassIsPixelAligned) {
3013+
auto image = CreateTextureForFixture("boston.jpg");
3014+
auto filter = ColorFilterContents::MakeBlend(
3015+
BlendMode::kSrcOver, FilterInput::Make({image, image}));
3016+
3017+
Entity entity;
3018+
entity.SetTransform(Matrix::MakeTranslation({10.4f, 20.6f}) *
3019+
Matrix::MakeScale(Vector2{0.37f, 0.37f}));
3020+
entity.SetContents(filter);
3021+
3022+
std::optional<Rect> coverage = filter->GetCoverage(entity);
3023+
ASSERT_TRUE(coverage.has_value());
3024+
// The test only means something if the coverage is off the pixel grid.
3025+
ASSERT_NE(coverage->GetLeft(), std::floor(coverage->GetLeft()));
3026+
ASSERT_NE(coverage->GetRight(), std::ceil(coverage->GetRight()));
3027+
3028+
std::optional<Entity> result = filter->GetEntity(GetContentContext(), entity,
3029+
/*coverage_hint=*/{});
3030+
ASSERT_TRUE(result.has_value());
3031+
std::optional<Rect> result_coverage = result->GetCoverage();
3032+
ASSERT_TRUE(result_coverage.has_value());
3033+
3034+
// The subpass texture is allocated, and composited back, on the pixel grid.
3035+
EXPECT_RECT_NEAR(result_coverage.value(), Rect::RoundOut(coverage.value()));
3036+
}
3037+
3038+
// The same invariant for the offscreen advanced blend path, which is what
3039+
// devices without framebuffer fetch (the iOS simulator, PowerVR, Adreno on
3040+
// GLES) take for every blend mode above kModulate.
3041+
//
3042+
// Regression test for https://github.com/flutter/flutter/issues/192980.
3043+
TEST_P(EntityTest, AdvancedBlendSubpassIsPixelAligned) {
3044+
if (GetParam() != PlaygroundBackend::kMetal) {
3045+
GTEST_SKIP()
3046+
<< "This backend doesn't yet support setting device capabilities.";
3047+
}
3048+
3049+
std::shared_ptr<const Capabilities> old_capabilities =
3050+
GetContext()->GetCapabilities();
3051+
auto mock_capabilities = std::make_shared<MockCapabilities>();
3052+
EXPECT_CALL(*mock_capabilities, SupportsFramebufferFetch())
3053+
.Times(::testing::AtLeast(1))
3054+
.WillRepeatedly(::testing::Return(false));
3055+
FLT_FORWARD(mock_capabilities, old_capabilities, GetDefaultColorFormat);
3056+
FLT_FORWARD(mock_capabilities, old_capabilities, GetDefaultStencilFormat);
3057+
FLT_FORWARD(mock_capabilities, old_capabilities,
3058+
GetDefaultDepthStencilFormat);
3059+
FLT_FORWARD(mock_capabilities, old_capabilities, SupportsOffscreenMSAA);
3060+
FLT_FORWARD(mock_capabilities, old_capabilities,
3061+
SupportsImplicitResolvingMSAA);
3062+
FLT_FORWARD(mock_capabilities, old_capabilities, SupportsReadFromResolve);
3063+
FLT_FORWARD(mock_capabilities, old_capabilities, SupportsSSBO);
3064+
FLT_FORWARD(mock_capabilities, old_capabilities, SupportsCompute);
3065+
FLT_FORWARD(mock_capabilities, old_capabilities,
3066+
SupportsTextureToTextureBlits);
3067+
FLT_FORWARD(mock_capabilities, old_capabilities, GetDefaultGlyphAtlasFormat);
3068+
FLT_FORWARD(mock_capabilities, old_capabilities, SupportsTriangleFan);
3069+
FLT_FORWARD(mock_capabilities, old_capabilities,
3070+
SupportsDecalSamplerAddressMode);
3071+
FLT_FORWARD(mock_capabilities, old_capabilities, SupportsPrimitiveRestart);
3072+
FLT_FORWARD(mock_capabilities, old_capabilities, GetMinimumUniformAlignment);
3073+
ASSERT_TRUE(SetCapabilities(mock_capabilities).ok());
3074+
// The playground context is shared across tests, so the mocked capabilities
3075+
// have to be undone however this test exits.
3076+
fml::ScopedCleanupClosure restore_capabilities(
3077+
[&]() { SetCapabilities(old_capabilities).IgnoreError(); });
3078+
3079+
auto image = CreateTextureForFixture("boston.jpg");
3080+
auto filter = ColorFilterContents::MakeBlend(
3081+
BlendMode::kScreen, FilterInput::Make({image, image}));
3082+
3083+
Entity entity;
3084+
entity.SetTransform(Matrix::MakeTranslation({10.4f, 20.6f}) *
3085+
Matrix::MakeScale(Vector2{0.37f, 0.37f}));
3086+
entity.SetContents(filter);
3087+
3088+
std::optional<Rect> coverage = filter->GetCoverage(entity);
3089+
ASSERT_TRUE(coverage.has_value());
3090+
ASSERT_NE(coverage->GetLeft(), std::floor(coverage->GetLeft()));
3091+
ASSERT_NE(coverage->GetRight(), std::ceil(coverage->GetRight()));
3092+
3093+
std::optional<Entity> result = filter->GetEntity(GetContentContext(), entity,
3094+
/*coverage_hint=*/{});
3095+
ASSERT_TRUE(result.has_value());
3096+
std::optional<Rect> result_coverage = result->GetCoverage();
3097+
ASSERT_TRUE(result_coverage.has_value());
3098+
3099+
EXPECT_RECT_NEAR(result_coverage.value(), Rect::RoundOut(coverage.value()));
3100+
}
3101+
30053102
} // namespace testing
30063103
} // namespace impeller
30073104

0 commit comments

Comments
 (0)