1 // Copyright 2023 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef VIRTGPU_GFXSTREAM_RENDERER_H 16 #define VIRTGPU_GFXSTREAM_RENDERER_H 17 18 /* An implementation of virtio-gpu-3d that streams rendering commands. */ 19 20 #include <stddef.h> 21 #include <stdint.h> 22 23 #if defined(_WIN32) 24 struct iovec; 25 #else 26 #include <sys/uio.h> 27 #endif 28 29 struct stream_renderer_box { 30 uint32_t x, y, z; 31 uint32_t w, h, d; 32 }; 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 /** 39 * Versioning 40 */ 41 #define STREAM_RENDERER_VERSION_MAJOR 0 42 #define STREAM_RENDERER_VERSION_MINOR 1 43 #define STREAM_RENDERER_VERSION_PATCH 2 44 45 #ifdef _WIN32 46 #define VG_EXPORT __declspec(dllexport) 47 #else 48 #define VG_EXPORT __attribute__((visibility("default"))) 49 #endif 50 51 struct stream_renderer_resource_create_args { 52 uint32_t handle; 53 uint32_t target; 54 uint32_t format; 55 uint32_t bind; 56 uint32_t width; 57 uint32_t height; 58 uint32_t depth; 59 uint32_t array_size; 60 uint32_t last_level; 61 uint32_t nr_samples; 62 uint32_t flags; 63 }; 64 65 #define STREAM_RENDERER_FLAG_FENCE (1 << 0) 66 #define STREAM_RENDERER_FLAG_FENCE_RING_IDX (1 << 1) 67 #define STREAM_RENDERER_FLAG_FENCE_SHAREABLE (1 << 2) 68 struct stream_renderer_fence { 69 uint32_t flags; 70 uint64_t fence_id; 71 uint32_t ctx_id; 72 uint8_t ring_idx; 73 }; 74 75 #define STREAM_MEM_HANDLE_TYPE_OPAQUE_FD 0x1 76 #define STREAM_MEM_HANDLE_TYPE_DMABUF 0x2 77 #define STREAM_MEM_HANDLE_TYPE_OPAQUE_WIN32 0x3 78 #define STREAM_MEM_HANDLE_TYPE_SHM 0x4 79 #define STREAM_MEM_HANDLE_TYPE_ZIRCON 0x5 80 #define STREAM_FENCE_HANDLE_TYPE_OPAQUE_FD 0x6 81 #define STREAM_FENCE_HANDLE_TYPE_SYNC_FD 0x7 82 #define STREAM_FENCE_HANDLE_TYPE_OPAQUE_WIN32 0x8 83 #define STREAM_FENCE_HANDLE_TYPE_ZIRCON 0x9 84 struct stream_renderer_handle { 85 int64_t os_handle; 86 uint32_t handle_type; 87 }; 88 89 // @user_data: custom user data passed during `stream_renderer_init` 90 // @type: one of STREAM_RENDERER_DEBUG_* 91 // @string: null-terminated C-string 92 #define STREAM_RENDERER_DEBUG_ERROR 0x1 93 #define STREAM_RENDERER_DEBUG_WARN 0x2 94 #define STREAM_RENDERER_DEBUG_INFO 0x3 95 #define STREAM_RENDERER_DEBUG_DEBUG 0x4 96 struct stream_renderer_debug { 97 uint32_t debug_type; 98 const char* message; 99 }; 100 101 // Log level of gfxstream 102 #ifndef STREAM_RENDERER_LOG_LEVEL 103 #define STREAM_RENDERER_LOG_LEVEL STREAM_RENDERER_DEBUG_INFO 104 #endif 105 106 // Callback for writing a fence. 107 typedef void (*stream_renderer_fence_callback)(void* user_data, 108 struct stream_renderer_fence* fence_data); 109 110 // Callback for allowing debug prints or possibly even aborts. 111 typedef void (*stream_renderer_debug_callback)(void* user_data, 112 struct stream_renderer_debug* debug); 113 114 // Parameters - data passed to initialize the renderer, with the goal of avoiding FFI breakages. 115 // To change the data a parameter is passing safely, you should create a new parameter and 116 // deprecate the old one. The old parameter may be removed after sufficient time. 117 // 118 // STREAM_RENDERER_PARAM_NULL: Reserved value 119 // 120 // The following are required for correct operation: 121 // STREAM_RENDERER_PARAM_USER_DATA: User data, for custom use by VMM. 122 // STREAM_RENDERER_PARAM_RENDERER_FLAGS: Bitwise flags for the renderer. 123 // STREAM_RENDERER_PARAM_FENCE_CALLBACK: A function of the type `stream_renderer_fence_callback` 124 125 // The following are optional: 126 // STREAM_RENDERER_PARAM_WIN0_WIDTH: The width of window[0], when using surface-based rendering 127 // STREAM_RENDERER_PARAM_WIN0_HEIGHT: The height of window[0], when using surface-based rendering 128 #define STREAM_RENDERER_PARAM_NULL 0 129 #define STREAM_RENDERER_PARAM_USER_DATA 1 130 #define STREAM_RENDERER_PARAM_RENDERER_FLAGS 2 131 #define STREAM_RENDERER_PARAM_FENCE_CALLBACK 3 132 #define STREAM_RENDERER_PARAM_WIN0_WIDTH 4 133 #define STREAM_RENDERER_PARAM_WIN0_HEIGHT 5 134 #define STREAM_RENDERER_PARAM_DEBUG_CALLBACK 6 135 136 // An entry in the stream renderer parameters list. 137 // The key should be one of STREAM_RENDERER_PARAM_* 138 // The value can be either a uint64_t or cast to a pointer to a struct, depending on if the 139 // parameter needs to pass data bigger than a single uint64_t. 140 struct stream_renderer_param { 141 uint64_t key; 142 uint64_t value; 143 }; 144 145 // Entry point for the stream renderer. 146 // Pass a list of parameters to configure the renderer. The available ones are listed above. If a 147 // parameter is not supported, the renderer will ignore it and warn in stderr. 148 // Return value 0 indicates success, and a negative number indicates failure. 149 VG_EXPORT int stream_renderer_init(struct stream_renderer_param* stream_renderer_params, 150 uint64_t num_params); 151 152 VG_EXPORT void stream_renderer_teardown(void); 153 154 VG_EXPORT int stream_renderer_resource_create(struct stream_renderer_resource_create_args* args, 155 struct iovec* iov, uint32_t num_iovs); 156 VG_EXPORT void stream_renderer_resource_unref(uint32_t res_handle); 157 VG_EXPORT void stream_renderer_context_destroy(uint32_t handle); 158 159 struct stream_renderer_command { 160 uint32_t ctx_id; 161 uint32_t cmd_size; 162 uint8_t* cmd; 163 164 // Unstable: do not use until release strictly greater than 0.1.2 165 uint32_t num_in_fences; 166 struct stream_renderer_handle* fences; 167 }; 168 169 VG_EXPORT int stream_renderer_submit_cmd(struct stream_renderer_command* cmd); 170 171 VG_EXPORT int stream_renderer_transfer_read_iov(uint32_t handle, uint32_t ctx_id, uint32_t level, 172 uint32_t stride, uint32_t layer_stride, 173 struct stream_renderer_box* box, uint64_t offset, 174 struct iovec* iov, int iovec_cnt); 175 VG_EXPORT int stream_renderer_transfer_write_iov(uint32_t handle, uint32_t ctx_id, int level, 176 uint32_t stride, uint32_t layer_stride, 177 struct stream_renderer_box* box, uint64_t offset, 178 struct iovec* iovec, unsigned int iovec_cnt); 179 VG_EXPORT void stream_renderer_get_cap_set(uint32_t set, uint32_t* max_ver, uint32_t* max_size); 180 VG_EXPORT void stream_renderer_fill_caps(uint32_t set, uint32_t version, void* caps); 181 182 VG_EXPORT int stream_renderer_resource_attach_iov(int res_handle, struct iovec* iov, int num_iovs); 183 VG_EXPORT void stream_renderer_resource_detach_iov(int res_handle, struct iovec** iov, 184 int* num_iovs); 185 VG_EXPORT void stream_renderer_ctx_attach_resource(int ctx_id, int res_handle); 186 VG_EXPORT void stream_renderer_ctx_detach_resource(int ctx_id, int res_handle); 187 188 struct stream_renderer_create_blob { 189 uint32_t blob_mem; 190 uint32_t blob_flags; 191 uint64_t blob_id; 192 uint64_t size; 193 }; 194 195 #define STREAM_BLOB_MEM_GUEST 1 196 #define STREAM_BLOB_MEM_HOST3D 2 197 #define STREAM_BLOB_MEM_HOST3D_GUEST 3 198 199 #define STREAM_BLOB_FLAG_USE_MAPPABLE 1 200 #define STREAM_BLOB_FLAG_USE_SHAREABLE 2 201 #define STREAM_BLOB_FLAG_USE_CROSS_DEVICE 4 202 #define STREAM_BLOB_FLAG_CREATE_GUEST_HANDLE 8 203 204 VG_EXPORT int stream_renderer_create_blob(uint32_t ctx_id, uint32_t res_handle, 205 const struct stream_renderer_create_blob* create_blob, 206 const struct iovec* iovecs, uint32_t num_iovs, 207 const struct stream_renderer_handle* handle); 208 209 VG_EXPORT int stream_renderer_export_blob(uint32_t res_handle, 210 struct stream_renderer_handle* handle); 211 212 VG_EXPORT int stream_renderer_resource_map(uint32_t res_handle, void** hvaOut, uint64_t* sizeOut); 213 VG_EXPORT int stream_renderer_resource_unmap(uint32_t res_handle); 214 215 VG_EXPORT int stream_renderer_context_create(uint32_t ctx_id, uint32_t nlen, const char* name, 216 uint32_t context_init); 217 218 VG_EXPORT int stream_renderer_create_fence(const struct stream_renderer_fence* fence); 219 220 #define STREAM_RENDERER_MAP_CACHE_MASK 0x0f 221 #define STREAM_RENDERER_MAP_CACHE_NONE 0x00 222 #define STREAM_RENDERER_MAP_CACHE_CACHED 0x01 223 #define STREAM_RENDERER_MAP_CACHE_UNCACHED 0x02 224 #define STREAM_RENDERER_MAP_CACHE_WC 0x03 225 VG_EXPORT int stream_renderer_resource_map_info(uint32_t res_handle, uint32_t* map_info); 226 227 // Unique identifier for a GPU device. 228 struct stream_renderer_device_id { 229 uint8_t device_uuid[16]; 230 uint8_t driver_uuid[16]; 231 }; 232 233 struct stream_renderer_vulkan_info { 234 uint32_t memory_index; 235 struct stream_renderer_device_id device_id; 236 }; 237 238 VG_EXPORT int stream_renderer_vulkan_info(uint32_t res_handle, 239 struct stream_renderer_vulkan_info* vulkan_info); 240 241 #ifdef __cplusplus 242 } // extern "C" 243 #endif 244 245 // based on VIRGL_RENDERER_USE* and friends 246 enum RendererFlags { 247 STREAM_RENDERER_FLAGS_USE_EGL_BIT = 1 << 0, 248 STREAM_RENDERER_FLAGS_THREAD_SYNC = 1 << 1, 249 STREAM_RENDERER_FLAGS_USE_GLX_BIT = 1 << 2, 250 STREAM_RENDERER_FLAGS_USE_SURFACELESS_BIT = 1 << 3, 251 STREAM_RENDERER_FLAGS_USE_GLES_BIT = 1 << 4, 252 STREAM_RENDERER_FLAGS_USE_VK_BIT = 1 << 5, 253 STREAM_RENDERER_FLAGS_USE_EXTERNAL_BLOB = 1 << 6, 254 STREAM_RENDERER_FLAGS_USE_SYSTEM_BLOB = 1 << 7, 255 STREAM_RENDERER_FLAGS_VULKAN_NATIVE_SWAPCHAIN_BIT = 1 << 8, 256 }; 257 258 #endif 259