blob: 6c3a5bdee97a34efd8ca7809a981e1ea523dfb77 [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 }
Kelvin Zhang90f783f2021-03-30 08:59:19 -0400108 printf("Allocating buffer with resolution %d x %d pitch: %d bpp: %d, size: %llu\n", width, height,
109 create_dumb.pitch, create_dumb.bpp, create_dumb.size);
Tao Baod096d7e2018-10-23 17:24:34 -0700110
Tao Bao710bc532018-10-24 07:59:49 -0700111 // Cannot use std::make_unique to access non-public ctor.
112 auto surface = std::unique_ptr<GRSurfaceDrm>(new GRSurfaceDrm(
113 width, height, create_dumb.pitch, create_dumb.bpp / 8, drm_fd, create_dumb.handle));
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700114
Tao Bao76be34c2017-02-07 13:30:19 -0800115 uint32_t handles[4], pitches[4], offsets[4];
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700116
Tao Bao76be34c2017-02-07 13:30:19 -0800117 handles[0] = surface->handle;
118 pitches[0] = create_dumb.pitch;
119 offsets[0] = 0;
Tao Baod096d7e2018-10-23 17:24:34 -0700120 if (drmModeAddFB2(drm_fd, width, height, format, handles, pitches, offsets, &surface->fb_id, 0) !=
121 0) {
122 perror("Failed to drmModeAddFB2");
Tao Bao76be34c2017-02-07 13:30:19 -0800123 return nullptr;
124 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700125
Tao Bao76be34c2017-02-07 13:30:19 -0800126 drm_mode_map_dumb map_dumb = {};
127 map_dumb.handle = create_dumb.handle;
Tao Baod096d7e2018-10-23 17:24:34 -0700128 if (drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb) != 0) {
129 perror("Failed to DRM_IOCTL_MODE_MAP_DUMB");
Tao Bao76be34c2017-02-07 13:30:19 -0800130 return nullptr;
131 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700132
Kelvin Zhang90f783f2021-03-30 08:59:19 -0400133 auto mmapped =
134 mmap(nullptr, create_dumb.size, PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, map_dumb.offset);
Tao Bao92bdb5a2018-10-21 12:12:37 -0700135 if (mmapped == MAP_FAILED) {
Tao Baod096d7e2018-10-23 17:24:34 -0700136 perror("Failed to mmap()");
Tao Bao76be34c2017-02-07 13:30:19 -0800137 return nullptr;
138 }
Tao Bao92bdb5a2018-10-21 12:12:37 -0700139 surface->mmapped_buffer_ = static_cast<uint8_t*>(mmapped);
Kelvin Zhang90f783f2021-03-30 08:59:19 -0400140 printf("Framebuffer of size %llu allocated @ %p\n", create_dumb.size, surface->mmapped_buffer_);
Tao Bao76be34c2017-02-07 13:30:19 -0800141 return surface;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700142}
143
Tao Baod096d7e2018-10-23 17:24:34 -0700144void MinuiBackendDrm::DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc) {
145 if (crtc) {
146 drmModeSetCrtc(drm_fd, crtc->crtc_id,
147 0, // fb_id
148 0, 0, // x,y
149 nullptr, // connectors
150 0, // connector_count
151 nullptr); // mode
152 }
153}
154
155bool MinuiBackendDrm::DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc,
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800156 const std::unique_ptr<GRSurfaceDrm>& surface,
157 uint32_t* connector_id) {
Tao Baod096d7e2018-10-23 17:24:34 -0700158 if (drmModeSetCrtc(drm_fd, crtc->crtc_id, surface->fb_id, 0, 0, // x,y
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800159 connector_id, 1, // connector_count
160 &crtc->mode) != 0) {
161 fprintf(stderr, "Failed to drmModeSetCrtc(%d)\n", *connector_id);
Tao Baod096d7e2018-10-23 17:24:34 -0700162 return false;
163 }
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800164
Tao Baod096d7e2018-10-23 17:24:34 -0700165 return true;
166}
167
168void MinuiBackendDrm::Blank(bool blank) {
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800169 Blank(blank, DRM_MAIN);
170}
171
172void MinuiBackendDrm::Blank(bool blank, DrmConnector index) {
173 const auto* drmInterface = &drm[DRM_MAIN];
174
175 switch (index) {
176 case DRM_MAIN:
177 drmInterface = &drm[DRM_MAIN];
178 break;
179 case DRM_SEC:
180 drmInterface = &drm[DRM_SEC];
181 break;
182 default:
183 fprintf(stderr, "Invalid index: %d\n", index);
184 return;
185 }
186
187 if (!drmInterface->monitor_connector) {
188 fprintf(stderr, "Unsupported. index = %d\n", index);
189 return;
190 }
191
Tao Baod096d7e2018-10-23 17:24:34 -0700192 if (blank) {
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800193 DrmDisableCrtc(drm_fd, drmInterface->monitor_crtc);
Tao Baod096d7e2018-10-23 17:24:34 -0700194 } else {
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800195 DrmEnableCrtc(drm_fd, drmInterface->monitor_crtc,
196 drmInterface->GRSurfaceDrms[drmInterface->current_buffer],
197 &drmInterface->monitor_connector->connector_id);
198
199 active_display = index;
Tao Baod096d7e2018-10-23 17:24:34 -0700200 }
201}
202
Weizhung Dinge43c5032022-07-19 16:34:43 +0800203bool MinuiBackendDrm::HasMultipleConnectors() {
204 return (drm[DRM_SEC].GRSurfaceDrms[0] && drm[DRM_SEC].GRSurfaceDrms[1]);
205}
206
Tao Bao76be34c2017-02-07 13:30:19 -0800207static drmModeCrtc* find_crtc_for_connector(int fd, drmModeRes* resources,
208 drmModeConnector* connector) {
209 // Find the encoder. If we already have one, just use it.
210 drmModeEncoder* encoder;
211 if (connector->encoder_id) {
212 encoder = drmModeGetEncoder(fd, connector->encoder_id);
213 } else {
214 encoder = nullptr;
215 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700216
Tao Bao76be34c2017-02-07 13:30:19 -0800217 int32_t crtc;
218 if (encoder && encoder->crtc_id) {
219 crtc = encoder->crtc_id;
220 drmModeFreeEncoder(encoder);
221 return drmModeGetCrtc(fd, crtc);
222 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700223
Tao Bao76be34c2017-02-07 13:30:19 -0800224 // Didn't find anything, try to find a crtc and encoder combo.
225 crtc = -1;
226 for (int i = 0; i < connector->count_encoders; i++) {
227 encoder = drmModeGetEncoder(fd, connector->encoders[i]);
228
229 if (encoder) {
230 for (int j = 0; j < resources->count_crtcs; j++) {
231 if (!(encoder->possible_crtcs & (1 << j))) continue;
232 crtc = resources->crtcs[j];
233 break;
234 }
235 if (crtc >= 0) {
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700236 drmModeFreeEncoder(encoder);
237 return drmModeGetCrtc(fd, crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800238 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700239 }
Tao Bao76be34c2017-02-07 13:30:19 -0800240 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700241
Tao Bao76be34c2017-02-07 13:30:19 -0800242 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700243}
244
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800245std::vector<drmModeConnector*> find_used_connector_by_type(int fd, drmModeRes* resources,
246 unsigned type) {
247 std::vector<drmModeConnector*> drmConnectors;
Tao Bao76be34c2017-02-07 13:30:19 -0800248 for (int i = 0; i < resources->count_connectors; i++) {
249 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
250 if (connector) {
251 if ((connector->connector_type == type) && (connector->connection == DRM_MODE_CONNECTED) &&
252 (connector->count_modes > 0)) {
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800253 drmConnectors.push_back(connector);
254 } else {
255 drmModeFreeConnector(connector);
Tao Bao76be34c2017-02-07 13:30:19 -0800256 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700257 }
Tao Bao76be34c2017-02-07 13:30:19 -0800258 }
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800259 return drmConnectors;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700260}
261
Tao Bao76be34c2017-02-07 13:30:19 -0800262static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* resources) {
263 for (int i = 0; i < resources->count_connectors; i++) {
264 drmModeConnector* connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700265
Tao Bao76be34c2017-02-07 13:30:19 -0800266 connector = drmModeGetConnector(fd, resources->connectors[i]);
267 if (connector) {
268 if ((connector->count_modes > 0) && (connector->connection == DRM_MODE_CONNECTED))
269 return connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700270
Tao Bao76be34c2017-02-07 13:30:19 -0800271 drmModeFreeConnector(connector);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700272 }
Tao Bao76be34c2017-02-07 13:30:19 -0800273 }
274 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700275}
276
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800277bool MinuiBackendDrm::FindAndSetMonitor(int fd, drmModeRes* resources) {
Tao Bao76be34c2017-02-07 13:30:19 -0800278 /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */
279 static constexpr unsigned kConnectorPriority[] = {
280 DRM_MODE_CONNECTOR_LVDS,
281 DRM_MODE_CONNECTOR_eDP,
282 DRM_MODE_CONNECTOR_DSI,
283 };
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700284
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800285 std::vector<drmModeConnector*> drmConnectors;
286 for (int i = 0; i < arraysize(kConnectorPriority) && drmConnectors.size() < DRM_MAX; i++) {
287 auto connectors = find_used_connector_by_type(fd, resources, kConnectorPriority[i]);
288 for (auto connector : connectors) {
289 drmConnectors.push_back(connector);
290 if (drmConnectors.size() >= DRM_MAX) break;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700291 }
Tao Bao76be34c2017-02-07 13:30:19 -0800292 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700293
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800294 /* If we didn't find a connector, grab the first one that is connected. */
295 if (drmConnectors.empty()) {
296 drmModeConnector* connector = find_first_connected_connector(fd, resources);
297 if (connector) {
298 drmConnectors.push_back(connector);
299 }
300 }
301
302 for (int drm_index = 0; drm_index < drmConnectors.size(); drm_index++) {
303 drm[drm_index].monitor_connector = drmConnectors[drm_index];
304
305 drm[drm_index].selected_mode = 0;
306 for (int modes = 0; modes < drmConnectors[drm_index]->count_modes; modes++) {
307 printf("Display Mode %d resolution: %d x %d @ %d FPS\n", modes,
308 drmConnectors[drm_index]->modes[modes].hdisplay,
309 drmConnectors[drm_index]->modes[modes].vdisplay,
310 drmConnectors[drm_index]->modes[modes].vrefresh);
311 if (drmConnectors[drm_index]->modes[modes].type & DRM_MODE_TYPE_PREFERRED) {
312 printf("Choosing display mode #%d\n", modes);
313 drm[drm_index].selected_mode = modes;
314 break;
315 }
316 }
317 }
318
319 return drmConnectors.size() > 0;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700320}
321
Tao Bao557fa1f2017-02-07 12:51:00 -0800322void MinuiBackendDrm::DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) {
Tao Bao76be34c2017-02-07 13:30:19 -0800323 for (int i = 0; i < resources->count_connectors; i++) {
324 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
325 drmModeCrtc* crtc = find_crtc_for_connector(fd, resources, connector);
326 if (crtc->crtc_id != main_crtc->crtc_id) {
Tao Bao557fa1f2017-02-07 12:51:00 -0800327 DrmDisableCrtc(fd, crtc);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700328 }
Tao Bao76be34c2017-02-07 13:30:19 -0800329 drmModeFreeCrtc(crtc);
330 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700331}
332
Tao Bao557fa1f2017-02-07 12:51:00 -0800333GRSurface* MinuiBackendDrm::Init() {
Tao Bao76be34c2017-02-07 13:30:19 -0800334 drmModeRes* res = nullptr;
Tao Baod096d7e2018-10-23 17:24:34 -0700335 drm_fd = -1;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700336
Tao Bao76be34c2017-02-07 13:30:19 -0800337 /* Consider DRM devices in order. */
338 for (int i = 0; i < DRM_MAX_MINOR; i++) {
Tao Baod096d7e2018-10-23 17:24:34 -0700339 auto dev_name = android::base::StringPrintf(DRM_DEV_NAME, DRM_DIR_NAME, i);
Tao Baob5492432019-01-16 09:29:17 -0800340 android::base::unique_fd fd(open(dev_name.c_str(), O_RDWR | O_CLOEXEC));
Tao Baod096d7e2018-10-23 17:24:34 -0700341 if (fd == -1) continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700342
Tao Bao76be34c2017-02-07 13:30:19 -0800343 /* We need dumb buffers. */
Tao Baod096d7e2018-10-23 17:24:34 -0700344 if (uint64_t cap = 0; drmGetCap(fd.get(), DRM_CAP_DUMB_BUFFER, &cap) != 0 || cap == 0) {
Tao Bao76be34c2017-02-07 13:30:19 -0800345 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700346 }
347
Tao Baod096d7e2018-10-23 17:24:34 -0700348 res = drmModeGetResources(fd.get());
Tao Bao76be34c2017-02-07 13:30:19 -0800349 if (!res) {
Tao Bao76be34c2017-02-07 13:30:19 -0800350 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700351 }
352
Tao Bao76be34c2017-02-07 13:30:19 -0800353 /* Use this device if it has at least one connected monitor. */
354 if (res->count_crtcs > 0 && res->count_connectors > 0) {
Tao Baod096d7e2018-10-23 17:24:34 -0700355 if (find_first_connected_connector(fd.get(), res)) {
356 drm_fd = fd.release();
357 break;
358 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700359 }
360
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700361 drmModeFreeResources(res);
Tao Bao76be34c2017-02-07 13:30:19 -0800362 res = nullptr;
363 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700364
Tao Baod096d7e2018-10-23 17:24:34 -0700365 if (drm_fd == -1 || res == nullptr) {
366 perror("Failed to find/open a drm device");
Tao Bao76be34c2017-02-07 13:30:19 -0800367 return nullptr;
368 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700369
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800370 if (!FindAndSetMonitor(drm_fd, res)) {
371 fprintf(stderr, "Failed to find main monitor_connector\n");
Tao Bao76be34c2017-02-07 13:30:19 -0800372 drmModeFreeResources(res);
Tao Bao76be34c2017-02-07 13:30:19 -0800373 return nullptr;
374 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700375
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800376 for (int i = 0; i < DRM_MAX; i++) {
377 if (drm[i].monitor_connector) {
378 drm[i].monitor_crtc = find_crtc_for_connector(drm_fd, res, drm[i].monitor_connector);
379 if (!drm[i].monitor_crtc) {
380 fprintf(stderr, "Failed to find monitor_crtc, drm index=%d\n", i);
381 drmModeFreeResources(res);
382 return nullptr;
383 }
384
385 drm[i].monitor_crtc->mode = drm[i].monitor_connector->modes[drm[i].selected_mode];
386
387 int width = drm[i].monitor_crtc->mode.hdisplay;
388 int height = drm[i].monitor_crtc->mode.vdisplay;
389
390 drm[i].GRSurfaceDrms[0] = GRSurfaceDrm::Create(drm_fd, width, height);
391 drm[i].GRSurfaceDrms[1] = GRSurfaceDrm::Create(drm_fd, width, height);
392 if (!drm[i].GRSurfaceDrms[0] || !drm[i].GRSurfaceDrms[1]) {
393 fprintf(stderr, "Failed to create GRSurfaceDrm, drm index=%d\n", i);
394 drmModeFreeResources(res);
395 return nullptr;
396 }
397
398 drm[i].current_buffer = 0;
399 }
Tao Bao76be34c2017-02-07 13:30:19 -0800400 }
401
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800402 DisableNonMainCrtcs(drm_fd, res, drm[DRM_MAIN].monitor_crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800403
404 drmModeFreeResources(res);
405
Tianjie Xuccf00a22018-06-05 17:10:23 -0700406 // We will likely encounter errors in the backend functions (i.e. Flip) if EnableCrtc fails.
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800407 if (!DrmEnableCrtc(drm_fd, drm[DRM_MAIN].monitor_crtc, drm[DRM_MAIN].GRSurfaceDrms[1],
408 &drm[DRM_MAIN].monitor_connector->connector_id)) {
Tianjie Xuccf00a22018-06-05 17:10:23 -0700409 return nullptr;
410 }
Tao Bao76be34c2017-02-07 13:30:19 -0800411
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800412 return drm[DRM_MAIN].GRSurfaceDrms[0].get();
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700413}
414
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700415static void page_flip_complete(__unused int fd,
416 __unused unsigned int sequence,
417 __unused unsigned int tv_sec,
418 __unused unsigned int tv_usec,
419 void *user_data) {
420 *static_cast<bool*>(user_data) = false;
421}
422
Tao Bao557fa1f2017-02-07 12:51:00 -0800423GRSurface* MinuiBackendDrm::Flip() {
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800424 GRSurface* surface = NULL;
425 DrmInterface* current_drm = &drm[active_display];
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700426 bool ongoing_flip = true;
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800427
428 if (!current_drm->monitor_connector) {
429 fprintf(stderr, "Unsupported. active_display = %d\n", active_display);
430 return nullptr;
431 }
432
433 if (drmModePageFlip(drm_fd, current_drm->monitor_crtc->crtc_id,
434 current_drm->GRSurfaceDrms[current_drm->current_buffer]->fb_id,
Tao Baod096d7e2018-10-23 17:24:34 -0700435 DRM_MODE_PAGE_FLIP_EVENT, &ongoing_flip) != 0) {
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800436 fprintf(stderr, "Failed to drmModePageFlip, active_display=%d", active_display);
Tao Bao76be34c2017-02-07 13:30:19 -0800437 return nullptr;
438 }
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700439
440 while (ongoing_flip) {
441 struct pollfd fds = {
442 .fd = drm_fd,
443 .events = POLLIN
444 };
445
Tao Baod096d7e2018-10-23 17:24:34 -0700446 if (poll(&fds, 1, -1) == -1 || !(fds.revents & POLLIN)) {
447 perror("Failed to poll() on drm fd");
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700448 break;
449 }
450
451 drmEventContext evctx = {
452 .version = DRM_EVENT_CONTEXT_VERSION,
453 .page_flip_handler = page_flip_complete
454 };
455
Tao Baod096d7e2018-10-23 17:24:34 -0700456 if (drmHandleEvent(drm_fd, &evctx) != 0) {
457 perror("Failed to drmHandleEvent");
Jeremy Compostella955da1e2018-06-07 10:51:33 -0700458 break;
459 }
460 }
461
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800462 current_drm->current_buffer = 1 - current_drm->current_buffer;
463 surface = current_drm->GRSurfaceDrms[current_drm->current_buffer].get();
464 return surface;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700465}
466
Tao Bao557fa1f2017-02-07 12:51:00 -0800467MinuiBackendDrm::~MinuiBackendDrm() {
Weizhung Dingf4dfa1a2021-09-27 11:30:26 +0800468 for (int i = 0; i < DRM_MAX; i++) {
469 if (drm[i].monitor_connector) {
470 DrmDisableCrtc(drm_fd, drm[i].monitor_crtc);
471 drmModeFreeCrtc(drm[i].monitor_crtc);
472 drmModeFreeConnector(drm[i].monitor_connector);
473 }
474 }
Tao Bao76be34c2017-02-07 13:30:19 -0800475 close(drm_fd);
476 drm_fd = -1;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700477}