gbm.c revision af69d88d
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#define _BSD_SOURCE
293464ebd5Sriastradh
303464ebd5Sriastradh#include <stddef.h>
313464ebd5Sriastradh#include <stdio.h>
323464ebd5Sriastradh#include <stdlib.h>
333464ebd5Sriastradh#include <string.h>
343464ebd5Sriastradh#include <stdint.h>
353464ebd5Sriastradh
363464ebd5Sriastradh#include <sys/types.h>
373464ebd5Sriastradh#include <sys/stat.h>
383464ebd5Sriastradh#include <unistd.h>
39af69d88dSmrg#include <errno.h>
403464ebd5Sriastradh
413464ebd5Sriastradh#include "gbm.h"
423464ebd5Sriastradh#include "gbmint.h"
433464ebd5Sriastradh#include "backend.h"
443464ebd5Sriastradh
453464ebd5Sriastradh#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
463464ebd5Sriastradh
47af69d88dSmrgstatic struct gbm_device *devices[16];
483464ebd5Sriastradh
493464ebd5Sriastradhstatic int device_num = 0;
503464ebd5Sriastradh
51af69d88dSmrg/** Returns the file description for the gbm device
52af69d88dSmrg *
53af69d88dSmrg * \return The fd that the struct gbm_device was created with
54af69d88dSmrg */
553464ebd5SriastradhGBM_EXPORT int
563464ebd5Sriastradhgbm_device_get_fd(struct gbm_device *gbm)
573464ebd5Sriastradh{
583464ebd5Sriastradh   return gbm->fd;
593464ebd5Sriastradh}
603464ebd5Sriastradh
613464ebd5Sriastradh/* FIXME: maybe superfluous, use udev subclass from the fd? */
62af69d88dSmrg/** Get the backend name for the given gbm device
63af69d88dSmrg *
64af69d88dSmrg * \return The backend name string - this belongs to the device and must not
65af69d88dSmrg * be freed
66af69d88dSmrg */
673464ebd5SriastradhGBM_EXPORT const char *
683464ebd5Sriastradhgbm_device_get_backend_name(struct gbm_device *gbm)
693464ebd5Sriastradh{
703464ebd5Sriastradh   return gbm->name;
713464ebd5Sriastradh}
723464ebd5Sriastradh
73af69d88dSmrg/** Test if a format is supported for a given set of usage flags.
74af69d88dSmrg *
75af69d88dSmrg * \param gbm The created buffer manager
76af69d88dSmrg * \param format The format to test
77af69d88dSmrg * \param usage A bitmask of the usages to test the format against
78af69d88dSmrg * \return 1 if the format is supported otherwise 0
79af69d88dSmrg *
80af69d88dSmrg * \sa enum gbm_bo_flags for the list of flags that the format can be
81af69d88dSmrg * tested against
82af69d88dSmrg *
83af69d88dSmrg * \sa enum gbm_bo_format for the list of formats
84af69d88dSmrg */
85af69d88dSmrgGBM_EXPORT int
863464ebd5Sriastradhgbm_device_is_format_supported(struct gbm_device *gbm,
87af69d88dSmrg                               uint32_t format, uint32_t usage)
883464ebd5Sriastradh{
893464ebd5Sriastradh   return gbm->is_format_supported(gbm, format, usage);
903464ebd5Sriastradh}
913464ebd5Sriastradh
92af69d88dSmrg/** Destroy the gbm device and free all resources associated with it.
93af69d88dSmrg *
94af69d88dSmrg * \param gbm The device created using gbm_create_device()
95af69d88dSmrg */
963464ebd5SriastradhGBM_EXPORT void
973464ebd5Sriastradhgbm_device_destroy(struct gbm_device *gbm)
983464ebd5Sriastradh{
993464ebd5Sriastradh   gbm->refcount--;
1003464ebd5Sriastradh   if (gbm->refcount == 0)
1013464ebd5Sriastradh      gbm->destroy(gbm);
1023464ebd5Sriastradh}
1033464ebd5Sriastradh
104af69d88dSmrgstruct gbm_device *
1053464ebd5Sriastradh_gbm_mesa_get_device(int fd)
1063464ebd5Sriastradh{
1073464ebd5Sriastradh   struct gbm_device *gbm = NULL;
1083464ebd5Sriastradh   struct stat buf;
1093464ebd5Sriastradh   dev_t dev;
1103464ebd5Sriastradh   int i;
1113464ebd5Sriastradh
1123464ebd5Sriastradh   if (fd < 0 || fstat(fd, &buf) < 0 || !S_ISCHR(buf.st_mode)) {
113af69d88dSmrg      errno = EINVAL;
1143464ebd5Sriastradh      return NULL;
1153464ebd5Sriastradh   }
1163464ebd5Sriastradh
1173464ebd5Sriastradh   for (i = 0; i < device_num; ++i) {
1183464ebd5Sriastradh      dev = devices[i]->stat.st_rdev;
1193464ebd5Sriastradh      if (major(dev) == major(buf.st_rdev) &&
1203464ebd5Sriastradh          minor(dev) == minor(buf.st_rdev)) {
1213464ebd5Sriastradh         gbm = devices[i];
1223464ebd5Sriastradh         gbm->refcount++;
1233464ebd5Sriastradh         break;
1243464ebd5Sriastradh      }
1253464ebd5Sriastradh   }
1263464ebd5Sriastradh
1273464ebd5Sriastradh   return gbm;
1283464ebd5Sriastradh}
1293464ebd5Sriastradh
130af69d88dSmrg/** Create a gbm device for allocating buffers
131af69d88dSmrg *
132af69d88dSmrg * The file descriptor passed in is used by the backend to communicate with
133af69d88dSmrg * platform for allocating the memory. For allocations using DRI this would be
134af69d88dSmrg * the file descriptor returned when opening a device such as \c
135af69d88dSmrg * /dev/dri/card0
136af69d88dSmrg *
137af69d88dSmrg * \param fd The file descriptor for an backend specific device
138af69d88dSmrg * \return The newly created struct gbm_device. The resources associated with
139af69d88dSmrg * the device should be freed with gbm_device_destroy() when it is no longer
140af69d88dSmrg * needed. If the creation of the device failed NULL will be returned.
141af69d88dSmrg */
1423464ebd5SriastradhGBM_EXPORT struct gbm_device *
1433464ebd5Sriastradhgbm_create_device(int fd)
1443464ebd5Sriastradh{
1453464ebd5Sriastradh   struct gbm_device *gbm = NULL;
1463464ebd5Sriastradh   struct stat buf;
1473464ebd5Sriastradh
1483464ebd5Sriastradh   if (fd < 0 || fstat(fd, &buf) < 0 || !S_ISCHR(buf.st_mode)) {
149af69d88dSmrg      errno = EINVAL;
1503464ebd5Sriastradh      return NULL;
1513464ebd5Sriastradh   }
1523464ebd5Sriastradh
1533464ebd5Sriastradh   if (device_num == 0)
1543464ebd5Sriastradh      memset(devices, 0, sizeof devices);
1553464ebd5Sriastradh
1563464ebd5Sriastradh   gbm = _gbm_create_device(fd);
1573464ebd5Sriastradh   if (gbm == NULL)
1583464ebd5Sriastradh      return NULL;
1593464ebd5Sriastradh
1603464ebd5Sriastradh   gbm->dummy = gbm_create_device;
1613464ebd5Sriastradh   gbm->stat = buf;
1623464ebd5Sriastradh   gbm->refcount = 1;
1633464ebd5Sriastradh
1643464ebd5Sriastradh   if (device_num < ARRAY_SIZE(devices)-1)
1653464ebd5Sriastradh      devices[device_num++] = gbm;
1663464ebd5Sriastradh
1673464ebd5Sriastradh   return gbm;
1683464ebd5Sriastradh}
1693464ebd5Sriastradh
170af69d88dSmrg/** Get the width of the buffer object
171af69d88dSmrg *
172af69d88dSmrg * \param bo The buffer object
173af69d88dSmrg * \return The width of the allocated buffer object
174af69d88dSmrg *
175af69d88dSmrg */
1763464ebd5SriastradhGBM_EXPORT unsigned int
1773464ebd5Sriastradhgbm_bo_get_width(struct gbm_bo *bo)
1783464ebd5Sriastradh{
1793464ebd5Sriastradh   return bo->width;
1803464ebd5Sriastradh}
1813464ebd5Sriastradh
182af69d88dSmrg/** Get the height of the buffer object
183af69d88dSmrg *
184af69d88dSmrg * \param bo The buffer object
185af69d88dSmrg * \return The height of the allocated buffer object
186af69d88dSmrg */
1873464ebd5SriastradhGBM_EXPORT unsigned int
1883464ebd5Sriastradhgbm_bo_get_height(struct gbm_bo *bo)
1893464ebd5Sriastradh{
1903464ebd5Sriastradh   return bo->height;
1913464ebd5Sriastradh}
1923464ebd5Sriastradh
193af69d88dSmrg/** Get the stride of the buffer object
194af69d88dSmrg *
195af69d88dSmrg * This is calculated by the backend when it does the allocation in
196af69d88dSmrg * gbm_bo_create()
197af69d88dSmrg *
198af69d88dSmrg * \param bo The buffer object
199af69d88dSmrg * \return The stride of the allocated buffer object in bytes
200af69d88dSmrg */
2013464ebd5SriastradhGBM_EXPORT uint32_t
202af69d88dSmrggbm_bo_get_stride(struct gbm_bo *bo)
2033464ebd5Sriastradh{
204af69d88dSmrg   return bo->stride;
2053464ebd5Sriastradh}
2063464ebd5Sriastradh
207af69d88dSmrg/** Get the format of the buffer object
208af69d88dSmrg *
209af69d88dSmrg * The format of the pixels in the buffer.
210af69d88dSmrg *
211af69d88dSmrg * \param bo The buffer object
212af69d88dSmrg * \return The format of buffer object, on of the GBM_FORMAT_* codes
213af69d88dSmrg */
214af69d88dSmrgGBM_EXPORT uint32_t
215af69d88dSmrggbm_bo_get_format(struct gbm_bo *bo)
216af69d88dSmrg{
217af69d88dSmrg   return bo->format;
218af69d88dSmrg}
219af69d88dSmrg
220af69d88dSmrg/** Get the handle of the buffer object
221af69d88dSmrg *
222af69d88dSmrg * This is stored in the platform generic union gbm_bo_handle type. However
223af69d88dSmrg * the format of this handle is platform specific.
224af69d88dSmrg *
225af69d88dSmrg * \param bo The buffer object
226af69d88dSmrg * \return Returns the handle of the allocated buffer object
227af69d88dSmrg */
2283464ebd5SriastradhGBM_EXPORT union gbm_bo_handle
2293464ebd5Sriastradhgbm_bo_get_handle(struct gbm_bo *bo)
2303464ebd5Sriastradh{
2313464ebd5Sriastradh   return bo->handle;
2323464ebd5Sriastradh}
2333464ebd5Sriastradh
234af69d88dSmrg/** Get a DMA-BUF file descriptor for the buffer object
235af69d88dSmrg *
236af69d88dSmrg * This function creates a DMA-BUF (also known as PRIME) file descriptor
237af69d88dSmrg * handle for the buffer object.  Eeach call to gbm_bo_get_fd() returns a new
238af69d88dSmrg * file descriptor and the caller is responsible for closing the file
239af69d88dSmrg * descriptor.
240af69d88dSmrg
241af69d88dSmrg * \param bo The buffer object
242af69d88dSmrg * \return Returns a file descriptor referring  to the underlying buffer
243af69d88dSmrg */
244af69d88dSmrgGBM_EXPORT int
245af69d88dSmrggbm_bo_get_fd(struct gbm_bo *bo)
246af69d88dSmrg{
247af69d88dSmrg   return bo->gbm->bo_get_fd(bo);
248af69d88dSmrg}
249af69d88dSmrg
250af69d88dSmrg
251af69d88dSmrg/** Write data into the buffer object
252af69d88dSmrg *
253af69d88dSmrg * If the buffer object was created with the GBM_BO_USE_WRITE flag,
254af69d88dSmrg * this function can used to write data into the buffer object.  The
255af69d88dSmrg * data is copied directly into the object and it's the responsiblity
256af69d88dSmrg * of the caller to make sure the data represents valid pixel data,
257af69d88dSmrg * according to the width, height, stride and format of the buffer object.
258af69d88dSmrg *
259af69d88dSmrg * \param bo The buffer object
260af69d88dSmrg * \param buf The data to write
261af69d88dSmrg * \param count The number of bytes to write
262af69d88dSmrg * \return Returns 0 on success, otherwise -1 is returned an errno set
263af69d88dSmrg */
264af69d88dSmrgGBM_EXPORT int
265af69d88dSmrggbm_bo_write(struct gbm_bo *bo, const void *buf, size_t count)
266af69d88dSmrg{
267af69d88dSmrg   return bo->gbm->bo_write(bo, buf, count);
268af69d88dSmrg}
269af69d88dSmrg
270af69d88dSmrg/** Get the gbm device used to create the buffer object
271af69d88dSmrg *
272af69d88dSmrg * \param bo The buffer object
273af69d88dSmrg * \return Returns the gbm device with which the buffer object was created
274af69d88dSmrg */
275af69d88dSmrgGBM_EXPORT struct gbm_device *
276af69d88dSmrggbm_bo_get_device(struct gbm_bo *bo)
277af69d88dSmrg{
278af69d88dSmrg	return bo->gbm;
279af69d88dSmrg}
280af69d88dSmrg
281af69d88dSmrg/** Set the user data associated with a buffer object
282af69d88dSmrg *
283af69d88dSmrg * \param bo The buffer object
284af69d88dSmrg * \param data The data to associate to the buffer object
285af69d88dSmrg * \param destroy_user_data A callback (which may be %NULL) that will be
286af69d88dSmrg * called prior to the buffer destruction
287af69d88dSmrg */
288af69d88dSmrgGBM_EXPORT void
289af69d88dSmrggbm_bo_set_user_data(struct gbm_bo *bo, void *data,
290af69d88dSmrg		     void (*destroy_user_data)(struct gbm_bo *, void *))
291af69d88dSmrg{
292af69d88dSmrg   bo->user_data = data;
293af69d88dSmrg   bo->destroy_user_data = destroy_user_data;
294af69d88dSmrg}
295af69d88dSmrg
296af69d88dSmrg/** Get the user data associated with a buffer object
297af69d88dSmrg *
298af69d88dSmrg * \param bo The buffer object
299af69d88dSmrg * \return Returns the user data associated with the buffer object or %NULL
300af69d88dSmrg * if no data was associated with it
301af69d88dSmrg *
302af69d88dSmrg * \sa gbm_bo_set_user_data()
303af69d88dSmrg */
304af69d88dSmrgGBM_EXPORT void *
305af69d88dSmrggbm_bo_get_user_data(struct gbm_bo *bo)
306af69d88dSmrg{
307af69d88dSmrg   return bo->user_data;
308af69d88dSmrg}
309af69d88dSmrg
310af69d88dSmrg/**
311af69d88dSmrg * Destroys the given buffer object and frees all resources associated with
312af69d88dSmrg * it.
313af69d88dSmrg *
314af69d88dSmrg * \param bo The buffer object
315af69d88dSmrg */
3163464ebd5SriastradhGBM_EXPORT void
3173464ebd5Sriastradhgbm_bo_destroy(struct gbm_bo *bo)
3183464ebd5Sriastradh{
319af69d88dSmrg   if (bo->destroy_user_data)
320af69d88dSmrg      bo->destroy_user_data(bo, bo->user_data);
321af69d88dSmrg
3223464ebd5Sriastradh   bo->gbm->bo_destroy(bo);
3233464ebd5Sriastradh}
3243464ebd5Sriastradh
325af69d88dSmrg/**
326af69d88dSmrg * Allocate a buffer object for the given dimensions
327af69d88dSmrg *
328af69d88dSmrg * \param gbm The gbm device returned from gbm_create_device()
329af69d88dSmrg * \param width The width for the buffer
330af69d88dSmrg * \param height The height for the buffer
331af69d88dSmrg * \param format The format to use for the buffer
332af69d88dSmrg * \param usage The union of the usage flags for this buffer
333af69d88dSmrg *
334af69d88dSmrg * \return A newly allocated buffer that should be freed with gbm_bo_destroy()
335af69d88dSmrg * when no longer needed. If an error occurs during allocation %NULL will be
336af69d88dSmrg * returned and errno set.
337af69d88dSmrg *
338af69d88dSmrg * \sa enum gbm_bo_format for the list of formats
339af69d88dSmrg * \sa enum gbm_bo_flags for the list of usage flags
340af69d88dSmrg */
3413464ebd5SriastradhGBM_EXPORT struct gbm_bo *
3423464ebd5Sriastradhgbm_bo_create(struct gbm_device *gbm,
3433464ebd5Sriastradh              uint32_t width, uint32_t height,
344af69d88dSmrg              uint32_t format, uint32_t usage)
3453464ebd5Sriastradh{
346af69d88dSmrg   if (width == 0 || height == 0) {
347af69d88dSmrg      errno = EINVAL;
3483464ebd5Sriastradh      return NULL;
349af69d88dSmrg   }
3503464ebd5Sriastradh
3513464ebd5Sriastradh   return gbm->bo_create(gbm, width, height, format, usage);
3523464ebd5Sriastradh}
3533464ebd5Sriastradh
354af69d88dSmrg/**
355af69d88dSmrg * Create a gbm buffer object from an foreign object
356af69d88dSmrg *
357af69d88dSmrg * This function imports a foreign object and creates a new gbm bo for it.
358af69d88dSmrg * This enabled using the foreign object with a display API such as KMS.
359af69d88dSmrg * Currently two types of foreign objects are supported, indicated by the type
360af69d88dSmrg * argument:
361af69d88dSmrg *
362af69d88dSmrg *   GBM_BO_IMPORT_WL_BUFFER
363af69d88dSmrg *   GBM_BO_IMPORT_EGL_IMAGE
364af69d88dSmrg *   GBM_BO_IMPORT_FD
365af69d88dSmrg *
366af69d88dSmrg * The the gbm bo shares the underlying pixels but its life-time is
367af69d88dSmrg * independent of the foreign object.
368af69d88dSmrg *
369af69d88dSmrg * \param gbm The gbm device returned from gbm_create_device()
370af69d88dSmrg * \param gbm The type of object we're importing
371af69d88dSmrg * \param gbm Pointer to the external object
372af69d88dSmrg * \param usage The union of the usage flags for this buffer
373af69d88dSmrg *
374af69d88dSmrg * \return A newly allocated buffer object that should be freed with
375af69d88dSmrg * gbm_bo_destroy() when no longer needed. On error, %NULL is returned
376af69d88dSmrg * and errno is set.
377af69d88dSmrg *
378af69d88dSmrg * \sa enum gbm_bo_flags for the list of usage flags
379af69d88dSmrg */
3803464ebd5SriastradhGBM_EXPORT struct gbm_bo *
381af69d88dSmrggbm_bo_import(struct gbm_device *gbm,
382af69d88dSmrg              uint32_t type, void *buffer, uint32_t usage)
3833464ebd5Sriastradh{
384af69d88dSmrg   return gbm->bo_import(gbm, type, buffer, usage);
385af69d88dSmrg}
386af69d88dSmrg
387af69d88dSmrg/**
388af69d88dSmrg * Allocate a surface object
389af69d88dSmrg *
390af69d88dSmrg * \param gbm The gbm device returned from gbm_create_device()
391af69d88dSmrg * \param width The width for the surface
392af69d88dSmrg * \param height The height for the surface
393af69d88dSmrg * \param format The format to use for the surface
394af69d88dSmrg *
395af69d88dSmrg * \return A newly allocated surface that should be freed with
396af69d88dSmrg * gbm_surface_destroy() when no longer needed. If an error occurs
397af69d88dSmrg * during allocation %NULL will be returned.
398af69d88dSmrg *
399af69d88dSmrg * \sa enum gbm_bo_format for the list of formats
400af69d88dSmrg */
401af69d88dSmrgGBM_EXPORT struct gbm_surface *
402af69d88dSmrggbm_surface_create(struct gbm_device *gbm,
403af69d88dSmrg                   uint32_t width, uint32_t height,
404af69d88dSmrg		   uint32_t format, uint32_t flags)
405af69d88dSmrg{
406af69d88dSmrg   return gbm->surface_create(gbm, width, height, format, flags);
407af69d88dSmrg}
408af69d88dSmrg
409af69d88dSmrg/**
410af69d88dSmrg * Destroys the given surface and frees all resources associated with
411af69d88dSmrg * it.
412af69d88dSmrg *
413af69d88dSmrg * All buffers locked with gbm_surface_lock_front_buffer() should be
414af69d88dSmrg * released prior to calling this function.
415af69d88dSmrg *
416af69d88dSmrg * \param surf The surface
417af69d88dSmrg */
418af69d88dSmrgGBM_EXPORT void
419af69d88dSmrggbm_surface_destroy(struct gbm_surface *surf)
420af69d88dSmrg{
421af69d88dSmrg   surf->gbm->surface_destroy(surf);
422af69d88dSmrg}
423af69d88dSmrg
424af69d88dSmrg/**
425af69d88dSmrg * Lock the surface's current front buffer
426af69d88dSmrg *
427af69d88dSmrg * Lock rendering to the surface's current front buffer until it is
428af69d88dSmrg * released with gbm_surface_release_buffer().
429af69d88dSmrg *
430af69d88dSmrg * This function must be called exactly once after calling
431af69d88dSmrg * eglSwapBuffers.  Calling it before any eglSwapBuffer has happened
432af69d88dSmrg * on the surface or two or more times after eglSwapBuffers is an
433af69d88dSmrg * error.  A new bo representing the new front buffer is returned.  On
434af69d88dSmrg * multiple invocations, all the returned bos must be released in
435af69d88dSmrg * order to release the actual surface buffer.
436af69d88dSmrg *
437af69d88dSmrg * \param surf The surface
438af69d88dSmrg *
439af69d88dSmrg * \return A buffer object that should be released with
440af69d88dSmrg * gbm_surface_release_buffer() when no longer needed.  The implementation
441af69d88dSmrg * is free to reuse buffers released with gbm_surface_release_buffer() so
442af69d88dSmrg * this bo should not be destroyed using gbm_bo_destroy().  If an error
443af69d88dSmrg * occurs this function returns %NULL.
444af69d88dSmrg */
445af69d88dSmrgGBM_EXPORT struct gbm_bo *
446af69d88dSmrggbm_surface_lock_front_buffer(struct gbm_surface *surf)
447af69d88dSmrg{
448af69d88dSmrg   return surf->gbm->surface_lock_front_buffer(surf);
449af69d88dSmrg}
4503464ebd5Sriastradh
451af69d88dSmrg/**
452af69d88dSmrg * Release a locked buffer obtained with gbm_surface_lock_front_buffer()
453af69d88dSmrg *
454af69d88dSmrg * Returns the underlying buffer to the gbm surface.  Releasing a bo
455af69d88dSmrg * will typically make gbm_surface_has_free_buffer() return 1 and thus
456af69d88dSmrg * allow rendering the next frame, but not always. The implementation
457af69d88dSmrg * may choose to destroy the bo immediately or reuse it, in which case
458af69d88dSmrg * the user data associated with it is unchanged.
459af69d88dSmrg *
460af69d88dSmrg * \param surf The surface
461af69d88dSmrg * \param bo The buffer object
462af69d88dSmrg */
463af69d88dSmrgGBM_EXPORT void
464af69d88dSmrggbm_surface_release_buffer(struct gbm_surface *surf, struct gbm_bo *bo)
465af69d88dSmrg{
466af69d88dSmrg   surf->gbm->surface_release_buffer(surf, bo);
467af69d88dSmrg}
468af69d88dSmrg
469af69d88dSmrg/**
470af69d88dSmrg * Return whether or not a surface has free (non-locked) buffers
471af69d88dSmrg *
472af69d88dSmrg * Before starting a new frame, the surface must have a buffer
473af69d88dSmrg * available for rendering.  Initially, a gbm surface will have a free
474af69d88dSmrg * buffer, but after one of more buffers have been locked (\sa
475af69d88dSmrg * gbm_surface_lock_front_buffer()), the application must check for a
476af69d88dSmrg * free buffer before rendering.
477af69d88dSmrg *
478af69d88dSmrg * If a surface doesn't have a free buffer, the application must
479af69d88dSmrg * return a buffer to the surface using gbm_surface_release_buffer()
480af69d88dSmrg * and after that, the application can query for free buffers again.
481af69d88dSmrg *
482af69d88dSmrg * \param surf The surface
483af69d88dSmrg * \return 1 if the surface has free buffers, 0 otherwise
484af69d88dSmrg */
485af69d88dSmrgGBM_EXPORT int
486af69d88dSmrggbm_surface_has_free_buffers(struct gbm_surface *surf)
487af69d88dSmrg{
488af69d88dSmrg   return surf->gbm->surface_has_free_buffers(surf);
4893464ebd5Sriastradh}
490