blob: 2eeff5800cf07718735794468c888dbac2853f98 [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
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070017#include <fcntl.h>
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070018#include <stdio.h>
19#include <stdlib.h>
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070020#include <sys/mman.h>
21#include <sys/types.h>
22#include <unistd.h>
Tao Bao76be34c2017-02-07 13:30:19 -080023
24#include <drm_fourcc.h>
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070025#include <xf86drm.h>
26#include <xf86drmMode.h>
27
Tao Bao0ecbd762017-01-16 21:16:58 -080028#include "minui/minui.h"
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070029#include "graphics.h"
30
31#define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A)))
32
33struct drm_surface {
Tao Bao76be34c2017-02-07 13:30:19 -080034 GRSurface base;
35 uint32_t fb_id;
36 uint32_t handle;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070037};
38
39static drm_surface *drm_surfaces[2];
40static int current_buffer;
41
42static drmModeCrtc *main_monitor_crtc;
43static drmModeConnector *main_monitor_connector;
44
45static int drm_fd = -1;
46
47static void drm_disable_crtc(int drm_fd, drmModeCrtc *crtc) {
Tao Bao76be34c2017-02-07 13:30:19 -080048 if (crtc) {
49 drmModeSetCrtc(drm_fd, crtc->crtc_id,
50 0, // fb_id
51 0, 0, // x,y
52 nullptr, // connectors
53 0, // connector_count
54 nullptr); // mode
55 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070056}
57
Tao Bao76be34c2017-02-07 13:30:19 -080058static void drm_enable_crtc(int drm_fd, drmModeCrtc* crtc, struct drm_surface* surface) {
59 int32_t ret = drmModeSetCrtc(drm_fd, crtc->crtc_id, surface->fb_id, 0, 0, // x,y
60 &main_monitor_connector->connector_id,
61 1, // connector_count
62 &main_monitor_crtc->mode);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070063
Tao Bao76be34c2017-02-07 13:30:19 -080064 if (ret) {
65 printf("drmModeSetCrtc failed ret=%d\n", ret);
66 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070067}
68
69static void drm_blank(minui_backend* backend __unused, bool blank) {
Tao Bao76be34c2017-02-07 13:30:19 -080070 if (blank) {
71 drm_disable_crtc(drm_fd, main_monitor_crtc);
72 } else {
73 drm_enable_crtc(drm_fd, main_monitor_crtc, drm_surfaces[current_buffer]);
74 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070075}
76
77static void drm_destroy_surface(struct drm_surface *surface) {
Tao Bao76be34c2017-02-07 13:30:19 -080078 if (!surface) return;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070079
Tao Bao76be34c2017-02-07 13:30:19 -080080 if (surface->base.data) {
81 munmap(surface->base.data, surface->base.row_bytes * surface->base.height);
82 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070083
Tao Bao76be34c2017-02-07 13:30:19 -080084 if (surface->fb_id) {
85 int ret = drmModeRmFB(drm_fd, surface->fb_id);
86 if (ret) {
87 printf("drmModeRmFB 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 Bao76be34c2017-02-07 13:30:19 -080091 if (surface->handle) {
92 drm_gem_close gem_close = {};
93 gem_close.handle = surface->handle;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070094
Tao Bao76be34c2017-02-07 13:30:19 -080095 int ret = drmIoctl(drm_fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
96 if (ret) {
97 printf("DRM_IOCTL_GEM_CLOSE failed ret=%d\n", ret);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070098 }
Tao Bao76be34c2017-02-07 13:30:19 -080099 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700100
Tao Bao76be34c2017-02-07 13:30:19 -0800101 free(surface);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700102}
103
104static int drm_format_to_bpp(uint32_t format) {
Tao Bao76be34c2017-02-07 13:30:19 -0800105 switch (format) {
106 case DRM_FORMAT_ABGR8888:
107 case DRM_FORMAT_BGRA8888:
108 case DRM_FORMAT_RGBX8888:
109 case DRM_FORMAT_BGRX8888:
110 case DRM_FORMAT_XBGR8888:
111 case DRM_FORMAT_XRGB8888:
112 return 32;
113 case DRM_FORMAT_RGB565:
114 return 16;
115 default:
116 printf("Unknown format %d\n", format);
117 return 32;
118 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700119}
120
121static drm_surface *drm_create_surface(int width, int height) {
Tao Bao76be34c2017-02-07 13:30:19 -0800122 drm_surface* surface = static_cast<drm_surface*>(calloc(1, sizeof(*surface)));
123 if (!surface) {
124 printf("Can't allocate memory\n");
125 return nullptr;
126 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700127
Tao Bao76be34c2017-02-07 13:30:19 -0800128 uint32_t format;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700129#if defined(RECOVERY_ABGR)
Tao Bao76be34c2017-02-07 13:30:19 -0800130 format = DRM_FORMAT_RGBA8888;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700131#elif defined(RECOVERY_BGRA)
Tao Bao76be34c2017-02-07 13:30:19 -0800132 format = DRM_FORMAT_ARGB8888;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700133#elif defined(RECOVERY_RGBX)
Tao Bao76be34c2017-02-07 13:30:19 -0800134 format = DRM_FORMAT_XBGR8888;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700135#else
Tao Bao76be34c2017-02-07 13:30:19 -0800136 format = DRM_FORMAT_RGB565;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700137#endif
138
Tao Bao76be34c2017-02-07 13:30:19 -0800139 drm_mode_create_dumb create_dumb = {};
140 create_dumb.height = height;
141 create_dumb.width = width;
142 create_dumb.bpp = drm_format_to_bpp(format);
143 create_dumb.flags = 0;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700144
Tao Bao76be34c2017-02-07 13:30:19 -0800145 int ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
146 if (ret) {
147 printf("DRM_IOCTL_MODE_CREATE_DUMB failed ret=%d\n", ret);
148 drm_destroy_surface(surface);
149 return nullptr;
150 }
151 surface->handle = create_dumb.handle;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700152
Tao Bao76be34c2017-02-07 13:30:19 -0800153 uint32_t handles[4], pitches[4], offsets[4];
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700154
Tao Bao76be34c2017-02-07 13:30:19 -0800155 handles[0] = surface->handle;
156 pitches[0] = create_dumb.pitch;
157 offsets[0] = 0;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700158
Tao Bao76be34c2017-02-07 13:30:19 -0800159 ret =
160 drmModeAddFB2(drm_fd, width, height, format, handles, pitches, offsets, &(surface->fb_id), 0);
161 if (ret) {
162 printf("drmModeAddFB2 failed ret=%d\n", ret);
163 drm_destroy_surface(surface);
164 return nullptr;
165 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700166
Tao Bao76be34c2017-02-07 13:30:19 -0800167 drm_mode_map_dumb map_dumb = {};
168 map_dumb.handle = create_dumb.handle;
169 ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
170 if (ret) {
171 printf("DRM_IOCTL_MODE_MAP_DUMB failed ret=%d\n", ret);
172 drm_destroy_surface(surface);
173 return nullptr;
174 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700175
Tao Bao76be34c2017-02-07 13:30:19 -0800176 surface->base.height = height;
177 surface->base.width = width;
178 surface->base.row_bytes = create_dumb.pitch;
179 surface->base.pixel_bytes = create_dumb.bpp / 8;
180 surface->base.data =
181 static_cast<unsigned char*>(mmap(nullptr, surface->base.height * surface->base.row_bytes,
182 PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, map_dumb.offset));
183 if (surface->base.data == MAP_FAILED) {
184 perror("mmap() failed");
185 drm_destroy_surface(surface);
186 return nullptr;
187 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700188
Tao Bao76be34c2017-02-07 13:30:19 -0800189 return surface;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700190}
191
Tao Bao76be34c2017-02-07 13:30:19 -0800192static drmModeCrtc* find_crtc_for_connector(int fd, drmModeRes* resources,
193 drmModeConnector* connector) {
194 // Find the encoder. If we already have one, just use it.
195 drmModeEncoder* encoder;
196 if (connector->encoder_id) {
197 encoder = drmModeGetEncoder(fd, connector->encoder_id);
198 } else {
199 encoder = nullptr;
200 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700201
Tao Bao76be34c2017-02-07 13:30:19 -0800202 int32_t crtc;
203 if (encoder && encoder->crtc_id) {
204 crtc = encoder->crtc_id;
205 drmModeFreeEncoder(encoder);
206 return drmModeGetCrtc(fd, crtc);
207 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700208
Tao Bao76be34c2017-02-07 13:30:19 -0800209 // Didn't find anything, try to find a crtc and encoder combo.
210 crtc = -1;
211 for (int i = 0; i < connector->count_encoders; i++) {
212 encoder = drmModeGetEncoder(fd, connector->encoders[i]);
213
214 if (encoder) {
215 for (int j = 0; j < resources->count_crtcs; j++) {
216 if (!(encoder->possible_crtcs & (1 << j))) continue;
217 crtc = resources->crtcs[j];
218 break;
219 }
220 if (crtc >= 0) {
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700221 drmModeFreeEncoder(encoder);
222 return drmModeGetCrtc(fd, crtc);
Tao Bao76be34c2017-02-07 13:30:19 -0800223 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700224 }
Tao Bao76be34c2017-02-07 13:30:19 -0800225 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700226
Tao Bao76be34c2017-02-07 13:30:19 -0800227 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700228}
229
Tao Bao76be34c2017-02-07 13:30:19 -0800230static drmModeConnector* find_used_connector_by_type(int fd, drmModeRes* resources, unsigned type) {
231 for (int i = 0; i < resources->count_connectors; i++) {
232 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
233 if (connector) {
234 if ((connector->connector_type == type) && (connector->connection == DRM_MODE_CONNECTED) &&
235 (connector->count_modes > 0)) {
236 return connector;
237 }
238 drmModeFreeConnector(connector);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700239 }
Tao Bao76be34c2017-02-07 13:30:19 -0800240 }
241 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700242}
243
Tao Bao76be34c2017-02-07 13:30:19 -0800244static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* resources) {
245 for (int i = 0; i < resources->count_connectors; i++) {
246 drmModeConnector* connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700247
Tao Bao76be34c2017-02-07 13:30:19 -0800248 connector = drmModeGetConnector(fd, resources->connectors[i]);
249 if (connector) {
250 if ((connector->count_modes > 0) && (connector->connection == DRM_MODE_CONNECTED))
251 return connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700252
Tao Bao76be34c2017-02-07 13:30:19 -0800253 drmModeFreeConnector(connector);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700254 }
Tao Bao76be34c2017-02-07 13:30:19 -0800255 }
256 return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700257}
258
Tao Bao76be34c2017-02-07 13:30:19 -0800259static drmModeConnector* find_main_monitor(int fd, drmModeRes* resources, uint32_t* mode_index) {
260 /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */
261 static constexpr unsigned kConnectorPriority[] = {
262 DRM_MODE_CONNECTOR_LVDS,
263 DRM_MODE_CONNECTOR_eDP,
264 DRM_MODE_CONNECTOR_DSI,
265 };
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700266
Tao Bao76be34c2017-02-07 13:30:19 -0800267 drmModeConnector* main_monitor_connector = nullptr;
268 unsigned i = 0;
269 do {
270 main_monitor_connector = find_used_connector_by_type(fd, resources, kConnectorPriority[i]);
271 i++;
272 } while (!main_monitor_connector && i < ARRAY_SIZE(kConnectorPriority));
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700273
Tao Bao76be34c2017-02-07 13:30:19 -0800274 /* If we didn't find a connector, grab the first one that is connected. */
275 if (!main_monitor_connector) {
276 main_monitor_connector = find_first_connected_connector(fd, resources);
277 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700278
Tao Bao76be34c2017-02-07 13:30:19 -0800279 /* If we still didn't find a connector, give up and return. */
280 if (!main_monitor_connector) return nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700281
Tao Bao76be34c2017-02-07 13:30:19 -0800282 *mode_index = 0;
283 for (int modes = 0; modes < main_monitor_connector->count_modes; modes++) {
284 if (main_monitor_connector->modes[modes].type & DRM_MODE_TYPE_PREFERRED) {
285 *mode_index = modes;
286 break;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700287 }
Tao Bao76be34c2017-02-07 13:30:19 -0800288 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700289
Tao Bao76be34c2017-02-07 13:30:19 -0800290 return main_monitor_connector;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700291}
292
Tao Bao76be34c2017-02-07 13:30:19 -0800293static void disable_non_main_crtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) {
294 for (int i = 0; i < resources->count_connectors; i++) {
295 drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]);
296 drmModeCrtc* crtc = find_crtc_for_connector(fd, resources, connector);
297 if (crtc->crtc_id != main_crtc->crtc_id) {
298 drm_disable_crtc(fd, crtc);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700299 }
Tao Bao76be34c2017-02-07 13:30:19 -0800300 drmModeFreeCrtc(crtc);
301 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700302}
303
304static GRSurface* drm_init(minui_backend* backend __unused) {
Tao Bao76be34c2017-02-07 13:30:19 -0800305 drmModeRes* res = nullptr;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700306
Tao Bao76be34c2017-02-07 13:30:19 -0800307 /* Consider DRM devices in order. */
308 for (int i = 0; i < DRM_MAX_MINOR; i++) {
309 char* dev_name;
310 int ret = asprintf(&dev_name, DRM_DEV_NAME, DRM_DIR_NAME, i);
311 if (ret < 0) continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700312
Tao Bao76be34c2017-02-07 13:30:19 -0800313 drm_fd = open(dev_name, O_RDWR, 0);
314 free(dev_name);
315 if (drm_fd < 0) continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700316
Tao Bao76be34c2017-02-07 13:30:19 -0800317 uint64_t cap = 0;
318 /* We need dumb buffers. */
319 ret = drmGetCap(drm_fd, DRM_CAP_DUMB_BUFFER, &cap);
320 if (ret || cap == 0) {
321 close(drm_fd);
322 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700323 }
324
Tao Bao76be34c2017-02-07 13:30:19 -0800325 res = drmModeGetResources(drm_fd);
326 if (!res) {
327 close(drm_fd);
328 continue;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700329 }
330
Tao Bao76be34c2017-02-07 13:30:19 -0800331 /* Use this device if it has at least one connected monitor. */
332 if (res->count_crtcs > 0 && res->count_connectors > 0) {
333 if (find_first_connected_connector(drm_fd, res)) break;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700334 }
335
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700336 drmModeFreeResources(res);
Tao Bao76be34c2017-02-07 13:30:19 -0800337 close(drm_fd);
338 res = nullptr;
339 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700340
Tao Bao76be34c2017-02-07 13:30:19 -0800341 if (drm_fd < 0 || res == nullptr) {
342 perror("cannot find/open a drm device");
343 return nullptr;
344 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700345
Tao Bao76be34c2017-02-07 13:30:19 -0800346 uint32_t selected_mode;
347 main_monitor_connector = find_main_monitor(drm_fd, res, &selected_mode);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700348
Tao Bao76be34c2017-02-07 13:30:19 -0800349 if (!main_monitor_connector) {
350 printf("main_monitor_connector not found\n");
351 drmModeFreeResources(res);
352 close(drm_fd);
353 return nullptr;
354 }
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700355
Tao Bao76be34c2017-02-07 13:30:19 -0800356 main_monitor_crtc = find_crtc_for_connector(drm_fd, res, main_monitor_connector);
357
358 if (!main_monitor_crtc) {
359 printf("main_monitor_crtc not found\n");
360 drmModeFreeResources(res);
361 close(drm_fd);
362 return nullptr;
363 }
364
365 disable_non_main_crtcs(drm_fd, res, main_monitor_crtc);
366
367 main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode];
368
369 int width = main_monitor_crtc->mode.hdisplay;
370 int height = main_monitor_crtc->mode.vdisplay;
371
372 drmModeFreeResources(res);
373
374 drm_surfaces[0] = drm_create_surface(width, height);
375 drm_surfaces[1] = drm_create_surface(width, height);
376 if (!drm_surfaces[0] || !drm_surfaces[1]) {
377 drm_destroy_surface(drm_surfaces[0]);
378 drm_destroy_surface(drm_surfaces[1]);
379 drmModeFreeResources(res);
380 close(drm_fd);
381 return nullptr;
382 }
383
384 current_buffer = 0;
385
386 drm_enable_crtc(drm_fd, main_monitor_crtc, drm_surfaces[1]);
387
388 return &(drm_surfaces[0]->base);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700389}
390
391static GRSurface* drm_flip(minui_backend* backend __unused) {
Tao Bao76be34c2017-02-07 13:30:19 -0800392 int ret = drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id, drm_surfaces[current_buffer]->fb_id,
393 0, nullptr);
394 if (ret < 0) {
395 printf("drmModePageFlip failed ret=%d\n", ret);
396 return nullptr;
397 }
398 current_buffer = 1 - current_buffer;
399 return &(drm_surfaces[current_buffer]->base);
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700400}
401
402static void drm_exit(minui_backend* backend __unused) {
Tao Bao76be34c2017-02-07 13:30:19 -0800403 drm_disable_crtc(drm_fd, main_monitor_crtc);
404 drm_destroy_surface(drm_surfaces[0]);
405 drm_destroy_surface(drm_surfaces[1]);
406 drmModeFreeCrtc(main_monitor_crtc);
407 drmModeFreeConnector(main_monitor_connector);
408 close(drm_fd);
409 drm_fd = -1;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700410}
411
412static minui_backend drm_backend = {
Tao Bao76be34c2017-02-07 13:30:19 -0800413 .init = drm_init,
414 .flip = drm_flip,
415 .blank = drm_blank,
416 .exit = drm_exit,
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700417};
418
419minui_backend* open_drm() {
Tao Bao76be34c2017-02-07 13:30:19 -0800420 return &drm_backend;
Stéphane Marchesin1a92c442015-06-29 20:05:48 -0700421}