• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Collabora Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef VIRGL_RESOURCE_CACHE_H
25 #define VIRGL_RESOURCE_CACHE_H
26 
27 #include <stdint.h>
28 
29 #include "util/list.h"
30 #include "gallium/include/pipe/p_defines.h"
31 
32 struct virgl_resource_params {
33    uint32_t size;
34    uint32_t bind;
35    uint32_t format;
36    uint32_t flags;
37    uint32_t nr_samples;
38    uint32_t width;
39    uint32_t height;
40    uint32_t depth;
41    uint32_t array_size;
42    uint32_t last_level;
43    enum pipe_texture_target target;
44 };
45 
46 struct virgl_resource_cache_entry {
47    struct list_head head;
48    int64_t timeout_start;
49    int64_t timeout_end;
50    struct virgl_resource_params params;
51 };
52 
53 /* Pointer to a function that returns whether the resource represented by
54  * the specified cache entry is busy.
55  */
56 typedef bool (*virgl_resource_cache_entry_is_busy_func) (
57    struct virgl_resource_cache_entry *entry, void *user_data);
58 
59 /* Pointer to a function that destroys the resource represented by
60  * the specified cache entry.
61  */
62 typedef void (*virgl_resource_cache_entry_release_func) (
63    struct virgl_resource_cache_entry *entry, void *user_data);
64 
65 struct virgl_resource_cache {
66    struct list_head resources;
67    unsigned timeout_usecs;
68    virgl_resource_cache_entry_is_busy_func entry_is_busy_func;
69    virgl_resource_cache_entry_release_func entry_release_func;
70    void *user_data;
71 };
72 
73 void
74 virgl_resource_cache_init(struct virgl_resource_cache *cache,
75                           unsigned timeout_usecs,
76                           virgl_resource_cache_entry_is_busy_func is_busy_func,
77                           virgl_resource_cache_entry_release_func destroy_func,
78                           void *user_data);
79 
80 /** Adds a resource to the cache.
81  *
82  *  Adding a resource that's already present in the cache leads to undefined
83  *  behavior.
84  */
85 void
86 virgl_resource_cache_add(struct virgl_resource_cache *cache,
87                          struct virgl_resource_cache_entry *entry);
88 
89 /** Finds and removes a cached resource compatible with size, bind and format.
90  *
91  *  Returns a pointer to the cache entry of the compatible resource, or NULL if
92  *  no such resource was found.
93  */
94 struct virgl_resource_cache_entry *
95 virgl_resource_cache_remove_compatible(struct virgl_resource_cache *cache,
96                                        struct virgl_resource_params params);
97 
98 /** Empties the resource cache. */
99 void
100 virgl_resource_cache_flush(struct virgl_resource_cache *cache);
101 
102 static inline void
virgl_resource_cache_entry_init(struct virgl_resource_cache_entry * entry,struct virgl_resource_params params)103 virgl_resource_cache_entry_init(struct virgl_resource_cache_entry *entry,
104                                 struct virgl_resource_params params)
105 {
106    entry->params = params;
107 }
108 
109 #endif
110