xref: /third_party/mesa3d/src/gbm/main/gbm.c (revision bf215546)
1/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 *    Benjamin Franzke <benjaminfranzke@googlemail.com>
26 */
27
28#include <stddef.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdint.h>
33
34#ifdef MAJOR_IN_MKDEV
35#include <sys/mkdev.h>
36#endif
37#ifdef MAJOR_IN_SYSMACROS
38#include <sys/sysmacros.h>
39#endif
40#include <sys/stat.h>
41#include <unistd.h>
42#include <errno.h>
43
44#include "gbm.h"
45#include "gbmint.h"
46#include "backend.h"
47
48/** Returns the file description for the gbm device
49 *
50 * \return The fd that the struct gbm_device was created with
51 */
52GBM_EXPORT int
53gbm_device_get_fd(struct gbm_device *gbm)
54{
55   return gbm->v0.fd;
56}
57
58/** Get the backend name for the given gbm device
59 *
60 * \return The backend name string - this belongs to the device and must not
61 * be freed
62 */
63GBM_EXPORT const char *
64gbm_device_get_backend_name(struct gbm_device *gbm)
65{
66   return gbm->v0.name;
67}
68
69/** Test if a format is supported for a given set of usage flags.
70 *
71 * \param gbm The created buffer manager
72 * \param format The format to test
73 * \param flags A bitmask of the usages to test the format against
74 * \return 1 if the format is supported otherwise 0
75 *
76 * \sa enum gbm_bo_flags for the list of flags that the format can be
77 * tested against
78 *
79 * \sa enum gbm_bo_format for the list of formats
80 */
81GBM_EXPORT int
82gbm_device_is_format_supported(struct gbm_device *gbm,
83                               uint32_t format, uint32_t flags)
84{
85   return gbm->v0.is_format_supported(gbm, format, flags);
86}
87
88/** Get the number of planes that are required for a given format+modifier
89 *
90 * \param gbm The gbm device returned from gbm_create_device()
91 * \param format The format to query
92 * \param modifier The modifier to query
93 */
94GBM_EXPORT int
95gbm_device_get_format_modifier_plane_count(struct gbm_device *gbm,
96                                           uint32_t format,
97                                           uint64_t modifier)
98{
99   return gbm->v0.get_format_modifier_plane_count(gbm, format, modifier);
100}
101
102/** Destroy the gbm device and free all resources associated with it.
103 *
104 * Prior to calling this function all buffers and surfaces created with the
105 * gbm device need to be destroyed.
106 *
107 * \param gbm The device created using gbm_create_device()
108 */
109GBM_EXPORT void
110gbm_device_destroy(struct gbm_device *gbm)
111{
112   _gbm_device_destroy(gbm);
113}
114
115/** Create a gbm device for allocating buffers
116 *
117 * The file descriptor passed in is used by the backend to communicate with
118 * platform for allocating the memory. For allocations using DRI this would be
119 * the file descriptor returned when opening a device such as \c
120 * /dev/dri/card0
121 *
122 * \param fd The file descriptor for a backend specific device
123 * \return The newly created struct gbm_device. The resources associated with
124 * the device should be freed with gbm_device_destroy() when it is no longer
125 * needed. If the creation of the device failed NULL will be returned.
126 */
127GBM_EXPORT struct gbm_device *
128gbm_create_device(int fd)
129{
130   struct gbm_device *gbm = NULL;
131   struct stat buf;
132
133   if (fd < 0 || fstat(fd, &buf) < 0 || !S_ISCHR(buf.st_mode)) {
134      errno = EINVAL;
135      return NULL;
136   }
137
138   gbm = _gbm_create_device(fd);
139   if (gbm == NULL)
140      return NULL;
141
142   gbm->dummy = gbm_create_device;
143
144   return gbm;
145}
146
147/** Get the width of the buffer object
148 *
149 * \param bo The buffer object
150 * \return The width of the allocated buffer object
151 *
152 */
153GBM_EXPORT uint32_t
154gbm_bo_get_width(struct gbm_bo *bo)
155{
156   return bo->v0.width;
157}
158
159/** Get the height of the buffer object
160 *
161 * \param bo The buffer object
162 * \return The height of the allocated buffer object
163 */
164GBM_EXPORT uint32_t
165gbm_bo_get_height(struct gbm_bo *bo)
166{
167   return bo->v0.height;
168}
169
170/** Get the stride of the buffer object
171 *
172 * This is calculated by the backend when it does the allocation in
173 * gbm_bo_create()
174 *
175 * \param bo The buffer object
176 * \return The stride of the allocated buffer object in bytes
177 */
178GBM_EXPORT uint32_t
179gbm_bo_get_stride(struct gbm_bo *bo)
180{
181   return gbm_bo_get_stride_for_plane(bo, 0);
182}
183
184/** Get the stride for the given plane
185 *
186 * \param bo The buffer object
187 * \param plane for which you want the stride
188 *
189 * \sa gbm_bo_get_stride()
190 */
191GBM_EXPORT uint32_t
192gbm_bo_get_stride_for_plane(struct gbm_bo *bo, int plane)
193{
194   return bo->gbm->v0.bo_get_stride(bo, plane);
195}
196
197/** Get the format of the buffer object
198 *
199 * The format of the pixels in the buffer.
200 *
201 * \param bo The buffer object
202 * \return The format of buffer object, one of the GBM_FORMAT_* codes
203 */
204GBM_EXPORT uint32_t
205gbm_bo_get_format(struct gbm_bo *bo)
206{
207   return bo->v0.format;
208}
209
210/** Get the bit-per-pixel of the buffer object's format
211 *
212 * The bits-per-pixel of the buffer object's format.
213 *
214 * Note; The 'in-memory pixel' concept makes no sense for YUV formats
215 * (pixels are the result of the combination of multiple memory sources:
216 * Y, Cb & Cr; usually these are even in separate buffers), so YUV
217 * formats are not supported by this function.
218 *
219 * \param bo The buffer object
220 * \return The number of bits0per-pixel of the buffer object's format.
221 */
222GBM_EXPORT uint32_t
223gbm_bo_get_bpp(struct gbm_bo *bo)
224{
225   switch (bo->v0.format) {
226      default:
227         return 0;
228      case GBM_FORMAT_C8:
229      case GBM_FORMAT_R8:
230      case GBM_FORMAT_RGB332:
231      case GBM_FORMAT_BGR233:
232         return 8;
233      case GBM_FORMAT_R16:
234      case GBM_FORMAT_GR88:
235      case GBM_FORMAT_XRGB4444:
236      case GBM_FORMAT_XBGR4444:
237      case GBM_FORMAT_RGBX4444:
238      case GBM_FORMAT_BGRX4444:
239      case GBM_FORMAT_ARGB4444:
240      case GBM_FORMAT_ABGR4444:
241      case GBM_FORMAT_RGBA4444:
242      case GBM_FORMAT_BGRA4444:
243      case GBM_FORMAT_XRGB1555:
244      case GBM_FORMAT_XBGR1555:
245      case GBM_FORMAT_RGBX5551:
246      case GBM_FORMAT_BGRX5551:
247      case GBM_FORMAT_ARGB1555:
248      case GBM_FORMAT_ABGR1555:
249      case GBM_FORMAT_RGBA5551:
250      case GBM_FORMAT_BGRA5551:
251      case GBM_FORMAT_RGB565:
252      case GBM_FORMAT_BGR565:
253         return 16;
254      case GBM_FORMAT_RGB888:
255      case GBM_FORMAT_BGR888:
256         return 24;
257      case GBM_FORMAT_RG1616:
258      case GBM_FORMAT_GR1616:
259      case GBM_FORMAT_XRGB8888:
260      case GBM_FORMAT_XBGR8888:
261      case GBM_FORMAT_RGBX8888:
262      case GBM_FORMAT_BGRX8888:
263      case GBM_FORMAT_ARGB8888:
264      case GBM_FORMAT_ABGR8888:
265      case GBM_FORMAT_RGBA8888:
266      case GBM_FORMAT_BGRA8888:
267      case GBM_FORMAT_XRGB2101010:
268      case GBM_FORMAT_XBGR2101010:
269      case GBM_FORMAT_RGBX1010102:
270      case GBM_FORMAT_BGRX1010102:
271      case GBM_FORMAT_ARGB2101010:
272      case GBM_FORMAT_ABGR2101010:
273      case GBM_FORMAT_RGBA1010102:
274      case GBM_FORMAT_BGRA1010102:
275         return 32;
276      case GBM_FORMAT_XBGR16161616:
277      case GBM_FORMAT_ABGR16161616:
278      case GBM_FORMAT_XBGR16161616F:
279      case GBM_FORMAT_ABGR16161616F:
280         return 64;
281   }
282}
283
284/** Get the offset for the data of the specified plane
285 *
286 * Extra planes, and even the first plane, may have an offset from the start of
287 * the buffer object. This function will provide the offset for the given plane
288 * to be used in various KMS APIs.
289 *
290 * \param bo The buffer object
291 * \return The offset
292 */
293GBM_EXPORT uint32_t
294gbm_bo_get_offset(struct gbm_bo *bo, int plane)
295{
296   return bo->gbm->v0.bo_get_offset(bo, plane);
297}
298
299/** Get the gbm device used to create the buffer object
300 *
301 * \param bo The buffer object
302 * \return Returns the gbm device with which the buffer object was created
303 */
304GBM_EXPORT struct gbm_device *
305gbm_bo_get_device(struct gbm_bo *bo)
306{
307	return bo->gbm;
308}
309
310/** Get the handle of the buffer object
311 *
312 * This is stored in the platform generic union gbm_bo_handle type. However
313 * the format of this handle is platform specific.
314 *
315 * \param bo The buffer object
316 * \return Returns the handle of the allocated buffer object
317 */
318GBM_EXPORT union gbm_bo_handle
319gbm_bo_get_handle(struct gbm_bo *bo)
320{
321   return bo->v0.handle;
322}
323
324/** Get a DMA-BUF file descriptor for the buffer object
325 *
326 * This function creates a DMA-BUF (also known as PRIME) file descriptor
327 * handle for the buffer object.  Each call to gbm_bo_get_fd() returns a new
328 * file descriptor and the caller is responsible for closing the file
329 * descriptor.
330
331 * \param bo The buffer object
332 * \return Returns a file descriptor referring to the underlying buffer or -1
333 * if an error occurs.
334 */
335GBM_EXPORT int
336gbm_bo_get_fd(struct gbm_bo *bo)
337{
338   return bo->gbm->v0.bo_get_fd(bo);
339}
340
341/** Get the number of planes for the given bo.
342 *
343 * \param bo The buffer object
344 * \return The number of planes
345 */
346GBM_EXPORT int
347gbm_bo_get_plane_count(struct gbm_bo *bo)
348{
349   return bo->gbm->v0.bo_get_planes(bo);
350}
351
352/** Get the handle for the specified plane of the buffer object
353 *
354 * This function gets the handle for any plane associated with the BO. When
355 * dealing with multi-planar formats, or formats which might have implicit
356 * planes based on different underlying hardware it is necessary for the client
357 * to be able to get this information to pass to the DRM.
358 *
359 * \param bo The buffer object
360 * \param plane the plane to get a handle for
361 *
362 * \sa gbm_bo_get_handle()
363 */
364GBM_EXPORT union gbm_bo_handle
365gbm_bo_get_handle_for_plane(struct gbm_bo *bo, int plane)
366{
367   return bo->gbm->v0.bo_get_handle(bo, plane);
368}
369
370/** Get a DMA-BUF file descriptor for the specified plane of the buffer object
371 *
372 * This function creates a DMA-BUF (also known as PRIME) file descriptor
373 * handle for the specified plane of the buffer object.  Each call to
374 * gbm_bo_get_fd_for_plane() returns a new file descriptor and the caller is
375 * responsible for closing the file descriptor.
376
377 * \param bo The buffer object
378 * \param plane The plane to get a DMA-BUF for
379 * \return Returns a file descriptor referring to the underlying buffer or -1
380 * if an error occurs.
381 *
382 * \sa gbm_bo_get_fd()
383 */
384GBM_EXPORT int
385gbm_bo_get_fd_for_plane(struct gbm_bo *bo, int plane)
386{
387   return bo->gbm->v0.bo_get_plane_fd(bo, plane);
388}
389
390/**
391 * Get the chosen modifier for the buffer object
392 *
393 * This function returns the modifier that was chosen for the object. These
394 * properties may be generic, or platform/implementation dependent.
395 *
396 * \param bo The buffer object
397 * \return Returns the selected modifier (chosen by the implementation) for the
398 * BO.
399 * \sa gbm_bo_create_with_modifiers() where possible modifiers are set
400 * \sa gbm_surface_create_with_modifiers() where possible modifiers are set
401 * \sa define DRM_FORMAT_MOD_* in drm_fourcc.h for possible modifiers
402 */
403GBM_EXPORT uint64_t
404gbm_bo_get_modifier(struct gbm_bo *bo)
405{
406   return bo->gbm->v0.bo_get_modifier(bo);
407}
408
409/** Write data into the buffer object
410 *
411 * If the buffer object was created with the GBM_BO_USE_WRITE flag,
412 * this function can be used to write data into the buffer object.  The
413 * data is copied directly into the object and it's the responsibility
414 * of the caller to make sure the data represents valid pixel data,
415 * according to the width, height, stride and format of the buffer object.
416 *
417 * \param bo The buffer object
418 * \param buf The data to write
419 * \param count The number of bytes to write
420 * \return Returns 0 on success, otherwise -1 is returned an errno set
421 */
422GBM_EXPORT int
423gbm_bo_write(struct gbm_bo *bo, const void *buf, size_t count)
424{
425   return bo->gbm->v0.bo_write(bo, buf, count);
426}
427
428/** Set the user data associated with a buffer object
429 *
430 * \param bo The buffer object
431 * \param data The data to associate to the buffer object
432 * \param destroy_user_data A callback (which may be %NULL) that will be
433 * called prior to the buffer destruction
434 */
435GBM_EXPORT void
436gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
437		     void (*destroy_user_data)(struct gbm_bo *, void *))
438{
439   bo->v0.user_data = data;
440   bo->v0.destroy_user_data = destroy_user_data;
441}
442
443/** Get the user data associated with a buffer object
444 *
445 * \param bo The buffer object
446 * \return Returns the user data associated with the buffer object or %NULL
447 * if no data was associated with it
448 *
449 * \sa gbm_bo_set_user_data()
450 */
451GBM_EXPORT void *
452gbm_bo_get_user_data(struct gbm_bo *bo)
453{
454   return bo->v0.user_data;
455}
456
457/**
458 * Destroys the given buffer object and frees all resources associated with
459 * it.
460 *
461 * \param bo The buffer object
462 */
463GBM_EXPORT void
464gbm_bo_destroy(struct gbm_bo *bo)
465{
466   if (bo->v0.destroy_user_data)
467      bo->v0.destroy_user_data(bo, bo->v0.user_data);
468
469   bo->gbm->v0.bo_destroy(bo);
470}
471
472/**
473 * Allocate a buffer object for the given dimensions
474 *
475 * \param gbm The gbm device returned from gbm_create_device()
476 * \param width The width for the buffer
477 * \param height The height for the buffer
478 * \param format The format to use for the buffer, from GBM_FORMAT_* or
479 * GBM_BO_FORMAT_* tokens
480 * \param flags The union of the usage flags for this buffer
481 *
482 * \return A newly allocated buffer that should be freed with gbm_bo_destroy()
483 * when no longer needed. If an error occurs during allocation %NULL will be
484 * returned and errno set.
485 *
486 * \sa enum gbm_bo_flags for the list of usage flags
487 */
488GBM_EXPORT struct gbm_bo *
489gbm_bo_create(struct gbm_device *gbm,
490              uint32_t width, uint32_t height,
491              uint32_t format, uint32_t flags)
492{
493   if (width == 0 || height == 0) {
494      errno = EINVAL;
495      return NULL;
496   }
497
498   return gbm->v0.bo_create(gbm, width, height, format, flags, NULL, 0);
499}
500
501GBM_EXPORT struct gbm_bo *
502gbm_bo_create_with_modifiers(struct gbm_device *gbm,
503                             uint32_t width, uint32_t height,
504                             uint32_t format,
505                             const uint64_t *modifiers,
506                             const unsigned int count)
507{
508   uint32_t flags = 0;
509
510   /*
511    * ABI version 1 added the modifiers+flags capability. Backends from
512    * prior versions may fail if "unknown" flags are provided along with
513    * modifiers, but assume scanout is required when modifiers are used.
514    * Newer backends expect scanout to be explicitly requested if required,
515    * but applications using this older interface rely on the older implied
516    * requirement, so that behavior must be preserved.
517    */
518   if (gbm->v0.backend_version >= 1) {
519      flags |= GBM_BO_USE_SCANOUT;
520   }
521
522   return gbm_bo_create_with_modifiers2(gbm, width, height, format, modifiers,
523                                        count, flags);
524}
525
526GBM_EXPORT struct gbm_bo *
527gbm_bo_create_with_modifiers2(struct gbm_device *gbm,
528                              uint32_t width, uint32_t height,
529                              uint32_t format,
530                              const uint64_t *modifiers,
531                              const unsigned int count,
532                              uint32_t flags)
533{
534   if (width == 0 || height == 0) {
535      errno = EINVAL;
536      return NULL;
537   }
538
539   if ((count && !modifiers) || (modifiers && !count)) {
540      errno = EINVAL;
541      return NULL;
542   }
543
544   if (modifiers && (flags & GBM_BO_USE_LINEAR)) {
545      errno = EINVAL;
546      return NULL;
547   }
548
549   return gbm->v0.bo_create(gbm, width, height, format, flags, modifiers, count);
550}
551
552/**
553 * Create a gbm buffer object from a foreign object
554 *
555 * This function imports a foreign object and creates a new gbm bo for it.
556 * This enables using the foreign object with a display API such as KMS.
557 * Currently these types of foreign objects are supported, indicated by the type
558 * argument:
559 *
560 *   GBM_BO_IMPORT_WL_BUFFER
561 *   GBM_BO_IMPORT_EGL_IMAGE
562 *   GBM_BO_IMPORT_FD
563 *   GBM_BO_IMPORT_FD_MODIFIER
564 *
565 * The gbm bo shares the underlying pixels but its life-time is
566 * independent of the foreign object.
567 *
568 * \param gbm The gbm device returned from gbm_create_device()
569 * \param type The type of object we're importing
570 * \param buffer Pointer to the external object
571 * \param flags The union of the usage flags for this buffer
572 *
573 * \return A newly allocated buffer object that should be freed with
574 * gbm_bo_destroy() when no longer needed. On error, %NULL is returned
575 * and errno is set.
576 *
577 * \sa enum gbm_bo_flags for the list of usage flags
578 */
579GBM_EXPORT struct gbm_bo *
580gbm_bo_import(struct gbm_device *gbm,
581              uint32_t type, void *buffer, uint32_t flags)
582{
583   return gbm->v0.bo_import(gbm, type, buffer, flags);
584}
585
586/**
587 * Map a region of a gbm buffer object for cpu access
588 *
589 * This function maps a region of a gbm bo for cpu read and/or write
590 * access.
591 *
592 * The mapping exposes a linear view of the buffer object even if the buffer
593 * has a non-linear modifier.
594 *
595 * This function may require intermediate buffer copies (ie. it may be slow).
596 *
597 * \param bo The buffer object
598 * \param x The X (top left origin) starting position of the mapped region for
599 * the buffer
600 * \param y The Y (top left origin) starting position of the mapped region for
601 * the buffer
602 * \param width The width of the mapped region for the buffer
603 * \param height The height of the mapped region for the buffer
604 * \param flags The union of the GBM_BO_TRANSFER_* flags for this buffer
605 * \param stride Ptr for returned stride in bytes of the mapped region
606 * \param map_data Returned opaque ptr for the mapped region
607 *
608 * \return Address of the mapped buffer that should be unmapped with
609 * gbm_bo_unmap() when no longer needed. On error, %NULL is returned
610 * and errno is set.
611 *
612 * \sa enum gbm_bo_transfer_flags for the list of flags
613 */
614GBM_EXPORT void *
615gbm_bo_map(struct gbm_bo *bo,
616              uint32_t x, uint32_t y,
617              uint32_t width, uint32_t height,
618              uint32_t flags, uint32_t *stride, void **map_data)
619{
620   if (!bo || width == 0 || height == 0 || !stride || !map_data) {
621      errno = EINVAL;
622      return NULL;
623   }
624
625   return bo->gbm->v0.bo_map(bo, x, y, width, height,
626                             flags, stride, map_data);
627}
628
629/**
630 * Unmap a previously mapped region of a gbm buffer object
631 *
632 * This function unmaps a region of a gbm bo for cpu read and/or write
633 * access.
634 *
635 * \param bo The buffer object
636 * \param map_data opaque ptr returned from prior gbm_bo_map
637 */
638GBM_EXPORT void
639gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
640{
641   bo->gbm->v0.bo_unmap(bo, map_data);
642}
643
644/**
645 * Allocate a surface object
646 *
647 * \param gbm The gbm device returned from gbm_create_device()
648 * \param width The width for the surface
649 * \param height The height for the surface
650 * \param format The format to use for the surface
651 *
652 * \return A newly allocated surface that should be freed with
653 * gbm_surface_destroy() when no longer needed. If an error occurs
654 * during allocation %NULL will be returned.
655 *
656 * \sa enum gbm_bo_format for the list of formats
657 */
658GBM_EXPORT struct gbm_surface *
659gbm_surface_create(struct gbm_device *gbm,
660                   uint32_t width, uint32_t height,
661		   uint32_t format, uint32_t flags)
662{
663   return gbm->v0.surface_create(gbm, width, height, format, flags, NULL, 0);
664}
665
666GBM_EXPORT struct gbm_surface *
667gbm_surface_create_with_modifiers(struct gbm_device *gbm,
668                                  uint32_t width, uint32_t height,
669                                  uint32_t format,
670                                  const uint64_t *modifiers,
671                                  const unsigned int count)
672{
673   uint32_t flags = 0;
674
675   /*
676    * ABI version 1 added the modifiers+flags capability. Backends from
677    * prior versions may fail if "unknown" flags are provided along with
678    * modifiers, but assume scanout is required when modifiers are used.
679    * Newer backends expect scanout to be explicitly requested if required,
680    * but applications using this older interface rely on the older implied
681    * requirement, so that behavior must be preserved.
682    */
683   if (gbm->v0.backend_version >= 1) {
684      flags |= GBM_BO_USE_SCANOUT;
685   }
686
687   return gbm_surface_create_with_modifiers2(gbm, width, height, format,
688                                             modifiers, count,
689                                             flags);
690}
691
692GBM_EXPORT struct gbm_surface *
693gbm_surface_create_with_modifiers2(struct gbm_device *gbm,
694                                   uint32_t width, uint32_t height,
695                                   uint32_t format,
696                                   const uint64_t *modifiers,
697                                   const unsigned int count,
698                                   uint32_t flags)
699{
700   if ((count && !modifiers) || (modifiers && !count)) {
701      errno = EINVAL;
702      return NULL;
703   }
704
705   if (modifiers && (flags & GBM_BO_USE_LINEAR)) {
706      errno = EINVAL;
707      return NULL;
708   }
709
710   return gbm->v0.surface_create(gbm, width, height, format, flags,
711                                 modifiers, count);
712}
713
714/**
715 * Destroys the given surface and frees all resources associated with it.
716 *
717 * Prior to calling this function all buffers locked with
718 * gbm_surface_lock_front_buffer() need to be released and the associated
719 * EGL surface destroyed.
720 *
721 * \param surf The surface
722 */
723GBM_EXPORT void
724gbm_surface_destroy(struct gbm_surface *surf)
725{
726   surf->gbm->v0.surface_destroy(surf);
727}
728
729/**
730 * Lock the surface's current front buffer
731 *
732 * Lock rendering to the surface's current front buffer until it is
733 * released with gbm_surface_release_buffer().
734 *
735 * This function must be called exactly once after calling
736 * eglSwapBuffers.  Calling it before any eglSwapBuffer has happened
737 * on the surface or two or more times after eglSwapBuffers is an error.
738 *
739 * \param surf The surface
740 *
741 * \return A buffer object representing the front buffer that should be
742 * released with gbm_surface_release_buffer() when no longer needed and before
743 * the associated EGL surface gets destroyed. The implementation is free to
744 * reuse buffers released with gbm_surface_release_buffer() so this bo should
745 * not be destroyed using gbm_bo_destroy(). If an error occurs this function
746 * returns %NULL.
747 */
748GBM_EXPORT struct gbm_bo *
749gbm_surface_lock_front_buffer(struct gbm_surface *surf)
750{
751   return surf->gbm->v0.surface_lock_front_buffer(surf);
752}
753
754/**
755 * Release a locked buffer obtained with gbm_surface_lock_front_buffer()
756 *
757 * Returns the underlying buffer to the gbm surface.  Releasing a bo
758 * will typically make gbm_surface_has_free_buffer() return 1 and thus
759 * allow rendering the next frame, but not always. The implementation
760 * may choose to destroy the bo immediately or reuse it, in which case
761 * the user data associated with it is unchanged.
762 *
763 * \param surf The surface
764 * \param bo The buffer object
765 */
766GBM_EXPORT void
767gbm_surface_release_buffer(struct gbm_surface *surf, struct gbm_bo *bo)
768{
769   surf->gbm->v0.surface_release_buffer(surf, bo);
770}
771
772/**
773 * Return whether or not a surface has free (non-locked) buffers
774 *
775 * Before starting a new frame, the surface must have a buffer
776 * available for rendering.  Initially, a gbm surface will have a free
777 * buffer, but after one or more buffers have been locked (\sa
778 * gbm_surface_lock_front_buffer()), the application must check for a
779 * free buffer before rendering.
780 *
781 * If a surface doesn't have a free buffer, the application must
782 * return a buffer to the surface using gbm_surface_release_buffer()
783 * and after that, the application can query for free buffers again.
784 *
785 * \param surf The surface
786 * \return 1 if the surface has free buffers, 0 otherwise
787 */
788GBM_EXPORT int
789gbm_surface_has_free_buffers(struct gbm_surface *surf)
790{
791   return surf->gbm->v0.surface_has_free_buffers(surf);
792}
793
794/* The two GBM_BO_FORMAT_[XA]RGB8888 formats alias the GBM_FORMAT_*
795 * formats of the same name. We want to accept them whenever someone
796 * has a GBM format, but never return them to the user. */
797static uint32_t
798format_canonicalize(uint32_t gbm_format)
799{
800   switch (gbm_format) {
801   case GBM_BO_FORMAT_XRGB8888:
802      return GBM_FORMAT_XRGB8888;
803   case GBM_BO_FORMAT_ARGB8888:
804      return GBM_FORMAT_ARGB8888;
805   default:
806      return gbm_format;
807   }
808}
809
810/**
811 * Returns a string representing the fourcc format name.
812 *
813 * \param desc Caller-provided storage for the format name string.
814 * \return String containing the fourcc of the format.
815 */
816GBM_EXPORT char *
817gbm_format_get_name(uint32_t gbm_format, struct gbm_format_name_desc *desc)
818{
819   gbm_format = format_canonicalize(gbm_format);
820
821   desc->name[0] = gbm_format;
822   desc->name[1] = gbm_format >> 8;
823   desc->name[2] = gbm_format >> 16;
824   desc->name[3] = gbm_format >> 24;
825   desc->name[4] = 0;
826
827   return desc->name;
828}
829
830/**
831 * A global table of functions and global variables defined in the core GBM
832 * code that need to be accessed directly by GBM backends.
833 */
834struct gbm_core gbm_core = {
835   .v0.core_version = GBM_BACKEND_ABI_VERSION,
836   .v0.format_canonicalize = format_canonicalize,
837};
838