gallium/pipebuffer: change pb_cache_manager_create() size_factor to float
[mesa.git] / src / gallium / auxiliary / pipebuffer / pb_bufmgr_cache.c
1 /**************************************************************************
2 *
3 * Copyright 2007-2008 VMware, Inc.
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 VMWARE 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 <jfonseca-at-vmware-dot-com>
33 * \author Thomas Hellström <thellstom-at-vmware-dot-com>
34 */
35
36
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"
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 int64_t 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 float size_factor;
86 unsigned bypass_usage;
87 };
88
89
90 static INLINE struct pb_cache_buffer *
91 pb_cache_buffer(struct pb_buffer *buf)
92 {
93 assert(buf);
94 return (struct pb_cache_buffer *)buf;
95 }
96
97
98 static INLINE struct pb_cache_manager *
99 pb_cache_manager(struct pb_manager *mgr)
100 {
101 assert(mgr);
102 return (struct pb_cache_manager *)mgr;
103 }
104
105
106 /**
107 * Actually destroy the buffer.
108 */
109 static INLINE void
110 _pb_cache_buffer_destroy(struct pb_cache_buffer *buf)
111 {
112 struct pb_cache_manager *mgr = buf->mgr;
113
114 LIST_DEL(&buf->head);
115 assert(mgr->numDelayed);
116 --mgr->numDelayed;
117 assert(!pipe_is_referenced(&buf->base.reference));
118 pb_reference(&buf->buffer, NULL);
119 FREE(buf);
120 }
121
122
123 /**
124 * Free as many cache buffers from the list head as possible.
125 */
126 static void
127 _pb_cache_buffer_list_check_free(struct pb_cache_manager *mgr)
128 {
129 struct list_head *curr, *next;
130 struct pb_cache_buffer *buf;
131 int64_t now;
132
133 now = os_time_get();
134
135 curr = mgr->delayed.next;
136 next = curr->next;
137 while(curr != &mgr->delayed) {
138 buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
139
140 if(!os_time_timeout(buf->start, buf->end, now))
141 break;
142
143 _pb_cache_buffer_destroy(buf);
144
145 curr = next;
146 next = curr->next;
147 }
148 }
149
150
151 static void
152 pb_cache_buffer_destroy(struct pb_buffer *_buf)
153 {
154 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
155 struct pb_cache_manager *mgr = buf->mgr;
156
157 pipe_mutex_lock(mgr->mutex);
158 assert(!pipe_is_referenced(&buf->base.reference));
159
160 _pb_cache_buffer_list_check_free(mgr);
161
162 buf->start = os_time_get();
163 buf->end = buf->start + mgr->usecs;
164 LIST_ADDTAIL(&buf->head, &mgr->delayed);
165 ++mgr->numDelayed;
166 pipe_mutex_unlock(mgr->mutex);
167 }
168
169
170 static void *
171 pb_cache_buffer_map(struct pb_buffer *_buf,
172 unsigned flags, void *flush_ctx)
173 {
174 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
175 return pb_map(buf->buffer, flags, flush_ctx);
176 }
177
178
179 static void
180 pb_cache_buffer_unmap(struct pb_buffer *_buf)
181 {
182 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
183 pb_unmap(buf->buffer);
184 }
185
186
187 static enum pipe_error
188 pb_cache_buffer_validate(struct pb_buffer *_buf,
189 struct pb_validate *vl,
190 unsigned flags)
191 {
192 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
193 return pb_validate(buf->buffer, vl, flags);
194 }
195
196
197 static void
198 pb_cache_buffer_fence(struct pb_buffer *_buf,
199 struct pipe_fence_handle *fence)
200 {
201 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
202 pb_fence(buf->buffer, fence);
203 }
204
205
206 static void
207 pb_cache_buffer_get_base_buffer(struct pb_buffer *_buf,
208 struct pb_buffer **base_buf,
209 pb_size *offset)
210 {
211 struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
212 pb_get_base_buffer(buf->buffer, base_buf, offset);
213 }
214
215
216 const struct pb_vtbl
217 pb_cache_buffer_vtbl = {
218 pb_cache_buffer_destroy,
219 pb_cache_buffer_map,
220 pb_cache_buffer_unmap,
221 pb_cache_buffer_validate,
222 pb_cache_buffer_fence,
223 pb_cache_buffer_get_base_buffer
224 };
225
226
227 static INLINE int
228 pb_cache_is_buffer_compat(struct pb_cache_buffer *buf,
229 pb_size size,
230 const struct pb_desc *desc)
231 {
232 if (desc->usage & buf->mgr->bypass_usage)
233 return 0;
234
235 if(buf->base.size < size)
236 return 0;
237
238 /* be lenient with size */
239 if(buf->base.size > (unsigned) (buf->mgr->size_factor * size))
240 return 0;
241
242 if(!pb_check_alignment(desc->alignment, buf->base.alignment))
243 return 0;
244
245 if(!pb_check_usage(desc->usage, buf->base.usage))
246 return 0;
247
248 if (buf->mgr->provider->is_buffer_busy) {
249 if (buf->mgr->provider->is_buffer_busy(buf->mgr->provider, buf->buffer))
250 return -1;
251 } else {
252 void *ptr = pb_map(buf->buffer, PB_USAGE_DONTBLOCK, NULL);
253
254 if (!ptr)
255 return -1;
256
257 pb_unmap(buf->buffer);
258 }
259
260 return 1;
261 }
262
263
264 static struct pb_buffer *
265 pb_cache_manager_create_buffer(struct pb_manager *_mgr,
266 pb_size size,
267 const struct pb_desc *desc)
268 {
269 struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
270 struct pb_cache_buffer *buf;
271 struct pb_cache_buffer *curr_buf;
272 struct list_head *curr, *next;
273 int64_t now;
274 int ret = 0;
275
276 pipe_mutex_lock(mgr->mutex);
277
278 buf = NULL;
279 curr = mgr->delayed.next;
280 next = curr->next;
281
282 /* search in the expired buffers, freeing them in the process */
283 now = os_time_get();
284 while(curr != &mgr->delayed) {
285 curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
286 if(!buf && (ret = pb_cache_is_buffer_compat(curr_buf, size, desc) > 0))
287 buf = curr_buf;
288 else if(os_time_timeout(curr_buf->start, curr_buf->end, now))
289 _pb_cache_buffer_destroy(curr_buf);
290 else
291 /* This buffer (and all hereafter) are still hot in cache */
292 break;
293 if (ret == -1)
294 break;
295 curr = next;
296 next = curr->next;
297 }
298
299 /* keep searching in the hot buffers */
300 if(!buf && ret != -1) {
301 while(curr != &mgr->delayed) {
302 curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
303 ret = pb_cache_is_buffer_compat(curr_buf, size, desc);
304 if (ret > 0) {
305 buf = curr_buf;
306 break;
307 }
308 if (ret == -1)
309 break;
310 /* no need to check the timeout here */
311 curr = next;
312 next = curr->next;
313 }
314 }
315
316 if(buf) {
317 LIST_DEL(&buf->head);
318 --mgr->numDelayed;
319 pipe_mutex_unlock(mgr->mutex);
320 /* Increase refcount */
321 pipe_reference_init(&buf->base.reference, 1);
322 return &buf->base;
323 }
324
325 pipe_mutex_unlock(mgr->mutex);
326
327 buf = CALLOC_STRUCT(pb_cache_buffer);
328 if(!buf)
329 return NULL;
330
331 buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc);
332
333 /* Empty the cache and try again. */
334 if (!buf->buffer) {
335 mgr->base.flush(&mgr->base);
336 buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc);
337 }
338
339 if(!buf->buffer) {
340 FREE(buf);
341 return NULL;
342 }
343
344 assert(pipe_is_referenced(&buf->buffer->reference));
345 assert(pb_check_alignment(desc->alignment, buf->buffer->alignment));
346 assert(pb_check_usage(desc->usage & ~mgr->bypass_usage, buf->buffer->usage));
347 assert(buf->buffer->size >= size);
348
349 pipe_reference_init(&buf->base.reference, 1);
350 buf->base.alignment = buf->buffer->alignment;
351 buf->base.usage = buf->buffer->usage;
352 buf->base.size = buf->buffer->size;
353
354 buf->base.vtbl = &pb_cache_buffer_vtbl;
355 buf->mgr = mgr;
356
357 return &buf->base;
358 }
359
360
361 static void
362 pb_cache_manager_flush(struct pb_manager *_mgr)
363 {
364 struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
365 struct list_head *curr, *next;
366 struct pb_cache_buffer *buf;
367
368 pipe_mutex_lock(mgr->mutex);
369 curr = mgr->delayed.next;
370 next = curr->next;
371 while(curr != &mgr->delayed) {
372 buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
373 _pb_cache_buffer_destroy(buf);
374 curr = next;
375 next = curr->next;
376 }
377 pipe_mutex_unlock(mgr->mutex);
378
379 assert(mgr->provider->flush);
380 if(mgr->provider->flush)
381 mgr->provider->flush(mgr->provider);
382 }
383
384
385 static void
386 pb_cache_manager_destroy(struct pb_manager *mgr)
387 {
388 pb_cache_manager_flush(mgr);
389 FREE(mgr);
390 }
391
392 /**
393 * Create a caching buffer manager
394 *
395 * @param provider The buffer manager to which cache miss buffer requests
396 * should be redirected.
397 * @param usecs Unused buffers may be released from the cache after this
398 * time
399 * @param size_factor Declare buffers that are size_factor times bigger than
400 * the requested size as cache hits.
401 * @param bypass_usage Bitmask. If (requested usage & bypass_usage) != 0,
402 * buffer allocation requests are redirected to the provider.
403 */
404 struct pb_manager *
405 pb_cache_manager_create(struct pb_manager *provider,
406 unsigned usecs,
407 float size_factor,
408 unsigned bypass_usage)
409 {
410 struct pb_cache_manager *mgr;
411
412 if(!provider)
413 return NULL;
414
415 mgr = CALLOC_STRUCT(pb_cache_manager);
416 if (!mgr)
417 return NULL;
418
419 mgr->base.destroy = pb_cache_manager_destroy;
420 mgr->base.create_buffer = pb_cache_manager_create_buffer;
421 mgr->base.flush = pb_cache_manager_flush;
422 mgr->provider = provider;
423 mgr->usecs = usecs;
424 mgr->size_factor = size_factor;
425 mgr->bypass_usage = bypass_usage;
426 LIST_INITHEAD(&mgr->delayed);
427 mgr->numDelayed = 0;
428 pipe_mutex_init(mgr->mutex);
429
430 return &mgr->base;
431 }