Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / ozone / platform / drm / test / mock_drm_device.cc
blob548df3dd1bbadc0f13be3d4297fa2d26e4e6d24f
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>
8 #include <xf86drm.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"
15 namespace ui {
17 namespace {
19 template <class Object>
20 Object* DrmAllocator() {
21 return static_cast<Object*>(drmMalloc(sizeof(Object)));
24 class MockHardwareDisplayPlaneManager
25 : public HardwareDisplayPlaneManagerLegacy {
26 public:
27 MockHardwareDisplayPlaneManager(DrmDevice* drm,
28 std::vector<uint32_t> crtcs,
29 size_t planes_per_crtc) {
30 const int kPlaneBaseId = 50;
31 drm_ = drm;
32 crtcs_.swap(crtcs);
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),
39 false, true);
40 planes_.push_back(plane.Pass());
46 } // namespace
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) {
87 plane_manager_.reset(
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_++;
111 return true;
114 bool MockDrmDevice::DisableCrtc(uint32_t crtc_id) {
115 current_framebuffer_ = 0;
116 return true;
119 ScopedDrmConnectorPtr MockDrmDevice::GetConnector(uint32_t connector_id) {
120 return ScopedDrmConnectorPtr(DrmAllocator<drmModeConnector>());
123 bool MockDrmDevice::AddFramebuffer(uint32_t width,
124 uint32_t height,
125 uint8_t depth,
126 uint8_t bpp,
127 uint32_t stride,
128 uint32_t handle,
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_++;
137 return true;
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_) {
150 if (use_sync_flips_)
151 callback.Run(0, 0, 0);
152 else
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,
163 int overlay_plane) {
164 if (!framebuffer)
165 overlay_clear_call_count_++;
166 overlay_flip_call_count_++;
167 return true;
170 ScopedDrmPropertyPtr MockDrmDevice::GetProperty(drmModeConnector* connector,
171 const char* name) {
172 return ScopedDrmPropertyPtr(DrmAllocator<drmModePropertyRes>());
175 bool MockDrmDevice::SetProperty(uint32_t connector_id,
176 uint32_t property_id,
177 uint64_t value) {
178 return true;
181 bool MockDrmDevice::GetCapability(uint64_t capability, uint64_t* value) {
182 return true;
185 ScopedDrmPropertyBlobPtr MockDrmDevice::GetPropertyBlob(
186 drmModeConnector* connector,
187 const char* name) {
188 return ScopedDrmPropertyBlobPtr(DrmAllocator<drmModePropertyBlobRes>());
191 bool MockDrmDevice::SetCursor(uint32_t crtc_id,
192 uint32_t handle,
193 const gfx::Size& size) {
194 crtc_cursor_map_[crtc_id] = handle;
195 return true;
198 bool MockDrmDevice::MoveCursor(uint32_t crtc_id, const gfx::Point& point) {
199 return true;
202 bool MockDrmDevice::CreateDumbBuffer(const SkImageInfo& info,
203 uint32_t* handle,
204 uint32_t* stride) {
205 if (!create_dumb_buffer_expectation_)
206 return false;
208 *handle = allocate_buffer_count_++;
209 *stride = info.minRowBytes();
210 void* pixels = new char[info.getSafeSize(*stride)];
211 buffers_.push_back(
212 skia::AdoptRef(SkSurface::NewRasterDirect(info, pixels, *stride)));
213 buffers_[*handle]->getCanvas()->clear(SK_ColorBLACK);
215 return true;
218 bool MockDrmDevice::DestroyDumbBuffer(uint32_t handle) {
219 if (handle >= buffers_.size() || !buffers_[handle])
220 return false;
222 buffers_[handle].clear();
223 return true;
226 bool MockDrmDevice::MapDumbBuffer(uint32_t handle, size_t size, void** pixels) {
227 if (handle >= buffers_.size() || !buffers_[handle])
228 return false;
230 *pixels = const_cast<void*>(buffers_[handle]->peekPixels(nullptr, nullptr));
231 return true;
234 bool MockDrmDevice::UnmapDumbBuffer(void* pixels, size_t size) {
235 return true;
238 bool MockDrmDevice::CloseBufferHandle(uint32_t handle) {
239 return true;
242 bool MockDrmDevice::CommitProperties(drmModeAtomicReq* properties,
243 uint32_t flags,
244 uint32_t crtc_count,
245 const PageFlipCallback& callback) {
246 return false;
249 bool MockDrmDevice::SetGammaRamp(uint32_t crtc_id,
250 const std::vector<GammaRampRGBEntry>& lut) {
251 return true;
254 bool MockDrmDevice::SetCapability(uint64_t capability, uint64_t value) {
255 return false;
258 void MockDrmDevice::RunCallbacks() {
259 while (!callbacks_.empty()) {
260 PageFlipCallback callback = callbacks_.front();
261 callbacks_.pop();
262 callback.Run(0, 0, 0);
266 } // namespace ui