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