ceeed5082e9c08bdda52028ca80093713f74a4e9
[mesa.git] / src / gbm / main / gbm.c
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
53 gbm_device_get_fd(struct gbm_device *gbm)
54 {
55 return gbm->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 *
64 gbm_device_get_backend_name(struct gbm_device *gbm)
65 {
66 return gbm->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 usage 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
82 gbm_device_is_format_supported(struct gbm_device *gbm,
83 uint32_t format, uint32_t usage)
84 {
85 return gbm->is_format_supported(gbm, format, usage);
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
95 gbm_device_get_format_modifier_plane_count(struct gbm_device *gbm,
96 uint32_t format,
97 uint64_t modifier)
98 {
99 return gbm->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
107 gbm_device_destroy(struct gbm_device *gbm)
108 {
109 gbm->refcount--;
110 if (gbm->refcount == 0)
111 gbm->destroy(gbm);
112 }
113
114 /** Create a gbm device for allocating buffers
115 *
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
119 * /dev/dri/card0
120 *
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.
125 */
126 GBM_EXPORT struct gbm_device *
127 gbm_create_device(int fd)
128 {
129 struct gbm_device *gbm = NULL;
130 struct stat buf;
131
132 if (fd < 0 || fstat(fd, &buf) < 0 || !S_ISCHR(buf.st_mode)) {
133 errno = EINVAL;
134 return NULL;
135 }
136
137 gbm = _gbm_create_device(fd);
138 if (gbm == NULL)
139 return NULL;
140
141 gbm->dummy = gbm_create_device;
142 gbm->stat = buf;
143 gbm->refcount = 1;
144
145 return gbm;
146 }
147
148 /** Get the width of the buffer object
149 *
150 * \param bo The buffer object
151 * \return The width of the allocated buffer object
152 *
153 */
154 GBM_EXPORT uint32_t
155 gbm_bo_get_width(struct gbm_bo *bo)
156 {
157 return bo->width;
158 }
159
160 /** Get the height of the buffer object
161 *
162 * \param bo The buffer object
163 * \return The height of the allocated buffer object
164 */
165 GBM_EXPORT uint32_t
166 gbm_bo_get_height(struct gbm_bo *bo)
167 {
168 return bo->height;
169 }
170
171 /** Get the stride of the buffer object
172 *
173 * This is calculated by the backend when it does the allocation in
174 * gbm_bo_create()
175 *
176 * \param bo The buffer object
177 * \return The stride of the allocated buffer object in bytes
178 */
179 GBM_EXPORT uint32_t
180 gbm_bo_get_stride(struct gbm_bo *bo)
181 {
182 return gbm_bo_get_stride_for_plane(bo, 0);
183 }
184
185 /** Get the stride for the given plane
186 *
187 * \param bo The buffer object
188 * \param plane for which you want the stride
189 *
190 * \sa gbm_bo_get_stride()
191 */
192 GBM_EXPORT uint32_t
193 gbm_bo_get_stride_for_plane(struct gbm_bo *bo, int plane)
194 {
195 return bo->gbm->bo_get_stride(bo, plane);
196 }
197
198 /** Get the format of the buffer object
199 *
200 * The format of the pixels in the buffer.
201 *
202 * \param bo The buffer object
203 * \return The format of buffer object, one of the GBM_FORMAT_* codes
204 */
205 GBM_EXPORT uint32_t
206 gbm_bo_get_format(struct gbm_bo *bo)
207 {
208 return bo->format;
209 }
210
211 /** Get the bit-per-pixel of the buffer object's format
212 *
213 * The bits-per-pixel of the buffer object's format.
214 *
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.
219 *
220 * \param bo The buffer object
221 * \return The number of bits0per-pixel of the buffer object's format.
222 */
223 GBM_EXPORT uint32_t
224 gbm_bo_get_bpp(struct gbm_bo *bo)
225 {
226 switch (bo->format) {
227 default:
228 return 0;
229 case GBM_FORMAT_C8:
230 case GBM_FORMAT_R8:
231 case GBM_FORMAT_RGB332:
232 case GBM_FORMAT_BGR233:
233 return 8;
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_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:
273 return 32;
274 case GBM_FORMAT_XBGR16161616F:
275 case GBM_FORMAT_ABGR16161616F:
276 return 64;
277 }
278 }
279
280 /** Get the offset for the data of the specified plane
281 *
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.
285 *
286 * \param bo The buffer object
287 * \return The offset
288 */
289 GBM_EXPORT uint32_t
290 gbm_bo_get_offset(struct gbm_bo *bo, int plane)
291 {
292 return bo->gbm->bo_get_offset(bo, plane);
293 }
294
295 /** Get the gbm device used to create the buffer object
296 *
297 * \param bo The buffer object
298 * \return Returns the gbm device with which the buffer object was created
299 */
300 GBM_EXPORT struct gbm_device *
301 gbm_bo_get_device(struct gbm_bo *bo)
302 {
303 return bo->gbm;
304 }
305
306 /** Get the handle of the buffer object
307 *
308 * This is stored in the platform generic union gbm_bo_handle type. However
309 * the format of this handle is platform specific.
310 *
311 * \param bo The buffer object
312 * \return Returns the handle of the allocated buffer object
313 */
314 GBM_EXPORT union gbm_bo_handle
315 gbm_bo_get_handle(struct gbm_bo *bo)
316 {
317 return bo->handle;
318 }
319
320 /** Get a DMA-BUF file descriptor for the buffer object
321 *
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
325 * descriptor.
326
327 * \param bo The buffer object
328 * \return Returns a file descriptor referring to the underlying buffer or -1
329 * if an error occurs.
330 */
331 GBM_EXPORT int
332 gbm_bo_get_fd(struct gbm_bo *bo)
333 {
334 return bo->gbm->bo_get_fd(bo);
335 }
336
337 /** Get the number of planes for the given bo.
338 *
339 * \param bo The buffer object
340 * \return The number of planes
341 */
342 GBM_EXPORT int
343 gbm_bo_get_plane_count(struct gbm_bo *bo)
344 {
345 return bo->gbm->bo_get_planes(bo);
346 }
347
348 /** Get the handle for the specified plane of the buffer object
349 *
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.
354 *
355 * \param bo The buffer object
356 * \param plane the plane to get a handle for
357 *
358 * \sa gbm_bo_get_handle()
359 */
360 GBM_EXPORT union gbm_bo_handle
361 gbm_bo_get_handle_for_plane(struct gbm_bo *bo, int plane)
362 {
363 return bo->gbm->bo_get_handle(bo, plane);
364 }
365
366 /**
367 * Get the chosen modifier for the buffer object
368 *
369 * This function returns the modifier that was chosen for the object. These
370 * properties may be generic, or platform/implementation dependent.
371 *
372 * \param bo The buffer object
373 * \return Returns the selected modifier (chosen by the implementation) for the
374 * BO.
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
378 */
379 GBM_EXPORT uint64_t
380 gbm_bo_get_modifier(struct gbm_bo *bo)
381 {
382 return bo->gbm->bo_get_modifier(bo);
383 }
384
385 /** Write data into the buffer object
386 *
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.
392 *
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
397 */
398 GBM_EXPORT int
399 gbm_bo_write(struct gbm_bo *bo, const void *buf, size_t count)
400 {
401 return bo->gbm->bo_write(bo, buf, count);
402 }
403
404 /** Set the user data associated with a buffer object
405 *
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
410 */
411 GBM_EXPORT void
412 gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
413 void (*destroy_user_data)(struct gbm_bo *, void *))
414 {
415 bo->user_data = data;
416 bo->destroy_user_data = destroy_user_data;
417 }
418
419 /** Get the user data associated with a buffer object
420 *
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
424 *
425 * \sa gbm_bo_set_user_data()
426 */
427 GBM_EXPORT void *
428 gbm_bo_get_user_data(struct gbm_bo *bo)
429 {
430 return bo->user_data;
431 }
432
433 /**
434 * Destroys the given buffer object and frees all resources associated with
435 * it.
436 *
437 * \param bo The buffer object
438 */
439 GBM_EXPORT void
440 gbm_bo_destroy(struct gbm_bo *bo)
441 {
442 if (bo->destroy_user_data)
443 bo->destroy_user_data(bo, bo->user_data);
444
445 bo->gbm->bo_destroy(bo);
446 }
447
448 /**
449 * Allocate a buffer object for the given dimensions
450 *
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
457 *
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.
461 *
462 * \sa enum gbm_bo_flags for the list of usage flags
463 */
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)
468 {
469 if (width == 0 || height == 0) {
470 errno = EINVAL;
471 return NULL;
472 }
473
474 return gbm->bo_create(gbm, width, height, format, usage, NULL, 0);
475 }
476
477 GBM_EXPORT struct gbm_bo *
478 gbm_bo_create_with_modifiers(struct gbm_device *gbm,
479 uint32_t width, uint32_t height,
480 uint32_t format,
481 const uint64_t *modifiers,
482 const unsigned int count)
483 {
484 if (width == 0 || height == 0) {
485 errno = EINVAL;
486 return NULL;
487 }
488
489 if ((count && !modifiers) || (modifiers && !count)) {
490 errno = EINVAL;
491 return NULL;
492 }
493
494 return gbm->bo_create(gbm, width, height, format, 0, modifiers, count);
495 }
496
497 /**
498 * Create a gbm buffer object from a foreign object
499 *
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
503 * argument:
504 *
505 * GBM_BO_IMPORT_WL_BUFFER
506 * GBM_BO_IMPORT_EGL_IMAGE
507 * GBM_BO_IMPORT_FD
508 * GBM_BO_IMPORT_FD_MODIFIER
509 *
510 * The gbm bo shares the underlying pixels but its life-time is
511 * independent of the foreign object.
512 *
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
517 *
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
520 * and errno is set.
521 *
522 * \sa enum gbm_bo_flags for the list of usage flags
523 */
524 GBM_EXPORT struct gbm_bo *
525 gbm_bo_import(struct gbm_device *gbm,
526 uint32_t type, void *buffer, uint32_t usage)
527 {
528 return gbm->bo_import(gbm, type, buffer, usage);
529 }
530
531 /**
532 * Map a region of a gbm buffer object for cpu access
533 *
534 * This function maps a region of a gbm bo for cpu read and/or write
535 * access.
536 *
537 * \param bo The buffer object
538 * \param x The X (top left origin) starting position of the mapped region for
539 * the buffer
540 * \param y The Y (top left origin) starting position of the mapped region for
541 * the buffer
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
547 *
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
550 * and errno is set.
551 *
552 * \sa enum gbm_bo_transfer_flags for the list of flags
553 */
554 GBM_EXPORT void *
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)
559 {
560 if (!bo || width == 0 || height == 0 || !stride || !map_data) {
561 errno = EINVAL;
562 return NULL;
563 }
564
565 return bo->gbm->bo_map(bo, x, y, width, height,
566 flags, stride, map_data);
567 }
568
569 /**
570 * Unmap a previously mapped region of a gbm buffer object
571 *
572 * This function unmaps a region of a gbm bo for cpu read and/or write
573 * access.
574 *
575 * \param bo The buffer object
576 * \param map_data opaque ptr returned from prior gbm_bo_map
577 */
578 GBM_EXPORT void
579 gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
580 {
581 bo->gbm->bo_unmap(bo, map_data);
582 }
583
584 /**
585 * Allocate a surface object
586 *
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
591 *
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.
595 *
596 * \sa enum gbm_bo_format for the list of formats
597 */
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)
602 {
603 return gbm->surface_create(gbm, width, height, format, flags, NULL, 0);
604 }
605
606 GBM_EXPORT struct gbm_surface *
607 gbm_surface_create_with_modifiers(struct gbm_device *gbm,
608 uint32_t width, uint32_t height,
609 uint32_t format,
610 const uint64_t *modifiers,
611 const unsigned int count)
612 {
613 if ((count && !modifiers) || (modifiers && !count)) {
614 errno = EINVAL;
615 return NULL;
616 }
617
618 return gbm->surface_create(gbm, width, height, format, 0,
619 modifiers, count);
620 }
621
622 /**
623 * Destroys the given surface and frees all resources associated with
624 * it.
625 *
626 * All buffers locked with gbm_surface_lock_front_buffer() should be
627 * released prior to calling this function.
628 *
629 * \param surf The surface
630 */
631 GBM_EXPORT void
632 gbm_surface_destroy(struct gbm_surface *surf)
633 {
634 surf->gbm->surface_destroy(surf);
635 }
636
637 /**
638 * Lock the surface's current front buffer
639 *
640 * Lock rendering to the surface's current front buffer until it is
641 * released with gbm_surface_release_buffer().
642 *
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.
649 *
650 * \param surf The surface
651 *
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.
657 */
658 GBM_EXPORT struct gbm_bo *
659 gbm_surface_lock_front_buffer(struct gbm_surface *surf)
660 {
661 return surf->gbm->surface_lock_front_buffer(surf);
662 }
663
664 /**
665 * Release a locked buffer obtained with gbm_surface_lock_front_buffer()
666 *
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.
672 *
673 * \param surf The surface
674 * \param bo The buffer object
675 */
676 GBM_EXPORT void
677 gbm_surface_release_buffer(struct gbm_surface *surf, struct gbm_bo *bo)
678 {
679 surf->gbm->surface_release_buffer(surf, bo);
680 }
681
682 /**
683 * Return whether or not a surface has free (non-locked) buffers
684 *
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.
690 *
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.
694 *
695 * \param surf The surface
696 * \return 1 if the surface has free buffers, 0 otherwise
697 */
698 GBM_EXPORT int
699 gbm_surface_has_free_buffers(struct gbm_surface *surf)
700 {
701 return surf->gbm->surface_has_free_buffers(surf);
702 }
703
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. */
707 uint32_t
708 gbm_format_canonicalize(uint32_t gbm_format)
709 {
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;
715 default:
716 return gbm_format;
717 }
718 }
719
720 /**
721 * Returns a string representing the fourcc format name.
722 *
723 * \param desc Caller-provided storage for the format name string.
724 * \return String containing the fourcc of the format.
725 */
726 GBM_EXPORT char *
727 gbm_format_get_name(uint32_t gbm_format, struct gbm_format_name_desc *desc)
728 {
729 gbm_format = gbm_format_canonicalize(gbm_format);
730
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;
735 desc->name[4] = 0;
736
737 return desc->name;
738 }