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> |
Jeremy Compostella | 955da1e | 2018-06-07 10:51:33 -0700 | [diff] [blame] | 20 | #include <poll.h> |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 23 | #include <sys/mman.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 26 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 27 | #include <memory> |
| 28 | |
| 29 | #include <android-base/macros.h> |
| 30 | #include <android-base/stringprintf.h> |
| 31 | #include <android-base/unique_fd.h> |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 32 | #include <drm_fourcc.h> |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 33 | #include <xf86drm.h> |
| 34 | #include <xf86drmMode.h> |
| 35 | |
Tao Bao | 0ecbd76 | 2017-01-16 21:16:58 -0800 | [diff] [blame] | 36 | #include "minui/minui.h" |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 37 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 38 | GRSurfaceDrm::~GRSurfaceDrm() { |
| 39 | if (mmapped_buffer_) { |
| 40 | munmap(mmapped_buffer_, row_bytes * height); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 41 | } |
Tianjie Xu | ccf00a2 | 2018-06-05 17:10:23 -0700 | [diff] [blame] | 42 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 43 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 47 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 48 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 49 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 50 | if (handle) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 51 | drm_gem_close gem_close = {}; |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 52 | gem_close.handle = handle; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 53 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 54 | if (drmIoctl(drm_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close) != 0) { |
| 55 | perror("Failed to DRM_IOCTL_GEM_CLOSE"); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 56 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 57 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static int drm_format_to_bpp(uint32_t format) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 61 | switch (format) { |
| 62 | case DRM_FORMAT_ABGR8888: |
| 63 | case DRM_FORMAT_BGRA8888: |
| 64 | case DRM_FORMAT_RGBX8888: |
Adrian Salido | cc7b7eb | 2019-12-12 20:18:09 -0800 | [diff] [blame] | 65 | case DRM_FORMAT_RGBA8888: |
| 66 | case DRM_FORMAT_ARGB8888: |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 67 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 79 | std::unique_ptr<GRSurfaceDrm> GRSurfaceDrm::Create(int drm_fd, int width, int height) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 80 | uint32_t format; |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 81 | PixelFormat pixel_format = gr_pixel_format(); |
tangrobin | 13bec76 | 2018-08-07 17:23:31 +0800 | [diff] [blame] | 82 | // 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 Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 86 | if (pixel_format == PixelFormat::ABGR) { |
tangrobin | 13bec76 | 2018-08-07 17:23:31 +0800 | [diff] [blame] | 87 | format = DRM_FORMAT_RGBA8888; |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 88 | } else if (pixel_format == PixelFormat::BGRA) { |
tangrobin | 13bec76 | 2018-08-07 17:23:31 +0800 | [diff] [blame] | 89 | format = DRM_FORMAT_ARGB8888; |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 90 | } else if (pixel_format == PixelFormat::RGBX) { |
tangrobin | 13bec76 | 2018-08-07 17:23:31 +0800 | [diff] [blame] | 91 | format = DRM_FORMAT_XBGR8888; |
Adrian Salido | cc7b7eb | 2019-12-12 20:18:09 -0800 | [diff] [blame] | 92 | } else if (pixel_format == PixelFormat::ARGB) { |
| 93 | format = DRM_FORMAT_BGRA8888; |
Tao Bao | ed876a7 | 2018-07-31 21:32:50 -0700 | [diff] [blame] | 94 | } else { |
| 95 | format = DRM_FORMAT_RGB565; |
| 96 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 97 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 98 | 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 103 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 104 | if (drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb) != 0) { |
| 105 | perror("Failed to DRM_IOCTL_MODE_CREATE_DUMB"); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 106 | return nullptr; |
| 107 | } |
Kelvin Zhang | 90f783f | 2021-03-30 08:59:19 -0400 | [diff] [blame] | 108 | 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 Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 110 | |
Tao Bao | 710bc53 | 2018-10-24 07:59:49 -0700 | [diff] [blame] | 111 | // 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 Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 114 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 115 | uint32_t handles[4], pitches[4], offsets[4]; |
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 | handles[0] = surface->handle; |
| 118 | pitches[0] = create_dumb.pitch; |
| 119 | offsets[0] = 0; |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 120 | if (drmModeAddFB2(drm_fd, width, height, format, handles, pitches, offsets, &surface->fb_id, 0) != |
| 121 | 0) { |
| 122 | perror("Failed to drmModeAddFB2"); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 123 | return nullptr; |
| 124 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 125 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 126 | drm_mode_map_dumb map_dumb = {}; |
| 127 | map_dumb.handle = create_dumb.handle; |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 128 | if (drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb) != 0) { |
| 129 | perror("Failed to DRM_IOCTL_MODE_MAP_DUMB"); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 130 | return nullptr; |
| 131 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 132 | |
Kelvin Zhang | 90f783f | 2021-03-30 08:59:19 -0400 | [diff] [blame] | 133 | auto mmapped = |
| 134 | mmap(nullptr, create_dumb.size, PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, map_dumb.offset); |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 135 | if (mmapped == MAP_FAILED) { |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 136 | perror("Failed to mmap()"); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 137 | return nullptr; |
| 138 | } |
Tao Bao | 92bdb5a | 2018-10-21 12:12:37 -0700 | [diff] [blame] | 139 | surface->mmapped_buffer_ = static_cast<uint8_t*>(mmapped); |
Kelvin Zhang | 90f783f | 2021-03-30 08:59:19 -0400 | [diff] [blame] | 140 | printf("Framebuffer of size %llu allocated @ %p\n", create_dumb.size, surface->mmapped_buffer_); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 141 | return surface; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 144 | void 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 | |
| 155 | bool MinuiBackendDrm::DrmEnableCrtc(int drm_fd, drmModeCrtc* crtc, |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 156 | const std::unique_ptr<GRSurfaceDrm>& surface, |
| 157 | uint32_t* connector_id) { |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 158 | if (drmModeSetCrtc(drm_fd, crtc->crtc_id, surface->fb_id, 0, 0, // x,y |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 159 | connector_id, 1, // connector_count |
| 160 | &crtc->mode) != 0) { |
| 161 | fprintf(stderr, "Failed to drmModeSetCrtc(%d)\n", *connector_id); |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 164 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
| 168 | void MinuiBackendDrm::Blank(bool blank) { |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 169 | Blank(blank, DRM_MAIN); |
| 170 | } |
| 171 | |
| 172 | void 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 Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 192 | if (blank) { |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 193 | DrmDisableCrtc(drm_fd, drmInterface->monitor_crtc); |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 194 | } else { |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 195 | DrmEnableCrtc(drm_fd, drmInterface->monitor_crtc, |
| 196 | drmInterface->GRSurfaceDrms[drmInterface->current_buffer], |
| 197 | &drmInterface->monitor_connector->connector_id); |
| 198 | |
| 199 | active_display = index; |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 203 | static drmModeCrtc* find_crtc_for_connector(int fd, drmModeRes* resources, |
| 204 | drmModeConnector* connector) { |
| 205 | // Find the encoder. If we already have one, just use it. |
| 206 | drmModeEncoder* encoder; |
| 207 | if (connector->encoder_id) { |
| 208 | encoder = drmModeGetEncoder(fd, connector->encoder_id); |
| 209 | } else { |
| 210 | encoder = nullptr; |
| 211 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 212 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 213 | int32_t crtc; |
| 214 | if (encoder && encoder->crtc_id) { |
| 215 | crtc = encoder->crtc_id; |
| 216 | drmModeFreeEncoder(encoder); |
| 217 | return drmModeGetCrtc(fd, crtc); |
| 218 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 219 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 220 | // Didn't find anything, try to find a crtc and encoder combo. |
| 221 | crtc = -1; |
| 222 | for (int i = 0; i < connector->count_encoders; i++) { |
| 223 | encoder = drmModeGetEncoder(fd, connector->encoders[i]); |
| 224 | |
| 225 | if (encoder) { |
| 226 | for (int j = 0; j < resources->count_crtcs; j++) { |
| 227 | if (!(encoder->possible_crtcs & (1 << j))) continue; |
| 228 | crtc = resources->crtcs[j]; |
| 229 | break; |
| 230 | } |
| 231 | if (crtc >= 0) { |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 232 | drmModeFreeEncoder(encoder); |
| 233 | return drmModeGetCrtc(fd, crtc); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 234 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 235 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 236 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 237 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 238 | return nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 241 | std::vector<drmModeConnector*> find_used_connector_by_type(int fd, drmModeRes* resources, |
| 242 | unsigned type) { |
| 243 | std::vector<drmModeConnector*> drmConnectors; |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 244 | for (int i = 0; i < resources->count_connectors; i++) { |
| 245 | drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]); |
| 246 | if (connector) { |
| 247 | if ((connector->connector_type == type) && (connector->connection == DRM_MODE_CONNECTED) && |
| 248 | (connector->count_modes > 0)) { |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 249 | drmConnectors.push_back(connector); |
| 250 | } else { |
| 251 | drmModeFreeConnector(connector); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 252 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 253 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 254 | } |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 255 | return drmConnectors; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 258 | static drmModeConnector* find_first_connected_connector(int fd, drmModeRes* resources) { |
| 259 | for (int i = 0; i < resources->count_connectors; i++) { |
| 260 | drmModeConnector* connector; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 261 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 262 | connector = drmModeGetConnector(fd, resources->connectors[i]); |
| 263 | if (connector) { |
| 264 | if ((connector->count_modes > 0) && (connector->connection == DRM_MODE_CONNECTED)) |
| 265 | return connector; |
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 | drmModeFreeConnector(connector); |
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 | } |
| 270 | return nullptr; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 273 | bool MinuiBackendDrm::FindAndSetMonitor(int fd, drmModeRes* resources) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 274 | /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */ |
| 275 | static constexpr unsigned kConnectorPriority[] = { |
| 276 | DRM_MODE_CONNECTOR_LVDS, |
| 277 | DRM_MODE_CONNECTOR_eDP, |
| 278 | DRM_MODE_CONNECTOR_DSI, |
| 279 | }; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 280 | |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 281 | std::vector<drmModeConnector*> drmConnectors; |
| 282 | for (int i = 0; i < arraysize(kConnectorPriority) && drmConnectors.size() < DRM_MAX; i++) { |
| 283 | auto connectors = find_used_connector_by_type(fd, resources, kConnectorPriority[i]); |
| 284 | for (auto connector : connectors) { |
| 285 | drmConnectors.push_back(connector); |
| 286 | if (drmConnectors.size() >= DRM_MAX) 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 | |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 290 | /* If we didn't find a connector, grab the first one that is connected. */ |
| 291 | if (drmConnectors.empty()) { |
| 292 | drmModeConnector* connector = find_first_connected_connector(fd, resources); |
| 293 | if (connector) { |
| 294 | drmConnectors.push_back(connector); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | for (int drm_index = 0; drm_index < drmConnectors.size(); drm_index++) { |
| 299 | drm[drm_index].monitor_connector = drmConnectors[drm_index]; |
| 300 | |
| 301 | drm[drm_index].selected_mode = 0; |
| 302 | for (int modes = 0; modes < drmConnectors[drm_index]->count_modes; modes++) { |
| 303 | printf("Display Mode %d resolution: %d x %d @ %d FPS\n", modes, |
| 304 | drmConnectors[drm_index]->modes[modes].hdisplay, |
| 305 | drmConnectors[drm_index]->modes[modes].vdisplay, |
| 306 | drmConnectors[drm_index]->modes[modes].vrefresh); |
| 307 | if (drmConnectors[drm_index]->modes[modes].type & DRM_MODE_TYPE_PREFERRED) { |
| 308 | printf("Choosing display mode #%d\n", modes); |
| 309 | drm[drm_index].selected_mode = modes; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return drmConnectors.size() > 0; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 318 | void MinuiBackendDrm::DisableNonMainCrtcs(int fd, drmModeRes* resources, drmModeCrtc* main_crtc) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 319 | for (int i = 0; i < resources->count_connectors; i++) { |
| 320 | drmModeConnector* connector = drmModeGetConnector(fd, resources->connectors[i]); |
| 321 | drmModeCrtc* crtc = find_crtc_for_connector(fd, resources, connector); |
| 322 | if (crtc->crtc_id != main_crtc->crtc_id) { |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 323 | DrmDisableCrtc(fd, crtc); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 324 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 325 | drmModeFreeCrtc(crtc); |
| 326 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 329 | GRSurface* MinuiBackendDrm::Init() { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 330 | drmModeRes* res = nullptr; |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 331 | drm_fd = -1; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 332 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 333 | /* Consider DRM devices in order. */ |
| 334 | for (int i = 0; i < DRM_MAX_MINOR; i++) { |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 335 | auto dev_name = android::base::StringPrintf(DRM_DEV_NAME, DRM_DIR_NAME, i); |
Tao Bao | b549243 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 336 | android::base::unique_fd fd(open(dev_name.c_str(), O_RDWR | O_CLOEXEC)); |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 337 | if (fd == -1) continue; |
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 | /* We need dumb buffers. */ |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 340 | if (uint64_t cap = 0; drmGetCap(fd.get(), DRM_CAP_DUMB_BUFFER, &cap) != 0 || cap == 0) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 341 | continue; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 344 | res = drmModeGetResources(fd.get()); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 345 | if (!res) { |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 346 | continue; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 349 | /* Use this device if it has at least one connected monitor. */ |
| 350 | if (res->count_crtcs > 0 && res->count_connectors > 0) { |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 351 | if (find_first_connected_connector(fd.get(), res)) { |
| 352 | drm_fd = fd.release(); |
| 353 | break; |
| 354 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 357 | drmModeFreeResources(res); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 358 | res = nullptr; |
| 359 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 360 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 361 | if (drm_fd == -1 || res == nullptr) { |
| 362 | perror("Failed to find/open a drm device"); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 363 | return nullptr; |
| 364 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 365 | |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 366 | if (!FindAndSetMonitor(drm_fd, res)) { |
| 367 | fprintf(stderr, "Failed to find main monitor_connector\n"); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 368 | drmModeFreeResources(res); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 369 | return nullptr; |
| 370 | } |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 371 | |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 372 | for (int i = 0; i < DRM_MAX; i++) { |
| 373 | if (drm[i].monitor_connector) { |
| 374 | drm[i].monitor_crtc = find_crtc_for_connector(drm_fd, res, drm[i].monitor_connector); |
| 375 | if (!drm[i].monitor_crtc) { |
| 376 | fprintf(stderr, "Failed to find monitor_crtc, drm index=%d\n", i); |
| 377 | drmModeFreeResources(res); |
| 378 | return nullptr; |
| 379 | } |
| 380 | |
| 381 | drm[i].monitor_crtc->mode = drm[i].monitor_connector->modes[drm[i].selected_mode]; |
| 382 | |
| 383 | int width = drm[i].monitor_crtc->mode.hdisplay; |
| 384 | int height = drm[i].monitor_crtc->mode.vdisplay; |
| 385 | |
| 386 | drm[i].GRSurfaceDrms[0] = GRSurfaceDrm::Create(drm_fd, width, height); |
| 387 | drm[i].GRSurfaceDrms[1] = GRSurfaceDrm::Create(drm_fd, width, height); |
| 388 | if (!drm[i].GRSurfaceDrms[0] || !drm[i].GRSurfaceDrms[1]) { |
| 389 | fprintf(stderr, "Failed to create GRSurfaceDrm, drm index=%d\n", i); |
| 390 | drmModeFreeResources(res); |
| 391 | return nullptr; |
| 392 | } |
| 393 | |
| 394 | drm[i].current_buffer = 0; |
| 395 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 398 | DisableNonMainCrtcs(drm_fd, res, drm[DRM_MAIN].monitor_crtc); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 399 | |
| 400 | drmModeFreeResources(res); |
| 401 | |
Tianjie Xu | ccf00a2 | 2018-06-05 17:10:23 -0700 | [diff] [blame] | 402 | // We will likely encounter errors in the backend functions (i.e. Flip) if EnableCrtc fails. |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 403 | if (!DrmEnableCrtc(drm_fd, drm[DRM_MAIN].monitor_crtc, drm[DRM_MAIN].GRSurfaceDrms[1], |
| 404 | &drm[DRM_MAIN].monitor_connector->connector_id)) { |
Tianjie Xu | ccf00a2 | 2018-06-05 17:10:23 -0700 | [diff] [blame] | 405 | return nullptr; |
| 406 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 407 | |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 408 | return drm[DRM_MAIN].GRSurfaceDrms[0].get(); |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Jeremy Compostella | 955da1e | 2018-06-07 10:51:33 -0700 | [diff] [blame] | 411 | static void page_flip_complete(__unused int fd, |
| 412 | __unused unsigned int sequence, |
| 413 | __unused unsigned int tv_sec, |
| 414 | __unused unsigned int tv_usec, |
| 415 | void *user_data) { |
| 416 | *static_cast<bool*>(user_data) = false; |
| 417 | } |
| 418 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 419 | GRSurface* MinuiBackendDrm::Flip() { |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 420 | GRSurface* surface = NULL; |
| 421 | DrmInterface* current_drm = &drm[active_display]; |
Jeremy Compostella | 955da1e | 2018-06-07 10:51:33 -0700 | [diff] [blame] | 422 | bool ongoing_flip = true; |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 423 | |
| 424 | if (!current_drm->monitor_connector) { |
| 425 | fprintf(stderr, "Unsupported. active_display = %d\n", active_display); |
| 426 | return nullptr; |
| 427 | } |
| 428 | |
| 429 | if (drmModePageFlip(drm_fd, current_drm->monitor_crtc->crtc_id, |
| 430 | current_drm->GRSurfaceDrms[current_drm->current_buffer]->fb_id, |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 431 | DRM_MODE_PAGE_FLIP_EVENT, &ongoing_flip) != 0) { |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 432 | fprintf(stderr, "Failed to drmModePageFlip, active_display=%d", active_display); |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 433 | return nullptr; |
| 434 | } |
Jeremy Compostella | 955da1e | 2018-06-07 10:51:33 -0700 | [diff] [blame] | 435 | |
| 436 | while (ongoing_flip) { |
| 437 | struct pollfd fds = { |
| 438 | .fd = drm_fd, |
| 439 | .events = POLLIN |
| 440 | }; |
| 441 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 442 | if (poll(&fds, 1, -1) == -1 || !(fds.revents & POLLIN)) { |
| 443 | perror("Failed to poll() on drm fd"); |
Jeremy Compostella | 955da1e | 2018-06-07 10:51:33 -0700 | [diff] [blame] | 444 | break; |
| 445 | } |
| 446 | |
| 447 | drmEventContext evctx = { |
| 448 | .version = DRM_EVENT_CONTEXT_VERSION, |
| 449 | .page_flip_handler = page_flip_complete |
| 450 | }; |
| 451 | |
Tao Bao | d096d7e | 2018-10-23 17:24:34 -0700 | [diff] [blame] | 452 | if (drmHandleEvent(drm_fd, &evctx) != 0) { |
| 453 | perror("Failed to drmHandleEvent"); |
Jeremy Compostella | 955da1e | 2018-06-07 10:51:33 -0700 | [diff] [blame] | 454 | break; |
| 455 | } |
| 456 | } |
| 457 | |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 458 | current_drm->current_buffer = 1 - current_drm->current_buffer; |
| 459 | surface = current_drm->GRSurfaceDrms[current_drm->current_buffer].get(); |
| 460 | return surface; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 463 | MinuiBackendDrm::~MinuiBackendDrm() { |
Weizhung Ding | f4dfa1a | 2021-09-27 11:30:26 +0800 | [diff] [blame] | 464 | for (int i = 0; i < DRM_MAX; i++) { |
| 465 | if (drm[i].monitor_connector) { |
| 466 | DrmDisableCrtc(drm_fd, drm[i].monitor_crtc); |
| 467 | drmModeFreeCrtc(drm[i].monitor_crtc); |
| 468 | drmModeFreeConnector(drm[i].monitor_connector); |
| 469 | } |
| 470 | } |
Tao Bao | 76be34c | 2017-02-07 13:30:19 -0800 | [diff] [blame] | 471 | close(drm_fd); |
| 472 | drm_fd = -1; |
Stéphane Marchesin | 1a92c44 | 2015-06-29 20:05:48 -0700 | [diff] [blame] | 473 | } |