blob: 765e2625a061ceb5b564ffcb8bdc1b3e35769eb0 [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>
Jeremy Compostella955da1e2018-06-07 10:51:33 -070020#include <poll.h>
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070021#include <stdio.h>
22#include <stdlib.h>
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070023#include <sys/mman.h>
24#include <sys/types.h>
25#include <unistd.h>
Tao Bao76be34c2017-02-07 13:30:19 -080026
Tao Baod096d7e2018-10-23 17:24:34 -070027#include <memory>
28
29#include <android-base/macros.h>
30#include <android-base/stringprintf.h>
31#include <android-base/unique_fd.h>
Tao Bao76be34c2017-02-07 13:30:19 -080032#include <drm_fourcc.h>
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070033#include <xf86drm.h>
34#include <xf86drmMode.h>
35
Tao Bao0ecbd762017-01-16 21:16:58 -080036#include "minui/minui.h"
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070037
Tao Baod096d7e2018-10-23 17:24:34 -070038GRSurfaceDrm::~GRSurfaceDrm() {
39 if (mmapped_buffer_) {
40 munmap(mmapped_buffer_, row_bytes * height);
Tao Bao76be34c2017-02-07 13:30:19 -080041 }
Tianjie Xuccf00a22018-06-05 17:10:23 -070042
Tao Baod096d7e2018-10-23 17:24:34 -070043 if (fb_id) {
44 if (drmModeRmFB(drm_fd_, fb_id) != 0) {
45 perror("Failed to drmModeRmFB");
46 // Falling through to free other resources.
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070047 }
Tao Bao76be34c2017-02-07 13:30:19 -080048 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070049
Tao Baod096d7e2018-10-23 17:24:34 -070050 if (handle) {
Tao Bao76be34c2017-02-07 13:30:19 -080051 drm_gem_close gem_close = {};
Tao Baod096d7e2018-10-23 17:24:34 -070052 gem_close.handle = handle;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070053
Tao Baod096d7e2018-10-23 17:24:34 -070054 if (drmIoctl(drm_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close) != 0) {
55 perror("Failed to DRM_IOCTL_GEM_CLOSE");
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070056 }
Tao Bao76be34c2017-02-07 13:30:19 -080057 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070058}
59
60static int drm_format_to_bpp(uint32_t format) {
Tao Bao76be34c2017-02-07 13:30:19 -080061 switch (format) {
62 case DRM_FORMAT_ABGR8888:
63 case DRM_FORMAT_BGRA8888:
64 case DRM_FORMAT_RGBX8888:
65 case DRM_FORMAT_BGRX8888:
66 case DRM_FORMAT_XBGR8888:
67 case DRM_FORMAT_XRGB8888:
68 return 32;
69 case DRM_FORMAT_RGB565:
70 return 16;
71 default:
72 printf("Unknown format %d\n", format);
73 return 32;
74 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070075}
76
Tao Baod096d7e2018-10-23 17:24:34 -070077std::unique_ptr<GRSurfaceDrm> GRSurfaceDrm::Create(int drm_fd, int width, int height) {
Tao Bao76be34c2017-02-07 13:30:19 -080078 uint32_t format;
Tao Baoed876a72018-07-31 21:32:50 -070079 PixelFormat pixel_format = gr_pixel_format();
tangrobin13bec762018-08-07 17:23:31 +080080 // PixelFormat comes in byte order, whereas DRM_FORMAT_* uses little-endian
81 // (external/libdrm/include/drm/drm_fourcc.h). Note that although drm_fourcc.h also defines a
82 // macro of DRM_FORMAT_BIG_ENDIAN, it doesn't seem to be actually supported (see the discussion
83 // in https://lists.freedesktop.org/archives/amd-gfx/2017-May/008560.html).
Tao Baoed876a72018-07-31 21:32:50 -070084 if (pixel_format == PixelFormat::ABGR) {
tangrobin13bec762018-08-07 17:23:31 +080085 format = DRM_FORMAT_RGBA8888;
Tao Baoed876a72018-07-31 21:32:50 -070086 } else if (pixel_format == PixelFormat::BGRA) {
tangrobin13bec762018-08-07 17:23:31 +080087 format = DRM_FORMAT_ARGB8888;
Tao Baoed876a72018-07-31 21:32:50 -070088 } else if (pixel_format == PixelFormat::RGBX) {
tangrobin13bec762018-08-07 17:23:31 +080089 format = DRM_FORMAT_XBGR8888;
Tao Baoed876a72018-07-31 21:32:50 -070090 } else {
91 format = DRM_FORMAT_RGB565;
92 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070093
Tao Bao76be34c2017-02-07 13:30:19 -080094 drm_mode_create_dumb create_dumb = {};
95 create_dumb.height = height;
96 create_dumb.width = width;
97 create_dumb.bpp = drm_format_to_bpp(format);
98 create_dumb.flags = 0;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070099
Tao Baod096d7e2018-10-23 17:24:34 -0700100 if (drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb) != 0) {
101 perror("Failed to DRM_IOCTL_MODE_CREATE_DUMB");
Tao Bao76be34c2017-02-07 13:30:19 -0800102 return nullptr;
103 }
Tao Baod096d7e2018-10-23 17:24:34 -0700104
Tao Bao710bc532018-10-24 07:59:49 -0700105 // Cannot use std::make_unique to access non-public ctor.
106 auto surface = std::unique_ptr<GRSurfaceDrm>(new GRSurfaceDrm(
107 width, height, create_dumb.pitch, create_dumb.bpp / 8, drm_fd, create_dumb.handle));
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700108
Tao Bao76be34c2017-02-07 13:30:19 -0800109 uint32_t handles[4], pitches[4], offsets[4];
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700110
Tao Bao76be34c2017-02-07 13:30:19 -0800111 handles[0] = surface->handle;
112 pitches[0] = create_dumb.pitch;
113 offsets[0] = 0;
Tao Baod096d7e2018-10-23 17:24:34 -0700114 if (drmModeAddFB2(drm_fd, width, height, format, handles, pitches, offsets, &surface->fb_id, 0) !=
115 0) {
116 perror("Failed to drmModeAddFB2");
Tao Bao76be34c2017-02-07 13:30:19 -0800117 return nullptr;
118 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700119
Tao Bao76be34c2017-02-07 13:30:19 -0800120 drm_mode_map_dumb map_dumb = {};
121 map_dumb.handle = create_dumb.handle;
Tao Baod096d7e2018-10-23 17:24:34 -0700122 if (drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb) != 0) {
123 perror("Failed to DRM_IOCTL_MODE_MAP_DUMB");
Tao Bao76be34c2017-02-07 13:30:19 -0800124 return nullptr;
125 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700126
Tao Bao92bdb5a2018-10-21 12:12:37 -0700127 auto mmapped = mmap(nullptr, surface->height * surface->row_bytes, PROT_READ | PROT_WRITE,
128 MAP_SHARED, drm_fd, map_dumb.offset);
129 if (mmapped == MAP_FAILED) {
Tao Baod096d7e2018-10-23 17:24:34 -0700130 perror("Failed to mmap()");
Tao Bao76be34c2017-02-07 13:30:19 -0800131 return nullptr;
132 }
Tao Bao92bdb5a2018-10-21 12:12:37 -0700133 surface->mmapped_buffer_ = static_cast<uint8_t*>(mmapped);
Tao Bao76be34c2017-02-07 13:30:19 -0800134 return surface;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700135}
136
Tao Baod096d7e2018-10-23 17:24:34 -0700137void MinuiBackendDrm::DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc) {
138 if (crtc) {
139 drmModeSetCrtc(drm_fd, crtc->crtc_id,
140 0, // fb_id
141 0, 0, // x,y
142 nullptr, // connectors
143 0, // connector_count
144 nullptr); // mode
145 }
146}
147
148bool MinuiBackendDrm::DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc,
149 const std::unique_ptr<GRSurfaceDrm>& surface) {
150 if (drmModeSetCrtc(drm_fd, crtc->crtc_id, surface->fb_id, 0, 0, // x,y
151 &main_monitor_connector->connector_id,
152 1, // connector_count
153 &main_monitor_crtc->mode) != 0) {
154 perror("Failed to drmModeSetCrtc");
155 return false;
156 }
157 return true;
158}
159
160void MinuiBackendDrm::Blank(bool blank) {
161 if (blank) {
162 DrmDisableCrtc(drm_fd, main_monitor_crtc);
163 } else {
164 DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[current_buffer]);
165 }
166}
167
Tao Bao76be34c2017-02-07 13:30:19 -0800168static drmModeCrtc* find_crtc_for_connector(int fd, drmModeRes* resources,
169 drmModeConnector* connector) {
170 // Find the encoder. If we already have one, just use it.
171 drmModeEncoder* encoder;
172 if (connector->encoder_id) {
173 encoder = drmModeGetEncoder(fd, connector->encoder_id);
174 } else {
175 encoder = nullptr;
176 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700177
Tao Bao76be34c2017-02-07 13:30:19 -0800178 int32_t crtc;
179 if (encoder && encoder->crtc_id) {
180 crtc = encoder->crtc_id;
181 drmModeFreeEncoder(encoder);
182 return drmModeGetCrtc(fd, crtc);
183 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700184
Tao Bao76be34c2017-02-07 13:30:19 -0800185 // Didn't find anything, try to find a crtc and encoder combo.
186 crtc = -1;
187 for (int i = 0; i < connector->count_encoders; i++) {
188 encoder = drmModeGetEncoder(fd, connector->encoders[i]);
189
190 if (encoder) {
191 for (int j = 0; j < resources->count_crtcs; j++) {
192 if (!(encoder->possible_crtcs & (1 << j))) continue;
193 crtc = resources->crtcs[j];
194 break;
195 }
196 if (crtc >= 0) {
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700197 drmModeFreeEncoder(encoder);
198 return drmModeGetCrtc(fd, crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800199 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700200 }
Tao Bao76be34c2017-02-07 13:30:19 -0800201 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700202
Tao Bao76be34c2017-02-07 13:30:19 -0800203 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700204}
205
Tao Bao76be34c2017-02-07 13:30:19 -0800206static drmModeConnector* find_used_connector_by_type(int fd, drmModeRes* resources, unsigned type) {
207 for (int i = 0; i < resources->count_connectors; i++) {
208 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
209 if (connector) {
210 if ((connector->connector_type == type) && (connector->connection == DRM_MODE_CONNECTED) &&
211 (connector->count_modes > 0)) {
212 return connector;
213 }
214 drmModeFreeConnector(connector);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700215 }
Tao Bao76be34c2017-02-07 13:30:19 -0800216 }
217 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700218}
219
Tao Bao76be34c2017-02-07 13:30:19 -0800220static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* resources) {
221 for (int i = 0; i < resources->count_connectors; i++) {
222 drmModeConnector* connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700223
Tao Bao76be34c2017-02-07 13:30:19 -0800224 connector = drmModeGetConnector(fd, resources->connectors[i]);
225 if (connector) {
226 if ((connector->count_modes > 0) && (connector->connection == DRM_MODE_CONNECTED))
227 return connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700228
Tao Bao76be34c2017-02-07 13:30:19 -0800229 drmModeFreeConnector(connector);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700230 }
Tao Bao76be34c2017-02-07 13:30:19 -0800231 }
232 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700233}
234
Tao Bao557fa1f2017-02-07 12:51:00 -0800235drmModeConnector* MinuiBackendDrm::FindMainMonitor(int fd, drmModeRes* resources,
236 uint32_t* mode_index) {
Tao Bao76be34c2017-02-07 13:30:19 -0800237 /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */
238 static constexpr unsigned kConnectorPriority[] = {
239 DRM_MODE_CONNECTOR_LVDS,
240 DRM_MODE_CONNECTOR_eDP,
241 DRM_MODE_CONNECTOR_DSI,
242 };
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700243
Tao Bao76be34c2017-02-07 13:30:19 -0800244 drmModeConnector* main_monitor_connector = nullptr;
245 unsigned i = 0;
246 do {
247 main_monitor_connector = find_used_connector_by_type(fd, resources, kConnectorPriority[i]);
248 i++;
Tao Baod096d7e2018-10-23 17:24:34 -0700249 } while (!main_monitor_connector && i < arraysize(kConnectorPriority));
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700250
Tao Bao76be34c2017-02-07 13:30:19 -0800251 /* If we didn't find a connector, grab the first one that is connected. */
252 if (!main_monitor_connector) {
253 main_monitor_connector = find_first_connected_connector(fd, resources);
254 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700255
Tao Bao76be34c2017-02-07 13:30:19 -0800256 /* If we still didn't find a connector, give up and return. */
257 if (!main_monitor_connector) return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700258
Tao Bao76be34c2017-02-07 13:30:19 -0800259 *mode_index = 0;
260 for (int modes = 0; modes < main_monitor_connector->count_modes; modes++) {
261 if (main_monitor_connector->modes[modes].type & DRM_MODE_TYPE_PREFERRED) {
262 *mode_index = modes;
263 break;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700264 }
Tao Bao76be34c2017-02-07 13:30:19 -0800265 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700266
Tao Bao76be34c2017-02-07 13:30:19 -0800267 return main_monitor_connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700268}
269
Tao Bao557fa1f2017-02-07 12:51:00 -0800270void MinuiBackendDrm::DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) {
Tao Bao76be34c2017-02-07 13:30:19 -0800271 for (int i = 0; i < resources->count_connectors; i++) {
272 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
273 drmModeCrtc* crtc = find_crtc_for_connector(fd, resources, connector);
274 if (crtc->crtc_id != main_crtc->crtc_id) {
Tao Bao557fa1f2017-02-07 12:51:00 -0800275 DrmDisableCrtc(fd, crtc);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700276 }
Tao Bao76be34c2017-02-07 13:30:19 -0800277 drmModeFreeCrtc(crtc);
278 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700279}
280
Tao Bao557fa1f2017-02-07 12:51:00 -0800281GRSurface* MinuiBackendDrm::Init() {
Tao Bao76be34c2017-02-07 13:30:19 -0800282 drmModeRes* res = nullptr;
Tao Baod096d7e2018-10-23 17:24:34 -0700283 drm_fd = -1;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700284
Tao Bao76be34c2017-02-07 13:30:19 -0800285 /* Consider DRM devices in order. */
286 for (int i = 0; i < DRM_MAX_MINOR; i++) {
Tao Baod096d7e2018-10-23 17:24:34 -0700287 auto dev_name = android::base::StringPrintf(DRM_DEV_NAME, DRM_DIR_NAME, i);
288 android::base::unique_fd fd(open(dev_name.c_str(), O_RDWR));
289 if (fd == -1) continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700290
Tao Bao76be34c2017-02-07 13:30:19 -0800291 /* We need dumb buffers. */
Tao Baod096d7e2018-10-23 17:24:34 -0700292 if (uint64_t cap = 0; drmGetCap(fd.get(), DRM_CAP_DUMB_BUFFER, &cap) != 0 || cap == 0) {
Tao Bao76be34c2017-02-07 13:30:19 -0800293 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700294 }
295
Tao Baod096d7e2018-10-23 17:24:34 -0700296 res = drmModeGetResources(fd.get());
Tao Bao76be34c2017-02-07 13:30:19 -0800297 if (!res) {
Tao Bao76be34c2017-02-07 13:30:19 -0800298 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700299 }
300
Tao Bao76be34c2017-02-07 13:30:19 -0800301 /* Use this device if it has at least one connected monitor. */
302 if (res->count_crtcs > 0 && res->count_connectors > 0) {
Tao Baod096d7e2018-10-23 17:24:34 -0700303 if (find_first_connected_connector(fd.get(), res)) {
304 drm_fd = fd.release();
305 break;
306 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700307 }
308
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700309 drmModeFreeResources(res);
Tao Bao76be34c2017-02-07 13:30:19 -0800310 res = nullptr;
311 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700312
Tao Baod096d7e2018-10-23 17:24:34 -0700313 if (drm_fd == -1 || res == nullptr) {
314 perror("Failed to find/open a drm device");
Tao Bao76be34c2017-02-07 13:30:19 -0800315 return nullptr;
316 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700317
Tao Bao76be34c2017-02-07 13:30:19 -0800318 uint32_t selected_mode;
Tao Bao557fa1f2017-02-07 12:51:00 -0800319 main_monitor_connector = FindMainMonitor(drm_fd, res, &selected_mode);
Tao Bao76be34c2017-02-07 13:30:19 -0800320 if (!main_monitor_connector) {
Tao Baod096d7e2018-10-23 17:24:34 -0700321 fprintf(stderr, "Failed to find main_monitor_connector\n");
Tao Bao76be34c2017-02-07 13:30:19 -0800322 drmModeFreeResources(res);
323 close(drm_fd);
324 return nullptr;
325 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700326
Tao Bao76be34c2017-02-07 13:30:19 -0800327 main_monitor_crtc = find_crtc_for_connector(drm_fd, res, main_monitor_connector);
Tao Bao76be34c2017-02-07 13:30:19 -0800328 if (!main_monitor_crtc) {
Tao Baod096d7e2018-10-23 17:24:34 -0700329 fprintf(stderr, "Failed to find main_monitor_crtc\n");
Tao Bao76be34c2017-02-07 13:30:19 -0800330 drmModeFreeResources(res);
331 close(drm_fd);
332 return nullptr;
333 }
334
Tao Bao557fa1f2017-02-07 12:51:00 -0800335 DisableNonMainCrtcs(drm_fd, res, main_monitor_crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800336
337 main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode];
338
339 int width = main_monitor_crtc->mode.hdisplay;
340 int height = main_monitor_crtc->mode.vdisplay;
341
342 drmModeFreeResources(res);
343
Tao Baod096d7e2018-10-23 17:24:34 -0700344 GRSurfaceDrms[0] = GRSurfaceDrm::Create(drm_fd, width, height);
345 GRSurfaceDrms[1] = GRSurfaceDrm::Create(drm_fd, width, height);
Tao Bao557fa1f2017-02-07 12:51:00 -0800346 if (!GRSurfaceDrms[0] || !GRSurfaceDrms[1]) {
Tao Bao76be34c2017-02-07 13:30:19 -0800347 return nullptr;
348 }
349
350 current_buffer = 0;
351
Tianjie Xuccf00a22018-06-05 17:10:23 -0700352 // We will likely encounter errors in the backend functions (i.e. Flip) if EnableCrtc fails.
Tao Baod096d7e2018-10-23 17:24:34 -0700353 if (!DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[1])) {
Tianjie Xuccf00a22018-06-05 17:10:23 -0700354 return nullptr;
355 }
Tao Bao76be34c2017-02-07 13:30:19 -0800356
Tao Baod096d7e2018-10-23 17:24:34 -0700357 return GRSurfaceDrms[0].get();
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700358}
359
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700360static void page_flip_complete(__unused int fd,
361 __unused unsigned int sequence,
362 __unused unsigned int tv_sec,
363 __unused unsigned int tv_usec,
364 void *user_data) {
365 *static_cast<bool*>(user_data) = false;
366}
367
Tao Bao557fa1f2017-02-07 12:51:00 -0800368GRSurface* MinuiBackendDrm::Flip() {
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700369 bool ongoing_flip = true;
Tao Baod096d7e2018-10-23 17:24:34 -0700370 if (drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id, GRSurfaceDrms[current_buffer]->fb_id,
371 DRM_MODE_PAGE_FLIP_EVENT, &ongoing_flip) != 0) {
372 perror("Failed to drmModePageFlip");
Tao Bao76be34c2017-02-07 13:30:19 -0800373 return nullptr;
374 }
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700375
376 while (ongoing_flip) {
377 struct pollfd fds = {
378 .fd = drm_fd,
379 .events = POLLIN
380 };
381
Tao Baod096d7e2018-10-23 17:24:34 -0700382 if (poll(&fds, 1, -1) == -1 || !(fds.revents & POLLIN)) {
383 perror("Failed to poll() on drm fd");
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700384 break;
385 }
386
387 drmEventContext evctx = {
388 .version = DRM_EVENT_CONTEXT_VERSION,
389 .page_flip_handler = page_flip_complete
390 };
391
Tao Baod096d7e2018-10-23 17:24:34 -0700392 if (drmHandleEvent(drm_fd, &evctx) != 0) {
393 perror("Failed to drmHandleEvent");
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700394 break;
395 }
396 }
397
Tao Bao76be34c2017-02-07 13:30:19 -0800398 current_buffer = 1 - current_buffer;
Tao Baod096d7e2018-10-23 17:24:34 -0700399 return GRSurfaceDrms[current_buffer].get();
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700400}
401
Tao Bao557fa1f2017-02-07 12:51:00 -0800402MinuiBackendDrm::~MinuiBackendDrm() {
403 DrmDisableCrtc(drm_fd, main_monitor_crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800404 drmModeFreeCrtc(main_monitor_crtc);
405 drmModeFreeConnector(main_monitor_connector);
406 close(drm_fd);
407 drm_fd = -1;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700408}