blob: 95759e3827d08f8e421b72a37b2b72a329f59334 [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:
Adrian Salidocc7b7eb2019-12-12 20:18:09 -080065 case DRM_FORMAT_RGBA8888:
66 case DRM_FORMAT_ARGB8888:
Tao Bao76be34c2017-02-07 13:30:19 -080067 case DRM_FORMAT_BGRX8888:
68 case DRM_FORMAT_XBGR8888:
69 case DRM_FORMAT_XRGB8888:
70 return 32;
71 case DRM_FORMAT_RGB565:
72 return 16;
73 default:
74 printf("Unknown format %d\n", format);
75 return 32;
76 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070077}
78
Tao Baod096d7e2018-10-23 17:24:34 -070079std::unique_ptr<GRSurfaceDrm> GRSurfaceDrm::Create(int drm_fd, int width, int height) {
Tao Bao76be34c2017-02-07 13:30:19 -080080 uint32_t format;
Tao Baoed876a72018-07-31 21:32:50 -070081 PixelFormat pixel_format = gr_pixel_format();
tangrobin13bec762018-08-07 17:23:31 +080082 // PixelFormat comes in byte order, whereas DRM_FORMAT_* uses little-endian
83 // (external/libdrm/include/drm/drm_fourcc.h). Note that although drm_fourcc.h also defines a
84 // macro of DRM_FORMAT_BIG_ENDIAN, it doesn't seem to be actually supported (see the discussion
85 // in https://lists.freedesktop.org/archives/amd-gfx/2017-May/008560.html).
Tao Baoed876a72018-07-31 21:32:50 -070086 if (pixel_format == PixelFormat::ABGR) {
tangrobin13bec762018-08-07 17:23:31 +080087 format = DRM_FORMAT_RGBA8888;
Tao Baoed876a72018-07-31 21:32:50 -070088 } else if (pixel_format == PixelFormat::BGRA) {
tangrobin13bec762018-08-07 17:23:31 +080089 format = DRM_FORMAT_ARGB8888;
Tao Baoed876a72018-07-31 21:32:50 -070090 } else if (pixel_format == PixelFormat::RGBX) {
tangrobin13bec762018-08-07 17:23:31 +080091 format = DRM_FORMAT_XBGR8888;
Adrian Salidocc7b7eb2019-12-12 20:18:09 -080092 } else if (pixel_format == PixelFormat::ARGB) {
93 format = DRM_FORMAT_BGRA8888;
Tao Baoed876a72018-07-31 21:32:50 -070094 } else {
95 format = DRM_FORMAT_RGB565;
96 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070097
Tao Bao76be34c2017-02-07 13:30:19 -080098 drm_mode_create_dumb create_dumb = {};
99 create_dumb.height = height;
100 create_dumb.width = width;
101 create_dumb.bpp = drm_format_to_bpp(format);
102 create_dumb.flags = 0;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700103
Tao Baod096d7e2018-10-23 17:24:34 -0700104 if (drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb) != 0) {
105 perror("Failed to DRM_IOCTL_MODE_CREATE_DUMB");
Tao Bao76be34c2017-02-07 13:30:19 -0800106 return nullptr;
107 }
Tao Baod096d7e2018-10-23 17:24:34 -0700108
Tao Bao710bc532018-10-24 07:59:49 -0700109 // Cannot use std::make_unique to access non-public ctor.
110 auto surface = std::unique_ptr<GRSurfaceDrm>(new GRSurfaceDrm(
111 width, height, create_dumb.pitch, create_dumb.bpp / 8, drm_fd, create_dumb.handle));
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700112
Tao Bao76be34c2017-02-07 13:30:19 -0800113 uint32_t handles[4], pitches[4], offsets[4];
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700114
Tao Bao76be34c2017-02-07 13:30:19 -0800115 handles[0] = surface->handle;
116 pitches[0] = create_dumb.pitch;
117 offsets[0] = 0;
Tao Baod096d7e2018-10-23 17:24:34 -0700118 if (drmModeAddFB2(drm_fd, width, height, format, handles, pitches, offsets, &surface->fb_id, 0) !=
119 0) {
120 perror("Failed to drmModeAddFB2");
Tao Bao76be34c2017-02-07 13:30:19 -0800121 return nullptr;
122 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700123
Tao Bao76be34c2017-02-07 13:30:19 -0800124 drm_mode_map_dumb map_dumb = {};
125 map_dumb.handle = create_dumb.handle;
Tao Baod096d7e2018-10-23 17:24:34 -0700126 if (drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb) != 0) {
127 perror("Failed to DRM_IOCTL_MODE_MAP_DUMB");
Tao Bao76be34c2017-02-07 13:30:19 -0800128 return nullptr;
129 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700130
Tao Bao92bdb5a2018-10-21 12:12:37 -0700131 auto mmapped = mmap(nullptr, surface->height * surface->row_bytes, PROT_READ | PROT_WRITE,
132 MAP_SHARED, drm_fd, map_dumb.offset);
133 if (mmapped == MAP_FAILED) {
Tao Baod096d7e2018-10-23 17:24:34 -0700134 perror("Failed to mmap()");
Tao Bao76be34c2017-02-07 13:30:19 -0800135 return nullptr;
136 }
Tao Bao92bdb5a2018-10-21 12:12:37 -0700137 surface->mmapped_buffer_ = static_cast<uint8_t*>(mmapped);
Tao Bao76be34c2017-02-07 13:30:19 -0800138 return surface;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700139}
140
Tao Baod096d7e2018-10-23 17:24:34 -0700141void MinuiBackendDrm::DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc) {
142 if (crtc) {
143 drmModeSetCrtc(drm_fd, crtc->crtc_id,
144 0, // fb_id
145 0, 0, // x,y
146 nullptr, // connectors
147 0, // connector_count
148 nullptr); // mode
149 }
150}
151
152bool MinuiBackendDrm::DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc,
153 const std::unique_ptr<GRSurfaceDrm>& surface) {
154 if (drmModeSetCrtc(drm_fd, crtc->crtc_id, surface->fb_id, 0, 0, // x,y
155 &main_monitor_connector->connector_id,
156 1, // connector_count
157 &main_monitor_crtc->mode) != 0) {
158 perror("Failed to drmModeSetCrtc");
159 return false;
160 }
161 return true;
162}
163
164void MinuiBackendDrm::Blank(bool blank) {
165 if (blank) {
166 DrmDisableCrtc(drm_fd, main_monitor_crtc);
167 } else {
168 DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[current_buffer]);
169 }
170}
171
Tao Bao76be34c2017-02-07 13:30:19 -0800172static drmModeCrtc* find_crtc_for_connector(int fd, drmModeRes* resources,
173 drmModeConnector* connector) {
174 // Find the encoder. If we already have one, just use it.
175 drmModeEncoder* encoder;
176 if (connector->encoder_id) {
177 encoder = drmModeGetEncoder(fd, connector->encoder_id);
178 } else {
179 encoder = nullptr;
180 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700181
Tao Bao76be34c2017-02-07 13:30:19 -0800182 int32_t crtc;
183 if (encoder && encoder->crtc_id) {
184 crtc = encoder->crtc_id;
185 drmModeFreeEncoder(encoder);
186 return drmModeGetCrtc(fd, crtc);
187 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700188
Tao Bao76be34c2017-02-07 13:30:19 -0800189 // Didn't find anything, try to find a crtc and encoder combo.
190 crtc = -1;
191 for (int i = 0; i < connector->count_encoders; i++) {
192 encoder = drmModeGetEncoder(fd, connector->encoders[i]);
193
194 if (encoder) {
195 for (int j = 0; j < resources->count_crtcs; j++) {
196 if (!(encoder->possible_crtcs & (1 << j))) continue;
197 crtc = resources->crtcs[j];
198 break;
199 }
200 if (crtc >= 0) {
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700201 drmModeFreeEncoder(encoder);
202 return drmModeGetCrtc(fd, crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800203 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700204 }
Tao Bao76be34c2017-02-07 13:30:19 -0800205 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700206
Tao Bao76be34c2017-02-07 13:30:19 -0800207 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700208}
209
Tao Bao76be34c2017-02-07 13:30:19 -0800210static drmModeConnector* find_used_connector_by_type(int fd, drmModeRes* resources, unsigned type) {
211 for (int i = 0; i < resources->count_connectors; i++) {
212 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
213 if (connector) {
214 if ((connector->connector_type == type) && (connector->connection == DRM_MODE_CONNECTED) &&
215 (connector->count_modes > 0)) {
216 return connector;
217 }
218 drmModeFreeConnector(connector);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700219 }
Tao Bao76be34c2017-02-07 13:30:19 -0800220 }
221 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700222}
223
Tao Bao76be34c2017-02-07 13:30:19 -0800224static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* resources) {
225 for (int i = 0; i < resources->count_connectors; i++) {
226 drmModeConnector* connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700227
Tao Bao76be34c2017-02-07 13:30:19 -0800228 connector = drmModeGetConnector(fd, resources->connectors[i]);
229 if (connector) {
230 if ((connector->count_modes > 0) && (connector->connection == DRM_MODE_CONNECTED))
231 return connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700232
Tao Bao76be34c2017-02-07 13:30:19 -0800233 drmModeFreeConnector(connector);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700234 }
Tao Bao76be34c2017-02-07 13:30:19 -0800235 }
236 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700237}
238
Tao Bao557fa1f2017-02-07 12:51:00 -0800239drmModeConnector* MinuiBackendDrm::FindMainMonitor(int fd, drmModeRes* resources,
240 uint32_t* mode_index) {
Tao Bao76be34c2017-02-07 13:30:19 -0800241 /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */
242 static constexpr unsigned kConnectorPriority[] = {
243 DRM_MODE_CONNECTOR_LVDS,
244 DRM_MODE_CONNECTOR_eDP,
245 DRM_MODE_CONNECTOR_DSI,
246 };
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700247
Tao Bao76be34c2017-02-07 13:30:19 -0800248 drmModeConnector* main_monitor_connector = nullptr;
249 unsigned i = 0;
250 do {
251 main_monitor_connector = find_used_connector_by_type(fd, resources, kConnectorPriority[i]);
252 i++;
Tao Baod096d7e2018-10-23 17:24:34 -0700253 } while (!main_monitor_connector && i < arraysize(kConnectorPriority));
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700254
Tao Bao76be34c2017-02-07 13:30:19 -0800255 /* If we didn't find a connector, grab the first one that is connected. */
256 if (!main_monitor_connector) {
257 main_monitor_connector = find_first_connected_connector(fd, resources);
258 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700259
Tao Bao76be34c2017-02-07 13:30:19 -0800260 /* If we still didn't find a connector, give up and return. */
261 if (!main_monitor_connector) return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700262
Tao Bao76be34c2017-02-07 13:30:19 -0800263 *mode_index = 0;
264 for (int modes = 0; modes < main_monitor_connector->count_modes; modes++) {
265 if (main_monitor_connector->modes[modes].type & DRM_MODE_TYPE_PREFERRED) {
266 *mode_index = modes;
267 break;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700268 }
Tao Bao76be34c2017-02-07 13:30:19 -0800269 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700270
Tao Bao76be34c2017-02-07 13:30:19 -0800271 return main_monitor_connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700272}
273
Tao Bao557fa1f2017-02-07 12:51:00 -0800274void MinuiBackendDrm::DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) {
Tao Bao76be34c2017-02-07 13:30:19 -0800275 for (int i = 0; i < resources->count_connectors; i++) {
276 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
277 drmModeCrtc* crtc = find_crtc_for_connector(fd, resources, connector);
278 if (crtc->crtc_id != main_crtc->crtc_id) {
Tao Bao557fa1f2017-02-07 12:51:00 -0800279 DrmDisableCrtc(fd, crtc);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700280 }
Tao Bao76be34c2017-02-07 13:30:19 -0800281 drmModeFreeCrtc(crtc);
282 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700283}
284
Tao Bao557fa1f2017-02-07 12:51:00 -0800285GRSurface* MinuiBackendDrm::Init() {
Tao Bao76be34c2017-02-07 13:30:19 -0800286 drmModeRes* res = nullptr;
Tao Baod096d7e2018-10-23 17:24:34 -0700287 drm_fd = -1;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700288
Tao Bao76be34c2017-02-07 13:30:19 -0800289 /* Consider DRM devices in order. */
290 for (int i = 0; i < DRM_MAX_MINOR; i++) {
Tao Baod096d7e2018-10-23 17:24:34 -0700291 auto dev_name = android::base::StringPrintf(DRM_DEV_NAME, DRM_DIR_NAME, i);
Tao Baob5492432019-01-16 09:29:17 -0800292 android::base::unique_fd fd(open(dev_name.c_str(), O_RDWR | O_CLOEXEC));
Tao Baod096d7e2018-10-23 17:24:34 -0700293 if (fd == -1) continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700294
Tao Bao76be34c2017-02-07 13:30:19 -0800295 /* We need dumb buffers. */
Tao Baod096d7e2018-10-23 17:24:34 -0700296 if (uint64_t cap = 0; drmGetCap(fd.get(), DRM_CAP_DUMB_BUFFER, &cap) != 0 || cap == 0) {
Tao Bao76be34c2017-02-07 13:30:19 -0800297 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700298 }
299
Tao Baod096d7e2018-10-23 17:24:34 -0700300 res = drmModeGetResources(fd.get());
Tao Bao76be34c2017-02-07 13:30:19 -0800301 if (!res) {
Tao Bao76be34c2017-02-07 13:30:19 -0800302 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700303 }
304
Tao Bao76be34c2017-02-07 13:30:19 -0800305 /* Use this device if it has at least one connected monitor. */
306 if (res->count_crtcs > 0 && res->count_connectors > 0) {
Tao Baod096d7e2018-10-23 17:24:34 -0700307 if (find_first_connected_connector(fd.get(), res)) {
308 drm_fd = fd.release();
309 break;
310 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700311 }
312
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700313 drmModeFreeResources(res);
Tao Bao76be34c2017-02-07 13:30:19 -0800314 res = nullptr;
315 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700316
Tao Baod096d7e2018-10-23 17:24:34 -0700317 if (drm_fd == -1 || res == nullptr) {
318 perror("Failed to find/open a drm device");
Tao Bao76be34c2017-02-07 13:30:19 -0800319 return nullptr;
320 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700321
Tao Bao76be34c2017-02-07 13:30:19 -0800322 uint32_t selected_mode;
Tao Bao557fa1f2017-02-07 12:51:00 -0800323 main_monitor_connector = FindMainMonitor(drm_fd, res, &selected_mode);
Tao Bao76be34c2017-02-07 13:30:19 -0800324 if (!main_monitor_connector) {
Tao Baod096d7e2018-10-23 17:24:34 -0700325 fprintf(stderr, "Failed to find main_monitor_connector\n");
Tao Bao76be34c2017-02-07 13:30:19 -0800326 drmModeFreeResources(res);
327 close(drm_fd);
328 return nullptr;
329 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700330
Tao Bao76be34c2017-02-07 13:30:19 -0800331 main_monitor_crtc = find_crtc_for_connector(drm_fd, res, main_monitor_connector);
Tao Bao76be34c2017-02-07 13:30:19 -0800332 if (!main_monitor_crtc) {
Tao Baod096d7e2018-10-23 17:24:34 -0700333 fprintf(stderr, "Failed to find main_monitor_crtc\n");
Tao Bao76be34c2017-02-07 13:30:19 -0800334 drmModeFreeResources(res);
335 close(drm_fd);
336 return nullptr;
337 }
338
Tao Bao557fa1f2017-02-07 12:51:00 -0800339 DisableNonMainCrtcs(drm_fd, res, main_monitor_crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800340
341 main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode];
342
343 int width = main_monitor_crtc->mode.hdisplay;
344 int height = main_monitor_crtc->mode.vdisplay;
345
346 drmModeFreeResources(res);
347
Tao Baod096d7e2018-10-23 17:24:34 -0700348 GRSurfaceDrms[0] = GRSurfaceDrm::Create(drm_fd, width, height);
349 GRSurfaceDrms[1] = GRSurfaceDrm::Create(drm_fd, width, height);
Tao Bao557fa1f2017-02-07 12:51:00 -0800350 if (!GRSurfaceDrms[0] || !GRSurfaceDrms[1]) {
Tao Bao76be34c2017-02-07 13:30:19 -0800351 return nullptr;
352 }
353
354 current_buffer = 0;
355
Tianjie Xuccf00a22018-06-05 17:10:23 -0700356 // We will likely encounter errors in the backend functions (i.e. Flip) if EnableCrtc fails.
Tao Baod096d7e2018-10-23 17:24:34 -0700357 if (!DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[1])) {
Tianjie Xuccf00a22018-06-05 17:10:23 -0700358 return nullptr;
359 }
Tao Bao76be34c2017-02-07 13:30:19 -0800360
Tao Baod096d7e2018-10-23 17:24:34 -0700361 return GRSurfaceDrms[0].get();
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700362}
363
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700364static void page_flip_complete(__unused int fd,
365 __unused unsigned int sequence,
366 __unused unsigned int tv_sec,
367 __unused unsigned int tv_usec,
368 void *user_data) {
369 *static_cast<bool*>(user_data) = false;
370}
371
Tao Bao557fa1f2017-02-07 12:51:00 -0800372GRSurface* MinuiBackendDrm::Flip() {
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700373 bool ongoing_flip = true;
Tao Baod096d7e2018-10-23 17:24:34 -0700374 if (drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id, GRSurfaceDrms[current_buffer]->fb_id,
375 DRM_MODE_PAGE_FLIP_EVENT, &ongoing_flip) != 0) {
376 perror("Failed to drmModePageFlip");
Tao Bao76be34c2017-02-07 13:30:19 -0800377 return nullptr;
378 }
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700379
380 while (ongoing_flip) {
381 struct pollfd fds = {
382 .fd = drm_fd,
383 .events = POLLIN
384 };
385
Tao Baod096d7e2018-10-23 17:24:34 -0700386 if (poll(&fds, 1, -1) == -1 || !(fds.revents & POLLIN)) {
387 perror("Failed to poll() on drm fd");
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700388 break;
389 }
390
391 drmEventContext evctx = {
392 .version = DRM_EVENT_CONTEXT_VERSION,
393 .page_flip_handler = page_flip_complete
394 };
395
Tao Baod096d7e2018-10-23 17:24:34 -0700396 if (drmHandleEvent(drm_fd, &evctx) != 0) {
397 perror("Failed to drmHandleEvent");
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700398 break;
399 }
400 }
401
Tao Bao76be34c2017-02-07 13:30:19 -0800402 current_buffer = 1 - current_buffer;
Tao Baod096d7e2018-10-23 17:24:34 -0700403 return GRSurfaceDrms[current_buffer].get();
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700404}
405
Tao Bao557fa1f2017-02-07 12:51:00 -0800406MinuiBackendDrm::~MinuiBackendDrm() {
407 DrmDisableCrtc(drm_fd, main_monitor_crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800408 drmModeFreeCrtc(main_monitor_crtc);
409 drmModeFreeConnector(main_monitor_connector);
410 close(drm_fd);
411 drm_fd = -1;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700412}