xref: /aosp_15_r20/external/virglrenderer/src/virgl_resource.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_RESOURCE_H
26 #define VIRGL_RESOURCE_H
27 
28 #include <stdint.h>
29 
30 struct iovec;
31 struct pipe_resource;
32 struct virgl_context;
33 
34 enum virgl_resource_fd_type {
35    VIRGL_RESOURCE_FD_DMABUF,
36    VIRGL_RESOURCE_FD_OPAQUE,
37    /* mmap()-able, usually memfd or shm */
38    VIRGL_RESOURCE_FD_SHM,
39 
40    /**
41     * An opaque handle can be something like a GEM handle, from which a
42     * fd can be created upon demand.
43     *
44     * Renderers which use this type must implement virgl_context::export_fd
45     *
46     * Do not use this type for resources that are _BLOB_FLAG_USE_SHAREABLE,
47     * as the opaque handle can become invalid/stale any time outside of the
48     * original context.
49     */
50    VIRGL_RESOURCE_OPAQUE_HANDLE,
51 
52    VIRGL_RESOURCE_FD_INVALID = -1,
53 };
54 
55 struct virgl_resource_opaque_fd_metadata {
56     uint8_t driver_uuid[16];
57     uint8_t device_uuid[16];
58     uint64_t allocation_size;
59     uint32_t memory_type_index;
60 };
61 
62 /**
63  * A global cross-context resource.  A virgl_resource is not directly usable
64  * by renderer contexts, but must be attached and imported into renderer
65  * contexts to create context objects first.  For example, it can be attached
66  * and imported into a vrend_decode_ctx to create a vrend_resource.
67  *
68  * It is also possible to create a virgl_resource from a context object.
69  *
70  * The underlying storage of a virgl_resource is provided by a pipe_resource
71  * and/or a fd.  When it is provided by a pipe_resource, the virgl_resource is
72  * said to be typed because pipe_resource also provides the type information.
73  *
74  * Conventional resources are always typed.  Blob resources by definition do
75  * not have nor need type information, but those created from vrend_decode_ctx
76  * objects are typed.  That should be considered a convenience rather than
77  * something to be relied upon.  Contexts must not assume that every resource is
78  * typed when interop is expected.
79  */
80 struct virgl_resource {
81    uint32_t res_id;
82 
83    struct pipe_resource *pipe_resource;
84 
85    /* valid fd or handle type: */
86    enum virgl_resource_fd_type fd_type;
87    int fd;
88 
89    /**
90     * For fd_type==VIRGL_RESOURCE_OPAQUE_HANDLE, the id of the context
91     * which created this resource
92     */
93    uint32_t opaque_handle_context_id;
94    uint32_t opaque_handle;
95 
96    const struct iovec *iov;
97    int iov_count;
98 
99    uint32_t map_info;
100 
101    uint64_t map_size;
102    void *mapped;
103 
104    struct virgl_resource_opaque_fd_metadata opaque_fd_metadata;
105 
106    void *private_data;
107 };
108 
109 struct virgl_resource_pipe_callbacks {
110    void *data;
111 
112    void (*unref)(struct pipe_resource *pres, void *data);
113 
114    void (*attach_iov)(struct pipe_resource *pres,
115                       const struct iovec *iov,
116                       int iov_count,
117                       void *data);
118    void (*detach_iov)(struct pipe_resource *pres, void *data);
119 
120    enum virgl_resource_fd_type (*export_fd)(struct pipe_resource *pres,
121                                             int *fd,
122                                             void *data);
123 
124    uint64_t (*get_size)(struct pipe_resource *pres, void *data);
125 };
126 
127 int
128 virgl_resource_table_init(const struct virgl_resource_pipe_callbacks *callbacks);
129 
130 void
131 virgl_resource_table_cleanup(void);
132 
133 void
134 virgl_resource_table_reset(void);
135 
136 struct virgl_resource *
137 virgl_resource_create_from_pipe(uint32_t res_id,
138                                 struct pipe_resource *pres,
139                                 const struct iovec *iov,
140                                 int iov_count);
141 
142 struct virgl_resource *
143 virgl_resource_create_from_fd(uint32_t res_id,
144                               enum virgl_resource_fd_type fd_type,
145                               int fd,
146                               const struct iovec *iov,
147                               int iov_count,
148                               const struct virgl_resource_opaque_fd_metadata *opaque_fd_metadata);
149 
150 struct virgl_resource *
151 virgl_resource_create_from_opaque_handle(struct virgl_context *ctx,
152                                          uint32_t res_id,
153                                          uint32_t opaque_handle);
154 
155 struct virgl_resource *
156 virgl_resource_create_from_iov(uint32_t res_id,
157                                const struct iovec *iov,
158                                int iov_count);
159 
160 void
161 virgl_resource_remove(uint32_t res_id);
162 
163 struct virgl_resource *
164 virgl_resource_lookup(uint32_t res_id);
165 
166 int
167 virgl_resource_attach_iov(struct virgl_resource *res,
168                           const struct iovec *iov,
169                           int iov_count);
170 
171 void
172 virgl_resource_detach_iov(struct virgl_resource *res);
173 
174 enum virgl_resource_fd_type
175 virgl_resource_export_fd(struct virgl_resource *res, int *fd);
176 
177 uint64_t
178 virgl_resource_get_size(struct virgl_resource *res);
179 
180 #endif /* VIRGL_RESOURCE_H */
181