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
31 struct virgl_resource_cache_entry {
32 struct list_head head;
33 int64_t timeout_start;
34 int64_t timeout_end;
35 uint32_t size;
36 uint32_t bind;
37 uint32_t format;
38 uint32_t flags;
39 };
40
41 /* Pointer to a function that returns whether the resource represented by
42 * the specified cache entry is busy.
43 */
44 typedef bool (*virgl_resource_cache_entry_is_busy_func) (
45 struct virgl_resource_cache_entry *entry, void *user_data);
46
47 /* Pointer to a function that destroys the resource represented by
48 * the specified cache entry.
49 */
50 typedef void (*virgl_resource_cache_entry_release_func) (
51 struct virgl_resource_cache_entry *entry, void *user_data);
52
53 struct virgl_resource_cache {
54 struct list_head resources;
55 unsigned timeout_usecs;
56 virgl_resource_cache_entry_is_busy_func entry_is_busy_func;
57 virgl_resource_cache_entry_release_func entry_release_func;
58 void *user_data;
59 };
60
61 void
62 virgl_resource_cache_init(struct virgl_resource_cache *cache,
63 unsigned timeout_usecs,
64 virgl_resource_cache_entry_is_busy_func is_busy_func,
65 virgl_resource_cache_entry_release_func destroy_func,
66 void *user_data);
67
68 /** Adds a resource to the cache.
69 *
70 * Adding a resource that's already present in the cache leads to undefined
71 * behavior.
72 */
73 void
74 virgl_resource_cache_add(struct virgl_resource_cache *cache,
75 struct virgl_resource_cache_entry *entry);
76
77 /** Finds and removes a cached resource compatible with size, bind and format.
78 *
79 * Returns a pointer to the cache entry of the compatible resource, or NULL if
80 * no such resource was found.
81 */
82 struct virgl_resource_cache_entry *
83 virgl_resource_cache_remove_compatible(struct virgl_resource_cache *cache,
84 uint32_t size, uint32_t bind,
85 uint32_t format, uint32_t flags);
86
87 /** Empties the resource cache. */
88 void
89 virgl_resource_cache_flush(struct virgl_resource_cache *cache);
90
91 static inline void
virgl_resource_cache_entry_init(struct virgl_resource_cache_entry * entry,uint32_t size,uint32_t bind,uint32_t format,uint32_t flags)92 virgl_resource_cache_entry_init(struct virgl_resource_cache_entry *entry,
93 uint32_t size, uint32_t bind,
94 uint32_t format, uint32_t flags)
95 {
96 entry->size = size;
97 entry->bind = bind;
98 entry->format = format;
99 entry->flags = flags;
100 }
101
102 #endif
103