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 3 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 /* interact with GL implementation */ 59 virgl_renderer_gl_context (*create_gl_context)(void *cookie, int scanout_idx, struct virgl_renderer_gl_ctx_param *param); 60 void (*destroy_gl_context)(void *cookie, virgl_renderer_gl_context ctx); 61 int (*make_current)(void *cookie, int scanout_idx, virgl_renderer_gl_context ctx); 62 63 int (*get_drm_fd)(void *cookie); /* v2, used with flags & VIRGL_RENDERER_USE_EGL */ 64 65 #ifdef VIRGL_RENDERER_UNSTABLE_APIS 66 void (*write_context_fence)(void *cookie, uint32_t ctx_id, uint64_t queue_id, void *fence_cookie); 67 #endif 68 }; 69 70 /* virtio-gpu compatible interface */ 71 #define VIRGL_RENDERER_USE_EGL 1 72 /* 73 * Wait for sync objects in thread rather than polling 74 * need to use virgl_renderer_get_poll_fd to know if this feature is in effect. 75 */ 76 #define VIRGL_RENDERER_THREAD_SYNC 2 77 #define VIRGL_RENDERER_USE_GLX (1 << 2) 78 #define VIRGL_RENDERER_USE_SURFACELESS (1 << 3) 79 #define VIRGL_RENDERER_USE_GLES (1 << 4) 80 81 #ifdef VIRGL_RENDERER_UNSTABLE_APIS 82 /* 83 * Blob resources used with the 3D driver must be able to be represented as file descriptors. 84 * The typical use case is the virtual machine manager (or vtest) is running in a multiprocess 85 * mode. In a standard Linux setup, that means the KVM process is different from the process that 86 * instantiated virglrenderer. For zero-copy capability to work, file descriptors must be used. 87 * 88 * VMMs that advertise support for the virtio-gpu feature VIRTIO_GPU_F_RESOURCE_BLOB and run in 89 * a multi-process mode *must* specify this flag. 90 */ 91 #define VIRGL_RENDERER_USE_EXTERNAL_BLOB (1 << 5) 92 93 /* Enable venus renderer. 94 */ 95 #define VIRGL_RENDERER_VENUS (1 << 6) 96 97 /* Disable virgl renderer. 98 */ 99 #define VIRGL_RENDERER_NO_VIRGL (1 << 7) 100 101 /* 102 * Used in conjonction with VIRGL_RENDERER_THREAD_SYNC; 103 * write_fence callback is executed directly from the polling thread. When enabled, 104 * virgl_renderer_get_poll_fd should not be used to watch for retired fences. 105 */ 106 #define VIRGL_RENDERER_ASYNC_FENCE_CB (1 << 8) 107 108 #endif /* VIRGL_RENDERER_UNSTABLE_APIS */ 109 110 VIRGL_EXPORT int virgl_renderer_init(void *cookie, int flags, struct virgl_renderer_callbacks *cb); 111 VIRGL_EXPORT void virgl_renderer_poll(void); /* force fences */ 112 113 /* we need to give qemu the cursor resource contents */ 114 VIRGL_EXPORT void *virgl_renderer_get_cursor_data(uint32_t resource_id, uint32_t *width, uint32_t *height); 115 116 VIRGL_EXPORT void virgl_renderer_get_rect(int resource_id, struct iovec *iov, unsigned int num_iovs, 117 uint32_t offset, int x, int y, int width, int height); 118 119 VIRGL_EXPORT int virgl_renderer_get_fd_for_texture(uint32_t tex_id, int *fd); 120 VIRGL_EXPORT int virgl_renderer_get_fd_for_texture2(uint32_t tex_id, int *fd, int *stride, int *offset); 121 122 /* 123 * These are only here for compatibility-reasons. In the future, use the flags 124 * from virgl_hw.h instead. 125 */ 126 #define VIRGL_RES_BIND_DEPTH_STENCIL (1 << 0) 127 #define VIRGL_RES_BIND_RENDER_TARGET (1 << 1) 128 #define VIRGL_RES_BIND_SAMPLER_VIEW (1 << 3) 129 #define VIRGL_RES_BIND_VERTEX_BUFFER (1 << 4) 130 #define VIRGL_RES_BIND_INDEX_BUFFER (1 << 5) 131 #define VIRGL_RES_BIND_CONSTANT_BUFFER (1 << 6) 132 #define VIRGL_RES_BIND_STREAM_OUTPUT (1 << 11) 133 #define VIRGL_RES_BIND_CURSOR (1 << 16) 134 #define VIRGL_RES_BIND_CUSTOM (1 << 17) 135 #define VIRGL_RES_BIND_SCANOUT (1 << 18) 136 #define VIRGL_RES_BIND_SHARED (1 << 20) 137 138 enum virgl_renderer_structure_type_v0 { 139 VIRGL_RENDERER_STRUCTURE_TYPE_NONE = 0, 140 VIRGL_RENDERER_STRUCTURE_TYPE_EXPORT_QUERY = (1 << 0), 141 VIRGL_RENDERER_STRUCTURE_TYPE_SUPPORTED_STRUCTURES = (1 << 1), 142 }; 143 144 struct virgl_renderer_resource_create_args { 145 uint32_t handle; 146 uint32_t target; 147 uint32_t format; 148 uint32_t bind; 149 uint32_t width; 150 uint32_t height; 151 uint32_t depth; 152 uint32_t array_size; 153 uint32_t last_level; 154 uint32_t nr_samples; 155 uint32_t flags; 156 }; 157 158 struct virgl_renderer_hdr { 159 uint32_t stype; 160 uint32_t stype_version; 161 uint32_t size; 162 }; 163 164 /* 165 * "out_num_fds" represents the number of distinct kernel buffers backing an 166 * allocation. If this number or 'out_fourcc' is zero, the resource is not 167 * exportable. The "out_fds" field will be populated with "out_num_fds" file 168 * descriptors if "in_export_fds" is non-zero. 169 */ 170 struct virgl_renderer_export_query { 171 struct virgl_renderer_hdr hdr; 172 uint32_t in_resource_id; 173 174 uint32_t out_num_fds; 175 uint32_t in_export_fds; 176 uint32_t out_fourcc; 177 uint32_t pad; 178 179 int32_t out_fds[4]; 180 uint32_t out_strides[4]; 181 uint32_t out_offsets[4]; 182 uint64_t out_modifier; 183 }; 184 185 /* 186 * "out_supported_structures_mask" is a bitmask representing the structures that 187 * virglrenderer knows how to handle for a given "in_stype_version". 188 */ 189 190 struct virgl_renderer_supported_structures { 191 struct virgl_renderer_hdr hdr; 192 uint32_t in_stype_version; 193 uint32_t out_supported_structures_mask; 194 }; 195 196 /* new API */ 197 /* This typedef must be kept in sync with vrend_debug.h */ 198 typedef void (*virgl_debug_callback_type)(const char *fmt, va_list ap); 199 200 VIRGL_EXPORT int virgl_renderer_resource_create(struct virgl_renderer_resource_create_args *args, struct iovec *iov, uint32_t num_iovs); 201 VIRGL_EXPORT int virgl_renderer_resource_import_eglimage(struct virgl_renderer_resource_create_args *args, void *image); 202 VIRGL_EXPORT void virgl_renderer_resource_unref(uint32_t res_handle); 203 204 VIRGL_EXPORT void virgl_renderer_resource_set_priv(uint32_t res_handle, void *priv); 205 VIRGL_EXPORT void *virgl_renderer_resource_get_priv(uint32_t res_handle); 206 207 VIRGL_EXPORT int virgl_renderer_context_create(uint32_t handle, uint32_t nlen, const char *name); 208 VIRGL_EXPORT void virgl_renderer_context_destroy(uint32_t handle); 209 210 VIRGL_EXPORT int virgl_renderer_submit_cmd(void *buffer, 211 int ctx_id, 212 int ndw); 213 214 VIRGL_EXPORT int virgl_renderer_transfer_read_iov(uint32_t handle, uint32_t ctx_id, 215 uint32_t level, uint32_t stride, 216 uint32_t layer_stride, 217 struct virgl_box *box, 218 uint64_t offset, struct iovec *iov, 219 int iovec_cnt); 220 221 VIRGL_EXPORT int virgl_renderer_transfer_write_iov(uint32_t handle, 222 uint32_t ctx_id, 223 int level, 224 uint32_t stride, 225 uint32_t layer_stride, 226 struct virgl_box *box, 227 uint64_t offset, 228 struct iovec *iovec, 229 unsigned int iovec_cnt); 230 231 VIRGL_EXPORT void virgl_renderer_get_cap_set(uint32_t set, uint32_t *max_ver, 232 uint32_t *max_size); 233 234 VIRGL_EXPORT void virgl_renderer_fill_caps(uint32_t set, uint32_t version, 235 void *caps); 236 237 VIRGL_EXPORT int virgl_renderer_resource_attach_iov(int res_handle, struct iovec *iov, 238 int num_iovs); 239 VIRGL_EXPORT void virgl_renderer_resource_detach_iov(int res_handle, struct iovec **iov, int *num_iovs); 240 241 VIRGL_EXPORT int virgl_renderer_create_fence(int client_fence_id, uint32_t ctx_id); 242 243 VIRGL_EXPORT void virgl_renderer_force_ctx_0(void); 244 245 VIRGL_EXPORT void virgl_renderer_ctx_attach_resource(int ctx_id, int res_handle); 246 VIRGL_EXPORT void virgl_renderer_ctx_detach_resource(int ctx_id, int res_handle); 247 248 VIRGL_EXPORT virgl_debug_callback_type virgl_set_debug_callback(virgl_debug_callback_type cb); 249 250 /* return information about a resource */ 251 252 struct virgl_renderer_resource_info { 253 uint32_t handle; 254 uint32_t virgl_format; 255 uint32_t width; 256 uint32_t height; 257 uint32_t depth; 258 uint32_t flags; 259 uint32_t tex_id; 260 uint32_t stride; 261 int drm_fourcc; 262 }; 263 264 VIRGL_EXPORT int virgl_renderer_resource_get_info(int res_handle, 265 struct virgl_renderer_resource_info *info); 266 267 VIRGL_EXPORT void virgl_renderer_cleanup(void *cookie); 268 269 /* reset the rendererer - destroy all contexts and resource */ 270 VIRGL_EXPORT void virgl_renderer_reset(void); 271 272 VIRGL_EXPORT int virgl_renderer_get_poll_fd(void); 273 274 VIRGL_EXPORT int virgl_renderer_execute(void *execute_args, uint32_t execute_size); 275 276 /* 277 * These are unstable APIs for development only. Use these for development/testing purposes 278 * only, not in production 279 */ 280 #ifdef VIRGL_RENDERER_UNSTABLE_APIS 281 282 #define VIRGL_RENDERER_CONTEXT_FLAG_CAPSET_ID_MASK 0xff 283 284 VIRGL_EXPORT int virgl_renderer_context_create_with_flags(uint32_t ctx_id, 285 uint32_t ctx_flags, 286 uint32_t nlen, 287 const char *name); 288 289 #define VIRGL_RENDERER_BLOB_MEM_GUEST 0x0001 290 #define VIRGL_RENDERER_BLOB_MEM_HOST3D 0x0002 291 #define VIRGL_RENDERER_BLOB_MEM_HOST3D_GUEST 0x0003 292 293 #define VIRGL_RENDERER_BLOB_FLAG_USE_MAPPABLE 0x0001 294 #define VIRGL_RENDERER_BLOB_FLAG_USE_SHAREABLE 0x0002 295 #define VIRGL_RENDERER_BLOB_FLAG_USE_CROSS_DEVICE 0x0004 296 297 struct virgl_renderer_resource_create_blob_args 298 { 299 uint32_t res_handle; 300 uint32_t ctx_id; 301 uint32_t blob_mem; 302 uint32_t blob_flags; 303 uint64_t blob_id; 304 uint64_t size; 305 const struct iovec *iovecs; 306 uint32_t num_iovs; 307 }; 308 309 VIRGL_EXPORT int 310 virgl_renderer_resource_create_blob(const struct virgl_renderer_resource_create_blob_args *args); 311 312 VIRGL_EXPORT int virgl_renderer_resource_map(uint32_t res_handle, void **map, uint64_t *out_size); 313 314 VIRGL_EXPORT int virgl_renderer_resource_unmap(uint32_t res_handle); 315 316 #define VIRGL_RENDERER_MAP_CACHE_MASK 0x0f 317 #define VIRGL_RENDERER_MAP_CACHE_NONE 0x00 318 #define VIRGL_RENDERER_MAP_CACHE_CACHED 0x01 319 #define VIRGL_RENDERER_MAP_CACHE_UNCACHED 0x02 320 #define VIRGL_RENDERER_MAP_CACHE_WC 0x03 321 322 VIRGL_EXPORT int virgl_renderer_resource_get_map_info(uint32_t res_handle, uint32_t *map_info); 323 324 #define VIRGL_RENDERER_BLOB_FD_TYPE_DMABUF 0x0001 325 #define VIRGL_RENDERER_BLOB_FD_TYPE_OPAQUE 0x0002 326 327 VIRGL_EXPORT int 328 virgl_renderer_resource_export_blob(uint32_t res_id, uint32_t *fd_type, int *fd); 329 330 VIRGL_EXPORT int 331 virgl_renderer_export_fence(uint32_t client_fence_id, int *fd); 332 333 #define VIRGL_RENDERER_FENCE_FLAG_MERGEABLE (1 << 0) 334 VIRGL_EXPORT int virgl_renderer_context_create_fence(uint32_t ctx_id, 335 uint32_t flags, 336 uint64_t queue_id, 337 void *fence_cookie); 338 VIRGL_EXPORT void virgl_renderer_context_poll(uint32_t ctx_id); /* force fences */ 339 VIRGL_EXPORT int virgl_renderer_context_get_poll_fd(uint32_t ctx_id); 340 341 #endif /* VIRGL_RENDERER_UNSTABLE_APIS */ 342 343 #endif 344