xref: /aosp_15_r20/external/virglrenderer/src/virglrenderer.h (revision bbecb9d118dfdb95f99bd754f8fa9be01f189df3)
1 /**************************************************************************
2  *
3  * Copyright (C) 2014 Red Hat Inc.
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 /* library interface from QEMU to virglrenderer */
26 
27 #ifndef VIRGLRENDERER_H
28 #define VIRGLRENDERER_H
29 
30 #include <stdint.h>
31 #include <stdbool.h>
32 #include <stdarg.h>
33 
34 struct virgl_box;
35 struct iovec;
36 
37 #define VIRGL_EXPORT  __attribute__((visibility("default")))
38 
39 typedef void *virgl_renderer_gl_context;
40 
41 struct virgl_renderer_gl_ctx_param {
42    int version;
43    bool shared;
44    int major_ver;
45    int minor_ver;
46 };
47 
48 #ifdef VIRGL_RENDERER_UNSTABLE_APIS
49 #define VIRGL_RENDERER_CALLBACKS_VERSION 4
50 #else
51 #define VIRGL_RENDERER_CALLBACKS_VERSION 2
52 #endif
53 
54 struct virgl_renderer_callbacks {
55    int version;
56    void (*write_fence)(void *cookie, uint32_t fence);
57 
58    /*
59     * The following 3 callbacks allows virglrenderer to
60     * use winsys from caller, instead of initializing it's own
61     * winsys (flag VIRGL_RENDERER_USE_EGL or VIRGL_RENDERER_USE_GLX).
62     */
63 
64    /* create a GL/GLES context */
65    virgl_renderer_gl_context (*create_gl_context)(void *cookie, int scanout_idx, struct virgl_renderer_gl_ctx_param *param);
66    /* destroy a GL/GLES context */
67    void (*destroy_gl_context)(void *cookie, virgl_renderer_gl_context ctx);
68    /* make a context current */
69    int (*make_current)(void *cookie, int scanout_idx, virgl_renderer_gl_context ctx);
70 
71    /*
72     * v2, used with flags & VIRGL_RENDERER_USE_EGL
73     * Chose the drm fd, that will be used by virglrenderer
74     * for winsys initialization.  Virglrenderer takes ownership of the fd
75     * that is returned and is responsible to close() it.  This should not
76     * return the same fd each time it is call, if called multiple times.
77     */
78    int (*get_drm_fd)(void *cookie);
79 
80 #ifdef VIRGL_RENDERER_UNSTABLE_APIS
81    void (*write_context_fence)(void *cookie, uint32_t ctx_id, uint32_t ring_idx, uint64_t fence_id);
82 
83    /* version 0: a connected socket of type SOCK_SEQPACKET */
84    int (*get_server_fd)(void *cookie, uint32_t version);
85 
86    /*
87     * Get the EGLDisplay from caller. It requires create_gl_context,
88     * destroy_gl_context, make_current to be implemented by caller.
89     */
90    void *(*get_egl_display)(void *cookie);
91 #endif
92 };
93 
94 /* virtio-gpu compatible interface */
95 #define VIRGL_RENDERER_USE_EGL 1
96 /*
97  * Wait for sync objects in thread rather than polling
98  * need to use virgl_renderer_get_poll_fd to know if this feature is in effect.
99  */
100 #define VIRGL_RENDERER_THREAD_SYNC 2
101 #define VIRGL_RENDERER_USE_GLX (1 << 2)
102 #define VIRGL_RENDERER_USE_SURFACELESS (1 << 3)
103 #define VIRGL_RENDERER_USE_GLES (1 << 4)
104 
105 #ifdef VIRGL_RENDERER_UNSTABLE_APIS
106 /*
107  * Blob resources used with the 3D driver must be able to be represented as file descriptors.
108  * The typical use case is the virtual machine manager (or vtest) is running in a multiprocess
109  * mode. In a standard Linux setup, that means the KVM process is different from the process that
110  * instantiated virglrenderer. For zero-copy capability to work, file descriptors must be used.
111  *
112  * VMMs that advertise support for the virtio-gpu feature VIRTIO_GPU_F_RESOURCE_BLOB and run in
113  * a multi-process mode *must* specify this flag.
114  */
115 #define VIRGL_RENDERER_USE_EXTERNAL_BLOB (1 << 5)
116 
117 /* Enable venus renderer.
118  */
119 #define VIRGL_RENDERER_VENUS         (1 << 6)
120 
121 /* Disable virgl renderer.
122  */
123 #define VIRGL_RENDERER_NO_VIRGL      (1 << 7)
124 
125 /*
126  * Used in conjonction with VIRGL_RENDERER_THREAD_SYNC;
127  * write_fence callback is executed directly from the polling thread. When enabled,
128  * virgl_renderer_get_poll_fd should not be used to watch for retired fences.
129  */
130 #define VIRGL_RENDERER_ASYNC_FENCE_CB (1 << 8)
131 
132 /* Start a render server and move GPU rendering to the render server.
133  *
134  * This is respected by the venus renderer but ignored by the virgl renderer.
135  */
136 #define VIRGL_RENDERER_RENDER_SERVER (1 << 9)
137 
138 /*
139  * Enable drm renderer.
140  */
141 #define VIRGL_RENDERER_DRM           (1 << 10)
142 
143 /* Video encode/decode */
144 #define VIRGL_RENDERER_USE_VIDEO     (1 << 11)
145 
146 
147 #endif /* VIRGL_RENDERER_UNSTABLE_APIS */
148 
149 VIRGL_EXPORT int virgl_renderer_init(void *cookie, int flags, struct virgl_renderer_callbacks *cb);
150 VIRGL_EXPORT void virgl_renderer_poll(void); /* force fences */
151 
152 /* we need to give qemu the cursor resource contents */
153 VIRGL_EXPORT void *virgl_renderer_get_cursor_data(uint32_t resource_id, uint32_t *width, uint32_t *height);
154 
155 VIRGL_EXPORT void virgl_renderer_get_rect(int resource_id, struct iovec *iov, unsigned int num_iovs,
156                                           uint32_t offset, int x, int y, int width, int height);
157 
158 VIRGL_EXPORT int virgl_renderer_get_fd_for_texture(uint32_t tex_id, int *fd);
159 VIRGL_EXPORT int virgl_renderer_get_fd_for_texture2(uint32_t tex_id, int *fd, int *stride, int *offset);
160 
161 /*
162  * These are only here for compatibility-reasons. In the future, use the flags
163  * from virgl_hw.h instead.
164  */
165 #define VIRGL_RES_BIND_DEPTH_STENCIL (1 << 0)
166 #define VIRGL_RES_BIND_RENDER_TARGET (1 << 1)
167 #define VIRGL_RES_BIND_SAMPLER_VIEW  (1 << 3)
168 #define VIRGL_RES_BIND_VERTEX_BUFFER (1 << 4)
169 #define VIRGL_RES_BIND_INDEX_BUFFER  (1 << 5)
170 #define VIRGL_RES_BIND_CONSTANT_BUFFER (1 << 6)
171 #define VIRGL_RES_BIND_STREAM_OUTPUT (1 << 11)
172 #define VIRGL_RES_BIND_CURSOR        (1 << 16)
173 #define VIRGL_RES_BIND_CUSTOM        (1 << 17)
174 #define VIRGL_RES_BIND_SCANOUT       (1 << 18)
175 #define VIRGL_RES_BIND_SHARED        (1 << 20)
176 
177 enum virgl_renderer_structure_type_v0 {
178    VIRGL_RENDERER_STRUCTURE_TYPE_NONE = 0,
179    VIRGL_RENDERER_STRUCTURE_TYPE_EXPORT_QUERY = (1 << 0),
180    VIRGL_RENDERER_STRUCTURE_TYPE_SUPPORTED_STRUCTURES = (1 << 1),
181 };
182 
183 struct virgl_renderer_resource_create_args {
184    uint32_t handle;
185    uint32_t target;
186    uint32_t format;
187    uint32_t bind;
188    uint32_t width;
189    uint32_t height;
190    uint32_t depth;
191    uint32_t array_size;
192    uint32_t last_level;
193    uint32_t nr_samples;
194    uint32_t flags;
195 };
196 
197 struct virgl_renderer_hdr {
198    uint32_t stype;
199    uint32_t stype_version;
200    uint32_t size;
201 };
202 
203 /*
204  * "out_num_fds" represents the number of distinct kernel buffers backing an
205  * allocation. If this number or 'out_fourcc' is zero, the resource is not
206  * exportable. The "out_fds" field will be populated with "out_num_fds" file
207  * descriptors if "in_export_fds" is non-zero.
208  */
209 struct virgl_renderer_export_query {
210    struct virgl_renderer_hdr hdr;
211    uint32_t in_resource_id;
212 
213    uint32_t out_num_fds;
214    uint32_t in_export_fds;
215    uint32_t out_fourcc;
216    uint32_t pad;
217 
218    int32_t out_fds[4];
219    uint32_t out_strides[4];
220    uint32_t out_offsets[4];
221    uint64_t out_modifier;
222 };
223 
224 /*
225  * "out_supported_structures_mask" is a bitmask representing the structures that
226  * virglrenderer knows how to handle for a given "in_stype_version".
227  */
228 
229 struct virgl_renderer_supported_structures {
230    struct virgl_renderer_hdr hdr;
231    uint32_t in_stype_version;
232    uint32_t out_supported_structures_mask;
233 };
234 
235 /* new API */
236 /* This typedef must be kept in sync with vrend_debug.h */
237 typedef void (*virgl_debug_callback_type)(const char *fmt, va_list ap);
238 
239 VIRGL_EXPORT int virgl_renderer_resource_create(struct virgl_renderer_resource_create_args *args, struct iovec *iov, uint32_t num_iovs);
240 VIRGL_EXPORT int virgl_renderer_resource_import_eglimage(struct virgl_renderer_resource_create_args *args, void *image);
241 VIRGL_EXPORT void virgl_renderer_resource_unref(uint32_t res_handle);
242 
243 VIRGL_EXPORT void virgl_renderer_resource_set_priv(uint32_t res_handle, void *priv);
244 VIRGL_EXPORT void *virgl_renderer_resource_get_priv(uint32_t res_handle);
245 
246 VIRGL_EXPORT int virgl_renderer_context_create(uint32_t handle, uint32_t nlen, const char *name);
247 VIRGL_EXPORT void virgl_renderer_context_destroy(uint32_t handle);
248 
249 VIRGL_EXPORT int virgl_renderer_submit_cmd(void *buffer,
250                                            int ctx_id,
251                                            int ndw);
252 
253 VIRGL_EXPORT int virgl_renderer_transfer_read_iov(uint32_t handle, uint32_t ctx_id,
254                                                   uint32_t level, uint32_t stride,
255                                                   uint32_t layer_stride,
256                                                   struct virgl_box *box,
257                                                   uint64_t offset, struct iovec *iov,
258                                                   int iovec_cnt);
259 
260 VIRGL_EXPORT int virgl_renderer_transfer_write_iov(uint32_t handle,
261                                                    uint32_t ctx_id,
262                                                    int level,
263                                                    uint32_t stride,
264                                                    uint32_t layer_stride,
265                                                    struct virgl_box *box,
266                                                    uint64_t offset,
267                                                    struct iovec *iovec,
268                                                    unsigned int iovec_cnt);
269 
270 VIRGL_EXPORT void virgl_renderer_get_cap_set(uint32_t set, uint32_t *max_ver,
271                                              uint32_t *max_size);
272 
273 VIRGL_EXPORT void virgl_renderer_fill_caps(uint32_t set, uint32_t version,
274                                            void *caps);
275 
276 VIRGL_EXPORT int virgl_renderer_resource_attach_iov(int res_handle, struct iovec *iov,
277                                                     int num_iovs);
278 VIRGL_EXPORT void virgl_renderer_resource_detach_iov(int res_handle, struct iovec **iov, int *num_iovs);
279 
280 VIRGL_EXPORT int virgl_renderer_create_fence(int client_fence_id, uint32_t ctx_id);
281 
282 VIRGL_EXPORT void virgl_renderer_force_ctx_0(void);
283 
284 VIRGL_EXPORT void virgl_renderer_ctx_attach_resource(int ctx_id, int res_handle);
285 VIRGL_EXPORT void virgl_renderer_ctx_detach_resource(int ctx_id, int res_handle);
286 
287 VIRGL_EXPORT virgl_debug_callback_type virgl_set_debug_callback(virgl_debug_callback_type cb);
288 
289 /* return information about a resource */
290 
291 struct virgl_renderer_resource_info {
292    uint32_t handle;
293    uint32_t virgl_format;
294    uint32_t width;
295    uint32_t height;
296    uint32_t depth;
297    uint32_t flags;
298    uint32_t tex_id;
299    uint32_t stride;
300    int drm_fourcc;
301 };
302 
303 VIRGL_EXPORT int virgl_renderer_resource_get_info(int res_handle,
304                                                   struct virgl_renderer_resource_info *info);
305 
306 VIRGL_EXPORT void virgl_renderer_cleanup(void *cookie);
307 
308 /* reset the rendererer - destroy all contexts and resource */
309 VIRGL_EXPORT void virgl_renderer_reset(void);
310 
311 VIRGL_EXPORT int virgl_renderer_get_poll_fd(void);
312 
313 VIRGL_EXPORT int virgl_renderer_execute(void *execute_args, uint32_t execute_size);
314 
315 #define VIRGL_RENDERER_CONTEXT_FLAG_CAPSET_ID_MASK 0xff
316 
317 VIRGL_EXPORT int virgl_renderer_context_create_with_flags(uint32_t ctx_id,
318                                                           uint32_t ctx_flags,
319                                                           uint32_t nlen,
320                                                           const char *name);
321 
322 #define VIRGL_RENDERER_BLOB_MEM_GUEST             0x0001
323 #define VIRGL_RENDERER_BLOB_MEM_HOST3D            0x0002
324 #define VIRGL_RENDERER_BLOB_MEM_HOST3D_GUEST      0x0003
325 #define VIRGL_RENDERER_BLOB_MEM_GUEST_VRAM        0x0004
326 
327 #define VIRGL_RENDERER_BLOB_FLAG_USE_MAPPABLE     0x0001
328 #define VIRGL_RENDERER_BLOB_FLAG_USE_SHAREABLE    0x0002
329 #define VIRGL_RENDERER_BLOB_FLAG_USE_CROSS_DEVICE 0x0004
330 
331 struct virgl_renderer_resource_create_blob_args
332 {
333    uint32_t res_handle;
334    uint32_t ctx_id;
335    uint32_t blob_mem;
336    uint32_t blob_flags;
337    uint64_t blob_id;
338    uint64_t size;
339    const struct iovec *iovecs;
340    uint32_t num_iovs;
341 };
342 
343 VIRGL_EXPORT int
344 virgl_renderer_resource_create_blob(const struct virgl_renderer_resource_create_blob_args *args);
345 
346 VIRGL_EXPORT int virgl_renderer_resource_map(uint32_t res_handle, void **map, uint64_t *out_size);
347 
348 VIRGL_EXPORT int virgl_renderer_resource_unmap(uint32_t res_handle);
349 
350 #define VIRGL_RENDERER_MAP_CACHE_MASK      0x0f
351 #define VIRGL_RENDERER_MAP_CACHE_NONE      0x00
352 #define VIRGL_RENDERER_MAP_CACHE_CACHED    0x01
353 #define VIRGL_RENDERER_MAP_CACHE_UNCACHED  0x02
354 #define VIRGL_RENDERER_MAP_CACHE_WC        0x03
355 
356 VIRGL_EXPORT int virgl_renderer_resource_get_map_info(uint32_t res_handle, uint32_t *map_info);
357 
358 #define VIRGL_RENDERER_BLOB_FD_TYPE_DMABUF        0x0001
359 #define VIRGL_RENDERER_BLOB_FD_TYPE_OPAQUE        0x0002
360 #define VIRGL_RENDERER_BLOB_FD_TYPE_SHM           0x0003
361 
362 VIRGL_EXPORT int
363 virgl_renderer_resource_export_blob(uint32_t res_id, uint32_t *fd_type, int *fd);
364 
365 /*
366  * These are unstable APIs for development only. Use these for development/testing purposes
367  * only, not in production
368  */
369 #ifdef VIRGL_RENDERER_UNSTABLE_APIS
370 
371 struct virgl_renderer_resource_import_blob_args
372 {
373    uint32_t res_handle;
374    uint32_t blob_mem;
375    uint32_t fd_type;
376    int fd;
377    uint64_t size;
378 };
379 
380 VIRGL_EXPORT int
381 virgl_renderer_resource_import_blob(const struct virgl_renderer_resource_import_blob_args *args);
382 
383 VIRGL_EXPORT int
384 virgl_renderer_export_fence(uint32_t client_fence_id, int *fd);
385 
386 #define VIRGL_RENDERER_FENCE_FLAG_MERGEABLE      (1 << 0)
387 VIRGL_EXPORT int virgl_renderer_context_create_fence(uint32_t ctx_id,
388                                                      uint32_t flags,
389                                                      uint32_t ring_idx,
390                                                      uint64_t fence_id);
391 VIRGL_EXPORT void virgl_renderer_context_poll(uint32_t ctx_id); /* force fences */
392 VIRGL_EXPORT int virgl_renderer_context_get_poll_fd(uint32_t ctx_id);
393 
394 #endif /* VIRGL_RENDERER_UNSTABLE_APIS */
395 
396 #endif
397