2 * Copyright © 2011 Intel Corporation
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:
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
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.
25 * Benjamin Franzke <benjaminfranzke@googlemail.com>
35 #include <sys/mkdev.h>
37 #ifdef MAJOR_IN_SYSMACROS
38 #include <sys/sysmacros.h>
48 /** Returns the file description for the gbm device
50 * \return The fd that the struct gbm_device was created with
53 gbm_device_get_fd(struct gbm_device
*gbm
)
58 /** Get the backend name for the given gbm device
60 * \return The backend name string - this belongs to the device and must not
63 GBM_EXPORT
const char *
64 gbm_device_get_backend_name(struct gbm_device
*gbm
)
69 /** Test if a format is supported for a given set of usage flags.
71 * \param gbm The created buffer manager
72 * \param format The format to test
73 * \param usage A bitmask of the usages to test the format against
74 * \return 1 if the format is supported otherwise 0
76 * \sa enum gbm_bo_flags for the list of flags that the format can be
79 * \sa enum gbm_bo_format for the list of formats
82 gbm_device_is_format_supported(struct gbm_device
*gbm
,
83 uint32_t format
, uint32_t usage
)
85 return gbm
->is_format_supported(gbm
, format
, usage
);
88 /** Get the number of planes that are required for a given format+modifier
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
95 gbm_device_get_format_modifier_plane_count(struct gbm_device
*gbm
,
99 return gbm
->get_format_modifier_plane_count(gbm
, format
, modifier
);
102 /** Destroy the gbm device and free all resources associated with it.
104 * \param gbm The device created using gbm_create_device()
107 gbm_device_destroy(struct gbm_device
*gbm
)
110 if (gbm
->refcount
== 0)
114 /** Create a gbm device for allocating buffers
116 * The file descriptor passed in is used by the backend to communicate with
117 * platform for allocating the memory. For allocations using DRI this would be
118 * the file descriptor returned when opening a device such as \c
121 * \param fd The file descriptor for a backend specific device
122 * \return The newly created struct gbm_device. The resources associated with
123 * the device should be freed with gbm_device_destroy() when it is no longer
124 * needed. If the creation of the device failed NULL will be returned.
126 GBM_EXPORT
struct gbm_device
*
127 gbm_create_device(int fd
)
129 struct gbm_device
*gbm
= NULL
;
132 if (fd
< 0 || fstat(fd
, &buf
) < 0 || !S_ISCHR(buf
.st_mode
)) {
137 gbm
= _gbm_create_device(fd
);
141 gbm
->dummy
= gbm_create_device
;
148 /** Get the width of the buffer object
150 * \param bo The buffer object
151 * \return The width of the allocated buffer object
155 gbm_bo_get_width(struct gbm_bo
*bo
)
160 /** Get the height of the buffer object
162 * \param bo The buffer object
163 * \return The height of the allocated buffer object
166 gbm_bo_get_height(struct gbm_bo
*bo
)
171 /** Get the stride of the buffer object
173 * This is calculated by the backend when it does the allocation in
176 * \param bo The buffer object
177 * \return The stride of the allocated buffer object in bytes
180 gbm_bo_get_stride(struct gbm_bo
*bo
)
182 return gbm_bo_get_stride_for_plane(bo
, 0);
185 /** Get the stride for the given plane
187 * \param bo The buffer object
188 * \param plane for which you want the stride
190 * \sa gbm_bo_get_stride()
193 gbm_bo_get_stride_for_plane(struct gbm_bo
*bo
, int plane
)
195 return bo
->gbm
->bo_get_stride(bo
, plane
);
198 /** Get the format of the buffer object
200 * The format of the pixels in the buffer.
202 * \param bo The buffer object
203 * \return The format of buffer object, one of the GBM_FORMAT_* codes
206 gbm_bo_get_format(struct gbm_bo
*bo
)
211 /** Get the bit-per-pixel of the buffer object's format
213 * The bits-per-pixel of the buffer object's format.
215 * Note; The 'in-memory pixel' concept makes no sense for YUV formats
216 * (pixels are the result of the combination of multiple memory sources:
217 * Y, Cb & Cr; usually these are even in separate buffers), so YUV
218 * formats are not supported by this function.
220 * \param bo The buffer object
221 * \return The number of bits0per-pixel of the buffer object's format.
224 gbm_bo_get_bpp(struct gbm_bo
*bo
)
226 switch (bo
->format
) {
231 case GBM_FORMAT_RGB332
:
232 case GBM_FORMAT_BGR233
:
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
:
254 case GBM_FORMAT_RGB888
:
255 case GBM_FORMAT_BGR888
:
257 case GBM_FORMAT_XRGB8888
:
258 case GBM_FORMAT_XBGR8888
:
259 case GBM_FORMAT_RGBX8888
:
260 case GBM_FORMAT_BGRX8888
:
261 case GBM_FORMAT_ARGB8888
:
262 case GBM_FORMAT_ABGR8888
:
263 case GBM_FORMAT_RGBA8888
:
264 case GBM_FORMAT_BGRA8888
:
265 case GBM_FORMAT_XRGB2101010
:
266 case GBM_FORMAT_XBGR2101010
:
267 case GBM_FORMAT_RGBX1010102
:
268 case GBM_FORMAT_BGRX1010102
:
269 case GBM_FORMAT_ARGB2101010
:
270 case GBM_FORMAT_ABGR2101010
:
271 case GBM_FORMAT_RGBA1010102
:
272 case GBM_FORMAT_BGRA1010102
:
274 case GBM_FORMAT_XBGR16161616F
:
275 case GBM_FORMAT_ABGR16161616F
:
280 /** Get the offset for the data of the specified plane
282 * Extra planes, and even the first plane, may have an offset from the start of
283 * the buffer object. This function will provide the offset for the given plane
284 * to be used in various KMS APIs.
286 * \param bo The buffer object
290 gbm_bo_get_offset(struct gbm_bo
*bo
, int plane
)
292 return bo
->gbm
->bo_get_offset(bo
, plane
);
295 /** Get the gbm device used to create the buffer object
297 * \param bo The buffer object
298 * \return Returns the gbm device with which the buffer object was created
300 GBM_EXPORT
struct gbm_device
*
301 gbm_bo_get_device(struct gbm_bo
*bo
)
306 /** Get the handle of the buffer object
308 * This is stored in the platform generic union gbm_bo_handle type. However
309 * the format of this handle is platform specific.
311 * \param bo The buffer object
312 * \return Returns the handle of the allocated buffer object
314 GBM_EXPORT
union gbm_bo_handle
315 gbm_bo_get_handle(struct gbm_bo
*bo
)
320 /** Get a DMA-BUF file descriptor for the buffer object
322 * This function creates a DMA-BUF (also known as PRIME) file descriptor
323 * handle for the buffer object. Each call to gbm_bo_get_fd() returns a new
324 * file descriptor and the caller is responsible for closing the file
327 * \param bo The buffer object
328 * \return Returns a file descriptor referring to the underlying buffer or -1
329 * if an error occurs.
332 gbm_bo_get_fd(struct gbm_bo
*bo
)
334 return bo
->gbm
->bo_get_fd(bo
);
337 /** Get the number of planes for the given bo.
339 * \param bo The buffer object
340 * \return The number of planes
343 gbm_bo_get_plane_count(struct gbm_bo
*bo
)
345 return bo
->gbm
->bo_get_planes(bo
);
348 /** Get the handle for the specified plane of the buffer object
350 * This function gets the handle for any plane associated with the BO. When
351 * dealing with multi-planar formats, or formats which might have implicit
352 * planes based on different underlying hardware it is necessary for the client
353 * to be able to get this information to pass to the DRM.
355 * \param bo The buffer object
356 * \param plane the plane to get a handle for
358 * \sa gbm_bo_get_handle()
360 GBM_EXPORT
union gbm_bo_handle
361 gbm_bo_get_handle_for_plane(struct gbm_bo
*bo
, int plane
)
363 return bo
->gbm
->bo_get_handle(bo
, plane
);
367 * Get the chosen modifier for the buffer object
369 * This function returns the modifier that was chosen for the object. These
370 * properties may be generic, or platform/implementation dependent.
372 * \param bo The buffer object
373 * \return Returns the selected modifier (chosen by the implementation) for the
375 * \sa gbm_bo_create_with_modifiers() where possible modifiers are set
376 * \sa gbm_surface_create_with_modifiers() where possible modifiers are set
377 * \sa define DRM_FORMAT_MOD_* in drm_fourcc.h for possible modifiers
380 gbm_bo_get_modifier(struct gbm_bo
*bo
)
382 return bo
->gbm
->bo_get_modifier(bo
);
385 /** Write data into the buffer object
387 * If the buffer object was created with the GBM_BO_USE_WRITE flag,
388 * this function can be used to write data into the buffer object. The
389 * data is copied directly into the object and it's the responsibility
390 * of the caller to make sure the data represents valid pixel data,
391 * according to the width, height, stride and format of the buffer object.
393 * \param bo The buffer object
394 * \param buf The data to write
395 * \param count The number of bytes to write
396 * \return Returns 0 on success, otherwise -1 is returned an errno set
399 gbm_bo_write(struct gbm_bo
*bo
, const void *buf
, size_t count
)
401 return bo
->gbm
->bo_write(bo
, buf
, count
);
404 /** Set the user data associated with a buffer object
406 * \param bo The buffer object
407 * \param data The data to associate to the buffer object
408 * \param destroy_user_data A callback (which may be %NULL) that will be
409 * called prior to the buffer destruction
412 gbm_bo_set_user_data(struct gbm_bo
*bo
, void *data
,
413 void (*destroy_user_data
)(struct gbm_bo
*, void *))
415 bo
->user_data
= data
;
416 bo
->destroy_user_data
= destroy_user_data
;
419 /** Get the user data associated with a buffer object
421 * \param bo The buffer object
422 * \return Returns the user data associated with the buffer object or %NULL
423 * if no data was associated with it
425 * \sa gbm_bo_set_user_data()
428 gbm_bo_get_user_data(struct gbm_bo
*bo
)
430 return bo
->user_data
;
434 * Destroys the given buffer object and frees all resources associated with
437 * \param bo The buffer object
440 gbm_bo_destroy(struct gbm_bo
*bo
)
442 if (bo
->destroy_user_data
)
443 bo
->destroy_user_data(bo
, bo
->user_data
);
445 bo
->gbm
->bo_destroy(bo
);
449 * Allocate a buffer object for the given dimensions
451 * \param gbm The gbm device returned from gbm_create_device()
452 * \param width The width for the buffer
453 * \param height The height for the buffer
454 * \param format The format to use for the buffer, from GBM_FORMAT_* or
455 * GBM_BO_FORMAT_* tokens
456 * \param usage The union of the usage flags for this buffer
458 * \return A newly allocated buffer that should be freed with gbm_bo_destroy()
459 * when no longer needed. If an error occurs during allocation %NULL will be
460 * returned and errno set.
462 * \sa enum gbm_bo_flags for the list of usage flags
464 GBM_EXPORT
struct gbm_bo
*
465 gbm_bo_create(struct gbm_device
*gbm
,
466 uint32_t width
, uint32_t height
,
467 uint32_t format
, uint32_t usage
)
469 if (width
== 0 || height
== 0) {
474 return gbm
->bo_create(gbm
, width
, height
, format
, usage
, NULL
, 0);
477 GBM_EXPORT
struct gbm_bo
*
478 gbm_bo_create_with_modifiers(struct gbm_device
*gbm
,
479 uint32_t width
, uint32_t height
,
481 const uint64_t *modifiers
,
482 const unsigned int count
)
484 if (width
== 0 || height
== 0) {
489 if ((count
&& !modifiers
) || (modifiers
&& !count
)) {
494 return gbm
->bo_create(gbm
, width
, height
, format
, 0, modifiers
, count
);
498 * Create a gbm buffer object from a foreign object
500 * This function imports a foreign object and creates a new gbm bo for it.
501 * This enables using the foreign object with a display API such as KMS.
502 * Currently these types of foreign objects are supported, indicated by the type
505 * GBM_BO_IMPORT_WL_BUFFER
506 * GBM_BO_IMPORT_EGL_IMAGE
508 * GBM_BO_IMPORT_FD_MODIFIER
510 * The gbm bo shares the underlying pixels but its life-time is
511 * independent of the foreign object.
513 * \param gbm The gbm device returned from gbm_create_device()
514 * \param type The type of object we're importing
515 * \param buffer Pointer to the external object
516 * \param usage The union of the usage flags for this buffer
518 * \return A newly allocated buffer object that should be freed with
519 * gbm_bo_destroy() when no longer needed. On error, %NULL is returned
522 * \sa enum gbm_bo_flags for the list of usage flags
524 GBM_EXPORT
struct gbm_bo
*
525 gbm_bo_import(struct gbm_device
*gbm
,
526 uint32_t type
, void *buffer
, uint32_t usage
)
528 return gbm
->bo_import(gbm
, type
, buffer
, usage
);
532 * Map a region of a gbm buffer object for cpu access
534 * This function maps a region of a gbm bo for cpu read and/or write
537 * \param bo The buffer object
538 * \param x The X (top left origin) starting position of the mapped region for
540 * \param y The Y (top left origin) starting position of the mapped region for
542 * \param width The width of the mapped region for the buffer
543 * \param height The height of the mapped region for the buffer
544 * \param flags The union of the GBM_BO_TRANSFER_* flags for this buffer
545 * \param stride Ptr for returned stride in bytes of the mapped region
546 * \param map_data Returned opaque ptr for the mapped region
548 * \return Address of the mapped buffer that should be unmapped with
549 * gbm_bo_unmap() when no longer needed. On error, %NULL is returned
552 * \sa enum gbm_bo_transfer_flags for the list of flags
555 gbm_bo_map(struct gbm_bo
*bo
,
556 uint32_t x
, uint32_t y
,
557 uint32_t width
, uint32_t height
,
558 uint32_t flags
, uint32_t *stride
, void **map_data
)
560 if (!bo
|| width
== 0 || height
== 0 || !stride
|| !map_data
) {
565 return bo
->gbm
->bo_map(bo
, x
, y
, width
, height
,
566 flags
, stride
, map_data
);
570 * Unmap a previously mapped region of a gbm buffer object
572 * This function unmaps a region of a gbm bo for cpu read and/or write
575 * \param bo The buffer object
576 * \param map_data opaque ptr returned from prior gbm_bo_map
579 gbm_bo_unmap(struct gbm_bo
*bo
, void *map_data
)
581 bo
->gbm
->bo_unmap(bo
, map_data
);
585 * Allocate a surface object
587 * \param gbm The gbm device returned from gbm_create_device()
588 * \param width The width for the surface
589 * \param height The height for the surface
590 * \param format The format to use for the surface
592 * \return A newly allocated surface that should be freed with
593 * gbm_surface_destroy() when no longer needed. If an error occurs
594 * during allocation %NULL will be returned.
596 * \sa enum gbm_bo_format for the list of formats
598 GBM_EXPORT
struct gbm_surface
*
599 gbm_surface_create(struct gbm_device
*gbm
,
600 uint32_t width
, uint32_t height
,
601 uint32_t format
, uint32_t flags
)
603 return gbm
->surface_create(gbm
, width
, height
, format
, flags
, NULL
, 0);
606 GBM_EXPORT
struct gbm_surface
*
607 gbm_surface_create_with_modifiers(struct gbm_device
*gbm
,
608 uint32_t width
, uint32_t height
,
610 const uint64_t *modifiers
,
611 const unsigned int count
)
613 if ((count
&& !modifiers
) || (modifiers
&& !count
)) {
618 return gbm
->surface_create(gbm
, width
, height
, format
, 0,
623 * Destroys the given surface and frees all resources associated with
626 * All buffers locked with gbm_surface_lock_front_buffer() should be
627 * released prior to calling this function.
629 * \param surf The surface
632 gbm_surface_destroy(struct gbm_surface
*surf
)
634 surf
->gbm
->surface_destroy(surf
);
638 * Lock the surface's current front buffer
640 * Lock rendering to the surface's current front buffer until it is
641 * released with gbm_surface_release_buffer().
643 * This function must be called exactly once after calling
644 * eglSwapBuffers. Calling it before any eglSwapBuffer has happened
645 * on the surface or two or more times after eglSwapBuffers is an
646 * error. A new bo representing the new front buffer is returned. On
647 * multiple invocations, all the returned bos must be released in
648 * order to release the actual surface buffer.
650 * \param surf The surface
652 * \return A buffer object that should be released with
653 * gbm_surface_release_buffer() when no longer needed. The implementation
654 * is free to reuse buffers released with gbm_surface_release_buffer() so
655 * this bo should not be destroyed using gbm_bo_destroy(). If an error
656 * occurs this function returns %NULL.
658 GBM_EXPORT
struct gbm_bo
*
659 gbm_surface_lock_front_buffer(struct gbm_surface
*surf
)
661 return surf
->gbm
->surface_lock_front_buffer(surf
);
665 * Release a locked buffer obtained with gbm_surface_lock_front_buffer()
667 * Returns the underlying buffer to the gbm surface. Releasing a bo
668 * will typically make gbm_surface_has_free_buffer() return 1 and thus
669 * allow rendering the next frame, but not always. The implementation
670 * may choose to destroy the bo immediately or reuse it, in which case
671 * the user data associated with it is unchanged.
673 * \param surf The surface
674 * \param bo The buffer object
677 gbm_surface_release_buffer(struct gbm_surface
*surf
, struct gbm_bo
*bo
)
679 surf
->gbm
->surface_release_buffer(surf
, bo
);
683 * Return whether or not a surface has free (non-locked) buffers
685 * Before starting a new frame, the surface must have a buffer
686 * available for rendering. Initially, a gbm surface will have a free
687 * buffer, but after one or more buffers have been locked (\sa
688 * gbm_surface_lock_front_buffer()), the application must check for a
689 * free buffer before rendering.
691 * If a surface doesn't have a free buffer, the application must
692 * return a buffer to the surface using gbm_surface_release_buffer()
693 * and after that, the application can query for free buffers again.
695 * \param surf The surface
696 * \return 1 if the surface has free buffers, 0 otherwise
699 gbm_surface_has_free_buffers(struct gbm_surface
*surf
)
701 return surf
->gbm
->surface_has_free_buffers(surf
);
704 /* The two GBM_BO_FORMAT_[XA]RGB8888 formats alias the GBM_FORMAT_*
705 * formats of the same name. We want to accept them whenever someone
706 * has a GBM format, but never return them to the user. */
708 gbm_format_canonicalize(uint32_t gbm_format
)
710 switch (gbm_format
) {
711 case GBM_BO_FORMAT_XRGB8888
:
712 return GBM_FORMAT_XRGB8888
;
713 case GBM_BO_FORMAT_ARGB8888
:
714 return GBM_FORMAT_ARGB8888
;
721 * Returns a string representing the fourcc format name.
723 * \param desc Caller-provided storage for the format name string.
724 * \return String containing the fourcc of the format.
727 gbm_format_get_name(uint32_t gbm_format
, struct gbm_format_name_desc
*desc
)
729 gbm_format
= gbm_format_canonicalize(gbm_format
);
731 desc
->name
[0] = gbm_format
;
732 desc
->name
[1] = gbm_format
>> 8;
733 desc
->name
[2] = gbm_format
>> 16;
734 desc
->name
[3] = gbm_format
>> 24;