1// Copyright 2013 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.
4
5#include "ui/gfx/ozone/dri/dri_wrapper.h"
6
7#include <fcntl.h>
8#include <unistd.h>
9#include <xf86drmMode.h>
10
11#include "base/logging.h"
12
13namespace gfx {
14
15DriWrapper::DriWrapper(const char* device_path) {
16  fd_ = open(device_path, O_RDWR | O_CLOEXEC);
17}
18
19DriWrapper::~DriWrapper() {
20  if (fd_ >= 0)
21    close(fd_);
22}
23
24drmModeCrtc* DriWrapper::GetCrtc(uint32_t crtc_id) {
25  CHECK(fd_ >= 0);
26  return drmModeGetCrtc(fd_, crtc_id);
27}
28
29void DriWrapper::FreeCrtc(drmModeCrtc* crtc) {
30  drmModeFreeCrtc(crtc);
31}
32
33bool DriWrapper::SetCrtc(uint32_t crtc_id,
34                              uint32_t framebuffer,
35                              uint32_t* connectors,
36                              drmModeModeInfo* mode) {
37  CHECK(fd_ >= 0);
38  return !drmModeSetCrtc(fd_, crtc_id, framebuffer, 0, 0, connectors, 1, mode);
39}
40
41bool DriWrapper::SetCrtc(drmModeCrtc* crtc, uint32_t* connectors) {
42  CHECK(fd_ >= 0);
43  return !drmModeSetCrtc(fd_,
44                         crtc->crtc_id,
45                         crtc->buffer_id,
46                         crtc->x,
47                         crtc->y,
48                         connectors,
49                         1,
50                         &crtc->mode);
51}
52
53bool DriWrapper::AddFramebuffer(const drmModeModeInfo& mode,
54                                     uint8_t depth,
55                                     uint8_t bpp,
56                                     uint32_t stride,
57                                     uint32_t handle,
58                                     uint32_t* framebuffer) {
59  CHECK(fd_ >= 0);
60  return !drmModeAddFB(fd_,
61                       mode.hdisplay,
62                       mode.vdisplay,
63                       depth,
64                       bpp,
65                       stride,
66                       handle,
67                       framebuffer);
68}
69
70bool DriWrapper::RemoveFramebuffer(uint32_t framebuffer) {
71  CHECK(fd_ >= 0);
72  return !drmModeRmFB(fd_, framebuffer);
73}
74
75bool DriWrapper::PageFlip(uint32_t crtc_id,
76                               uint32_t framebuffer,
77                               void* data) {
78  CHECK(fd_ >= 0);
79  return !drmModePageFlip(fd_,
80                          crtc_id,
81                          framebuffer,
82                          DRM_MODE_PAGE_FLIP_EVENT,
83                          data);
84}
85
86bool DriWrapper::ConnectorSetProperty(uint32_t connector_id,
87                                           uint32_t property_id,
88                                           uint64_t value) {
89  CHECK(fd_ >= 0);
90  return !drmModeConnectorSetProperty(fd_, connector_id, property_id, value);
91}
92
93}  // namespace gfx
94