blob: 9ab0b06bf659bbde8329f172f7d48d19c285f40c [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
17#include <errno.h>
18#include <fcntl.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070019#include <stdio.h>
20#include <stdlib.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070021#include <sys/mman.h>
Tao Bao8f0e21b2017-02-07 13:00:18 -080022#include <unistd.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070023
24#include <adf/adf.h>
xinglong.zhuc4fa2c22016-08-05 14:52:22 +080025#include <sync/sync.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070026
27#include "graphics.h"
28
29struct adf_surface_pdata {
Tao Bao8f0e21b2017-02-07 13:00:18 -080030 GRSurface base;
31 int fence_fd;
32 int fd;
33 __u32 offset;
34 __u32 pitch;
Greg Hackmann41909dd2014-04-25 10:39:50 -070035};
36
37struct adf_pdata {
Tao Bao8f0e21b2017-02-07 13:00:18 -080038 minui_backend base;
39 int intf_fd;
40 adf_id_t eng_id;
41 __u32 format;
Greg Hackmann41909dd2014-04-25 10:39:50 -070042
Tao Bao8f0e21b2017-02-07 13:00:18 -080043 adf_device dev;
Jonathan Hamiltonbab6e492016-05-05 15:30:57 -070044
Tao Bao8f0e21b2017-02-07 13:00:18 -080045 unsigned int current_surface;
46 unsigned int n_surfaces;
47 adf_surface_pdata surfaces[2];
Greg Hackmann41909dd2014-04-25 10:39:50 -070048};
49
Tao Bao8f0e21b2017-02-07 13:00:18 -080050static GRSurface* adf_flip(minui_backend* backend);
51static void adf_blank(minui_backend* backend, bool blank);
Greg Hackmann41909dd2014-04-25 10:39:50 -070052
Tao Bao8f0e21b2017-02-07 13:00:18 -080053static int adf_surface_init(adf_pdata* pdata, drm_mode_modeinfo* mode, adf_surface_pdata* surf) {
54 *surf = {};
55 surf->fence_fd = -1;
56 surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, mode->vdisplay,
57 pdata->format, &surf->offset, &surf->pitch);
58 if (surf->fd < 0) {
59 return surf->fd;
60 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070061
Tao Bao8f0e21b2017-02-07 13:00:18 -080062 surf->base.width = mode->hdisplay;
63 surf->base.height = mode->vdisplay;
64 surf->base.row_bytes = surf->pitch;
65 surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4;
Greg Hackmann41909dd2014-04-25 10:39:50 -070066
Tao Bao8f0e21b2017-02-07 13:00:18 -080067 surf->base.data = static_cast<uint8_t*>(mmap(nullptr, surf->pitch * surf->base.height, PROT_WRITE,
68 MAP_SHARED, surf->fd, surf->offset));
69 if (surf->base.data == MAP_FAILED) {
Tao Baof04592b2017-02-09 12:59:19 -080070 int saved_errno = errno;
Tao Bao8f0e21b2017-02-07 13:00:18 -080071 close(surf->fd);
Tao Baof04592b2017-02-09 12:59:19 -080072 return -saved_errno;
Tao Bao8f0e21b2017-02-07 13:00:18 -080073 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070074
Tao Bao8f0e21b2017-02-07 13:00:18 -080075 return 0;
Greg Hackmann41909dd2014-04-25 10:39:50 -070076}
77
Tao Bao8f0e21b2017-02-07 13:00:18 -080078static int adf_interface_init(adf_pdata* pdata) {
79 adf_interface_data intf_data;
80 int err = adf_get_interface_data(pdata->intf_fd, &intf_data);
81 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070082
Tao Bao8f0e21b2017-02-07 13:00:18 -080083 int ret = 0;
84 err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]);
85 if (err < 0) {
86 fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err));
87 ret = err;
88 goto done;
89 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070090
Tao Bao8f0e21b2017-02-07 13:00:18 -080091 err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[1]);
92 if (err < 0) {
93 fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err));
94 pdata->surfaces[1] = {};
95 pdata->n_surfaces = 1;
96 } else {
97 pdata->n_surfaces = 2;
98 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070099
100done:
Tao Bao8f0e21b2017-02-07 13:00:18 -0800101 adf_free_interface_data(&intf_data);
102 return ret;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700103}
104
Tao Bao8f0e21b2017-02-07 13:00:18 -0800105static int adf_device_init(adf_pdata* pdata, adf_device* dev) {
106 adf_id_t intf_id;
107 int err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id, &pdata->eng_id);
108 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700109
Tao Bao8f0e21b2017-02-07 13:00:18 -0800110 err = adf_device_attach(dev, pdata->eng_id, intf_id);
111 if (err < 0 && err != -EALREADY) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700112
Tao Bao8f0e21b2017-02-07 13:00:18 -0800113 pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR);
114 if (pdata->intf_fd < 0) return pdata->intf_fd;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700115
Tao Bao8f0e21b2017-02-07 13:00:18 -0800116 err = adf_interface_init(pdata);
117 if (err < 0) {
118 close(pdata->intf_fd);
119 pdata->intf_fd = -1;
120 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700121
Tao Bao8f0e21b2017-02-07 13:00:18 -0800122 return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700123}
124
Tao Bao8f0e21b2017-02-07 13:00:18 -0800125static GRSurface* adf_init(minui_backend* backend) {
126 adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700127
Tony Kuofd778e32015-02-05 21:25:56 +0800128#if defined(RECOVERY_ABGR)
Tao Bao8f0e21b2017-02-07 13:00:18 -0800129 pdata->format = DRM_FORMAT_ABGR8888;
Tony Kuofd778e32015-02-05 21:25:56 +0800130#elif defined(RECOVERY_BGRA)
Tao Bao8f0e21b2017-02-07 13:00:18 -0800131 pdata->format = DRM_FORMAT_BGRA8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700132#elif defined(RECOVERY_RGBX)
Tao Bao8f0e21b2017-02-07 13:00:18 -0800133 pdata->format = DRM_FORMAT_RGBX8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700134#else
Tao Bao8f0e21b2017-02-07 13:00:18 -0800135 pdata->format = DRM_FORMAT_RGB565;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700136#endif
137
Tao Bao8f0e21b2017-02-07 13:00:18 -0800138 adf_id_t* dev_ids = nullptr;
139 ssize_t n_dev_ids = adf_devices(&dev_ids);
140 if (n_dev_ids == 0) {
141 return nullptr;
142 } else if (n_dev_ids < 0) {
143 fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids));
144 return nullptr;
145 }
146
147 pdata->intf_fd = -1;
148
149 for (ssize_t i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) {
150 int err = adf_device_open(dev_ids[i], O_RDWR, &pdata->dev);
151 if (err < 0) {
152 fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err));
153 continue;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700154 }
155
Tao Bao8f0e21b2017-02-07 13:00:18 -0800156 err = adf_device_init(pdata, &pdata->dev);
157 if (err < 0) {
158 fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err));
159 adf_device_close(&pdata->dev);
160 }
161 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700162
Tao Bao8f0e21b2017-02-07 13:00:18 -0800163 free(dev_ids);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700164
Tao Bao8f0e21b2017-02-07 13:00:18 -0800165 if (pdata->intf_fd < 0) return nullptr;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700166
Tao Bao8f0e21b2017-02-07 13:00:18 -0800167 GRSurface* ret = adf_flip(backend);
168
169 adf_blank(backend, true);
170 adf_blank(backend, false);
171
172 return ret;
173}
174
175static void adf_sync(adf_surface_pdata* surf) {
176 static constexpr unsigned int warningTimeout = 3000;
177
178 if (surf == nullptr) return;
179
180 if (surf->fence_fd >= 0) {
181 int err = sync_wait(surf->fence_fd, warningTimeout);
182 if (err < 0) {
183 perror("adf sync fence wait error\n");
Greg Hackmann41909dd2014-04-25 10:39:50 -0700184 }
185
xinglong.zhuc4fa2c22016-08-05 14:52:22 +0800186 close(surf->fence_fd);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800187 surf->fence_fd = -1;
188 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700189}
190
Tao Bao8f0e21b2017-02-07 13:00:18 -0800191static GRSurface* adf_flip(minui_backend* backend) {
192 adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
193 adf_surface_pdata* surf = &pdata->surfaces[pdata->current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700194
Tao Bao8f0e21b2017-02-07 13:00:18 -0800195 int fence_fd =
196 adf_interface_simple_post(pdata->intf_fd, pdata->eng_id, surf->base.width, surf->base.height,
197 pdata->format, surf->fd, surf->offset, surf->pitch, -1);
198 if (fence_fd >= 0) surf->fence_fd = fence_fd;
199
200 pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces;
201 adf_sync(&pdata->surfaces[pdata->current_surface]);
202 return &pdata->surfaces[pdata->current_surface].base;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700203}
204
Tao Bao8f0e21b2017-02-07 13:00:18 -0800205static void adf_blank(minui_backend* backend, bool blank) {
206 adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
207 adf_interface_blank(pdata->intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
208}
Greg Hackmann41909dd2014-04-25 10:39:50 -0700209
Tao Bao8f0e21b2017-02-07 13:00:18 -0800210static void adf_surface_destroy(adf_surface_pdata* surf) {
211 munmap(surf->base.data, surf->pitch * surf->base.height);
212 close(surf->fence_fd);
213 close(surf->fd);
214}
215
216static void adf_exit(minui_backend* backend) {
217 adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
218 adf_device_close(&pdata->dev);
219 for (unsigned int i = 0; i < pdata->n_surfaces; i++) {
220 adf_surface_destroy(&pdata->surfaces[i]);
221 }
222 if (pdata->intf_fd >= 0) close(pdata->intf_fd);
223 free(pdata);
224}
225
226minui_backend* open_adf() {
227 adf_pdata* pdata = static_cast<adf_pdata*>(calloc(1, sizeof(*pdata)));
228 if (!pdata) {
229 perror("allocating adf backend failed");
230 return nullptr;
231 }
232
233 pdata->base.init = adf_init;
234 pdata->base.flip = adf_flip;
235 pdata->base.blank = adf_blank;
236 pdata->base.exit = adf_exit;
237
238 return &pdata->base;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700239}