1 // Copyright 2014 The Chromium 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.
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/ozone/platform/drm/gpu/crtc_controller.h"
10 #include "ui/ozone/platform/drm/gpu/hardware_display_controller.h"
11 #include "ui/ozone/platform/drm/gpu/hardware_display_plane.h"
12 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager.h"
13 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_legacy.h"
14 #include "ui/ozone/platform/drm/gpu/overlay_plane.h"
15 #include "ui/ozone/platform/drm/gpu/scanout_buffer.h"
16 #include "ui/ozone/platform/drm/test/mock_drm_device.h"
20 struct FakePlaneInfo
{
22 uint32_t allowed_crtc_mask
;
25 const FakePlaneInfo kOnePlanePerCrtc
[] = {{10, 1}, {20, 2}};
26 const FakePlaneInfo kTwoPlanesPerCrtc
[] = {{10, 1}, {11, 1}, {20, 2}, {21, 2}};
27 const FakePlaneInfo kOnePlanePerCrtcWithShared
[] = {{10, 1}, {20, 2}, {50, 3}};
29 class FakeScanoutBuffer
: public ui::ScanoutBuffer
{
31 FakeScanoutBuffer() {}
34 uint32_t GetFramebufferId() const override
{ return 1; }
35 uint32_t GetHandle() const override
{ return 0; }
36 gfx::Size
GetSize() const override
{ return gfx::Size(1, 1); }
39 ~FakeScanoutBuffer() override
{}
42 class FakePlaneManager
: public ui::HardwareDisplayPlaneManager
{
44 FakePlaneManager() : plane_count_(0) {}
45 ~FakePlaneManager() override
{}
47 // Normally we'd use DRM to figure out the controller configuration. But we
48 // can't use DRM in unit tests, so we just create a fake configuration.
49 void InitForTest(const FakePlaneInfo
* planes
,
51 const std::vector
<uint32_t>& crtcs
) {
53 for (size_t i
= 0; i
< count
; i
++) {
54 planes_
.push_back(new ui::HardwareDisplayPlane(
55 planes
[i
].id
, planes
[i
].allowed_crtc_mask
));
57 // The real HDPM uses sorted planes, so sort them for consistency.
58 std::sort(planes_
.begin(), planes_
.end(),
59 [](ui::HardwareDisplayPlane
* l
, ui::HardwareDisplayPlane
* r
) {
60 return l
->plane_id() < r
->plane_id();
64 bool Commit(ui::HardwareDisplayPlaneList
* plane_list
, bool is_sync
) override
{
68 bool SetPlaneData(ui::HardwareDisplayPlaneList
* plane_list
,
69 ui::HardwareDisplayPlane
* hw_plane
,
70 const ui::OverlayPlane
& overlay
,
72 const gfx::Rect
& src_rect
,
73 ui::CrtcController
* crtc
) override
{
74 // Check that the chosen plane is a legal choice for the crtc.
75 EXPECT_NE(-1, LookupCrtcIndex(crtc_id
));
76 EXPECT_TRUE(hw_plane
->CanUseForCrtc(LookupCrtcIndex(crtc_id
)));
77 EXPECT_FALSE(hw_plane
->in_use());
82 int plane_count() const { return plane_count_
; }
85 DISALLOW_COPY_AND_ASSIGN(FakePlaneManager
);
90 class HardwareDisplayPlaneManagerTest
: public testing::Test
{
92 HardwareDisplayPlaneManagerTest() {}
94 void SetUp() override
;
97 scoped_ptr
<FakePlaneManager
> plane_manager_
;
98 ui::HardwareDisplayPlaneList state_
;
99 std::vector
<uint32_t> default_crtcs_
;
100 scoped_refptr
<ui::ScanoutBuffer
> fake_buffer_
;
103 DISALLOW_COPY_AND_ASSIGN(HardwareDisplayPlaneManagerTest
);
106 void HardwareDisplayPlaneManagerTest::SetUp() {
107 fake_buffer_
= new FakeScanoutBuffer();
108 plane_manager_
.reset(new FakePlaneManager());
109 default_crtcs_
.push_back(100);
110 default_crtcs_
.push_back(200);
113 TEST_F(HardwareDisplayPlaneManagerTest
, SinglePlaneAssignment
) {
114 ui::OverlayPlaneList assigns
;
115 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
116 plane_manager_
->InitForTest(kOnePlanePerCrtc
, arraysize(kOnePlanePerCrtc
),
118 EXPECT_TRUE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
119 default_crtcs_
[0], nullptr));
120 EXPECT_EQ(1, plane_manager_
->plane_count());
123 TEST_F(HardwareDisplayPlaneManagerTest
, BadCrtc
) {
124 ui::OverlayPlaneList assigns
;
125 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
126 plane_manager_
->InitForTest(kOnePlanePerCrtc
, arraysize(kOnePlanePerCrtc
),
129 plane_manager_
->AssignOverlayPlanes(&state_
, assigns
, 1, nullptr));
132 TEST_F(HardwareDisplayPlaneManagerTest
, MultiplePlaneAssignment
) {
133 ui::OverlayPlaneList assigns
;
134 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
135 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
136 plane_manager_
->InitForTest(kTwoPlanesPerCrtc
, arraysize(kTwoPlanesPerCrtc
),
138 EXPECT_TRUE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
139 default_crtcs_
[0], nullptr));
140 EXPECT_EQ(2, plane_manager_
->plane_count());
143 TEST_F(HardwareDisplayPlaneManagerTest
, NotEnoughPlanes
) {
144 ui::OverlayPlaneList assigns
;
145 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
146 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
147 plane_manager_
->InitForTest(kOnePlanePerCrtc
, arraysize(kOnePlanePerCrtc
),
150 EXPECT_FALSE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
151 default_crtcs_
[0], nullptr));
154 TEST_F(HardwareDisplayPlaneManagerTest
, MultipleCrtcs
) {
155 ui::OverlayPlaneList assigns
;
156 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
157 plane_manager_
->InitForTest(kOnePlanePerCrtc
, arraysize(kOnePlanePerCrtc
),
160 EXPECT_TRUE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
161 default_crtcs_
[0], nullptr));
162 EXPECT_TRUE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
163 default_crtcs_
[1], nullptr));
164 EXPECT_EQ(2, plane_manager_
->plane_count());
167 TEST_F(HardwareDisplayPlaneManagerTest
, MultiplePlanesAndCrtcs
) {
168 ui::OverlayPlaneList assigns
;
169 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
170 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
171 plane_manager_
->InitForTest(kTwoPlanesPerCrtc
, arraysize(kTwoPlanesPerCrtc
),
173 EXPECT_TRUE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
174 default_crtcs_
[0], nullptr));
175 EXPECT_TRUE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
176 default_crtcs_
[1], nullptr));
177 EXPECT_EQ(4, plane_manager_
->plane_count());
180 TEST_F(HardwareDisplayPlaneManagerTest
, MultipleFrames
) {
181 ui::OverlayPlaneList assigns
;
182 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
183 plane_manager_
->InitForTest(kTwoPlanesPerCrtc
, arraysize(kTwoPlanesPerCrtc
),
186 EXPECT_TRUE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
187 default_crtcs_
[0], nullptr));
188 EXPECT_EQ(1, plane_manager_
->plane_count());
189 // Pretend we committed the frame.
190 state_
.committed
= true;
191 state_
.plane_list
.swap(state_
.old_plane_list
);
192 ui::HardwareDisplayPlane
* old_plane
= state_
.old_plane_list
[0];
193 // The same plane should be used.
194 EXPECT_TRUE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
195 default_crtcs_
[0], nullptr));
196 EXPECT_EQ(2, plane_manager_
->plane_count());
197 EXPECT_EQ(state_
.plane_list
[0], old_plane
);
200 TEST_F(HardwareDisplayPlaneManagerTest
, MultipleFramesDifferentPlanes
) {
201 ui::OverlayPlaneList assigns
;
202 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
203 plane_manager_
->InitForTest(kTwoPlanesPerCrtc
, arraysize(kTwoPlanesPerCrtc
),
206 EXPECT_TRUE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
207 default_crtcs_
[0], nullptr));
208 EXPECT_EQ(1, plane_manager_
->plane_count());
209 // The other plane should be used.
210 EXPECT_TRUE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
211 default_crtcs_
[0], nullptr));
212 EXPECT_EQ(2, plane_manager_
->plane_count());
213 EXPECT_NE(state_
.plane_list
[0], state_
.plane_list
[1]);
216 TEST_F(HardwareDisplayPlaneManagerTest
, SharedPlanes
) {
217 ui::OverlayPlaneList assigns
;
218 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
219 assigns
.push_back(ui::OverlayPlane(fake_buffer_
));
220 plane_manager_
->InitForTest(kOnePlanePerCrtcWithShared
,
221 arraysize(kOnePlanePerCrtcWithShared
),
224 EXPECT_TRUE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
225 default_crtcs_
[1], nullptr));
226 EXPECT_EQ(2, plane_manager_
->plane_count());
227 // The shared plane is now unavailable for use by the other CRTC.
228 EXPECT_FALSE(plane_manager_
->AssignOverlayPlanes(&state_
, assigns
,
229 default_crtcs_
[0], nullptr));
232 TEST(HardwareDisplayPlaneManagerLegacyTest
, UnusedPlanesAreReleased
) {
233 std::vector
<uint32_t> crtcs
;
234 crtcs
.push_back(100);
235 scoped_refptr
<ui::MockDrmDevice
> drm
= new ui::MockDrmDevice(false, crtcs
, 2);
236 ui::OverlayPlaneList assigns
;
237 scoped_refptr
<FakeScanoutBuffer
> fake_buffer
= new FakeScanoutBuffer();
238 assigns
.push_back(ui::OverlayPlane(fake_buffer
));
239 assigns
.push_back(ui::OverlayPlane(fake_buffer
));
240 ui::HardwareDisplayPlaneList hdpl
;
241 ui::CrtcController
crtc(drm
, crtcs
[0], 0);
242 EXPECT_TRUE(drm
->plane_manager()->AssignOverlayPlanes(&hdpl
, assigns
,
244 EXPECT_TRUE(drm
->plane_manager()->Commit(&hdpl
, false));
246 assigns
.push_back(ui::OverlayPlane(fake_buffer
));
247 EXPECT_TRUE(drm
->plane_manager()->AssignOverlayPlanes(&hdpl
, assigns
,
249 EXPECT_EQ(0, drm
->get_overlay_clear_call_count());
250 EXPECT_TRUE(drm
->plane_manager()->Commit(&hdpl
, false));
251 EXPECT_EQ(1, drm
->get_overlay_clear_call_count());