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