This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
gbm.c
335 lines (262 loc) · 7.27 KB
/
gbm.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
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright 2014 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <assert.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xf86drm.h>
#include "drv.h"
#include "gbm_helpers.h"
#include "gbm_priv.h"
#include "util.h"
PUBLIC int gbm_device_get_fd(struct gbm_device *gbm)
{
return drv_get_fd(gbm->drv);
}
PUBLIC const char *gbm_device_get_backend_name(struct gbm_device *gbm)
{
return drv_get_name(gbm->drv);
}
PUBLIC int gbm_device_is_format_supported(struct gbm_device *gbm, uint32_t format, uint32_t usage)
{
uint64_t use_flags;
if (usage & GBM_BO_USE_CURSOR && usage & GBM_BO_USE_RENDERING)
return 0;
use_flags = gbm_convert_usage(usage);
return (drv_get_combination(gbm->drv, format, use_flags) != NULL);
}
PUBLIC struct gbm_device *gbm_create_device(int fd)
{
struct gbm_device *gbm;
gbm = (struct gbm_device *)malloc(sizeof(*gbm));
if (!gbm)
return NULL;
gbm->drv = drv_create(fd);
if (!gbm->drv) {
free(gbm);
return NULL;
}
return gbm;
}
PUBLIC void gbm_device_destroy(struct gbm_device *gbm)
{
drv_destroy(gbm->drv);
free(gbm);
}
PUBLIC struct gbm_surface *gbm_surface_create(struct gbm_device *gbm, uint32_t width,
uint32_t height, uint32_t format, uint32_t usage)
{
struct gbm_surface *surface = (struct gbm_surface *)malloc(sizeof(*surface));
if (!surface)
return NULL;
return surface;
}
PUBLIC void gbm_surface_destroy(struct gbm_surface *surface)
{
free(surface);
}
PUBLIC struct gbm_bo *gbm_surface_lock_front_buffer(struct gbm_surface *surface)
{
return NULL;
}
PUBLIC void gbm_surface_release_buffer(struct gbm_surface *surface, struct gbm_bo *bo)
{
}
static struct gbm_bo *gbm_bo_new(struct gbm_device *gbm, uint32_t format)
{
struct gbm_bo *bo;
bo = (struct gbm_bo *)calloc(1, sizeof(*bo));
if (!bo)
return NULL;
bo->gbm = gbm;
bo->gbm_format = format;
return bo;
}
PUBLIC struct gbm_bo *gbm_bo_create(struct gbm_device *gbm, uint32_t width, uint32_t height,
uint32_t format, uint32_t usage)
{
struct gbm_bo *bo;
if (!gbm_device_is_format_supported(gbm, format, usage))
return NULL;
bo = gbm_bo_new(gbm, format);
if (!bo)
return NULL;
bo->bo = drv_bo_create(gbm->drv, width, height, format, gbm_convert_usage(usage));
if (!bo->bo) {
free(bo);
return NULL;
}
return bo;
}
PUBLIC struct gbm_bo *gbm_bo_create_with_modifiers(struct gbm_device *gbm, uint32_t width,
uint32_t height, uint32_t format,
const uint64_t *modifiers, uint32_t count)
{
struct gbm_bo *bo;
bo = gbm_bo_new(gbm, format);
if (!bo)
return NULL;
bo->bo = drv_bo_create_with_modifiers(gbm->drv, width, height, format, modifiers, count);
if (!bo->bo) {
free(bo);
return NULL;
}
return bo;
}
PUBLIC void gbm_bo_destroy(struct gbm_bo *bo)
{
if (bo->destroy_user_data) {
bo->destroy_user_data(bo, bo->user_data);
bo->destroy_user_data = NULL;
bo->user_data = NULL;
}
drv_bo_destroy(bo->bo);
free(bo);
}
PUBLIC struct gbm_bo *gbm_bo_import(struct gbm_device *gbm, uint32_t type, void *buffer,
uint32_t usage)
{
struct gbm_bo *bo;
struct drv_import_fd_data drv_data;
struct gbm_import_fd_data *fd_data = buffer;
struct gbm_import_fd_planar_data *fd_planar_data = buffer;
uint32_t gbm_format;
size_t num_planes, i;
memset(&drv_data, 0, sizeof(drv_data));
drv_data.use_flags = gbm_convert_usage(usage);
switch (type) {
case GBM_BO_IMPORT_FD:
gbm_format = fd_data->format;
drv_data.width = fd_data->width;
drv_data.height = fd_data->height;
drv_data.format = fd_data->format;
drv_data.fds[0] = fd_data->fd;
drv_data.strides[0] = fd_data->stride;
break;
case GBM_BO_IMPORT_FD_PLANAR:
gbm_format = fd_planar_data->format;
drv_data.width = fd_planar_data->width;
drv_data.height = fd_planar_data->height;
drv_data.format = fd_planar_data->format;
num_planes = drv_num_planes_from_format(drv_data.format);
assert(num_planes);
for (i = 0; i < num_planes; i++) {
drv_data.fds[i] = fd_planar_data->fds[i];
drv_data.offsets[i] = fd_planar_data->offsets[i];
drv_data.strides[i] = fd_planar_data->strides[i];
drv_data.format_modifiers[i] = fd_planar_data->format_modifiers[i];
}
for (i = num_planes; i < GBM_MAX_PLANES; i++)
drv_data.fds[i] = -1;
break;
default:
return NULL;
}
if (!gbm_device_is_format_supported(gbm, gbm_format, usage))
return NULL;
bo = gbm_bo_new(gbm, gbm_format);
if (!bo)
return NULL;
bo->bo = drv_bo_import(gbm->drv, &drv_data);
if (!bo->bo) {
free(bo);
return NULL;
}
return bo;
}
PUBLIC void *gbm_bo_map(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
uint32_t transfer_flags, uint32_t *stride, void **map_data, size_t plane)
{
uint32_t map_flags;
if (!bo || width == 0 || height == 0 || !stride || !map_data)
return NULL;
*stride = gbm_bo_get_plane_stride(bo, plane);
map_flags = (transfer_flags & GBM_BO_TRANSFER_READ) ? BO_MAP_READ : BO_MAP_NONE;
map_flags |= (transfer_flags & GBM_BO_TRANSFER_WRITE) ? BO_MAP_WRITE : BO_MAP_NONE;
return drv_bo_map(bo->bo, x, y, width, height, map_flags, (struct map_info **)map_data,
plane);
}
PUBLIC void gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
{
assert(bo);
drv_bo_unmap(bo->bo, map_data);
}
PUBLIC uint32_t gbm_bo_get_width(struct gbm_bo *bo)
{
return drv_bo_get_width(bo->bo);
}
PUBLIC uint32_t gbm_bo_get_height(struct gbm_bo *bo)
{
return drv_bo_get_height(bo->bo);
}
PUBLIC uint32_t gbm_bo_get_stride(struct gbm_bo *bo)
{
return gbm_bo_get_plane_stride(bo, 0);
}
PUBLIC uint32_t gbm_bo_get_stride_or_tiling(struct gbm_bo *bo)
{
return drv_bo_get_stride_or_tiling(bo->bo);
}
PUBLIC uint32_t gbm_bo_get_format(struct gbm_bo *bo)
{
return bo->gbm_format;
}
PUBLIC uint64_t gbm_bo_get_format_modifier(struct gbm_bo *bo)
{
return gbm_bo_get_plane_format_modifier(bo, 0);
}
PUBLIC struct gbm_device *gbm_bo_get_device(struct gbm_bo *bo)
{
return bo->gbm;
}
PUBLIC union gbm_bo_handle gbm_bo_get_handle(struct gbm_bo *bo)
{
return gbm_bo_get_plane_handle(bo, 0);
}
PUBLIC int gbm_bo_get_fd(struct gbm_bo *bo)
{
return gbm_bo_get_plane_fd(bo, 0);
}
PUBLIC size_t gbm_bo_get_num_planes(struct gbm_bo *bo)
{
return drv_bo_get_num_planes(bo->bo);
}
PUBLIC union gbm_bo_handle gbm_bo_get_plane_handle(struct gbm_bo *bo, size_t plane)
{
return (union gbm_bo_handle)drv_bo_get_plane_handle(bo->bo, plane).u64;
}
PUBLIC int gbm_bo_get_plane_fd(struct gbm_bo *bo, size_t plane)
{
return drv_bo_get_plane_fd(bo->bo, plane);
}
PUBLIC uint32_t gbm_bo_get_plane_offset(struct gbm_bo *bo, size_t plane)
{
return drv_bo_get_plane_offset(bo->bo, plane);
}
PUBLIC uint32_t gbm_bo_get_plane_size(struct gbm_bo *bo, size_t plane)
{
return drv_bo_get_plane_size(bo->bo, plane);
}
PUBLIC uint32_t gbm_bo_get_plane_stride(struct gbm_bo *bo, size_t plane)
{
return drv_bo_get_plane_stride(bo->bo, plane);
}
PUBLIC uint64_t gbm_bo_get_plane_format_modifier(struct gbm_bo *bo, size_t plane)
{
return drv_bo_get_plane_format_modifier(bo->bo, plane);
}
PUBLIC void gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
void (*destroy_user_data)(struct gbm_bo *, void *))
{
bo->user_data = data;
bo->destroy_user_data = destroy_user_data;
}
PUBLIC void *gbm_bo_get_user_data(struct gbm_bo *bo)
{
return bo->user_data;
}