Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / gallium / drivers / svga / svga_screen_cache.h
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 #ifndef SVGA_SCREEN_CACHE_H_
27 #define SVGA_SCREEN_CACHE_H_
28
29
30 #include "svga_types.h"
31 #include "svga_reg.h"
32 #include "svga3d_reg.h"
33
34 #include "pipe/p_thread.h"
35
36 #include "util/u_double_list.h"
37
38
39 /* Guess the storage size of cached surfaces and try and keep it under
40 * this amount:
41 */
42 #define SVGA_HOST_SURFACE_CACHE_BYTES 16*1024*1024
43
44 /* Maximum number of discrete surfaces in the cache:
45 */
46 #define SVGA_HOST_SURFACE_CACHE_SIZE 1024
47
48 /* Number of hash buckets:
49 */
50 #define SVGA_HOST_SURFACE_CACHE_BUCKETS 256
51
52
53 struct svga_winsys_surface;
54 struct svga_screen;
55
56 /**
57 * Same as svga_winsys_screen::surface_create.
58 */
59 struct svga_host_surface_cache_key
60 {
61 SVGA3dSurfaceFlags flags;
62 SVGA3dSurfaceFormat format;
63 SVGA3dSize size;
64 uint32_t numFaces:24;
65 uint32_t numMipLevels:7;
66 uint32_t cachable:1; /* False if this is a shared surface */
67 };
68
69
70 struct svga_host_surface_cache_entry
71 {
72 /**
73 * Head for the LRU list, svga_host_surface_cache::unused, and
74 * svga_host_surface_cache::empty
75 */
76 struct list_head head;
77
78 /** Head for the bucket lists. */
79 struct list_head bucket_head;
80
81 struct svga_host_surface_cache_key key;
82 struct svga_winsys_surface *handle;
83
84 struct pipe_fence_handle *fence;
85 };
86
87
88 /**
89 * Cache of the host surfaces.
90 *
91 * A cache entry can be in the following stages:
92 * 1. empty
93 * 2. holding a buffer in a validate list
94 * 3. holding a flushed buffer (not in any validate list) with an active fence
95 * 4. holding a flushed buffer with an expired fence
96 *
97 * An entry progresses from 1 -> 2 -> 3 -> 4. When we need an entry to put a
98 * buffer into we preferencial take from 1, or from the least recentely used
99 * buffer from 3/4.
100 */
101 struct svga_host_surface_cache
102 {
103 pipe_mutex mutex;
104
105 /* Unused buffers are put in buckets to speed up lookups */
106 struct list_head bucket[SVGA_HOST_SURFACE_CACHE_BUCKETS];
107
108 /* Entries with unused buffers, ordered from most to least recently used
109 * (3 and 4) */
110 struct list_head unused;
111
112 /* Entries with buffers still in validate lists (2) */
113 struct list_head validated;
114
115 /** Empty entries (1) */
116 struct list_head empty;
117
118 /** The actual storage for the entries */
119 struct svga_host_surface_cache_entry entries[SVGA_HOST_SURFACE_CACHE_SIZE];
120 };
121
122
123 void
124 svga_screen_cache_cleanup(struct svga_screen *svgascreen);
125
126 void
127 svga_screen_cache_flush(struct svga_screen *svgascreen,
128 struct pipe_fence_handle *fence);
129
130 enum pipe_error
131 svga_screen_cache_init(struct svga_screen *svgascreen);
132
133
134 struct svga_winsys_surface *
135 svga_screen_surface_create(struct svga_screen *svgascreen,
136 struct svga_host_surface_cache_key *key);
137
138 void
139 svga_screen_surface_destroy(struct svga_screen *svgascreen,
140 const struct svga_host_surface_cache_key *key,
141 struct svga_winsys_surface **handle);
142
143
144 #endif /* SVGA_SCREEN_CACHE_H_ */