xref: /aosp_15_r20/external/virglrenderer/server/render_virgl.h (revision bbecb9d118dfdb95f99bd754f8fa9be01f189df3)
1 /*
2  * Copyright 2021 Google LLC
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #ifndef RENDER_VIRGL_H
7 #define RENDER_VIRGL_H
8 
9 #include "render_common.h"
10 
11 #ifdef ENABLE_RENDER_SERVER_WORKER_THREAD
12 #include "c11/threads.h"
13 #endif
14 
15 /* Workers call into virglrenderer.  When they are processes, not much care is
16  * required.  We just want to be careful that the server process might have
17  * initialized viglrenderer before workers are forked.
18  *
19  * But when workers are threads, we need to grab a lock to protect
20  * virglrenderer.
21  *
22  * TODO skip virglrenderer.h and go straight to vkr_renderer.h.  That allows
23  * us to remove this file.
24  */
25 struct render_virgl {
26 #ifdef ENABLE_RENDER_SERVER_WORKER_THREAD
27    /* this protects the struct */
28    mtx_t struct_mutex;
29    /* this protects virglrenderer */
30    mtx_t dispatch_mutex;
31 #endif
32 
33    /* for nested initialization */
34    int init_count;
35    uint32_t init_flags;
36 
37    struct list_head contexts;
38 };
39 
40 extern struct render_virgl render_virgl_internal;
41 
42 bool
43 render_virgl_init(uint32_t init_flags);
44 
45 void
46 render_virgl_fini(void);
47 
48 void
49 render_virgl_add_context(struct render_context *ctx);
50 
51 void
52 render_virgl_remove_context(struct render_context *ctx);
53 
54 static inline void
render_virgl_lock_dispatch(void)55 render_virgl_lock_dispatch(void)
56 {
57 #ifdef ENABLE_RENDER_SERVER_WORKER_THREAD
58    mtx_lock(&render_virgl_internal.dispatch_mutex);
59 #endif
60 }
61 
62 static inline void
render_virgl_unlock_dispatch(void)63 render_virgl_unlock_dispatch(void)
64 {
65 #ifdef ENABLE_RENDER_SERVER_WORKER_THREAD
66    mtx_unlock(&render_virgl_internal.dispatch_mutex);
67 #endif
68 }
69 
70 #endif /* RENDER_VIRGL_H */
71