svga: update driver for version 10 GPU interface
[mesa.git] / src / gallium / drivers / svga / svga_screen_cache.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "util/u_math.h"
27 #include "util/u_memory.h"
28 #include "util/u_hash.h"
29
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
36
37 #define SVGA_SURFACE_CACHE_ENABLED 1
38
39
40 /**
41 * Return the size of the surface described by the key (in bytes).
42 */
43 static unsigned
44 surface_size(const struct svga_host_surface_cache_key *key)
45 {
46 unsigned bw, bh, bpb, total_size, i;
47
48 assert(key->numMipLevels > 0);
49 assert(key->numFaces > 0);
50
51 if (key->format == SVGA3D_BUFFER) {
52 /* Special case: we don't want to count vertex/index buffers
53 * against the cache size limit, so view them as zero-sized.
54 */
55 return 0;
56 }
57
58 svga_format_size(key->format, &bw, &bh, &bpb);
59
60 total_size = 0;
61
62 for (i = 0; i < key->numMipLevels; i++) {
63 unsigned w = u_minify(key->size.width, i);
64 unsigned h = u_minify(key->size.height, i);
65 unsigned d = u_minify(key->size.depth, i);
66 unsigned img_size = ((w + bw - 1) / bw) * ((h + bh - 1) / bh) * d * bpb;
67 total_size += img_size;
68 }
69
70 total_size *= key->numFaces;
71
72 return total_size;
73 }
74
75
76 /**
77 * Compute the bucket for this key.
78 */
79 static inline unsigned
80 svga_screen_cache_bucket(const struct svga_host_surface_cache_key *key)
81 {
82 return util_hash_crc32(key, sizeof *key) % SVGA_HOST_SURFACE_CACHE_BUCKETS;
83 }
84
85
86 /**
87 * Search the cache for a surface that matches the key. If a match is
88 * found, remove it from the cache and return the surface pointer.
89 * Return NULL otherwise.
90 */
91 static struct svga_winsys_surface *
92 svga_screen_cache_lookup(struct svga_screen *svgascreen,
93 const struct svga_host_surface_cache_key *key)
94 {
95 struct svga_host_surface_cache *cache = &svgascreen->cache;
96 struct svga_winsys_screen *sws = svgascreen->sws;
97 struct svga_host_surface_cache_entry *entry;
98 struct svga_winsys_surface *handle = NULL;
99 struct list_head *curr, *next;
100 unsigned bucket;
101 unsigned tries = 0;
102
103 assert(key->cachable);
104
105 bucket = svga_screen_cache_bucket(key);
106
107 pipe_mutex_lock(cache->mutex);
108
109 curr = cache->bucket[bucket].next;
110 next = curr->next;
111 while (curr != &cache->bucket[bucket]) {
112 ++tries;
113
114 entry = LIST_ENTRY(struct svga_host_surface_cache_entry, curr, bucket_head);
115
116 assert(entry->handle);
117
118 /* If the key matches and the fence is signalled (the surface is no
119 * longer needed) the lookup was successful. We found a surface that
120 * can be reused.
121 * We unlink the surface from the cache entry and we add the entry to
122 * the 'empty' list.
123 */
124 if (memcmp(&entry->key, key, sizeof *key) == 0 &&
125 sws->fence_signalled(sws, entry->fence, 0) == 0) {
126 unsigned surf_size;
127
128 assert(sws->surface_is_flushed(sws, entry->handle));
129
130 handle = entry->handle; /* Reference is transfered here. */
131 entry->handle = NULL;
132
133 /* Remove from hash table */
134 LIST_DEL(&entry->bucket_head);
135
136 /* remove from LRU list */
137 LIST_DEL(&entry->head);
138
139 /* Add the cache entry (but not the surface!) to the empty list */
140 LIST_ADD(&entry->head, &cache->empty);
141
142 /* update the cache size */
143 surf_size = surface_size(&entry->key);
144 assert(surf_size <= cache->total_size);
145 if (surf_size > cache->total_size)
146 cache->total_size = 0; /* should never happen, but be safe */
147 else
148 cache->total_size -= surf_size;
149
150 break;
151 }
152
153 curr = next;
154 next = curr->next;
155 }
156
157 pipe_mutex_unlock(cache->mutex);
158
159 if (SVGA_DEBUG & DEBUG_DMA)
160 debug_printf("%s: cache %s after %u tries (bucket %d)\n", __FUNCTION__,
161 handle ? "hit" : "miss", tries, bucket);
162
163 return handle;
164 }
165
166
167 /**
168 * Free the least recently used entries in the surface cache until the
169 * cache size is <= the target size OR there are no unused entries left
170 * to discard. We don't do any flushing to try to free up additional
171 * surfaces.
172 */
173 static void
174 svga_screen_cache_shrink(struct svga_screen *svgascreen,
175 unsigned target_size)
176 {
177 struct svga_host_surface_cache *cache = &svgascreen->cache;
178 struct svga_winsys_screen *sws = svgascreen->sws;
179 struct svga_host_surface_cache_entry *entry = NULL, *next_entry;
180
181 /* Walk over the list of unused buffers in reverse order: from oldest
182 * to newest.
183 */
184 LIST_FOR_EACH_ENTRY_SAFE_REV(entry, next_entry, &cache->unused, head) {
185 if (entry->key.format != SVGA3D_BUFFER) {
186 /* we don't want to discard vertex/index buffers */
187
188 cache->total_size -= surface_size(&entry->key);
189
190 assert(entry->handle);
191 sws->surface_reference(sws, &entry->handle, NULL);
192
193 LIST_DEL(&entry->bucket_head);
194 LIST_DEL(&entry->head);
195 LIST_ADD(&entry->head, &cache->empty);
196
197 if (cache->total_size <= target_size) {
198 /* all done */
199 break;
200 }
201 }
202 }
203 }
204
205
206 /**
207 * Add a surface to the cache. This is done when the driver deletes
208 * the surface. Note: transfers a handle reference.
209 */
210 static void
211 svga_screen_cache_add(struct svga_screen *svgascreen,
212 const struct svga_host_surface_cache_key *key,
213 struct svga_winsys_surface **p_handle)
214 {
215 struct svga_host_surface_cache *cache = &svgascreen->cache;
216 struct svga_winsys_screen *sws = svgascreen->sws;
217 struct svga_host_surface_cache_entry *entry = NULL;
218 struct svga_winsys_surface *handle = *p_handle;
219 unsigned surf_size;
220
221 assert(key->cachable);
222
223 if (!handle)
224 return;
225
226 surf_size = surface_size(key);
227
228 *p_handle = NULL;
229 pipe_mutex_lock(cache->mutex);
230
231 if (surf_size >= SVGA_HOST_SURFACE_CACHE_BYTES) {
232 /* this surface is too large to cache, just free it */
233 sws->surface_reference(sws, &handle, NULL);
234 pipe_mutex_unlock(cache->mutex);
235 return;
236 }
237
238 if (cache->total_size + surf_size > SVGA_HOST_SURFACE_CACHE_BYTES) {
239 /* Adding this surface would exceed the cache size.
240 * Try to discard least recently used entries until we hit the
241 * new target cache size.
242 */
243 unsigned target_size = SVGA_HOST_SURFACE_CACHE_BYTES - surf_size;
244
245 svga_screen_cache_shrink(svgascreen, target_size);
246
247 if (cache->total_size > target_size) {
248 /* we weren't able to shrink the cache as much as we wanted so
249 * just discard this surface.
250 */
251 sws->surface_reference(sws, &handle, NULL);
252 pipe_mutex_unlock(cache->mutex);
253 return;
254 }
255 }
256
257 if (!LIST_IS_EMPTY(&cache->empty)) {
258 /* An empty entry has no surface associated with it.
259 * Use the first empty entry.
260 */
261 entry = LIST_ENTRY(struct svga_host_surface_cache_entry,
262 cache->empty.next, head);
263
264 /* Remove from LRU list */
265 LIST_DEL(&entry->head);
266 }
267 else if (!LIST_IS_EMPTY(&cache->unused)) {
268 /* free the last used buffer and reuse its entry */
269 entry = LIST_ENTRY(struct svga_host_surface_cache_entry,
270 cache->unused.prev, head);
271 SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
272 "unref sid %p (make space)\n", entry->handle);
273
274 cache->total_size -= surface_size(&entry->key);
275
276 sws->surface_reference(sws, &entry->handle, NULL);
277
278 /* Remove from hash table */
279 LIST_DEL(&entry->bucket_head);
280
281 /* Remove from LRU list */
282 LIST_DEL(&entry->head);
283 }
284
285 if (entry) {
286 assert(entry->handle == NULL);
287 entry->handle = handle;
288 memcpy(&entry->key, key, sizeof entry->key);
289
290 SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
291 "cache sid %p\n", entry->handle);
292 LIST_ADD(&entry->head, &cache->validated);
293
294 cache->total_size += surf_size;
295 }
296 else {
297 /* Couldn't cache the buffer -- this really shouldn't happen */
298 SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
299 "unref sid %p (couldn't find space)\n", handle);
300 sws->surface_reference(sws, &handle, NULL);
301 }
302
303 pipe_mutex_unlock(cache->mutex);
304 }
305
306
307 /**
308 * Called during the screen flush to move all buffers not in a validate list
309 * into the unused list.
310 */
311 void
312 svga_screen_cache_flush(struct svga_screen *svgascreen,
313 struct pipe_fence_handle *fence)
314 {
315 struct svga_host_surface_cache *cache = &svgascreen->cache;
316 struct svga_winsys_screen *sws = svgascreen->sws;
317 struct svga_host_surface_cache_entry *entry;
318 struct list_head *curr, *next;
319 unsigned bucket;
320
321 pipe_mutex_lock(cache->mutex);
322
323 /* Loop over entries in the validated list */
324 curr = cache->validated.next;
325 next = curr->next;
326 while (curr != &cache->validated) {
327 entry = LIST_ENTRY(struct svga_host_surface_cache_entry, curr, head);
328
329 assert(entry->handle);
330
331 if (sws->surface_is_flushed(sws, entry->handle)) {
332 /* remove entry from LRU list */
333 LIST_DEL(&entry->head);
334
335 svgascreen->sws->fence_reference(svgascreen->sws, &entry->fence, fence);
336
337 /* Add entry to the unused list */
338 LIST_ADD(&entry->head, &cache->unused);
339
340 /* Add entry to the hash table bucket */
341 bucket = svga_screen_cache_bucket(&entry->key);
342 LIST_ADD(&entry->bucket_head, &cache->bucket[bucket]);
343 }
344
345 curr = next;
346 next = curr->next;
347 }
348
349 pipe_mutex_unlock(cache->mutex);
350 }
351
352
353 /**
354 * Free all the surfaces in the cache.
355 * Called when destroying the svga screen object.
356 */
357 void
358 svga_screen_cache_cleanup(struct svga_screen *svgascreen)
359 {
360 struct svga_host_surface_cache *cache = &svgascreen->cache;
361 struct svga_winsys_screen *sws = svgascreen->sws;
362 unsigned i;
363
364 for (i = 0; i < SVGA_HOST_SURFACE_CACHE_SIZE; ++i) {
365 if (cache->entries[i].handle) {
366 SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
367 "unref sid %p (shutdown)\n", cache->entries[i].handle);
368 sws->surface_reference(sws, &cache->entries[i].handle, NULL);
369
370 cache->total_size -= surface_size(&cache->entries[i].key);
371 }
372
373 if (cache->entries[i].fence)
374 svgascreen->sws->fence_reference(svgascreen->sws,
375 &cache->entries[i].fence, NULL);
376 }
377
378 pipe_mutex_destroy(cache->mutex);
379 }
380
381
382 enum pipe_error
383 svga_screen_cache_init(struct svga_screen *svgascreen)
384 {
385 struct svga_host_surface_cache *cache = &svgascreen->cache;
386 unsigned i;
387
388 assert(cache->total_size == 0);
389
390 pipe_mutex_init(cache->mutex);
391
392 for (i = 0; i < SVGA_HOST_SURFACE_CACHE_BUCKETS; ++i)
393 LIST_INITHEAD(&cache->bucket[i]);
394
395 LIST_INITHEAD(&cache->unused);
396
397 LIST_INITHEAD(&cache->validated);
398
399 LIST_INITHEAD(&cache->empty);
400 for (i = 0; i < SVGA_HOST_SURFACE_CACHE_SIZE; ++i)
401 LIST_ADDTAIL(&cache->entries[i].head, &cache->empty);
402
403 return PIPE_OK;
404 }
405
406
407 /**
408 * Allocate a new host-side surface. If the surface is marked as cachable,
409 * first try re-using a surface in the cache of freed surfaces. Otherwise,
410 * allocate a new surface.
411 * \param bind_flags bitmask of PIPE_BIND_x flags
412 * \param usage one of PIPE_USAGE_x values
413 */
414 struct svga_winsys_surface *
415 svga_screen_surface_create(struct svga_screen *svgascreen,
416 unsigned bind_flags, unsigned usage,
417 struct svga_host_surface_cache_key *key)
418 {
419 struct svga_winsys_screen *sws = svgascreen->sws;
420 struct svga_winsys_surface *handle = NULL;
421 boolean cachable = SVGA_SURFACE_CACHE_ENABLED && key->cachable;
422
423 SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
424 "%s sz %dx%dx%d mips %d faces %d arraySize %d cachable %d\n",
425 __FUNCTION__,
426 key->size.width,
427 key->size.height,
428 key->size.depth,
429 key->numMipLevels,
430 key->numFaces,
431 key->arraySize,
432 key->cachable);
433
434 if (cachable) {
435 if (key->format == SVGA3D_BUFFER) {
436 SVGA3dSurfaceFlags hint_flag;
437
438 /* For buffers, round the buffer size up to the nearest power
439 * of two to increase the probability of cache hits. Keep
440 * texture surface dimensions unchanged.
441 */
442 uint32_t size = 1;
443 while (size < key->size.width)
444 size <<= 1;
445 key->size.width = size;
446
447 /* Determine whether the buffer is static or dynamic.
448 * This is a bit of a heuristic which can be tuned as needed.
449 */
450 if (usage == PIPE_USAGE_DEFAULT ||
451 usage == PIPE_USAGE_IMMUTABLE) {
452 hint_flag = SVGA3D_SURFACE_HINT_STATIC;
453 }
454 else if (bind_flags & PIPE_BIND_INDEX_BUFFER) {
455 /* Index buffers don't change too often. Mark them as static.
456 */
457 hint_flag = SVGA3D_SURFACE_HINT_STATIC;
458 }
459 else {
460 /* Since we're reusing buffers we're effectively transforming all
461 * of them into dynamic buffers.
462 *
463 * It would be nice to not cache long lived static buffers. But there
464 * is no way to detect the long lived from short lived ones yet. A
465 * good heuristic would be buffer size.
466 */
467 hint_flag = SVGA3D_SURFACE_HINT_DYNAMIC;
468 }
469
470 key->flags &= ~(SVGA3D_SURFACE_HINT_STATIC |
471 SVGA3D_SURFACE_HINT_DYNAMIC);
472 key->flags |= hint_flag;
473 }
474
475 handle = svga_screen_cache_lookup(svgascreen, key);
476 if (handle) {
477 if (key->format == SVGA3D_BUFFER)
478 SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
479 "reuse sid %p sz %d (buffer)\n", handle,
480 key->size.width);
481 else
482 SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
483 "reuse sid %p sz %dx%dx%d mips %d faces %d arraySize %d\n", handle,
484 key->size.width,
485 key->size.height,
486 key->size.depth,
487 key->numMipLevels,
488 key->numFaces,
489 key->arraySize);
490 }
491 }
492
493 if (!handle) {
494 unsigned usage = 0;
495
496 if (!key->cachable)
497 usage |= SVGA_SURFACE_USAGE_SHARED;
498 if (key->scanout)
499 usage |= SVGA_SURFACE_USAGE_SCANOUT;
500
501 handle = sws->surface_create(sws,
502 key->flags,
503 key->format,
504 usage,
505 key->size,
506 key->numFaces * key->arraySize,
507 key->numMipLevels,
508 key->sampleCount);
509 if (handle)
510 SVGA_DBG(DEBUG_CACHE|DEBUG_DMA,
511 " CREATE sid %p sz %dx%dx%d\n",
512 handle,
513 key->size.width,
514 key->size.height,
515 key->size.depth);
516 }
517
518 return handle;
519 }
520
521
522 /**
523 * Release a surface. We don't actually free the surface- we put
524 * it into the cache of freed surfaces (if it's cachable).
525 */
526 void
527 svga_screen_surface_destroy(struct svga_screen *svgascreen,
528 const struct svga_host_surface_cache_key *key,
529 struct svga_winsys_surface **p_handle)
530 {
531 struct svga_winsys_screen *sws = svgascreen->sws;
532
533 /* We only set the cachable flag for surfaces of which we are the
534 * exclusive owner. So just hold onto our existing reference in
535 * that case.
536 */
537 if (SVGA_SURFACE_CACHE_ENABLED && key->cachable) {
538 svga_screen_cache_add(svgascreen, key, p_handle);
539 }
540 else {
541 SVGA_DBG(DEBUG_DMA,
542 "unref sid %p (uncachable)\n", *p_handle);
543 sws->surface_reference(sws, p_handle, NULL);
544 }
545 }
546
547
548 /**
549 * Print/dump the contents of the screen cache. For debugging.
550 */
551 void
552 svga_screen_cache_dump(const struct svga_screen *svgascreen)
553 {
554 const struct svga_host_surface_cache *cache = &svgascreen->cache;
555 unsigned bucket;
556 unsigned count = 0;
557
558 debug_printf("svga3d surface cache:\n");
559 for (bucket = 0; bucket < SVGA_HOST_SURFACE_CACHE_BUCKETS; bucket++) {
560 struct list_head *curr;
561 curr = cache->bucket[bucket].next;
562 while (curr && curr != &cache->bucket[bucket]) {
563 struct svga_host_surface_cache_entry *entry =
564 LIST_ENTRY(struct svga_host_surface_cache_entry,
565 curr, bucket_head);
566 if (entry->key.format != 37) {
567 debug_printf(" %u x %u x %u format %u\n",
568 entry->key.size.width,
569 entry->key.size.height,
570 entry->key.size.depth,
571 entry->key.format);
572 }
573 curr = curr->next;
574 count++;
575 }
576 }
577
578 debug_printf("%u surfaces, %u bytes\n", count, cache->total_size);
579 }