blob: e7d4b38ef63ce81f3ec87c1a6a1375fe01e6f7f8 [file] [log] [blame]
Stéphane Marchesin1a92c442015-06-29 20:05:48 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao557fa1f2017-02-07 12:51:00 -080017#include "graphics_drm.h"
18
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070019#include <fcntl.h>
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070020#include <stdio.h>
21#include <stdlib.h>
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070022#include <sys/mman.h>
23#include <sys/types.h>
24#include <unistd.h>
Tao Bao76be34c2017-02-07 13:30:19 -080025
26#include <drm_fourcc.h>
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070027#include <xf86drm.h>
28#include <xf86drmMode.h>
29
Tao Bao0ecbd762017-01-16 21:16:58 -080030#include "minui/minui.h"
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070031
32#define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A)))
33
Tao Bao557fa1f2017-02-07 12:51:00 -080034MinuiBackendDrm::MinuiBackendDrm()
35 : GRSurfaceDrms(), main_monitor_crtc(nullptr), main_monitor_connector(nullptr), drm_fd(-1) {}
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070036
Tao Bao557fa1f2017-02-07 12:51:00 -080037void MinuiBackendDrm::DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc) {
Tao Bao76be34c2017-02-07 13:30:19 -080038 if (crtc) {
39 drmModeSetCrtc(drm_fd, crtc->crtc_id,
40 0, // fb_id
41 0, 0, // x,y
42 nullptr, // connectors
43 0, // connector_count
44 nullptr); // mode
45 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070046}
47
Tao Bao557fa1f2017-02-07 12:51:00 -080048void MinuiBackendDrm::DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc, GRSurfaceDrm* surface) {
Tao Bao76be34c2017-02-07 13:30:19 -080049 int32_t ret = drmModeSetCrtc(drm_fd, crtc->crtc_id, surface->fb_id, 0, 0, // x,y
50 &main_monitor_connector->connector_id,
51 1, // connector_count
52 &main_monitor_crtc->mode);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070053
Tao Bao76be34c2017-02-07 13:30:19 -080054 if (ret) {
55 printf("drmModeSetCrtc failed ret=%d\n", ret);
56 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070057}
58
Tao Bao557fa1f2017-02-07 12:51:00 -080059void MinuiBackendDrm::Blank(bool blank) {
Tao Bao76be34c2017-02-07 13:30:19 -080060 if (blank) {
Tao Bao557fa1f2017-02-07 12:51:00 -080061 DrmDisableCrtc(drm_fd, main_monitor_crtc);
Tao Bao76be34c2017-02-07 13:30:19 -080062 } else {
Tao Bao557fa1f2017-02-07 12:51:00 -080063 DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[current_buffer]);
Tao Bao76be34c2017-02-07 13:30:19 -080064 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070065}
66
Tao Bao557fa1f2017-02-07 12:51:00 -080067void MinuiBackendDrm::DrmDestroySurface(GRSurfaceDrm* surface) {
Tao Bao76be34c2017-02-07 13:30:19 -080068 if (!surface) return;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070069
Tao Bao557fa1f2017-02-07 12:51:00 -080070 if (surface->data) {
71 munmap(surface->data, surface->row_bytes * surface->height);
Tao Bao76be34c2017-02-07 13:30:19 -080072 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070073
Tao Bao76be34c2017-02-07 13:30:19 -080074 if (surface->fb_id) {
75 int ret = drmModeRmFB(drm_fd, surface->fb_id);
76 if (ret) {
77 printf("drmModeRmFB failed ret=%d\n", ret);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070078 }
Tao Bao76be34c2017-02-07 13:30:19 -080079 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070080
Tao Bao76be34c2017-02-07 13:30:19 -080081 if (surface->handle) {
82 drm_gem_close gem_close = {};
83 gem_close.handle = surface->handle;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070084
Tao Bao76be34c2017-02-07 13:30:19 -080085 int ret = drmIoctl(drm_fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
86 if (ret) {
87 printf("DRM_IOCTL_GEM_CLOSE failed ret=%d\n", ret);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070088 }
Tao Bao76be34c2017-02-07 13:30:19 -080089 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070090
Tao Bao557fa1f2017-02-07 12:51:00 -080091 delete surface;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070092}
93
94static int drm_format_to_bpp(uint32_t format) {
Tao Bao76be34c2017-02-07 13:30:19 -080095 switch (format) {
96 case DRM_FORMAT_ABGR8888:
97 case DRM_FORMAT_BGRA8888:
98 case DRM_FORMAT_RGBX8888:
99 case DRM_FORMAT_BGRX8888:
100 case DRM_FORMAT_XBGR8888:
101 case DRM_FORMAT_XRGB8888:
102 return 32;
103 case DRM_FORMAT_RGB565:
104 return 16;
105 default:
106 printf("Unknown format %d\n", format);
107 return 32;
108 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700109}
110
Tao Bao557fa1f2017-02-07 12:51:00 -0800111GRSurfaceDrm* MinuiBackendDrm::DrmCreateSurface(int width, int height) {
112 GRSurfaceDrm* surface = new GRSurfaceDrm;
113 *surface = {};
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700114
Tao Bao76be34c2017-02-07 13:30:19 -0800115 uint32_t format;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700116#if defined(RECOVERY_ABGR)
Tao Bao76be34c2017-02-07 13:30:19 -0800117 format = DRM_FORMAT_RGBA8888;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700118#elif defined(RECOVERY_BGRA)
Tao Bao76be34c2017-02-07 13:30:19 -0800119 format = DRM_FORMAT_ARGB8888;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700120#elif defined(RECOVERY_RGBX)
Tao Bao76be34c2017-02-07 13:30:19 -0800121 format = DRM_FORMAT_XBGR8888;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700122#else
Tao Bao76be34c2017-02-07 13:30:19 -0800123 format = DRM_FORMAT_RGB565;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700124#endif
125
Tao Bao76be34c2017-02-07 13:30:19 -0800126 drm_mode_create_dumb create_dumb = {};
127 create_dumb.height = height;
128 create_dumb.width = width;
129 create_dumb.bpp = drm_format_to_bpp(format);
130 create_dumb.flags = 0;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700131
Tao Bao76be34c2017-02-07 13:30:19 -0800132 int ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
133 if (ret) {
134 printf("DRM_IOCTL_MODE_CREATE_DUMB failed ret=%d\n", ret);
Tao Bao557fa1f2017-02-07 12:51:00 -0800135 DrmDestroySurface(surface);
Tao Bao76be34c2017-02-07 13:30:19 -0800136 return nullptr;
137 }
138 surface->handle = create_dumb.handle;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700139
Tao Bao76be34c2017-02-07 13:30:19 -0800140 uint32_t handles[4], pitches[4], offsets[4];
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700141
Tao Bao76be34c2017-02-07 13:30:19 -0800142 handles[0] = surface->handle;
143 pitches[0] = create_dumb.pitch;
144 offsets[0] = 0;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700145
Tao Bao76be34c2017-02-07 13:30:19 -0800146 ret =
147 drmModeAddFB2(drm_fd, width, height, format, handles, pitches, offsets, &(surface->fb_id), 0);
148 if (ret) {
149 printf("drmModeAddFB2 failed ret=%d\n", ret);
Tao Bao557fa1f2017-02-07 12:51:00 -0800150 DrmDestroySurface(surface);
Tao Bao76be34c2017-02-07 13:30:19 -0800151 return nullptr;
152 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700153
Tao Bao76be34c2017-02-07 13:30:19 -0800154 drm_mode_map_dumb map_dumb = {};
155 map_dumb.handle = create_dumb.handle;
156 ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
157 if (ret) {
158 printf("DRM_IOCTL_MODE_MAP_DUMB failed ret=%d\n", ret);
Tao Bao557fa1f2017-02-07 12:51:00 -0800159 DrmDestroySurface(surface);
Tao Bao76be34c2017-02-07 13:30:19 -0800160 return nullptr;
161 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700162
Tao Bao557fa1f2017-02-07 12:51:00 -0800163 surface->height = height;
164 surface->width = width;
165 surface->row_bytes = create_dumb.pitch;
166 surface->pixel_bytes = create_dumb.bpp / 8;
167 surface->data = static_cast<unsigned char*>(mmap(nullptr, surface->height * surface->row_bytes,
168 PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd,
169 map_dumb.offset));
170 if (surface->data == MAP_FAILED) {
Tao Bao76be34c2017-02-07 13:30:19 -0800171 perror("mmap() failed");
Tao Bao557fa1f2017-02-07 12:51:00 -0800172 DrmDestroySurface(surface);
Tao Bao76be34c2017-02-07 13:30:19 -0800173 return nullptr;
174 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700175
Tao Bao76be34c2017-02-07 13:30:19 -0800176 return surface;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700177}
178
Tao Bao76be34c2017-02-07 13:30:19 -0800179static drmModeCrtc* find_crtc_for_connector(int fd, drmModeRes* resources,
180 drmModeConnector* connector) {
181 // Find the encoder. If we already have one, just use it.
182 drmModeEncoder* encoder;
183 if (connector->encoder_id) {
184 encoder = drmModeGetEncoder(fd, connector->encoder_id);
185 } else {
186 encoder = nullptr;
187 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700188
Tao Bao76be34c2017-02-07 13:30:19 -0800189 int32_t crtc;
190 if (encoder && encoder->crtc_id) {
191 crtc = encoder->crtc_id;
192 drmModeFreeEncoder(encoder);
193 return drmModeGetCrtc(fd, crtc);
194 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700195
Tao Bao76be34c2017-02-07 13:30:19 -0800196 // Didn't find anything, try to find a crtc and encoder combo.
197 crtc = -1;
198 for (int i = 0; i < connector->count_encoders; i++) {
199 encoder = drmModeGetEncoder(fd, connector->encoders[i]);
200
201 if (encoder) {
202 for (int j = 0; j < resources->count_crtcs; j++) {
203 if (!(encoder->possible_crtcs & (1 << j))) continue;
204 crtc = resources->crtcs[j];
205 break;
206 }
207 if (crtc >= 0) {
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700208 drmModeFreeEncoder(encoder);
209 return drmModeGetCrtc(fd, crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800210 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700211 }
Tao Bao76be34c2017-02-07 13:30:19 -0800212 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700213
Tao Bao76be34c2017-02-07 13:30:19 -0800214 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700215}
216
Tao Bao76be34c2017-02-07 13:30:19 -0800217static drmModeConnector* find_used_connector_by_type(int fd, drmModeRes* resources, unsigned type) {
218 for (int i = 0; i < resources->count_connectors; i++) {
219 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
220 if (connector) {
221 if ((connector->connector_type == type) && (connector->connection == DRM_MODE_CONNECTED) &&
222 (connector->count_modes > 0)) {
223 return connector;
224 }
225 drmModeFreeConnector(connector);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700226 }
Tao Bao76be34c2017-02-07 13:30:19 -0800227 }
228 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700229}
230
Tao Bao76be34c2017-02-07 13:30:19 -0800231static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* resources) {
232 for (int i = 0; i < resources->count_connectors; i++) {
233 drmModeConnector* connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700234
Tao Bao76be34c2017-02-07 13:30:19 -0800235 connector = drmModeGetConnector(fd, resources->connectors[i]);
236 if (connector) {
237 if ((connector->count_modes > 0) && (connector->connection == DRM_MODE_CONNECTED))
238 return connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700239
Tao Bao76be34c2017-02-07 13:30:19 -0800240 drmModeFreeConnector(connector);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700241 }
Tao Bao76be34c2017-02-07 13:30:19 -0800242 }
243 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700244}
245
Tao Bao557fa1f2017-02-07 12:51:00 -0800246drmModeConnector* MinuiBackendDrm::FindMainMonitor(int fd, drmModeRes* resources,
247 uint32_t* mode_index) {
Tao Bao76be34c2017-02-07 13:30:19 -0800248 /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */
249 static constexpr unsigned kConnectorPriority[] = {
250 DRM_MODE_CONNECTOR_LVDS,
251 DRM_MODE_CONNECTOR_eDP,
252 DRM_MODE_CONNECTOR_DSI,
253 };
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700254
Tao Bao76be34c2017-02-07 13:30:19 -0800255 drmModeConnector* main_monitor_connector = nullptr;
256 unsigned i = 0;
257 do {
258 main_monitor_connector = find_used_connector_by_type(fd, resources, kConnectorPriority[i]);
259 i++;
260 } while (!main_monitor_connector && i < ARRAY_SIZE(kConnectorPriority));
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700261
Tao Bao76be34c2017-02-07 13:30:19 -0800262 /* If we didn't find a connector, grab the first one that is connected. */
263 if (!main_monitor_connector) {
264 main_monitor_connector = find_first_connected_connector(fd, resources);
265 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700266
Tao Bao76be34c2017-02-07 13:30:19 -0800267 /* If we still didn't find a connector, give up and return. */
268 if (!main_monitor_connector) return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700269
Tao Bao76be34c2017-02-07 13:30:19 -0800270 *mode_index = 0;
271 for (int modes = 0; modes < main_monitor_connector->count_modes; modes++) {
272 if (main_monitor_connector->modes[modes].type & DRM_MODE_TYPE_PREFERRED) {
273 *mode_index = modes;
274 break;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700275 }
Tao Bao76be34c2017-02-07 13:30:19 -0800276 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700277
Tao Bao76be34c2017-02-07 13:30:19 -0800278 return main_monitor_connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700279}
280
Tao Bao557fa1f2017-02-07 12:51:00 -0800281void MinuiBackendDrm::DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) {
Tao Bao76be34c2017-02-07 13:30:19 -0800282 for (int i = 0; i < resources->count_connectors; i++) {
283 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
284 drmModeCrtc* crtc = find_crtc_for_connector(fd, resources, connector);
285 if (crtc->crtc_id != main_crtc->crtc_id) {
Tao Bao557fa1f2017-02-07 12:51:00 -0800286 DrmDisableCrtc(fd, crtc);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700287 }
Tao Bao76be34c2017-02-07 13:30:19 -0800288 drmModeFreeCrtc(crtc);
289 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700290}
291
Tao Bao557fa1f2017-02-07 12:51:00 -0800292GRSurface* MinuiBackendDrm::Init() {
Tao Bao76be34c2017-02-07 13:30:19 -0800293 drmModeRes* res = nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700294
Tao Bao76be34c2017-02-07 13:30:19 -0800295 /* Consider DRM devices in order. */
296 for (int i = 0; i < DRM_MAX_MINOR; i++) {
297 char* dev_name;
298 int ret = asprintf(&dev_name, DRM_DEV_NAME, DRM_DIR_NAME, i);
299 if (ret < 0) continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700300
Tao Bao76be34c2017-02-07 13:30:19 -0800301 drm_fd = open(dev_name, O_RDWR, 0);
302 free(dev_name);
303 if (drm_fd < 0) continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700304
Tao Bao76be34c2017-02-07 13:30:19 -0800305 uint64_t cap = 0;
306 /* We need dumb buffers. */
307 ret = drmGetCap(drm_fd, DRM_CAP_DUMB_BUFFER, &cap);
308 if (ret || cap == 0) {
309 close(drm_fd);
310 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700311 }
312
Tao Bao76be34c2017-02-07 13:30:19 -0800313 res = drmModeGetResources(drm_fd);
314 if (!res) {
315 close(drm_fd);
316 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700317 }
318
Tao Bao76be34c2017-02-07 13:30:19 -0800319 /* Use this device if it has at least one connected monitor. */
320 if (res->count_crtcs > 0 && res->count_connectors > 0) {
321 if (find_first_connected_connector(drm_fd, res)) break;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700322 }
323
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700324 drmModeFreeResources(res);
Tao Bao76be34c2017-02-07 13:30:19 -0800325 close(drm_fd);
326 res = nullptr;
327 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700328
Tao Bao76be34c2017-02-07 13:30:19 -0800329 if (drm_fd < 0 || res == nullptr) {
330 perror("cannot find/open a drm device");
331 return nullptr;
332 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700333
Tao Bao76be34c2017-02-07 13:30:19 -0800334 uint32_t selected_mode;
Tao Bao557fa1f2017-02-07 12:51:00 -0800335 main_monitor_connector = FindMainMonitor(drm_fd, res, &selected_mode);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700336
Tao Bao76be34c2017-02-07 13:30:19 -0800337 if (!main_monitor_connector) {
338 printf("main_monitor_connector not found\n");
339 drmModeFreeResources(res);
340 close(drm_fd);
341 return nullptr;
342 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700343
Tao Bao76be34c2017-02-07 13:30:19 -0800344 main_monitor_crtc = find_crtc_for_connector(drm_fd, res, main_monitor_connector);
345
346 if (!main_monitor_crtc) {
347 printf("main_monitor_crtc not found\n");
348 drmModeFreeResources(res);
349 close(drm_fd);
350 return nullptr;
351 }
352
Tao Bao557fa1f2017-02-07 12:51:00 -0800353 DisableNonMainCrtcs(drm_fd, res, main_monitor_crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800354
355 main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode];
356
357 int width = main_monitor_crtc->mode.hdisplay;
358 int height = main_monitor_crtc->mode.vdisplay;
359
360 drmModeFreeResources(res);
361
Tao Bao557fa1f2017-02-07 12:51:00 -0800362 GRSurfaceDrms[0] = DrmCreateSurface(width, height);
363 GRSurfaceDrms[1] = DrmCreateSurface(width, height);
364 if (!GRSurfaceDrms[0] || !GRSurfaceDrms[1]) {
365 // GRSurfaceDrms and drm_fd should be freed in d'tor.
Tao Bao76be34c2017-02-07 13:30:19 -0800366 return nullptr;
367 }
368
369 current_buffer = 0;
370
Tao Bao557fa1f2017-02-07 12:51:00 -0800371 DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[1]);
Tao Bao76be34c2017-02-07 13:30:19 -0800372
Tao Bao557fa1f2017-02-07 12:51:00 -0800373 return GRSurfaceDrms[0];
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700374}
375
Tao Bao557fa1f2017-02-07 12:51:00 -0800376GRSurface* MinuiBackendDrm::Flip() {
377 int ret = drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id,
378 GRSurfaceDrms[current_buffer]->fb_id, 0, nullptr);
Tao Bao76be34c2017-02-07 13:30:19 -0800379 if (ret < 0) {
380 printf("drmModePageFlip failed ret=%d\n", ret);
381 return nullptr;
382 }
383 current_buffer = 1 - current_buffer;
Tao Bao557fa1f2017-02-07 12:51:00 -0800384 return GRSurfaceDrms[current_buffer];
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700385}
386
Tao Bao557fa1f2017-02-07 12:51:00 -0800387MinuiBackendDrm::~MinuiBackendDrm() {
388 DrmDisableCrtc(drm_fd, main_monitor_crtc);
389 DrmDestroySurface(GRSurfaceDrms[0]);
390 DrmDestroySurface(GRSurfaceDrms[1]);
Tao Bao76be34c2017-02-07 13:30:19 -0800391 drmModeFreeCrtc(main_monitor_crtc);
392 drmModeFreeConnector(main_monitor_connector);
393 close(drm_fd);
394 drm_fd = -1;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700395}