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 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 17 | #include "graphics_drm.h" |
| 18 | |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 22 | #include <sys/mman.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 25 | |
| 26 | #include <drm_fourcc.h> |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 27 | #include <xf86drm.h> |
| 28 | #include <xf86drmMode.h> |
| 29 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 30 | #include "minui/minui.h" |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 31 | |
| 32 | #define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A))) |
| 33 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 34 | MinuiBackendDrm::MinuiBackendDrm() |
| 35 | : GRSurfaceDrms(), main_monitor_crtc(nullptr), main_monitor_connector(nullptr), drm_fd(-1) {} |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 36 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 37 | void MinuiBackendDrm::DrmDisableCrtc(int drm_fd, drmModeCrtc* crtc) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 38 | if (crtc) { |
| 39 | drmModeSetCrtc(drm_fd, crtc->crtc_id, |
| 40 | 0, // fb_id |
| 41 | 0, 0, // x,y |
| 42 | nullptr, // connectors |
| 43 | 0, // connector_count |
| 44 | nullptr); // mode |
| 45 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Tianjie Xu | ccf00a2 | 2018-06-05 17:10:23 -0700 | [diff] [blame^] | 48 | int MinuiBackendDrm::DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc, GRSurfaceDrm* surface) { |
| 49 | int ret = drmModeSetCrtc(drm_fd, crtc->crtc_id, surface->fb_id, 0, 0, // x,y |
| 50 | &main_monitor_connector->connector_id, |
| 51 | 1, // connector_count |
| 52 | &main_monitor_crtc->mode); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 53 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 54 | if (ret) { |
| 55 | printf("drmModeSetCrtc failed ret=%d\n", ret); |
| 56 | } |
Tianjie Xu | ccf00a2 | 2018-06-05 17:10:23 -0700 | [diff] [blame^] | 57 | |
| 58 | return ret; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 61 | void MinuiBackendDrm::Blank(bool blank) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 62 | if (blank) { |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 63 | DrmDisableCrtc(drm_fd, main_monitor_crtc); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 64 | } else { |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 65 | DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[current_buffer]); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 66 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 69 | void MinuiBackendDrm::DrmDestroySurface(GRSurfaceDrm* surface) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 70 | if (!surface) return; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 71 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 72 | if (surface->data) { |
| 73 | munmap(surface->data, surface->row_bytes * surface->height); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 74 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 75 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 76 | if (surface->fb_id) { |
| 77 | int ret = drmModeRmFB(drm_fd, surface->fb_id); |
| 78 | if (ret) { |
| 79 | printf("drmModeRmFB failed ret=%d\n", ret); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 80 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 81 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 82 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 83 | if (surface->handle) { |
| 84 | drm_gem_close gem_close = {}; |
| 85 | gem_close.handle = surface->handle; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 86 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 87 | int ret = drmIoctl(drm_fd, DRM_IOCTL_GEM_CLOSE, &gem_close); |
| 88 | if (ret) { |
| 89 | printf("DRM_IOCTL_GEM_CLOSE failed ret=%d\n", ret); |
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 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 92 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 93 | delete surface; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static int drm_format_to_bpp(uint32_t format) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 97 | switch (format) { |
| 98 | case DRM_FORMAT_ABGR8888: |
| 99 | case DRM_FORMAT_BGRA8888: |
| 100 | case DRM_FORMAT_RGBX8888: |
| 101 | case DRM_FORMAT_BGRX8888: |
| 102 | case DRM_FORMAT_XBGR8888: |
| 103 | case DRM_FORMAT_XRGB8888: |
| 104 | return 32; |
| 105 | case DRM_FORMAT_RGB565: |
| 106 | return 16; |
| 107 | default: |
| 108 | printf("Unknown format %d\n", format); |
| 109 | return 32; |
| 110 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 113 | GRSurfaceDrm* MinuiBackendDrm::DrmCreateSurface(int width, int height) { |
| 114 | GRSurfaceDrm* surface = new GRSurfaceDrm; |
| 115 | *surface = {}; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 116 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 117 | uint32_t format; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 118 | #if defined(RECOVERY_ABGR) |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 119 | format = DRM_FORMAT_RGBA8888; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 120 | #elif defined(RECOVERY_BGRA) |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 121 | format = DRM_FORMAT_ARGB8888; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 122 | #elif defined(RECOVERY_RGBX) |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 123 | format = DRM_FORMAT_XBGR8888; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 124 | #else |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 125 | format = DRM_FORMAT_RGB565; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 126 | #endif |
| 127 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 128 | drm_mode_create_dumb create_dumb = {}; |
| 129 | create_dumb.height = height; |
| 130 | create_dumb.width = width; |
| 131 | create_dumb.bpp = drm_format_to_bpp(format); |
| 132 | create_dumb.flags = 0; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 133 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 134 | int ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb); |
| 135 | if (ret) { |
| 136 | printf("DRM_IOCTL_MODE_CREATE_DUMB failed ret=%d\n", ret); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 137 | DrmDestroySurface(surface); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 138 | return nullptr; |
| 139 | } |
| 140 | surface->handle = create_dumb.handle; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 141 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 142 | uint32_t handles[4], pitches[4], offsets[4]; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 143 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 144 | handles[0] = surface->handle; |
| 145 | pitches[0] = create_dumb.pitch; |
| 146 | offsets[0] = 0; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 147 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 148 | ret = |
| 149 | drmModeAddFB2(drm_fd, width, height, format, handles, pitches, offsets, &(surface->fb_id), 0); |
| 150 | if (ret) { |
| 151 | printf("drmModeAddFB2 failed ret=%d\n", ret); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 152 | DrmDestroySurface(surface); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 153 | return nullptr; |
| 154 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 155 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 156 | drm_mode_map_dumb map_dumb = {}; |
| 157 | map_dumb.handle = create_dumb.handle; |
| 158 | ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb); |
| 159 | if (ret) { |
| 160 | printf("DRM_IOCTL_MODE_MAP_DUMB failed ret=%d\n", ret); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 161 | DrmDestroySurface(surface); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 162 | return nullptr; |
| 163 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 164 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 165 | surface->height = height; |
| 166 | surface->width = width; |
| 167 | surface->row_bytes = create_dumb.pitch; |
| 168 | surface->pixel_bytes = create_dumb.bpp / 8; |
| 169 | surface->data = static_cast<unsigned char*>(mmap(nullptr, surface->height * surface->row_bytes, |
| 170 | PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, |
| 171 | map_dumb.offset)); |
| 172 | if (surface->data == MAP_FAILED) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 173 | perror("mmap() failed"); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 174 | DrmDestroySurface(surface); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 175 | return nullptr; |
| 176 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 177 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 178 | return surface; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 181 | static drmModeCrtc* find_crtc_for_connector(int fd, drmModeRes* resources, |
| 182 | drmModeConnector* connector) { |
| 183 | // Find the encoder. If we already have one, just use it. |
| 184 | drmModeEncoder* encoder; |
| 185 | if (connector->encoder_id) { |
| 186 | encoder = drmModeGetEncoder(fd, connector->encoder_id); |
| 187 | } else { |
| 188 | encoder = nullptr; |
| 189 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 190 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 191 | int32_t crtc; |
| 192 | if (encoder && encoder->crtc_id) { |
| 193 | crtc = encoder->crtc_id; |
| 194 | drmModeFreeEncoder(encoder); |
| 195 | return drmModeGetCrtc(fd, crtc); |
| 196 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 197 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 198 | // Didn't find anything, try to find a crtc and encoder combo. |
| 199 | crtc = -1; |
| 200 | for (int i = 0; i < connector->count_encoders; i++) { |
| 201 | encoder = drmModeGetEncoder(fd, connector->encoders[i]); |
| 202 | |
| 203 | if (encoder) { |
| 204 | for (int j = 0; j < resources->count_crtcs; j++) { |
| 205 | if (!(encoder->possible_crtcs & (1 << j))) continue; |
| 206 | crtc = resources->crtcs[j]; |
| 207 | break; |
| 208 | } |
| 209 | if (crtc >= 0) { |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 210 | drmModeFreeEncoder(encoder); |
| 211 | return drmModeGetCrtc(fd, crtc); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 212 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 213 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 214 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 215 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 216 | return nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 219 | static drmModeConnector* find_used_connector_by_type(int fd, drmModeRes* resources, unsigned type) { |
| 220 | for (int i = 0; i < resources->count_connectors; i++) { |
| 221 | drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]); |
| 222 | if (connector) { |
| 223 | if ((connector->connector_type == type) && (connector->connection == DRM_MODE_CONNECTED) && |
| 224 | (connector->count_modes > 0)) { |
| 225 | return connector; |
| 226 | } |
| 227 | drmModeFreeConnector(connector); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 228 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 229 | } |
| 230 | return nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 233 | static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* resources) { |
| 234 | for (int i = 0; i < resources->count_connectors; i++) { |
| 235 | drmModeConnector* connector; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 236 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 237 | connector = drmModeGetConnector(fd, resources->connectors[i]); |
| 238 | if (connector) { |
| 239 | if ((connector->count_modes > 0) && (connector->connection == DRM_MODE_CONNECTED)) |
| 240 | return connector; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 241 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 242 | drmModeFreeConnector(connector); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 243 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 244 | } |
| 245 | return nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 248 | drmModeConnector* MinuiBackendDrm::FindMainMonitor(int fd, drmModeRes* resources, |
| 249 | uint32_t* mode_index) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 250 | /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */ |
| 251 | static constexpr unsigned kConnectorPriority[] = { |
| 252 | DRM_MODE_CONNECTOR_LVDS, |
| 253 | DRM_MODE_CONNECTOR_eDP, |
| 254 | DRM_MODE_CONNECTOR_DSI, |
| 255 | }; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 256 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 257 | drmModeConnector* main_monitor_connector = nullptr; |
| 258 | unsigned i = 0; |
| 259 | do { |
| 260 | main_monitor_connector = find_used_connector_by_type(fd, resources, kConnectorPriority[i]); |
| 261 | i++; |
| 262 | } while (!main_monitor_connector && i < ARRAY_SIZE(kConnectorPriority)); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 263 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 264 | /* If we didn't find a connector, grab the first one that is connected. */ |
| 265 | if (!main_monitor_connector) { |
| 266 | main_monitor_connector = find_first_connected_connector(fd, resources); |
| 267 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 268 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 269 | /* If we still didn't find a connector, give up and return. */ |
| 270 | if (!main_monitor_connector) return nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 271 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 272 | *mode_index = 0; |
| 273 | for (int modes = 0; modes < main_monitor_connector->count_modes; modes++) { |
| 274 | if (main_monitor_connector->modes[modes].type & DRM_MODE_TYPE_PREFERRED) { |
| 275 | *mode_index = modes; |
| 276 | break; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 277 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 278 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 279 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 280 | return main_monitor_connector; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 283 | void MinuiBackendDrm::DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 284 | for (int i = 0; i < resources->count_connectors; i++) { |
| 285 | drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]); |
| 286 | drmModeCrtc* crtc = find_crtc_for_connector(fd, resources, connector); |
| 287 | if (crtc->crtc_id != main_crtc->crtc_id) { |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 288 | DrmDisableCrtc(fd, crtc); |
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 | drmModeFreeCrtc(crtc); |
| 291 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 294 | GRSurface* MinuiBackendDrm::Init() { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 295 | drmModeRes* res = nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 296 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 297 | /* Consider DRM devices in order. */ |
| 298 | for (int i = 0; i < DRM_MAX_MINOR; i++) { |
| 299 | char* dev_name; |
| 300 | int ret = asprintf(&dev_name, DRM_DEV_NAME, DRM_DIR_NAME, i); |
| 301 | if (ret < 0) continue; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 302 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 303 | drm_fd = open(dev_name, O_RDWR, 0); |
| 304 | free(dev_name); |
| 305 | if (drm_fd < 0) continue; |
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 | uint64_t cap = 0; |
| 308 | /* We need dumb buffers. */ |
| 309 | ret = drmGetCap(drm_fd, DRM_CAP_DUMB_BUFFER, &cap); |
| 310 | if (ret || cap == 0) { |
| 311 | close(drm_fd); |
| 312 | continue; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 315 | res = drmModeGetResources(drm_fd); |
| 316 | if (!res) { |
| 317 | close(drm_fd); |
| 318 | continue; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 321 | /* Use this device if it has at least one connected monitor. */ |
| 322 | if (res->count_crtcs > 0 && res->count_connectors > 0) { |
| 323 | if (find_first_connected_connector(drm_fd, res)) break; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 326 | drmModeFreeResources(res); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 327 | close(drm_fd); |
| 328 | res = nullptr; |
| 329 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 330 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 331 | if (drm_fd < 0 || res == nullptr) { |
| 332 | perror("cannot find/open a drm device"); |
| 333 | return nullptr; |
| 334 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 335 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 336 | uint32_t selected_mode; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 337 | main_monitor_connector = FindMainMonitor(drm_fd, res, &selected_mode); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 338 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 339 | if (!main_monitor_connector) { |
| 340 | printf("main_monitor_connector not found\n"); |
| 341 | drmModeFreeResources(res); |
| 342 | close(drm_fd); |
| 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 | main_monitor_crtc = find_crtc_for_connector(drm_fd, res, main_monitor_connector); |
| 347 | |
| 348 | if (!main_monitor_crtc) { |
| 349 | printf("main_monitor_crtc not found\n"); |
| 350 | drmModeFreeResources(res); |
| 351 | close(drm_fd); |
| 352 | return nullptr; |
| 353 | } |
| 354 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 355 | DisableNonMainCrtcs(drm_fd, res, main_monitor_crtc); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 356 | |
| 357 | main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode]; |
| 358 | |
| 359 | int width = main_monitor_crtc->mode.hdisplay; |
| 360 | int height = main_monitor_crtc->mode.vdisplay; |
| 361 | |
| 362 | drmModeFreeResources(res); |
| 363 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 364 | GRSurfaceDrms[0] = DrmCreateSurface(width, height); |
| 365 | GRSurfaceDrms[1] = DrmCreateSurface(width, height); |
| 366 | if (!GRSurfaceDrms[0] || !GRSurfaceDrms[1]) { |
| 367 | // GRSurfaceDrms and drm_fd should be freed in d'tor. |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 368 | return nullptr; |
| 369 | } |
| 370 | |
| 371 | current_buffer = 0; |
| 372 | |
Tianjie Xu | ccf00a2 | 2018-06-05 17:10:23 -0700 | [diff] [blame^] | 373 | // We will likely encounter errors in the backend functions (i.e. Flip) if EnableCrtc fails. |
| 374 | if (DrmEnableCrtc(drm_fd, main_monitor_crtc, GRSurfaceDrms[1]) != 0) { |
| 375 | return nullptr; |
| 376 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 377 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 378 | return GRSurfaceDrms[0]; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 381 | GRSurface* MinuiBackendDrm::Flip() { |
| 382 | int ret = drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id, |
| 383 | GRSurfaceDrms[current_buffer]->fb_id, 0, nullptr); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 384 | if (ret < 0) { |
| 385 | printf("drmModePageFlip failed ret=%d\n", ret); |
| 386 | return nullptr; |
| 387 | } |
| 388 | current_buffer = 1 - current_buffer; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 389 | return GRSurfaceDrms[current_buffer]; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 392 | MinuiBackendDrm::~MinuiBackendDrm() { |
| 393 | DrmDisableCrtc(drm_fd, main_monitor_crtc); |
| 394 | DrmDestroySurface(GRSurfaceDrms[0]); |
| 395 | DrmDestroySurface(GRSurfaceDrms[1]); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 396 | drmModeFreeCrtc(main_monitor_crtc); |
| 397 | drmModeFreeConnector(main_monitor_connector); |
| 398 | close(drm_fd); |
| 399 | drm_fd = -1; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 400 | } |