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 "third_party/skia/include/core/SkCanvas.h"
11 #include "ui/ozone/platform/dri/hardware_display_controller.h"
17 template<class Object> Object* DrmAllocator() {
18 return static_cast<Object*>(drmMalloc(sizeof(Object)));
23 MockDriWrapper::MockDriWrapper(int fd)
25 get_crtc_call_count_(0),
26 set_crtc_call_count_(0),
27 restore_crtc_call_count_(0),
28 add_framebuffer_call_count_(0),
29 remove_framebuffer_call_count_(0),
30 page_flip_call_count_(0),
31 overlay_flip_call_count_(0),
32 set_crtc_expectation_(true),
33 add_framebuffer_expectation_(true),
34 page_flip_expectation_(true),
35 create_dumb_buffer_expectation_(true),
36 current_framebuffer_(0) {
40 MockDriWrapper::~MockDriWrapper() {
44 ScopedDrmCrtcPtr MockDriWrapper::GetCrtc(uint32_t crtc_id) {
45 get_crtc_call_count_++;
46 return ScopedDrmCrtcPtr(DrmAllocator<drmModeCrtc>());
49 bool MockDriWrapper::SetCrtc(uint32_t crtc_id,
51 std::vector<uint32_t> connectors,
52 drmModeModeInfo* mode) {
53 current_framebuffer_ = framebuffer;
54 set_crtc_call_count_++;
55 return set_crtc_expectation_;
58 bool MockDriWrapper::SetCrtc(drmModeCrtc* crtc,
59 std::vector<uint32_t> connectors) {
60 restore_crtc_call_count_++;
64 ScopedDrmConnectorPtr MockDriWrapper::GetConnector(uint32_t connector_id) {
65 return ScopedDrmConnectorPtr(DrmAllocator<drmModeConnector>());
68 bool MockDriWrapper::AddFramebuffer(uint32_t width,
74 uint32_t* framebuffer) {
75 add_framebuffer_call_count_++;
76 *framebuffer = add_framebuffer_call_count_;
77 return add_framebuffer_expectation_;
80 bool MockDriWrapper::RemoveFramebuffer(uint32_t framebuffer) {
81 remove_framebuffer_call_count_++;
85 bool MockDriWrapper::PageFlip(uint32_t crtc_id,
88 page_flip_call_count_++;
89 current_framebuffer_ = framebuffer;
90 controllers_.push(static_cast<ui::HardwareDisplayController*>(data));
91 return page_flip_expectation_;
94 bool MockDriWrapper::PageFlipOverlay(uint32_t crtc_id,
96 const gfx::Rect& location,
97 const gfx::RectF& source,
99 overlay_flip_call_count_++;
103 ScopedDrmPropertyPtr MockDriWrapper::GetProperty(drmModeConnector* connector,
105 return ScopedDrmPropertyPtr(DrmAllocator<drmModePropertyRes>());
108 bool MockDriWrapper::SetProperty(uint32_t connector_id,
109 uint32_t property_id,
114 ScopedDrmPropertyBlobPtr MockDriWrapper::GetPropertyBlob(
115 drmModeConnector* connector,
117 return ScopedDrmPropertyBlobPtr(DrmAllocator<drmModePropertyBlobRes>());
120 bool MockDriWrapper::SetCursor(uint32_t crtc_id,
122 const gfx::Size& size) {
126 bool MockDriWrapper::MoveCursor(uint32_t crtc_id, const gfx::Point& point) {
130 void MockDriWrapper::HandleEvent(drmEventContext& event) {
131 CHECK(!controllers_.empty());
132 controllers_.front()->OnPageFlipEvent(0, 0, 0);
136 bool MockDriWrapper::CreateDumbBuffer(const SkImageInfo& info,
140 if (!create_dumb_buffer_expectation_)
144 *stride = info.minRowBytes();
145 *pixels = new char[info.getSafeSize(*stride)];
147 skia::AdoptRef(SkSurface::NewRasterDirect(info, *pixels, *stride)));
148 buffers_.back()->getCanvas()->clear(SK_ColorBLACK);
153 void MockDriWrapper::DestroyDumbBuffer(const SkImageInfo& info,
157 delete[] static_cast<char*>(pixels);