pb_bufmgr_pool.c revision 2af0173e9e4eefe910c6011038e7346091a9b2a4
1/************************************************************************** 2 * 3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA 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 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. 21 * 22 * The above copyright notice and this permission notice (including the 23 * next paragraph) shall be included in all copies or substantial portions 24 * of the Software. 25 * 26 * 27 **************************************************************************/ 28 29/** 30 * \file 31 * Batch buffer pool management. 32 * 33 * \author Jose Fonseca <jrfonseca-at-tungstengraphics-dot-com> 34 * \author Thomas Hellström <thomas-at-tungstengraphics-dot-com> 35 */ 36 37 38#include "pipe/p_compiler.h" 39#include "util/u_debug.h" 40#include "pipe/p_thread.h" 41#include "pipe/p_defines.h" 42#include "util/u_memory.h" 43#include "util/u_double_list.h" 44 45#include "pb_buffer.h" 46#include "pb_bufmgr.h" 47 48 49/** 50 * Convenience macro (type safe). 51 */ 52#define SUPER(__derived) (&(__derived)->base) 53 54 55struct pool_pb_manager 56{ 57 struct pb_manager base; 58 59 pipe_mutex mutex; 60 61 pb_size bufSize; 62 pb_size bufAlign; 63 64 pb_size numFree; 65 pb_size numTot; 66 67 struct list_head free; 68 69 struct pb_buffer *buffer; 70 void *map; 71 72 struct pool_buffer *bufs; 73}; 74 75 76static INLINE struct pool_pb_manager * 77pool_pb_manager(struct pb_manager *mgr) 78{ 79 assert(mgr); 80 return (struct pool_pb_manager *)mgr; 81} 82 83 84struct pool_buffer 85{ 86 struct pb_buffer base; 87 88 struct pool_pb_manager *mgr; 89 90 struct list_head head; 91 92 pb_size start; 93}; 94 95 96static INLINE struct pool_buffer * 97pool_buffer(struct pb_buffer *buf) 98{ 99 assert(buf); 100 return (struct pool_buffer *)buf; 101} 102 103 104 105static void 106pool_buffer_destroy(struct pb_buffer *buf) 107{ 108 struct pool_buffer *pool_buf = pool_buffer(buf); 109 struct pool_pb_manager *pool = pool_buf->mgr; 110 111 assert(!pipe_is_referenced(&pool_buf->base.base.reference)); 112 113 pipe_mutex_lock(pool->mutex); 114 LIST_ADD(&pool_buf->head, &pool->free); 115 pool->numFree++; 116 pipe_mutex_unlock(pool->mutex); 117} 118 119 120static void * 121pool_buffer_map(struct pb_buffer *buf, unsigned flags) 122{ 123 struct pool_buffer *pool_buf = pool_buffer(buf); 124 struct pool_pb_manager *pool = pool_buf->mgr; 125 void *map; 126 127 pipe_mutex_lock(pool->mutex); 128 map = (unsigned char *) pool->map + pool_buf->start; 129 pipe_mutex_unlock(pool->mutex); 130 return map; 131} 132 133 134static void 135pool_buffer_unmap(struct pb_buffer *buf) 136{ 137 /* No-op */ 138} 139 140 141static enum pipe_error 142pool_buffer_validate(struct pb_buffer *buf, 143 struct pb_validate *vl, 144 unsigned flags) 145{ 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); 149} 150 151 152static void 153pool_buffer_fence(struct pb_buffer *buf, 154 struct pipe_fence_handle *fence) 155{ 156 struct pool_buffer *pool_buf = pool_buffer(buf); 157 struct pool_pb_manager *pool = pool_buf->mgr; 158 pb_fence(pool->buffer, fence); 159} 160 161 162static void 163pool_buffer_get_base_buffer(struct pb_buffer *buf, 164 struct pb_buffer **base_buf, 165 pb_size *offset) 166{ 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; 171} 172 173 174static const struct pb_vtbl 175pool_buffer_vtbl = { 176 pool_buffer_destroy, 177 pool_buffer_map, 178 pool_buffer_unmap, 179 pool_buffer_validate, 180 pool_buffer_fence, 181 pool_buffer_get_base_buffer 182}; 183 184 185static struct pb_buffer * 186pool_bufmgr_create_buffer(struct pb_manager *mgr, 187 pb_size size, 188 const struct pb_desc *desc) 189{ 190 struct pool_pb_manager *pool = pool_pb_manager(mgr); 191 struct pool_buffer *pool_buf; 192 struct list_head *item; 193 194 assert(size == pool->bufSize); 195 assert(pool->bufAlign % desc->alignment == 0); 196 197 pipe_mutex_lock(pool->mutex); 198 199 if (pool->numFree == 0) { 200 pipe_mutex_unlock(pool->mutex); 201 debug_printf("warning: out of fixed size buffer objects\n"); 202 return NULL; 203 } 204 205 item = pool->free.next; 206 207 if (item == &pool->free) { 208 pipe_mutex_unlock(pool->mutex); 209 debug_printf("error: fixed size buffer pool corruption\n"); 210 return NULL; 211 } 212 213 LIST_DEL(item); 214 --pool->numFree; 215 216 pipe_mutex_unlock(pool->mutex); 217 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; 223 224 return SUPER(pool_buf); 225} 226 227 228static void 229pool_bufmgr_flush(struct pb_manager *mgr) 230{ 231 /* No-op */ 232} 233 234 235static void 236pool_bufmgr_destroy(struct pb_manager *mgr) 237{ 238 struct pool_pb_manager *pool = pool_pb_manager(mgr); 239 pipe_mutex_lock(pool->mutex); 240 241 FREE(pool->bufs); 242 243 pb_unmap(pool->buffer); 244 pb_reference(&pool->buffer, NULL); 245 246 pipe_mutex_unlock(pool->mutex); 247 248 FREE(mgr); 249} 250 251 252struct pb_manager * 253pool_bufmgr_create(struct pb_manager *provider, 254 pb_size numBufs, 255 pb_size bufSize, 256 const struct pb_desc *desc) 257{ 258 struct pool_pb_manager *pool; 259 struct pool_buffer *pool_buf; 260 pb_size i; 261 262 if(!provider) 263 return NULL; 264 265 pool = CALLOC_STRUCT(pool_pb_manager); 266 if (!pool) 267 return NULL; 268 269 pool->base.destroy = pool_bufmgr_destroy; 270 pool->base.create_buffer = pool_bufmgr_create_buffer; 271 pool->base.flush = pool_bufmgr_flush; 272 273 LIST_INITHEAD(&pool->free); 274 275 pool->numTot = numBufs; 276 pool->numFree = numBufs; 277 pool->bufSize = bufSize; 278 pool->bufAlign = desc->alignment; 279 280 pipe_mutex_init(pool->mutex); 281 282 pool->buffer = provider->create_buffer(provider, numBufs*bufSize, desc); 283 if (!pool->buffer) 284 goto failure; 285 286 pool->map = pb_map(pool->buffer, 287 PIPE_BUFFER_USAGE_CPU_READ | 288 PIPE_BUFFER_USAGE_CPU_WRITE); 289 if(!pool->map) 290 goto failure; 291 292 pool->bufs = (struct pool_buffer *)CALLOC(numBufs, sizeof(*pool->bufs)); 293 if (!pool->bufs) 294 goto failure; 295 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); 306 pool_buf++; 307 } 308 309 return SUPER(pool); 310 311failure: 312 if(pool->bufs) 313 FREE(pool->bufs); 314 if(pool->map) 315 pb_unmap(pool->buffer); 316 if(pool->buffer) 317 pb_reference(&pool->buffer, NULL); 318 if(pool) 319 FREE(pool); 320 return NULL; 321} 322