Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / pipebuffer / pb_bufmgr_cache.c
1 /**************************************************************************
2 *
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * \file
30 * Buffer cache.
31 *
32 * \author Jose Fonseca <jrfonseca-at-tungstengraphics-dot-com>
33 * \author Thomas Hellström <thomas-at-tungstengraphics-dot-com>
34 */
35
36
37 #include "pipe/p_compiler.h"
38 #include "util/u_debug.h"
39 #include "pipe/p_thread.h"
40 #include "util/u_memory.h"
41 #include "util/u_double_list.h"
42 #include "util/u_time.h"
43
44 #include "pb_buffer.h"
45 #include "pb_bufmgr.h"
46
47
48 /**
49 * Convenience macro (type safe).
50 */
51 #define SUPER(__derived) (&(__derived)->base)
52
53
54 struct pb_cache_manager;
55
56
57 /**
58 * Wrapper around a pipe buffer which adds delayed destruction.
59 */
60 struct pb_cache_buffer
61 {
62 struct pb_buffer base;
63
64 struct pb_buffer *buffer;
65 struct pb_cache_manager *mgr;
66
67 /** Caching time interval */
68 struct util_time start, end;
69
70 struct list_head head;
71 };
72
73
74 struct pb_cache_manager
75 {
76 struct pb_manager base;
77
78 struct pb_manager *provider;
79 unsigned usecs;
80
81 pipe_mutex mutex;
82
83 struct list_head delayed;
84 pb_size numDelayed;
85 };
86
87
88 static INLINE struct pb_cache_buffer *
89 pb_cache_buffer(struct pb_buffer *buf)
90 {
91 assert(buf);
92 return (struct pb_cache_buffer *)buf;
93 }
94
95
96 static INLINE struct pb_cache_manager *
97 pb_cache_manager(struct pb_manager *mgr)
98 {
99 assert(mgr);
100 return (struct pb_cache_manager *)mgr;
101 }
102
103
104 /**
105 * Actually destroy the buffer.
106 */
107 static INLINE void
108 _pb_cache_buffer_destroy(struct pb_cache_buffer *buf)
109 {
110 struct pb_cache_manager *mgr = buf->mgr;
111
112 LIST_DEL(&buf->head);
113 assert(mgr->numDelayed);
114 --mgr->numDelayed;
115 assert(!pipe_is_referenced(&buf->base.base.reference));
116 pb_reference(&buf->buffer, NULL);
117 FREE(buf);
118 }
119
120
121 /**
122 * Free as many cache buffers from the list head as possible.
123 */
124 static void
125 _pb_cache_buffer_list_check_free(struct pb_cache_manager *mgr)
126 {
127 struct list_head *curr, *next;
128 struct pb_cache_buffer *buf;
129 struct util_time now;
130
131 util_time_get(&now);
132
133 curr = mgr->delayed.next;
134 next = curr->next;
135 while(curr != &mgr->delayed) {
136 buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
137
138 if(!util_time_timeout(&buf->start, &buf->end, &now))
139 break;
140
141 _pb_cache_buffer_destroy(buf);
142
143 curr = next;
144 next = curr->next;
145 }
146 }
147
148
149 static void
150 pb_cache_buffer_destroy(struct pb_buffer *_buf)
151 {
152 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
153 struct pb_cache_manager *mgr = buf->mgr;
154
155 pipe_mutex_lock(mgr->mutex);
156 assert(!pipe_is_referenced(&buf->base.base.reference));
157
158 _pb_cache_buffer_list_check_free(mgr);
159
160 util_time_get(&buf->start);
161 util_time_add(&buf->start, mgr->usecs, &buf->end);
162 LIST_ADDTAIL(&buf->head, &mgr->delayed);
163 ++mgr->numDelayed;
164 pipe_mutex_unlock(mgr->mutex);
165 }
166
167
168 static void *
169 pb_cache_buffer_map(struct pb_buffer *_buf,
170 unsigned flags)
171 {
172 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
173 return pb_map(buf->buffer, flags);
174 }
175
176
177 static void
178 pb_cache_buffer_unmap(struct pb_buffer *_buf)
179 {
180 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
181 pb_unmap(buf->buffer);
182 }
183
184
185 static enum pipe_error
186 pb_cache_buffer_validate(struct pb_buffer *_buf,
187 struct pb_validate *vl,
188 unsigned flags)
189 {
190 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
191 return pb_validate(buf->buffer, vl, flags);
192 }
193
194
195 static void
196 pb_cache_buffer_fence(struct pb_buffer *_buf,
197 struct pipe_fence_handle *fence)
198 {
199 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
200 pb_fence(buf->buffer, fence);
201 }
202
203
204 static void
205 pb_cache_buffer_get_base_buffer(struct pb_buffer *_buf,
206 struct pb_buffer **base_buf,
207 pb_size *offset)
208 {
209 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
210 pb_get_base_buffer(buf->buffer, base_buf, offset);
211 }
212
213
214 const struct pb_vtbl
215 pb_cache_buffer_vtbl = {
216 pb_cache_buffer_destroy,
217 pb_cache_buffer_map,
218 pb_cache_buffer_unmap,
219 pb_cache_buffer_validate,
220 pb_cache_buffer_fence,
221 pb_cache_buffer_get_base_buffer
222 };
223
224
225 static INLINE boolean
226 pb_cache_is_buffer_compat(struct pb_cache_buffer *buf,
227 pb_size size,
228 const struct pb_desc *desc)
229 {
230 if(buf->base.base.size < size)
231 return FALSE;
232
233 /* be lenient with size */
234 if(buf->base.base.size >= 2*size)
235 return FALSE;
236
237 if(!pb_check_alignment(desc->alignment, buf->base.base.alignment))
238 return FALSE;
239
240 if(!pb_check_usage(desc->usage, buf->base.base.usage))
241 return FALSE;
242
243 return TRUE;
244 }
245
246
247 static struct pb_buffer *
248 pb_cache_manager_create_buffer(struct pb_manager *_mgr,
249 pb_size size,
250 const struct pb_desc *desc)
251 {
252 struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
253 struct pb_cache_buffer *buf;
254 struct pb_cache_buffer *curr_buf;
255 struct list_head *curr, *next;
256 struct util_time now;
257
258 pipe_mutex_lock(mgr->mutex);
259
260 buf = NULL;
261 curr = mgr->delayed.next;
262 next = curr->next;
263
264 /* search in the expired buffers, freeing them in the process */
265 util_time_get(&now);
266 while(curr != &mgr->delayed) {
267 curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
268 if(!buf && pb_cache_is_buffer_compat(curr_buf, size, desc))
269 buf = curr_buf;
270 else if(util_time_timeout(&curr_buf->start, &curr_buf->end, &now))
271 _pb_cache_buffer_destroy(curr_buf);
272 else
273 /* This buffer (and all hereafter) are still hot in cache */
274 break;
275 curr = next;
276 next = curr->next;
277 }
278
279 /* keep searching in the hot buffers */
280 if(!buf) {
281 while(curr != &mgr->delayed) {
282 curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
283 if(pb_cache_is_buffer_compat(curr_buf, size, desc)) {
284 buf = curr_buf;
285 break;
286 }
287 /* no need to check the timeout here */
288 curr = next;
289 next = curr->next;
290 }
291 }
292
293 if(buf) {
294 LIST_DEL(&buf->head);
295 pipe_mutex_unlock(mgr->mutex);
296 /* Increase refcount */
297 pipe_reference(NULL, &buf->base.base.reference);
298 return &buf->base;
299 }
300
301 pipe_mutex_unlock(mgr->mutex);
302
303 buf = CALLOC_STRUCT(pb_cache_buffer);
304 if(!buf)
305 return NULL;
306
307 buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc);
308 if(!buf->buffer) {
309 FREE(buf);
310 return NULL;
311 }
312
313 assert(pipe_is_referenced(&buf->buffer->base.reference));
314 assert(pb_check_alignment(desc->alignment, buf->buffer->base.alignment));
315 assert(pb_check_usage(desc->usage, buf->buffer->base.usage));
316 assert(buf->buffer->base.size >= size);
317
318 pipe_reference_init(&buf->base.base.reference, 1);
319 buf->base.base.alignment = buf->buffer->base.alignment;
320 buf->base.base.usage = buf->buffer->base.usage;
321 buf->base.base.size = buf->buffer->base.size;
322
323 buf->base.vtbl = &pb_cache_buffer_vtbl;
324 buf->mgr = mgr;
325
326 return &buf->base;
327 }
328
329
330 static void
331 pb_cache_manager_flush(struct pb_manager *_mgr)
332 {
333 struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
334 struct list_head *curr, *next;
335 struct pb_cache_buffer *buf;
336
337 pipe_mutex_lock(mgr->mutex);
338 curr = mgr->delayed.next;
339 next = curr->next;
340 while(curr != &mgr->delayed) {
341 buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
342 _pb_cache_buffer_destroy(buf);
343 curr = next;
344 next = curr->next;
345 }
346 pipe_mutex_unlock(mgr->mutex);
347
348 assert(mgr->provider->flush);
349 if(mgr->provider->flush)
350 mgr->provider->flush(mgr->provider);
351 }
352
353
354 static void
355 pb_cache_manager_destroy(struct pb_manager *mgr)
356 {
357 pb_cache_manager_flush(mgr);
358 FREE(mgr);
359 }
360
361
362 struct pb_manager *
363 pb_cache_manager_create(struct pb_manager *provider,
364 unsigned usecs)
365 {
366 struct pb_cache_manager *mgr;
367
368 if(!provider)
369 return NULL;
370
371 mgr = CALLOC_STRUCT(pb_cache_manager);
372 if (!mgr)
373 return NULL;
374
375 mgr->base.destroy = pb_cache_manager_destroy;
376 mgr->base.create_buffer = pb_cache_manager_create_buffer;
377 mgr->base.flush = pb_cache_manager_flush;
378 mgr->provider = provider;
379 mgr->usecs = usecs;
380 LIST_INITHEAD(&mgr->delayed);
381 mgr->numDelayed = 0;
382 pipe_mutex_init(mgr->mutex);
383
384 return &mgr->base;
385 }