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"
7 #include <drm_fourcc.h>
9 #include <xf86drmMode.h>
11 #include "base/logging.h"
12 #include "third_party/skia/include/core/SkCanvas.h"
13 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_legacy.h"
19 template <class Object
>
20 Object
* DrmAllocator() {
21 return static_cast<Object
*>(drmMalloc(sizeof(Object
)));
24 class MockHardwareDisplayPlaneManager
25 : public HardwareDisplayPlaneManagerLegacy
{
27 MockHardwareDisplayPlaneManager(DrmDevice
* drm
,
28 std::vector
<uint32_t> crtcs
,
29 size_t planes_per_crtc
) {
30 const int kPlaneBaseId
= 50;
33 for (size_t crtc_idx
= 0; crtc_idx
< crtcs_
.size(); crtc_idx
++) {
34 for (size_t i
= 0; i
< planes_per_crtc
; i
++) {
35 scoped_ptr
<HardwareDisplayPlane
> plane(
36 new HardwareDisplayPlane(kPlaneBaseId
+ i
, 1 << crtc_idx
));
37 // Add support to test more formats.
38 plane
->Initialize(drm
, std::vector
<uint32_t>(1, DRM_FORMAT_XRGB8888
),
40 planes_
.push_back(plane
.Pass());
48 MockDrmDevice::MockDrmDevice()
49 : DrmDevice(base::FilePath(), base::File()),
50 get_crtc_call_count_(0),
51 set_crtc_call_count_(0),
52 restore_crtc_call_count_(0),
53 add_framebuffer_call_count_(0),
54 remove_framebuffer_call_count_(0),
55 page_flip_call_count_(0),
56 overlay_flip_call_count_(0),
57 overlay_clear_call_count_(0),
58 allocate_buffer_count_(0),
59 set_crtc_expectation_(true),
60 add_framebuffer_expectation_(true),
61 page_flip_expectation_(true),
62 create_dumb_buffer_expectation_(true),
63 use_sync_flips_(false),
64 current_framebuffer_(0) {
65 plane_manager_
.reset(new HardwareDisplayPlaneManagerLegacy());
68 MockDrmDevice::MockDrmDevice(bool use_sync_flips
,
69 std::vector
<uint32_t> crtcs
,
70 size_t planes_per_crtc
)
71 : DrmDevice(base::FilePath(), base::File()),
72 get_crtc_call_count_(0),
73 set_crtc_call_count_(0),
74 restore_crtc_call_count_(0),
75 add_framebuffer_call_count_(0),
76 remove_framebuffer_call_count_(0),
77 page_flip_call_count_(0),
78 overlay_flip_call_count_(0),
79 overlay_clear_call_count_(0),
80 allocate_buffer_count_(0),
81 set_crtc_expectation_(true),
82 add_framebuffer_expectation_(true),
83 page_flip_expectation_(true),
84 create_dumb_buffer_expectation_(true),
85 use_sync_flips_(use_sync_flips
),
86 current_framebuffer_(0) {
88 new MockHardwareDisplayPlaneManager(this, crtcs
, planes_per_crtc
));
91 MockDrmDevice::~MockDrmDevice() {
94 ScopedDrmCrtcPtr
MockDrmDevice::GetCrtc(uint32_t crtc_id
) {
95 get_crtc_call_count_
++;
96 return ScopedDrmCrtcPtr(DrmAllocator
<drmModeCrtc
>());
99 bool MockDrmDevice::SetCrtc(uint32_t crtc_id
,
100 uint32_t framebuffer
,
101 std::vector
<uint32_t> connectors
,
102 drmModeModeInfo
* mode
) {
103 current_framebuffer_
= framebuffer
;
104 set_crtc_call_count_
++;
105 return set_crtc_expectation_
;
108 bool MockDrmDevice::SetCrtc(drmModeCrtc
* crtc
,
109 std::vector
<uint32_t> connectors
) {
110 restore_crtc_call_count_
++;
114 bool MockDrmDevice::DisableCrtc(uint32_t crtc_id
) {
115 current_framebuffer_
= 0;
119 ScopedDrmConnectorPtr
MockDrmDevice::GetConnector(uint32_t connector_id
) {
120 return ScopedDrmConnectorPtr(DrmAllocator
<drmModeConnector
>());
123 bool MockDrmDevice::AddFramebuffer(uint32_t width
,
129 uint32_t* framebuffer
) {
130 add_framebuffer_call_count_
++;
131 *framebuffer
= add_framebuffer_call_count_
;
132 return add_framebuffer_expectation_
;
135 bool MockDrmDevice::RemoveFramebuffer(uint32_t framebuffer
) {
136 remove_framebuffer_call_count_
++;
140 ScopedDrmFramebufferPtr
MockDrmDevice::GetFramebuffer(uint32_t framebuffer
) {
141 return ScopedDrmFramebufferPtr();
144 bool MockDrmDevice::PageFlip(uint32_t crtc_id
,
145 uint32_t framebuffer
,
146 const PageFlipCallback
& callback
) {
147 page_flip_call_count_
++;
148 current_framebuffer_
= framebuffer
;
149 if (page_flip_expectation_
) {
151 callback
.Run(0, 0, 0);
153 callbacks_
.push(callback
);
156 return page_flip_expectation_
;
159 bool MockDrmDevice::PageFlipOverlay(uint32_t crtc_id
,
160 uint32_t framebuffer
,
161 const gfx::Rect
& location
,
162 const gfx::Rect
& source
,
165 overlay_clear_call_count_
++;
166 overlay_flip_call_count_
++;
170 ScopedDrmPropertyPtr
MockDrmDevice::GetProperty(drmModeConnector
* connector
,
172 return ScopedDrmPropertyPtr(DrmAllocator
<drmModePropertyRes
>());
175 bool MockDrmDevice::SetProperty(uint32_t connector_id
,
176 uint32_t property_id
,
181 bool MockDrmDevice::GetCapability(uint64_t capability
, uint64_t* value
) {
185 ScopedDrmPropertyBlobPtr
MockDrmDevice::GetPropertyBlob(
186 drmModeConnector
* connector
,
188 return ScopedDrmPropertyBlobPtr(DrmAllocator
<drmModePropertyBlobRes
>());
191 bool MockDrmDevice::SetCursor(uint32_t crtc_id
,
193 const gfx::Size
& size
) {
194 crtc_cursor_map_
[crtc_id
] = handle
;
198 bool MockDrmDevice::MoveCursor(uint32_t crtc_id
, const gfx::Point
& point
) {
202 bool MockDrmDevice::CreateDumbBuffer(const SkImageInfo
& info
,
205 if (!create_dumb_buffer_expectation_
)
208 *handle
= allocate_buffer_count_
++;
209 *stride
= info
.minRowBytes();
210 void* pixels
= new char[info
.getSafeSize(*stride
)];
212 skia::AdoptRef(SkSurface::NewRasterDirect(info
, pixels
, *stride
)));
213 buffers_
[*handle
]->getCanvas()->clear(SK_ColorBLACK
);
218 bool MockDrmDevice::DestroyDumbBuffer(uint32_t handle
) {
219 if (handle
>= buffers_
.size() || !buffers_
[handle
])
222 buffers_
[handle
].clear();
226 bool MockDrmDevice::MapDumbBuffer(uint32_t handle
, size_t size
, void** pixels
) {
227 if (handle
>= buffers_
.size() || !buffers_
[handle
])
230 *pixels
= const_cast<void*>(buffers_
[handle
]->peekPixels(nullptr, nullptr));
234 bool MockDrmDevice::UnmapDumbBuffer(void* pixels
, size_t size
) {
238 bool MockDrmDevice::CloseBufferHandle(uint32_t handle
) {
242 bool MockDrmDevice::CommitProperties(drmModeAtomicReq
* properties
,
245 const PageFlipCallback
& callback
) {
249 bool MockDrmDevice::SetGammaRamp(uint32_t crtc_id
,
250 const std::vector
<GammaRampRGBEntry
>& lut
) {
254 bool MockDrmDevice::SetCapability(uint64_t capability
, uint64_t value
) {
258 void MockDrmDevice::RunCallbacks() {
259 while (!callbacks_
.empty()) {
260 PageFlipCallback callback
= callbacks_
.front();
262 callback
.Run(0, 0, 0);