• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <zackr@vmware.com>
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 struct cso_cache {
40    struct cso_hash *hashes[CSO_CACHE_MAX];
41    int    max_size;
42 
43    cso_sanitize_callback sanitize_cb;
44    void                 *sanitize_data;
45 };
46 
47 #if 1
hash_key(const void * key,unsigned key_size)48 static unsigned hash_key(const void *key, unsigned key_size)
49 {
50    unsigned *ikey = (unsigned *)key;
51    unsigned hash = 0, i;
52 
53    assert(key_size % 4 == 0);
54 
55    /* I'm sure this can be improved on:
56     */
57    for (i = 0; i < key_size/4; i++) {
58       hash = (hash << 7) | (hash >> 25);
59       hash ^= ikey[i];
60    }
61 
62    return hash;
63 }
64 #else
hash_key(const unsigned char * p,int n)65 static unsigned hash_key(const unsigned char *p, int n)
66 {
67    unsigned h = 0;
68    unsigned g;
69 
70    while (n--) {
71       h = (h << 4) + *p++;
72       if ((g = (h & 0xf0000000)) != 0)
73          h ^= g >> 23;
74       h &= ~g;
75    }
76    return h;
77 }
78 #endif
79 
cso_construct_key(void * item,int item_size)80 unsigned cso_construct_key(void *item, int item_size)
81 {
82    return hash_key((item), item_size);
83 }
84 
_cso_hash_for_type(struct cso_cache * sc,enum cso_cache_type type)85 static inline struct cso_hash *_cso_hash_for_type(struct cso_cache *sc, enum cso_cache_type type)
86 {
87    struct cso_hash *hash;
88    hash = sc->hashes[type];
89    return hash;
90 }
91 
delete_blend_state(void * state,UNUSED void * data)92 static void delete_blend_state(void *state, UNUSED void *data)
93 {
94    struct cso_blend *cso = (struct cso_blend *)state;
95    if (cso->delete_state)
96       cso->delete_state(cso->context, cso->data);
97    FREE(state);
98 }
99 
delete_depth_stencil_state(void * state,UNUSED void * data)100 static void delete_depth_stencil_state(void *state, UNUSED void *data)
101 {
102    struct cso_depth_stencil_alpha *cso = (struct cso_depth_stencil_alpha *)state;
103    if (cso->delete_state)
104       cso->delete_state(cso->context, cso->data);
105    FREE(state);
106 }
107 
delete_sampler_state(void * state,UNUSED void * data)108 static void delete_sampler_state(void *state, UNUSED void *data)
109 {
110    struct cso_sampler *cso = (struct cso_sampler *)state;
111    if (cso->delete_state)
112       cso->delete_state(cso->context, cso->data);
113    FREE(state);
114 }
115 
delete_rasterizer_state(void * state,UNUSED void * data)116 static void delete_rasterizer_state(void *state, UNUSED void *data)
117 {
118    struct cso_rasterizer *cso = (struct cso_rasterizer *)state;
119    if (cso->delete_state)
120       cso->delete_state(cso->context, cso->data);
121    FREE(state);
122 }
123 
delete_velements(void * state,UNUSED void * data)124 static void delete_velements(void *state, UNUSED void *data)
125 {
126    struct cso_velements *cso = (struct cso_velements *)state;
127    if (cso->delete_state)
128       cso->delete_state(cso->context, cso->data);
129    FREE(state);
130 }
131 
delete_cso(void * state,enum cso_cache_type type)132 static inline void delete_cso(void *state, enum cso_cache_type type)
133 {
134    switch (type) {
135    case CSO_BLEND:
136       delete_blend_state(state, 0);
137       break;
138    case CSO_SAMPLER:
139       delete_sampler_state(state, 0);
140       break;
141    case CSO_DEPTH_STENCIL_ALPHA:
142       delete_depth_stencil_state(state, 0);
143       break;
144    case CSO_RASTERIZER:
145       delete_rasterizer_state(state, 0);
146       break;
147    case CSO_VELEMENTS:
148       delete_velements(state, 0);
149       break;
150    default:
151       assert(0);
152       FREE(state);
153    }
154 }
155 
156 
sanitize_hash(struct cso_cache * sc,struct cso_hash * hash,enum cso_cache_type type,int max_size)157 static inline void sanitize_hash(struct cso_cache *sc,
158                                  struct cso_hash *hash,
159                                  enum cso_cache_type type,
160                                  int max_size)
161 {
162    if (sc->sanitize_cb)
163       sc->sanitize_cb(hash, type, max_size, sc->sanitize_data);
164 }
165 
166 
sanitize_cb(struct cso_hash * hash,enum cso_cache_type type,int max_size,UNUSED void * user_data)167 static inline void sanitize_cb(struct cso_hash *hash, enum cso_cache_type type,
168                                int max_size, UNUSED void *user_data)
169 {
170    /* if we're approach the maximum size, remove fourth of the entries
171     * otherwise every subsequent call will go through the same */
172    int hash_size = cso_hash_size(hash);
173    int max_entries = (max_size > hash_size) ? max_size : hash_size;
174    int to_remove =  (max_size < max_entries) * max_entries/4;
175    if (hash_size > max_size)
176       to_remove += hash_size - max_size;
177    while (to_remove) {
178       /*remove elements until we're good */
179       /*fixme: currently we pick the nodes to remove at random*/
180       struct cso_hash_iter iter = cso_hash_first_node(hash);
181       void  *cso = cso_hash_take(hash, cso_hash_iter_key(iter));
182       delete_cso(cso, type);
183       --to_remove;
184    }
185 }
186 
187 struct cso_hash_iter
cso_insert_state(struct cso_cache * sc,unsigned hash_key,enum cso_cache_type type,void * state)188 cso_insert_state(struct cso_cache *sc,
189                  unsigned hash_key, enum cso_cache_type type,
190                  void *state)
191 {
192    struct cso_hash *hash = _cso_hash_for_type(sc, type);
193    sanitize_hash(sc, hash, type, sc->max_size);
194 
195    return cso_hash_insert(hash, hash_key, state);
196 }
197 
198 struct cso_hash_iter
cso_find_state(struct cso_cache * sc,unsigned hash_key,enum cso_cache_type type)199 cso_find_state(struct cso_cache *sc,
200                unsigned hash_key, enum cso_cache_type type)
201 {
202    struct cso_hash *hash = _cso_hash_for_type(sc, type);
203 
204    return cso_hash_find(hash, hash_key);
205 }
206 
207 
cso_hash_find_data_from_template(struct cso_hash * hash,unsigned hash_key,void * templ,int size)208 void *cso_hash_find_data_from_template( struct cso_hash *hash,
209 				        unsigned hash_key,
210 				        void *templ,
211 				        int size )
212 {
213    struct cso_hash_iter iter = cso_hash_find(hash, hash_key);
214    while (!cso_hash_iter_is_null(iter)) {
215       void *iter_data = cso_hash_iter_data(iter);
216       if (!memcmp(iter_data, templ, size)) {
217 	 /* We found a match
218 	  */
219          return iter_data;
220       }
221       iter = cso_hash_iter_next(iter);
222    }
223    return NULL;
224 }
225 
226 
cso_find_state_template(struct cso_cache * sc,unsigned hash_key,enum cso_cache_type type,void * templ,unsigned size)227 struct cso_hash_iter cso_find_state_template(struct cso_cache *sc,
228                                              unsigned hash_key, enum cso_cache_type type,
229                                              void *templ, unsigned size)
230 {
231    struct cso_hash_iter iter = cso_find_state(sc, hash_key, type);
232    while (!cso_hash_iter_is_null(iter)) {
233       void *iter_data = cso_hash_iter_data(iter);
234       if (!memcmp(iter_data, templ, size))
235          return iter;
236       iter = cso_hash_iter_next(iter);
237    }
238    return iter;
239 }
240 
cso_take_state(struct cso_cache * sc,unsigned hash_key,enum cso_cache_type type)241 void * cso_take_state(struct cso_cache *sc,
242                       unsigned hash_key, enum cso_cache_type type)
243 {
244    struct cso_hash *hash = _cso_hash_for_type(sc, type);
245    return cso_hash_take(hash, hash_key);
246 }
247 
cso_cache_create(void)248 struct cso_cache *cso_cache_create(void)
249 {
250    struct cso_cache *sc = MALLOC_STRUCT(cso_cache);
251    int i;
252    if (sc == NULL)
253       return NULL;
254 
255    sc->max_size           = 4096;
256    for (i = 0; i < CSO_CACHE_MAX; i++)
257       sc->hashes[i] = cso_hash_create();
258 
259    sc->sanitize_cb        = sanitize_cb;
260    sc->sanitize_data      = 0;
261 
262    return sc;
263 }
264 
cso_for_each_state(struct cso_cache * sc,enum cso_cache_type type,cso_state_callback func,void * user_data)265 void cso_for_each_state(struct cso_cache *sc, enum cso_cache_type type,
266                         cso_state_callback func, void *user_data)
267 {
268    struct cso_hash *hash = _cso_hash_for_type(sc, type);
269    struct cso_hash_iter iter;
270 
271    iter = cso_hash_first_node(hash);
272    while (!cso_hash_iter_is_null(iter)) {
273       void *state = cso_hash_iter_data(iter);
274       iter = cso_hash_iter_next(iter);
275       if (state) {
276          func(state, user_data);
277       }
278    }
279 }
280 
cso_cache_delete(struct cso_cache * sc)281 void cso_cache_delete(struct cso_cache *sc)
282 {
283    int i;
284    assert(sc);
285 
286    if (!sc)
287       return;
288 
289    /* delete driver data */
290    cso_for_each_state(sc, CSO_BLEND, delete_blend_state, 0);
291    cso_for_each_state(sc, CSO_DEPTH_STENCIL_ALPHA, delete_depth_stencil_state, 0);
292    cso_for_each_state(sc, CSO_RASTERIZER, delete_rasterizer_state, 0);
293    cso_for_each_state(sc, CSO_SAMPLER, delete_sampler_state, 0);
294    cso_for_each_state(sc, CSO_VELEMENTS, delete_velements, 0);
295 
296    for (i = 0; i < CSO_CACHE_MAX; i++)
297       cso_hash_delete(sc->hashes[i]);
298 
299    FREE(sc);
300 }
301 
cso_set_maximum_cache_size(struct cso_cache * sc,int number)302 void cso_set_maximum_cache_size(struct cso_cache *sc, int number)
303 {
304    int i;
305 
306    sc->max_size = number;
307 
308    for (i = 0; i < CSO_CACHE_MAX; i++)
309       sanitize_hash(sc, sc->hashes[i], i, sc->max_size);
310 }
311 
cso_maximum_cache_size(const struct cso_cache * sc)312 int cso_maximum_cache_size(const struct cso_cache *sc)
313 {
314    return sc->max_size;
315 }
316 
cso_cache_set_sanitize_callback(struct cso_cache * sc,cso_sanitize_callback cb,void * user_data)317 void cso_cache_set_sanitize_callback(struct cso_cache *sc,
318                                      cso_sanitize_callback cb,
319                                      void *user_data)
320 {
321    sc->sanitize_cb   = cb;
322    sc->sanitize_data = user_data;
323 }
324 
325