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