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/dri/test/mock_dri_wrapper.h"
8 #include <xf86drmMode.h>
10 #include "base/logging.h"
11 #include "third_party/skia/include/core/SkCanvas.h"
12 #include "ui/ozone/platform/dri/crtc_controller.h"
18 template<class Object> Object* DrmAllocator() {
19 return static_cast<Object*>(drmMalloc(sizeof(Object)));
24 MockDriWrapper::MockDriWrapper(int fd)
26 get_crtc_call_count_(0),
27 set_crtc_call_count_(0),
28 restore_crtc_call_count_(0),
29 add_framebuffer_call_count_(0),
30 remove_framebuffer_call_count_(0),
31 page_flip_call_count_(0),
32 overlay_flip_call_count_(0),
33 handle_events_count_(0),
34 set_crtc_expectation_(true),
35 add_framebuffer_expectation_(true),
36 page_flip_expectation_(true),
37 create_dumb_buffer_expectation_(true),
38 current_framebuffer_(0) {
42 MockDriWrapper::~MockDriWrapper() {
46 ScopedDrmCrtcPtr MockDriWrapper::GetCrtc(uint32_t crtc_id) {
47 get_crtc_call_count_++;
48 return ScopedDrmCrtcPtr(DrmAllocator<drmModeCrtc>());
51 bool MockDriWrapper::SetCrtc(uint32_t crtc_id,
53 std::vector<uint32_t> connectors,
54 drmModeModeInfo* mode) {
55 current_framebuffer_ = framebuffer;
56 set_crtc_call_count_++;
57 return set_crtc_expectation_;
60 bool MockDriWrapper::SetCrtc(drmModeCrtc* crtc,
61 std::vector<uint32_t> connectors) {
62 restore_crtc_call_count_++;
66 ScopedDrmConnectorPtr MockDriWrapper::GetConnector(uint32_t connector_id) {
67 return ScopedDrmConnectorPtr(DrmAllocator<drmModeConnector>());
70 bool MockDriWrapper::AddFramebuffer(uint32_t width,
76 uint32_t* framebuffer) {
77 add_framebuffer_call_count_++;
78 *framebuffer = add_framebuffer_call_count_;
79 return add_framebuffer_expectation_;
82 bool MockDriWrapper::RemoveFramebuffer(uint32_t framebuffer) {
83 remove_framebuffer_call_count_++;
87 bool MockDriWrapper::PageFlip(uint32_t crtc_id,
90 page_flip_call_count_++;
91 current_framebuffer_ = framebuffer;
92 controllers_.push(static_cast<ui::CrtcController*>(data));
93 return page_flip_expectation_;
96 bool MockDriWrapper::PageFlipOverlay(uint32_t crtc_id,
98 const gfx::Rect& location,
99 const gfx::RectF& source,
101 overlay_flip_call_count_++;
105 ScopedDrmPropertyPtr MockDriWrapper::GetProperty(drmModeConnector* connector,
107 return ScopedDrmPropertyPtr(DrmAllocator<drmModePropertyRes>());
110 bool MockDriWrapper::SetProperty(uint32_t connector_id,
111 uint32_t property_id,
116 bool MockDriWrapper::GetCapability(uint64_t capability, uint64_t* value) {
120 ScopedDrmPropertyBlobPtr MockDriWrapper::GetPropertyBlob(
121 drmModeConnector* connector,
123 return ScopedDrmPropertyBlobPtr(DrmAllocator<drmModePropertyBlobRes>());
126 bool MockDriWrapper::SetCursor(uint32_t crtc_id,
128 const gfx::Size& size) {
132 bool MockDriWrapper::MoveCursor(uint32_t crtc_id, const gfx::Point& point) {
136 void MockDriWrapper::HandleEvent(drmEventContext& event) {
137 CHECK(!controllers_.empty());
138 controllers_.front()->OnPageFlipEvent(0, 0, 0);
140 handle_events_count_++;
143 bool MockDriWrapper::CreateDumbBuffer(const SkImageInfo& info,
147 if (!create_dumb_buffer_expectation_)
151 *stride = info.minRowBytes();
152 *pixels = new char[info.getSafeSize(*stride)];
154 skia::AdoptRef(SkSurface::NewRasterDirect(info, *pixels, *stride)));
155 buffers_.back()->getCanvas()->clear(SK_ColorBLACK);
160 void MockDriWrapper::DestroyDumbBuffer(const SkImageInfo& info,
164 delete[] static_cast<char*>(pixels);