Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_adf.h" |
| 18 | |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 23 | #include <sys/mman.h> |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 26 | #include <adf/adf.h> |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 27 | #ifdef HAS_LIBSYNC |
xinglong.zhu | c4fa2c2 | 2016-08-05 14:52:22 +0800 | [diff] [blame] | 28 | #include <sync/sync.h> |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 29 | #endif |
| 30 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 31 | #include "minui/minui.h" |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 32 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 33 | MinuiBackendAdf::MinuiBackendAdf() : intf_fd(-1), dev(), n_surfaces(0), surfaces() {} |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 34 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 35 | int MinuiBackendAdf::SurfaceInit(const drm_mode_modeinfo* mode, GRSurfaceAdf* surf) { |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 36 | *surf = {}; |
| 37 | surf->fence_fd = -1; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 38 | surf->fd = adf_interface_simple_buffer_alloc(intf_fd, mode->hdisplay, mode->vdisplay, format, |
| 39 | &surf->offset, &surf->pitch); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 40 | if (surf->fd < 0) { |
| 41 | return surf->fd; |
| 42 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 43 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 44 | surf->width = mode->hdisplay; |
| 45 | surf->height = mode->vdisplay; |
| 46 | surf->row_bytes = surf->pitch; |
| 47 | surf->pixel_bytes = (format == DRM_FORMAT_RGB565) ? 2 : 4; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 48 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 49 | surf->data = static_cast<uint8_t*>( |
| 50 | mmap(nullptr, surf->pitch * surf->height, PROT_WRITE, MAP_SHARED, surf->fd, surf->offset)); |
| 51 | if (surf->data == MAP_FAILED) { |
Tao Bao | f04592b | 2017-02-09 12:59:19 -0800 | [diff] [blame] | 52 | int saved_errno = errno; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 53 | close(surf->fd); |
Tao Bao | f04592b | 2017-02-09 12:59:19 -0800 | [diff] [blame] | 54 | return -saved_errno; |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 55 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 56 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 57 | return 0; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 60 | int MinuiBackendAdf::InterfaceInit() { |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 61 | adf_interface_data intf_data; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 62 | int err = adf_get_interface_data(intf_fd, &intf_data); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 63 | if (err < 0) return err; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 64 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 65 | int ret = 0; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 66 | err = SurfaceInit(&intf_data.current_mode, &surfaces[0]); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 67 | if (err < 0) { |
| 68 | fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err)); |
| 69 | ret = err; |
| 70 | goto done; |
| 71 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 72 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 73 | err = SurfaceInit(&intf_data.current_mode, &surfaces[1]); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 74 | if (err < 0) { |
| 75 | fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err)); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 76 | surfaces[1] = {}; |
| 77 | n_surfaces = 1; |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 78 | } else { |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 79 | n_surfaces = 2; |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 80 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 81 | |
| 82 | done: |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 83 | adf_free_interface_data(&intf_data); |
| 84 | return ret; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 87 | int MinuiBackendAdf::DeviceInit(adf_device* dev) { |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 88 | adf_id_t intf_id; |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 89 | int err = adf_find_simple_post_configuration(dev, &format, 1, &intf_id, &eng_id); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 90 | if (err < 0) return err; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 91 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 92 | err = adf_device_attach(dev, eng_id, intf_id); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 93 | if (err < 0 && err != -EALREADY) return err; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 94 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 95 | intf_fd = adf_interface_open(dev, intf_id, O_RDWR); |
| 96 | if (intf_fd < 0) return intf_fd; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 97 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 98 | err = InterfaceInit(); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 99 | if (err < 0) { |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 100 | close(intf_fd); |
| 101 | intf_fd = -1; |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 102 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 103 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 104 | return err; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 107 | GRSurface* MinuiBackendAdf::Init() { |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 108 | #if defined(RECOVERY_ABGR) |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 109 | format = DRM_FORMAT_ABGR8888; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 110 | #elif defined(RECOVERY_BGRA) |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 111 | format = DRM_FORMAT_BGRA8888; |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 112 | #elif defined(RECOVERY_RGBA) |
| 113 | format = DRM_FORMAT_RGBA8888; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 114 | #elif defined(RECOVERY_RGBX) |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 115 | format = DRM_FORMAT_RGBX8888; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 116 | #else |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 117 | format = DRM_FORMAT_RGB565; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 118 | #endif |
| 119 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 120 | adf_id_t* dev_ids = nullptr; |
| 121 | ssize_t n_dev_ids = adf_devices(&dev_ids); |
| 122 | if (n_dev_ids == 0) { |
| 123 | return nullptr; |
| 124 | } else if (n_dev_ids < 0) { |
| 125 | fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids)); |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 129 | intf_fd = -1; |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 130 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 131 | for (ssize_t i = 0; i < n_dev_ids && intf_fd < 0; i++) { |
| 132 | int err = adf_device_open(dev_ids[i], O_RDWR, &dev); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 133 | if (err < 0) { |
| 134 | fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err)); |
| 135 | continue; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 138 | err = DeviceInit(&dev); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 139 | if (err < 0) { |
| 140 | fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err)); |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 141 | adf_device_close(&dev); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 144 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 145 | free(dev_ids); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 146 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 147 | if (intf_fd < 0) return nullptr; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 148 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 149 | GRSurface* ret = Flip(); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 150 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 151 | Blank(true); |
| 152 | Blank(false); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 157 | void MinuiBackendAdf::Sync(__unused GRSurfaceAdf* surf) { |
| 158 | #ifdef HAS_LIBSYNC |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 159 | static constexpr unsigned int warningTimeout = 3000; |
| 160 | |
| 161 | if (surf == nullptr) return; |
| 162 | |
| 163 | if (surf->fence_fd >= 0) { |
| 164 | int err = sync_wait(surf->fence_fd, warningTimeout); |
| 165 | if (err < 0) { |
| 166 | perror("adf sync fence wait error\n"); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 167 | } |
| 168 | |
xinglong.zhu | c4fa2c2 | 2016-08-05 14:52:22 +0800 | [diff] [blame] | 169 | close(surf->fence_fd); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 170 | surf->fence_fd = -1; |
| 171 | } |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 172 | #endif |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 175 | GRSurface* MinuiBackendAdf::Flip() { |
| 176 | GRSurfaceAdf* surf = &surfaces[current_surface]; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 177 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 178 | int fence_fd = adf_interface_simple_post(intf_fd, eng_id, surf->width, surf->height, format, |
| 179 | surf->fd, surf->offset, surf->pitch, -1); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 180 | if (fence_fd >= 0) surf->fence_fd = fence_fd; |
| 181 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 182 | current_surface = (current_surface + 1) % n_surfaces; |
| 183 | Sync(&surfaces[current_surface]); |
| 184 | return &surfaces[current_surface]; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 187 | void MinuiBackendAdf::Blank(bool blank) { |
| 188 | adf_interface_blank(intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 189 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 190 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 191 | void MinuiBackendAdf::SurfaceDestroy(GRSurfaceAdf* surf) { |
| 192 | munmap(surf->data, surf->pitch * surf->height); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 193 | close(surf->fence_fd); |
| 194 | close(surf->fd); |
| 195 | } |
| 196 | |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 197 | MinuiBackendAdf::~MinuiBackendAdf() { |
| 198 | adf_device_close(&dev); |
| 199 | for (unsigned int i = 0; i < n_surfaces; i++) { |
| 200 | SurfaceDestroy(&surfaces[i]); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 201 | } |
Tao Bao | 557fa1f | 2017-02-07 12:51:00 -0800 | [diff] [blame] | 202 | if (intf_fd >= 0) close(intf_fd); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 203 | } |