blob: 289c3be637424817914abe4cbaa333167f47f985 [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>
19#include <stdbool.h>
20#include <stdio.h>
21#include <stdlib.h>
Elliott Hughescd3c55a2015-01-29 20:50:08 -080022#include <string.h>
Greg Hackmann41909dd2014-04-25 10:39:50 -070023#include <unistd.h>
24
25#include <sys/cdefs.h>
26#include <sys/mman.h>
27
28#include <adf/adf.h>
29
30#include "graphics.h"
31
32struct adf_surface_pdata {
33 GRSurface base;
34 int fd;
35 __u32 offset;
36 __u32 pitch;
37};
38
39struct adf_pdata {
40 minui_backend base;
41 int intf_fd;
42 adf_id_t eng_id;
43 __u32 format;
44
45 unsigned int current_surface;
46 unsigned int n_surfaces;
47 struct adf_surface_pdata surfaces[2];
48};
49
50static gr_surface adf_flip(struct minui_backend *backend);
51static void adf_blank(struct minui_backend *backend, bool blank);
52
53static int adf_surface_init(struct adf_pdata *pdata,
54 struct drm_mode_modeinfo *mode, struct adf_surface_pdata *surf)
55{
56 memset(surf, 0, sizeof(*surf));
57
58 surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay,
59 mode->vdisplay, pdata->format, &surf->offset, &surf->pitch);
60 if (surf->fd < 0)
61 return surf->fd;
62
63 surf->base.width = mode->hdisplay;
64 surf->base.height = mode->vdisplay;
65 surf->base.row_bytes = surf->pitch;
66 surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4;
67
68 surf->base.data = mmap(NULL, surf->pitch * surf->base.height, PROT_WRITE,
69 MAP_SHARED, surf->fd, surf->offset);
70 if (surf->base.data == MAP_FAILED) {
71 close(surf->fd);
72 return -errno;
73 }
74
75 return 0;
76}
77
78static int adf_interface_init(struct adf_pdata *pdata)
79{
80 struct adf_interface_data intf_data;
81 int ret = 0;
82 int err;
83
84 err = adf_get_interface_data(pdata->intf_fd, &intf_data);
85 if (err < 0)
86 return err;
87
88 err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]);
89 if (err < 0) {
90 fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err));
91 ret = err;
92 goto done;
93 }
94
95 err = adf_surface_init(pdata, &intf_data.current_mode,
96 &pdata->surfaces[1]);
97 if (err < 0) {
98 fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err));
99 memset(&pdata->surfaces[1], 0, sizeof(pdata->surfaces[1]));
100 pdata->n_surfaces = 1;
101 } else {
102 pdata->n_surfaces = 2;
103 }
104
105done:
106 adf_free_interface_data(&intf_data);
107 return ret;
108}
109
110static int adf_device_init(struct adf_pdata *pdata, struct adf_device *dev)
111{
112 adf_id_t intf_id;
113 int intf_fd;
114 int err;
115
116 err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id,
117 &pdata->eng_id);
118 if (err < 0)
119 return err;
120
121 err = adf_device_attach(dev, pdata->eng_id, intf_id);
122 if (err < 0 && err != -EALREADY)
123 return err;
124
125 pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR);
126 if (pdata->intf_fd < 0)
127 return pdata->intf_fd;
128
129 err = adf_interface_init(pdata);
130 if (err < 0) {
131 close(pdata->intf_fd);
132 pdata->intf_fd = -1;
133 }
134
135 return err;
136}
137
138static gr_surface adf_init(minui_backend *backend)
139{
140 struct adf_pdata *pdata = (struct adf_pdata *)backend;
141 adf_id_t *dev_ids = NULL;
142 ssize_t n_dev_ids, i;
143 gr_surface ret;
144
145#if defined(RECOVERY_BGRA)
146 pdata->format = DRM_FORMAT_BGRA8888;
147#elif defined(RECOVERY_RGBX)
148 pdata->format = DRM_FORMAT_RGBX8888;
149#else
150 pdata->format = DRM_FORMAT_RGB565;
151#endif
152
153 n_dev_ids = adf_devices(&dev_ids);
154 if (n_dev_ids == 0) {
155 return NULL;
156 } else if (n_dev_ids < 0) {
157 fprintf(stderr, "enumerating adf devices failed: %s\n",
158 strerror(-n_dev_ids));
159 return NULL;
160 }
161
162 pdata->intf_fd = -1;
163
164 for (i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) {
165 struct adf_device dev;
166
167 int err = adf_device_open(dev_ids[i], O_RDWR, &dev);
168 if (err < 0) {
169 fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i],
170 strerror(-err));
171 continue;
172 }
173
174 err = adf_device_init(pdata, &dev);
175 if (err < 0)
176 fprintf(stderr, "initializing adf device %u failed: %s\n",
177 dev_ids[i], strerror(-err));
178
179 adf_device_close(&dev);
180 }
181
182 free(dev_ids);
183
184 if (pdata->intf_fd < 0)
185 return NULL;
186
187 ret = adf_flip(backend);
188
189 adf_blank(backend, true);
190 adf_blank(backend, false);
191
192 return ret;
193}
194
195static gr_surface adf_flip(struct minui_backend *backend)
196{
197 struct adf_pdata *pdata = (struct adf_pdata *)backend;
198 struct adf_surface_pdata *surf = &pdata->surfaces[pdata->current_surface];
199
200 int fence_fd = adf_interface_simple_post(pdata->intf_fd, pdata->eng_id,
201 surf->base.width, surf->base.height, pdata->format, surf->fd,
202 surf->offset, surf->pitch, -1);
203 if (fence_fd >= 0)
204 close(fence_fd);
205
206 pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces;
207 return &pdata->surfaces[pdata->current_surface].base;
208}
209
210static void adf_blank(struct minui_backend *backend, bool blank)
211{
212 struct adf_pdata *pdata = (struct adf_pdata *)backend;
213 adf_interface_blank(pdata->intf_fd,
214 blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
215}
216
217static void adf_surface_destroy(struct adf_surface_pdata *surf)
218{
219 munmap(surf->base.data, surf->pitch * surf->base.height);
220 close(surf->fd);
221}
222
223static void adf_exit(struct minui_backend *backend)
224{
225 struct adf_pdata *pdata = (struct adf_pdata *)backend;
226 unsigned int i;
227
228 for (i = 0; i < pdata->n_surfaces; i++)
229 adf_surface_destroy(&pdata->surfaces[i]);
230 if (pdata->intf_fd >= 0)
231 close(pdata->intf_fd);
232 free(pdata);
233}
234
235minui_backend *open_adf()
236{
237 struct adf_pdata *pdata = calloc(1, sizeof(*pdata));
238 if (!pdata) {
239 perror("allocating adf backend failed");
240 return NULL;
241 }
242
243 pdata->base.init = adf_init;
244 pdata->base.flip = adf_flip;
245 pdata->base.blank = adf_blank;
246 pdata->base.exit = adf_exit;
247 return &pdata->base;
248}