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