1 /**************************************************************************
2  *
3  * Copyright (C) 2019 Chromium.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  **************************************************************************/
24 
25 #ifndef VIRGL_GBM_H
26 #define VIRGL_GBM_H
27 
28 #include <gbm.h>
29 #include "vrend_iov.h"
30 #include "virglrenderer.h"
31 
32 #ifdef GBM_MAX_PLANES
33 #define VIRGL_GBM_MAX_PLANES GBM_MAX_PLANES
34 #else
35 #define VIRGL_GBM_MAX_PLANES 4
36 #endif
37 
38 /* GBM_FORMAT_ABGR16161616F was added since mesa 20.0 */
39 #ifndef GBM_FORMAT_ABGR16161616F
40 #define GBM_FORMAT_ABGR16161616F __gbm_fourcc_code('A', 'B', '4', 'H') /* [63:0] A:B:G:R 16:16:16:16 little endian */
41 #endif
42 
43 #ifndef MINIGBM
44 
45 #define GBM_BO_USE_TEXTURING (1 << 5),
46 #define GBM_BO_USE_CAMERA_WRITE (1 << 6)
47 #define GBM_BO_USE_CAMERA_READ (1 << 7)
48 #define GBM_BO_USE_PROTECTED (1 << 8)
49 #define GBM_BO_USE_SW_READ_OFTEN (1 << 9)
50 #define GBM_BO_USE_SW_READ_RARELY (1 << 10)
51 #define GBM_BO_USE_SW_WRITE_OFTEN (1 << 11)
52 #define GBM_BO_USE_SW_WRITE_RARELY (1 << 12)
53 #define GBM_BO_USE_HW_VIDEO_DECODER (1 << 13)
54 #define GBM_BO_USE_HW_VIDEO_ENCODER (1 << 14)
55 #define GBM_TEST_ALLOC (1 << 15)
56 
57 #endif
58 
59 /*
60  * If fd >= 0, virglrenderer owns the fd since it was opened via a rendernode
61  * query. If fd < 0, the gbm device was opened with the fd provided by the
62  * (*get_drm_fd) hook.
63  */
64 struct virgl_gbm {
65    int fd;
66    struct gbm_device *device;
67 };
68 
69 struct virgl_gbm *virgl_gbm_init(int fd);
70 
71 void virgl_gbm_fini(struct virgl_gbm *gbm);
72 
73 int virgl_gbm_convert_format(uint32_t *virgl_format, uint32_t *gbm_format);
74 
75 int virgl_gbm_transfer(struct gbm_bo *bo, uint32_t direction, const struct iovec *iovecs,
76                        uint32_t num_iovecs, const struct vrend_transfer_info *info);
77 
78 uint32_t virgl_gbm_convert_flags(uint32_t virgl_bind_flags);
79 
80 int virgl_gbm_export_fd(struct gbm_device *gbm, uint32_t handle, int32_t *out_fd);
81 
82 int virgl_gbm_export_query(struct gbm_bo *bo, struct virgl_renderer_export_query *query);
83 
84 int virgl_gbm_get_plane_width(struct gbm_bo *bo, int plane);
85 int virgl_gbm_get_plane_height(struct gbm_bo *bo, int plane);
86 int virgl_gbm_get_plane_bytes_per_pixel(struct gbm_bo *bo, int plane);
87 
88 bool virgl_gbm_external_allocation_preferred(uint32_t flags);
89 bool virgl_gbm_gpu_import_required(uint32_t flags);
90 
91 #endif
92