xref: /aosp_15_r20/external/virglrenderer/src/virgl_context.h (revision bbecb9d118dfdb95f99bd754f8fa9be01f189df3)
1 /**************************************************************************
2  *
3  * Copyright (C) 2020 Chromium.
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 #ifndef VIRGL_CONTEXT_H
26 #define VIRGL_CONTEXT_H
27 
28 #include <stdbool.h>
29 #include <stddef.h>
30 #include <stdint.h>
31 
32 #include "virglrenderer_hw.h"
33 #include "virgl_resource.h"
34 
35 struct vrend_transfer_info;
36 struct pipe_resource;
37 
38 struct virgl_context_blob {
39    /* valid fd or pipe resource */
40    enum virgl_resource_fd_type type;
41    union {
42       int fd;
43       uint32_t opaque_handle;
44       struct pipe_resource *pipe_resource;
45    } u;
46 
47    uint32_t map_info;
48 
49    struct virgl_resource_opaque_fd_metadata opaque_fd_metadata;
50 };
51 
52 struct virgl_context;
53 
54 typedef void (*virgl_context_fence_retire)(struct virgl_context *ctx,
55                                            uint32_t ring_idx,
56                                            uint64_t fence_id);
57 
58 /**
59  * Base class for renderer contexts.  For example, vrend_decode_ctx is a
60  * subclass of virgl_context.
61  */
62 struct virgl_context {
63    uint32_t ctx_id;
64 
65    enum virgl_renderer_capset capset_id;
66 
67    /*
68     * Each fence goes through submitted, signaled, and retired.  This callback
69     * is called from virgl_context::retire_fences to retire signaled fences of
70     * each queue.  When a queue has multiple signaled fences by the time
71     * virgl_context::retire_fences is called, this callback might not be called
72     * on all fences but only on the latest one, depending on the flags of the
73     * fences.
74     */
75    virgl_context_fence_retire fence_retire;
76 
77    void (*destroy)(struct virgl_context *ctx);
78 
79    void (*attach_resource)(struct virgl_context *ctx,
80                            struct virgl_resource *res);
81    void (*detach_resource)(struct virgl_context *ctx,
82                            struct virgl_resource *res);
83    enum virgl_resource_fd_type (*export_opaque_handle)(struct virgl_context *ctx,
84                                                        struct virgl_resource *res,
85                                                        int *out_fd);
86 
87    int (*transfer_3d)(struct virgl_context *ctx,
88                       struct virgl_resource *res,
89                       const struct vrend_transfer_info *info,
90                       int transfer_mode);
91 
92    /* These are used to create a virgl_resource from a context object.
93     *
94     * get_blob returns a virgl_context_blob from which a virgl_resource can be
95     * created.
96     *
97     * Note that get_blob is a one-time thing.  The context object might be
98     * destroyed or reject subsequent get_blob calls.
99     */
100    int (*get_blob)(struct virgl_context *ctx,
101                    uint32_t res_id,
102                    uint64_t blob_id,
103                    uint64_t blob_size,
104                    uint32_t blob_flags,
105                    struct virgl_context_blob *blob);
106 
107    int (*submit_cmd)(struct virgl_context *ctx,
108                      const void *buffer,
109                      size_t size);
110 
111    /*
112     * Return an fd that is readable whenever there is any signaled fence in
113     * any queue, or -1 if not supported.
114     */
115    int (*get_fencing_fd)(struct virgl_context *ctx);
116 
117    /* retire signaled fences of all queues */
118    void (*retire_fences)(struct virgl_context *ctx);
119 
120    /* submit a fence to the queue identified by queue_id */
121    int (*submit_fence)(struct virgl_context *ctx,
122                        uint32_t flags,
123                        uint32_t ring_idx,
124                        uint64_t fence_id);
125 };
126 
127 struct virgl_context_foreach_args {
128    bool (*callback)(struct virgl_context *ctx, void *data);
129    void *data;
130 };
131 
132 int
133 virgl_context_table_init(void);
134 
135 void
136 virgl_context_table_cleanup(void);
137 
138 void
139 virgl_context_table_reset(void);
140 
141 int
142 virgl_context_add(struct virgl_context *ctx);
143 
144 void
145 virgl_context_remove(uint32_t ctx_id);
146 
147 struct virgl_context *
148 virgl_context_lookup(uint32_t ctx_id);
149 
150 void
151 virgl_context_foreach(const struct virgl_context_foreach_args *args);
152 
153 #endif /* VIRGL_CONTEXT_H */
154