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 "ui/ozone/platform/drm/test/mock_drm_device.h"
8 #include <xf86drmMode.h>
10 #include "base/logging.h"
11 #include "third_party/skia/include/core/SkCanvas.h"
12 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_legacy.h"
18 template <class Object
>
19 Object
* DrmAllocator() {
20 return static_cast<Object
*>(drmMalloc(sizeof(Object
)));
23 class MockHardwareDisplayPlaneManager
24 : public HardwareDisplayPlaneManagerLegacy
{
26 MockHardwareDisplayPlaneManager(DrmDevice
* drm
,
27 std::vector
<uint32_t> crtcs
,
28 size_t planes_per_crtc
) {
29 const int kPlaneBaseId
= 50;
32 for (size_t crtc_idx
= 0; crtc_idx
< crtcs_
.size(); crtc_idx
++) {
33 for (size_t i
= 0; i
< planes_per_crtc
; i
++) {
35 new HardwareDisplayPlane(kPlaneBaseId
+ i
, 1 << crtc_idx
));
43 MockDrmDevice::MockDrmDevice()
44 : DrmDevice(base::FilePath(), base::File()),
45 get_crtc_call_count_(0),
46 set_crtc_call_count_(0),
47 restore_crtc_call_count_(0),
48 add_framebuffer_call_count_(0),
49 remove_framebuffer_call_count_(0),
50 page_flip_call_count_(0),
51 overlay_flip_call_count_(0),
52 overlay_clear_call_count_(0),
53 allocate_buffer_count_(0),
54 set_crtc_expectation_(true),
55 add_framebuffer_expectation_(true),
56 page_flip_expectation_(true),
57 create_dumb_buffer_expectation_(true),
58 use_sync_flips_(false),
59 current_framebuffer_(0) {
60 plane_manager_
.reset(new HardwareDisplayPlaneManagerLegacy());
63 MockDrmDevice::MockDrmDevice(bool use_sync_flips
,
64 std::vector
<uint32_t> crtcs
,
65 size_t planes_per_crtc
)
66 : DrmDevice(base::FilePath(), base::File()),
67 get_crtc_call_count_(0),
68 set_crtc_call_count_(0),
69 restore_crtc_call_count_(0),
70 add_framebuffer_call_count_(0),
71 remove_framebuffer_call_count_(0),
72 page_flip_call_count_(0),
73 overlay_flip_call_count_(0),
74 overlay_clear_call_count_(0),
75 allocate_buffer_count_(0),
76 set_crtc_expectation_(true),
77 add_framebuffer_expectation_(true),
78 page_flip_expectation_(true),
79 create_dumb_buffer_expectation_(true),
80 use_sync_flips_(use_sync_flips
),
81 current_framebuffer_(0) {
83 new MockHardwareDisplayPlaneManager(this, crtcs
, planes_per_crtc
));
86 MockDrmDevice::~MockDrmDevice() {
89 ScopedDrmCrtcPtr
MockDrmDevice::GetCrtc(uint32_t crtc_id
) {
90 get_crtc_call_count_
++;
91 return ScopedDrmCrtcPtr(DrmAllocator
<drmModeCrtc
>());
94 bool MockDrmDevice::SetCrtc(uint32_t crtc_id
,
96 std::vector
<uint32_t> connectors
,
97 drmModeModeInfo
* mode
) {
98 current_framebuffer_
= framebuffer
;
99 set_crtc_call_count_
++;
100 return set_crtc_expectation_
;
103 bool MockDrmDevice::SetCrtc(drmModeCrtc
* crtc
,
104 std::vector
<uint32_t> connectors
) {
105 restore_crtc_call_count_
++;
109 bool MockDrmDevice::DisableCrtc(uint32_t crtc_id
) {
110 current_framebuffer_
= 0;
114 ScopedDrmConnectorPtr
MockDrmDevice::GetConnector(uint32_t connector_id
) {
115 return ScopedDrmConnectorPtr(DrmAllocator
<drmModeConnector
>());
118 bool MockDrmDevice::AddFramebuffer(uint32_t width
,
124 uint32_t* framebuffer
) {
125 add_framebuffer_call_count_
++;
126 *framebuffer
= add_framebuffer_call_count_
;
127 return add_framebuffer_expectation_
;
130 bool MockDrmDevice::RemoveFramebuffer(uint32_t framebuffer
) {
131 remove_framebuffer_call_count_
++;
135 ScopedDrmFramebufferPtr
MockDrmDevice::GetFramebuffer(uint32_t framebuffer
) {
136 return ScopedDrmFramebufferPtr();
139 bool MockDrmDevice::PageFlip(uint32_t crtc_id
,
140 uint32_t framebuffer
,
142 const PageFlipCallback
& callback
) {
143 page_flip_call_count_
++;
144 current_framebuffer_
= framebuffer
;
145 if (page_flip_expectation_
) {
147 callback
.Run(0, 0, 0);
149 callbacks_
.push(callback
);
152 return page_flip_expectation_
;
155 bool MockDrmDevice::PageFlipOverlay(uint32_t crtc_id
,
156 uint32_t framebuffer
,
157 const gfx::Rect
& location
,
158 const gfx::Rect
& source
,
161 overlay_clear_call_count_
++;
162 overlay_flip_call_count_
++;
166 ScopedDrmPropertyPtr
MockDrmDevice::GetProperty(drmModeConnector
* connector
,
168 return ScopedDrmPropertyPtr(DrmAllocator
<drmModePropertyRes
>());
171 bool MockDrmDevice::SetProperty(uint32_t connector_id
,
172 uint32_t property_id
,
177 bool MockDrmDevice::GetCapability(uint64_t capability
, uint64_t* value
) {
181 ScopedDrmPropertyBlobPtr
MockDrmDevice::GetPropertyBlob(
182 drmModeConnector
* connector
,
184 return ScopedDrmPropertyBlobPtr(DrmAllocator
<drmModePropertyBlobRes
>());
187 bool MockDrmDevice::SetCursor(uint32_t crtc_id
,
189 const gfx::Size
& size
) {
190 crtc_cursor_map_
[crtc_id
] = handle
;
194 bool MockDrmDevice::MoveCursor(uint32_t crtc_id
, const gfx::Point
& point
) {
198 bool MockDrmDevice::CreateDumbBuffer(const SkImageInfo
& info
,
201 if (!create_dumb_buffer_expectation_
)
204 *handle
= allocate_buffer_count_
++;
205 *stride
= info
.minRowBytes();
206 void* pixels
= new char[info
.getSafeSize(*stride
)];
208 skia::AdoptRef(SkSurface::NewRasterDirect(info
, pixels
, *stride
)));
209 buffers_
[*handle
]->getCanvas()->clear(SK_ColorBLACK
);
214 bool MockDrmDevice::DestroyDumbBuffer(uint32_t handle
) {
215 if (handle
>= buffers_
.size() || !buffers_
[handle
])
218 buffers_
[handle
].clear();
222 bool MockDrmDevice::MapDumbBuffer(uint32_t handle
, size_t size
, void** pixels
) {
223 if (handle
>= buffers_
.size() || !buffers_
[handle
])
226 *pixels
= const_cast<void*>(buffers_
[handle
]->peekPixels(nullptr, nullptr));
230 bool MockDrmDevice::UnmapDumbBuffer(void* pixels
, size_t size
) {
234 bool MockDrmDevice::CloseBufferHandle(uint32_t handle
) {
238 bool MockDrmDevice::CommitProperties(drmModePropertySet
* properties
,
242 const PageFlipCallback
& callback
) {
246 bool MockDrmDevice::SetGammaRamp(uint32_t crtc_id
,
247 const std::vector
<GammaRampRGBEntry
>& lut
) {
251 bool MockDrmDevice::SetCapability(uint64_t capability
, uint64_t value
) {
255 void MockDrmDevice::RunCallbacks() {
256 while (!callbacks_
.empty()) {
257 PageFlipCallback callback
= callbacks_
.front();
259 callback
.Run(0, 0, 0);