Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / ozone / platform / dri / dri_wrapper.cc
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.
4
5 #include "ui/ozone/platform/dri/dri_wrapper.h"
6
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <xf86drmMode.h>
10
11 #include "base/logging.h"
12
13 namespace ui {
14
15 DriWrapper::DriWrapper(const char* device_path) {
16   fd_ = open(device_path, O_RDWR | O_CLOEXEC);
17 }
18
19 DriWrapper::~DriWrapper() {
20   if (fd_ >= 0)
21     close(fd_);
22 }
23
24 drmModeCrtc* DriWrapper::GetCrtc(uint32_t crtc_id) {
25   CHECK(fd_ >= 0);
26   return drmModeGetCrtc(fd_, crtc_id);
27 }
28
29 void DriWrapper::FreeCrtc(drmModeCrtc* crtc) {
30   drmModeFreeCrtc(crtc);
31 }
32
33 bool 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
41 bool 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
53 bool DriWrapper::DisableCrtc(uint32_t crtc_id) {
54   CHECK(fd_ >= 0);
55   return !drmModeSetCrtc(fd_, crtc_id, 0, 0, 0, NULL, 0, NULL);
56 }
57
58 bool DriWrapper::AddFramebuffer(const drmModeModeInfo& mode,
59                                      uint8_t depth,
60                                      uint8_t bpp,
61                                      uint32_t stride,
62                                      uint32_t handle,
63                                      uint32_t* framebuffer) {
64   CHECK(fd_ >= 0);
65   return !drmModeAddFB(fd_,
66                        mode.hdisplay,
67                        mode.vdisplay,
68                        depth,
69                        bpp,
70                        stride,
71                        handle,
72                        framebuffer);
73 }
74
75 bool DriWrapper::RemoveFramebuffer(uint32_t framebuffer) {
76   CHECK(fd_ >= 0);
77   return !drmModeRmFB(fd_, framebuffer);
78 }
79
80 bool DriWrapper::PageFlip(uint32_t crtc_id,
81                                uint32_t framebuffer,
82                                void* data) {
83   CHECK(fd_ >= 0);
84   return !drmModePageFlip(fd_,
85                           crtc_id,
86                           framebuffer,
87                           DRM_MODE_PAGE_FLIP_EVENT,
88                           data);
89 }
90
91 drmModePropertyRes* DriWrapper::GetProperty(drmModeConnector* connector,
92                                             const char* name) {
93   for (int i = 0; i < connector->count_props; ++i) {
94     drmModePropertyRes* property = drmModeGetProperty(fd_, connector->props[i]);
95     if (!property)
96       continue;
97
98     if (strcmp(property->name, name) == 0)
99       return property;
100
101     drmModeFreeProperty(property);
102   }
103
104   return NULL;
105 }
106
107 bool DriWrapper::SetProperty(uint32_t connector_id,
108                              uint32_t property_id,
109                              uint64_t value) {
110   CHECK(fd_ >= 0);
111   return !drmModeConnectorSetProperty(fd_, connector_id, property_id, value);
112 }
113
114 void DriWrapper::FreeProperty(drmModePropertyRes* prop) {
115   drmModeFreeProperty(prop);
116 }
117
118 drmModePropertyBlobRes* DriWrapper::GetPropertyBlob(drmModeConnector* connector,
119                                                     const char* name) {
120   CHECK(fd_ >= 0);
121   for (int i = 0; i < connector->count_props; ++i) {
122     drmModePropertyRes* property = drmModeGetProperty(fd_, connector->props[i]);
123     if (!property)
124       continue;
125
126     if (strcmp(property->name, name) == 0 &&
127         property->flags & DRM_MODE_PROP_BLOB) {
128       drmModePropertyBlobRes* blob =
129           drmModeGetPropertyBlob(fd_, connector->prop_values[i]);
130       drmModeFreeProperty(property);
131       return blob;
132     }
133
134     drmModeFreeProperty(property);
135   }
136
137   return NULL;
138 }
139
140 void DriWrapper::FreePropertyBlob(drmModePropertyBlobRes* blob) {
141   drmModeFreePropertyBlob(blob);
142 }
143
144 bool DriWrapper::SetCursor(uint32_t crtc_id,
145                            uint32_t handle,
146                            uint32_t width,
147                            uint32_t height) {
148   CHECK(fd_ >= 0);
149   return !drmModeSetCursor(fd_, crtc_id, handle, width, height);
150 }
151
152 bool DriWrapper::MoveCursor(uint32_t crtc_id, int x, int y) {
153   CHECK(fd_ >= 0);
154   return !drmModeMoveCursor(fd_, crtc_id, x, y);
155 }
156
157 }  // namespace ui