blob: 7524bfac43e6c65c26dddc5569cc5df3cd866027 [file] [log] [blame]
Greg Hackmann41909dd2014-04-25 10:39:50 -07001/*
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 Bao557fa1f2017-02-07 12:51:00 -080017#include "graphics_adf.h"
18
Greg Hackmann41909dd2014-04-25 10:39:50 -070019#include <errno.h>
20#include <fcntl.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070021#include <stdio.h>
22#include <stdlib.h>
Tao Bao1b18cf52018-10-31 00:12:11 -070023#include <string.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070024#include <sys/mman.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070025#include <unistd.h>
26
Greg Hackmann41909dd2014-04-25 10:39:50 -070027#include <adf/adf.h>
Ethan Yonker8373cfe2017-09-08 06:50:54 -050028#ifdef HAS_LIBSYNC
xinglong.zhuc4fa2c22016-08-05 14:52:22 +080029#include <sync/sync.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070030#endif
31
Tao Bao557fa1f2017-02-07 12:51:00 -080032#include "minui/minui.h"
Greg Hackmann41909dd2014-04-25 10:39:50 -070033
Tao Bao1b18cf52018-10-31 00:12:11 -070034GRSurfaceAdf::~GRSurfaceAdf() {
35 if (mmapped_buffer_) {
36 munmap(mmapped_buffer_, pitch * height);
Tao Bao8f0e21b2017-02-07 13:00:18 -080037 }
Tao Bao1b18cf52018-10-31 00:12:11 -070038 if (fence_fd != -1) {
39 close(fence_fd);
Tao Bao8f0e21b2017-02-07 13:00:18 -080040 }
Tao Bao1b18cf52018-10-31 00:12:11 -070041 if (fd != -1) {
42 close(fd);
43 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070044}
45
Tao Bao1b18cf52018-10-31 00:12:11 -070046std::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 Hackmann41909dd2014-04-25 10:39:50 -070056 }
57
Tao Bao1b18cf52018-10-31 00:12:11 -070058 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 Hackmann41909dd2014-04-25 10:39:50 -070061
Tao Bao92bdb5a2018-10-21 12:12:37 -070062 auto mmapped =
63 mmap(nullptr, surf->pitch * surf->height, PROT_WRITE, MAP_SHARED, surf->fd, surf->offset);
64 if (mmapped == MAP_FAILED) {
Tao Bao1b18cf52018-10-31 00:12:11 -070065 *err = -errno;
66 return nullptr;
Greg Hackmann41909dd2014-04-25 10:39:50 -070067 }
Tao Bao92bdb5a2018-10-21 12:12:37 -070068 surf->mmapped_buffer_ = static_cast<uint8_t*>(mmapped);
Tao Bao1b18cf52018-10-31 00:12:11 -070069 return surf;
Greg Hackmann41909dd2014-04-25 10:39:50 -070070}
71
Tao Bao1b18cf52018-10-31 00:12:11 -070072MinuiBackendAdf::MinuiBackendAdf() : intf_fd(-1), dev(), current_surface(0), n_surfaces(0) {}
73
Tao Bao557fa1f2017-02-07 12:51:00 -080074int MinuiBackendAdf::InterfaceInit() {
Tao Bao8f0e21b2017-02-07 13:00:18 -080075 adf_interface_data intf_data;
Tao Bao1b18cf52018-10-31 00:12:11 -070076 if (int err = adf_get_interface_data(intf_fd, &intf_data); err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070077
Tao Bao1b18cf52018-10-31 00:12:11 -070078 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 Bao8f0e21b2017-02-07 13:00:18 -080082 goto done;
83 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070084
Tao Bao1b18cf52018-10-31 00:12:11 -070085 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 Bao557fa1f2017-02-07 12:51:00 -080088 n_surfaces = 1;
Tao Bao8f0e21b2017-02-07 13:00:18 -080089 } else {
Tao Bao557fa1f2017-02-07 12:51:00 -080090 n_surfaces = 2;
Tao Bao8f0e21b2017-02-07 13:00:18 -080091 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070092
93done:
Tao Bao8f0e21b2017-02-07 13:00:18 -080094 adf_free_interface_data(&intf_data);
Tao Bao1b18cf52018-10-31 00:12:11 -070095 return result;
Greg Hackmann41909dd2014-04-25 10:39:50 -070096}
97
Tao Bao557fa1f2017-02-07 12:51:00 -080098int MinuiBackendAdf::DeviceInit(adf_device* dev) {
Tao Bao8f0e21b2017-02-07 13:00:18 -080099 adf_id_t intf_id;
Tao Bao557fa1f2017-02-07 12:51:00 -0800100 int err = adf_find_simple_post_configuration(dev, &format, 1, &intf_id, &eng_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800101 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700102
Tao Bao557fa1f2017-02-07 12:51:00 -0800103 err = adf_device_attach(dev, eng_id, intf_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800104 if (err < 0 && err != -EALREADY) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700105
Tao Baob5492432019-01-16 09:29:17 -0800106 intf_fd = adf_interface_open(dev, intf_id, O_RDWR | O_CLOEXEC);
Tao Bao557fa1f2017-02-07 12:51:00 -0800107 if (intf_fd < 0) return intf_fd;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700108
Tao Bao557fa1f2017-02-07 12:51:00 -0800109 err = InterfaceInit();
Tao Bao8f0e21b2017-02-07 13:00:18 -0800110 if (err < 0) {
Tao Bao557fa1f2017-02-07 12:51:00 -0800111 close(intf_fd);
112 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800113 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700114
Tao Bao8f0e21b2017-02-07 13:00:18 -0800115 return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700116}
117
Tao Bao557fa1f2017-02-07 12:51:00 -0800118GRSurface* MinuiBackendAdf::Init() {
Tao Baoed876a72018-07-31 21:32:50 -0700119 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 Hackmann41909dd2014-04-25 10:39:50 -0700129
Tao Bao8f0e21b2017-02-07 13:00:18 -0800130 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 Bao557fa1f2017-02-07 12:51:00 -0800139 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800140
Tao Bao557fa1f2017-02-07 12:51:00 -0800141 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 Bao8f0e21b2017-02-07 13:00:18 -0800143 if (err < 0) {
144 fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err));
145 continue;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700146 }
147
Tao Bao557fa1f2017-02-07 12:51:00 -0800148 err = DeviceInit(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800149 if (err < 0) {
150 fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err));
Tao Bao557fa1f2017-02-07 12:51:00 -0800151 adf_device_close(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800152 }
153 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700154
Tao Bao8f0e21b2017-02-07 13:00:18 -0800155 free(dev_ids);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700156
Tao Bao557fa1f2017-02-07 12:51:00 -0800157 if (intf_fd < 0) return nullptr;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700158
Tao Bao557fa1f2017-02-07 12:51:00 -0800159 GRSurface* ret = Flip();
Tao Bao8f0e21b2017-02-07 13:00:18 -0800160
Tao Bao557fa1f2017-02-07 12:51:00 -0800161 Blank(true);
162 Blank(false);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800163
164 return ret;
165}
166
bigbiff20edca82020-07-03 09:55:28 -0400167void MinuiBackendAdf::Sync(__unused GRSurfaceAdf* surf) {
168#ifdef HAS_LIBSYNC
Tao Bao1b18cf52018-10-31 00:12:11 -0700169 static constexpr unsigned int kWarningTimeout = 3000;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800170
171 if (surf == nullptr) return;
172
173 if (surf->fence_fd >= 0) {
Tao Bao1b18cf52018-10-31 00:12:11 -0700174 int err = sync_wait(surf->fence_fd, kWarningTimeout);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800175 if (err < 0) {
176 perror("adf sync fence wait error\n");
Greg Hackmann41909dd2014-04-25 10:39:50 -0700177 }
178
xinglong.zhuc4fa2c22016-08-05 14:52:22 +0800179 close(surf->fence_fd);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800180 surf->fence_fd = -1;
181 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500182#endif
Greg Hackmann41909dd2014-04-25 10:39:50 -0700183}
184
Tao Bao557fa1f2017-02-07 12:51:00 -0800185GRSurface* MinuiBackendAdf::Flip() {
Tao Bao1b18cf52018-10-31 00:12:11 -0700186 const auto& surf = surfaces[current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700187
Tao Bao557fa1f2017-02-07 12:51:00 -0800188 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 Bao8f0e21b2017-02-07 13:00:18 -0800190 if (fence_fd >= 0) surf->fence_fd = fence_fd;
191
Tao Bao557fa1f2017-02-07 12:51:00 -0800192 current_surface = (current_surface + 1) % n_surfaces;
Tao Bao1b18cf52018-10-31 00:12:11 -0700193 Sync(surfaces[current_surface].get());
194 return surfaces[current_surface].get();
Greg Hackmann41909dd2014-04-25 10:39:50 -0700195}
196
Tao Bao557fa1f2017-02-07 12:51:00 -0800197void MinuiBackendAdf::Blank(bool blank) {
198 adf_interface_blank(intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800199}
Greg Hackmann41909dd2014-04-25 10:39:50 -0700200
Tao Bao557fa1f2017-02-07 12:51:00 -0800201MinuiBackendAdf::~MinuiBackendAdf() {
202 adf_device_close(&dev);
Tao Bao557fa1f2017-02-07 12:51:00 -0800203 if (intf_fd >= 0) close(intf_fd);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700204}