Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Impeller] fix drawVertices dest fast path to apply alpha. #47695

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ci/licenses_golden/excluded_files
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@
../../../flutter/impeller/docs
../../../flutter/impeller/entity/contents/checkerboard_contents_unittests.cc
../../../flutter/impeller/entity/contents/filters/inputs/filter_input_unittests.cc
../../../flutter/impeller/entity/contents/test
../../../flutter/impeller/entity/contents/vertices_contents_unittests.cc
../../../flutter/impeller/entity/entity_unittests.cc
../../../flutter/impeller/entity/geometry/geometry_unittests.cc
../../../flutter/impeller/entity/render_target_cache_unittests.cc
Expand Down
13 changes: 13 additions & 0 deletions impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,24 @@ impeller_component("entity") {
deps = [ "//flutter/fml" ]
}

impeller_component("entity_test_helpers") {
testonly = true

sources = [
"contents/test/contents_test_helpers.cc",
"contents/test/contents_test_helpers.h",
]

deps = [ ":entity" ]
}

impeller_component("entity_unittests") {
testonly = true

sources = [
"contents/checkerboard_contents_unittests.cc",
"contents/filters/inputs/filter_input_unittests.cc",
"contents/vertices_contents_unittests.cc",
"entity_playground.cc",
"entity_playground.h",
"entity_unittests.cc",
Expand All @@ -278,6 +290,7 @@ impeller_component("entity_unittests") {

deps = [
":entity",
":entity_test_helpers",
"../geometry:geometry_asserts",
"../playground:playground_test",
"//flutter/impeller/typographer/backends/skia:typographer_skia_backend",
Expand Down
11 changes: 11 additions & 0 deletions impeller/entity/contents/test/contents_test_helpers.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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/test/contents_test_helpers.h"

namespace impeller {

//

}
37 changes: 37 additions & 0 deletions impeller/entity/contents/test/contents_test_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 "impeller/renderer/command.h"

namespace impeller {

/// @brief Retrieve the [VertInfo] struct data from the provided [command].
template <typename T>
typename T::VertInfo* GetVertInfo(const Command& command) {
auto resource = command.vertex_bindings.buffers.find(0u);
if (resource == command.vertex_bindings.buffers.end()) {
return nullptr;
}

auto data = (resource->second.view.resource.contents +
resource->second.view.resource.range.offset);
return reinterpret_cast<typename T::VertInfo*>(data);
}

/// @brief Retrieve the [FragInfo] struct data from the provided [command].
template <typename T>
typename T::FragInfo* GetFragInfo(const Command& command) {
auto resource = command.fragment_bindings.buffers.find(0u);
if (resource == command.fragment_bindings.buffers.end()) {
return nullptr;
}

auto data = (resource->second.view.resource.contents +
resource->second.view.resource.range.offset);
return reinterpret_cast<typename T::FragInfo*>(data);
}

} // namespace impeller
5 changes: 1 addition & 4 deletions impeller/entity/contents/vertices_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@

#include "vertices_contents.h"

#include "impeller/core/formats.h"
#include "impeller/core/vertex_buffer.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/filters/color_filter_contents.h"
#include "impeller/entity/contents/filters/filter_contents.h"
#include "impeller/entity/contents/texture_contents.h"
#include "impeller/entity/position_color.vert.h"
#include "impeller/entity/vertices.frag.h"
#include "impeller/geometry/color.h"
Expand Down Expand Up @@ -73,6 +69,7 @@ bool VerticesContents::Render(const ContentContext& renderer,

std::shared_ptr<Contents> contents;
if (blend_mode_ == BlendMode::kDestination) {
dst_contents->SetAlpha(alpha_);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the bug

contents = dst_contents;
} else {
auto color_filter_contents = ColorFilterContents::MakeBlend(
Expand Down
78 changes: 78 additions & 0 deletions impeller/entity/contents/vertices_contents_unittests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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 <memory>
#include <optional>

#include "gtest/gtest.h"

#include "impeller/entity/contents/contents.h"
#include "impeller/entity/contents/solid_color_contents.h"
#include "impeller/entity/contents/test/contents_test_helpers.h"
#include "impeller/entity/contents/vertices_contents.h"
#include "impeller/entity/entity.h"
#include "impeller/entity/entity_playground.h"
#include "impeller/entity/vertices.frag.h"
#include "impeller/geometry/path_builder.h"
#include "impeller/renderer/render_target.h"

namespace impeller {
namespace testing {

using EntityTest = EntityPlayground;
INSTANTIATE_PLAYGROUND_SUITE(EntityTest);

std::shared_ptr<VerticesGeometry> CreateColorVertices(
const std::vector<Point>& vertices,
const std::vector<Color>& colors) {
auto bounds = Rect::MakePointBounds(vertices.begin(), vertices.end());
std::vector<uint16_t> indices = {};
for (auto i = 0u; i < vertices.size(); i++) {
indices.emplace_back(i);
}
std::vector<Point> texture_coordinates = {};

return std::make_shared<VerticesGeometry>(
vertices, indices, texture_coordinates, colors,
bounds.value_or(Rect::MakeLTRB(0, 0, 0, 0)),
VerticesGeometry::VertexMode::kTriangles);
}

// Verifies that the destination blend fast path still sets an alpha value.
TEST_P(EntityTest, RendersDstPerColorWithAlpha) {
using FS = GeometryColorPipeline::FragmentShader;

auto contents = std::make_shared<VerticesContents>();
auto vertices = CreateColorVertices(
{{0, 0}, {100, 0}, {0, 100}, {100, 0}, {0, 100}, {100, 100}},
{Color::Red(), Color::Red(), Color::Red(), Color::Red(), Color::Red(),
Color::Red()});
auto src_contents = SolidColorContents::Make(
PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 100, 100)).TakePath(),
Color::Red());

contents->SetGeometry(vertices);
contents->SetAlpha(0.5);
contents->SetBlendMode(BlendMode::kDestination);
contents->SetSourceContents(std::move(src_contents));

auto content_context = GetContentContext();
auto buffer = content_context->GetContext()->CreateCommandBuffer();
auto render_target = RenderTarget::CreateOffscreenMSAA(
*content_context->GetContext(),
*GetContentContext()->GetRenderTargetCache(), {100, 100});
auto render_pass = buffer->CreateRenderPass(render_target);
Entity entity;

ASSERT_TRUE(render_pass->GetCommands().empty());
ASSERT_TRUE(contents->Render(*content_context, entity, *render_pass));

const auto& cmd = render_pass->GetCommands()[0];
auto* frag_uniforms = GetFragInfo<FS>(cmd);

ASSERT_EQ(frag_uniforms->alpha, 0.5);
}

} // namespace testing
} // namespace impeller