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 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 21 | #include <sys/mman.h> |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 22 | #include <unistd.h> |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 23 | |
| 24 | #include <adf/adf.h> |
xinglong.zhu | c4fa2c2 | 2016-08-05 14:52:22 +0800 | [diff] [blame] | 25 | #include <sync/sync.h> |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 26 | |
| 27 | #include "graphics.h" |
| 28 | |
| 29 | struct adf_surface_pdata { |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 30 | GRSurface base; |
| 31 | int fence_fd; |
| 32 | int fd; |
| 33 | __u32 offset; |
| 34 | __u32 pitch; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | struct adf_pdata { |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 38 | minui_backend base; |
| 39 | int intf_fd; |
| 40 | adf_id_t eng_id; |
| 41 | __u32 format; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 42 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 43 | adf_device dev; |
Jonathan Hamilton | bab6e49 | 2016-05-05 15:30:57 -0700 | [diff] [blame] | 44 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 45 | unsigned int current_surface; |
| 46 | unsigned int n_surfaces; |
| 47 | adf_surface_pdata surfaces[2]; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 50 | static GRSurface* adf_flip(minui_backend* backend); |
| 51 | static void adf_blank(minui_backend* backend, bool blank); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 52 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 53 | static int adf_surface_init(adf_pdata* pdata, drm_mode_modeinfo* mode, adf_surface_pdata* surf) { |
| 54 | *surf = {}; |
| 55 | surf->fence_fd = -1; |
| 56 | surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, mode->vdisplay, |
| 57 | pdata->format, &surf->offset, &surf->pitch); |
| 58 | if (surf->fd < 0) { |
| 59 | return surf->fd; |
| 60 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 61 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 62 | surf->base.width = mode->hdisplay; |
| 63 | surf->base.height = mode->vdisplay; |
| 64 | surf->base.row_bytes = surf->pitch; |
| 65 | surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 66 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 67 | surf->base.data = static_cast<uint8_t*>(mmap(nullptr, surf->pitch * surf->base.height, PROT_WRITE, |
| 68 | MAP_SHARED, surf->fd, surf->offset)); |
| 69 | if (surf->base.data == MAP_FAILED) { |
| 70 | close(surf->fd); |
| 71 | return -errno; |
| 72 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 73 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 74 | return 0; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 77 | static int adf_interface_init(adf_pdata* pdata) { |
| 78 | adf_interface_data intf_data; |
| 79 | int err = adf_get_interface_data(pdata->intf_fd, &intf_data); |
| 80 | if (err < 0) return err; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 81 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 82 | int ret = 0; |
| 83 | err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]); |
| 84 | if (err < 0) { |
| 85 | fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err)); |
| 86 | ret = err; |
| 87 | goto done; |
| 88 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 89 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 90 | err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[1]); |
| 91 | if (err < 0) { |
| 92 | fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err)); |
| 93 | pdata->surfaces[1] = {}; |
| 94 | pdata->n_surfaces = 1; |
| 95 | } else { |
| 96 | pdata->n_surfaces = 2; |
| 97 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 98 | |
| 99 | done: |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 100 | adf_free_interface_data(&intf_data); |
| 101 | return ret; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 104 | static int adf_device_init(adf_pdata* pdata, adf_device* dev) { |
| 105 | adf_id_t intf_id; |
| 106 | int err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id, &pdata->eng_id); |
| 107 | if (err < 0) return err; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 108 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 109 | err = adf_device_attach(dev, pdata->eng_id, intf_id); |
| 110 | if (err < 0 && err != -EALREADY) return err; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 111 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 112 | pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR); |
| 113 | if (pdata->intf_fd < 0) return pdata->intf_fd; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 114 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 115 | err = adf_interface_init(pdata); |
| 116 | if (err < 0) { |
| 117 | close(pdata->intf_fd); |
| 118 | pdata->intf_fd = -1; |
| 119 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 120 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 121 | return err; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 124 | static GRSurface* adf_init(minui_backend* backend) { |
| 125 | adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 126 | |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 127 | #if defined(RECOVERY_ABGR) |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 128 | pdata->format = DRM_FORMAT_ABGR8888; |
Tony Kuo | fd778e3 | 2015-02-05 21:25:56 +0800 | [diff] [blame] | 129 | #elif defined(RECOVERY_BGRA) |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 130 | pdata->format = DRM_FORMAT_BGRA8888; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 131 | #elif defined(RECOVERY_RGBX) |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 132 | pdata->format = DRM_FORMAT_RGBX8888; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 133 | #else |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 134 | pdata->format = DRM_FORMAT_RGB565; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 135 | #endif |
| 136 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 137 | adf_id_t* dev_ids = nullptr; |
| 138 | ssize_t n_dev_ids = adf_devices(&dev_ids); |
| 139 | if (n_dev_ids == 0) { |
| 140 | return nullptr; |
| 141 | } else if (n_dev_ids < 0) { |
| 142 | fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids)); |
| 143 | return nullptr; |
| 144 | } |
| 145 | |
| 146 | pdata->intf_fd = -1; |
| 147 | |
| 148 | for (ssize_t i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) { |
| 149 | int err = adf_device_open(dev_ids[i], O_RDWR, &pdata->dev); |
| 150 | if (err < 0) { |
| 151 | fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err)); |
| 152 | continue; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 155 | err = adf_device_init(pdata, &pdata->dev); |
| 156 | if (err < 0) { |
| 157 | fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err)); |
| 158 | adf_device_close(&pdata->dev); |
| 159 | } |
| 160 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 161 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 162 | free(dev_ids); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 163 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 164 | if (pdata->intf_fd < 0) return nullptr; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 165 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 166 | GRSurface* ret = adf_flip(backend); |
| 167 | |
| 168 | adf_blank(backend, true); |
| 169 | adf_blank(backend, false); |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | static void adf_sync(adf_surface_pdata* surf) { |
| 175 | static constexpr unsigned int warningTimeout = 3000; |
| 176 | |
| 177 | if (surf == nullptr) return; |
| 178 | |
| 179 | if (surf->fence_fd >= 0) { |
| 180 | int err = sync_wait(surf->fence_fd, warningTimeout); |
| 181 | if (err < 0) { |
| 182 | perror("adf sync fence wait error\n"); |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 183 | } |
| 184 | |
xinglong.zhu | c4fa2c2 | 2016-08-05 14:52:22 +0800 | [diff] [blame] | 185 | close(surf->fence_fd); |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 186 | surf->fence_fd = -1; |
| 187 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 190 | static GRSurface* adf_flip(minui_backend* backend) { |
| 191 | adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend); |
| 192 | adf_surface_pdata* surf = &pdata->surfaces[pdata->current_surface]; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 193 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 194 | int fence_fd = |
| 195 | adf_interface_simple_post(pdata->intf_fd, pdata->eng_id, surf->base.width, surf->base.height, |
| 196 | pdata->format, surf->fd, surf->offset, surf->pitch, -1); |
| 197 | if (fence_fd >= 0) surf->fence_fd = fence_fd; |
| 198 | |
| 199 | pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces; |
| 200 | adf_sync(&pdata->surfaces[pdata->current_surface]); |
| 201 | return &pdata->surfaces[pdata->current_surface].base; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 204 | static void adf_blank(minui_backend* backend, bool blank) { |
| 205 | adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend); |
| 206 | adf_interface_blank(pdata->intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON); |
| 207 | } |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 208 | |
Tao Bao | 8f0e21b | 2017-02-07 13:00:18 -0800 | [diff] [blame] | 209 | static void adf_surface_destroy(adf_surface_pdata* surf) { |
| 210 | munmap(surf->base.data, surf->pitch * surf->base.height); |
| 211 | close(surf->fence_fd); |
| 212 | close(surf->fd); |
| 213 | } |
| 214 | |
| 215 | static void adf_exit(minui_backend* backend) { |
| 216 | adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend); |
| 217 | adf_device_close(&pdata->dev); |
| 218 | for (unsigned int i = 0; i < pdata->n_surfaces; i++) { |
| 219 | adf_surface_destroy(&pdata->surfaces[i]); |
| 220 | } |
| 221 | if (pdata->intf_fd >= 0) close(pdata->intf_fd); |
| 222 | free(pdata); |
| 223 | } |
| 224 | |
| 225 | minui_backend* open_adf() { |
| 226 | adf_pdata* pdata = static_cast<adf_pdata*>(calloc(1, sizeof(*pdata))); |
| 227 | if (!pdata) { |
| 228 | perror("allocating adf backend failed"); |
| 229 | return nullptr; |
| 230 | } |
| 231 | |
| 232 | pdata->base.init = adf_init; |
| 233 | pdata->base.flip = adf_flip; |
| 234 | pdata->base.blank = adf_blank; |
| 235 | pdata->base.exit = adf_exit; |
| 236 | |
| 237 | return &pdata->base; |
Greg Hackmann | 41909dd | 2014-04-25 10:39:50 -0700 | [diff] [blame] | 238 | } |