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/message_loop/message_loop.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "third_party/skia/include/core/SkCanvas.h"
8 #include "third_party/skia/include/core/SkColor.h"
9 #include "third_party/skia/include/core/SkDevice.h"
10 #include "ui/ozone/platform/drm/gpu/crtc_controller.h"
11 #include "ui/ozone/platform/drm/gpu/drm_buffer.h"
12 #include "ui/ozone/platform/drm/gpu/drm_device_generator.h"
13 #include "ui/ozone/platform/drm/gpu/drm_device_manager.h"
14 #include "ui/ozone/platform/drm/gpu/drm_surface.h"
15 #include "ui/ozone/platform/drm/gpu/drm_window.h"
16 #include "ui/ozone/platform/drm/gpu/hardware_display_controller.h"
17 #include "ui/ozone/platform/drm/gpu/screen_manager.h"
18 #include "ui/ozone/platform/drm/test/mock_drm_device.h"
22 // Create a basic mode for a 6x4 screen.
23 const drmModeModeInfo kDefaultMode
=
24 {0, 6, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, {'\0'}};
26 const gfx::AcceleratedWidget kDefaultWidgetHandle
= 1;
27 const uint32_t kDefaultCrtc
= 1;
28 const uint32_t kDefaultConnector
= 2;
29 const size_t kPlanesPerCrtc
= 1;
30 const uint32_t kDefaultCursorSize
= 64;
34 class DrmSurfaceTest
: public testing::Test
{
38 void SetUp() override
;
39 void TearDown() override
;
42 scoped_ptr
<base::MessageLoop
> message_loop_
;
43 scoped_refptr
<ui::MockDrmDevice
> drm_
;
44 scoped_ptr
<ui::DrmBufferGenerator
> buffer_generator_
;
45 scoped_ptr
<ui::ScreenManager
> screen_manager_
;
46 scoped_ptr
<ui::DrmDeviceManager
> drm_device_manager_
;
47 scoped_ptr
<ui::DrmSurface
> surface_
;
50 DISALLOW_COPY_AND_ASSIGN(DrmSurfaceTest
);
53 void DrmSurfaceTest::SetUp() {
54 message_loop_
.reset(new base::MessageLoopForUI
);
55 std::vector
<uint32_t> crtcs
;
56 crtcs
.push_back(kDefaultCrtc
);
57 drm_
= new ui::MockDrmDevice(false, crtcs
, kPlanesPerCrtc
);
58 buffer_generator_
.reset(new ui::DrmBufferGenerator());
59 screen_manager_
.reset(new ui::ScreenManager(buffer_generator_
.get()));
60 screen_manager_
->AddDisplayController(drm_
, kDefaultCrtc
, kDefaultConnector
);
61 screen_manager_
->ConfigureDisplayController(
62 drm_
, kDefaultCrtc
, kDefaultConnector
, gfx::Point(), kDefaultMode
);
64 drm_device_manager_
.reset(new ui::DrmDeviceManager(nullptr));
65 scoped_ptr
<ui::DrmWindow
> window(new ui::DrmWindow(
66 kDefaultWidgetHandle
, drm_device_manager_
.get(), screen_manager_
.get()));
68 window
->OnBoundsChanged(
69 gfx::Rect(gfx::Size(kDefaultMode
.hdisplay
, kDefaultMode
.vdisplay
)));
70 screen_manager_
->AddWindow(kDefaultWidgetHandle
, window
.Pass());
73 new ui::DrmSurface(screen_manager_
->GetWindow(kDefaultWidgetHandle
)));
74 surface_
->ResizeCanvas(
75 gfx::Size(kDefaultMode
.hdisplay
, kDefaultMode
.vdisplay
));
78 void DrmSurfaceTest::TearDown() {
80 scoped_ptr
<ui::DrmWindow
> window
=
81 screen_manager_
->RemoveWindow(kDefaultWidgetHandle
);
84 message_loop_
.reset();
87 TEST_F(DrmSurfaceTest
, CheckFBIDOnSwap
) {
88 surface_
->PresentCanvas(gfx::Rect());
90 // Framebuffer ID 1 is allocated in SetUp for the buffer used to modeset.
91 EXPECT_EQ(3u, drm_
->current_framebuffer());
92 surface_
->PresentCanvas(gfx::Rect());
94 EXPECT_EQ(2u, drm_
->current_framebuffer());
97 TEST_F(DrmSurfaceTest
, CheckSurfaceContents
) {
99 paint
.setColor(SK_ColorWHITE
);
101 SkRect::MakeWH(kDefaultMode
.hdisplay
/ 2, kDefaultMode
.vdisplay
/ 2);
102 surface_
->GetSurface()->getCanvas()->drawRect(rect
, paint
);
103 surface_
->PresentCanvas(
104 gfx::Rect(0, 0, kDefaultMode
.hdisplay
/ 2, kDefaultMode
.vdisplay
/ 2));
105 drm_
->RunCallbacks();
108 std::vector
<skia::RefPtr
<SkSurface
>> framebuffers
;
109 for (const auto& buffer
: drm_
->buffers()) {
110 // Skip cursor buffers.
111 if (buffer
->width() == kDefaultCursorSize
&&
112 buffer
->height() == kDefaultCursorSize
)
115 framebuffers
.push_back(buffer
);
118 // Buffer 0 is the modesetting buffer, buffer 1 is the frontbuffer and buffer
119 // 2 is the backbuffer.
120 EXPECT_EQ(3u, framebuffers
.size());
122 image
.setInfo(framebuffers
[2]->getCanvas()->imageInfo());
123 EXPECT_TRUE(framebuffers
[2]->getCanvas()->readPixels(&image
, 0, 0));
125 EXPECT_EQ(kDefaultMode
.hdisplay
, image
.width());
126 EXPECT_EQ(kDefaultMode
.vdisplay
, image
.height());
128 // Make sure the updates are correctly propagated to the native surface.
129 for (int i
= 0; i
< image
.height(); ++i
) {
130 for (int j
= 0; j
< image
.width(); ++j
) {
131 if (j
< kDefaultMode
.hdisplay
/ 2 && i
< kDefaultMode
.vdisplay
/ 2)
132 EXPECT_EQ(SK_ColorWHITE
, image
.getColor(j
, i
));
134 EXPECT_EQ(SK_ColorBLACK
, image
.getColor(j
, i
));
139 TEST_F(DrmSurfaceTest
, CheckSurfaceContentsAfter2QueuedPresents
) {
141 // Present an empty buffer but don't respond with the page flip event since we
142 // want to make sure the following presents will aggregate correctly.
143 surface_
->PresentCanvas(rect
);
146 paint
.setColor(SK_ColorWHITE
);
147 rect
.SetRect(0, 0, kDefaultMode
.hdisplay
/ 2, kDefaultMode
.vdisplay
/ 2);
148 surface_
->GetSurface()->getCanvas()->drawRect(RectToSkRect(rect
), paint
);
149 surface_
->PresentCanvas(rect
);
151 paint
.setColor(SK_ColorRED
);
152 rect
.SetRect(0, kDefaultMode
.vdisplay
/ 2, kDefaultMode
.hdisplay
/ 2,
153 kDefaultMode
.vdisplay
/ 2);
154 surface_
->GetSurface()->getCanvas()->drawRect(RectToSkRect(rect
), paint
);
155 surface_
->PresentCanvas(rect
);
157 drm_
->RunCallbacks();
160 std::vector
<skia::RefPtr
<SkSurface
>> framebuffers
;
161 for (const auto& buffer
: drm_
->buffers()) {
162 // Skip cursor buffers.
163 if (buffer
->width() == kDefaultCursorSize
&&
164 buffer
->height() == kDefaultCursorSize
)
167 framebuffers
.push_back(buffer
);
170 // Buffer 0 is the modesetting buffer, buffer 1 is the backbuffer and buffer
171 // 2 is the frontbuffer.
172 EXPECT_EQ(3u, framebuffers
.size());
174 image
.setInfo(framebuffers
[1]->getCanvas()->imageInfo());
175 EXPECT_TRUE(framebuffers
[1]->getCanvas()->readPixels(&image
, 0, 0));
177 EXPECT_EQ(kDefaultMode
.hdisplay
, image
.width());
178 EXPECT_EQ(kDefaultMode
.vdisplay
, image
.height());
180 // Make sure the updates are correctly propagated to the native surface.
181 for (int i
= 0; i
< image
.height(); ++i
) {
182 for (int j
= 0; j
< image
.width(); ++j
) {
183 if (j
< kDefaultMode
.hdisplay
/ 2 && i
< kDefaultMode
.vdisplay
/ 2)
184 EXPECT_EQ(SK_ColorWHITE
, image
.getColor(j
, i
));
185 else if (j
< kDefaultMode
.hdisplay
/ 2)
186 EXPECT_EQ(SK_ColorRED
, image
.getColor(j
, i
));
188 EXPECT_EQ(SK_ColorBLACK
, image
.getColor(j
, i
));