1 /*
2 * Copyright 2014 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/mman.h>
14 #include <xf86drm.h>
15
16 #include "drv.h"
17 #include "gbm_helpers.h"
18 #include "gbm_priv.h"
19 #include "util.h"
20
gbm_device_get_fd(struct gbm_device * gbm)21 PUBLIC int gbm_device_get_fd(struct gbm_device *gbm)
22 {
23
24 return drv_get_fd(gbm->drv);
25 }
26
gbm_device_get_backend_name(struct gbm_device * gbm)27 PUBLIC const char *gbm_device_get_backend_name(struct gbm_device *gbm)
28 {
29 return drv_get_name(gbm->drv);
30 }
31
gbm_device_is_format_supported(struct gbm_device * gbm,uint32_t format,uint32_t usage)32 PUBLIC int gbm_device_is_format_supported(struct gbm_device *gbm, uint32_t format, uint32_t usage)
33 {
34 uint64_t use_flags;
35
36 if (usage & GBM_BO_USE_CURSOR && usage & GBM_BO_USE_RENDERING)
37 return 0;
38
39 use_flags = gbm_convert_usage(usage);
40
41 return (drv_get_combination(gbm->drv, format, use_flags) != NULL);
42 }
43
gbm_device_get_format_modifier_plane_count(struct gbm_device * gbm,uint32_t format,uint64_t modifier)44 PUBLIC int gbm_device_get_format_modifier_plane_count(struct gbm_device *gbm, uint32_t format,
45 uint64_t modifier)
46 {
47 return 0;
48 }
49
gbm_create_device(int fd)50 PUBLIC struct gbm_device *gbm_create_device(int fd)
51 {
52 struct gbm_device *gbm;
53
54 gbm = (struct gbm_device *)malloc(sizeof(*gbm));
55
56 if (!gbm)
57 return NULL;
58
59 gbm->drv = drv_create(fd);
60 if (!gbm->drv) {
61 free(gbm);
62 return NULL;
63 }
64
65 return gbm;
66 }
67
gbm_device_destroy(struct gbm_device * gbm)68 PUBLIC void gbm_device_destroy(struct gbm_device *gbm)
69 {
70 drv_destroy(gbm->drv);
71 free(gbm);
72 }
73
gbm_surface_create(struct gbm_device * gbm,uint32_t width,uint32_t height,uint32_t format,uint32_t usage)74 PUBLIC struct gbm_surface *gbm_surface_create(struct gbm_device *gbm, uint32_t width,
75 uint32_t height, uint32_t format, uint32_t usage)
76 {
77 struct gbm_surface *surface = (struct gbm_surface *)malloc(sizeof(*surface));
78
79 if (!surface)
80 return NULL;
81
82 return surface;
83 }
84
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)85 PUBLIC struct gbm_surface *gbm_surface_create_with_modifiers(struct gbm_device *gbm, uint32_t width,
86 uint32_t height, uint32_t format,
87 const uint64_t *modifiers,
88 const unsigned int count)
89 {
90 if (count != 0 || modifiers != NULL)
91 return NULL;
92
93 return gbm_surface_create(gbm, width, height, format, 0);
94 }
95
gbm_surface_lock_front_buffer(struct gbm_surface * surface)96 PUBLIC struct gbm_bo *gbm_surface_lock_front_buffer(struct gbm_surface *surface)
97 {
98 return NULL;
99 }
100
gbm_surface_release_buffer(struct gbm_surface * surface,struct gbm_bo * bo)101 PUBLIC void gbm_surface_release_buffer(struct gbm_surface *surface, struct gbm_bo *bo)
102 {
103 }
104
gbm_surface_has_free_buffers(struct gbm_surface * surface)105 PUBLIC int gbm_surface_has_free_buffers(struct gbm_surface *surface)
106 {
107 return 0;
108 }
109
gbm_surface_destroy(struct gbm_surface * surface)110 PUBLIC void gbm_surface_destroy(struct gbm_surface *surface)
111 {
112 free(surface);
113 }
114
gbm_bo_new(struct gbm_device * gbm,uint32_t format)115 static struct gbm_bo *gbm_bo_new(struct gbm_device *gbm, uint32_t format)
116 {
117 struct gbm_bo *bo;
118
119 bo = (struct gbm_bo *)calloc(1, sizeof(*bo));
120 if (!bo)
121 return NULL;
122
123 bo->gbm = gbm;
124 bo->gbm_format = format;
125
126 return bo;
127 }
128
gbm_bo_create(struct gbm_device * gbm,uint32_t width,uint32_t height,uint32_t format,uint32_t usage)129 PUBLIC struct gbm_bo *gbm_bo_create(struct gbm_device *gbm, uint32_t width, uint32_t height,
130 uint32_t format, uint32_t usage)
131 {
132 struct gbm_bo *bo;
133
134 if (!gbm_device_is_format_supported(gbm, format, usage))
135 return NULL;
136
137 bo = gbm_bo_new(gbm, format);
138
139 if (!bo)
140 return NULL;
141
142 /*
143 * HACK: This is for HAL_PIXEL_FORMAT_YV12 buffers allocated by arcvm. None of
144 * our platforms can display YV12, so we can treat as a SW buffer. Remove once
145 * this can be intelligently resolved in the guest. Also see compute_virgl_bind_flags.
146 */
147 if (format == GBM_FORMAT_YVU420 && (usage & GBM_BO_USE_LINEAR))
148 format = DRM_FORMAT_YVU420_ANDROID;
149
150 bo->bo = drv_bo_create(gbm->drv, width, height, format, gbm_convert_usage(usage));
151
152 if (!bo->bo) {
153 free(bo);
154 return NULL;
155 }
156
157 return bo;
158 }
159
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)160 PUBLIC struct gbm_bo *gbm_bo_create_with_modifiers(struct gbm_device *gbm, uint32_t width,
161 uint32_t height, uint32_t format,
162 const uint64_t *modifiers, uint32_t count)
163 {
164 struct gbm_bo *bo;
165
166 bo = gbm_bo_new(gbm, format);
167
168 if (!bo)
169 return NULL;
170
171 bo->bo = drv_bo_create_with_modifiers(gbm->drv, width, height, format, modifiers, count);
172
173 if (!bo->bo) {
174 free(bo);
175 return NULL;
176 }
177
178 return bo;
179 }
180
gbm_bo_destroy(struct gbm_bo * bo)181 PUBLIC void gbm_bo_destroy(struct gbm_bo *bo)
182 {
183 if (bo->destroy_user_data) {
184 bo->destroy_user_data(bo, bo->user_data);
185 bo->destroy_user_data = NULL;
186 bo->user_data = NULL;
187 }
188
189 drv_bo_destroy(bo->bo);
190 free(bo);
191 }
192
gbm_bo_import(struct gbm_device * gbm,uint32_t type,void * buffer,uint32_t usage)193 PUBLIC struct gbm_bo *gbm_bo_import(struct gbm_device *gbm, uint32_t type, void *buffer,
194 uint32_t usage)
195 {
196 struct gbm_bo *bo;
197 struct drv_import_fd_data drv_data = { 0 };
198 struct gbm_import_fd_data *fd_data = buffer;
199 struct gbm_import_fd_modifier_data *fd_modifier_data = buffer;
200 uint32_t gbm_format;
201 size_t num_planes, i, num_fds;
202
203 drv_data.use_flags = gbm_convert_usage(usage);
204 switch (type) {
205 case GBM_BO_IMPORT_FD:
206 gbm_format = fd_data->format;
207 drv_data.width = fd_data->width;
208 drv_data.height = fd_data->height;
209 drv_data.format = fd_data->format;
210 drv_data.fds[0] = fd_data->fd;
211 drv_data.strides[0] = fd_data->stride;
212 drv_data.format_modifier = DRM_FORMAT_MOD_INVALID;
213
214 break;
215 case GBM_BO_IMPORT_FD_MODIFIER:
216 gbm_format = fd_modifier_data->format;
217 drv_data.width = fd_modifier_data->width;
218 drv_data.height = fd_modifier_data->height;
219 drv_data.format = fd_modifier_data->format;
220 num_planes = drv_num_planes_from_modifier(gbm->drv, drv_data.format,
221 fd_modifier_data->modifier);
222 assert(num_planes);
223
224 num_fds = fd_modifier_data->num_fds;
225 if (!num_fds || num_fds > num_planes)
226 return NULL;
227
228 drv_data.format_modifier = fd_modifier_data->modifier;
229 for (i = 0; i < num_planes; i++) {
230 if (num_fds != num_planes)
231 drv_data.fds[i] = fd_modifier_data->fds[0];
232 else
233 drv_data.fds[i] = fd_modifier_data->fds[i];
234 drv_data.offsets[i] = fd_modifier_data->offsets[i];
235 drv_data.strides[i] = fd_modifier_data->strides[i];
236 }
237
238 for (i = num_planes; i < GBM_MAX_PLANES; i++)
239 drv_data.fds[i] = -1;
240
241 break;
242 default:
243 return NULL;
244 }
245
246 if (!gbm_device_is_format_supported(gbm, gbm_format, usage))
247 return NULL;
248
249 bo = gbm_bo_new(gbm, gbm_format);
250
251 if (!bo)
252 return NULL;
253
254 bo->bo = drv_bo_import(gbm->drv, &drv_data);
255
256 if (!bo->bo) {
257 free(bo);
258 return NULL;
259 }
260
261 return bo;
262 }
263
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)264 PUBLIC void *gbm_bo_map(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
265 uint32_t transfer_flags, uint32_t *stride, void **map_data)
266 {
267 return gbm_bo_map2(bo, x, y, width, height, transfer_flags, stride, map_data, 0);
268 }
269
gbm_bo_unmap(struct gbm_bo * bo,void * map_data)270 PUBLIC void gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
271 {
272 assert(bo);
273 drv_bo_flush_or_unmap(bo->bo, map_data);
274 }
275
gbm_bo_get_width(struct gbm_bo * bo)276 PUBLIC uint32_t gbm_bo_get_width(struct gbm_bo *bo)
277 {
278 return drv_bo_get_width(bo->bo);
279 }
280
gbm_bo_get_height(struct gbm_bo * bo)281 PUBLIC uint32_t gbm_bo_get_height(struct gbm_bo *bo)
282 {
283 return drv_bo_get_height(bo->bo);
284 }
285
gbm_bo_get_stride(struct gbm_bo * bo)286 PUBLIC uint32_t gbm_bo_get_stride(struct gbm_bo *bo)
287 {
288 return gbm_bo_get_stride_for_plane(bo, 0);
289 }
290
gbm_bo_get_format(struct gbm_bo * bo)291 PUBLIC uint32_t gbm_bo_get_format(struct gbm_bo *bo)
292 {
293 return bo->gbm_format;
294 }
295
gbm_bo_get_bpp(struct gbm_bo * bo)296 PUBLIC uint32_t gbm_bo_get_bpp(struct gbm_bo *bo)
297 {
298 return drv_bytes_per_pixel_from_format(drv_bo_get_format(bo->bo), 0);
299 }
300
gbm_bo_get_modifier(struct gbm_bo * bo)301 PUBLIC uint64_t gbm_bo_get_modifier(struct gbm_bo *bo)
302 {
303 return drv_bo_get_format_modifier(bo->bo);
304 }
305
gbm_bo_get_device(struct gbm_bo * bo)306 PUBLIC struct gbm_device *gbm_bo_get_device(struct gbm_bo *bo)
307 {
308 return bo->gbm;
309 }
310
gbm_bo_get_handle(struct gbm_bo * bo)311 PUBLIC union gbm_bo_handle gbm_bo_get_handle(struct gbm_bo *bo)
312 {
313 return gbm_bo_get_handle_for_plane(bo, 0);
314 }
315
gbm_bo_get_fd(struct gbm_bo * bo)316 PUBLIC int gbm_bo_get_fd(struct gbm_bo *bo)
317 {
318 return gbm_bo_get_plane_fd(bo, 0);
319 }
320
gbm_bo_get_plane_count(struct gbm_bo * bo)321 PUBLIC int gbm_bo_get_plane_count(struct gbm_bo *bo)
322 {
323 return drv_bo_get_num_planes(bo->bo);
324 }
325
gbm_bo_get_handle_for_plane(struct gbm_bo * bo,size_t plane)326 PUBLIC union gbm_bo_handle gbm_bo_get_handle_for_plane(struct gbm_bo *bo, size_t plane)
327 {
328 return (union gbm_bo_handle)drv_bo_get_plane_handle(bo->bo, (size_t)plane).u64;
329 }
330
gbm_bo_get_fd_for_plane(struct gbm_bo * bo,int plane)331 PUBLIC int gbm_bo_get_fd_for_plane(struct gbm_bo *bo, int plane)
332 {
333 return drv_bo_get_plane_fd(bo->bo, plane);
334 }
335
gbm_bo_get_offset(struct gbm_bo * bo,size_t plane)336 PUBLIC uint32_t gbm_bo_get_offset(struct gbm_bo *bo, size_t plane)
337 {
338 return drv_bo_get_plane_offset(bo->bo, (size_t)plane);
339 }
340
gbm_bo_get_stride_for_plane(struct gbm_bo * bo,size_t plane)341 PUBLIC uint32_t gbm_bo_get_stride_for_plane(struct gbm_bo *bo, size_t plane)
342 {
343 return drv_bo_get_plane_stride(bo->bo, (size_t)plane);
344 }
345
gbm_bo_set_user_data(struct gbm_bo * bo,void * data,void (* destroy_user_data)(struct gbm_bo *,void *))346 PUBLIC void gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
347 void (*destroy_user_data)(struct gbm_bo *, void *))
348 {
349 bo->user_data = data;
350 bo->destroy_user_data = destroy_user_data;
351 }
352
gbm_bo_get_user_data(struct gbm_bo * bo)353 PUBLIC void *gbm_bo_get_user_data(struct gbm_bo *bo)
354 {
355 return bo->user_data;
356 }
357
358 /* The two GBM_BO_FORMAT_[XA]RGB8888 formats alias the GBM_FORMAT_*
359 * formats of the same name. We want to accept them whenever someone
360 * has a GBM format, but never return them to the user.
361 */
gbm_format_canonicalize(uint32_t gbm_format)362 static uint32_t gbm_format_canonicalize(uint32_t gbm_format)
363 {
364 switch (gbm_format) {
365 case GBM_BO_FORMAT_XRGB8888:
366 return GBM_FORMAT_XRGB8888;
367 case GBM_BO_FORMAT_ARGB8888:
368 return GBM_FORMAT_ARGB8888;
369 default:
370 return gbm_format;
371 }
372 }
373
374 /**
375 * Returns a string representing the fourcc format name.
376 */
gbm_format_get_name(uint32_t gbm_format,struct gbm_format_name_desc * desc)377 PUBLIC char *gbm_format_get_name(uint32_t gbm_format, struct gbm_format_name_desc *desc)
378 {
379 gbm_format = gbm_format_canonicalize(gbm_format);
380
381 desc->name[0] = gbm_format;
382 desc->name[1] = gbm_format >> 8;
383 desc->name[2] = gbm_format >> 16;
384 desc->name[3] = gbm_format >> 24;
385 desc->name[4] = 0;
386
387 return desc->name;
388 }
389
390 /*
391 * The following functions are not deprecated, but not in the Mesa the gbm
392 * header. The main difference is minigbm allows for the possibility of
393 * disjoint YUV images, while Mesa GBM does not.
394 */
gbm_bo_get_plane_size(struct gbm_bo * bo,size_t plane)395 PUBLIC uint32_t gbm_bo_get_plane_size(struct gbm_bo *bo, size_t plane)
396 {
397 return drv_bo_get_plane_size(bo->bo, plane);
398 }
399
gbm_bo_get_plane_fd(struct gbm_bo * bo,size_t plane)400 PUBLIC int gbm_bo_get_plane_fd(struct gbm_bo *bo, size_t plane)
401 {
402 return drv_bo_get_plane_fd(bo->bo, plane);
403 }
404
gbm_bo_map2(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,int plane)405 PUBLIC void *gbm_bo_map2(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
406 uint32_t transfer_flags, uint32_t *stride, void **map_data, int plane)
407 {
408 void *addr;
409 off_t offset;
410 uint32_t map_flags;
411 plane = (size_t)plane;
412 struct rectangle rect = { .x = x, .y = y, .width = width, .height = height };
413 if (!bo || width == 0 || height == 0 || !stride || !map_data)
414 return NULL;
415
416 map_flags = (transfer_flags & GBM_BO_TRANSFER_READ) ? BO_MAP_READ : BO_MAP_NONE;
417 map_flags |= (transfer_flags & GBM_BO_TRANSFER_WRITE) ? BO_MAP_WRITE : BO_MAP_NONE;
418
419 addr = drv_bo_map(bo->bo, &rect, map_flags, (struct mapping **)map_data, plane);
420 if (addr == MAP_FAILED)
421 return MAP_FAILED;
422
423 *stride = ((struct mapping *)*map_data)->vma->map_strides[plane];
424
425 offset = *stride * rect.y;
426 offset += rect.x * drv_bytes_per_pixel_from_format(bo->gbm_format, plane);
427 return (void *)((uint8_t *)addr + offset);
428 }
429