| Flutter Impeller |

vertices_contents_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <memory>
6 #include <optional>
7
8 #include "gtest/gtest.h"
9
10 #include "impeller/entity/contents/contents.h"
11 #include "impeller/entity/contents/solid_color_contents.h"
12 #include "impeller/entity/contents/test/contents_test_helpers.h"
13 #include "impeller/entity/contents/test/recording_render_pass.h"
14 #include "impeller/entity/contents/vertices_contents.h"
15 #include "impeller/entity/entity.h"
16 #include "impeller/entity/entity_playground.h"
17 #include "impeller/entity/vertices.frag.h"
18 #include "impeller/geometry/path_builder.h"
19 #include "impeller/renderer/render_target.h"
20
21 namespace impeller {
22 namespace testing {
23
24 using EntityTest = EntityPlayground;
25
26 std::shared_ptr<VerticesGeometry> CreateColorVertices(
27 const std::vector<Point>& vertices,
28 const std::vector<Color>& colors) {
29 auto bounds = Rect::MakePointBounds(vertices.begin(), vertices.end());
30 std::vector<uint16_t> indices = {};
31 indices.reserve(vertices.size());
32 for (auto i = 0u; i < vertices.size(); i++) {
33 indices.emplace_back(i);
34 }
35 std::vector<Point> texture_coordinates = {};
36
37 return std::make_shared<VerticesGeometry>(
38 vertices, indices, texture_coordinates, colors,
39 bounds.value_or(Rect::MakeLTRB(0, 0, 0, 0)),
40 VerticesGeometry::VertexMode::kTriangles);
41 }
42
43 // Verifies that the destination blend fast path still sets an alpha value.
44 TEST_P(EntityTest, RendersDstPerColorWithAlpha) {
45 using FS = GeometryColorPipeline::FragmentShader;
46
47 auto contents = std::make_shared<VerticesContents>();
48 auto vertices = CreateColorVertices(
49 {{0, 0}, {100, 0}, {0, 100}, {100, 0}, {0, 100}, {100, 100}},
50 {Color::Red(), Color::Red(), Color::Red(), Color::Red(), Color::Red(),
51 Color::Red()});
52 auto src_contents = SolidColorContents::Make(
53 PathBuilder{}.AddRect(Rect::MakeLTRB(0, 0, 100, 100)).TakePath(),
54 Color::Red());
55
56 contents->SetGeometry(vertices);
57 contents->SetAlpha(0.5);
58 contents->SetBlendMode(BlendMode::kDestination);
59 contents->SetSourceContents(std::move(src_contents));
60
61 auto content_context = GetContentContext();
62 auto buffer = content_context->GetContext()->CreateCommandBuffer();
63 auto render_target =
64 GetContentContext()->GetRenderTargetCache()->CreateOffscreenMSAA(
65 *content_context->GetContext(), {100, 100},
66 /*mip_count=*/1);
67 auto render_pass = buffer->CreateRenderPass(render_target);
68 auto recording_pass = std::make_shared<RecordingRenderPass>(
69 render_pass, GetContext(), render_target);
70 Entity entity;
71
72 ASSERT_TRUE(recording_pass->GetCommands().empty());
73 ASSERT_TRUE(contents->Render(*content_context, entity, *recording_pass));
74
75 ASSERT_TRUE(recording_pass->GetCommands().size() > 0);
76 const auto& cmd = recording_pass->GetCommands()[0];
77 auto* frag_uniforms = GetFragInfo<FS>(cmd);
78
79 ASSERT_TRUE(frag_uniforms);
80 ASSERT_EQ(frag_uniforms->alpha, 0.5);
81
82 if (GetParam() == PlaygroundBackend::kMetal) {
83 recording_pass->EncodeCommands();
84 }
85 }
86
87 } // namespace testing
88 } // namespace impeller
impeller::SolidColorContents::Make
static std::unique_ptr< SolidColorContents > Make(const Path &path, Color color)
Definition: solid_color_contents.cc:69
contents.h
impeller::Color::Red
static constexpr Color Red()
Definition: color.h:264
entity.h
solid_color_contents.h
impeller::PlaygroundBackend::kMetal
@ kMetal
impeller::BlendMode::kDestination
@ kDestination
impeller::PathBuilder
Definition: path_builder.h:14
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:94
impeller::PathBuilder::AddRect
PathBuilder & AddRect(Rect rect)
Definition: path_builder.cc:112
impeller::TRect< Scalar >::MakePointBounds
constexpr static std::optional< TRect > MakePointBounds(const U &value)
Definition: rect.h:151
vertices_contents.h
path_builder.h
impeller::Entity
Definition: entity.h:21
impeller::testing::CreateColorVertices
std::shared_ptr< VerticesGeometry > CreateColorVertices(const std::vector< Point > &vertices, const std::vector< Color > &colors)
Definition: vertices_contents_unittests.cc:26
impeller::testing::EntityTest
EntityPlayground EntityTest
Definition: checkerboard_contents_unittests.cc:20
impeller::EntityPlayground
Definition: entity_playground.h:18
impeller::testing::TEST_P
TEST_P(AiksTest, CanRenderMaskBlurHugeSigma)
Definition: aiks_blur_unittests.cc:23
impeller::VerticesGeometry::VertexMode::kTriangles
@ kTriangles
impeller::TRect< Scalar >::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
render_target.h
impeller
Definition: aiks_blur_unittests.cc:20
entity_playground.h