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.
17 #include "gbm_helpers.h"
21 PUBLIC int gbm_device_get_fd(struct gbm_device *gbm)
24 return drv_get_fd(gbm->drv);
27 PUBLIC const char *gbm_device_get_backend_name(struct gbm_device *gbm)
29 return drv_get_name(gbm->drv);
32 PUBLIC int gbm_device_is_format_supported(struct gbm_device *gbm, uint32_t format, uint32_t usage)
36 if (usage & GBM_BO_USE_CURSOR && usage & GBM_BO_USE_RENDERING)
39 use_flags = gbm_convert_usage(usage);
41 return (drv_get_combination(gbm->drv, format, use_flags) != NULL);
44 PUBLIC struct gbm_device *gbm_create_device(int fd)
46 struct gbm_device *gbm;
48 gbm = (struct gbm_device *)malloc(sizeof(*gbm));
53 gbm->drv = drv_create(fd);
62 PUBLIC void gbm_device_destroy(struct gbm_device *gbm)
64 drv_destroy(gbm->drv);
68 PUBLIC struct gbm_surface *gbm_surface_create(struct gbm_device *gbm, uint32_t width,
69 uint32_t height, uint32_t format, uint32_t usage)
71 struct gbm_surface *surface = (struct gbm_surface *)malloc(sizeof(*surface));
79 PUBLIC void gbm_surface_destroy(struct gbm_surface *surface)
84 PUBLIC struct gbm_bo *gbm_surface_lock_front_buffer(struct gbm_surface *surface)
89 PUBLIC void gbm_surface_release_buffer(struct gbm_surface *surface, struct gbm_bo *bo)
93 static struct gbm_bo *gbm_bo_new(struct gbm_device *gbm, uint32_t format)
97 bo = (struct gbm_bo *)calloc(1, sizeof(*bo));
102 bo->gbm_format = format;
107 PUBLIC struct gbm_bo *gbm_bo_create(struct gbm_device *gbm, uint32_t width, uint32_t height,
108 uint32_t format, uint32_t usage)
112 if (!gbm_device_is_format_supported(gbm, format, usage))
115 bo = gbm_bo_new(gbm, format);
120 bo->bo = drv_bo_create(gbm->drv, width, height, format, gbm_convert_usage(usage));
130 PUBLIC struct gbm_bo *gbm_bo_create_with_modifiers(struct gbm_device *gbm, uint32_t width,
131 uint32_t height, uint32_t format,
132 const uint64_t *modifiers, uint32_t count)
136 bo = gbm_bo_new(gbm, format);
141 bo->bo = drv_bo_create_with_modifiers(gbm->drv, width, height, format, modifiers, count);
151 PUBLIC void gbm_bo_destroy(struct gbm_bo *bo)
153 if (bo->destroy_user_data) {
154 bo->destroy_user_data(bo, bo->user_data);
155 bo->destroy_user_data = NULL;
156 bo->user_data = NULL;
159 drv_bo_destroy(bo->bo);
163 PUBLIC struct gbm_bo *gbm_bo_import(struct gbm_device *gbm, uint32_t type, void *buffer,
167 struct drv_import_fd_data drv_data;
168 struct gbm_import_fd_data *fd_data = buffer;
169 struct gbm_import_fd_planar_data *fd_planar_data = buffer;
170 struct gbm_import_fd_modifier_data *fd_modifier_data = buffer;
172 size_t num_planes, i, num_fds;
174 memset(&drv_data, 0, sizeof(drv_data));
175 drv_data.use_flags = gbm_convert_usage(usage);
177 case GBM_BO_IMPORT_FD:
178 gbm_format = fd_data->format;
179 drv_data.width = fd_data->width;
180 drv_data.height = fd_data->height;
181 drv_data.format = fd_data->format;
182 drv_data.fds[0] = fd_data->fd;
183 drv_data.strides[0] = fd_data->stride;
185 case GBM_BO_IMPORT_FD_MODIFIER:
186 gbm_format = fd_modifier_data->format;
187 drv_data.width = fd_modifier_data->width;
188 drv_data.height = fd_modifier_data->height;
189 drv_data.format = fd_modifier_data->format;
190 num_planes = drv_num_planes_from_format(drv_data.format);
191 num_fds = fd_modifier_data->num_fds;
193 assert(num_fds > 0 && num_fds <= num_planes);
194 for (i = 0; i < num_planes; i++) {
195 if (num_fds != num_planes)
196 drv_data.fds[i] = fd_modifier_data->fds[0];
198 drv_data.fds[i] = fd_modifier_data->fds[i];
199 drv_data.offsets[i] = fd_modifier_data->offsets[i];
200 drv_data.strides[i] = fd_modifier_data->strides[i];
201 drv_data.format_modifiers[i] = fd_modifier_data->modifier;
204 for (i = num_planes; i < GBM_MAX_PLANES; i++)
205 drv_data.fds[i] = -1;
208 case GBM_BO_IMPORT_FD_PLANAR:
209 gbm_format = fd_planar_data->format;
210 drv_data.width = fd_planar_data->width;
211 drv_data.height = fd_planar_data->height;
212 drv_data.format = fd_planar_data->format;
213 num_planes = drv_num_planes_from_format(drv_data.format);
217 for (i = 0; i < num_planes; i++) {
218 drv_data.fds[i] = fd_planar_data->fds[i];
219 drv_data.offsets[i] = fd_planar_data->offsets[i];
220 drv_data.strides[i] = fd_planar_data->strides[i];
221 drv_data.format_modifiers[i] = fd_planar_data->format_modifiers[i];
224 for (i = num_planes; i < GBM_MAX_PLANES; i++)
225 drv_data.fds[i] = -1;
232 if (!gbm_device_is_format_supported(gbm, gbm_format, usage))
235 bo = gbm_bo_new(gbm, gbm_format);
240 bo->bo = drv_bo_import(gbm->drv, &drv_data);
250 PUBLIC void *gbm_bo_map(struct gbm_bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
251 uint32_t transfer_flags, uint32_t *stride, void **map_data, size_t plane)
256 struct rectangle rect = { .x = x, .y = y, .width = width, .height = height };
257 if (!bo || width == 0 || height == 0 || !stride || !map_data)
260 map_flags = (transfer_flags & GBM_BO_TRANSFER_READ) ? BO_MAP_READ : BO_MAP_NONE;
261 map_flags |= (transfer_flags & GBM_BO_TRANSFER_WRITE) ? BO_MAP_WRITE : BO_MAP_NONE;
263 addr = drv_bo_map(bo->bo, &rect, map_flags, (struct mapping **)map_data, plane);
264 if (addr == MAP_FAILED)
267 *stride = ((struct mapping *)*map_data)->vma->map_strides[plane];
269 offset = *stride * rect.y;
270 offset += drv_stride_from_format(bo->gbm_format, rect.x, plane);
271 return (void *)((uint8_t *)addr + offset);
274 PUBLIC void gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
277 drv_bo_flush_or_unmap(bo->bo, map_data);
280 PUBLIC uint32_t gbm_bo_get_width(struct gbm_bo *bo)
282 return drv_bo_get_width(bo->bo);
285 PUBLIC uint32_t gbm_bo_get_height(struct gbm_bo *bo)
287 return drv_bo_get_height(bo->bo);
290 PUBLIC uint32_t gbm_bo_get_stride(struct gbm_bo *bo)
292 return gbm_bo_get_stride_for_plane(bo, 0);
295 PUBLIC uint32_t gbm_bo_get_stride_or_tiling(struct gbm_bo *bo)
297 return drv_bo_get_stride_or_tiling(bo->bo);
300 PUBLIC uint32_t gbm_bo_get_format(struct gbm_bo *bo)
302 return bo->gbm_format;
305 PUBLIC uint64_t gbm_bo_get_format_modifier(struct gbm_bo *bo)
307 return gbm_bo_get_modifier(bo);
310 PUBLIC uint64_t gbm_bo_get_modifier(struct gbm_bo *bo)
312 return gbm_bo_get_plane_format_modifier(bo, 0);
315 PUBLIC struct gbm_device *gbm_bo_get_device(struct gbm_bo *bo)
320 PUBLIC union gbm_bo_handle gbm_bo_get_handle(struct gbm_bo *bo)
322 return gbm_bo_get_handle_for_plane(bo, 0);
325 PUBLIC int gbm_bo_get_fd(struct gbm_bo *bo)
327 return gbm_bo_get_plane_fd(bo, 0);
330 PUBLIC size_t gbm_bo_get_num_planes(struct gbm_bo *bo)
332 return gbm_bo_get_plane_count(bo);
335 PUBLIC size_t gbm_bo_get_plane_count(struct gbm_bo *bo)
337 return drv_bo_get_num_planes(bo->bo);
340 PUBLIC union gbm_bo_handle gbm_bo_get_plane_handle(struct gbm_bo *bo, size_t plane)
342 return gbm_bo_get_handle_for_plane(bo, plane);
345 PUBLIC union gbm_bo_handle gbm_bo_get_handle_for_plane(struct gbm_bo *bo, size_t plane)
347 return (union gbm_bo_handle)drv_bo_get_plane_handle(bo->bo, plane).u64;
350 PUBLIC int gbm_bo_get_plane_fd(struct gbm_bo *bo, size_t plane)
352 return drv_bo_get_plane_fd(bo->bo, plane);
355 PUBLIC uint32_t gbm_bo_get_plane_offset(struct gbm_bo *bo, size_t plane)
357 return gbm_bo_get_offset(bo, plane);
360 PUBLIC uint32_t gbm_bo_get_offset(struct gbm_bo *bo, size_t plane)
362 return drv_bo_get_plane_offset(bo->bo, plane);
365 PUBLIC uint32_t gbm_bo_get_plane_size(struct gbm_bo *bo, size_t plane)
367 return drv_bo_get_plane_size(bo->bo, plane);
370 PUBLIC uint32_t gbm_bo_get_plane_stride(struct gbm_bo *bo, size_t plane)
372 return gbm_bo_get_stride_for_plane(bo, plane);
375 PUBLIC uint32_t gbm_bo_get_stride_for_plane(struct gbm_bo *bo, size_t plane)
377 return drv_bo_get_plane_stride(bo->bo, plane);
380 PUBLIC uint64_t gbm_bo_get_plane_format_modifier(struct gbm_bo *bo, size_t plane)
382 return drv_bo_get_plane_format_modifier(bo->bo, plane);
385 PUBLIC void gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
386 void (*destroy_user_data)(struct gbm_bo *, void *))
388 bo->user_data = data;
389 bo->destroy_user_data = destroy_user_data;
392 PUBLIC void *gbm_bo_get_user_data(struct gbm_bo *bo)
394 return bo->user_data;