1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **********************************************************/
26 #include "util/u_memory.h"
28 #include "svga_debug.h"
29 #include "svga_winsys.h"
30 #include "svga_screen.h"
31 #include "svga_screen_cache.h"
34 #define SVGA_SURFACE_CACHE_ENABLED 1
38 * Compute the bucket for this key.
40 * We simply compute log2(width) for now, but
42 static INLINE
unsigned
43 svga_screen_cache_bucket(const struct svga_host_surface_cache_key
*key
)
46 unsigned size
= key
->size
.width
;
51 if(key
->flags
& SVGA3D_SURFACE_HINT_INDEXBUFFER
)
54 assert(bucket
< SVGA_HOST_SURFACE_CACHE_BUCKETS
);
60 static INLINE
struct svga_winsys_surface
*
61 svga_screen_cache_lookup(struct svga_screen
*svgascreen
,
62 const struct svga_host_surface_cache_key
*key
)
64 struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
65 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
66 struct svga_host_surface_cache_entry
*entry
;
67 struct svga_winsys_surface
*handle
= NULL
;
68 struct list_head
*curr
, *next
;
72 bucket
= svga_screen_cache_bucket(key
);
74 pipe_mutex_lock(cache
->mutex
);
76 curr
= cache
->bucket
[bucket
].next
;
78 while(curr
!= &cache
->bucket
[bucket
]) {
81 entry
= LIST_ENTRY(struct svga_host_surface_cache_entry
, curr
, bucket_head
);
83 assert(entry
->handle
);
85 if(memcmp(&entry
->key
, key
, sizeof *key
) == 0 &&
86 sws
->fence_signalled( sws
, entry
->fence
, 0 ) == 0) {
87 assert(sws
->surface_is_flushed(sws
, entry
->handle
));
89 handle
= entry
->handle
; // Reference is transfered here.
92 LIST_DEL(&entry
->bucket_head
);
94 LIST_DEL(&entry
->head
);
96 LIST_ADD(&entry
->head
, &cache
->empty
);
105 pipe_mutex_unlock(cache
->mutex
);
108 _debug_printf("%s: cache %s after %u tries\n", __FUNCTION__
, handle
? "hit" : "miss", tries
);
118 * Transfers a handle reference.
122 svga_screen_cache_add(struct svga_screen
*svgascreen
,
123 const struct svga_host_surface_cache_key
*key
,
124 struct svga_winsys_surface
**p_handle
)
126 struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
127 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
128 struct svga_host_surface_cache_entry
*entry
= NULL
;
129 struct svga_winsys_surface
*handle
= *p_handle
;
137 pipe_mutex_lock(cache
->mutex
);
139 if(!LIST_IS_EMPTY(&cache
->empty
)) {
140 /* use the first empty entry */
141 entry
= LIST_ENTRY(struct svga_host_surface_cache_entry
, cache
->empty
.next
, head
);
143 LIST_DEL(&entry
->head
);
145 else if(!LIST_IS_EMPTY(&cache
->unused
)) {
146 /* free the last used buffer and reuse its entry */
147 entry
= LIST_ENTRY(struct svga_host_surface_cache_entry
, cache
->unused
.prev
, head
);
148 SVGA_DBG(DEBUG_DMA
, "unref sid %p\n", entry
->handle
);
149 sws
->surface_reference(sws
, &entry
->handle
, NULL
);
151 LIST_DEL(&entry
->bucket_head
);
153 LIST_DEL(&entry
->head
);
157 entry
->handle
= handle
;
158 memcpy(&entry
->key
, key
, sizeof entry
->key
);
160 LIST_ADD(&entry
->head
, &cache
->validated
);
163 /* Couldn't cache the buffer -- this really shouldn't happen */
164 SVGA_DBG(DEBUG_DMA
, "unref sid %p\n", handle
);
165 sws
->surface_reference(sws
, &handle
, NULL
);
168 pipe_mutex_unlock(cache
->mutex
);
173 * Called during the screen flush to move all buffers not in a validate list
174 * into the unused list.
177 svga_screen_cache_flush(struct svga_screen
*svgascreen
,
178 struct pipe_fence_handle
*fence
)
180 struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
181 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
182 struct svga_host_surface_cache_entry
*entry
;
183 struct list_head
*curr
, *next
;
186 pipe_mutex_lock(cache
->mutex
);
188 curr
= cache
->validated
.next
;
190 while(curr
!= &cache
->validated
) {
191 entry
= LIST_ENTRY(struct svga_host_surface_cache_entry
, curr
, head
);
193 assert(entry
->handle
);
195 if(sws
->surface_is_flushed(sws
, entry
->handle
)) {
196 LIST_DEL(&entry
->head
);
198 svgascreen
->sws
->fence_reference(svgascreen
->sws
, &entry
->fence
, fence
);
200 LIST_ADD(&entry
->head
, &cache
->unused
);
202 bucket
= svga_screen_cache_bucket(&entry
->key
);
203 LIST_ADD(&entry
->bucket_head
, &cache
->bucket
[bucket
]);
210 pipe_mutex_unlock(cache
->mutex
);
215 svga_screen_cache_cleanup(struct svga_screen
*svgascreen
)
217 struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
218 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
221 for(i
= 0; i
< SVGA_HOST_SURFACE_CACHE_SIZE
; ++i
) {
222 if(cache
->entries
[i
].handle
) {
223 SVGA_DBG(DEBUG_DMA
, "unref sid %p\n", cache
->entries
[i
].handle
);
224 sws
->surface_reference(sws
, &cache
->entries
[i
].handle
, NULL
);
227 if(cache
->entries
[i
].fence
)
228 svgascreen
->sws
->fence_reference(svgascreen
->sws
, &cache
->entries
[i
].fence
, NULL
);
231 pipe_mutex_destroy(cache
->mutex
);
236 svga_screen_cache_init(struct svga_screen
*svgascreen
)
238 struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
241 pipe_mutex_init(cache
->mutex
);
243 for(i
= 0; i
< SVGA_HOST_SURFACE_CACHE_BUCKETS
; ++i
)
244 LIST_INITHEAD(&cache
->bucket
[i
]);
246 LIST_INITHEAD(&cache
->unused
);
248 LIST_INITHEAD(&cache
->validated
);
250 LIST_INITHEAD(&cache
->empty
);
251 for(i
= 0; i
< SVGA_HOST_SURFACE_CACHE_SIZE
; ++i
)
252 LIST_ADDTAIL(&cache
->entries
[i
].head
, &cache
->empty
);
258 struct svga_winsys_surface
*
259 svga_screen_surface_create(struct svga_screen
*svgascreen
,
260 struct svga_host_surface_cache_key
*key
)
262 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
263 struct svga_winsys_surface
*handle
= NULL
;
265 if (SVGA_SURFACE_CACHE_ENABLED
&& key
->format
== SVGA3D_BUFFER
) {
266 /* round the buffer size up to the nearest power of two to increase the
267 * probability of cache hits */
269 while(size
< key
->size
.width
)
271 key
->size
.width
= size
;
273 handle
= svga_screen_cache_lookup(svgascreen
, key
);
275 SVGA_DBG(DEBUG_DMA
, " reuse sid %p sz %d\n", handle
, size
);
279 handle
= sws
->surface_create(sws
,
286 SVGA_DBG(DEBUG_DMA
, "create sid %p sz %d\n", handle
, key
->size
);
294 svga_screen_surface_destroy(struct svga_screen
*svgascreen
,
295 const struct svga_host_surface_cache_key
*key
,
296 struct svga_winsys_surface
**p_handle
)
298 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
300 if(SVGA_SURFACE_CACHE_ENABLED
&& key
->format
== SVGA3D_BUFFER
) {
301 svga_screen_cache_add(svgascreen
, key
, p_handle
);
304 SVGA_DBG(DEBUG_DMA
, "unref sid %p\n", *p_handle
);
305 sws
->surface_reference(sws
, p_handle
, NULL
);