-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
323 lines (267 loc) · 9.74 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
#include <linux/memfd.h>
#include <gbm.h>
#include "gbm_backend_abi.h"
#include <hybris/gralloc/gralloc.h>
#include <hardware/gralloc.h>
#include <assert.h>
struct gbm_hybris_bo {
struct gbm_bo base;
buffer_handle_t handle;
};
static const struct gbm_core *core;
int memfd_create(const char *name, unsigned int flags);
struct gbm_hybris_bo *gbm_hybris_bo(struct gbm_bo *bo)
{
return (struct gbm_hybris_bo *) bo;
}
static int get_hal_pixel_format(uint32_t gbm_format)
{
int format;
switch (gbm_format) {
case GBM_FORMAT_ABGR8888:
format = HAL_PIXEL_FORMAT_RGBA_8888;
break;
case GBM_FORMAT_XBGR8888:
format = HAL_PIXEL_FORMAT_RGBX_8888;
break;
case GBM_FORMAT_RGB888:
format = HAL_PIXEL_FORMAT_RGB_888;
break;
case GBM_FORMAT_RGB565:
format = HAL_PIXEL_FORMAT_RGB_565;
break;
case GBM_FORMAT_ARGB8888:
format = HAL_PIXEL_FORMAT_BGRA_8888;
break;
case GBM_FORMAT_GR88:
/* GR88 corresponds to YV12 which is planar */
format = HAL_PIXEL_FORMAT_YV12;
break;
case GBM_FORMAT_ABGR16161616F:
format = HAL_PIXEL_FORMAT_RGBA_FP16;
break;
case GBM_FORMAT_ABGR2101010:
format = HAL_PIXEL_FORMAT_RGBA_1010102;
break;
default:
format = HAL_PIXEL_FORMAT_RGBA_8888; // Invalid or unsupported format assume RGBA8888
break;
}
return format;
}
struct gbm_bo* hybris_gbm_bo_create(struct gbm_device* device, uint32_t width, uint32_t height, uint32_t format, uint32_t flags, const uint64_t *modifiers, const unsigned int count) {
printf("[libgbm-hybris1] gbm_bo_create called with width: %u, height: %u, format: %u, flags: %u\n", width, height, format, flags);
if (!device) {
fprintf(stderr, "[libgbm-hybris] Invalid GBM device.\n");
return NULL;
}
struct gbm_hybris_bo *bo;
// Malloc dont work for unknown reason
bo = calloc(1, sizeof(gbm_hybris_bo));
// Not inited in libgbm?
bo->base.v0.user_data = NULL;
if (!bo) {
fprintf(stderr, "[libgbm-hybris] Failed to allocate memory for GBM buffer object.\n");
return NULL;
}
format = core->v0.format_canonicalize(format);
bo->base.gbm = device;
bo->base.v0.width = width;
bo->base.v0.height = height;
bo->base.v0.format = format;
int usage = 0;
if (flags & GBM_BO_USE_SCANOUT)
usage |= GRALLOC_USAGE_HW_FB;
if (flags & GBM_BO_USE_RENDERING)
usage |= GRALLOC_USAGE_HW_RENDER;
if (flags & GBM_BO_USE_LINEAR)
usage |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_RARELY;
int stride = 0;
buffer_handle_t handle = NULL;
int ret = hybris_gralloc_allocate(width, height, get_hal_pixel_format(format), GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER, &handle, &stride);
if (ret != 0) {
fprintf(stderr, "[libgbm-hybris] hybris_gralloc_allocate failed: %d\n", ret);
free(bo);
return NULL;
}
bo->handle = handle;
bo->base.v0.stride = stride;
fprintf(stderr, "[libgbm-hybris] Bo created\n");
return &bo->base;
}
struct gbm_bo *hybris_gbm_bo_create_with_modifiers(struct gbm_device *gbm,
uint32_t width, uint32_t height,
uint32_t format,
const uint64_t *modifiers,
const unsigned int count)
{
//TBD: it do not work that way :D
return gbm_bo_create(gbm, width, height, format, 0);
}
struct gbm_bo * hybris_gbm_bo_create_with_modifiers2(struct gbm_device *gbm, uint32_t width, uint32_t height, uint32_t format, const uint64_t *modifiers, const unsigned int count, uint32_t flags){
//TBD
printf("[libgbm-hybris] gbm_bo_create_with_modifiers2\n");
return NULL;
}
struct gbm_bo *hybris_gbm_bo_import(struct gbm_device *gbm, uint32_t type, void *buffer, uint32_t usage){
// How do that even work with fake dma buf's?
printf("[libgbm-hybris] gbm_bo_import called\n");
return NULL;
}
// Suprisingly not part of libgbm
uint32_t hybris_gbm_bo_get_stride(struct gbm_bo* bo, int plane) {
printf("[libgbm-hybris] gbm_bo_get_stride called\n");
return bo ? (uint32_t)(bo->v0.stride) : 0;
}
uint32_t hybris_gbm_bo_get_stride_for_plane(struct gbm_bo *bo, int plane)
{
//TBD
printf("[libgbm-hybris] gbm_bo_get_stride_for_plane called\n");
return 0;
}
uint64_t hybris_gbm_bo_get_modifier(struct gbm_bo* bo) {
//TBD: Implement modifier
printf("[libgbm-hybris] gbm_bo_get_modifier called\n");
return 0;
}
void* hybris_gbm_bo_map(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint32_t flags, uint32_t *stride, void **map_data) {
//TBD: Implement based on grlloc lock
printf("[libgbm-hybris] gbm_bo_map called with x: %u, y: %u, width: %u, height: %u, flags: %u\n", x, y, width, height, flags);
return NULL;
}
void hybris_gbm_surface_destroy(struct gbm_surface *surf) {
//TBD: Implement surfaces
printf("[libgbm-hybris] gbm_surface_destroy called\n");
}
struct gbm_bo* hybris_gbm_surface_lock_front_buffer(struct gbm_surface* surface) {
//TBD: Implement surfaces
printf("[libgbm-hybris] gbm_surface_lock_front_buffer called\n");
return (struct gbm_bo*)malloc(sizeof(struct gbm_bo));
}
void hybris_gbm_surface_release_buffer(struct gbm_surface* surface, struct gbm_bo* bo) {
//TBD: Implement surfaces
printf("[libgbm-hybris] gbm_surface_release_buffer called\n");
if (bo) {
free(bo);
}
}
int hybris_gbm_bo_get_fd(struct gbm_bo* _bo) {
if(!_bo) {
printf("[libgbm-hybris] gbm_bo_get_fd missing bo\n");
return -1;
}
struct gbm_hybris_bo *bo = gbm_hybris_bo(_bo);
if(!bo || !bo->handle) {
printf("[libgbm-hybris] gbm_bo_get_fd missing bo->handle\n");
return -1;
}
if(!bo->handle) {
printf("[libgbm-hybris] missing handle\n");
return -1;
}
printf("[libgbm-hybris] gbm_bo_get_fd called version: %d numFds: %d numInts: %d\n", bo->handle->version, bo->handle->numFds, bo->handle->numInts);
int fd = memfd_create("whatever", MFD_CLOEXEC);
if (fd == -1) {
printf("[libgbm-hybris] memfd_create failed\n");
return -1;
}
size_t handle_size = sizeof(native_handle_t) + sizeof(int) * (bo->handle->numFds + bo->handle->numInts);
printf("[libgbm-hybris] fd: %d going to write %d bytes\n", fd, handle_size);
printf("[libgbm-hybris] data:");
for(int i=0; i<bo->handle->numFds + bo->handle->numInts; i++) {
printf(" %d ", bo->handle->data[i]);
}
printf("\n");
if(write(fd, bo->handle, handle_size) != handle_size) {
printf("[libgbm-hybris] failed to write native_handle_t into mefd\n");
close(fd);
return -1;
}
return fd;
}
int hybris_gbm_bo_get_plane_count(struct gbm_bo *bo)
{
//TBD and rename to bo_get_planes
printf("[libgbm-hybris] gbm_bo_get_plane_count called\n");
return 0;
}
int hybris_gbm_bo_get_fd_for_plane(struct gbm_bo *bo, int plane)
{
//TBD and rename to bo_get_plane_fd
printf("[libgbm-hybris] gbm_bo_get_fd_for_plane called\n");
return 0;
}
uint32_t hybris_gbm_bo_get_offset(struct gbm_bo *bo, int plane)
{
//TBD and rename bo_get_offset
printf("[libgbm-hybris] gbm_bo_get_offset called\n");
return 0;
}
struct gbm_surface *hybris_gbm_surface_create_with_modifiers(struct gbm_device *gbm, uint32_t width, uint32_t height, uint32_t format, const uint64_t *modifiers, const unsigned int count){
//TBD: Implement surfaces
printf("[libgbm-hybris] gbm_surface_create_with_modifiers\n");
if ((count && !modifiers) || (modifiers && !count)) {
errno = EINVAL;
return NULL;
}
return NULL;
}
struct gbm_surface *hybris_gbm_surface_create(struct gbm_device *gbm, uint32_t width, uint32_t height, uint32_t format, uint32_t flags) {
//TBD: Implement surfaces
printf("[libgbm-hybris] gbm_surface_create called with width: %u, height: %u, format: %u, flags: %u\n", width, height, format, flags);
return NULL;
}
void hybris_gbm_bo_unmap(struct gbm_bo* bo, void* map_data) {
//TBD: Implement using gralloc unlock
printf("[libgbm-hybris] gbm_bo_unmap called\n");
if (map_data) {
free(map_data);
}
}
char *hybris_gbm_format_get_name(uint32_t gbm_format, struct gbm_format_name_desc *desc)
{
//TBD
//gbm_format = gbm_format_canonicalize(gbm_format);
printf("[libgbm-hybris] gbm_format_get_name called\n");
desc->name[0] = 0;
desc->name[1] = 0;
desc->name[2] = 0;
desc->name[3] = 0;
desc->name[4] = 0;
return desc->name;
}
static struct gbm_device *hybris_device_create(int fd, uint32_t gbm_backend_version){
printf("[libgbm-hybris] hybris_device_create called\n");
struct gbm_device *device;
if (gbm_backend_version != GBM_BACKEND_ABI_VERSION) {
printf("Wrong gbm version, built for: %d current: %d\n", GBM_BACKEND_ABI_VERSION, gbm_backend_version);
return NULL;
}
device = calloc(1, sizeof *device);
if (!device)
return NULL;
device->v0.fd = fd;
device->v0.backend_version = gbm_backend_version;
device->v0.bo_create = hybris_gbm_bo_create;
device->v0.bo_get_fd = hybris_gbm_bo_get_fd;
device->v0.bo_get_stride = hybris_gbm_bo_get_stride;
device->v0.bo_get_modifier = hybris_gbm_bo_get_modifier;
device->v0.bo_get_planes = hybris_gbm_bo_get_plane_count;
return device;
}
struct gbm_backend gbm_hybris_backend = {
.v0.backend_version = GBM_BACKEND_ABI_VERSION,
.v0.backend_name = "hybris",
.v0.create_device = hybris_device_create,
};
struct gbm_backend * gbmint_get_backend(const struct gbm_core *gbm_core);
struct gbm_backend *
gbmint_get_backend(const struct gbm_core *gbm_core) {
core = gbm_core;
return &gbm_hybris_backend;
};