ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / ozone / platform / dri / hardware_display_controller_unittest.cc
blobf63c1367585aeb419d2955f0931e79407df42b16
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.
5 #include "base/bind.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "third_party/skia/include/core/SkCanvas.h"
8 #include "ui/ozone/platform/dri/crtc_controller.h"
9 #include "ui/ozone/platform/dri/dri_buffer.h"
10 #include "ui/ozone/platform/dri/drm_device.h"
11 #include "ui/ozone/platform/dri/hardware_display_controller.h"
12 #include "ui/ozone/platform/dri/test/mock_drm_device.h"
13 #include "ui/ozone/public/native_pixmap.h"
15 namespace {
17 // Create a basic mode for a 6x4 screen.
18 const drmModeModeInfo kDefaultMode =
19 {0, 6, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, {'\0'}};
21 const uint32_t kPrimaryCrtc = 1;
22 const uint32_t kPrimaryConnector = 2;
23 const uint32_t kSecondaryCrtc = 3;
24 const uint32_t kSecondaryConnector = 4;
25 const size_t kPlanesPerCrtc = 2;
27 const gfx::Size kDefaultModeSize(kDefaultMode.hdisplay, kDefaultMode.vdisplay);
28 const gfx::SizeF kDefaultModeSizeF(1.0, 1.0);
30 class MockScanoutBuffer : public ui::ScanoutBuffer {
31 public:
32 MockScanoutBuffer(const gfx::Size& size) : size_(size) {}
34 // ScanoutBuffer:
35 uint32_t GetFramebufferId() const override { return 0; }
36 uint32_t GetHandle() const override { return 0; }
37 gfx::Size GetSize() const override { return size_; }
39 private:
40 ~MockScanoutBuffer() override {}
42 gfx::Size size_;
44 DISALLOW_COPY_AND_ASSIGN(MockScanoutBuffer);
47 } // namespace
49 class HardwareDisplayControllerTest : public testing::Test {
50 public:
51 HardwareDisplayControllerTest() : page_flips_(0) {}
52 ~HardwareDisplayControllerTest() override {}
54 void SetUp() override;
55 void TearDown() override;
57 void PageFlipCallback();
59 protected:
60 scoped_ptr<ui::HardwareDisplayController> controller_;
61 scoped_refptr<ui::MockDrmDevice> drm_;
63 int page_flips_;
65 private:
66 DISALLOW_COPY_AND_ASSIGN(HardwareDisplayControllerTest);
69 void HardwareDisplayControllerTest::SetUp() {
70 std::vector<uint32_t> crtcs;
71 crtcs.push_back(kPrimaryCrtc);
72 crtcs.push_back(kSecondaryCrtc);
73 drm_ = new ui::MockDrmDevice(false, crtcs, kPlanesPerCrtc);
74 controller_.reset(new ui::HardwareDisplayController(
75 scoped_ptr<ui::CrtcController>(new ui::CrtcController(
76 drm_.get(), kPrimaryCrtc, kPrimaryConnector))));
79 void HardwareDisplayControllerTest::TearDown() {
80 controller_.reset();
81 drm_ = nullptr;
84 void HardwareDisplayControllerTest::PageFlipCallback() {
85 page_flips_++;
88 TEST_F(HardwareDisplayControllerTest, CheckModesettingResult) {
89 ui::OverlayPlane plane(scoped_refptr<ui::ScanoutBuffer>(
90 new MockScanoutBuffer(kDefaultModeSize)));
92 EXPECT_TRUE(controller_->Modeset(plane, kDefaultMode));
93 EXPECT_FALSE(plane.buffer->HasOneRef());
96 TEST_F(HardwareDisplayControllerTest, CheckStateAfterPageFlip) {
97 ui::OverlayPlane plane1(scoped_refptr<ui::ScanoutBuffer>(
98 new MockScanoutBuffer(kDefaultModeSize)));
100 EXPECT_TRUE(controller_->Modeset(plane1, kDefaultMode));
102 ui::OverlayPlane plane2(scoped_refptr<ui::ScanoutBuffer>(
103 new MockScanoutBuffer(kDefaultModeSize)));
104 controller_->QueueOverlayPlane(plane2);
105 EXPECT_TRUE(controller_->SchedulePageFlip(
106 false, base::Bind(&HardwareDisplayControllerTest::PageFlipCallback,
107 base::Unretained(this))));
108 drm_->RunCallbacks();
109 EXPECT_TRUE(plane1.buffer->HasOneRef());
110 EXPECT_FALSE(plane2.buffer->HasOneRef());
112 EXPECT_EQ(1, drm_->get_page_flip_call_count());
113 EXPECT_EQ(0, drm_->get_overlay_flip_call_count());
116 TEST_F(HardwareDisplayControllerTest, CheckStateIfModesetFails) {
117 drm_->set_set_crtc_expectation(false);
119 ui::OverlayPlane plane(scoped_refptr<ui::ScanoutBuffer>(new MockScanoutBuffer(
120 kDefaultModeSize)));
122 EXPECT_FALSE(controller_->Modeset(plane, kDefaultMode));
125 TEST_F(HardwareDisplayControllerTest, CheckStateIfPageFlipFails) {
126 drm_->set_page_flip_expectation(false);
128 ui::OverlayPlane plane1(scoped_refptr<ui::ScanoutBuffer>(
129 new MockScanoutBuffer(kDefaultModeSize)));
131 EXPECT_TRUE(controller_->Modeset(plane1, kDefaultMode));
133 ui::OverlayPlane plane2(scoped_refptr<ui::ScanoutBuffer>(
134 new MockScanoutBuffer(kDefaultModeSize)));
135 controller_->QueueOverlayPlane(plane2);
136 EXPECT_FALSE(controller_->SchedulePageFlip(
137 false, base::Bind(&HardwareDisplayControllerTest::PageFlipCallback,
138 base::Unretained(this))));
139 drm_->RunCallbacks();
141 EXPECT_FALSE(plane1.buffer->HasOneRef());
142 EXPECT_TRUE(plane2.buffer->HasOneRef());
145 TEST_F(HardwareDisplayControllerTest, VerifyNoDRMCallsWhenDisabled) {
146 ui::OverlayPlane plane1(scoped_refptr<ui::ScanoutBuffer>(
147 new MockScanoutBuffer(kDefaultModeSize)));
149 EXPECT_TRUE(controller_->Modeset(plane1, kDefaultMode));
150 controller_->Disable();
151 ui::OverlayPlane plane2(scoped_refptr<ui::ScanoutBuffer>(
152 new MockScanoutBuffer(kDefaultModeSize)));
153 controller_->QueueOverlayPlane(plane2);
154 EXPECT_TRUE(controller_->SchedulePageFlip(
155 false, base::Bind(&HardwareDisplayControllerTest::PageFlipCallback,
156 base::Unretained(this))));
157 drm_->RunCallbacks();
158 EXPECT_EQ(0, drm_->get_page_flip_call_count());
160 EXPECT_TRUE(controller_->Modeset(plane1, kDefaultMode));
161 controller_->QueueOverlayPlane(plane2);
162 EXPECT_TRUE(controller_->SchedulePageFlip(
163 false, base::Bind(&HardwareDisplayControllerTest::PageFlipCallback,
164 base::Unretained(this))));
165 drm_->RunCallbacks();
166 EXPECT_EQ(1, drm_->get_page_flip_call_count());
169 TEST_F(HardwareDisplayControllerTest, CheckOverlayPresent) {
170 ui::OverlayPlane plane1(scoped_refptr<ui::ScanoutBuffer>(
171 new MockScanoutBuffer(kDefaultModeSize)));
172 ui::OverlayPlane plane2(
173 scoped_refptr<ui::ScanoutBuffer>(new MockScanoutBuffer(kDefaultModeSize)),
175 gfx::OVERLAY_TRANSFORM_NONE,
176 gfx::Rect(kDefaultModeSize),
177 gfx::RectF(kDefaultModeSizeF));
179 EXPECT_TRUE(controller_->Modeset(plane1, kDefaultMode));
181 controller_->QueueOverlayPlane(plane1);
182 controller_->QueueOverlayPlane(plane2);
184 EXPECT_TRUE(controller_->SchedulePageFlip(
185 false, base::Bind(&HardwareDisplayControllerTest::PageFlipCallback,
186 base::Unretained(this))));
187 drm_->RunCallbacks();
188 EXPECT_EQ(1, drm_->get_page_flip_call_count());
189 EXPECT_EQ(1, drm_->get_overlay_flip_call_count());
192 TEST_F(HardwareDisplayControllerTest, PageflipMirroredControllers) {
193 controller_->AddCrtc(scoped_ptr<ui::CrtcController>(
194 new ui::CrtcController(drm_.get(), kSecondaryCrtc, kSecondaryConnector)));
196 ui::OverlayPlane plane1(scoped_refptr<ui::ScanoutBuffer>(
197 new MockScanoutBuffer(kDefaultModeSize)));
198 EXPECT_TRUE(controller_->Modeset(plane1, kDefaultMode));
199 EXPECT_EQ(2, drm_->get_set_crtc_call_count());
201 ui::OverlayPlane plane2(scoped_refptr<ui::ScanoutBuffer>(
202 new MockScanoutBuffer(kDefaultModeSize)));
203 controller_->QueueOverlayPlane(plane2);
204 EXPECT_TRUE(controller_->SchedulePageFlip(
205 false, base::Bind(&HardwareDisplayControllerTest::PageFlipCallback,
206 base::Unretained(this))));
207 drm_->RunCallbacks();
208 EXPECT_EQ(2, drm_->get_page_flip_call_count());
209 EXPECT_EQ(1, page_flips_);
212 TEST_F(HardwareDisplayControllerTest, PlaneStateAfterRemoveCrtc) {
213 ui::OverlayPlane plane1(scoped_refptr<ui::ScanoutBuffer>(
214 new MockScanoutBuffer(kDefaultModeSize)));
215 EXPECT_TRUE(controller_->Modeset(plane1, kDefaultMode));
216 controller_->QueueOverlayPlane(plane1);
217 EXPECT_TRUE(controller_->SchedulePageFlip(
218 false, base::Bind(&HardwareDisplayControllerTest::PageFlipCallback,
219 base::Unretained(this))));
220 drm_->RunCallbacks();
222 const ui::HardwareDisplayPlane* owned_plane = nullptr;
223 for (const auto& plane : drm_->plane_manager()->planes())
224 if (plane->in_use())
225 owned_plane = plane;
226 ASSERT_TRUE(owned_plane != nullptr);
227 EXPECT_EQ(kPrimaryCrtc, owned_plane->owning_crtc());
228 // Removing the crtc should free the plane.
229 scoped_ptr<ui::CrtcController> crtc =
230 controller_->RemoveCrtc(drm_, kPrimaryCrtc);
231 EXPECT_FALSE(owned_plane->in_use());
234 TEST_F(HardwareDisplayControllerTest, ModesetWhilePageFlipping) {
235 ui::OverlayPlane plane1(scoped_refptr<ui::ScanoutBuffer>(
236 new MockScanoutBuffer(kDefaultModeSize)));
237 EXPECT_TRUE(controller_->Modeset(plane1, kDefaultMode));
238 controller_->QueueOverlayPlane(plane1);
239 EXPECT_TRUE(controller_->SchedulePageFlip(
240 false, base::Bind(&HardwareDisplayControllerTest::PageFlipCallback,
241 base::Unretained(this))));
243 EXPECT_TRUE(controller_->Modeset(plane1, kDefaultMode));
244 drm_->RunCallbacks();
245 EXPECT_EQ(1, page_flips_);
248 TEST_F(HardwareDisplayControllerTest, AddCrtcMidPageFlip) {
249 ui::OverlayPlane plane1(scoped_refptr<ui::ScanoutBuffer>(
250 new MockScanoutBuffer(kDefaultModeSize)));
251 EXPECT_TRUE(controller_->Modeset(plane1, kDefaultMode));
252 controller_->QueueOverlayPlane(plane1);
253 EXPECT_TRUE(controller_->SchedulePageFlip(
254 false, base::Bind(&HardwareDisplayControllerTest::PageFlipCallback,
255 base::Unretained(this))));
257 controller_->AddCrtc(scoped_ptr<ui::CrtcController>(
258 new ui::CrtcController(drm_.get(), kSecondaryCrtc, kSecondaryConnector)));
260 drm_->RunCallbacks();
261 EXPECT_EQ(1, page_flips_);
264 TEST_F(HardwareDisplayControllerTest, RemoveCrtcMidPageFlip) {
265 ui::OverlayPlane plane1(scoped_refptr<ui::ScanoutBuffer>(
266 new MockScanoutBuffer(kDefaultModeSize)));
267 EXPECT_TRUE(controller_->Modeset(plane1, kDefaultMode));
268 controller_->QueueOverlayPlane(plane1);
269 EXPECT_TRUE(controller_->SchedulePageFlip(
270 false, base::Bind(&HardwareDisplayControllerTest::PageFlipCallback,
271 base::Unretained(this))));
273 controller_->RemoveCrtc(drm_, kPrimaryCrtc);
275 EXPECT_EQ(1, page_flips_);
276 drm_->RunCallbacks();
277 EXPECT_EQ(1, page_flips_);