aboutsummaryrefslogtreecommitdiff
path: root/src/virgl_resource.h
blob: f610bec95d5f2fe3a95af5e5962d13e6b3c1c4a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**************************************************************************
 *
 * Copyright (C) 2020 Chromium
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#ifndef VIRGL_RESOURCE_H
#define VIRGL_RESOURCE_H

#include <stdint.h>

struct iovec;
struct pipe_resource;
struct virgl_context;

enum virgl_resource_fd_type {
   VIRGL_RESOURCE_FD_DMABUF,
   VIRGL_RESOURCE_FD_OPAQUE,
   /* mmap()-able, usually memfd or shm */
   VIRGL_RESOURCE_FD_SHM,

   /**
    * An opaque handle can be something like a GEM handle, from which a
    * fd can be created upon demand.
    *
    * Renderers which use this type must implement virgl_context::export_fd
    *
    * Do not use this type for resources that are _BLOB_FLAG_USE_SHAREABLE,
    * as the opaque handle can become invalid/stale any time outside of the
    * original context.
    */
   VIRGL_RESOURCE_OPAQUE_HANDLE,

   VIRGL_RESOURCE_FD_INVALID = -1,
};

struct virgl_resource_opaque_fd_metadata {
    uint8_t driver_uuid[16];
    uint8_t device_uuid[16];
    uint64_t allocation_size;
    uint32_t memory_type_index;
};

/**
 * A global cross-context resource.  A virgl_resource is not directly usable
 * by renderer contexts, but must be attached and imported into renderer
 * contexts to create context objects first.  For example, it can be attached
 * and imported into a vrend_decode_ctx to create a vrend_resource.
 *
 * It is also possible to create a virgl_resource from a context object.
 *
 * The underlying storage of a virgl_resource is provided by a pipe_resource
 * and/or a fd.  When it is provided by a pipe_resource, the virgl_resource is
 * said to be typed because pipe_resource also provides the type information.
 *
 * Conventional resources are always typed.  Blob resources by definition do
 * not have nor need type information, but those created from vrend_decode_ctx
 * objects are typed.  That should be considered a convenience rather than
 * something to be relied upon.  Contexts must not assume that every resource is
 * typed when interop is expected.
 */
struct virgl_resource {
   uint32_t res_id;

   struct pipe_resource *pipe_resource;

   /* valid fd or handle type: */
   enum virgl_resource_fd_type fd_type;
   int fd;

   /**
    * For fd_type==VIRGL_RESOURCE_OPAQUE_HANDLE, the id of the context
    * which created this resource
    */
   uint32_t opaque_handle_context_id;
   uint32_t opaque_handle;

   const struct iovec *iov;
   int iov_count;

   uint32_t map_info;

   uint64_t map_size;
   void *mapped;

   struct virgl_resource_opaque_fd_metadata opaque_fd_metadata;

   void *private_data;
};

struct virgl_resource_pipe_callbacks {
   void *data;

   void (*unref)(struct pipe_resource *pres, void *data);

   void (*attach_iov)(struct pipe_resource *pres,
                      const struct iovec *iov,
                      int iov_count,
                      void *data);
   void (*detach_iov)(struct pipe_resource *pres, void *data);

   enum virgl_resource_fd_type (*export_fd)(struct pipe_resource *pres,
                                            int *fd,
                                            void *data);

   uint64_t (*get_size)(struct pipe_resource *pres, void *data);
};

int
virgl_resource_table_init(const struct virgl_resource_pipe_callbacks *callbacks);

void
virgl_resource_table_cleanup(void);

void
virgl_resource_table_reset(void);

struct virgl_resource *
virgl_resource_create_from_pipe(uint32_t res_id,
                                struct pipe_resource *pres,
                                const struct iovec *iov,
                                int iov_count);

struct virgl_resource *
virgl_resource_create_from_fd(uint32_t res_id,
                              enum virgl_resource_fd_type fd_type,
                              int fd,
                              const struct iovec *iov,
                              int iov_count,
                              const struct virgl_resource_opaque_fd_metadata *opaque_fd_metadata);

struct virgl_resource *
virgl_resource_create_from_opaque_handle(struct virgl_context *ctx,
                                         uint32_t res_id,
                                         uint32_t opaque_handle);

struct virgl_resource *
virgl_resource_create_from_iov(uint32_t res_id,
                               const struct iovec *iov,
                               int iov_count);

void
virgl_resource_remove(uint32_t res_id);

struct virgl_resource *
virgl_resource_lookup(uint32_t res_id);

int
virgl_resource_attach_iov(struct virgl_resource *res,
                          const struct iovec *iov,
                          int iov_count);

void
virgl_resource_detach_iov(struct virgl_resource *res);

enum virgl_resource_fd_type
virgl_resource_export_fd(struct virgl_resource *res, int *fd);

uint64_t
virgl_resource_get_size(struct virgl_resource *res);

#endif /* VIRGL_RESOURCE_H */