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_math.h"
27 #include "util/u_memory.h"
28 #include "util/crc32.h"
30 #include "svga_debug.h"
31 #include "svga_format.h"
32 #include "svga_winsys.h"
33 #include "svga_screen.h"
34 #include "svga_screen_cache.h"
35 #include "svga_context.h"
38 #define SVGA_SURFACE_CACHE_ENABLED 1
42 * Return the size of the surface described by the key (in bytes).
45 surface_size(const struct svga_host_surface_cache_key
*key
)
47 unsigned bw
, bh
, bpb
, total_size
, i
;
49 assert(key
->numMipLevels
> 0);
50 assert(key
->numFaces
> 0);
51 assert(key
->arraySize
> 0);
53 if (key
->format
== SVGA3D_BUFFER
) {
54 /* Special case: we don't want to count vertex/index buffers
55 * against the cache size limit, so view them as zero-sized.
60 svga_format_size(key
->format
, &bw
, &bh
, &bpb
);
64 for (i
= 0; i
< key
->numMipLevels
; i
++) {
65 unsigned w
= u_minify(key
->size
.width
, i
);
66 unsigned h
= u_minify(key
->size
.height
, i
);
67 unsigned d
= u_minify(key
->size
.depth
, i
);
68 unsigned img_size
= ((w
+ bw
- 1) / bw
) * ((h
+ bh
- 1) / bh
) * d
* bpb
;
69 total_size
+= img_size
;
72 total_size
*= key
->numFaces
* key
->arraySize
* MAX2(1, key
->sampleCount
);
79 * Compute the bucket for this key.
81 static inline unsigned
82 svga_screen_cache_bucket(const struct svga_host_surface_cache_key
*key
)
84 return util_hash_crc32(key
, sizeof *key
) % SVGA_HOST_SURFACE_CACHE_BUCKETS
;
89 * Search the cache for a surface that matches the key. If a match is
90 * found, remove it from the cache and return the surface pointer.
91 * Return NULL otherwise.
93 static struct svga_winsys_surface
*
94 svga_screen_cache_lookup(struct svga_screen
*svgascreen
,
95 const struct svga_host_surface_cache_key
*key
)
97 struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
98 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
99 struct svga_host_surface_cache_entry
*entry
;
100 struct svga_winsys_surface
*handle
= NULL
;
101 struct list_head
*curr
, *next
;
105 assert(key
->cachable
);
107 bucket
= svga_screen_cache_bucket(key
);
109 mtx_lock(&cache
->mutex
);
111 curr
= cache
->bucket
[bucket
].next
;
113 while (curr
!= &cache
->bucket
[bucket
]) {
116 entry
= LIST_ENTRY(struct svga_host_surface_cache_entry
, curr
, bucket_head
);
118 assert(entry
->handle
);
120 /* If the key matches and the fence is signalled (the surface is no
121 * longer needed) the lookup was successful. We found a surface that
123 * We unlink the surface from the cache entry and we add the entry to
126 if (memcmp(&entry
->key
, key
, sizeof *key
) == 0 &&
127 sws
->fence_signalled(sws
, entry
->fence
, 0) == 0) {
130 assert(sws
->surface_is_flushed(sws
, entry
->handle
));
132 handle
= entry
->handle
; /* Reference is transfered here. */
133 entry
->handle
= NULL
;
135 /* Remove from hash table */
136 list_del(&entry
->bucket_head
);
138 /* remove from LRU list */
139 list_del(&entry
->head
);
141 /* Add the cache entry (but not the surface!) to the empty list */
142 list_add(&entry
->head
, &cache
->empty
);
144 /* update the cache size */
145 surf_size
= surface_size(&entry
->key
);
146 assert(surf_size
<= cache
->total_size
);
147 if (surf_size
> cache
->total_size
)
148 cache
->total_size
= 0; /* should never happen, but be safe */
150 cache
->total_size
-= surf_size
;
159 mtx_unlock(&cache
->mutex
);
161 if (SVGA_DEBUG
& DEBUG_DMA
)
162 debug_printf("%s: cache %s after %u tries (bucket %d)\n", __FUNCTION__
,
163 handle
? "hit" : "miss", tries
, bucket
);
170 * Free the least recently used entries in the surface cache until the
171 * cache size is <= the target size OR there are no unused entries left
172 * to discard. We don't do any flushing to try to free up additional
176 svga_screen_cache_shrink(struct svga_screen
*svgascreen
,
177 unsigned target_size
)
179 struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
180 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
181 struct svga_host_surface_cache_entry
*entry
= NULL
, *next_entry
;
183 /* Walk over the list of unused buffers in reverse order: from oldest
186 LIST_FOR_EACH_ENTRY_SAFE_REV(entry
, next_entry
, &cache
->unused
, head
) {
187 if (entry
->key
.format
!= SVGA3D_BUFFER
) {
188 /* we don't want to discard vertex/index buffers */
190 cache
->total_size
-= surface_size(&entry
->key
);
192 assert(entry
->handle
);
193 sws
->surface_reference(sws
, &entry
->handle
, NULL
);
195 list_del(&entry
->bucket_head
);
196 list_del(&entry
->head
);
197 list_add(&entry
->head
, &cache
->empty
);
199 if (cache
->total_size
<= target_size
) {
209 * Add a surface to the cache. This is done when the driver deletes
210 * the surface. Note: transfers a handle reference.
213 svga_screen_cache_add(struct svga_screen
*svgascreen
,
214 const struct svga_host_surface_cache_key
*key
,
215 struct svga_winsys_surface
**p_handle
)
217 struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
218 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
219 struct svga_host_surface_cache_entry
*entry
= NULL
;
220 struct svga_winsys_surface
*handle
= *p_handle
;
223 assert(key
->cachable
);
228 surf_size
= surface_size(key
);
231 mtx_lock(&cache
->mutex
);
233 if (surf_size
>= SVGA_HOST_SURFACE_CACHE_BYTES
) {
234 /* this surface is too large to cache, just free it */
235 sws
->surface_reference(sws
, &handle
, NULL
);
236 mtx_unlock(&cache
->mutex
);
240 if (cache
->total_size
+ surf_size
> SVGA_HOST_SURFACE_CACHE_BYTES
) {
241 /* Adding this surface would exceed the cache size.
242 * Try to discard least recently used entries until we hit the
243 * new target cache size.
245 unsigned target_size
= SVGA_HOST_SURFACE_CACHE_BYTES
- surf_size
;
247 svga_screen_cache_shrink(svgascreen
, target_size
);
249 if (cache
->total_size
> target_size
) {
250 /* we weren't able to shrink the cache as much as we wanted so
251 * just discard this surface.
253 sws
->surface_reference(sws
, &handle
, NULL
);
254 mtx_unlock(&cache
->mutex
);
259 if (!list_is_empty(&cache
->empty
)) {
260 /* An empty entry has no surface associated with it.
261 * Use the first empty entry.
263 entry
= LIST_ENTRY(struct svga_host_surface_cache_entry
,
264 cache
->empty
.next
, head
);
266 /* Remove from LRU list */
267 list_del(&entry
->head
);
269 else if (!list_is_empty(&cache
->unused
)) {
270 /* free the last used buffer and reuse its entry */
271 entry
= LIST_ENTRY(struct svga_host_surface_cache_entry
,
272 cache
->unused
.prev
, head
);
273 SVGA_DBG(DEBUG_CACHE
|DEBUG_DMA
,
274 "unref sid %p (make space)\n", entry
->handle
);
276 cache
->total_size
-= surface_size(&entry
->key
);
278 sws
->surface_reference(sws
, &entry
->handle
, NULL
);
280 /* Remove from hash table */
281 list_del(&entry
->bucket_head
);
283 /* Remove from LRU list */
284 list_del(&entry
->head
);
288 assert(entry
->handle
== NULL
);
289 entry
->handle
= handle
;
290 memcpy(&entry
->key
, key
, sizeof entry
->key
);
292 SVGA_DBG(DEBUG_CACHE
|DEBUG_DMA
,
293 "cache sid %p\n", entry
->handle
);
295 /* If we don't have gb objects, we don't need to invalidate. */
296 if (sws
->have_gb_objects
)
297 list_add(&entry
->head
, &cache
->validated
);
299 list_add(&entry
->head
, &cache
->invalidated
);
301 cache
->total_size
+= surf_size
;
304 /* Couldn't cache the buffer -- this really shouldn't happen */
305 SVGA_DBG(DEBUG_CACHE
|DEBUG_DMA
,
306 "unref sid %p (couldn't find space)\n", handle
);
307 sws
->surface_reference(sws
, &handle
, NULL
);
310 mtx_unlock(&cache
->mutex
);
315 * Called during the screen flush to move all buffers not in a validate list
316 * into the unused list.
319 svga_screen_cache_flush(struct svga_screen
*svgascreen
,
320 struct svga_context
*svga
,
321 struct pipe_fence_handle
*fence
)
323 struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
324 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
325 struct svga_host_surface_cache_entry
*entry
;
326 struct list_head
*curr
, *next
;
329 mtx_lock(&cache
->mutex
);
331 /* Loop over entries in the invalidated list */
332 curr
= cache
->invalidated
.next
;
334 while (curr
!= &cache
->invalidated
) {
335 entry
= LIST_ENTRY(struct svga_host_surface_cache_entry
, curr
, head
);
337 assert(entry
->handle
);
339 if (sws
->surface_is_flushed(sws
, entry
->handle
)) {
340 /* remove entry from the invalidated list */
341 list_del(&entry
->head
);
343 sws
->fence_reference(sws
, &entry
->fence
, fence
);
345 /* Add entry to the unused list */
346 list_add(&entry
->head
, &cache
->unused
);
348 /* Add entry to the hash table bucket */
349 bucket
= svga_screen_cache_bucket(&entry
->key
);
350 list_add(&entry
->bucket_head
, &cache
->bucket
[bucket
]);
357 curr
= cache
->validated
.next
;
359 while (curr
!= &cache
->validated
) {
360 entry
= LIST_ENTRY(struct svga_host_surface_cache_entry
, curr
, head
);
362 assert(entry
->handle
);
363 assert(svga_have_gb_objects(svga
));
365 if (sws
->surface_is_flushed(sws
, entry
->handle
)) {
366 /* remove entry from the validated list */
367 list_del(&entry
->head
);
369 /* It is now safe to invalidate the surface content.
370 * It will be done using the current context.
372 if (SVGA3D_InvalidateGBSurface(svga
->swc
, entry
->handle
) != PIPE_OK
) {
373 ASSERTED
enum pipe_error ret
;
375 /* Even though surface invalidation here is done after the command
376 * buffer is flushed, it is still possible that it will
377 * fail because there might be just enough of this command that is
378 * filling up the command buffer, so in this case we will call
379 * the winsys flush directly to flush the buffer.
380 * Note, we don't want to call svga_context_flush() here because
381 * this function itself is called inside svga_context_flush().
383 svga
->swc
->flush(svga
->swc
, NULL
);
384 ret
= SVGA3D_InvalidateGBSurface(svga
->swc
, entry
->handle
);
385 assert(ret
== PIPE_OK
);
388 /* add the entry to the invalidated list */
389 list_add(&entry
->head
, &cache
->invalidated
);
396 mtx_unlock(&cache
->mutex
);
401 * Free all the surfaces in the cache.
402 * Called when destroying the svga screen object.
405 svga_screen_cache_cleanup(struct svga_screen
*svgascreen
)
407 struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
408 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
411 for (i
= 0; i
< SVGA_HOST_SURFACE_CACHE_SIZE
; ++i
) {
412 if (cache
->entries
[i
].handle
) {
413 SVGA_DBG(DEBUG_CACHE
|DEBUG_DMA
,
414 "unref sid %p (shutdown)\n", cache
->entries
[i
].handle
);
415 sws
->surface_reference(sws
, &cache
->entries
[i
].handle
, NULL
);
417 cache
->total_size
-= surface_size(&cache
->entries
[i
].key
);
420 if (cache
->entries
[i
].fence
)
421 sws
->fence_reference(sws
, &cache
->entries
[i
].fence
, NULL
);
424 mtx_destroy(&cache
->mutex
);
429 svga_screen_cache_init(struct svga_screen
*svgascreen
)
431 struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
434 assert(cache
->total_size
== 0);
436 (void) mtx_init(&cache
->mutex
, mtx_plain
);
438 for (i
= 0; i
< SVGA_HOST_SURFACE_CACHE_BUCKETS
; ++i
)
439 list_inithead(&cache
->bucket
[i
]);
441 list_inithead(&cache
->unused
);
443 list_inithead(&cache
->validated
);
445 list_inithead(&cache
->invalidated
);
447 list_inithead(&cache
->empty
);
448 for (i
= 0; i
< SVGA_HOST_SURFACE_CACHE_SIZE
; ++i
)
449 list_addtail(&cache
->entries
[i
].head
, &cache
->empty
);
456 * Allocate a new host-side surface. If the surface is marked as cachable,
457 * first try re-using a surface in the cache of freed surfaces. Otherwise,
458 * allocate a new surface.
459 * \param bind_flags bitmask of PIPE_BIND_x flags
460 * \param usage one of PIPE_USAGE_x values
461 * \param validated return True if the surface is a reused surface
463 struct svga_winsys_surface
*
464 svga_screen_surface_create(struct svga_screen
*svgascreen
,
465 unsigned bind_flags
, enum pipe_resource_usage usage
,
467 struct svga_host_surface_cache_key
*key
)
469 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
470 struct svga_winsys_surface
*handle
= NULL
;
471 boolean cachable
= SVGA_SURFACE_CACHE_ENABLED
&& key
->cachable
;
473 SVGA_DBG(DEBUG_CACHE
|DEBUG_DMA
,
474 "%s sz %dx%dx%d mips %d faces %d arraySize %d cachable %d\n",
485 /* Try to re-cycle a previously freed, cached surface */
486 if (key
->format
== SVGA3D_BUFFER
) {
487 SVGA3dSurfaceAllFlags hint_flag
;
489 /* For buffers, round the buffer size up to the nearest power
490 * of two to increase the probability of cache hits. Keep
491 * texture surface dimensions unchanged.
494 while (size
< key
->size
.width
)
496 key
->size
.width
= size
;
498 /* Determine whether the buffer is static or dynamic.
499 * This is a bit of a heuristic which can be tuned as needed.
501 if (usage
== PIPE_USAGE_DEFAULT
||
502 usage
== PIPE_USAGE_IMMUTABLE
) {
503 hint_flag
= SVGA3D_SURFACE_HINT_STATIC
;
505 else if (bind_flags
& PIPE_BIND_INDEX_BUFFER
) {
506 /* Index buffers don't change too often. Mark them as static.
508 hint_flag
= SVGA3D_SURFACE_HINT_STATIC
;
511 /* Since we're reusing buffers we're effectively transforming all
512 * of them into dynamic buffers.
514 * It would be nice to not cache long lived static buffers. But there
515 * is no way to detect the long lived from short lived ones yet. A
516 * good heuristic would be buffer size.
518 hint_flag
= SVGA3D_SURFACE_HINT_DYNAMIC
;
521 key
->flags
&= ~(SVGA3D_SURFACE_HINT_STATIC
|
522 SVGA3D_SURFACE_HINT_DYNAMIC
);
523 key
->flags
|= hint_flag
;
526 handle
= svga_screen_cache_lookup(svgascreen
, key
);
528 if (key
->format
== SVGA3D_BUFFER
)
529 SVGA_DBG(DEBUG_CACHE
|DEBUG_DMA
,
530 "reuse sid %p sz %d (buffer)\n", handle
,
533 SVGA_DBG(DEBUG_CACHE
|DEBUG_DMA
,
534 "reuse sid %p sz %dx%dx%d mips %d faces %d arraySize %d\n", handle
,
546 /* Unable to recycle surface, allocate a new one */
550 usage
|= SVGA_SURFACE_USAGE_SHARED
;
552 usage
|= SVGA_SURFACE_USAGE_SCANOUT
;
554 usage
|= SVGA_SURFACE_USAGE_COHERENT
;
556 handle
= sws
->surface_create(sws
,
561 key
->numFaces
* key
->arraySize
,
565 SVGA_DBG(DEBUG_CACHE
|DEBUG_DMA
,
566 " CREATE sid %p sz %dx%dx%d\n",
580 * Release a surface. We don't actually free the surface- we put
581 * it into the cache of freed surfaces (if it's cachable).
584 svga_screen_surface_destroy(struct svga_screen
*svgascreen
,
585 const struct svga_host_surface_cache_key
*key
,
586 struct svga_winsys_surface
**p_handle
)
588 struct svga_winsys_screen
*sws
= svgascreen
->sws
;
590 /* We only set the cachable flag for surfaces of which we are the
591 * exclusive owner. So just hold onto our existing reference in
594 if (SVGA_SURFACE_CACHE_ENABLED
&& key
->cachable
) {
595 svga_screen_cache_add(svgascreen
, key
, p_handle
);
599 "unref sid %p (uncachable)\n", *p_handle
);
600 sws
->surface_reference(sws
, p_handle
, NULL
);
606 * Print/dump the contents of the screen cache. For debugging.
609 svga_screen_cache_dump(const struct svga_screen
*svgascreen
)
611 const struct svga_host_surface_cache
*cache
= &svgascreen
->cache
;
615 debug_printf("svga3d surface cache:\n");
616 for (bucket
= 0; bucket
< SVGA_HOST_SURFACE_CACHE_BUCKETS
; bucket
++) {
617 struct list_head
*curr
;
618 curr
= cache
->bucket
[bucket
].next
;
619 while (curr
&& curr
!= &cache
->bucket
[bucket
]) {
620 struct svga_host_surface_cache_entry
*entry
=
621 LIST_ENTRY(struct svga_host_surface_cache_entry
,
623 if (entry
->key
.format
== SVGA3D_BUFFER
) {
624 debug_printf(" %p: buffer %u bytes\n",
626 entry
->key
.size
.width
);
629 debug_printf(" %p: %u x %u x %u format %u\n",
631 entry
->key
.size
.width
,
632 entry
->key
.size
.height
,
633 entry
->key
.size
.depth
,
641 debug_printf("%u surfaces, %u bytes\n", count
, cache
->total_size
);