Flutter Engine
The Flutter Engine
host_buffer_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 "flutter/testing/testing.h"
8
9namespace impeller {
10namespace testing {
11
14
15TEST_P(HostBufferTest, CanEmplace) {
16 struct Length2 {
17 uint8_t pad[2];
18 };
19 static_assert(sizeof(Length2) == 2u);
20
21 auto buffer = HostBuffer::Create(GetContext()->GetResourceAllocator());
22
23 for (size_t i = 0; i < 12500; i++) {
24 auto view = buffer->Emplace(Length2{});
25 ASSERT_TRUE(view);
26 ASSERT_EQ(view.range, Range(i * sizeof(Length2), 2u));
27 }
28}
29
30TEST_P(HostBufferTest, CanEmplaceWithAlignment) {
31 struct Length2 {
32 uint8_t pad[2];
33 };
34 static_assert(sizeof(Length2) == 2);
35 struct alignas(16) Align16 {
36 uint8_t pad[2];
37 };
38 static_assert(alignof(Align16) == 16);
39 static_assert(sizeof(Align16) == 16);
40
41 auto buffer = HostBuffer::Create(GetContext()->GetResourceAllocator());
42 ASSERT_TRUE(buffer);
43
44 {
45 auto view = buffer->Emplace(Length2{});
46 ASSERT_TRUE(view);
47 ASSERT_EQ(view.range, Range(0u, 2u));
48 }
49
50 {
51 auto view = buffer->Emplace(Align16{});
52 ASSERT_TRUE(view);
53 ASSERT_EQ(view.range.offset, 16u);
54 ASSERT_EQ(view.range.length, 16u);
55 }
56 {
57 auto view = buffer->Emplace(Length2{});
58 ASSERT_TRUE(view);
59 ASSERT_EQ(view.range, Range(32u, 2u));
60 }
61
62 {
63 auto view = buffer->Emplace(Align16{});
64 ASSERT_TRUE(view);
65 ASSERT_EQ(view.range.offset, 48u);
66 ASSERT_EQ(view.range.length, 16u);
67 }
68}
69
70TEST_P(HostBufferTest, HostBufferInitialState) {
71 auto buffer = HostBuffer::Create(GetContext()->GetResourceAllocator());
72
73 EXPECT_EQ(buffer->GetStateForTest().current_buffer, 0u);
74 EXPECT_EQ(buffer->GetStateForTest().current_frame, 0u);
75 EXPECT_EQ(buffer->GetStateForTest().total_buffer_count, 1u);
76}
77
78TEST_P(HostBufferTest, ResetIncrementsFrameCounter) {
79 auto buffer = HostBuffer::Create(GetContext()->GetResourceAllocator());
80
81 EXPECT_EQ(buffer->GetStateForTest().current_frame, 0u);
82
83 buffer->Reset();
84 EXPECT_EQ(buffer->GetStateForTest().current_frame, 1u);
85
86 buffer->Reset();
87 EXPECT_EQ(buffer->GetStateForTest().current_frame, 2u);
88
89 buffer->Reset();
90 EXPECT_EQ(buffer->GetStateForTest().current_frame, 0u);
91}
92
94 EmplacingLargerThanBlockSizeCreatesOneOffBufferCallback) {
95 auto buffer = HostBuffer::Create(GetContext()->GetResourceAllocator());
96
97 // Emplace an amount larger than the block size, to verify that the host
98 // buffer does not create a buffer.
99 auto buffer_view = buffer->Emplace(1024000 + 10, 0, [](uint8_t* data) {});
100
101 EXPECT_EQ(buffer->GetStateForTest().current_buffer, 0u);
102 EXPECT_EQ(buffer->GetStateForTest().current_frame, 0u);
103 EXPECT_EQ(buffer->GetStateForTest().total_buffer_count, 1u);
104}
105
106TEST_P(HostBufferTest, EmplacingLargerThanBlockSizeCreatesOneOffBuffer) {
107 auto buffer = HostBuffer::Create(GetContext()->GetResourceAllocator());
108
109 // Emplace an amount larger than the block size, to verify that the host
110 // buffer does not create a buffer.
111 auto buffer_view = buffer->Emplace(nullptr, 1024000 + 10, 0);
112
113 EXPECT_EQ(buffer->GetStateForTest().current_buffer, 0u);
114 EXPECT_EQ(buffer->GetStateForTest().current_frame, 0u);
115 EXPECT_EQ(buffer->GetStateForTest().total_buffer_count, 1u);
116}
117
118TEST_P(HostBufferTest, UnusedBuffersAreDiscardedWhenResetting) {
119 auto buffer = HostBuffer::Create(GetContext()->GetResourceAllocator());
120
121 // Emplace two large allocations to force the allocation of a second buffer.
122 auto buffer_view_a = buffer->Emplace(1020000, 0, [](uint8_t* data) {});
123 auto buffer_view_b = buffer->Emplace(1020000, 0, [](uint8_t* data) {});
124
125 EXPECT_EQ(buffer->GetStateForTest().current_buffer, 1u);
126 EXPECT_EQ(buffer->GetStateForTest().total_buffer_count, 2u);
127 EXPECT_EQ(buffer->GetStateForTest().current_frame, 0u);
128
129 // Reset until we get back to this frame.
130 for (auto i = 0; i < 3; i++) {
131 buffer->Reset();
132 }
133
134 EXPECT_EQ(buffer->GetStateForTest().current_buffer, 0u);
135 EXPECT_EQ(buffer->GetStateForTest().total_buffer_count, 2u);
136 EXPECT_EQ(buffer->GetStateForTest().current_frame, 0u);
137
138 // Now when we reset, the buffer should get dropped.
139 // Reset until we get back to this frame.
140 for (auto i = 0; i < 3; i++) {
141 buffer->Reset();
142 }
143
144 EXPECT_EQ(buffer->GetStateForTest().current_buffer, 0u);
145 EXPECT_EQ(buffer->GetStateForTest().total_buffer_count, 1u);
146 EXPECT_EQ(buffer->GetStateForTest().current_frame, 0u);
147}
148
149TEST_P(HostBufferTest, EmplaceWithProcIsAligned) {
150 auto buffer = HostBuffer::Create(GetContext()->GetResourceAllocator());
151
152 BufferView view = buffer->Emplace(std::array<char, 21>());
153 EXPECT_EQ(view.range, Range(0, 21));
154
155 view = buffer->Emplace(64, 16, [](uint8_t*) {});
156 EXPECT_EQ(view.range, Range(32, 64));
157}
158
159} // namespace testing
160} // namespace impeller
BufferView buffer_view
static std::shared_ptr< HostBuffer > Create(const std::shared_ptr< Allocator > &allocator)
Definition: host_buffer.cc:20
SK_API GrDirectContext * GetContext(const SkImage *src)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
TEST_P(AiksTest, CanRenderAdvancedBlendColorFilterWithSaveLayer)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63