gbm/dri: Expose visuals table through gbm_dri_device
[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 }
275 }
276
277 /** Get the offset for the data of the specified plane
278 *
279 * Extra planes, and even the first plane, may have an offset from the start of
280 * the buffer object. This function will provide the offset for the given plane
281 * to be used in various KMS APIs.
282 *
283 * \param bo The buffer object
284 * \return The offset
285 */
286 GBM_EXPORT uint32_t
287 gbm_bo_get_offset(struct gbm_bo *bo, int plane)
288 {
289 return bo->gbm->bo_get_offset(bo, plane);
290 }
291
292 /** Get the gbm device used to create the buffer object
293 *
294 * \param bo The buffer object
295 * \return Returns the gbm device with which the buffer object was created
296 */
297 GBM_EXPORT struct gbm_device *
298 gbm_bo_get_device(struct gbm_bo *bo)
299 {
300 return bo->gbm;
301 }
302
303 /** Get the handle of the buffer object
304 *
305 * This is stored in the platform generic union gbm_bo_handle type. However
306 * the format of this handle is platform specific.
307 *
308 * \param bo The buffer object
309 * \return Returns the handle of the allocated buffer object
310 */
311 GBM_EXPORT union gbm_bo_handle
312 gbm_bo_get_handle(struct gbm_bo *bo)
313 {
314 return bo->handle;
315 }
316
317 /** Get a DMA-BUF file descriptor for the buffer object
318 *
319 * This function creates a DMA-BUF (also known as PRIME) file descriptor
320 * handle for the buffer object. Each call to gbm_bo_get_fd() returns a new
321 * file descriptor and the caller is responsible for closing the file
322 * descriptor.
323
324 * \param bo The buffer object
325 * \return Returns a file descriptor referring to the underlying buffer or -1
326 * if an error occurs.
327 */
328 GBM_EXPORT int
329 gbm_bo_get_fd(struct gbm_bo *bo)
330 {
331 return bo->gbm->bo_get_fd(bo);
332 }
333
334 /** Get the number of planes for the given bo.
335 *
336 * \param bo The buffer object
337 * \return The number of planes
338 */
339 GBM_EXPORT int
340 gbm_bo_get_plane_count(struct gbm_bo *bo)
341 {
342 return bo->gbm->bo_get_planes(bo);
343 }
344
345 /** Get the handle for the specified plane of the buffer object
346 *
347 * This function gets the handle for any plane associated with the BO. When
348 * dealing with multi-planar formats, or formats which might have implicit
349 * planes based on different underlying hardware it is necessary for the client
350 * to be able to get this information to pass to the DRM.
351 *
352 * \param bo The buffer object
353 * \param plane the plane to get a handle for
354 *
355 * \sa gbm_bo_get_handle()
356 */
357 GBM_EXPORT union gbm_bo_handle
358 gbm_bo_get_handle_for_plane(struct gbm_bo *bo, int plane)
359 {
360 return bo->gbm->bo_get_handle(bo, plane);
361 }
362
363 /**
364 * Get the chosen modifier for the buffer object
365 *
366 * This function returns the modifier that was chosen for the object. These
367 * properties may be generic, or platform/implementation dependent.
368 *
369 * \param bo The buffer object
370 * \return Returns the selected modifier (chosen by the implementation) for the
371 * BO.
372 * \sa gbm_bo_create_with_modifiers() where possible modifiers are set
373 * \sa gbm_surface_create_with_modifiers() where possible modifiers are set
374 * \sa define DRM_FORMAT_MOD_* in drm_fourcc.h for possible modifiers
375 */
376 GBM_EXPORT uint64_t
377 gbm_bo_get_modifier(struct gbm_bo *bo)
378 {
379 return bo->gbm->bo_get_modifier(bo);
380 }
381
382 /** Write data into the buffer object
383 *
384 * If the buffer object was created with the GBM_BO_USE_WRITE flag,
385 * this function can be used to write data into the buffer object. The
386 * data is copied directly into the object and it's the responsibility
387 * of the caller to make sure the data represents valid pixel data,
388 * according to the width, height, stride and format of the buffer object.
389 *
390 * \param bo The buffer object
391 * \param buf The data to write
392 * \param count The number of bytes to write
393 * \return Returns 0 on success, otherwise -1 is returned an errno set
394 */
395 GBM_EXPORT int
396 gbm_bo_write(struct gbm_bo *bo, const void *buf, size_t count)
397 {
398 return bo->gbm->bo_write(bo, buf, count);
399 }
400
401 /** Set the user data associated with a buffer object
402 *
403 * \param bo The buffer object
404 * \param data The data to associate to the buffer object
405 * \param destroy_user_data A callback (which may be %NULL) that will be
406 * called prior to the buffer destruction
407 */
408 GBM_EXPORT void
409 gbm_bo_set_user_data(struct gbm_bo *bo, void *data,
410 void (*destroy_user_data)(struct gbm_bo *, void *))
411 {
412 bo->user_data = data;
413 bo->destroy_user_data = destroy_user_data;
414 }
415
416 /** Get the user data associated with a buffer object
417 *
418 * \param bo The buffer object
419 * \return Returns the user data associated with the buffer object or %NULL
420 * if no data was associated with it
421 *
422 * \sa gbm_bo_set_user_data()
423 */
424 GBM_EXPORT void *
425 gbm_bo_get_user_data(struct gbm_bo *bo)
426 {
427 return bo->user_data;
428 }
429
430 /**
431 * Destroys the given buffer object and frees all resources associated with
432 * it.
433 *
434 * \param bo The buffer object
435 */
436 GBM_EXPORT void
437 gbm_bo_destroy(struct gbm_bo *bo)
438 {
439 if (bo->destroy_user_data)
440 bo->destroy_user_data(bo, bo->user_data);
441
442 bo->gbm->bo_destroy(bo);
443 }
444
445 /**
446 * Allocate a buffer object for the given dimensions
447 *
448 * \param gbm The gbm device returned from gbm_create_device()
449 * \param width The width for the buffer
450 * \param height The height for the buffer
451 * \param format The format to use for the buffer
452 * \param usage The union of the usage flags for this buffer
453 *
454 * \return A newly allocated buffer that should be freed with gbm_bo_destroy()
455 * when no longer needed. If an error occurs during allocation %NULL will be
456 * returned and errno set.
457 *
458 * \sa enum gbm_bo_format for the list of formats
459 * \sa enum gbm_bo_flags for the list of usage flags
460 */
461 GBM_EXPORT struct gbm_bo *
462 gbm_bo_create(struct gbm_device *gbm,
463 uint32_t width, uint32_t height,
464 uint32_t format, uint32_t usage)
465 {
466 if (width == 0 || height == 0) {
467 errno = EINVAL;
468 return NULL;
469 }
470
471 return gbm->bo_create(gbm, width, height, format, usage, NULL, 0);
472 }
473
474 GBM_EXPORT struct gbm_bo *
475 gbm_bo_create_with_modifiers(struct gbm_device *gbm,
476 uint32_t width, uint32_t height,
477 uint32_t format,
478 const uint64_t *modifiers,
479 const unsigned int count)
480 {
481 if (width == 0 || height == 0) {
482 errno = EINVAL;
483 return NULL;
484 }
485
486 if ((count && !modifiers) || (modifiers && !count)) {
487 errno = EINVAL;
488 return NULL;
489 }
490
491 return gbm->bo_create(gbm, width, height, format, 0, modifiers, count);
492 }
493 /**
494 * Create a gbm buffer object from an foreign object
495 *
496 * This function imports a foreign object and creates a new gbm bo for it.
497 * This enabled using the foreign object with a display API such as KMS.
498 * Currently three types of foreign objects are supported, indicated by the type
499 * argument:
500 *
501 * GBM_BO_IMPORT_WL_BUFFER
502 * GBM_BO_IMPORT_EGL_IMAGE
503 * GBM_BO_IMPORT_FD
504 *
505 * The gbm bo shares the underlying pixels but its life-time is
506 * independent of the foreign object.
507 *
508 * \param gbm The gbm device returned from gbm_create_device()
509 * \param type The type of object we're importing
510 * \param buffer Pointer to the external object
511 * \param usage The union of the usage flags for this buffer
512 *
513 * \return A newly allocated buffer object that should be freed with
514 * gbm_bo_destroy() when no longer needed. On error, %NULL is returned
515 * and errno is set.
516 *
517 * \sa enum gbm_bo_flags for the list of usage flags
518 */
519 GBM_EXPORT struct gbm_bo *
520 gbm_bo_import(struct gbm_device *gbm,
521 uint32_t type, void *buffer, uint32_t usage)
522 {
523 return gbm->bo_import(gbm, type, buffer, usage);
524 }
525
526 /**
527 * Map a region of a gbm buffer object for cpu access
528 *
529 * This function maps a region of a gbm bo for cpu read and/or write
530 * access.
531 *
532 * \param bo The buffer object
533 * \param x The X (top left origin) starting position of the mapped region for
534 * the buffer
535 * \param y The Y (top left origin) starting position of the mapped region for
536 * the buffer
537 * \param width The width of the mapped region for the buffer
538 * \param height The height of the mapped region for the buffer
539 * \param flags The union of the GBM_BO_TRANSFER_* flags for this buffer
540 * \param stride Ptr for returned stride in bytes of the mapped region
541 * \param map_data Returned opaque ptr for the mapped region
542 *
543 * \return Address of the mapped buffer that should be unmapped with
544 * gbm_bo_unmap() when no longer needed. On error, %NULL is returned
545 * and errno is set.
546 *
547 * \sa enum gbm_bo_transfer_flags for the list of flags
548 */
549 GBM_EXPORT void *
550 gbm_bo_map(struct gbm_bo *bo,
551 uint32_t x, uint32_t y,
552 uint32_t width, uint32_t height,
553 uint32_t flags, uint32_t *stride, void **map_data)
554 {
555 if (!bo || width == 0 || height == 0 || !stride || !map_data) {
556 errno = EINVAL;
557 return NULL;
558 }
559
560 return bo->gbm->bo_map(bo, x, y, width, height,
561 flags, stride, map_data);
562 }
563
564 /**
565 * Unmap a previously mapped region of a gbm buffer object
566 *
567 * This function unmaps a region of a gbm bo for cpu read and/or write
568 * access.
569 *
570 * \param bo The buffer object
571 * \param map_data opaque ptr returned from prior gbm_bo_map
572 */
573 GBM_EXPORT void
574 gbm_bo_unmap(struct gbm_bo *bo, void *map_data)
575 {
576 bo->gbm->bo_unmap(bo, map_data);
577 }
578
579 /**
580 * Allocate a surface object
581 *
582 * \param gbm The gbm device returned from gbm_create_device()
583 * \param width The width for the surface
584 * \param height The height for the surface
585 * \param format The format to use for the surface
586 *
587 * \return A newly allocated surface that should be freed with
588 * gbm_surface_destroy() when no longer needed. If an error occurs
589 * during allocation %NULL will be returned.
590 *
591 * \sa enum gbm_bo_format for the list of formats
592 */
593 GBM_EXPORT struct gbm_surface *
594 gbm_surface_create(struct gbm_device *gbm,
595 uint32_t width, uint32_t height,
596 uint32_t format, uint32_t flags)
597 {
598 return gbm->surface_create(gbm, width, height, format, flags, NULL, 0);
599 }
600
601 GBM_EXPORT struct gbm_surface *
602 gbm_surface_create_with_modifiers(struct gbm_device *gbm,
603 uint32_t width, uint32_t height,
604 uint32_t format,
605 const uint64_t *modifiers,
606 const unsigned int count)
607 {
608 if ((count && !modifiers) || (modifiers && !count)) {
609 errno = EINVAL;
610 return NULL;
611 }
612
613 return gbm->surface_create(gbm, width, height, format, 0,
614 modifiers, count);
615 }
616
617 /**
618 * Destroys the given surface and frees all resources associated with
619 * it.
620 *
621 * All buffers locked with gbm_surface_lock_front_buffer() should be
622 * released prior to calling this function.
623 *
624 * \param surf The surface
625 */
626 GBM_EXPORT void
627 gbm_surface_destroy(struct gbm_surface *surf)
628 {
629 surf->gbm->surface_destroy(surf);
630 }
631
632 /**
633 * Lock the surface's current front buffer
634 *
635 * Lock rendering to the surface's current front buffer until it is
636 * released with gbm_surface_release_buffer().
637 *
638 * This function must be called exactly once after calling
639 * eglSwapBuffers. Calling it before any eglSwapBuffer has happened
640 * on the surface or two or more times after eglSwapBuffers is an
641 * error. A new bo representing the new front buffer is returned. On
642 * multiple invocations, all the returned bos must be released in
643 * order to release the actual surface buffer.
644 *
645 * \param surf The surface
646 *
647 * \return A buffer object that should be released with
648 * gbm_surface_release_buffer() when no longer needed. The implementation
649 * is free to reuse buffers released with gbm_surface_release_buffer() so
650 * this bo should not be destroyed using gbm_bo_destroy(). If an error
651 * occurs this function returns %NULL.
652 */
653 GBM_EXPORT struct gbm_bo *
654 gbm_surface_lock_front_buffer(struct gbm_surface *surf)
655 {
656 return surf->gbm->surface_lock_front_buffer(surf);
657 }
658
659 /**
660 * Release a locked buffer obtained with gbm_surface_lock_front_buffer()
661 *
662 * Returns the underlying buffer to the gbm surface. Releasing a bo
663 * will typically make gbm_surface_has_free_buffer() return 1 and thus
664 * allow rendering the next frame, but not always. The implementation
665 * may choose to destroy the bo immediately or reuse it, in which case
666 * the user data associated with it is unchanged.
667 *
668 * \param surf The surface
669 * \param bo The buffer object
670 */
671 GBM_EXPORT void
672 gbm_surface_release_buffer(struct gbm_surface *surf, struct gbm_bo *bo)
673 {
674 surf->gbm->surface_release_buffer(surf, bo);
675 }
676
677 /**
678 * Return whether or not a surface has free (non-locked) buffers
679 *
680 * Before starting a new frame, the surface must have a buffer
681 * available for rendering. Initially, a gbm surface will have a free
682 * buffer, but after one or more buffers have been locked (\sa
683 * gbm_surface_lock_front_buffer()), the application must check for a
684 * free buffer before rendering.
685 *
686 * If a surface doesn't have a free buffer, the application must
687 * return a buffer to the surface using gbm_surface_release_buffer()
688 * and after that, the application can query for free buffers again.
689 *
690 * \param surf The surface
691 * \return 1 if the surface has free buffers, 0 otherwise
692 */
693 GBM_EXPORT int
694 gbm_surface_has_free_buffers(struct gbm_surface *surf)
695 {
696 return surf->gbm->surface_has_free_buffers(surf);
697 }