Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 17 | #include <fcntl.h> |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 20 | #include <sys/mman.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 23 | |
| 24 | #include <drm_fourcc.h> |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 25 | #include <xf86drm.h> |
| 26 | #include <xf86drmMode.h> |
| 27 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 28 | #include "minui/minui.h" |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 29 | #include "graphics.h" |
| 30 | |
| 31 | #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A))) |
| 32 | |
| 33 | struct drm_surface { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 34 | GRSurface base; |
| 35 | uint32_t fb_id; |
| 36 | uint32_t handle; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static drm_surface *drm_surfaces[2]; |
| 40 | static int current_buffer; |
| 41 | |
| 42 | static drmModeCrtc *main_monitor_crtc; |
| 43 | static drmModeConnector *main_monitor_connector; |
| 44 | |
| 45 | static int drm_fd = -1; |
| 46 | |
| 47 | static void drm_disable_crtc(int drm_fd, drmModeCrtc *crtc) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 48 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 58 | static 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 63 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 64 | if (ret) { |
| 65 | printf("drmModeSetCrtc failed ret=%d\n", ret); |
| 66 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void drm_blank(minui_backend* backend __unused, bool blank) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 70 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void drm_destroy_surface(struct drm_surface *surface) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 78 | if (!surface) return; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 79 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 80 | if (surface->base.data) { |
| 81 | munmap(surface->base.data, surface->base.row_bytes * surface->base.height); |
| 82 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 83 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 84 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 88 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 89 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 90 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 91 | if (surface->handle) { |
| 92 | drm_gem_close gem_close = {}; |
| 93 | gem_close.handle = surface->handle; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 94 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 95 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 98 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 99 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 100 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 101 | free(surface); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static int drm_format_to_bpp(uint32_t format) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 105 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static drm_surface *drm_create_surface(int width, int height) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 122 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 127 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 128 | uint32_t format; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 129 | #if defined(RECOVERY_ABGR) |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 130 | format = DRM_FORMAT_RGBA8888; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 131 | #elif defined(RECOVERY_BGRA) |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 132 | format = DRM_FORMAT_ARGB8888; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 133 | #elif defined(RECOVERY_RGBX) |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 134 | format = DRM_FORMAT_XBGR8888; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 135 | #else |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 136 | format = DRM_FORMAT_RGB565; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 137 | #endif |
| 138 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 139 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 144 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 145 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 152 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 153 | uint32_t handles[4], pitches[4], offsets[4]; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 154 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 155 | handles[0] = surface->handle; |
| 156 | pitches[0] = create_dumb.pitch; |
| 157 | offsets[0] = 0; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 158 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 159 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 166 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 167 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 175 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 176 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 188 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 189 | return surface; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 192 | static 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 201 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 202 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 208 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 209 | // 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 221 | drmModeFreeEncoder(encoder); |
| 222 | return drmModeGetCrtc(fd, crtc); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 223 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 224 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 225 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 226 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 227 | return nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 230 | static 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 239 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 240 | } |
| 241 | return nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 244 | static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* resources) { |
| 245 | for (int i = 0; i < resources->count_connectors; i++) { |
| 246 | drmModeConnector* connector; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 247 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 248 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 252 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 253 | drmModeFreeConnector(connector); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 254 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 255 | } |
| 256 | return nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 259 | static 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 266 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 267 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 273 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 274 | /* 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 278 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 279 | /* If we still didn't find a connector, give up and return. */ |
| 280 | if (!main_monitor_connector) return nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 281 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 282 | *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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 287 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 288 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 289 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 290 | return main_monitor_connector; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 293 | static 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 299 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 300 | drmModeFreeCrtc(crtc); |
| 301 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static GRSurface* drm_init(minui_backend* backend __unused) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 305 | drmModeRes* res = nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 306 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 307 | /* 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 312 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 313 | drm_fd = open(dev_name, O_RDWR, 0); |
| 314 | free(dev_name); |
| 315 | if (drm_fd < 0) continue; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 316 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 317 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 325 | res = drmModeGetResources(drm_fd); |
| 326 | if (!res) { |
| 327 | close(drm_fd); |
| 328 | continue; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 331 | /* 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 336 | drmModeFreeResources(res); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 337 | close(drm_fd); |
| 338 | res = nullptr; |
| 339 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 340 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 341 | if (drm_fd < 0 || res == nullptr) { |
| 342 | perror("cannot find/open a drm device"); |
| 343 | return nullptr; |
| 344 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 345 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 346 | uint32_t selected_mode; |
| 347 | main_monitor_connector = find_main_monitor(drm_fd, res, &selected_mode); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 348 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 349 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 355 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 356 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static GRSurface* drm_flip(minui_backend* backend __unused) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 392 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static void drm_exit(minui_backend* backend __unused) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 403 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static minui_backend drm_backend = { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 413 | .init = drm_init, |
| 414 | .flip = drm_flip, |
| 415 | .blank = drm_blank, |
| 416 | .exit = drm_exit, |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 417 | }; |
| 418 | |
| 419 | minui_backend* open_drm() { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 420 | return &drm_backend; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 421 | } |