gbm.c revision 01e04c3f
13464ebd5Sriastradh/*
23464ebd5Sriastradh * Copyright © 2011 Intel Corporation
33464ebd5Sriastradh *
43464ebd5Sriastradh * Permission is hereby granted, free of charge, to any person obtaining a
53464ebd5Sriastradh * copy of this software and associated documentation files (the "Software"),
63464ebd5Sriastradh * to deal in the Software without restriction, including without limitation
73464ebd5Sriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83464ebd5Sriastradh * and/or sell copies of the Software, and to permit persons to whom the
93464ebd5Sriastradh * Software is furnished to do so, subject to the following conditions:
103464ebd5Sriastradh *
113464ebd5Sriastradh * The above copyright notice and this permission notice (including the next
123464ebd5Sriastradh * paragraph) shall be included in all copies or substantial portions of the
133464ebd5Sriastradh * Software.
143464ebd5Sriastradh *
153464ebd5Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
163464ebd5Sriastradh * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
173464ebd5Sriastradh * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
183464ebd5Sriastradh * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
193464ebd5Sriastradh * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
203464ebd5Sriastradh * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
213464ebd5Sriastradh * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
223464ebd5Sriastradh * DEALINGS IN THE SOFTWARE.
233464ebd5Sriastradh *
243464ebd5Sriastradh * Authors:
253464ebd5Sriastradh *    Benjamin Franzke <benjaminfranzke@googlemail.com>
263464ebd5Sriastradh */
273464ebd5Sriastradh
283464ebd5Sriastradh#include <stddef.h>
293464ebd5Sriastradh#include <stdio.h>
303464ebd5Sriastradh#include <stdlib.h>
313464ebd5Sriastradh#include <string.h>
323464ebd5Sriastradh#include <stdint.h>
333464ebd5Sriastradh
3401e04c3fSmrg#ifdef MAJOR_IN_MKDEV
3501e04c3fSmrg#include <sys/mkdev.h>
3601e04c3fSmrg#endif
3701e04c3fSmrg#ifdef MAJOR_IN_SYSMACROS
3801e04c3fSmrg#include <sys/sysmacros.h>
3901e04c3fSmrg#endif
403464ebd5Sriastradh#include <sys/stat.h>
413464ebd5Sriastradh#include <unistd.h>
42af69d88dSmrg#include <errno.h>
433464ebd5Sriastradh
443464ebd5Sriastradh#include "gbm.h"
453464ebd5Sriastradh#include "gbmint.h"
463464ebd5Sriastradh#include "backend.h"
473464ebd5Sriastradh
48af69d88dSmrg/** Returns the file description for the gbm device
49af69d88dSmrg *
50af69d88dSmrg * \return The fd that the struct gbm_device was created with
51af69d88dSmrg */
523464ebd5SriastradhGBM_EXPORT int
533464ebd5Sriastradhgbm_device_get_fd(struct gbm_device *gbm)
543464ebd5Sriastradh{
553464ebd5Sriastradh   return gbm->fd;
563464ebd5Sriastradh}
573464ebd5Sriastradh
58af69d88dSmrg/** Get the backend name for the given gbm device
59af69d88dSmrg *
60af69d88dSmrg * \return The backend name string - this belongs to the device and must not
61af69d88dSmrg * be freed
62af69d88dSmrg */
633464ebd5SriastradhGBM_EXPORT const char *
643464ebd5Sriastradhgbm_device_get_backend_name(struct gbm_device *gbm)
653464ebd5Sriastradh{
663464ebd5Sriastradh   return gbm->name;
673464ebd5Sriastradh}
683464ebd5Sriastradh
69af69d88dSmrg/** Test if a format is supported for a given set of usage flags.
70af69d88dSmrg *
71af69d88dSmrg * \param gbm The created buffer manager
72af69d88dSmrg * \param format The format to test
73af69d88dSmrg * \param usage A bitmask of the usages to test the format against
74af69d88dSmrg * \return 1 if the format is supported otherwise 0
75af69d88dSmrg *
76af69d88dSmrg * \sa enum gbm_bo_flags for the list of flags that the format can be
77af69d88dSmrg * tested against
78af69d88dSmrg *
79af69d88dSmrg * \sa enum gbm_bo_format for the list of formats
80af69d88dSmrg */
81af69d88dSmrgGBM_EXPORT int
823464ebd5Sriastradhgbm_device_is_format_supported(struct gbm_device *gbm,
83af69d88dSmrg                               uint32_t format, uint32_t usage)
843464ebd5Sriastradh{
853464ebd5Sriastradh   return gbm->is_format_supported(gbm, format, usage);
863464ebd5Sriastradh}
873464ebd5Sriastradh
8801e04c3fSmrg/** Get the number of planes that are required for a given format+modifier
8901e04c3fSmrg *
9001e04c3fSmrg * \param gbm The gbm device returned from gbm_create_device()
9101e04c3fSmrg * \param format The format to query
9201e04c3fSmrg * \param modifier The modifier to query
9301e04c3fSmrg */
9401e04c3fSmrgGBM_EXPORT int
9501e04c3fSmrggbm_device_get_format_modifier_plane_count(struct gbm_device *gbm,
9601e04c3fSmrg                                           uint32_t format,
9701e04c3fSmrg                                           uint64_t modifier)
9801e04c3fSmrg{
9901e04c3fSmrg   return gbm->get_format_modifier_plane_count(gbm, format, modifier);
10001e04c3fSmrg}
10101e04c3fSmrg
102af69d88dSmrg/** Destroy the gbm device and free all resources associated with it.
103af69d88dSmrg *
104af69d88dSmrg * \param gbm The device created using gbm_create_device()
105af69d88dSmrg */
1063464ebd5SriastradhGBM_EXPORT void
1073464ebd5Sriastradhgbm_device_destroy(struct gbm_device *gbm)
1083464ebd5Sriastradh{
1093464ebd5Sriastradh   gbm->refcount--;
1103464ebd5Sriastradh   if (gbm->refcount == 0)
1113464ebd5Sriastradh      gbm->destroy(gbm);
1123464ebd5Sriastradh}
1133464ebd5Sriastradh
114af69d88dSmrg/** Create a gbm device for allocating buffers
115af69d88dSmrg *
116af69d88dSmrg * The file descriptor passed in is used by the backend to communicate with
117af69d88dSmrg * platform for allocating the memory. For allocations using DRI this would be
118af69d88dSmrg * the file descriptor returned when opening a device such as \c
119af69d88dSmrg * /dev/dri/card0
120af69d88dSmrg *
12101e04c3fSmrg * \param fd The file descriptor for a backend specific device
122af69d88dSmrg * \return The newly created struct gbm_device. The resources associated with
123af69d88dSmrg * the device should be freed with gbm_device_destroy() when it is no longer
124af69d88dSmrg * needed. If the creation of the device failed NULL will be returned.
125af69d88dSmrg */
1263464ebd5SriastradhGBM_EXPORT struct gbm_device *
1273464ebd5Sriastradhgbm_create_device(int fd)
1283464ebd5Sriastradh{
1293464ebd5Sriastradh   struct gbm_device *gbm = NULL;
1303464ebd5Sriastradh   struct stat buf;
1313464ebd5Sriastradh
1323464ebd5Sriastradh   if (fd < 0 || fstat(fd, &buf) < 0 || !S_ISCHR(buf.st_mode)) {
133af69d88dSmrg      errno = EINVAL;
1343464ebd5Sriastradh      return NULL;
1353464ebd5Sriastradh   }
1363464ebd5Sriastradh
1373464ebd5Sriastradh   gbm = _gbm_create_device(fd);
1383464ebd5Sriastradh   if (gbm == NULL)
1393464ebd5Sriastradh      return NULL;
1403464ebd5Sriastradh
1413464ebd5Sriastradh   gbm->dummy = gbm_create_device;
1423464ebd5Sriastradh   gbm->stat = buf;
1433464ebd5Sriastradh   gbm->refcount = 1;
1443464ebd5Sriastradh
1453464ebd5Sriastradh   return gbm;
1463464ebd5Sriastradh}
1473464ebd5Sriastradh
148af69d88dSmrg/** Get the width of the buffer object
149af69d88dSmrg *
150af69d88dSmrg * \param bo The buffer object
151af69d88dSmrg * \return The width of the allocated buffer object
152af69d88dSmrg *
153af69d88dSmrg */
15401e04c3fSmrgGBM_EXPORT uint32_t
1553464ebd5Sriastradhgbm_bo_get_width(struct gbm_bo *bo)
1563464ebd5Sriastradh{
1573464ebd5Sriastradh   return bo->width;
1583464ebd5Sriastradh}
1593464ebd5Sriastradh
160af69d88dSmrg/** Get the height of the buffer object
161af69d88dSmrg *
162af69d88dSmrg * \param bo The buffer object
163af69d88dSmrg * \return The height of the allocated buffer object
164af69d88dSmrg */
16501e04c3fSmrgGBM_EXPORT uint32_t
1663464ebd5Sriastradhgbm_bo_get_height(struct gbm_bo *bo)
1673464ebd5Sriastradh{
1683464ebd5Sriastradh   return bo->height;
1693464ebd5Sriastradh}
1703464ebd5Sriastradh
171af69d88dSmrg/** Get the stride of the buffer object
172af69d88dSmrg *
173af69d88dSmrg * This is calculated by the backend when it does the allocation in
174af69d88dSmrg * gbm_bo_create()
175af69d88dSmrg *
176af69d88dSmrg * \param bo The buffer object
177af69d88dSmrg * \return The stride of the allocated buffer object in bytes
178af69d88dSmrg */
1793464ebd5SriastradhGBM_EXPORT uint32_t
180af69d88dSmrggbm_bo_get_stride(struct gbm_bo *bo)
1813464ebd5Sriastradh{
18201e04c3fSmrg   return gbm_bo_get_stride_for_plane(bo, 0);
18301e04c3fSmrg}
18401e04c3fSmrg
18501e04c3fSmrg/** Get the stride for the given plane
18601e04c3fSmrg *
18701e04c3fSmrg * \param bo The buffer object
18801e04c3fSmrg * \param plane for which you want the stride
18901e04c3fSmrg *
19001e04c3fSmrg * \sa gbm_bo_get_stride()
19101e04c3fSmrg */
19201e04c3fSmrgGBM_EXPORT uint32_t
19301e04c3fSmrggbm_bo_get_stride_for_plane(struct gbm_bo *bo, int plane)
19401e04c3fSmrg{
19501e04c3fSmrg   return bo->gbm->bo_get_stride(bo, plane);
1963464ebd5Sriastradh}
1973464ebd5Sriastradh
198af69d88dSmrg/** Get the format of the buffer object
199af69d88dSmrg *
200af69d88dSmrg * The format of the pixels in the buffer.
201af69d88dSmrg *
202af69d88dSmrg * \param bo The buffer object
20301e04c3fSmrg * \return The format of buffer object, one of the GBM_FORMAT_* codes
204af69d88dSmrg */
205af69d88dSmrgGBM_EXPORT uint32_t
206af69d88dSmrggbm_bo_get_format(struct gbm_bo *bo)
207af69d88dSmrg{
208af69d88dSmrg   return bo->format;
209af69d88dSmrg}
210af69d88dSmrg
21101e04c3fSmrg/** Get the bit-per-pixel of the buffer object's format
21201e04c3fSmrg *
21301e04c3fSmrg * The bits-per-pixel of the buffer object's format.
21401e04c3fSmrg *
21501e04c3fSmrg * Note; The 'in-memory pixel' concept makes no sense for YUV formats
21601e04c3fSmrg * (pixels are the result of the combination of multiple memory sources:
21701e04c3fSmrg * Y, Cb & Cr; usually these are even in separate buffers), so YUV
21801e04c3fSmrg * formats are not supported by this function.
21901e04c3fSmrg *
22001e04c3fSmrg * \param bo The buffer object
22101e04c3fSmrg * \return The number of bits0per-pixel of the buffer object's format.
22201e04c3fSmrg */
22301e04c3fSmrgGBM_EXPORT uint32_t
22401e04c3fSmrggbm_bo_get_bpp(struct gbm_bo *bo)
22501e04c3fSmrg{
22601e04c3fSmrg   switch (bo->format) {
22701e04c3fSmrg      default:
22801e04c3fSmrg         return 0;
22901e04c3fSmrg      case GBM_FORMAT_C8:
23001e04c3fSmrg      case GBM_FORMAT_R8:
23101e04c3fSmrg      case GBM_FORMAT_RGB332:
23201e04c3fSmrg      case GBM_FORMAT_BGR233:
23301e04c3fSmrg         return 8;
23401e04c3fSmrg      case GBM_FORMAT_GR88:
23501e04c3fSmrg      case GBM_FORMAT_XRGB4444:
23601e04c3fSmrg      case GBM_FORMAT_XBGR4444:
23701e04c3fSmrg      case GBM_FORMAT_RGBX4444:
23801e04c3fSmrg      case GBM_FORMAT_BGRX4444:
23901e04c3fSmrg      case GBM_FORMAT_ARGB4444:
24001e04c3fSmrg      case GBM_FORMAT_ABGR4444:
24101e04c3fSmrg      case GBM_FORMAT_RGBA4444:
24201e04c3fSmrg      case GBM_FORMAT_BGRA4444:
24301e04c3fSmrg      case GBM_FORMAT_XRGB1555:
24401e04c3fSmrg      case GBM_FORMAT_XBGR1555:
24501e04c3fSmrg      case GBM_FORMAT_RGBX5551:
24601e04c3fSmrg      case GBM_FORMAT_BGRX5551:
24701e04c3fSmrg      case GBM_FORMAT_ARGB1555:
24801e04c3fSmrg      case GBM_FORMAT_ABGR1555:
24901e04c3fSmrg      case GBM_FORMAT_RGBA5551:
25001e04c3fSmrg      case GBM_FORMAT_BGRA5551:
25101e04c3fSmrg      case GBM_FORMAT_RGB565:
25201e04c3fSmrg      case GBM_FORMAT_BGR565:
25301e04c3fSmrg         return 16;
25401e04c3fSmrg      case GBM_FORMAT_RGB888:
25501e04c3fSmrg      case GBM_FORMAT_BGR888:
25601e04c3fSmrg         return 24;
25701e04c3fSmrg      case GBM_FORMAT_XRGB8888:
25801e04c3fSmrg      case GBM_FORMAT_XBGR8888:
25901e04c3fSmrg      case GBM_FORMAT_RGBX8888:
26001e04c3fSmrg      case GBM_FORMAT_BGRX8888:
26101e04c3fSmrg      case GBM_FORMAT_ARGB8888:
26201e04c3fSmrg      case GBM_FORMAT_ABGR8888:
26301e04c3fSmrg      case GBM_FORMAT_RGBA8888:
26401e04c3fSmrg      case GBM_FORMAT_BGRA8888:
26501e04c3fSmrg      case GBM_FORMAT_XRGB2101010:
26601e04c3fSmrg      case GBM_FORMAT_XBGR2101010:
26701e04c3fSmrg      case GBM_FORMAT_RGBX1010102:
26801e04c3fSmrg      case GBM_FORMAT_BGRX1010102:
26901e04c3fSmrg      case GBM_FORMAT_ARGB2101010:
27001e04c3fSmrg      case GBM_FORMAT_ABGR2101010:
27101e04c3fSmrg      case GBM_FORMAT_RGBA1010102:
27201e04c3fSmrg      case GBM_FORMAT_BGRA1010102:
27301e04c3fSmrg         return 32;
27401e04c3fSmrg   }
27501e04c3fSmrg}
27601e04c3fSmrg
27701e04c3fSmrg/** Get the offset for the data of the specified plane
27801e04c3fSmrg *
27901e04c3fSmrg * Extra planes, and even the first plane, may have an offset from the start of
28001e04c3fSmrg * the buffer object. This function will provide the offset for the given plane
28101e04c3fSmrg * to be used in various KMS APIs.
28201e04c3fSmrg *
28301e04c3fSmrg * \param bo The buffer object
28401e04c3fSmrg * \return The offset
28501e04c3fSmrg */
28601e04c3fSmrgGBM_EXPORT uint32_t
28701e04c3fSmrggbm_bo_get_offset(struct gbm_bo *bo, int plane)
28801e04c3fSmrg{
28901e04c3fSmrg   return bo->gbm->bo_get_offset(bo, plane);
29001e04c3fSmrg}
29101e04c3fSmrg
29201e04c3fSmrg/** Get the gbm device used to create the buffer object
29301e04c3fSmrg *
29401e04c3fSmrg * \param bo The buffer object
29501e04c3fSmrg * \return Returns the gbm device with which the buffer object was created
29601e04c3fSmrg */
29701e04c3fSmrgGBM_EXPORT struct gbm_device *
29801e04c3fSmrggbm_bo_get_device(struct gbm_bo *bo)
29901e04c3fSmrg{
30001e04c3fSmrg	return bo->gbm;
30101e04c3fSmrg}
30201e04c3fSmrg
303af69d88dSmrg/** Get the handle of the buffer object
304af69d88dSmrg *
305af69d88dSmrg * This is stored in the platform generic union gbm_bo_handle type. However
306af69d88dSmrg * the format of this handle is platform specific.
307af69d88dSmrg *
308af69d88dSmrg * \param bo The buffer object
309af69d88dSmrg * \return Returns the handle of the allocated buffer object
310af69d88dSmrg */
3113464ebd5SriastradhGBM_EXPORT union gbm_bo_handle
3123464ebd5Sriastradhgbm_bo_get_handle(struct gbm_bo *bo)
3133464ebd5Sriastradh{
3143464ebd5Sriastradh   return bo->handle;
3153464ebd5Sriastradh}
3163464ebd5Sriastradh
317af69d88dSmrg/** Get a DMA-BUF file descriptor for the buffer object
318af69d88dSmrg *
319af69d88dSmrg * This function creates a DMA-BUF (also known as PRIME) file descriptor
32001e04c3fSmrg * handle for the buffer object.  Each call to gbm_bo_get_fd() returns a new
321af69d88dSmrg * file descriptor and the caller is responsible for closing the file
322af69d88dSmrg * descriptor.
323af69d88dSmrg
324af69d88dSmrg * \param bo The buffer object
32501e04c3fSmrg * \return Returns a file descriptor referring to the underlying buffer or -1
32601e04c3fSmrg * if an error occurs.
327af69d88dSmrg */
328af69d88dSmrgGBM_EXPORT int
329af69d88dSmrggbm_bo_get_fd(struct gbm_bo *bo)
330af69d88dSmrg{
331af69d88dSmrg   return bo->gbm->bo_get_fd(bo);
332af69d88dSmrg}
333af69d88dSmrg
33401e04c3fSmrg/** Get the number of planes for the given bo.
33501e04c3fSmrg *
33601e04c3fSmrg * \param bo The buffer object
33701e04c3fSmrg * \return The number of planes
33801e04c3fSmrg */
33901e04c3fSmrgGBM_EXPORT int
34001e04c3fSmrggbm_bo_get_plane_count(struct gbm_bo *bo)
34101e04c3fSmrg{
34201e04c3fSmrg   return bo->gbm->bo_get_planes(bo);
34301e04c3fSmrg}
34401e04c3fSmrg
34501e04c3fSmrg/** Get the handle for the specified plane of the buffer object
34601e04c3fSmrg *
34701e04c3fSmrg * This function gets the handle for any plane associated with the BO. When
34801e04c3fSmrg * dealing with multi-planar formats, or formats which might have implicit
34901e04c3fSmrg * planes based on different underlying hardware it is necessary for the client
35001e04c3fSmrg * to be able to get this information to pass to the DRM.
35101e04c3fSmrg *
35201e04c3fSmrg * \param bo The buffer object
35301e04c3fSmrg * \param plane the plane to get a handle for
35401e04c3fSmrg *
35501e04c3fSmrg * \sa gbm_bo_get_handle()
35601e04c3fSmrg */
35701e04c3fSmrgGBM_EXPORT union gbm_bo_handle
35801e04c3fSmrggbm_bo_get_handle_for_plane(struct gbm_bo *bo, int plane)
35901e04c3fSmrg{
36001e04c3fSmrg   return bo->gbm->bo_get_handle(bo, plane);
36101e04c3fSmrg}
36201e04c3fSmrg
36301e04c3fSmrg/**
36401e04c3fSmrg * Get the chosen modifier for the buffer object
36501e04c3fSmrg *
36601e04c3fSmrg * This function returns the modifier that was chosen for the object. These
36701e04c3fSmrg * properties may be generic, or platform/implementation dependent.
36801e04c3fSmrg *
36901e04c3fSmrg * \param bo The buffer object
37001e04c3fSmrg * \return Returns the selected modifier (chosen by the implementation) for the
37101e04c3fSmrg * BO.
37201e04c3fSmrg * \sa gbm_bo_create_with_modifiers() where possible modifiers are set
37301e04c3fSmrg * \sa gbm_surface_create_with_modifiers() where possible modifiers are set
37401e04c3fSmrg * \sa define DRM_FORMAT_MOD_* in drm_fourcc.h for possible modifiers
37501e04c3fSmrg */
37601e04c3fSmrgGBM_EXPORT uint64_t
37701e04c3fSmrggbm_bo_get_modifier(struct gbm_bo *bo)
37801e04c3fSmrg{
37901e04c3fSmrg   return bo->gbm->bo_get_modifier(bo);
38001e04c3fSmrg}
381af69d88dSmrg
382af69d88dSmrg/** Write data into the buffer object
383af69d88dSmrg *
384af69d88dSmrg * If the buffer object was created with the GBM_BO_USE_WRITE flag,
38501e04c3fSmrg * this function can be used to write data into the buffer object.  The
38601e04c3fSmrg * data is copied directly into the object and it's the responsibility
387af69d88dSmrg * of the caller to make sure the data represents valid pixel data,
388af69d88dSmrg * according to the width, height, stride and format of the buffer object.
389af69d88dSmrg *
390af69d88dSmrg * \param bo The buffer object
391af69d88dSmrg * \param buf The data to write
392af69d88dSmrg * \param count The number of bytes to write
393af69d88dSmrg * \return Returns 0 on success, otherwise -1 is returned an errno set
394af69d88dSmrg */
395af69d88dSmrgGBM_EXPORT int
396af69d88dSmrggbm_bo_write(struct gbm_bo *bo, const void *buf, size_t count)
397af69d88dSmrg{
398af69d88dSmrg   return bo->gbm->bo_write(bo, buf, count);
399af69d88dSmrg}
400af69d88dSmrg
401af69d88dSmrg/** Set the user data associated with a buffer object
402af69d88dSmrg *
403af69d88dSmrg * \param bo The buffer object
404af69d88dSmrg * \param data The data to associate to the buffer object
405af69d88dSmrg * \param destroy_user_data A callback (which may be %NULL) that will be
406af69d88dSmrg * called prior to the buffer destruction
407af69d88dSmrg */
408af69d88dSmrgGBM_EXPORT void
409af69d88dSmrggbm_bo_set_user_data(struct gbm_bo *bo, void *data,
410af69d88dSmrg		     void (*destroy_user_data)(struct gbm_bo *, void *))
411af69d88dSmrg{
412af69d88dSmrg   bo->user_data = data;
413af69d88dSmrg   bo->destroy_user_data = destroy_user_data;
414af69d88dSmrg}
415af69d88dSmrg
416af69d88dSmrg/** Get the user data associated with a buffer object
417af69d88dSmrg *
418af69d88dSmrg * \param bo The buffer object
419af69d88dSmrg * \return Returns the user data associated with the buffer object or %NULL
420af69d88dSmrg * if no data was associated with it
421af69d88dSmrg *
422af69d88dSmrg * \sa gbm_bo_set_user_data()
423af69d88dSmrg */
424af69d88dSmrgGBM_EXPORT void *
425af69d88dSmrggbm_bo_get_user_data(struct gbm_bo *bo)
426af69d88dSmrg{
427af69d88dSmrg   return bo->user_data;
428af69d88dSmrg}
429af69d88dSmrg
430af69d88dSmrg/**
431af69d88dSmrg * Destroys the given buffer object and frees all resources associated with
432af69d88dSmrg * it.
433af69d88dSmrg *
434af69d88dSmrg * \param bo The buffer object
435af69d88dSmrg */
4363464ebd5SriastradhGBM_EXPORT void
4373464ebd5Sriastradhgbm_bo_destroy(struct gbm_bo *bo)
4383464ebd5Sriastradh{
439af69d88dSmrg   if (bo->destroy_user_data)
440af69d88dSmrg      bo->destroy_user_data(bo, bo->user_data);
441af69d88dSmrg
4423464ebd5Sriastradh   bo->gbm->bo_destroy(bo);
4433464ebd5Sriastradh}
4443464ebd5Sriastradh
445af69d88dSmrg/**
446af69d88dSmrg * Allocate a buffer object for the given dimensions
447af69d88dSmrg *
448af69d88dSmrg * \param gbm The gbm device returned from gbm_create_device()
449af69d88dSmrg * \param width The width for the buffer
450af69d88dSmrg * \param height The height for the buffer
451af69d88dSmrg * \param format The format to use for the buffer
452af69d88dSmrg * \param usage The union of the usage flags for this buffer
453af69d88dSmrg *
454af69d88dSmrg * \return A newly allocated buffer that should be freed with gbm_bo_destroy()
455af69d88dSmrg * when no longer needed. If an error occurs during allocation %NULL will be
456af69d88dSmrg * returned and errno set.
457af69d88dSmrg *
458af69d88dSmrg * \sa enum gbm_bo_format for the list of formats
459af69d88dSmrg * \sa enum gbm_bo_flags for the list of usage flags
460af69d88dSmrg */
4613464ebd5SriastradhGBM_EXPORT struct gbm_bo *
4623464ebd5Sriastradhgbm_bo_create(struct gbm_device *gbm,
4633464ebd5Sriastradh              uint32_t width, uint32_t height,
464af69d88dSmrg              uint32_t format, uint32_t usage)
4653464ebd5Sriastradh{
466af69d88dSmrg   if (width == 0 || height == 0) {
467af69d88dSmrg      errno = EINVAL;
4683464ebd5Sriastradh      return NULL;
469af69d88dSmrg   }
4703464ebd5Sriastradh
47101e04c3fSmrg   return gbm->bo_create(gbm, width, height, format, usage, NULL, 0);
4723464ebd5Sriastradh}
4733464ebd5Sriastradh
47401e04c3fSmrgGBM_EXPORT struct gbm_bo *
47501e04c3fSmrggbm_bo_create_with_modifiers(struct gbm_device *gbm,
47601e04c3fSmrg                             uint32_t width, uint32_t height,
47701e04c3fSmrg                             uint32_t format,
47801e04c3fSmrg                             const uint64_t *modifiers,
47901e04c3fSmrg                             const unsigned int count)
48001e04c3fSmrg{
48101e04c3fSmrg   if (width == 0 || height == 0) {
48201e04c3fSmrg      errno = EINVAL;
48301e04c3fSmrg      return NULL;
48401e04c3fSmrg   }
48501e04c3fSmrg
48601e04c3fSmrg   if ((count && !modifiers) || (modifiers && !count)) {
48701e04c3fSmrg      errno = EINVAL;
48801e04c3fSmrg      return NULL;
48901e04c3fSmrg   }
49001e04c3fSmrg
49101e04c3fSmrg   return gbm->bo_create(gbm, width, height, format, 0, modifiers, count);
49201e04c3fSmrg}
493af69d88dSmrg/**
494af69d88dSmrg * Create a gbm buffer object from an foreign object
495af69d88dSmrg *
496af69d88dSmrg * This function imports a foreign object and creates a new gbm bo for it.
497af69d88dSmrg * This enabled using the foreign object with a display API such as KMS.
49801e04c3fSmrg * Currently three types of foreign objects are supported, indicated by the type
499af69d88dSmrg * argument:
500af69d88dSmrg *
501af69d88dSmrg *   GBM_BO_IMPORT_WL_BUFFER
502af69d88dSmrg *   GBM_BO_IMPORT_EGL_IMAGE
503af69d88dSmrg *   GBM_BO_IMPORT_FD
504af69d88dSmrg *
50501e04c3fSmrg * The gbm bo shares the underlying pixels but its life-time is
506af69d88dSmrg * independent of the foreign object.
507af69d88dSmrg *
508af69d88dSmrg * \param gbm The gbm device returned from gbm_create_device()
50901e04c3fSmrg * \param type The type of object we're importing
51001e04c3fSmrg * \param buffer Pointer to the external object
511af69d88dSmrg * \param usage The union of the usage flags for this buffer
512af69d88dSmrg *
513af69d88dSmrg * \return A newly allocated buffer object that should be freed with
514af69d88dSmrg * gbm_bo_destroy() when no longer needed. On error, %NULL is returned
515af69d88dSmrg * and errno is set.
516af69d88dSmrg *
517af69d88dSmrg * \sa enum gbm_bo_flags for the list of usage flags
518af69d88dSmrg */
5193464ebd5SriastradhGBM_EXPORT struct gbm_bo *
520af69d88dSmrggbm_bo_import(struct gbm_device *gbm,
521af69d88dSmrg              uint32_t type, void *buffer, uint32_t usage)
5223464ebd5Sriastradh{
523af69d88dSmrg   return gbm->bo_import(gbm, type, buffer, usage);
524af69d88dSmrg}
525af69d88dSmrg
52601e04c3fSmrg/**
52701e04c3fSmrg * Map a region of a gbm buffer object for cpu access
52801e04c3fSmrg *
52901e04c3fSmrg * This function maps a region of a gbm bo for cpu read and/or write
53001e04c3fSmrg * access.
53101e04c3fSmrg *
53201e04c3fSmrg * \param bo The buffer object
53301e04c3fSmrg * \param x The X (top left origin) starting position of the mapped region for
53401e04c3fSmrg * the buffer
53501e04c3fSmrg * \param y The Y (top left origin) starting position of the mapped region for
53601e04c3fSmrg * the buffer
53701e04c3fSmrg * \param width The width of the mapped region for the buffer
53801e04c3fSmrg * \param height The height of the mapped region for the buffer
53901e04c3fSmrg * \param flags The union of the GBM_BO_TRANSFER_* flags for this buffer
54001e04c3fSmrg * \param stride Ptr for returned stride in bytes of the mapped region
54101e04c3fSmrg * \param map_data Returned opaque ptr for the mapped region
54201e04c3fSmrg *
54301e04c3fSmrg * \return Address of the mapped buffer that should be unmapped with
54401e04c3fSmrg * gbm_bo_unmap() when no longer needed. On error, %NULL is returned
54501e04c3fSmrg * and errno is set.
54601e04c3fSmrg *
54701e04c3fSmrg * \sa enum gbm_bo_transfer_flags for the list of flags
54801e04c3fSmrg */
54901e04c3fSmrgGBM_EXPORT void *
55001e04c3fSmrggbm_bo_map(struct gbm_bo *bo,
55101e04c3fSmrg              uint32_t x, uint32_t y,
55201e04c3fSmrg              uint32_t width, uint32_t height,
55301e04c3fSmrg              uint32_t flags, uint32_t *stride, void **map_data)
55401e04c3fSmrg{
55501e04c3fSmrg   if (!bo || width == 0 || height == 0 || !stride || !map_data) {
55601e04c3fSmrg      errno = EINVAL;
55701e04c3fSmrg      return NULL;
55801e04c3fSmrg   }
55901e04c3fSmrg
56001e04c3fSmrg   return bo->gbm->bo_map(bo, x, y, width, height,
56101e04c3fSmrg                          flags, stride, map_data);
56201e04c3fSmrg}
56301e04c3fSmrg
56401e04c3fSmrg/**
56501e04c3fSmrg * Unmap a previously mapped region of a gbm buffer object
56601e04c3fSmrg *
56701e04c3fSmrg * This function unmaps a region of a gbm bo for cpu read and/or write
56801e04c3fSmrg * access.
56901e04c3fSmrg *
57001e04c3fSmrg * \param bo The buffer object
57101e04c3fSmrg * \param map_data opaque ptr returned from prior gbm_bo_map
57201e04c3fSmrg */
57301e04c3fSmrgGBM_EXPORT void
57401e04c3fSmrggbm_bo_unmap(struct gbm_bo *bo, void *map_data)
57501e04c3fSmrg{
57601e04c3fSmrg   bo->gbm->bo_unmap(bo, map_data);
57701e04c3fSmrg}
57801e04c3fSmrg
579af69d88dSmrg/**
580af69d88dSmrg * Allocate a surface object
581af69d88dSmrg *
582af69d88dSmrg * \param gbm The gbm device returned from gbm_create_device()
583af69d88dSmrg * \param width The width for the surface
584af69d88dSmrg * \param height The height for the surface
585af69d88dSmrg * \param format The format to use for the surface
586af69d88dSmrg *
587af69d88dSmrg * \return A newly allocated surface that should be freed with
588af69d88dSmrg * gbm_surface_destroy() when no longer needed. If an error occurs
589af69d88dSmrg * during allocation %NULL will be returned.
590af69d88dSmrg *
591af69d88dSmrg * \sa enum gbm_bo_format for the list of formats
592af69d88dSmrg */
593af69d88dSmrgGBM_EXPORT struct gbm_surface *
594af69d88dSmrggbm_surface_create(struct gbm_device *gbm,
595af69d88dSmrg                   uint32_t width, uint32_t height,
596af69d88dSmrg		   uint32_t format, uint32_t flags)
597af69d88dSmrg{
59801e04c3fSmrg   return gbm->surface_create(gbm, width, height, format, flags, NULL, 0);
59901e04c3fSmrg}
60001e04c3fSmrg
60101e04c3fSmrgGBM_EXPORT struct gbm_surface *
60201e04c3fSmrggbm_surface_create_with_modifiers(struct gbm_device *gbm,
60301e04c3fSmrg                                  uint32_t width, uint32_t height,
60401e04c3fSmrg                                  uint32_t format,
60501e04c3fSmrg                                  const uint64_t *modifiers,
60601e04c3fSmrg                                  const unsigned int count)
60701e04c3fSmrg{
60801e04c3fSmrg   if ((count && !modifiers) || (modifiers && !count)) {
60901e04c3fSmrg      errno = EINVAL;
61001e04c3fSmrg      return NULL;
61101e04c3fSmrg   }
61201e04c3fSmrg
61301e04c3fSmrg   return gbm->surface_create(gbm, width, height, format, 0,
61401e04c3fSmrg                              modifiers, count);
615af69d88dSmrg}
616af69d88dSmrg
617af69d88dSmrg/**
618af69d88dSmrg * Destroys the given surface and frees all resources associated with
619af69d88dSmrg * it.
620af69d88dSmrg *
621af69d88dSmrg * All buffers locked with gbm_surface_lock_front_buffer() should be
622af69d88dSmrg * released prior to calling this function.
623af69d88dSmrg *
624af69d88dSmrg * \param surf The surface
625af69d88dSmrg */
626af69d88dSmrgGBM_EXPORT void
627af69d88dSmrggbm_surface_destroy(struct gbm_surface *surf)
628af69d88dSmrg{
629af69d88dSmrg   surf->gbm->surface_destroy(surf);
630af69d88dSmrg}
631af69d88dSmrg
632af69d88dSmrg/**
633af69d88dSmrg * Lock the surface's current front buffer
634af69d88dSmrg *
635af69d88dSmrg * Lock rendering to the surface's current front buffer until it is
636af69d88dSmrg * released with gbm_surface_release_buffer().
637af69d88dSmrg *
638af69d88dSmrg * This function must be called exactly once after calling
639af69d88dSmrg * eglSwapBuffers.  Calling it before any eglSwapBuffer has happened
640af69d88dSmrg * on the surface or two or more times after eglSwapBuffers is an
641af69d88dSmrg * error.  A new bo representing the new front buffer is returned.  On
642af69d88dSmrg * multiple invocations, all the returned bos must be released in
643af69d88dSmrg * order to release the actual surface buffer.
644af69d88dSmrg *
645af69d88dSmrg * \param surf The surface
646af69d88dSmrg *
647af69d88dSmrg * \return A buffer object that should be released with
648af69d88dSmrg * gbm_surface_release_buffer() when no longer needed.  The implementation
649af69d88dSmrg * is free to reuse buffers released with gbm_surface_release_buffer() so
650af69d88dSmrg * this bo should not be destroyed using gbm_bo_destroy().  If an error
651af69d88dSmrg * occurs this function returns %NULL.
652af69d88dSmrg */
653af69d88dSmrgGBM_EXPORT struct gbm_bo *
654af69d88dSmrggbm_surface_lock_front_buffer(struct gbm_surface *surf)
655af69d88dSmrg{
656af69d88dSmrg   return surf->gbm->surface_lock_front_buffer(surf);
657af69d88dSmrg}
6583464ebd5Sriastradh
659af69d88dSmrg/**
660af69d88dSmrg * Release a locked buffer obtained with gbm_surface_lock_front_buffer()
661af69d88dSmrg *
662af69d88dSmrg * Returns the underlying buffer to the gbm surface.  Releasing a bo
663af69d88dSmrg * will typically make gbm_surface_has_free_buffer() return 1 and thus
664af69d88dSmrg * allow rendering the next frame, but not always. The implementation
665af69d88dSmrg * may choose to destroy the bo immediately or reuse it, in which case
666af69d88dSmrg * the user data associated with it is unchanged.
667af69d88dSmrg *
668af69d88dSmrg * \param surf The surface
669af69d88dSmrg * \param bo The buffer object
670af69d88dSmrg */
671af69d88dSmrgGBM_EXPORT void
672af69d88dSmrggbm_surface_release_buffer(struct gbm_surface *surf, struct gbm_bo *bo)
673af69d88dSmrg{
674af69d88dSmrg   surf->gbm->surface_release_buffer(surf, bo);
675af69d88dSmrg}
676af69d88dSmrg
677af69d88dSmrg/**
678af69d88dSmrg * Return whether or not a surface has free (non-locked) buffers
679af69d88dSmrg *
680af69d88dSmrg * Before starting a new frame, the surface must have a buffer
681af69d88dSmrg * available for rendering.  Initially, a gbm surface will have a free
68201e04c3fSmrg * buffer, but after one or more buffers have been locked (\sa
683af69d88dSmrg * gbm_surface_lock_front_buffer()), the application must check for a
684af69d88dSmrg * free buffer before rendering.
685af69d88dSmrg *
686af69d88dSmrg * If a surface doesn't have a free buffer, the application must
687af69d88dSmrg * return a buffer to the surface using gbm_surface_release_buffer()
688af69d88dSmrg * and after that, the application can query for free buffers again.
689af69d88dSmrg *
690af69d88dSmrg * \param surf The surface
691af69d88dSmrg * \return 1 if the surface has free buffers, 0 otherwise
692af69d88dSmrg */
693af69d88dSmrgGBM_EXPORT int
694af69d88dSmrggbm_surface_has_free_buffers(struct gbm_surface *surf)
695af69d88dSmrg{
696af69d88dSmrg   return surf->gbm->surface_has_free_buffers(surf);
6973464ebd5Sriastradh}
698