blob: 79f4db8b5df13a6ff6ccd103f0a73f85b8191d04 [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>
Greg Hackmann41909dd2014-04-25 10:39:50 -070023#include <sys/mman.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070024#include <unistd.h>
25
Greg Hackmann41909dd2014-04-25 10:39:50 -070026#include <adf/adf.h>
Ethan Yonker8373cfe2017-09-08 06:50:54 -050027#ifdef HAS_LIBSYNC
xinglong.zhuc4fa2c22016-08-05 14:52:22 +080028#include <sync/sync.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070029#endif
30
Tao Bao557fa1f2017-02-07 12:51:00 -080031#include "minui/minui.h"
Greg Hackmann41909dd2014-04-25 10:39:50 -070032
Tao Bao557fa1f2017-02-07 12:51:00 -080033MinuiBackendAdf::MinuiBackendAdf() : intf_fd(-1), dev(), n_surfaces(0), surfaces() {}
Greg Hackmann41909dd2014-04-25 10:39:50 -070034
Tao Bao557fa1f2017-02-07 12:51:00 -080035int MinuiBackendAdf::SurfaceInit(const drm_mode_modeinfo* mode, GRSurfaceAdf* surf) {
Tao Bao8f0e21b2017-02-07 13:00:18 -080036 *surf = {};
37 surf->fence_fd = -1;
Tao Bao557fa1f2017-02-07 12:51:00 -080038 surf->fd = adf_interface_simple_buffer_alloc(intf_fd, mode->hdisplay, mode->vdisplay, format,
39 &surf->offset, &surf->pitch);
Tao Bao8f0e21b2017-02-07 13:00:18 -080040 if (surf->fd < 0) {
41 return surf->fd;
42 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070043
Tao Bao557fa1f2017-02-07 12:51:00 -080044 surf->width = mode->hdisplay;
45 surf->height = mode->vdisplay;
46 surf->row_bytes = surf->pitch;
47 surf->pixel_bytes = (format == DRM_FORMAT_RGB565) ? 2 : 4;
Greg Hackmann41909dd2014-04-25 10:39:50 -070048
Tao Bao557fa1f2017-02-07 12:51:00 -080049 surf->data = static_cast<uint8_t*>(
50 mmap(nullptr, surf->pitch * surf->height, PROT_WRITE, MAP_SHARED, surf->fd, surf->offset));
51 if (surf->data == MAP_FAILED) {
Tao Baof04592b2017-02-09 12:59:19 -080052 int saved_errno = errno;
Greg Hackmann41909dd2014-04-25 10:39:50 -070053 close(surf->fd);
Tao Baof04592b2017-02-09 12:59:19 -080054 return -saved_errno;
Tao Bao8f0e21b2017-02-07 13:00:18 -080055 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070056
Tao Bao8f0e21b2017-02-07 13:00:18 -080057 return 0;
Greg Hackmann41909dd2014-04-25 10:39:50 -070058}
59
Tao Bao557fa1f2017-02-07 12:51:00 -080060int MinuiBackendAdf::InterfaceInit() {
Tao Bao8f0e21b2017-02-07 13:00:18 -080061 adf_interface_data intf_data;
Tao Bao557fa1f2017-02-07 12:51:00 -080062 int err = adf_get_interface_data(intf_fd, &intf_data);
Tao Bao8f0e21b2017-02-07 13:00:18 -080063 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070064
Tao Bao8f0e21b2017-02-07 13:00:18 -080065 int ret = 0;
Tao Bao557fa1f2017-02-07 12:51:00 -080066 err = SurfaceInit(&intf_data.current_mode, &surfaces[0]);
Tao Bao8f0e21b2017-02-07 13:00:18 -080067 if (err < 0) {
68 fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err));
69 ret = err;
70 goto done;
71 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070072
Tao Bao557fa1f2017-02-07 12:51:00 -080073 err = SurfaceInit(&intf_data.current_mode, &surfaces[1]);
Tao Bao8f0e21b2017-02-07 13:00:18 -080074 if (err < 0) {
75 fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err));
Tao Bao557fa1f2017-02-07 12:51:00 -080076 surfaces[1] = {};
77 n_surfaces = 1;
Tao Bao8f0e21b2017-02-07 13:00:18 -080078 } else {
Tao Bao557fa1f2017-02-07 12:51:00 -080079 n_surfaces = 2;
Tao Bao8f0e21b2017-02-07 13:00:18 -080080 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070081
82done:
Tao Bao8f0e21b2017-02-07 13:00:18 -080083 adf_free_interface_data(&intf_data);
84 return ret;
Greg Hackmann41909dd2014-04-25 10:39:50 -070085}
86
Tao Bao557fa1f2017-02-07 12:51:00 -080087int MinuiBackendAdf::DeviceInit(adf_device* dev) {
Tao Bao8f0e21b2017-02-07 13:00:18 -080088 adf_id_t intf_id;
Tao Bao557fa1f2017-02-07 12:51:00 -080089 int err = adf_find_simple_post_configuration(dev, &format, 1, &intf_id, &eng_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -080090 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070091
Tao Bao557fa1f2017-02-07 12:51:00 -080092 err = adf_device_attach(dev, eng_id, intf_id);
Tao Bao8f0e21b2017-02-07 13:00:18 -080093 if (err < 0 && err != -EALREADY) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070094
Tao Bao557fa1f2017-02-07 12:51:00 -080095 intf_fd = adf_interface_open(dev, intf_id, O_RDWR);
96 if (intf_fd < 0) return intf_fd;
Greg Hackmann41909dd2014-04-25 10:39:50 -070097
Tao Bao557fa1f2017-02-07 12:51:00 -080098 err = InterfaceInit();
Tao Bao8f0e21b2017-02-07 13:00:18 -080099 if (err < 0) {
Tao Bao557fa1f2017-02-07 12:51:00 -0800100 close(intf_fd);
101 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800102 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700103
Tao Bao8f0e21b2017-02-07 13:00:18 -0800104 return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700105}
106
Tao Bao557fa1f2017-02-07 12:51:00 -0800107GRSurface* MinuiBackendAdf::Init() {
Greg Hackmann41909dd2014-04-25 10:39:50 -0700108#if defined(RECOVERY_ABGR)
Tao Bao557fa1f2017-02-07 12:51:00 -0800109 format = DRM_FORMAT_ABGR8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700110#elif defined(RECOVERY_BGRA)
Tao Bao557fa1f2017-02-07 12:51:00 -0800111 format = DRM_FORMAT_BGRA8888;
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500112#elif defined(RECOVERY_RGBA)
113 format = DRM_FORMAT_RGBA8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700114#elif defined(RECOVERY_RGBX)
Tao Bao557fa1f2017-02-07 12:51:00 -0800115 format = DRM_FORMAT_RGBX8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700116#else
Tao Bao557fa1f2017-02-07 12:51:00 -0800117 format = DRM_FORMAT_RGB565;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700118#endif
119
Tao Bao8f0e21b2017-02-07 13:00:18 -0800120 adf_id_t* dev_ids = nullptr;
121 ssize_t n_dev_ids = adf_devices(&dev_ids);
122 if (n_dev_ids == 0) {
123 return nullptr;
124 } else if (n_dev_ids < 0) {
125 fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids));
126 return nullptr;
127 }
128
Tao Bao557fa1f2017-02-07 12:51:00 -0800129 intf_fd = -1;
Tao Bao8f0e21b2017-02-07 13:00:18 -0800130
Tao Bao557fa1f2017-02-07 12:51:00 -0800131 for (ssize_t i = 0; i < n_dev_ids && intf_fd < 0; i++) {
132 int err = adf_device_open(dev_ids[i], O_RDWR, &dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800133 if (err < 0) {
134 fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err));
135 continue;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700136 }
137
Tao Bao557fa1f2017-02-07 12:51:00 -0800138 err = DeviceInit(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800139 if (err < 0) {
140 fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err));
Tao Bao557fa1f2017-02-07 12:51:00 -0800141 adf_device_close(&dev);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800142 }
143 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700144
Tao Bao8f0e21b2017-02-07 13:00:18 -0800145 free(dev_ids);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700146
Tao Bao557fa1f2017-02-07 12:51:00 -0800147 if (intf_fd < 0) return nullptr;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700148
Tao Bao557fa1f2017-02-07 12:51:00 -0800149 GRSurface* ret = Flip();
Tao Bao8f0e21b2017-02-07 13:00:18 -0800150
Tao Bao557fa1f2017-02-07 12:51:00 -0800151 Blank(true);
152 Blank(false);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800153
154 return ret;
155}
156
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500157void MinuiBackendAdf::Sync(__unused GRSurfaceAdf* surf) {
158#ifdef HAS_LIBSYNC
Tao Bao8f0e21b2017-02-07 13:00:18 -0800159 static constexpr unsigned int warningTimeout = 3000;
160
161 if (surf == nullptr) return;
162
163 if (surf->fence_fd >= 0) {
164 int err = sync_wait(surf->fence_fd, warningTimeout);
165 if (err < 0) {
166 perror("adf sync fence wait error\n");
Greg Hackmann41909dd2014-04-25 10:39:50 -0700167 }
168
xinglong.zhuc4fa2c22016-08-05 14:52:22 +0800169 close(surf->fence_fd);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800170 surf->fence_fd = -1;
171 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500172#endif
Greg Hackmann41909dd2014-04-25 10:39:50 -0700173}
174
Tao Bao557fa1f2017-02-07 12:51:00 -0800175GRSurface* MinuiBackendAdf::Flip() {
176 GRSurfaceAdf* surf = &surfaces[current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700177
Tao Bao557fa1f2017-02-07 12:51:00 -0800178 int fence_fd = adf_interface_simple_post(intf_fd, eng_id, surf->width, surf->height, format,
179 surf->fd, surf->offset, surf->pitch, -1);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800180 if (fence_fd >= 0) surf->fence_fd = fence_fd;
181
Tao Bao557fa1f2017-02-07 12:51:00 -0800182 current_surface = (current_surface + 1) % n_surfaces;
183 Sync(&surfaces[current_surface]);
184 return &surfaces[current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700185}
186
Tao Bao557fa1f2017-02-07 12:51:00 -0800187void MinuiBackendAdf::Blank(bool blank) {
188 adf_interface_blank(intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800189}
Greg Hackmann41909dd2014-04-25 10:39:50 -0700190
Tao Bao557fa1f2017-02-07 12:51:00 -0800191void MinuiBackendAdf::SurfaceDestroy(GRSurfaceAdf* surf) {
192 munmap(surf->data, surf->pitch * surf->height);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800193 close(surf->fence_fd);
194 close(surf->fd);
195}
196
Tao Bao557fa1f2017-02-07 12:51:00 -0800197MinuiBackendAdf::~MinuiBackendAdf() {
198 adf_device_close(&dev);
199 for (unsigned int i = 0; i < n_surfaces; i++) {
200 SurfaceDestroy(&surfaces[i]);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800201 }
Tao Bao557fa1f2017-02-07 12:51:00 -0800202 if (intf_fd >= 0) close(intf_fd);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700203}