blob: 17f30d1d4baa0dfd09639ef41b21a91f8cc690f4 [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) {
70 close(surf->fd);
71 return -errno;
72 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070073
Tao Bao8f0e21b2017-02-07 13:00:18 -080074 return 0;
Greg Hackmann41909dd2014-04-25 10:39:50 -070075}
76
Tao Bao8f0e21b2017-02-07 13:00:18 -080077static int adf_interface_init(adf_pdata* pdata) {
78 adf_interface_data intf_data;
79 int err = adf_get_interface_data(pdata->intf_fd, &intf_data);
80 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -070081
Tao Bao8f0e21b2017-02-07 13:00:18 -080082 int ret = 0;
83 err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]);
84 if (err < 0) {
85 fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err));
86 ret = err;
87 goto done;
88 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070089
Tao Bao8f0e21b2017-02-07 13:00:18 -080090 err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[1]);
91 if (err < 0) {
92 fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err));
93 pdata->surfaces[1] = {};
94 pdata->n_surfaces = 1;
95 } else {
96 pdata->n_surfaces = 2;
97 }
Greg Hackmann41909dd2014-04-25 10:39:50 -070098
99done:
Tao Bao8f0e21b2017-02-07 13:00:18 -0800100 adf_free_interface_data(&intf_data);
101 return ret;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700102}
103
Tao Bao8f0e21b2017-02-07 13:00:18 -0800104static int adf_device_init(adf_pdata* pdata, adf_device* dev) {
105 adf_id_t intf_id;
106 int err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id, &pdata->eng_id);
107 if (err < 0) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700108
Tao Bao8f0e21b2017-02-07 13:00:18 -0800109 err = adf_device_attach(dev, pdata->eng_id, intf_id);
110 if (err < 0 && err != -EALREADY) return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700111
Tao Bao8f0e21b2017-02-07 13:00:18 -0800112 pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR);
113 if (pdata->intf_fd < 0) return pdata->intf_fd;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700114
Tao Bao8f0e21b2017-02-07 13:00:18 -0800115 err = adf_interface_init(pdata);
116 if (err < 0) {
117 close(pdata->intf_fd);
118 pdata->intf_fd = -1;
119 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700120
Tao Bao8f0e21b2017-02-07 13:00:18 -0800121 return err;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700122}
123
Tao Bao8f0e21b2017-02-07 13:00:18 -0800124static GRSurface* adf_init(minui_backend* backend) {
125 adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700126
Tony Kuofd778e32015-02-05 21:25:56 +0800127#if defined(RECOVERY_ABGR)
Tao Bao8f0e21b2017-02-07 13:00:18 -0800128 pdata->format = DRM_FORMAT_ABGR8888;
Tony Kuofd778e32015-02-05 21:25:56 +0800129#elif defined(RECOVERY_BGRA)
Tao Bao8f0e21b2017-02-07 13:00:18 -0800130 pdata->format = DRM_FORMAT_BGRA8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700131#elif defined(RECOVERY_RGBX)
Tao Bao8f0e21b2017-02-07 13:00:18 -0800132 pdata->format = DRM_FORMAT_RGBX8888;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700133#else
Tao Bao8f0e21b2017-02-07 13:00:18 -0800134 pdata->format = DRM_FORMAT_RGB565;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700135#endif
136
Tao Bao8f0e21b2017-02-07 13:00:18 -0800137 adf_id_t* dev_ids = nullptr;
138 ssize_t n_dev_ids = adf_devices(&dev_ids);
139 if (n_dev_ids == 0) {
140 return nullptr;
141 } else if (n_dev_ids < 0) {
142 fprintf(stderr, "enumerating adf devices failed: %s\n", strerror(-n_dev_ids));
143 return nullptr;
144 }
145
146 pdata->intf_fd = -1;
147
148 for (ssize_t i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) {
149 int err = adf_device_open(dev_ids[i], O_RDWR, &pdata->dev);
150 if (err < 0) {
151 fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], strerror(-err));
152 continue;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700153 }
154
Tao Bao8f0e21b2017-02-07 13:00:18 -0800155 err = adf_device_init(pdata, &pdata->dev);
156 if (err < 0) {
157 fprintf(stderr, "initializing adf device %u failed: %s\n", dev_ids[i], strerror(-err));
158 adf_device_close(&pdata->dev);
159 }
160 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700161
Tao Bao8f0e21b2017-02-07 13:00:18 -0800162 free(dev_ids);
Greg Hackmann41909dd2014-04-25 10:39:50 -0700163
Tao Bao8f0e21b2017-02-07 13:00:18 -0800164 if (pdata->intf_fd < 0) return nullptr;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700165
Tao Bao8f0e21b2017-02-07 13:00:18 -0800166 GRSurface* ret = adf_flip(backend);
167
168 adf_blank(backend, true);
169 adf_blank(backend, false);
170
171 return ret;
172}
173
174static void adf_sync(adf_surface_pdata* surf) {
175 static constexpr unsigned int warningTimeout = 3000;
176
177 if (surf == nullptr) return;
178
179 if (surf->fence_fd >= 0) {
180 int err = sync_wait(surf->fence_fd, warningTimeout);
181 if (err < 0) {
182 perror("adf sync fence wait error\n");
Greg Hackmann41909dd2014-04-25 10:39:50 -0700183 }
184
xinglong.zhuc4fa2c22016-08-05 14:52:22 +0800185 close(surf->fence_fd);
Tao Bao8f0e21b2017-02-07 13:00:18 -0800186 surf->fence_fd = -1;
187 }
Greg Hackmann41909dd2014-04-25 10:39:50 -0700188}
189
Tao Bao8f0e21b2017-02-07 13:00:18 -0800190static GRSurface* adf_flip(minui_backend* backend) {
191 adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
192 adf_surface_pdata* surf = &pdata->surfaces[pdata->current_surface];
Greg Hackmann41909dd2014-04-25 10:39:50 -0700193
Tao Bao8f0e21b2017-02-07 13:00:18 -0800194 int fence_fd =
195 adf_interface_simple_post(pdata->intf_fd, pdata->eng_id, surf->base.width, surf->base.height,
196 pdata->format, surf->fd, surf->offset, surf->pitch, -1);
197 if (fence_fd >= 0) surf->fence_fd = fence_fd;
198
199 pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces;
200 adf_sync(&pdata->surfaces[pdata->current_surface]);
201 return &pdata->surfaces[pdata->current_surface].base;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700202}
203
Tao Bao8f0e21b2017-02-07 13:00:18 -0800204static void adf_blank(minui_backend* backend, bool blank) {
205 adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
206 adf_interface_blank(pdata->intf_fd, blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
207}
Greg Hackmann41909dd2014-04-25 10:39:50 -0700208
Tao Bao8f0e21b2017-02-07 13:00:18 -0800209static void adf_surface_destroy(adf_surface_pdata* surf) {
210 munmap(surf->base.data, surf->pitch * surf->base.height);
211 close(surf->fence_fd);
212 close(surf->fd);
213}
214
215static void adf_exit(minui_backend* backend) {
216 adf_pdata* pdata = reinterpret_cast<adf_pdata*>(backend);
217 adf_device_close(&pdata->dev);
218 for (unsigned int i = 0; i < pdata->n_surfaces; i++) {
219 adf_surface_destroy(&pdata->surfaces[i]);
220 }
221 if (pdata->intf_fd >= 0) close(pdata->intf_fd);
222 free(pdata);
223}
224
225minui_backend* open_adf() {
226 adf_pdata* pdata = static_cast<adf_pdata*>(calloc(1, sizeof(*pdata)));
227 if (!pdata) {
228 perror("allocating adf backend failed");
229 return nullptr;
230 }
231
232 pdata->base.init = adf_init;
233 pdata->base.flip = adf_flip;
234 pdata->base.blank = adf_blank;
235 pdata->base.exit = adf_exit;
236
237 return &pdata->base;
Greg Hackmann41909dd2014-04-25 10:39:50 -0700238}