1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
27 **************************************************************************/
31 * Batch buffer pool management.
33 * \author Jose Fonseca <jrfonseca-at-tungstengraphics-dot-com>
34 * \author Thomas Hellström <thomas-at-tungstengraphics-dot-com>
38 #include "pipe/p_compiler.h"
39 #include "util/u_debug.h"
40 #include "os/os_thread.h"
41 #include "pipe/p_defines.h"
42 #include "util/u_memory.h"
43 #include "util/u_double_list.h"
45 #include "pb_buffer.h"
46 #include "pb_bufmgr.h"
50 * Convenience macro (type safe).
52 #define SUPER(__derived) (&(__derived)->base)
55 struct pool_pb_manager
57 struct pb_manager base
;
67 struct list_head free
;
69 struct pb_buffer
*buffer
;
72 struct pool_buffer
*bufs
;
76 static INLINE
struct pool_pb_manager
*
77 pool_pb_manager(struct pb_manager
*mgr
)
80 return (struct pool_pb_manager
*)mgr
;
86 struct pb_buffer base
;
88 struct pool_pb_manager
*mgr
;
90 struct list_head head
;
96 static INLINE
struct pool_buffer
*
97 pool_buffer(struct pb_buffer
*buf
)
100 return (struct pool_buffer
*)buf
;
106 pool_buffer_destroy(struct pb_buffer
*buf
)
108 struct pool_buffer
*pool_buf
= pool_buffer(buf
);
109 struct pool_pb_manager
*pool
= pool_buf
->mgr
;
111 assert(!pipe_is_referenced(&pool_buf
->base
.base
.reference
));
113 pipe_mutex_lock(pool
->mutex
);
114 LIST_ADD(&pool_buf
->head
, &pool
->free
);
116 pipe_mutex_unlock(pool
->mutex
);
121 pool_buffer_map(struct pb_buffer
*buf
, unsigned flags
, void *flush_ctx
)
123 struct pool_buffer
*pool_buf
= pool_buffer(buf
);
124 struct pool_pb_manager
*pool
= pool_buf
->mgr
;
127 /* XXX: it will be necessary to remap here to propagate flush_ctx */
129 pipe_mutex_lock(pool
->mutex
);
130 map
= (unsigned char *) pool
->map
+ pool_buf
->start
;
131 pipe_mutex_unlock(pool
->mutex
);
137 pool_buffer_unmap(struct pb_buffer
*buf
)
143 static enum pipe_error
144 pool_buffer_validate(struct pb_buffer
*buf
,
145 struct pb_validate
*vl
,
148 struct pool_buffer
*pool_buf
= pool_buffer(buf
);
149 struct pool_pb_manager
*pool
= pool_buf
->mgr
;
150 return pb_validate(pool
->buffer
, vl
, flags
);
155 pool_buffer_fence(struct pb_buffer
*buf
,
156 struct pipe_fence_handle
*fence
)
158 struct pool_buffer
*pool_buf
= pool_buffer(buf
);
159 struct pool_pb_manager
*pool
= pool_buf
->mgr
;
160 pb_fence(pool
->buffer
, fence
);
165 pool_buffer_get_base_buffer(struct pb_buffer
*buf
,
166 struct pb_buffer
**base_buf
,
169 struct pool_buffer
*pool_buf
= pool_buffer(buf
);
170 struct pool_pb_manager
*pool
= pool_buf
->mgr
;
171 pb_get_base_buffer(pool
->buffer
, base_buf
, offset
);
172 *offset
+= pool_buf
->start
;
176 static const struct pb_vtbl
181 pool_buffer_validate
,
183 pool_buffer_get_base_buffer
187 static struct pb_buffer
*
188 pool_bufmgr_create_buffer(struct pb_manager
*mgr
,
190 const struct pb_desc
*desc
)
192 struct pool_pb_manager
*pool
= pool_pb_manager(mgr
);
193 struct pool_buffer
*pool_buf
;
194 struct list_head
*item
;
196 assert(size
== pool
->bufSize
);
197 assert(pool
->bufAlign
% desc
->alignment
== 0);
199 pipe_mutex_lock(pool
->mutex
);
201 if (pool
->numFree
== 0) {
202 pipe_mutex_unlock(pool
->mutex
);
203 debug_printf("warning: out of fixed size buffer objects\n");
207 item
= pool
->free
.next
;
209 if (item
== &pool
->free
) {
210 pipe_mutex_unlock(pool
->mutex
);
211 debug_printf("error: fixed size buffer pool corruption\n");
218 pipe_mutex_unlock(pool
->mutex
);
220 pool_buf
= LIST_ENTRY(struct pool_buffer
, item
, head
);
221 assert(!pipe_is_referenced(&pool_buf
->base
.base
.reference
));
222 pipe_reference_init(&pool_buf
->base
.base
.reference
, 1);
223 pool_buf
->base
.base
.alignment
= desc
->alignment
;
224 pool_buf
->base
.base
.usage
= desc
->usage
;
226 return SUPER(pool_buf
);
231 pool_bufmgr_flush(struct pb_manager
*mgr
)
238 pool_bufmgr_destroy(struct pb_manager
*mgr
)
240 struct pool_pb_manager
*pool
= pool_pb_manager(mgr
);
241 pipe_mutex_lock(pool
->mutex
);
245 pb_unmap(pool
->buffer
);
246 pb_reference(&pool
->buffer
, NULL
);
248 pipe_mutex_unlock(pool
->mutex
);
255 pool_bufmgr_create(struct pb_manager
*provider
,
258 const struct pb_desc
*desc
)
260 struct pool_pb_manager
*pool
;
261 struct pool_buffer
*pool_buf
;
267 pool
= CALLOC_STRUCT(pool_pb_manager
);
271 pool
->base
.destroy
= pool_bufmgr_destroy
;
272 pool
->base
.create_buffer
= pool_bufmgr_create_buffer
;
273 pool
->base
.flush
= pool_bufmgr_flush
;
275 LIST_INITHEAD(&pool
->free
);
277 pool
->numTot
= numBufs
;
278 pool
->numFree
= numBufs
;
279 pool
->bufSize
= bufSize
;
280 pool
->bufAlign
= desc
->alignment
;
282 pipe_mutex_init(pool
->mutex
);
284 pool
->buffer
= provider
->create_buffer(provider
, numBufs
*bufSize
, desc
);
288 pool
->map
= pb_map(pool
->buffer
,
290 PB_USAGE_CPU_WRITE
, NULL
);
294 pool
->bufs
= (struct pool_buffer
*)CALLOC(numBufs
, sizeof(*pool
->bufs
));
298 pool_buf
= pool
->bufs
;
299 for (i
= 0; i
< numBufs
; ++i
) {
300 pipe_reference_init(&pool_buf
->base
.base
.reference
, 0);
301 pool_buf
->base
.base
.alignment
= 0;
302 pool_buf
->base
.base
.usage
= 0;
303 pool_buf
->base
.base
.size
= bufSize
;
304 pool_buf
->base
.vtbl
= &pool_buffer_vtbl
;
305 pool_buf
->mgr
= pool
;
306 pool_buf
->start
= i
* bufSize
;
307 LIST_ADDTAIL(&pool_buf
->head
, &pool
->free
);
317 pb_unmap(pool
->buffer
);
319 pb_reference(&pool
->buffer
, NULL
);