aboutsummaryrefslogtreecommitdiff
path: root/src/virgl_resource.c
blob: 7f2c3e6a745be4b4d26882839554baffe141bea4 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**************************************************************************
 *
 * 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.
 *
 **************************************************************************/

#include "virgl_resource.h"

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#include "util/os_file.h"
#include "util/u_hash_table.h"
#include "util/u_pointer.h"
#include "virgl_util.h"
#include "virgl_context.h"

static struct util_hash_table *virgl_resource_table;
static struct virgl_resource_pipe_callbacks pipe_callbacks;

static void
virgl_resource_destroy_func(void *val)
{
   struct virgl_resource *res = (struct virgl_resource *)val;

   if (res->pipe_resource)
      pipe_callbacks.unref(res->pipe_resource, pipe_callbacks.data);
   if ((res->fd_type != VIRGL_RESOURCE_FD_INVALID) &&
       (res->fd_type != VIRGL_RESOURCE_OPAQUE_HANDLE))
      close(res->fd);

   free(res);
}

int
virgl_resource_table_init(const struct virgl_resource_pipe_callbacks *callbacks)
{
   virgl_resource_table = util_hash_table_create(hash_func_u32,
                                                 equal_func,
                                                 virgl_resource_destroy_func);
   if (!virgl_resource_table)
      return ENOMEM;

   if (callbacks)
      pipe_callbacks = *callbacks;

   return 0;
}

void
virgl_resource_table_cleanup(void)
{
   util_hash_table_destroy(virgl_resource_table);
   virgl_resource_table = NULL;
   memset(&pipe_callbacks, 0, sizeof(pipe_callbacks));
}

void
virgl_resource_table_reset(void)
{
   util_hash_table_clear(virgl_resource_table);
}

static struct virgl_resource *
virgl_resource_create(uint32_t res_id)
{
   struct virgl_resource *res;
   enum pipe_error err;

   res = calloc(1, sizeof(*res));
   if (!res)
      return NULL;

   err = util_hash_table_set(virgl_resource_table,
                             uintptr_to_pointer(res_id),
                             res);
   if (err != PIPE_OK) {
      free(res);
      return NULL;
   }

   res->res_id = res_id;
   res->fd_type = VIRGL_RESOURCE_FD_INVALID;
   res->fd = -1;

   return res;
}

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 *res;

   res = virgl_resource_create(res_id);
   if (!res)
      return NULL;

   /* take ownership */
   res->pipe_resource = pres;

   res->iov = iov;
   res->iov_count = iov_count;

   return res;
}

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 *res;

   assert(fd_type != VIRGL_RESOURCE_FD_INVALID  && fd >= 0);

   res = virgl_resource_create(res_id);
   if (!res)
      return NULL;

   res->fd_type = fd_type;
   /* take ownership */
   res->fd = fd;

   res->iov = iov;
   res->iov_count = iov_count;

   if (opaque_fd_metadata && fd_type == VIRGL_RESOURCE_FD_OPAQUE)
      res->opaque_fd_metadata = *opaque_fd_metadata;

   return res;
}

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

   res = virgl_resource_create(res_id);
   if (!res)
      return NULL;

   res->fd_type = VIRGL_RESOURCE_OPAQUE_HANDLE;
   res->opaque_handle = opaque_handle;

   /* We need the ctx to get an fd from handle (which we don't want to do
    * until asked, to avoid file descriptor limits)
    *
    * Shareable resources should not use OPAQUE_HANDLE, to avoid lifetime
    * issues (ie. resource outliving the context which created it).
    */
   res->opaque_handle_context_id = ctx->ctx_id;

   return res;
}

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

   if (iov_count)
      assert(iov);

   res = virgl_resource_create(res_id);
   if (!res)
      return NULL;

   res->iov = iov;
   res->iov_count = iov_count;

   return res;
}

void
virgl_resource_remove(uint32_t res_id)
{
   util_hash_table_remove(virgl_resource_table, uintptr_to_pointer(res_id));
}

struct virgl_resource *virgl_resource_lookup(uint32_t res_id)
{
   return util_hash_table_get(virgl_resource_table,
                              uintptr_to_pointer(res_id));
}

int
virgl_resource_attach_iov(struct virgl_resource *res,
                          const struct iovec *iov,
                          int iov_count)
{
   if (res->iov)
      return EINVAL;

   res->iov = iov;
   res->iov_count = iov_count;

   if (res->pipe_resource) {
      pipe_callbacks.attach_iov(res->pipe_resource,
                                iov,
                                iov_count,
                                pipe_callbacks.data);
   }

   return 0;
}

void
virgl_resource_detach_iov(struct virgl_resource *res)
{
   if (!res->iov)
      return;

   if (res->pipe_resource)
      pipe_callbacks.detach_iov(res->pipe_resource, pipe_callbacks.data);

   res->iov = NULL;
   res->iov_count = 0;
}

enum virgl_resource_fd_type
virgl_resource_export_fd(struct virgl_resource *res, int *fd)
{
   if (res->fd_type == VIRGL_RESOURCE_OPAQUE_HANDLE) {
      struct virgl_context *ctx;

      ctx = virgl_context_lookup(res->opaque_handle_context_id);
      if (!ctx)
         return VIRGL_RESOURCE_FD_INVALID;

      return ctx->export_opaque_handle(ctx, res, fd);
   } else if (res->fd_type != VIRGL_RESOURCE_FD_INVALID) {
      *fd = os_dupfd_cloexec(res->fd);
      return *fd >= 0 ? res->fd_type : VIRGL_RESOURCE_FD_INVALID;
   } else if (res->pipe_resource) {
      return pipe_callbacks.export_fd(res->pipe_resource,
                                      fd,
                                      pipe_callbacks.data);
   }

   return VIRGL_RESOURCE_FD_INVALID;
}

uint64_t
virgl_resource_get_size(struct virgl_resource *res)
{
   if (res->map_size)
      return res->map_size;

   if (res->pipe_resource)
      return pipe_callbacks.get_size(res->pipe_resource, pipe_callbacks.data);

   return 0;
}