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