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 #define VIRGL_RENDERER_CALLBACKS_VERSION 2
49 
50 struct virgl_renderer_callbacks {
51    int version;
52    void (*write_fence)(void *cookie, uint32_t fence);
53 
54    /* interact with GL implementation */
55    virgl_renderer_gl_context (*create_gl_context)(void *cookie, int scanout_idx, struct virgl_renderer_gl_ctx_param *param);
56    void (*destroy_gl_context)(void *cookie, virgl_renderer_gl_context ctx);
57    int (*make_current)(void *cookie, int scanout_idx, virgl_renderer_gl_context ctx);
58 
59    int (*get_drm_fd)(void *cookie); /* v2, used with flags & VIRGL_RENDERER_USE_EGL */
60 };
61 
62 /* virtio-gpu compatible interface */
63 #define VIRGL_RENDERER_USE_EGL 1
64 /*
65  * Wait for sync objects in thread rather than polling
66  * need to use virgl_renderer_get_poll_fd to know if this feature is in effect.
67  */
68 #define VIRGL_RENDERER_THREAD_SYNC 2
69 #define VIRGL_RENDERER_USE_GLX (1 << 2)
70 #define VIRGL_RENDERER_USE_SURFACELESS (1 << 3)
71 #define VIRGL_RENDERER_USE_GLES (1 << 4)
72 
73 VIRGL_EXPORT int virgl_renderer_init(void *cookie, int flags, struct virgl_renderer_callbacks *cb);
74 VIRGL_EXPORT void virgl_renderer_poll(void); /* force fences */
75 
76 /* we need to give qemu the cursor resource contents */
77 VIRGL_EXPORT void *virgl_renderer_get_cursor_data(uint32_t resource_id, uint32_t *width, uint32_t *height);
78 
79 VIRGL_EXPORT void virgl_renderer_get_rect(int resource_id, struct iovec *iov, unsigned int num_iovs,
80                                           uint32_t offset, int x, int y, int width, int height);
81 
82 VIRGL_EXPORT int virgl_renderer_get_fd_for_texture(uint32_t tex_id, int *fd);
83 VIRGL_EXPORT int virgl_renderer_get_fd_for_texture2(uint32_t tex_id, int *fd, int *stride, int *offset);
84 
85 /*
86  * These are only here for compatibility-reasons. In the future, use the flags
87  * from virgl_hw.h instead.
88  */
89 #define VIRGL_RES_BIND_DEPTH_STENCIL (1 << 0)
90 #define VIRGL_RES_BIND_RENDER_TARGET (1 << 1)
91 #define VIRGL_RES_BIND_SAMPLER_VIEW  (1 << 3)
92 #define VIRGL_RES_BIND_VERTEX_BUFFER (1 << 4)
93 #define VIRGL_RES_BIND_INDEX_BUFFER  (1 << 5)
94 #define VIRGL_RES_BIND_CONSTANT_BUFFER (1 << 6)
95 #define VIRGL_RES_BIND_STREAM_OUTPUT (1 << 11)
96 #define VIRGL_RES_BIND_CURSOR        (1 << 16)
97 #define VIRGL_RES_BIND_CUSTOM        (1 << 17)
98 #define VIRGL_RES_BIND_SCANOUT       (1 << 18)
99 #define VIRGL_RES_BIND_SHARED        (1 << 20)
100 
101 enum virgl_renderer_structure_type_v0 {
102    VIRGL_RENDERER_STRUCTURE_TYPE_NONE = 0,
103    VIRGL_RENDERER_STRUCTURE_TYPE_EXPORT_QUERY = (1 << 0),
104    VIRGL_RENDERER_STRUCTURE_TYPE_SUPPORTED_STRUCTURES = (1 << 1),
105 };
106 
107 struct virgl_renderer_resource_create_args {
108    uint32_t handle;
109    uint32_t target;
110    uint32_t format;
111    uint32_t bind;
112    uint32_t width;
113    uint32_t height;
114    uint32_t depth;
115    uint32_t array_size;
116    uint32_t last_level;
117    uint32_t nr_samples;
118    uint32_t flags;
119 };
120 
121 struct virgl_renderer_hdr {
122    uint32_t stype;
123    uint32_t stype_version;
124    uint32_t size;
125 };
126 
127 /*
128  * "out_num_fds" represents the number of distinct kernel buffers backing an
129  * allocation. If this number or 'out_fourcc' is zero, the resource is not
130  * exportable. The "out_fds" field will be populated with "out_num_fds" file
131  * descriptors if "in_export_fds" is non-zero.
132  */
133 struct virgl_renderer_export_query {
134    struct virgl_renderer_hdr hdr;
135    uint32_t in_resource_id;
136 
137    uint32_t out_num_fds;
138    uint32_t in_export_fds;
139    uint32_t out_fourcc;
140    uint32_t pad;
141 
142    int32_t out_fds[4];
143    uint32_t out_strides[4];
144    uint32_t out_offsets[4];
145    uint64_t out_modifier;
146 };
147 
148 /*
149  * "out_supported_structures_mask" is a bitmask representing the structures that
150  * virglrenderer knows how to handle for a given "in_stype_version".
151  */
152 
153 struct virgl_renderer_supported_structures {
154    struct virgl_renderer_hdr hdr;
155    uint32_t in_stype_version;
156    uint32_t out_supported_structures_mask;
157 };
158 
159 /* new API */
160 /* This typedef must be kept in sync with vrend_debug.h */
161 typedef void (*virgl_debug_callback_type)(const char *fmt, va_list ap);
162 
163 VIRGL_EXPORT int virgl_renderer_resource_create(struct virgl_renderer_resource_create_args *args, struct iovec *iov, uint32_t num_iovs);
164 VIRGL_EXPORT int virgl_renderer_resource_import_eglimage(struct virgl_renderer_resource_create_args *args, void *image);
165 VIRGL_EXPORT void virgl_renderer_resource_unref(uint32_t res_handle);
166 
167 VIRGL_EXPORT void virgl_renderer_resource_set_priv(uint32_t res_handle, void *priv);
168 VIRGL_EXPORT void *virgl_renderer_resource_get_priv(uint32_t res_handle);
169 
170 VIRGL_EXPORT int virgl_renderer_context_create(uint32_t handle, uint32_t nlen, const char *name);
171 VIRGL_EXPORT void virgl_renderer_context_destroy(uint32_t handle);
172 
173 VIRGL_EXPORT int virgl_renderer_submit_cmd(void *buffer,
174                                            int ctx_id,
175                                            int ndw);
176 
177 VIRGL_EXPORT int virgl_renderer_transfer_read_iov(uint32_t handle, uint32_t ctx_id,
178                                                   uint32_t level, uint32_t stride,
179                                                   uint32_t layer_stride,
180                                                   struct virgl_box *box,
181                                                   uint64_t offset, struct iovec *iov,
182                                                   int iovec_cnt);
183 
184 VIRGL_EXPORT int virgl_renderer_transfer_write_iov(uint32_t handle,
185                                                    uint32_t ctx_id,
186                                                    int level,
187                                                    uint32_t stride,
188                                                    uint32_t layer_stride,
189                                                    struct virgl_box *box,
190                                                    uint64_t offset,
191                                                    struct iovec *iovec,
192                                                    unsigned int iovec_cnt);
193 
194 VIRGL_EXPORT void virgl_renderer_get_cap_set(uint32_t set, uint32_t *max_ver,
195                                              uint32_t *max_size);
196 
197 VIRGL_EXPORT void virgl_renderer_fill_caps(uint32_t set, uint32_t version,
198                                            void *caps);
199 
200 VIRGL_EXPORT int virgl_renderer_resource_attach_iov(int res_handle, struct iovec *iov,
201                                                     int num_iovs);
202 VIRGL_EXPORT void virgl_renderer_resource_detach_iov(int res_handle, struct iovec **iov, int *num_iovs);
203 
204 VIRGL_EXPORT int virgl_renderer_create_fence(int client_fence_id, uint32_t ctx_id);
205 
206 VIRGL_EXPORT void virgl_renderer_force_ctx_0(void);
207 
208 VIRGL_EXPORT void virgl_renderer_ctx_attach_resource(int ctx_id, int res_handle);
209 VIRGL_EXPORT void virgl_renderer_ctx_detach_resource(int ctx_id, int res_handle);
210 
211 VIRGL_EXPORT virgl_debug_callback_type virgl_set_debug_callback(virgl_debug_callback_type cb);
212 
213 /* return information about a resource */
214 
215 struct virgl_renderer_resource_info {
216    uint32_t handle;
217    uint32_t virgl_format;
218    uint32_t width;
219    uint32_t height;
220    uint32_t depth;
221    uint32_t flags;
222    uint32_t tex_id;
223    uint32_t stride;
224    int drm_fourcc;
225 };
226 
227 VIRGL_EXPORT int virgl_renderer_resource_get_info(int res_handle,
228                                                   struct virgl_renderer_resource_info *info);
229 
230 VIRGL_EXPORT void virgl_renderer_cleanup(void *cookie);
231 
232 /* reset the rendererer - destroy all contexts and resource */
233 VIRGL_EXPORT void virgl_renderer_reset(void);
234 
235 VIRGL_EXPORT int virgl_renderer_get_poll_fd(void);
236 
237 VIRGL_EXPORT int virgl_renderer_execute(void *execute_args, uint32_t execute_size);
238 
239 #endif
240