Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / ozone / platform / drm / test / mock_drm_device.cc
blob0692f36baaa24cad907f4ed7cf56e59c5f8b12cc
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 <xf86drm.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"
14 namespace ui {
16 namespace {
18 template <class Object>
19 Object* DrmAllocator() {
20 return static_cast<Object*>(drmMalloc(sizeof(Object)));
23 class MockHardwareDisplayPlaneManager
24 : public HardwareDisplayPlaneManagerLegacy {
25 public:
26 MockHardwareDisplayPlaneManager(DrmDevice* drm,
27 std::vector<uint32_t> crtcs,
28 size_t planes_per_crtc) {
29 const int kPlaneBaseId = 50;
30 drm_ = drm;
31 crtcs_.swap(crtcs);
32 for (size_t crtc_idx = 0; crtc_idx < crtcs_.size(); crtc_idx++) {
33 for (size_t i = 0; i < planes_per_crtc; i++) {
34 planes_.push_back(
35 new HardwareDisplayPlane(kPlaneBaseId + i, 1 << crtc_idx));
41 } // namespace
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) {
82 plane_manager_.reset(
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,
95 uint32_t framebuffer,
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_++;
106 return true;
109 bool MockDrmDevice::DisableCrtc(uint32_t crtc_id) {
110 current_framebuffer_ = 0;
111 return true;
114 ScopedDrmConnectorPtr MockDrmDevice::GetConnector(uint32_t connector_id) {
115 return ScopedDrmConnectorPtr(DrmAllocator<drmModeConnector>());
118 bool MockDrmDevice::AddFramebuffer(uint32_t width,
119 uint32_t height,
120 uint8_t depth,
121 uint8_t bpp,
122 uint32_t stride,
123 uint32_t handle,
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_++;
132 return true;
135 ScopedDrmFramebufferPtr MockDrmDevice::GetFramebuffer(uint32_t framebuffer) {
136 return ScopedDrmFramebufferPtr();
139 bool MockDrmDevice::PageFlip(uint32_t crtc_id,
140 uint32_t framebuffer,
141 bool is_sync,
142 const PageFlipCallback& callback) {
143 page_flip_call_count_++;
144 current_framebuffer_ = framebuffer;
145 if (page_flip_expectation_) {
146 if (use_sync_flips_)
147 callback.Run(0, 0, 0);
148 else
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,
159 int overlay_plane) {
160 if (!framebuffer)
161 overlay_clear_call_count_++;
162 overlay_flip_call_count_++;
163 return true;
166 ScopedDrmPropertyPtr MockDrmDevice::GetProperty(drmModeConnector* connector,
167 const char* name) {
168 return ScopedDrmPropertyPtr(DrmAllocator<drmModePropertyRes>());
171 bool MockDrmDevice::SetProperty(uint32_t connector_id,
172 uint32_t property_id,
173 uint64_t value) {
174 return true;
177 bool MockDrmDevice::GetCapability(uint64_t capability, uint64_t* value) {
178 return true;
181 ScopedDrmPropertyBlobPtr MockDrmDevice::GetPropertyBlob(
182 drmModeConnector* connector,
183 const char* name) {
184 return ScopedDrmPropertyBlobPtr(DrmAllocator<drmModePropertyBlobRes>());
187 bool MockDrmDevice::SetCursor(uint32_t crtc_id,
188 uint32_t handle,
189 const gfx::Size& size) {
190 crtc_cursor_map_[crtc_id] = handle;
191 return true;
194 bool MockDrmDevice::MoveCursor(uint32_t crtc_id, const gfx::Point& point) {
195 return true;
198 bool MockDrmDevice::CreateDumbBuffer(const SkImageInfo& info,
199 uint32_t* handle,
200 uint32_t* stride) {
201 if (!create_dumb_buffer_expectation_)
202 return false;
204 *handle = allocate_buffer_count_++;
205 *stride = info.minRowBytes();
206 void* pixels = new char[info.getSafeSize(*stride)];
207 buffers_.push_back(
208 skia::AdoptRef(SkSurface::NewRasterDirect(info, pixels, *stride)));
209 buffers_[*handle]->getCanvas()->clear(SK_ColorBLACK);
211 return true;
214 bool MockDrmDevice::DestroyDumbBuffer(uint32_t handle) {
215 if (handle >= buffers_.size() || !buffers_[handle])
216 return false;
218 buffers_[handle].clear();
219 return true;
222 bool MockDrmDevice::MapDumbBuffer(uint32_t handle, size_t size, void** pixels) {
223 if (handle >= buffers_.size() || !buffers_[handle])
224 return false;
226 *pixels = const_cast<void*>(buffers_[handle]->peekPixels(nullptr, nullptr));
227 return true;
230 bool MockDrmDevice::UnmapDumbBuffer(void* pixels, size_t size) {
231 return true;
234 bool MockDrmDevice::CloseBufferHandle(uint32_t handle) {
235 return true;
238 bool MockDrmDevice::CommitProperties(drmModePropertySet* properties,
239 uint32_t flags,
240 bool is_sync,
241 bool test_only,
242 const PageFlipCallback& callback) {
243 return false;
246 bool MockDrmDevice::SetGammaRamp(uint32_t crtc_id,
247 const std::vector<GammaRampRGBEntry>& lut) {
248 return true;
251 bool MockDrmDevice::SetCapability(uint64_t capability, uint64_t value) {
252 return false;
255 void MockDrmDevice::RunCallbacks() {
256 while (!callbacks_.empty()) {
257 PageFlipCallback callback = callbacks_.front();
258 callbacks_.pop();
259 callback.Run(0, 0, 0);
263 } // namespace ui