blob: b8d813e0c71773a885f569cb84f9790bc019d93a [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 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -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;
bigbiff26d5d5f2020-03-23 09:56:16 -040079<<<<<<< HEAD
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070080#if defined(RECOVERY_ABGR)
Tao Bao76be34c2017-02-07 13:30:19 -080081 format = DRM_FORMAT_RGBA8888;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070082#elif defined(RECOVERY_BGRA)
Tao Bao76be34c2017-02-07 13:30:19 -080083 format = DRM_FORMAT_ARGB8888;
Kra1o577568592015-10-14 18:09:54 +020084#elif defined(RECOVERY_RGBA)
Ethan Yonker8373cfe2017-09-08 06:50:54 -050085 format = DRM_FORMAT_ARGB8888;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070086#elif defined(RECOVERY_RGBX)
Tao Bao76be34c2017-02-07 13:30:19 -080087 format = DRM_FORMAT_XBGR8888;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070088#else
Tao Bao76be34c2017-02-07 13:30:19 -080089 format = DRM_FORMAT_RGB565;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070090#endif
bigbiff26d5d5f2020-03-23 09:56:16 -040091=======
Tao Baoed876a72018-07-31 21:32:50 -070092 PixelFormat pixel_format = gr_pixel_format();
tangrobin13bec762018-08-07 17:23:31 +080093 // PixelFormat comes in byte order, whereas DRM_FORMAT_* uses little-endian
94 // (external/libdrm/include/drm/drm_fourcc.h). Note that although drm_fourcc.h also defines a
95 // macro of DRM_FORMAT_BIG_ENDIAN, it doesn't seem to be actually supported (see the discussion
96 // in https://lists.freedesktop.org/archives/amd-gfx/2017-May/008560.html).
Tao Baoed876a72018-07-31 21:32:50 -070097 if (pixel_format == PixelFormat::ABGR) {
tangrobin13bec762018-08-07 17:23:31 +080098 format = DRM_FORMAT_RGBA8888;
Tao Baoed876a72018-07-31 21:32:50 -070099 } else if (pixel_format == PixelFormat::BGRA) {
tangrobin13bec762018-08-07 17:23:31 +0800100 format = DRM_FORMAT_ARGB8888;
Tao Baoed876a72018-07-31 21:32:50 -0700101 } else if (pixel_format == PixelFormat::RGBX) {
tangrobin13bec762018-08-07 17:23:31 +0800102 format = DRM_FORMAT_XBGR8888;
Tao Baoed876a72018-07-31 21:32:50 -0700103 } else {
104 format = DRM_FORMAT_RGB565;
105 }
bigbiff26d5d5f2020-03-23 09:56:16 -0400106>>>>>>> android-10.0.0_r25
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700107
Tao Bao76be34c2017-02-07 13:30:19 -0800108 drm_mode_create_dumb create_dumb = {};
109 create_dumb.height = height;
110 create_dumb.width = width;
111 create_dumb.bpp = drm_format_to_bpp(format);
112 create_dumb.flags = 0;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700113
Tao Baod096d7e2018-10-23 17:24:34 -0700114 if (drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb) != 0) {
115 perror("Failed to DRM_IOCTL_MODE_CREATE_DUMB");
Tao Bao76be34c2017-02-07 13:30:19 -0800116 return nullptr;
117 }
Tao Baod096d7e2018-10-23 17:24:34 -0700118
Tao Bao710bc532018-10-24 07:59:49 -0700119 // Cannot use std::make_unique to access non-public ctor.
120 auto surface = std::unique_ptr<GRSurfaceDrm>(new GRSurfaceDrm(
121 width, height, create_dumb.pitch, create_dumb.bpp / 8, drm_fd, create_dumb.handle));
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700122
Tao Bao76be34c2017-02-07 13:30:19 -0800123 uint32_t handles[4], pitches[4], offsets[4];
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700124
Tao Bao76be34c2017-02-07 13:30:19 -0800125 handles[0] = surface->handle;
126 pitches[0] = create_dumb.pitch;
127 offsets[0] = 0;
Tao Baod096d7e2018-10-23 17:24:34 -0700128 if (drmModeAddFB2(drm_fd, width, height, format, handles, pitches, offsets, &surface->fb_id, 0) !=
129 0) {
130 perror("Failed to drmModeAddFB2");
Tao Bao76be34c2017-02-07 13:30:19 -0800131 return nullptr;
132 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700133
Tao Bao76be34c2017-02-07 13:30:19 -0800134 drm_mode_map_dumb map_dumb = {};
135 map_dumb.handle = create_dumb.handle;
Tao Baod096d7e2018-10-23 17:24:34 -0700136 if (drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb) != 0) {
137 perror("Failed to DRM_IOCTL_MODE_MAP_DUMB");
Tao Bao76be34c2017-02-07 13:30:19 -0800138 return nullptr;
139 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700140
Tao Bao92bdb5a2018-10-21 12:12:37 -0700141 auto mmapped = mmap(nullptr, surface->height * surface->row_bytes, PROT_READ | PROT_WRITE,
142 MAP_SHARED, drm_fd, map_dumb.offset);
143 if (mmapped == MAP_FAILED) {
Tao Baod096d7e2018-10-23 17:24:34 -0700144 perror("Failed to mmap()");
Tao Bao76be34c2017-02-07 13:30:19 -0800145 return nullptr;
146 }
Tao Bao92bdb5a2018-10-21 12:12:37 -0700147 surface->mmapped_buffer_ = static_cast<uint8_t*>(mmapped);
Tao Bao76be34c2017-02-07 13:30:19 -0800148 return surface;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700149}
150
Tao Baod096d7e2018-10-23 17:24:34 -0700151void MinuiBackendDrm::DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc) {
152 if (crtc) {
153 drmModeSetCrtc(drm_fd, crtc->crtc_id,
154 0, // fb_id
155 0, 0, // x,y
156 nullptr, // connectors
157 0, // connector_count
158 nullptr); // mode
159 }
160}
161
162bool MinuiBackendDrm::DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc,
163 const std::unique_ptr<GRSurfaceDrm>& surface) {
164 if (drmModeSetCrtc(drm_fd, crtc->crtc_id, surface->fb_id, 0, 0, // x,y
165 &main_monitor_connector->connector_id,
166 1, // connector_count
167 &main_monitor_crtc->mode) != 0) {
168 perror("Failed to drmModeSetCrtc");
169 return false;
170 }
171 return true;
172}
173
174void MinuiBackendDrm::Blank(bool blank) {
175 if (blank) {
176 DrmDisableCrtc(drm_fd, main_monitor_crtc);
177 } else {
178 DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[current_buffer]);
179 }
180}
181
Tao Bao76be34c2017-02-07 13:30:19 -0800182static drmModeCrtc* find_crtc_for_connector(int fd, drmModeRes* resources,
183 drmModeConnector* connector) {
184 // Find the encoder. If we already have one, just use it.
185 drmModeEncoder* encoder;
186 if (connector->encoder_id) {
187 encoder = drmModeGetEncoder(fd, connector->encoder_id);
188 } else {
189 encoder = nullptr;
190 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700191
Tao Bao76be34c2017-02-07 13:30:19 -0800192 int32_t crtc;
193 if (encoder && encoder->crtc_id) {
194 crtc = encoder->crtc_id;
195 drmModeFreeEncoder(encoder);
196 return drmModeGetCrtc(fd, crtc);
197 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700198
Tao Bao76be34c2017-02-07 13:30:19 -0800199 // Didn't find anything, try to find a crtc and encoder combo.
200 crtc = -1;
201 for (int i = 0; i < connector->count_encoders; i++) {
202 encoder = drmModeGetEncoder(fd, connector->encoders[i]);
203
204 if (encoder) {
205 for (int j = 0; j < resources->count_crtcs; j++) {
206 if (!(encoder->possible_crtcs & (1 << j))) continue;
207 crtc = resources->crtcs[j];
208 break;
209 }
210 if (crtc >= 0) {
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700211 drmModeFreeEncoder(encoder);
212 return drmModeGetCrtc(fd, crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800213 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700214 }
Tao Bao76be34c2017-02-07 13:30:19 -0800215 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700216
Tao Bao76be34c2017-02-07 13:30:19 -0800217 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700218}
219
Tao Bao76be34c2017-02-07 13:30:19 -0800220static drmModeConnector* find_used_connector_by_type(int fd, drmModeRes* resources, unsigned type) {
221 for (int i = 0; i < resources->count_connectors; i++) {
222 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
223 if (connector) {
224 if ((connector->connector_type == type) && (connector->connection == DRM_MODE_CONNECTED) &&
225 (connector->count_modes > 0)) {
226 return connector;
227 }
228 drmModeFreeConnector(connector);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700229 }
Tao Bao76be34c2017-02-07 13:30:19 -0800230 }
231 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700232}
233
Tao Bao76be34c2017-02-07 13:30:19 -0800234static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* resources) {
235 for (int i = 0; i < resources->count_connectors; i++) {
236 drmModeConnector* connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700237
Tao Bao76be34c2017-02-07 13:30:19 -0800238 connector = drmModeGetConnector(fd, resources->connectors[i]);
239 if (connector) {
240 if ((connector->count_modes > 0) && (connector->connection == DRM_MODE_CONNECTED))
241 return connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700242
Tao Bao76be34c2017-02-07 13:30:19 -0800243 drmModeFreeConnector(connector);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700244 }
Tao Bao76be34c2017-02-07 13:30:19 -0800245 }
246 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700247}
248
Tao Bao557fa1f2017-02-07 12:51:00 -0800249drmModeConnector* MinuiBackendDrm::FindMainMonitor(int fd, drmModeRes* resources,
250 uint32_t* mode_index) {
Tao Bao76be34c2017-02-07 13:30:19 -0800251 /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */
252 static constexpr unsigned kConnectorPriority[] = {
253 DRM_MODE_CONNECTOR_LVDS,
254 DRM_MODE_CONNECTOR_eDP,
255 DRM_MODE_CONNECTOR_DSI,
256 };
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700257
Tao Bao76be34c2017-02-07 13:30:19 -0800258 drmModeConnector* main_monitor_connector = nullptr;
259 unsigned i = 0;
260 do {
261 main_monitor_connector = find_used_connector_by_type(fd, resources, kConnectorPriority[i]);
262 i++;
Tao Baod096d7e2018-10-23 17:24:34 -0700263 } while (!main_monitor_connector && i < arraysize(kConnectorPriority));
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700264
Tao Bao76be34c2017-02-07 13:30:19 -0800265 /* If we didn't find a connector, grab the first one that is connected. */
266 if (!main_monitor_connector) {
267 main_monitor_connector = find_first_connected_connector(fd, resources);
268 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700269
Tao Bao76be34c2017-02-07 13:30:19 -0800270 /* If we still didn't find a connector, give up and return. */
271 if (!main_monitor_connector) return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700272
Tao Bao76be34c2017-02-07 13:30:19 -0800273 *mode_index = 0;
274 for (int modes = 0; modes < main_monitor_connector->count_modes; modes++) {
275 if (main_monitor_connector->modes[modes].type & DRM_MODE_TYPE_PREFERRED) {
276 *mode_index = modes;
277 break;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700278 }
Tao Bao76be34c2017-02-07 13:30:19 -0800279 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700280
Tao Bao76be34c2017-02-07 13:30:19 -0800281 return main_monitor_connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700282}
283
Tao Bao557fa1f2017-02-07 12:51:00 -0800284void MinuiBackendDrm::DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) {
Tao Bao76be34c2017-02-07 13:30:19 -0800285 for (int i = 0; i < resources->count_connectors; i++) {
286 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
287 drmModeCrtc* crtc = find_crtc_for_connector(fd, resources, connector);
288 if (crtc->crtc_id != main_crtc->crtc_id) {
Tao Bao557fa1f2017-02-07 12:51:00 -0800289 DrmDisableCrtc(fd, crtc);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700290 }
Tao Bao76be34c2017-02-07 13:30:19 -0800291 drmModeFreeCrtc(crtc);
292 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700293}
294
Tao Bao557fa1f2017-02-07 12:51:00 -0800295GRSurface* MinuiBackendDrm::Init() {
Tao Bao76be34c2017-02-07 13:30:19 -0800296 drmModeRes* res = nullptr;
Tao Baod096d7e2018-10-23 17:24:34 -0700297 drm_fd = -1;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700298
Tao Bao76be34c2017-02-07 13:30:19 -0800299 /* Consider DRM devices in order. */
300 for (int i = 0; i < DRM_MAX_MINOR; i++) {
Tao Baod096d7e2018-10-23 17:24:34 -0700301 auto dev_name = android::base::StringPrintf(DRM_DEV_NAME, DRM_DIR_NAME, i);
Tao Baob5492432019-01-16 09:29:17 -0800302 android::base::unique_fd fd(open(dev_name.c_str(), O_RDWR | O_CLOEXEC));
Tao Baod096d7e2018-10-23 17:24:34 -0700303 if (fd == -1) continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700304
Tao Bao76be34c2017-02-07 13:30:19 -0800305 /* We need dumb buffers. */
Tao Baod096d7e2018-10-23 17:24:34 -0700306 if (uint64_t cap = 0; drmGetCap(fd.get(), DRM_CAP_DUMB_BUFFER, &cap) != 0 || cap == 0) {
Tao Bao76be34c2017-02-07 13:30:19 -0800307 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700308 }
309
Tao Baod096d7e2018-10-23 17:24:34 -0700310 res = drmModeGetResources(fd.get());
Tao Bao76be34c2017-02-07 13:30:19 -0800311 if (!res) {
Tao Bao76be34c2017-02-07 13:30:19 -0800312 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700313 }
314
Tao Bao76be34c2017-02-07 13:30:19 -0800315 /* Use this device if it has at least one connected monitor. */
316 if (res->count_crtcs > 0 && res->count_connectors > 0) {
Tao Baod096d7e2018-10-23 17:24:34 -0700317 if (find_first_connected_connector(fd.get(), res)) {
318 drm_fd = fd.release();
319 break;
320 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700321 }
322
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700323 drmModeFreeResources(res);
Tao Bao76be34c2017-02-07 13:30:19 -0800324 res = nullptr;
325 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700326
Tao Baod096d7e2018-10-23 17:24:34 -0700327 if (drm_fd == -1 || res == nullptr) {
328 perror("Failed to find/open a drm device");
Tao Bao76be34c2017-02-07 13:30:19 -0800329 return nullptr;
330 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700331
Tao Bao76be34c2017-02-07 13:30:19 -0800332 uint32_t selected_mode;
Tao Bao557fa1f2017-02-07 12:51:00 -0800333 main_monitor_connector = FindMainMonitor(drm_fd, res, &selected_mode);
Tao Bao76be34c2017-02-07 13:30:19 -0800334 if (!main_monitor_connector) {
Tao Baod096d7e2018-10-23 17:24:34 -0700335 fprintf(stderr, "Failed to find main_monitor_connector\n");
Tao Bao76be34c2017-02-07 13:30:19 -0800336 drmModeFreeResources(res);
337 close(drm_fd);
338 return nullptr;
339 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700340
Tao Bao76be34c2017-02-07 13:30:19 -0800341 main_monitor_crtc = find_crtc_for_connector(drm_fd, res, main_monitor_connector);
Tao Bao76be34c2017-02-07 13:30:19 -0800342 if (!main_monitor_crtc) {
Tao Baod096d7e2018-10-23 17:24:34 -0700343 fprintf(stderr, "Failed to find main_monitor_crtc\n");
Tao Bao76be34c2017-02-07 13:30:19 -0800344 drmModeFreeResources(res);
345 close(drm_fd);
346 return nullptr;
347 }
348
Tao Bao557fa1f2017-02-07 12:51:00 -0800349 DisableNonMainCrtcs(drm_fd, res, main_monitor_crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800350
351 main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode];
352
353 int width = main_monitor_crtc->mode.hdisplay;
354 int height = main_monitor_crtc->mode.vdisplay;
355
356 drmModeFreeResources(res);
357
Tao Baod096d7e2018-10-23 17:24:34 -0700358 GRSurfaceDrms[0] = GRSurfaceDrm::Create(drm_fd, width, height);
359 GRSurfaceDrms[1] = GRSurfaceDrm::Create(drm_fd, width, height);
Tao Bao557fa1f2017-02-07 12:51:00 -0800360 if (!GRSurfaceDrms[0] || !GRSurfaceDrms[1]) {
Tao Bao76be34c2017-02-07 13:30:19 -0800361 return nullptr;
362 }
363
364 current_buffer = 0;
365
Tianjie Xuccf00a22018-06-05 17:10:23 -0700366 // We will likely encounter errors in the backend functions (i.e. Flip) if EnableCrtc fails.
Tao Baod096d7e2018-10-23 17:24:34 -0700367 if (!DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[1])) {
Tianjie Xuccf00a22018-06-05 17:10:23 -0700368 return nullptr;
369 }
Tao Bao76be34c2017-02-07 13:30:19 -0800370
Tao Baod096d7e2018-10-23 17:24:34 -0700371 return GRSurfaceDrms[0].get();
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700372}
373
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700374static void page_flip_complete(__unused int fd,
375 __unused unsigned int sequence,
376 __unused unsigned int tv_sec,
377 __unused unsigned int tv_usec,
378 void *user_data) {
379 *static_cast<bool*>(user_data) = false;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700380}
381
Tao Bao557fa1f2017-02-07 12:51:00 -0800382GRSurface* MinuiBackendDrm::Flip() {
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700383 bool ongoing_flip = true;
Tao Baod096d7e2018-10-23 17:24:34 -0700384 if (drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id, GRSurfaceDrms[current_buffer]->fb_id,
385 DRM_MODE_PAGE_FLIP_EVENT, &ongoing_flip) != 0) {
386 perror("Failed to drmModePageFlip");
Tao Bao76be34c2017-02-07 13:30:19 -0800387 return nullptr;
388 }
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700389
390 while (ongoing_flip) {
391 struct pollfd fds = {
392 .fd = drm_fd,
393 .events = POLLIN
394 };
395
Tao Baod096d7e2018-10-23 17:24:34 -0700396 if (poll(&fds, 1, -1) == -1 || !(fds.revents & POLLIN)) {
397 perror("Failed to poll() on drm fd");
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700398 break;
399 }
400
401 drmEventContext evctx = {
402 .version = DRM_EVENT_CONTEXT_VERSION,
403 .page_flip_handler = page_flip_complete
404 };
405
Tao Baod096d7e2018-10-23 17:24:34 -0700406 if (drmHandleEvent(drm_fd, &evctx) != 0) {
407 perror("Failed to drmHandleEvent");
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700408 break;
409 }
410 }
411
Tao Bao76be34c2017-02-07 13:30:19 -0800412 current_buffer = 1 - current_buffer;
Tao Baod096d7e2018-10-23 17:24:34 -0700413 return GRSurfaceDrms[current_buffer].get();
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700414}
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700415
Tao Bao557fa1f2017-02-07 12:51:00 -0800416MinuiBackendDrm::~MinuiBackendDrm() {
417 DrmDisableCrtc(drm_fd, main_monitor_crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800418 drmModeFreeCrtc(main_monitor_crtc);
419 drmModeFreeConnector(main_monitor_connector);
420 close(drm_fd);
421 drm_fd = -1;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700422}