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
)
123 struct pool_buffer
*pool_buf
= pool_buffer(buf
);
124 struct pool_pb_manager
*pool
= pool_buf
->mgr
;
127 pipe_mutex_lock(pool
->mutex
);
128 map
= (unsigned char *) pool
->map
+ pool_buf
->start
;
129 pipe_mutex_unlock(pool
->mutex
);
135 pool_buffer_unmap(struct pb_buffer
*buf
)
141 static enum pipe_error
142 pool_buffer_validate(struct pb_buffer
*buf
,
143 struct pb_validate
*vl
,
146 struct pool_buffer
*pool_buf
= pool_buffer(buf
);
147 struct pool_pb_manager
*pool
= pool_buf
->mgr
;
148 return pb_validate(pool
->buffer
, vl
, flags
);
153 pool_buffer_fence(struct pb_buffer
*buf
,
154 struct pipe_fence_handle
*fence
)
156 struct pool_buffer
*pool_buf
= pool_buffer(buf
);
157 struct pool_pb_manager
*pool
= pool_buf
->mgr
;
158 pb_fence(pool
->buffer
, fence
);
163 pool_buffer_get_base_buffer(struct pb_buffer
*buf
,
164 struct pb_buffer
**base_buf
,
167 struct pool_buffer
*pool_buf
= pool_buffer(buf
);
168 struct pool_pb_manager
*pool
= pool_buf
->mgr
;
169 pb_get_base_buffer(pool
->buffer
, base_buf
, offset
);
170 *offset
+= pool_buf
->start
;
174 static const struct pb_vtbl
179 pool_buffer_validate
,
181 pool_buffer_get_base_buffer
185 static struct pb_buffer
*
186 pool_bufmgr_create_buffer(struct pb_manager
*mgr
,
188 const struct pb_desc
*desc
)
190 struct pool_pb_manager
*pool
= pool_pb_manager(mgr
);
191 struct pool_buffer
*pool_buf
;
192 struct list_head
*item
;
194 assert(size
== pool
->bufSize
);
195 assert(pool
->bufAlign
% desc
->alignment
== 0);
197 pipe_mutex_lock(pool
->mutex
);
199 if (pool
->numFree
== 0) {
200 pipe_mutex_unlock(pool
->mutex
);
201 debug_printf("warning: out of fixed size buffer objects\n");
205 item
= pool
->free
.next
;
207 if (item
== &pool
->free
) {
208 pipe_mutex_unlock(pool
->mutex
);
209 debug_printf("error: fixed size buffer pool corruption\n");
216 pipe_mutex_unlock(pool
->mutex
);
218 pool_buf
= LIST_ENTRY(struct pool_buffer
, item
, head
);
219 assert(!pipe_is_referenced(&pool_buf
->base
.base
.reference
));
220 pipe_reference_init(&pool_buf
->base
.base
.reference
, 1);
221 pool_buf
->base
.base
.alignment
= desc
->alignment
;
222 pool_buf
->base
.base
.usage
= desc
->usage
;
224 return SUPER(pool_buf
);
229 pool_bufmgr_flush(struct pb_manager
*mgr
)
236 pool_bufmgr_destroy(struct pb_manager
*mgr
)
238 struct pool_pb_manager
*pool
= pool_pb_manager(mgr
);
239 pipe_mutex_lock(pool
->mutex
);
243 pb_unmap(pool
->buffer
);
244 pb_reference(&pool
->buffer
, NULL
);
246 pipe_mutex_unlock(pool
->mutex
);
253 pool_bufmgr_create(struct pb_manager
*provider
,
256 const struct pb_desc
*desc
)
258 struct pool_pb_manager
*pool
;
259 struct pool_buffer
*pool_buf
;
265 pool
= CALLOC_STRUCT(pool_pb_manager
);
269 pool
->base
.destroy
= pool_bufmgr_destroy
;
270 pool
->base
.create_buffer
= pool_bufmgr_create_buffer
;
271 pool
->base
.flush
= pool_bufmgr_flush
;
273 LIST_INITHEAD(&pool
->free
);
275 pool
->numTot
= numBufs
;
276 pool
->numFree
= numBufs
;
277 pool
->bufSize
= bufSize
;
278 pool
->bufAlign
= desc
->alignment
;
280 pipe_mutex_init(pool
->mutex
);
282 pool
->buffer
= provider
->create_buffer(provider
, numBufs
*bufSize
, desc
);
286 pool
->map
= pb_map(pool
->buffer
,
287 PIPE_BUFFER_USAGE_CPU_READ
|
288 PIPE_BUFFER_USAGE_CPU_WRITE
);
292 pool
->bufs
= (struct pool_buffer
*)CALLOC(numBufs
, sizeof(*pool
->bufs
));
296 pool_buf
= pool
->bufs
;
297 for (i
= 0; i
< numBufs
; ++i
) {
298 pipe_reference_init(&pool_buf
->base
.base
.reference
, 0);
299 pool_buf
->base
.base
.alignment
= 0;
300 pool_buf
->base
.base
.usage
= 0;
301 pool_buf
->base
.base
.size
= bufSize
;
302 pool_buf
->base
.vtbl
= &pool_buffer_vtbl
;
303 pool_buf
->mgr
= pool
;
304 pool_buf
->start
= i
* bufSize
;
305 LIST_ADDTAIL(&pool_buf
->head
, &pool
->free
);
315 pb_unmap(pool
->buffer
);
317 pb_reference(&pool
->buffer
, NULL
);