virgl: add virgl_resource

Add virgl_resource as the type of global cross-renderer resources.
A virgl_resource cannot be used in a renderer context directly.
After a virgl_resource is attached to a renderer context, a local
context object can be created from the virgl_resource.  This is
known as "import".  A renderer context always imports global
resources and deals with context objects exclusively.

It is also possible to create a virgl_resource from a context
object.  This is known as "export".  Whether import or export, the
global resource and the context object have independent lifetimes.

There is only GL renderer right now, and the context object for GL
renderer is vrend_resource.  Importing from virgl_resource or
exporting to virgl_resource is a matter of increasing the refcount
of vrend_resource.  It is fast but can be a bit confusing.

In the future, a virgl_resource can be from a different source
(e.g., VK renderer or an external dmabuf).  Importing such a
virgl_resource as a vrend_resource will entail more works.

Signed-off-by: Chia-I Wu <olvaffe@gmail.com>
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
macos/master
Chia-I Wu 5 years ago
parent 4691a160a3
commit dfd16cbcbc
  1. 2
      src/Makefile.am
  2. 2
      src/meson.build
  3. 4
      src/virgl_context.h
  4. 110
      src/virgl_resource.c
  5. 70
      src/virgl_resource.h

@ -23,6 +23,8 @@ libvrend_la_SOURCES = \
virgl_context.h \
virgl_hw.h \
virgl_protocol.h \
virgl_resource.c \
virgl_resource.h \
virgl_util.c \
virgl_util.h \
vrend_iov.h \

@ -29,6 +29,8 @@ virgl_sources = [
'virgl_context.h',
'virgl_hw.h',
'virgl_protocol.h',
'virgl_resource.c',
'virgl_resource.h',
'virgl_util.c',
'virgl_util.h',
'vrend_blitter.c',

@ -28,6 +28,10 @@
#include <stddef.h>
#include <stdint.h>
/**
* Base class for renderer contexts. For example, vrend_decode_ctx is a
* subclass of virgl_context.
*/
struct virgl_context {
uint32_t ctx_id;

@ -0,0 +1,110 @@
/**************************************************************************
*
* 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 <errno.h>
#include <string.h>
#include "util/u_hash_table.h"
#include "util/u_pointer.h"
#include "virgl_util.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);
free(res);
}
int
virgl_resource_table_init(const struct virgl_resource_pipe_callbacks *callbacks)
{
virgl_resource_table = util_hash_table_create(hash_func_u32,
compare_func,
virgl_resource_destroy_func);
if (!virgl_resource_table)
return ENOMEM;
pipe_callbacks = *callbacks;
return 0;
}
void
virgl_resource_table_cleanup(void)
{
util_hash_table_destroy(virgl_resource_table);
memset(&pipe_callbacks, 0, sizeof(pipe_callbacks));
}
void
virgl_resource_table_reset(void)
{
util_hash_table_clear(virgl_resource_table);
}
int
virgl_resource_create_from_pipe(uint32_t res_id, struct pipe_resource *pres)
{
struct virgl_resource *res;
enum pipe_error err;
res = calloc(1, sizeof(*res));
if (!res)
return ENOMEM;
err = util_hash_table_set(virgl_resource_table,
uintptr_to_pointer(res_id),
res);
if (err != PIPE_OK) {
free(res);
return ENOMEM;
}
res->res_id = res_id;
/* take ownership */
res->pipe_resource = pres;
return 0;
}
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));
}

@ -0,0 +1,70 @@
/**************************************************************************
*
* 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 pipe_resource;
/**
* 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.
*/
struct virgl_resource {
uint32_t res_id;
struct pipe_resource *pipe_resource;
};
struct virgl_resource_pipe_callbacks {
void *data;
void (*unref)(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);
int
virgl_resource_create_from_pipe(uint32_t res_id, struct pipe_resource *pres);
void
virgl_resource_remove(uint32_t res_id);
struct virgl_resource *
virgl_resource_lookup(uint32_t res_id);
#endif /* VIRGL_RESOURCE_H */
Loading…
Cancel
Save