• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef VIRTIO_GOLDFISH_PIPE
2 #define VIRTIO_GOLDFISH_PIPE
3 
4 /* An override of virtio-gpu-3d (virgl) that runs goldfish pipe.  One could
5  * implement an actual virtio goldfish pipe, but this hijacking of virgl  is
6  * done in order to avoid any guest kernel changes. */
7 
8 #include <stddef.h>
9 
10 #include "virglrenderer.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 typedef uint32_t VirtioGpuCtxId;
16 typedef uint8_t VirtioGpuRingIdx;
17 struct virgl_renderer_virtio_interface* get_goldfish_pipe_virgl_renderer_virtio_interface(void);
18 
19 /* Needed for goldfish pipe */
20 void virgl_write_fence(void* opaque, uint32_t fence);
21 
22 #ifdef _WIN32
23 #define VG_EXPORT __declspec(dllexport)
24 #else
25 #define VG_EXPORT __attribute__((visibility("default")))
26 #endif
27 
28 #define VIRTIO_GOLDFISH_EXPORT_API
29 #ifdef VIRTIO_GOLDFISH_EXPORT_API
30 
31 VG_EXPORT int pipe_virgl_renderer_init(void* cookie, int flags,
32                                        struct virgl_renderer_callbacks* cb);
33 VG_EXPORT void pipe_virgl_renderer_poll(void);
34 VG_EXPORT void* pipe_virgl_renderer_get_cursor_data(uint32_t resource_id, uint32_t* width,
35                                                     uint32_t* height);
36 VG_EXPORT int pipe_virgl_renderer_resource_create(struct virgl_renderer_resource_create_args* args,
37                                                   struct iovec* iov, uint32_t num_iovs);
38 VG_EXPORT void pipe_virgl_renderer_resource_unref(uint32_t res_handle);
39 VG_EXPORT int pipe_virgl_renderer_context_create(uint32_t handle, uint32_t nlen, const char* name);
40 VG_EXPORT void pipe_virgl_renderer_context_destroy(uint32_t handle);
41 VG_EXPORT int pipe_virgl_renderer_submit_cmd(void* buffer, int ctx_id, int bytes);
42 VG_EXPORT int pipe_virgl_renderer_transfer_read_iov(uint32_t handle, uint32_t ctx_id,
43                                                     uint32_t level, uint32_t stride,
44                                                     uint32_t layer_stride, struct virgl_box* box,
45                                                     uint64_t offset, struct iovec* iov,
46                                                     int iovec_cnt);
47 VG_EXPORT int pipe_virgl_renderer_transfer_write_iov(uint32_t handle, uint32_t ctx_id, int level,
48                                                      uint32_t stride, uint32_t layer_stride,
49                                                      struct virgl_box* box, uint64_t offset,
50                                                      struct iovec* iovec, unsigned int iovec_cnt);
51 VG_EXPORT void pipe_virgl_renderer_get_cap_set(uint32_t set, uint32_t* max_ver, uint32_t* max_size);
52 VG_EXPORT void pipe_virgl_renderer_fill_caps(uint32_t set, uint32_t version, void* caps);
53 
54 VG_EXPORT int pipe_virgl_renderer_resource_attach_iov(int res_handle, struct iovec* iov,
55                                                       int num_iovs);
56 VG_EXPORT void pipe_virgl_renderer_resource_detach_iov(int res_handle, struct iovec** iov,
57                                                        int* num_iovs);
58 VG_EXPORT int pipe_virgl_renderer_create_fence(int client_fence_id, uint32_t cmd_type);
59 VG_EXPORT void pipe_virgl_renderer_force_ctx_0(void);
60 VG_EXPORT void pipe_virgl_renderer_ctx_attach_resource(int ctx_id, int res_handle);
61 VG_EXPORT void pipe_virgl_renderer_ctx_detach_resource(int ctx_id, int res_handle);
62 VG_EXPORT int pipe_virgl_renderer_resource_get_info(int res_handle,
63                                                     struct virgl_renderer_resource_info* info);
64 
65 VG_EXPORT void stream_renderer_flush_resource_and_readback(uint32_t res_handle, uint32_t x,
66                                                            uint32_t y, uint32_t width,
67                                                            uint32_t height, void* pixels,
68                                                            uint32_t max_bytes);
69 
70 #define STREAM_MEM_HANDLE_TYPE_OPAQUE_FD 0x1
71 #define STREAM_MEM_HANDLE_TYPE_DMABUF 0x2
72 #define STREAM_MEM_HANDLE_TYPE_OPAQUE_WIN32 0x3
73 #define STREAM_MEM_HANDLE_TYPE_SHM 0x4
74 #define STREAM_MEM_HANDLE_TYPE_ZIRCON 0x5
75 #define STREAM_FENCE_HANDLE_TYPE_OPAQUE_FD 0x6
76 #define STREAM_FENCE_HANDLE_TYPE_SYNC_FD 0x7
77 #define STREAM_FENCE_HANDLE_TYPE_OPAQUE_WIN32 0x8
78 #define STREAM_FENCE_HANDLE_TYPE_ZIRCON 0x9
79 struct stream_renderer_handle {
80     int64_t os_handle;
81     uint32_t handle_type;
82 };
83 
84 struct stream_renderer_create_blob {
85     uint32_t blob_mem;
86     uint32_t blob_flags;
87     uint64_t blob_id;
88     uint64_t size;
89 };
90 
91 #define STREAM_BLOB_MEM_GUEST 1
92 #define STREAM_BLOB_MEM_HOST3D 2
93 #define STREAM_BLOB_MEM_HOST3D_GUEST 3
94 
95 #define STREAM_BLOB_FLAG_USE_MAPPABLE 1
96 #define STREAM_BLOB_FLAG_USE_SHAREABLE 2
97 #define STREAM_BLOB_FLAG_USE_CROSS_DEVICE 4
98 #define STREAM_BLOB_FLAG_CREATE_GUEST_HANDLE 8
99 
100 VG_EXPORT int stream_renderer_create_blob(uint32_t ctx_id, uint32_t res_handle,
101                                           const struct stream_renderer_create_blob* create_blob,
102                                           const struct iovec* iovecs, uint32_t num_iovs,
103                                           const struct stream_renderer_handle* handle);
104 
105 VG_EXPORT int stream_renderer_export_blob(uint32_t res_handle,
106                                           struct stream_renderer_handle* handle);
107 
108 VG_EXPORT int stream_renderer_resource_map(uint32_t res_handle, void** hvaOut, uint64_t* sizeOut);
109 VG_EXPORT int stream_renderer_resource_unmap(uint32_t res_handle);
110 
111 VG_EXPORT int stream_renderer_context_create(uint32_t ctx_id, uint32_t nlen, const char* name,
112                                              uint32_t context_init);
113 
114 VG_EXPORT int stream_renderer_context_create_fence(uint64_t fence_id, uint32_t ctx_id,
115                                                    uint8_t ring_idx);
116 
117 // Platform resources and contexts support
118 #define STREAM_RENDERER_PLATFORM_RESOURCE_USE_MASK 0xF0
119 #define STREAM_RENDERER_PLATFORM_RESOURCE_TYPE_MASK 0x0F
120 
121 // types
122 #define STREAM_RENDERER_PLATFORM_RESOURCE_TYPE_EGL_NATIVE_PIXMAP 0x01
123 #define STREAM_RENDERER_PLATFORM_RESOURCE_TYPE_EGL_IMAGE 0x02
124 
125 // uses
126 #define STREAM_RENDERER_PLATFORM_RESOURCE_USE_PRESERVE 0x10
127 
128 VG_EXPORT int stream_renderer_platform_import_resource(int res_handle, int res_info,
129                                                        void* resource);
130 VG_EXPORT int stream_renderer_platform_resource_info(int res_handle, int* width, int* height,
131                                                      int* internal_format);
132 VG_EXPORT void* stream_renderer_platform_create_shared_egl_context(void);
133 VG_EXPORT int stream_renderer_platform_destroy_shared_egl_context(void*);
134 
135 #define STREAM_RENDERER_MAP_CACHE_MASK 0x0f
136 #define STREAM_RENDERER_MAP_CACHE_NONE 0x00
137 #define STREAM_RENDERER_MAP_CACHE_CACHED 0x01
138 #define STREAM_RENDERER_MAP_CACHE_UNCACHED 0x02
139 #define STREAM_RENDERER_MAP_CACHE_WC 0x03
140 VG_EXPORT int stream_renderer_resource_map_info(uint32_t res_handle, uint32_t* map_info);
141 
142 // Unique identifier for a GPU device.
143 struct stream_renderer_device_id {
144     uint8_t device_uuid[16];
145     uint8_t driver_uuid[16];
146 };
147 
148 struct stream_renderer_vulkan_info {
149     uint32_t memory_index;
150     struct stream_renderer_device_id device_id;
151 };
152 
153 VG_EXPORT int stream_renderer_vulkan_info(uint32_t res_handle,
154                                           struct stream_renderer_vulkan_info* vulkan_info);
155 
156 // Parameters - data passed to initialize the renderer, with the goal of avoiding FFI breakages.
157 // To change the data a parameter is passing safely, you should create a new parameter and
158 // deprecate the old one. The old parameter may be removed after sufficient time.
159 
160 // Reserved.
161 #define STREAM_RENDERER_PARAM_NULL 0
162 
163 // User data, for custom use by renderer. An example is VirglCookie which includes a fence
164 // handler and render server descriptor.
165 #define STREAM_RENDERER_PARAM_USER_DATA 1
166 
167 // Bitwise flags for the renderer.
168 #define STREAM_RENDERER_PARAM_RENDERER_FLAGS 2
169 
170 // Reserved to replace write_fence / write_context_fence.
171 #define STREAM_RENDERER_PARAM_FENCE_CALLBACK 3
172 
173 // Callback for writing a fence.
174 #define STREAM_RENDERER_PARAM_WRITE_FENCE_CALLBACK 4
175 typedef void (*stream_renderer_param_write_fence_callback)(void* user_data, uint32_t fence);
176 
177 // Callback for writing a fence with context.
178 #define STREAM_RENDERER_PARAM_WRITE_CONTEXT_FENCE_CALLBACK 5
179 typedef void (*stream_renderer_param_write_context_fence_callback)(void* user_data, uint64_t fence,
180                                                                    uint32_t ctx_id,
181                                                                    uint8_t ring_idx);
182 
183 // Window 0's width.
184 #define STREAM_RENDERER_PARAM_WIN0_WIDTH 6
185 
186 // Window 0's height.
187 #define STREAM_RENDERER_PARAM_WIN0_HEIGHT 7
188 
189 // Enables the host to control which memory types the guest will be allowed to map. For types not
190 // in the mask, the bits HOST_VISIBLE and HOST_COHERENT will be removed.
191 #define STREAM_RENDERER_PARAM_HOST_VISIBLE_MEMORY_MASK 8
192 
193 // Information about one device's memory mask.
194 struct stream_renderer_param_host_visible_memory_mask_entry {
195     // Which device the mask applies to.
196     struct stream_renderer_device_id device_id;
197     // Memory types allowed to be host visible are 1, otherwise 0.
198     uint32_t memory_type_mask;
199 };
200 
201 // Information about the devices in the system with host visible memory type constraints.
202 struct stream_renderer_param_host_visible_memory_mask {
203     // Points to a stream_renderer_param_host_visible_memory_mask_entry array.
204     uint64_t entries;
205     // Length of the entries array.
206     uint64_t num_entries;
207 };
208 
209 // Enables the host to control which GPU is used for rendering.
210 #define STREAM_RENDERER_PARAM_RENDERING_GPU 9
211 
212 // External callbacks for tracking metrics.
213 // Separating each function to a parameter allows new functions to be added later.
214 #define STREAM_RENDERER_PARAM_METRICS_CALLBACK_ADD_INSTANT_EVENT 1024
215 typedef void (*stream_renderer_param_metrics_callback_add_instant_event)(int64_t event_code);
216 
217 #define STREAM_RENDERER_PARAM_METRICS_CALLBACK_ADD_INSTANT_EVENT_WITH_DESCRIPTOR 1025
218 typedef void (*stream_renderer_param_metrics_callback_add_instant_event_with_descriptor)(
219     int64_t event_code, int64_t descriptor);
220 
221 #define STREAM_RENDERER_PARAM_METRICS_CALLBACK_ADD_INSTANT_EVENT_WITH_METRIC 1026
222 typedef void (*stream_renderer_param_metrics_callback_add_instant_event_with_metric)(
223     int64_t event_code, int64_t metric_value);
224 
225 #define STREAM_RENDERER_PARAM_METRICS_CALLBACK_ADD_VULKAN_OUT_OF_MEMORY_EVENT 1027
226 typedef void (*stream_renderer_param_metrics_callback_add_vulkan_out_of_memory_event)(
227     int64_t result_code, uint32_t op_code, const char* function, uint32_t line,
228     uint64_t allocation_size, bool is_host_side_result, bool is_allocation);
229 
230 #define STREAM_RENDERER_PARAM_METRICS_CALLBACK_SET_ANNOTATION 1028
231 typedef void (*stream_renderer_param_metrics_callback_set_annotation)(const char* key,
232                                                                       const char* value);
233 
234 #define STREAM_RENDERER_PARAM_METRICS_CALLBACK_ABORT 1029
235 typedef void (*stream_renderer_param_metrics_callback_abort)();
236 
237 // An entry in the stream renderer parameters list.
238 struct stream_renderer_param {
239     // The key should be one of STREAM_RENDERER_PARAM_*
240     uint64_t key;
241     // The value can be either a uint64_t or cast to a pointer to a struct, depending on if the
242     // parameter needs to pass data bigger than a single uint64_t.
243     uint64_t value;
244 };
245 
246 // Entry point for the stream renderer.
247 // Pass a list of parameters to configure the renderer. The available ones are listed above. If a
248 // parameter is not supported, the renderer will ignore it and warn in stderr.
249 // Return value 0 indicates success, and a negative number indicates failure.
250 VG_EXPORT int stream_renderer_init(struct stream_renderer_param* stream_renderer_params,
251                                    uint64_t num_params);
252 
253 VG_EXPORT void gfxstream_backend_setup_window(void* native_window_handle, int32_t window_x,
254                                               int32_t window_y, int32_t window_width,
255                                               int32_t window_height, int32_t fb_width,
256                                               int32_t fb_height);
257 
258 VG_EXPORT void gfxstream_backend_teardown(void);
259 
260 #else
261 
262 #define VG_EXPORT
263 
264 #endif  // !VIRTIO_GOLDFISH_EXPORT_API
265 
266 #ifdef __cplusplus
267 }  // extern "C"
268 #endif
269 
270 // based on VIRGL_RENDERER_USE* and friends
271 enum RendererFlags {
272     GFXSTREAM_RENDERER_FLAGS_USE_EGL_BIT = 1 << 0,
273     GFXSTREAM_RENDERER_FLAGS_THREAD_SYNC = 1 << 1,
274     GFXSTREAM_RENDERER_FLAGS_USE_GLX_BIT = 1 << 2,
275     GFXSTREAM_RENDERER_FLAGS_USE_SURFACELESS_BIT = 1 << 3,
276     GFXSTREAM_RENDERER_FLAGS_USE_GLES_BIT = 1 << 4,
277     GFXSTREAM_RENDERER_FLAGS_NO_VK_BIT = 1 << 5,          // for disabling vk
278     GFXSTREAM_RENDERER_FLAGS_ENABLE_GLES31_BIT = 1 << 9,  // disables the PlayStoreImage flag
279     GFXSTREAM_RENDERER_FLAGS_USE_EXTERNAL_BLOB = 1 << 10,
280     GFXSTREAM_RENDERER_FLAGS_USE_SYSTEM_BLOB = 1 << 11,
281     GFXSTREAM_RENDERER_FLAGS_GUEST_USES_ANGLE = 1 << 21,
282     GFXSTREAM_RENDERER_FLAGS_VULKAN_NATIVE_SWAPCHAIN_BIT = 1 << 22,
283     GFXSTREAM_RENDERER_FLAGS_ASYNC_FENCE_CB = 1 << 23,
284 };
285 
286 #endif
287