[Impeller] make sure buffers are 4 aligned for foreground color blending (flutter/engine#53077)

fixes https://github.com/flutter/flutter/issues/149216

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
gaaclarke
2024-05-30 14:02:53 -07:00
committed by GitHub
parent 2d3cfa1e4c
commit 419275bce0
5 changed files with 77 additions and 5 deletions

View File

@@ -145,6 +145,7 @@
../../../flutter/impeller/display_list/skia_conversions_unittests.cc
../../../flutter/impeller/docs
../../../flutter/impeller/entity/contents/clip_contents_unittests.cc
../../../flutter/impeller/entity/contents/filters/blend_filter_contents_unittests.cc
../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc
../../../flutter/impeller/entity/contents/filters/inputs/filter_input_unittests.cc
../../../flutter/impeller/entity/contents/filters/matrix_filter_contents_unittests.cc

View File

@@ -84,6 +84,7 @@ class HostBuffer {
/// host buffer.
///
/// @param[in] buffer The buffer data.
/// @param[in] alignment Minimum alignment of the data being emplaced.
///
/// @tparam BufferType The type of the buffer data.
///
@@ -91,10 +92,11 @@ class HostBuffer {
///
template <class BufferType,
class = std::enable_if_t<std::is_standard_layout_v<BufferType>>>
[[nodiscard]] BufferView Emplace(const BufferType& buffer) {
return Emplace(reinterpret_cast<const void*>(&buffer), // buffer
sizeof(BufferType), // size
alignof(BufferType) // alignment
[[nodiscard]] BufferView Emplace(const BufferType& buffer,
size_t alignment = 0) {
return Emplace(reinterpret_cast<const void*>(&buffer), // buffer
sizeof(BufferType), // size
std::max(alignment, alignof(BufferType)) // alignment
);
}

View File

@@ -239,6 +239,7 @@ impeller_component("entity_unittests") {
sources = [
"contents/clip_contents_unittests.cc",
"contents/filters/blend_filter_contents_unittests.cc",
"contents/filters/gaussian_blur_filter_contents_unittests.cc",
"contents/filters/inputs/filter_input_unittests.cc",
"contents/filters/matrix_filter_contents_unittests.cc",

View File

@@ -867,7 +867,7 @@ std::optional<Entity> BlendFilterContents::CreateFramebufferAdvancedBlend(
}
auto blit_pass = cmd_buffer->CreateBlitPass();
auto buffer_view = renderer.GetTransientsBuffer().Emplace(
foreground_color->Premultiply().ToR8G8B8A8());
foreground_color->Premultiply().ToR8G8B8A8(), /*alignment=*/4);
blit_pass->AddCopy(std::move(buffer_view), foreground_texture);
if (!blit_pass->EncodeCommands(

View File

@@ -0,0 +1,68 @@
// 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 "flutter/testing/testing.h"
#include "gmock/gmock.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/filters/blend_filter_contents.h"
#include "impeller/entity/entity_playground.h"
namespace impeller {
namespace testing {
class BlendFilterContentsTest : public EntityPlayground {
public:
/// Create a texture that has been cleared to transparent black.
std::shared_ptr<Texture> MakeTexture(ISize size) {
std::shared_ptr<CommandBuffer> command_buffer =
GetContentContext()->GetContext()->CreateCommandBuffer();
if (!command_buffer) {
return nullptr;
}
auto render_target = GetContentContext()->MakeSubpass(
"Clear Subpass", size, command_buffer,
[](const ContentContext&, RenderPass&) { return true; });
if (!GetContentContext()
->GetContext()
->GetCommandQueue()
->Submit(/*buffers=*/{command_buffer})
.ok()) {
return nullptr;
}
if (render_target.ok()) {
return render_target.value().GetRenderTargetTexture();
}
return nullptr;
}
};
INSTANTIATE_PLAYGROUND_SUITE(BlendFilterContentsTest);
// https://github.com/flutter/flutter/issues/149216
TEST_P(BlendFilterContentsTest, AdvancedBlendColorAlignsColorTo4) {
std::shared_ptr<Texture> texture = MakeTexture(ISize(100, 100));
BlendFilterContents filter_contents;
filter_contents.SetInputs({FilterInput::Make(texture)});
filter_contents.SetForegroundColor(Color(1.0, 0.0, 0.0, 1.0));
filter_contents.SetBlendMode(BlendMode::kColorDodge);
std::shared_ptr<ContentContext> renderer = GetContentContext();
// Add random byte to get the HostBuffer in a bad alignment.
uint8_t byte = 0xff;
BufferView buffer_view =
renderer->GetTransientsBuffer().Emplace(&byte, /*length=*/1, /*align=*/1);
EXPECT_EQ(buffer_view.range.offset, 4u);
EXPECT_EQ(buffer_view.range.length, 1u);
Entity entity;
std::optional<Entity> result = filter_contents.GetEntity(
*renderer, entity, /*coverage_hint=*/std::nullopt);
EXPECT_TRUE(result.has_value());
}
} // namespace testing
} // namespace impeller