blob: 199f4d83cbc4cfc0af2092ed035bef2f33a20d8e [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
17#include <drm_fourcc.h>
18#include <fcntl.h>
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/cdefs.h>
23#include <sys/ioctl.h>
24#include <sys/mman.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <xf86drm.h>
28#include <xf86drmMode.h>
29
Tao Bao0ecbd762017-01-16 21:16:58 -080030#include "minui/minui.h"
Stéphane Marchesin1a92c442015-06-29 20:05:48 -070031#include "graphics.h"
32
33#define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A)))
34
35struct drm_surface {
36 GRSurface base;
37 uint32_t fb_id;
38 uint32_t handle;
39};
40
41static drm_surface *drm_surfaces[2];
42static int current_buffer;
43
44static drmModeCrtc *main_monitor_crtc;
45static drmModeConnector *main_monitor_connector;
46
47static int drm_fd = -1;
48
49static void drm_disable_crtc(int drm_fd, drmModeCrtc *crtc) {
50 if (crtc) {
51 drmModeSetCrtc(drm_fd, crtc->crtc_id,
52 0, // fb_id
53 0, 0, // x,y
54 NULL, // connectors
55 0, // connector_count
56 NULL); // mode
57 }
58}
59
60static void drm_enable_crtc(int drm_fd, drmModeCrtc *crtc,
61 struct drm_surface *surface) {
62 int32_t ret;
63
64 ret = drmModeSetCrtc(drm_fd, crtc->crtc_id,
65 surface->fb_id,
66 0, 0, // x,y
67 &main_monitor_connector->connector_id,
68 1, // connector_count
69 &main_monitor_crtc->mode);
70
71 if (ret)
72 printf("drmModeSetCrtc failed ret=%d\n", ret);
73}
74
75static void drm_blank(minui_backend* backend __unused, bool blank) {
76 if (blank)
77 drm_disable_crtc(drm_fd, main_monitor_crtc);
78 else
79 drm_enable_crtc(drm_fd, main_monitor_crtc,
80 drm_surfaces[current_buffer]);
81}
82
83static void drm_destroy_surface(struct drm_surface *surface) {
84 struct drm_gem_close gem_close;
85 int ret;
86
87 if(!surface)
88 return;
89
90 if (surface->base.data)
91 munmap(surface->base.data,
92 surface->base.row_bytes * surface->base.height);
93
94 if (surface->fb_id) {
95 ret = drmModeRmFB(drm_fd, surface->fb_id);
96 if (ret)
97 printf("drmModeRmFB failed ret=%d\n", ret);
98 }
99
100 if (surface->handle) {
101 memset(&gem_close, 0, sizeof(gem_close));
102 gem_close.handle = surface->handle;
103
104 ret = drmIoctl(drm_fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
105 if (ret)
106 printf("DRM_IOCTL_GEM_CLOSE failed ret=%d\n", ret);
107 }
108
109 free(surface);
110}
111
112static int drm_format_to_bpp(uint32_t format) {
113 switch(format) {
114 case DRM_FORMAT_ABGR8888:
115 case DRM_FORMAT_BGRA8888:
116 case DRM_FORMAT_RGBX8888:
117 case DRM_FORMAT_BGRX8888:
118 case DRM_FORMAT_XBGR8888:
119 case DRM_FORMAT_XRGB8888:
120 return 32;
121 case DRM_FORMAT_RGB565:
122 return 16;
123 default:
124 printf("Unknown format %d\n", format);
125 return 32;
126 }
127}
128
129static drm_surface *drm_create_surface(int width, int height) {
130 struct drm_surface *surface;
131 struct drm_mode_create_dumb create_dumb;
132 uint32_t format;
133 int ret;
134
135 surface = (struct drm_surface*)calloc(1, sizeof(*surface));
136 if (!surface) {
137 printf("Can't allocate memory\n");
138 return NULL;
139 }
140
141#if defined(RECOVERY_ABGR)
142 format = DRM_FORMAT_RGBA8888;
143#elif defined(RECOVERY_BGRA)
144 format = DRM_FORMAT_ARGB8888;
145#elif defined(RECOVERY_RGBX)
146 format = DRM_FORMAT_XBGR8888;
147#else
148 format = DRM_FORMAT_RGB565;
149#endif
150
151 memset(&create_dumb, 0, sizeof(create_dumb));
152 create_dumb.height = height;
153 create_dumb.width = width;
154 create_dumb.bpp = drm_format_to_bpp(format);
155 create_dumb.flags = 0;
156
157 ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
158 if (ret) {
159 printf("DRM_IOCTL_MODE_CREATE_DUMB failed ret=%d\n",ret);
160 drm_destroy_surface(surface);
161 return NULL;
162 }
163 surface->handle = create_dumb.handle;
164
165 uint32_t handles[4], pitches[4], offsets[4];
166
167 handles[0] = surface->handle;
168 pitches[0] = create_dumb.pitch;
169 offsets[0] = 0;
170
171 ret = drmModeAddFB2(drm_fd, width, height,
172 format, handles, pitches, offsets,
173 &(surface->fb_id), 0);
174 if (ret) {
175 printf("drmModeAddFB2 failed ret=%d\n", ret);
176 drm_destroy_surface(surface);
177 return NULL;
178 }
179
180 struct drm_mode_map_dumb map_dumb;
181 memset(&map_dumb, 0, sizeof(map_dumb));
182 map_dumb.handle = create_dumb.handle;
183 ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
184 if (ret) {
185 printf("DRM_IOCTL_MODE_MAP_DUMB failed ret=%d\n",ret);
186 drm_destroy_surface(surface);
187 return NULL;;
188 }
189
190 surface->base.height = height;
191 surface->base.width = width;
192 surface->base.row_bytes = create_dumb.pitch;
193 surface->base.pixel_bytes = create_dumb.bpp / 8;
194 surface->base.data = (unsigned char*)
195 mmap(NULL,
196 surface->base.height * surface->base.row_bytes,
197 PROT_READ | PROT_WRITE, MAP_SHARED,
198 drm_fd, map_dumb.offset);
199 if (surface->base.data == MAP_FAILED) {
200 perror("mmap() failed");
201 drm_destroy_surface(surface);
202 return NULL;
203 }
204
205 return surface;
206}
207
208static drmModeCrtc *find_crtc_for_connector(int fd,
209 drmModeRes *resources,
210 drmModeConnector *connector) {
211 int i, j;
212 drmModeEncoder *encoder;
213 int32_t crtc;
214
215 /*
216 * Find the encoder. If we already have one, just use it.
217 */
218 if (connector->encoder_id)
219 encoder = drmModeGetEncoder(fd, connector->encoder_id);
220 else
221 encoder = NULL;
222
223 if (encoder && encoder->crtc_id) {
224 crtc = encoder->crtc_id;
225 drmModeFreeEncoder(encoder);
226 return drmModeGetCrtc(fd, crtc);
227 }
228
229 /*
230 * Didn't find anything, try to find a crtc and encoder combo.
231 */
232 crtc = -1;
233 for (i = 0; i < connector->count_encoders; i++) {
234 encoder = drmModeGetEncoder(fd, connector->encoders[i]);
235
236 if (encoder) {
237 for (j = 0; j < resources->count_crtcs; j++) {
238 if (!(encoder->possible_crtcs & (1 << j)))
239 continue;
240 crtc = resources->crtcs[j];
241 break;
242 }
243 if (crtc >= 0) {
244 drmModeFreeEncoder(encoder);
245 return drmModeGetCrtc(fd, crtc);
246 }
247 }
248 }
249
250 return NULL;
251}
252
253static drmModeConnector *find_used_connector_by_type(int fd,
254 drmModeRes *resources,
255 unsigned type) {
256 int i;
257 for (i = 0; i < resources->count_connectors; i++) {
258 drmModeConnector *connector;
259
260 connector = drmModeGetConnector(fd, resources->connectors[i]);
261 if (connector) {
262 if ((connector->connector_type == type) &&
263 (connector->connection == DRM_MODE_CONNECTED) &&
264 (connector->count_modes > 0))
265 return connector;
266
267 drmModeFreeConnector(connector);
268 }
269 }
270 return NULL;
271}
272
273static drmModeConnector *find_first_connected_connector(int fd,
274 drmModeRes *resources) {
275 int i;
276 for (i = 0; i < resources->count_connectors; i++) {
277 drmModeConnector *connector;
278
279 connector = drmModeGetConnector(fd, resources->connectors[i]);
280 if (connector) {
281 if ((connector->count_modes > 0) &&
282 (connector->connection == DRM_MODE_CONNECTED))
283 return connector;
284
285 drmModeFreeConnector(connector);
286 }
287 }
288 return NULL;
289}
290
291static drmModeConnector *find_main_monitor(int fd, drmModeRes *resources,
292 uint32_t *mode_index) {
293 unsigned i = 0;
294 int modes;
295 /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */
296 unsigned kConnectorPriority[] = {
297 DRM_MODE_CONNECTOR_LVDS,
298 DRM_MODE_CONNECTOR_eDP,
299 DRM_MODE_CONNECTOR_DSI,
300 };
301
302 drmModeConnector *main_monitor_connector = NULL;
303 do {
304 main_monitor_connector = find_used_connector_by_type(fd,
305 resources,
306 kConnectorPriority[i]);
307 i++;
308 } while (!main_monitor_connector && i < ARRAY_SIZE(kConnectorPriority));
309
310 /* If we didn't find a connector, grab the first one that is connected. */
311 if (!main_monitor_connector)
312 main_monitor_connector =
313 find_first_connected_connector(fd, resources);
314
315 /* If we still didn't find a connector, give up and return. */
316 if (!main_monitor_connector)
317 return NULL;
318
319 *mode_index = 0;
320 for (modes = 0; modes < main_monitor_connector->count_modes; modes++) {
321 if (main_monitor_connector->modes[modes].type &
322 DRM_MODE_TYPE_PREFERRED) {
323 *mode_index = modes;
324 break;
325 }
326 }
327
328 return main_monitor_connector;
329}
330
331static void disable_non_main_crtcs(int fd,
332 drmModeRes *resources,
333 drmModeCrtc* main_crtc) {
334 int i;
335 drmModeCrtc* crtc;
336
337 for (i = 0; i < resources->count_connectors; i++) {
338 drmModeConnector *connector;
339
340 connector = drmModeGetConnector(fd, resources->connectors[i]);
341 crtc = find_crtc_for_connector(fd, resources, connector);
342 if (crtc->crtc_id != main_crtc->crtc_id)
343 drm_disable_crtc(fd, crtc);
344 drmModeFreeCrtc(crtc);
345 }
346}
347
348static GRSurface* drm_init(minui_backend* backend __unused) {
349 drmModeRes *res = NULL;
350 uint32_t selected_mode;
351 char *dev_name;
352 int width, height;
353 int ret, i;
354
355 /* Consider DRM devices in order. */
356 for (i = 0; i < DRM_MAX_MINOR; i++) {
357 uint64_t cap = 0;
358
359 ret = asprintf(&dev_name, DRM_DEV_NAME, DRM_DIR_NAME, i);
360 if (ret < 0)
361 continue;
362
363 drm_fd = open(dev_name, O_RDWR, 0);
364 free(dev_name);
365 if (drm_fd < 0)
366 continue;
367
368 /* We need dumb buffers. */
369 ret = drmGetCap(drm_fd, DRM_CAP_DUMB_BUFFER, &cap);
370 if (ret || cap == 0) {
371 close(drm_fd);
372 continue;
373 }
374
375 res = drmModeGetResources(drm_fd);
376 if (!res) {
377 close(drm_fd);
378 continue;
379 }
380
381 /* Use this device if it has at least one connected monitor. */
382 if (res->count_crtcs > 0 && res->count_connectors > 0)
383 if (find_first_connected_connector(drm_fd, res))
384 break;
385
386 drmModeFreeResources(res);
387 close(drm_fd);
388 res = NULL;
389 }
390
391 if (drm_fd < 0 || res == NULL) {
392 perror("cannot find/open a drm device");
393 return NULL;
394 }
395
396 main_monitor_connector = find_main_monitor(drm_fd,
397 res, &selected_mode);
398
399 if (!main_monitor_connector) {
400 printf("main_monitor_connector not found\n");
401 drmModeFreeResources(res);
402 close(drm_fd);
403 return NULL;
404 }
405
406 main_monitor_crtc = find_crtc_for_connector(drm_fd, res,
407 main_monitor_connector);
408
409 if (!main_monitor_crtc) {
410 printf("main_monitor_crtc not found\n");
411 drmModeFreeResources(res);
412 close(drm_fd);
413 return NULL;
414 }
415
416 disable_non_main_crtcs(drm_fd,
417 res, main_monitor_crtc);
418
419 main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode];
420
421 width = main_monitor_crtc->mode.hdisplay;
422 height = main_monitor_crtc->mode.vdisplay;
423
424 drmModeFreeResources(res);
425
426 drm_surfaces[0] = drm_create_surface(width, height);
427 drm_surfaces[1] = drm_create_surface(width, height);
428 if (!drm_surfaces[0] || !drm_surfaces[1]) {
429 drm_destroy_surface(drm_surfaces[0]);
430 drm_destroy_surface(drm_surfaces[1]);
431 drmModeFreeResources(res);
432 close(drm_fd);
433 return NULL;
434 }
435
436 current_buffer = 0;
437
438 drm_enable_crtc(drm_fd, main_monitor_crtc, drm_surfaces[1]);
439
440 return &(drm_surfaces[0]->base);
441}
442
443static GRSurface* drm_flip(minui_backend* backend __unused) {
444 int ret;
445
446 ret = drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id,
447 drm_surfaces[current_buffer]->fb_id, 0, NULL);
448 if (ret < 0) {
449 printf("drmModePageFlip failed ret=%d\n", ret);
450 return NULL;
451 }
452 current_buffer = 1 - current_buffer;
453 return &(drm_surfaces[current_buffer]->base);
454}
455
456static void drm_exit(minui_backend* backend __unused) {
457 drm_disable_crtc(drm_fd, main_monitor_crtc);
458 drm_destroy_surface(drm_surfaces[0]);
459 drm_destroy_surface(drm_surfaces[1]);
460 drmModeFreeCrtc(main_monitor_crtc);
461 drmModeFreeConnector(main_monitor_connector);
462 close(drm_fd);
463 drm_fd = -1;
464}
465
466static minui_backend drm_backend = {
467 .init = drm_init,
468 .flip = drm_flip,
469 .blank = drm_blank,
470 .exit = drm_exit,
471};
472
473minui_backend* open_drm() {
474 return &drm_backend;
475}