1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
ash / fast_ink / view_tree_host_root_view_frame_factory_unittest.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/fast_ink/view_tree_host_root_view_frame_factory.h"
#include <memory>
#include <utility>
#include <vector>
#include "ash/frame_sink/ui_resource.h"
#include "ash/frame_sink/ui_resource_manager.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "components/viz/common/quads/compositor_frame.h"
#include "components/viz/common/resources/resource_id.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace {
constexpr viz::SharedImageFormat kTestSharedImageFormat =
SK_B32_SHIFT ? viz::SinglePlaneFormat::kRGBA_8888
: viz::SinglePlaneFormat::kBGRA_8888;
constexpr UiSourceId kTestSourceId = 1u;
constexpr gfx::Rect kTestContentRect = gfx::Rect(0, 0, 200, 100);
constexpr gfx::Rect kTestTotalDamageRect = gfx::Rect(0, 0, 50, 25);
class ViewTreeHostRootViewFrameFactoryTest : public AshTestBase {
public:
ViewTreeHostRootViewFrameFactoryTest() = default;
ViewTreeHostRootViewFrameFactoryTest(
const ViewTreeHostRootViewFrameFactoryTest&) = delete;
ViewTreeHostRootViewFrameFactoryTest& operator=(
const ViewTreeHostRootViewFrameFactoryTest&) = delete;
~ViewTreeHostRootViewFrameFactoryTest() override = default;
// AshTestBase:
void SetUp() override {
AshTestBase::SetUp();
widget_ = CreateTestWidget(
views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET, nullptr,
kShellWindowId_OverlayContainer, gfx::Rect(0, 0, 200, 100));
factory_ =
std::make_unique<ViewTreeHostRootViewFrameFactory>(widget_.get());
}
// AshTestBase:
void TearDown() override {
resource_manager_.ClearAvailableResources();
resource_manager_.LostExportedResources();
AshTestBase::TearDown();
}
protected:
std::unique_ptr<ViewTreeHostRootViewFrameFactory> factory_;
UiResourceManager resource_manager_;
std::unique_ptr<views::Widget> widget_;
};
TEST_F(ViewTreeHostRootViewFrameFactoryTest,
CompositorFrameHasCorrectStructure) {
UpdateDisplay("1920x1080");
auto frame = factory_->CreateCompositorFrame(
viz::BeginFrameAck::CreateManualAckWithDamage(), kTestContentRect,
kTestTotalDamageRect,
/*use_overlays=*/true, resource_manager_);
auto primary_display = display::Screen::GetScreen()->GetPrimaryDisplay();
// We should only have the root render pass.
EXPECT_EQ(frame->render_pass_list.size(), 1u);
// Frame size should be the size of content_rect in pixels.
EXPECT_EQ(frame->size_in_pixels(), gfx::Size(200, 100));
// We should have a single resource.
EXPECT_EQ(frame->resource_list.size(), 1u);
EXPECT_EQ(resource_manager_.exported_resources_count(), 1u);
auto& quad_list = frame->render_pass_list.front()->quad_list;
// We should have created a single quad.
EXPECT_EQ(quad_list.size(), 1u);
auto& shared_quad_state_list =
frame->render_pass_list.front()->shared_quad_state_list;
// We should create a single shared_quad_state.
EXPECT_EQ(shared_quad_state_list.size(), 1u);
EXPECT_EQ(frame->device_scale_factor(),
primary_display.device_scale_factor());
}
TEST_F(ViewTreeHostRootViewFrameFactoryTest, HasValidSourceId) {
UpdateDisplay("1920x1080*2");
auto frame = factory_->CreateCompositorFrame(
viz::BeginFrameAck::CreateManualAckWithDamage(), kTestContentRect,
kTestTotalDamageRect,
/*use_overlays=*/true, resource_manager_);
ASSERT_EQ(frame->resource_list.size(), 1u);
viz::ResourceId resource_id = frame->resource_list.back().id;
EXPECT_NE(resource_manager_.PeekExportedResource(resource_id)->ui_source_id,
kInvalidUiSourceId);
}
TEST_F(ViewTreeHostRootViewFrameFactoryTest, FrameDamage) {
UpdateDisplay("1920x1080*2");
auto frame = factory_->CreateCompositorFrame(
viz::BeginFrameAck::CreateManualAckWithDamage(), kTestContentRect,
kTestTotalDamageRect,
/*use_overlays=*/true, resource_manager_);
EXPECT_EQ(frame->render_pass_list.front()->damage_rect,
gfx::Rect(0, 0, 100, 50));
// If total damage is more than content_rect, we crop the damage to
// content_rect.
frame = factory_->CreateCompositorFrame(
viz::BeginFrameAck::CreateManualAckWithDamage(), kTestContentRect,
gfx::Rect(0, 0, 201, 101),
/*use_overlays=*/true, resource_manager_);
EXPECT_EQ(frame->render_pass_list.front()->damage_rect,
gfx::Rect(0, 0, 400, 200));
}
TEST_F(ViewTreeHostRootViewFrameFactoryTest, CorrectQuadConfigured) {
UpdateDisplay("1920x1080*2");
auto frame = factory_->CreateCompositorFrame(
viz::BeginFrameAck::CreateManualAckWithDamage(), kTestContentRect,
kTestTotalDamageRect,
/*use_overlays=*/true, resource_manager_);
auto& quad_list = frame->render_pass_list.front()->quad_list;
ASSERT_EQ(quad_list.size(), 1u);
viz::DrawQuad* quad = quad_list.back();
EXPECT_EQ(quad->material, viz::DrawQuad::Material::kTextureContent);
// Both should be same as content_rect in pixels.
EXPECT_EQ(quad->rect, gfx::Rect(400, 200));
EXPECT_EQ(quad->visible_rect, gfx::Rect(400, 200));
auto* shared_quad_state =
frame->render_pass_list.front()->shared_quad_state_list.front();
EXPECT_EQ(shared_quad_state->quad_layer_rect, gfx::Rect(400, 200));
EXPECT_EQ(shared_quad_state->visible_quad_layer_rect, gfx::Rect(400, 200));
}
class ViewTreeHostRootViewFrameResourceTest
: public ViewTreeHostRootViewFrameFactoryTest,
public ::testing::WithParamInterface<
std::tuple<std::string, gfx::Rect, gfx::Size>> {
public:
ViewTreeHostRootViewFrameResourceTest()
: display_spec_(std::get<0>(GetParam())),
content_rect_(std::get<1>(GetParam())),
expected_resource_size_(std::get<2>(GetParam())) {}
ViewTreeHostRootViewFrameResourceTest(
const ViewTreeHostRootViewFrameResourceTest&) = delete;
ViewTreeHostRootViewFrameResourceTest& operator=(
const ViewTreeHostRootViewFrameResourceTest&) = delete;
~ViewTreeHostRootViewFrameResourceTest() override = default;
protected:
std::string display_spec_;
gfx::Rect content_rect_;
gfx::Size expected_resource_size_;
};
TEST_P(ViewTreeHostRootViewFrameResourceTest, CorrectResourceCreated) {
UpdateDisplay(display_spec_);
auto frame = factory_->CreateCompositorFrame(
viz::BeginFrameAck::CreateManualAckWithDamage(), content_rect_,
content_rect_,
/*use_overlays=*/true, resource_manager_);
auto primary_display = display::Screen::GetScreen()->GetPrimaryDisplay();
ASSERT_EQ(frame->resource_list.size(), 1u);
auto& resource = frame->resource_list.back();
EXPECT_EQ(resource.size, expected_resource_size_);
}
INSTANTIATE_TEST_SUITE_P(
/* no prefix */,
ViewTreeHostRootViewFrameResourceTest,
testing::Values(
std::make_tuple("500x400",
/*content_rect=*/gfx::Rect(200, 100),
/*expected_resource_size=*/gfx::Size(200, 100)),
std::make_tuple("500x400*2",
/*content_rect=*/gfx::Rect(200, 100),
/*expected_resource_size=*/gfx::Size(400, 200)),
// Display is rotated by 90 degrees clockwise.
std::make_tuple("500x400/r",
/*content_rect=*/gfx::Rect(200, 100),
/*expected_resource_size=*/gfx::Size(100, 200)),
// Display is rotated by 180 degrees clockwise.
std::make_tuple("500x400/u",
/*content_rect=*/gfx::Rect(200, 100),
/*expected_resource_size=*/gfx::Size(200, 100)),
// Display is rotated by 270 degrees clockwise.
std::make_tuple("500x400/l",
/*content_rect=*/gfx::Rect(200, 100),
/*expected_resource_size=*/gfx::Size(100, 200)),
// Display is rotated by 90 degrees clockwise and has device scale
// factor of 2.
std::make_tuple("500x400*2/r",
/*content_rect=*/gfx::Rect(200, 100),
/*expected_resource_size=*/gfx::Size(200, 400))));
TEST_F(ViewTreeHostRootViewFrameFactoryTest,
OnlyCreateNewResourcesWhenNecessary) {
// Populate resources in the resource manager.
constexpr gfx::Size kResourceSizes[4] = {
{200, 100}, {200, 100}, {250, 150}, {50, 25}};
for (const auto& size : kResourceSizes) {
resource_manager_.OfferResource(
ViewTreeHostRootViewFrameFactory::CreateUiResource(
size, kTestSharedImageFormat, kTestSourceId,
/*is_overlay_candidate=*/false));
}
EXPECT_EQ(resource_manager_.available_resources_count(), 4u);
UpdateDisplay("1920x1080");
auto frame = factory_->CreateCompositorFrame(
viz::BeginFrameAck::CreateManualAckWithDamage(), kTestContentRect,
kTestTotalDamageRect,
/*use_overlays=*/true, resource_manager_);
// We reuse one of the matching available resources.
EXPECT_EQ(resource_manager_.available_resources_count(), 3u);
EXPECT_EQ(resource_manager_.exported_resources_count(), 1u);
frame = factory_->CreateCompositorFrame(
viz::BeginFrameAck::CreateManualAckWithDamage(), kTestContentRect,
kTestTotalDamageRect,
/*use_overlays=*/true, resource_manager_);
// We again reuse one of the matching available resources.
EXPECT_EQ(resource_manager_.available_resources_count(), 2u);
EXPECT_EQ(resource_manager_.exported_resources_count(), 2u);
frame = factory_->CreateCompositorFrame(
viz::BeginFrameAck::CreateManualAckWithDamage(), kTestContentRect,
kTestTotalDamageRect,
/*use_overlays=*/true, resource_manager_);
// Now the factory create a new resource since any available resource does not
// match our requirements. The total number of resources in the manager has
// increased by 1.
EXPECT_EQ(resource_manager_.available_resources_count(), 2u);
EXPECT_EQ(resource_manager_.exported_resources_count(), 3u);
frame = factory_->CreateCompositorFrame(
viz::BeginFrameAck::CreateManualAckWithDamage(), gfx::Rect(50, 25),
kTestTotalDamageRect,
/*use_overlays=*/true, resource_manager_);
// We do not create a resource since there is an available resource for the
// new needed size.
EXPECT_EQ(resource_manager_.available_resources_count(), 1u);
EXPECT_EQ(resource_manager_.exported_resources_count(), 4u);
}
} // namespace
} // namespace ash