revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / libs / mesa / src / gallium / auxiliary / pipebuffer / pb_bufmgr_cache.c
blob25accefa8d6412807397fd087ac897b47620874d
1 /**************************************************************************
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
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:
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.
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.
26 **************************************************************************/
28 /**
29 * \file
30 * Buffer cache.
32 * \author Jose Fonseca <jrfonseca-at-tungstengraphics-dot-com>
33 * \author Thomas Hellström <thomas-at-tungstengraphics-dot-com>
37 #include "pipe/p_compiler.h"
38 #include "util/u_debug.h"
39 #include "os/os_thread.h"
40 #include "util/u_memory.h"
41 #include "util/u_double_list.h"
42 #include "util/u_time.h"
44 #include "pb_buffer.h"
45 #include "pb_bufmgr.h"
48 /**
49 * Convenience macro (type safe).
51 #define SUPER(__derived) (&(__derived)->base)
54 struct pb_cache_manager;
57 /**
58 * Wrapper around a pipe buffer which adds delayed destruction.
60 struct pb_cache_buffer
62 struct pb_buffer base;
64 struct pb_buffer *buffer;
65 struct pb_cache_manager *mgr;
67 /** Caching time interval */
68 int64_t start, end;
70 struct list_head head;
74 struct pb_cache_manager
76 struct pb_manager base;
78 struct pb_manager *provider;
79 unsigned usecs;
81 pipe_mutex mutex;
83 struct list_head delayed;
84 pb_size numDelayed;
88 static INLINE struct pb_cache_buffer *
89 pb_cache_buffer(struct pb_buffer *buf)
91 assert(buf);
92 return (struct pb_cache_buffer *)buf;
96 static INLINE struct pb_cache_manager *
97 pb_cache_manager(struct pb_manager *mgr)
99 assert(mgr);
100 return (struct pb_cache_manager *)mgr;
105 * Actually destroy the buffer.
107 static INLINE void
108 _pb_cache_buffer_destroy(struct pb_cache_buffer *buf)
110 struct pb_cache_manager *mgr = buf->mgr;
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);
122 * Free as many cache buffers from the list head as possible.
124 static void
125 _pb_cache_buffer_list_check_free(struct pb_cache_manager *mgr)
127 struct list_head *curr, *next;
128 struct pb_cache_buffer *buf;
129 int64_t now;
131 now = os_time_get();
133 curr = mgr->delayed.next;
134 next = curr->next;
135 while(curr != &mgr->delayed) {
136 buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
138 if(!os_time_timeout(buf->start, buf->end, now))
139 break;
141 _pb_cache_buffer_destroy(buf);
143 curr = next;
144 next = curr->next;
149 static void
150 pb_cache_buffer_destroy(struct pb_buffer *_buf)
152 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
153 struct pb_cache_manager *mgr = buf->mgr;
155 pipe_mutex_lock(mgr->mutex);
156 assert(!pipe_is_referenced(&buf->base.base.reference));
158 _pb_cache_buffer_list_check_free(mgr);
160 buf->start = os_time_get();
161 buf->end = buf->start + mgr->usecs;
162 LIST_ADDTAIL(&buf->head, &mgr->delayed);
163 ++mgr->numDelayed;
164 pipe_mutex_unlock(mgr->mutex);
168 static void *
169 pb_cache_buffer_map(struct pb_buffer *_buf,
170 unsigned flags, void *flush_ctx)
172 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
173 return pb_map(buf->buffer, flags, flush_ctx);
177 static void
178 pb_cache_buffer_unmap(struct pb_buffer *_buf)
180 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
181 pb_unmap(buf->buffer);
185 static enum pipe_error
186 pb_cache_buffer_validate(struct pb_buffer *_buf,
187 struct pb_validate *vl,
188 unsigned flags)
190 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
191 return pb_validate(buf->buffer, vl, flags);
195 static void
196 pb_cache_buffer_fence(struct pb_buffer *_buf,
197 struct pipe_fence_handle *fence)
199 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
200 pb_fence(buf->buffer, fence);
204 static void
205 pb_cache_buffer_get_base_buffer(struct pb_buffer *_buf,
206 struct pb_buffer **base_buf,
207 pb_size *offset)
209 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
210 pb_get_base_buffer(buf->buffer, base_buf, offset);
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
225 static INLINE int
226 pb_cache_is_buffer_compat(struct pb_cache_buffer *buf,
227 pb_size size,
228 const struct pb_desc *desc)
230 if(buf->base.base.size < size)
231 return 0;
233 /* be lenient with size */
234 if(buf->base.base.size >= 2*size)
235 return 0;
237 if(!pb_check_alignment(desc->alignment, buf->base.base.alignment))
238 return 0;
240 if(!pb_check_usage(desc->usage, buf->base.base.usage))
241 return 0;
243 if (buf->mgr->provider->is_buffer_busy) {
244 if (buf->mgr->provider->is_buffer_busy(buf->mgr->provider, buf->buffer))
245 return -1;
246 } else {
247 void *ptr = pb_map(buf->buffer, PB_USAGE_DONTBLOCK, NULL);
249 if (!ptr)
250 return -1;
252 pb_unmap(buf->buffer);
255 return 1;
259 static struct pb_buffer *
260 pb_cache_manager_create_buffer(struct pb_manager *_mgr,
261 pb_size size,
262 const struct pb_desc *desc)
264 struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
265 struct pb_cache_buffer *buf;
266 struct pb_cache_buffer *curr_buf;
267 struct list_head *curr, *next;
268 int64_t now;
269 int ret = 0;
271 pipe_mutex_lock(mgr->mutex);
273 buf = NULL;
274 curr = mgr->delayed.next;
275 next = curr->next;
277 /* search in the expired buffers, freeing them in the process */
278 now = os_time_get();
279 while(curr != &mgr->delayed) {
280 curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
281 if(!buf && (ret = pb_cache_is_buffer_compat(curr_buf, size, desc) > 0))
282 buf = curr_buf;
283 else if(os_time_timeout(curr_buf->start, curr_buf->end, now))
284 _pb_cache_buffer_destroy(curr_buf);
285 else
286 /* This buffer (and all hereafter) are still hot in cache */
287 break;
288 if (ret == -1)
289 break;
290 curr = next;
291 next = curr->next;
294 /* keep searching in the hot buffers */
295 if(!buf && ret != -1) {
296 while(curr != &mgr->delayed) {
297 curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
298 ret = pb_cache_is_buffer_compat(curr_buf, size, desc);
299 if (ret > 0) {
300 buf = curr_buf;
301 break;
303 if (ret == -1)
304 break;
305 /* no need to check the timeout here */
306 curr = next;
307 next = curr->next;
311 if(buf) {
312 LIST_DEL(&buf->head);
313 --mgr->numDelayed;
314 pipe_mutex_unlock(mgr->mutex);
315 /* Increase refcount */
316 pipe_reference_init(&buf->base.base.reference, 1);
317 return &buf->base;
320 pipe_mutex_unlock(mgr->mutex);
322 buf = CALLOC_STRUCT(pb_cache_buffer);
323 if(!buf)
324 return NULL;
326 buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc);
327 if(!buf->buffer) {
328 FREE(buf);
329 return NULL;
332 assert(pipe_is_referenced(&buf->buffer->base.reference));
333 assert(pb_check_alignment(desc->alignment, buf->buffer->base.alignment));
334 assert(pb_check_usage(desc->usage, buf->buffer->base.usage));
335 assert(buf->buffer->base.size >= size);
337 pipe_reference_init(&buf->base.base.reference, 1);
338 buf->base.base.alignment = buf->buffer->base.alignment;
339 buf->base.base.usage = buf->buffer->base.usage;
340 buf->base.base.size = buf->buffer->base.size;
342 buf->base.vtbl = &pb_cache_buffer_vtbl;
343 buf->mgr = mgr;
345 return &buf->base;
349 static void
350 pb_cache_manager_flush(struct pb_manager *_mgr)
352 struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
353 struct list_head *curr, *next;
354 struct pb_cache_buffer *buf;
356 pipe_mutex_lock(mgr->mutex);
357 curr = mgr->delayed.next;
358 next = curr->next;
359 while(curr != &mgr->delayed) {
360 buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
361 _pb_cache_buffer_destroy(buf);
362 curr = next;
363 next = curr->next;
365 pipe_mutex_unlock(mgr->mutex);
367 assert(mgr->provider->flush);
368 if(mgr->provider->flush)
369 mgr->provider->flush(mgr->provider);
373 static void
374 pb_cache_manager_destroy(struct pb_manager *mgr)
376 pb_cache_manager_flush(mgr);
377 FREE(mgr);
381 struct pb_manager *
382 pb_cache_manager_create(struct pb_manager *provider,
383 unsigned usecs)
385 struct pb_cache_manager *mgr;
387 if(!provider)
388 return NULL;
390 mgr = CALLOC_STRUCT(pb_cache_manager);
391 if (!mgr)
392 return NULL;
394 mgr->base.destroy = pb_cache_manager_destroy;
395 mgr->base.create_buffer = pb_cache_manager_create_buffer;
396 mgr->base.flush = pb_cache_manager_flush;
397 mgr->provider = provider;
398 mgr->usecs = usecs;
399 LIST_INITHEAD(&mgr->delayed);
400 mgr->numDelayed = 0;
401 pipe_mutex_init(mgr->mutex);
403 return &mgr->base;