00001 /************************************************************************** 00002 * 00003 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. 00004 * All Rights Reserved. 00005 * 00006 * Permission is hereby granted, free of charge, to any person obtaining a 00007 * copy of this software and associated documentation files (the 00008 * "Software"), to deal in the Software without restriction, including 00009 * without limitation the rights to use, copy, modify, merge, publish, 00010 * distribute, sub license, and/or sell copies of the Software, and to 00011 * permit persons to whom the Software is furnished to do so, subject to 00012 * the following conditions: 00013 * 00014 * The above copyright notice and this permission notice (including the 00015 * next paragraph) shall be included in all copies or substantial portions 00016 * of the Software. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00019 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00020 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 00021 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 00022 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 00023 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 00024 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00025 * 00026 **************************************************************************/ 00027 00028 #include "util/u_memory.h" 00029 #include "pipe/p_state.h" 00030 #include "translate.h" 00031 #include "translate_cache.h" 00032 00033 #include "cso_cache/cso_cache.h" 00034 #include "cso_cache/cso_hash.h" 00035 00036 struct translate_cache { 00037 struct cso_hash *hash; 00038 }; 00039 00040 struct translate_cache * translate_cache_create( void ) 00041 { 00042 struct translate_cache *cache = MALLOC_STRUCT(translate_cache); 00043 cache->hash = cso_hash_create(); 00044 return cache; 00045 } 00046 00047 00048 static INLINE void delete_translates(struct translate_cache *cache) 00049 { 00050 struct cso_hash *hash = cache->hash; 00051 struct cso_hash_iter iter = cso_hash_first_node(hash); 00052 while (!cso_hash_iter_is_null(iter)) { 00053 struct translate *state = (struct translate*)cso_hash_iter_data(iter); 00054 iter = cso_hash_iter_next(iter); 00055 if (state) { 00056 state->release(state); 00057 } 00058 } 00059 } 00060 00061 void translate_cache_destroy(struct translate_cache *cache) 00062 { 00063 delete_translates(cache); 00064 cso_hash_delete(cache->hash); 00065 FREE(cache); 00066 } 00067 00068 00069 static INLINE unsigned translate_hash_key_size(struct translate_key *key) 00070 { 00071 unsigned size = sizeof(struct translate_key) - 00072 sizeof(struct translate_element) * (PIPE_MAX_ATTRIBS - key->nr_elements); 00073 return size; 00074 } 00075 00076 static INLINE unsigned create_key(struct translate_key *key) 00077 { 00078 unsigned hash_key; 00079 unsigned size = translate_hash_key_size(key); 00080 /*debug_printf("key size = %d, (els = %d)\n", 00081 size, key->nr_elements);*/ 00082 hash_key = cso_construct_key(key, size); 00083 return hash_key; 00084 } 00085 00086 struct translate * translate_cache_find(struct translate_cache *cache, 00087 struct translate_key *key) 00088 { 00089 unsigned hash_key = create_key(key); 00090 struct translate *translate = (struct translate*) 00091 cso_hash_find_data_from_template(cache->hash, 00092 hash_key, 00093 key, sizeof(*key)); 00094 00095 if (!translate) { 00096 /* create/insert */ 00097 translate = translate_create(key); 00098 cso_hash_insert(cache->hash, hash_key, translate); 00099 } 00100 00101 return translate; 00102 }