1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /* Authors: Zack Rusin <[email protected]>
29 */
30
31 #include "util/u_debug.h"
32
33 #include "util/u_memory.h"
34
35 #include "cso_cache.h"
36 #include "cso_hash.h"
37
38
39 /* Default delete callback. It can also be used by custom callbacks. */
40 void
cso_delete_state(struct pipe_context * pipe,void * state,enum cso_cache_type type)41 cso_delete_state(struct pipe_context *pipe, void *state,
42 enum cso_cache_type type)
43 {
44 switch (type) {
45 case CSO_BLEND:
46 pipe->delete_blend_state(pipe, ((struct cso_blend*)state)->data);
47 break;
48 case CSO_SAMPLER:
49 pipe->delete_sampler_state(pipe, ((struct cso_sampler*)state)->data);
50 break;
51 case CSO_DEPTH_STENCIL_ALPHA:
52 pipe->delete_depth_stencil_alpha_state(pipe,
53 ((struct cso_depth_stencil_alpha*)state)->data);
54 break;
55 case CSO_RASTERIZER:
56 pipe->delete_rasterizer_state(pipe, ((struct cso_rasterizer*)state)->data);
57 break;
58 case CSO_VELEMENTS:
59 pipe->delete_vertex_elements_state(pipe, ((struct cso_velements*)state)->data);
60 break;
61 default:
62 assert(0);
63 }
64
65 FREE(state);
66 }
67
68
69 static inline void
sanitize_hash(struct cso_cache * sc,struct cso_hash * hash,enum cso_cache_type type,int max_size)70 sanitize_hash(struct cso_cache *sc,
71 struct cso_hash *hash,
72 enum cso_cache_type type,
73 int max_size)
74 {
75 if (sc->sanitize_cb)
76 sc->sanitize_cb(hash, type, max_size, sc->sanitize_data);
77 }
78
79
80 static inline void
sanitize_cb(struct cso_hash * hash,enum cso_cache_type type,int max_size,void * user_data)81 sanitize_cb(struct cso_hash *hash, enum cso_cache_type type,
82 int max_size, void *user_data)
83 {
84 struct cso_cache *cache = (struct cso_cache *)user_data;
85
86 /* if we're approach the maximum size, remove fourth of the entries
87 * otherwise every subsequent call will go through the same */
88 int hash_size = cso_hash_size(hash);
89 int max_entries = (max_size > hash_size) ? max_size : hash_size;
90 int to_remove = (max_size < max_entries) * max_entries/4;
91 if (hash_size > max_size)
92 to_remove += hash_size - max_size;
93 while (to_remove) {
94 /*remove elements until we're good */
95 /*fixme: currently we pick the nodes to remove at random*/
96 struct cso_hash_iter iter = cso_hash_first_node(hash);
97 void *cso = cso_hash_take(hash, cso_hash_iter_key(iter));
98 cache->delete_cso(cache->delete_cso_ctx, cso, type);
99 --to_remove;
100 }
101 }
102
103
104 struct cso_hash_iter
cso_insert_state(struct cso_cache * sc,unsigned hash_key,enum cso_cache_type type,void * state)105 cso_insert_state(struct cso_cache *sc,
106 unsigned hash_key, enum cso_cache_type type,
107 void *state)
108 {
109 struct cso_hash *hash = &sc->hashes[type];
110 sanitize_hash(sc, hash, type, sc->max_size);
111 return cso_hash_insert(hash, hash_key, state);
112 }
113
114
115 void *
cso_hash_find_data_from_template(struct cso_hash * hash,unsigned hash_key,void * templ,int size)116 cso_hash_find_data_from_template(struct cso_hash *hash,
117 unsigned hash_key,
118 void *templ,
119 int size )
120 {
121 struct cso_hash_iter iter = cso_hash_find(hash, hash_key);
122 while (!cso_hash_iter_is_null(iter)) {
123 void *iter_data = cso_hash_iter_data(iter);
124 if (!memcmp(iter_data, templ, size)) {
125 /* We found a match */
126 return iter_data;
127 }
128 iter = cso_hash_iter_next(iter);
129 }
130 return NULL;
131 }
132
133
134 void
cso_cache_init(struct cso_cache * sc,struct pipe_context * pipe)135 cso_cache_init(struct cso_cache *sc, struct pipe_context *pipe)
136 {
137 memset(sc, 0, sizeof(*sc));
138
139 sc->max_size = 4096;
140 for (int i = 0; i < CSO_CACHE_MAX; i++)
141 cso_hash_init(&sc->hashes[i]);
142
143 sc->sanitize_cb = sanitize_cb;
144 sc->sanitize_data = sc;
145 sc->delete_cso = (cso_delete_cso_callback)cso_delete_state;
146 sc->delete_cso_ctx = pipe;
147 }
148
149
150 static void
cso_delete_all(struct cso_cache * sc,enum cso_cache_type type)151 cso_delete_all(struct cso_cache *sc, enum cso_cache_type type)
152 {
153 struct cso_hash *hash = &sc->hashes[type];
154 struct cso_hash_iter iter = cso_hash_first_node(hash);
155 while (!cso_hash_iter_is_null(iter)) {
156 void *state = cso_hash_iter_data(iter);
157 iter = cso_hash_iter_next(iter);
158 if (state) {
159 sc->delete_cso(sc->delete_cso_ctx, state, type);
160 }
161 }
162 }
163
164
165 void
cso_cache_delete(struct cso_cache * sc)166 cso_cache_delete(struct cso_cache *sc)
167 {
168 /* delete driver data */
169 cso_delete_all(sc, CSO_BLEND);
170 cso_delete_all(sc, CSO_DEPTH_STENCIL_ALPHA);
171 cso_delete_all(sc, CSO_RASTERIZER);
172 cso_delete_all(sc, CSO_SAMPLER);
173 cso_delete_all(sc, CSO_VELEMENTS);
174
175 for (int i = 0; i < CSO_CACHE_MAX; i++)
176 cso_hash_deinit(&sc->hashes[i]);
177 }
178
179
180 void
cso_set_maximum_cache_size(struct cso_cache * sc,int number)181 cso_set_maximum_cache_size(struct cso_cache *sc, int number)
182 {
183 sc->max_size = number;
184
185 for (int i = 0; i < CSO_CACHE_MAX; i++)
186 sanitize_hash(sc, &sc->hashes[i], i, sc->max_size);
187 }
188
189
190 void
cso_cache_set_sanitize_callback(struct cso_cache * sc,cso_sanitize_callback cb,void * user_data)191 cso_cache_set_sanitize_callback(struct cso_cache *sc,
192 cso_sanitize_callback cb,
193 void *user_data)
194 {
195 sc->sanitize_cb = cb;
196 sc->sanitize_data = user_data;
197 }
198
199
200 void
cso_cache_set_delete_cso_callback(struct cso_cache * sc,cso_delete_cso_callback delete_cso,void * ctx)201 cso_cache_set_delete_cso_callback(struct cso_cache *sc,
202 cso_delete_cso_callback delete_cso,
203 void *ctx)
204 {
205 sc->delete_cso = delete_cso;
206 sc->delete_cso_ctx = ctx;
207 }
208