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 // Log level of gfxstream 90 #ifndef STREAM_RENDERER_LOG_LEVEL 91 #define STREAM_RENDERER_LOG_LEVEL 1 92 #endif 93 94 // @user_data: custom user data passed during `stream_renderer_init` 95 // @type: one of STREAM_RENDERER_DEBUG_* 96 // @string: null-terminated C-string 97 #define STREAM_RENDERER_DEBUG_ERROR 0x1 98 #define STREAM_RENDERER_DEBUG_WARN 0x2 99 #define STREAM_RENDERER_DEBUG_INFO 0x3 100 struct stream_renderer_debug { 101 uint32_t debug_type; 102 const char* message; 103 }; 104 105 // Callback for writing a fence. 106 typedef void (*stream_renderer_fence_callback)(void* user_data, 107 struct stream_renderer_fence* fence_data); 108 109 // Callback for allowing debug prints or possibly even aborts. 110 typedef void (*stream_renderer_debug_callback)(void* user_data, 111 struct stream_renderer_debug* debug); 112 113 // Parameters - data passed to initialize the renderer, with the goal of avoiding FFI breakages. 114 // To change the data a parameter is passing safely, you should create a new parameter and 115 // deprecate the old one. The old parameter may be removed after sufficient time. 116 // 117 // STREAM_RENDERER_PARAM_NULL: Reserved value 118 // 119 // The following are required for correct operation: 120 // STREAM_RENDERER_PARAM_USER_DATA: User data, for custom use by VMM. 121 // STREAM_RENDERER_PARAM_RENDERER_FLAGS: Bitwise flags for the renderer. 122 // STREAM_RENDERER_PARAM_FENCE_CALLBACK: A function of the type `stream_renderer_fence_callback` 123 124 // The following are optional: 125 // STREAM_RENDERER_PARAM_WIN0_WIDTH: The width of window[0], when using surface-based rendering 126 // STREAM_RENDERER_PARAM_WIN0_HEIGHT: The height of window[0], when using surface-based rendering 127 #define STREAM_RENDERER_PARAM_NULL 0 128 #define STREAM_RENDERER_PARAM_USER_DATA 1 129 #define STREAM_RENDERER_PARAM_RENDERER_FLAGS 2 130 #define STREAM_RENDERER_PARAM_FENCE_CALLBACK 3 131 #define STREAM_RENDERER_PARAM_WIN0_WIDTH 4 132 #define STREAM_RENDERER_PARAM_WIN0_HEIGHT 5 133 #define STREAM_RENDERER_PARAM_DEBUG_CALLBACK 6 134 135 // An entry in the stream renderer parameters list. 136 // The key should be one of STREAM_RENDERER_PARAM_* 137 // The value can be either a uint64_t or cast to a pointer to a struct, depending on if the 138 // parameter needs to pass data bigger than a single uint64_t. 139 struct stream_renderer_param { 140 uint64_t key; 141 uint64_t value; 142 }; 143 144 // Entry point for the stream renderer. 145 // Pass a list of parameters to configure the renderer. The available ones are listed above. If a 146 // parameter is not supported, the renderer will ignore it and warn in stderr. 147 // Return value 0 indicates success, and a negative number indicates failure. 148 VG_EXPORT int stream_renderer_init(struct stream_renderer_param* stream_renderer_params, 149 uint64_t num_params); 150 151 VG_EXPORT void stream_renderer_teardown(void); 152 153 VG_EXPORT int stream_renderer_resource_create(struct stream_renderer_resource_create_args* args, 154 struct iovec* iov, uint32_t num_iovs); 155 VG_EXPORT void stream_renderer_resource_unref(uint32_t res_handle); 156 VG_EXPORT void stream_renderer_context_destroy(uint32_t handle); 157 158 struct stream_renderer_command { 159 uint32_t ctx_id; 160 uint32_t cmd_size; 161 uint8_t* cmd; 162 163 // Unstable: do not use until release strictly greater than 0.1.2 164 uint32_t num_in_fences; 165 struct stream_renderer_handle* fences; 166 }; 167 168 VG_EXPORT int stream_renderer_submit_cmd(struct stream_renderer_command* cmd); 169 170 VG_EXPORT int stream_renderer_transfer_read_iov(uint32_t handle, uint32_t ctx_id, uint32_t level, 171 uint32_t stride, uint32_t layer_stride, 172 struct stream_renderer_box* box, uint64_t offset, 173 struct iovec* iov, int iovec_cnt); 174 VG_EXPORT int stream_renderer_transfer_write_iov(uint32_t handle, uint32_t ctx_id, int level, 175 uint32_t stride, uint32_t layer_stride, 176 struct stream_renderer_box* box, uint64_t offset, 177 struct iovec* iovec, unsigned int iovec_cnt); 178 VG_EXPORT void stream_renderer_get_cap_set(uint32_t set, uint32_t* max_ver, uint32_t* max_size); 179 VG_EXPORT void stream_renderer_fill_caps(uint32_t set, uint32_t version, void* caps); 180 181 VG_EXPORT int stream_renderer_resource_attach_iov(int res_handle, struct iovec* iov, int num_iovs); 182 VG_EXPORT void stream_renderer_resource_detach_iov(int res_handle, struct iovec** iov, 183 int* num_iovs); 184 VG_EXPORT void stream_renderer_ctx_attach_resource(int ctx_id, int res_handle); 185 VG_EXPORT void stream_renderer_ctx_detach_resource(int ctx_id, int res_handle); 186 187 struct stream_renderer_create_blob { 188 uint32_t blob_mem; 189 uint32_t blob_flags; 190 uint64_t blob_id; 191 uint64_t size; 192 }; 193 194 #define STREAM_BLOB_MEM_GUEST 1 195 #define STREAM_BLOB_MEM_HOST3D 2 196 #define STREAM_BLOB_MEM_HOST3D_GUEST 3 197 198 #define STREAM_BLOB_FLAG_USE_MAPPABLE 1 199 #define STREAM_BLOB_FLAG_USE_SHAREABLE 2 200 #define STREAM_BLOB_FLAG_USE_CROSS_DEVICE 4 201 #define STREAM_BLOB_FLAG_CREATE_GUEST_HANDLE 8 202 203 VG_EXPORT int stream_renderer_create_blob(uint32_t ctx_id, uint32_t res_handle, 204 const struct stream_renderer_create_blob* create_blob, 205 const struct iovec* iovecs, uint32_t num_iovs, 206 const struct stream_renderer_handle* handle); 207 208 VG_EXPORT int stream_renderer_export_blob(uint32_t res_handle, 209 struct stream_renderer_handle* handle); 210 211 VG_EXPORT int stream_renderer_resource_map(uint32_t res_handle, void** hvaOut, uint64_t* sizeOut); 212 VG_EXPORT int stream_renderer_resource_unmap(uint32_t res_handle); 213 214 VG_EXPORT int stream_renderer_context_create(uint32_t ctx_id, uint32_t nlen, const char* name, 215 uint32_t context_init); 216 217 VG_EXPORT int stream_renderer_create_fence(const struct stream_renderer_fence* fence); 218 219 #define STREAM_RENDERER_MAP_CACHE_MASK 0x0f 220 #define STREAM_RENDERER_MAP_CACHE_NONE 0x00 221 #define STREAM_RENDERER_MAP_CACHE_CACHED 0x01 222 #define STREAM_RENDERER_MAP_CACHE_UNCACHED 0x02 223 #define STREAM_RENDERER_MAP_CACHE_WC 0x03 224 VG_EXPORT int stream_renderer_resource_map_info(uint32_t res_handle, uint32_t* map_info); 225 226 // Unique identifier for a GPU device. 227 struct stream_renderer_device_id { 228 uint8_t device_uuid[16]; 229 uint8_t driver_uuid[16]; 230 }; 231 232 struct stream_renderer_vulkan_info { 233 uint32_t memory_index; 234 struct stream_renderer_device_id device_id; 235 }; 236 237 VG_EXPORT int stream_renderer_vulkan_info(uint32_t res_handle, 238 struct stream_renderer_vulkan_info* vulkan_info); 239 240 #ifdef __cplusplus 241 } // extern "C" 242 #endif 243 244 // based on VIRGL_RENDERER_USE* and friends 245 enum RendererFlags { 246 STREAM_RENDERER_FLAGS_USE_EGL_BIT = 1 << 0, 247 STREAM_RENDERER_FLAGS_THREAD_SYNC = 1 << 1, 248 STREAM_RENDERER_FLAGS_USE_GLX_BIT = 1 << 2, 249 STREAM_RENDERER_FLAGS_USE_SURFACELESS_BIT = 1 << 3, 250 STREAM_RENDERER_FLAGS_USE_GLES_BIT = 1 << 4, 251 STREAM_RENDERER_FLAGS_USE_VK_BIT = 1 << 5, 252 STREAM_RENDERER_FLAGS_USE_EXTERNAL_BLOB = 1 << 6, 253 STREAM_RENDERER_FLAGS_USE_SYSTEM_BLOB = 1 << 7, 254 STREAM_RENDERER_FLAGS_VULKAN_NATIVE_SWAPCHAIN_BIT = 1 << 8, 255 STREAM_RENDERER_FLAGS_VULKAN_SNAPSHOTS = 1 << 9, 256 }; 257 258 #endif 259