pb_buffer.h revision 781676c7cc5ae7586ee8edd07de880892c5a2d86
1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/************************************************************************** 2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * 3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * All Rights Reserved. 5f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) * 6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining a 7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch * copy of this software and associated documentation files (the 8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * "Software"), to deal in the Software without restriction, including 95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * without limitation the rights to use, copy, modify, merge, publish, 10f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) * distribute, sub license, and/or sell copies of the Software, and to 11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * permit persons to whom the Software is furnished to do so, subject to 12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * the following conditions: 13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * 14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * The above copyright notice and this permission notice (including the 15f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) * next paragraph) shall be included in all copies or substantial portions 165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * of the Software. 175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * 185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * 265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) **************************************************************************/ 275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)/** 295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * \file 306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) * Generic code for buffers. 31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch * 32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch * Behind a pipe buffle handle there can be DMA buffers, client (or user) 33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * buffers, regular malloced buffers, etc. This file provides an abstract base 34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * buffer handle that allows the driver to cope with all those kinds of buffers 35 * in a more flexible way. 36 * 37 * There is no obligation of a winsys driver to use this library. And a pipe 38 * driver should be completly agnostic about it. 39 * 40 * \author Jos� Fonseca <jrfonseca@tungstengraphics.com> 41 */ 42 43#ifndef PB_BUFFER_H_ 44#define PB_BUFFER_H_ 45 46 47#include "pipe/p_compiler.h" 48#include "pipe/p_debug.h" 49#include "pipe/p_state.h" 50#include "pipe/p_inlines.h" 51 52 53#ifdef __cplusplus 54extern "C" { 55#endif 56 57 58struct pb_vtbl; 59 60/** 61 * Buffer description. 62 * 63 * Used when allocating the buffer. 64 */ 65struct pb_desc 66{ 67 unsigned alignment; 68 unsigned usage; 69}; 70 71 72/** 73 * Base class for all pb_* buffers. 74 */ 75struct pb_buffer 76{ 77 struct pipe_buffer base; 78 79 /** 80 * Pointer to the virtual function table. 81 * 82 * Avoid accessing this table directly. Use the inline functions below 83 * instead to avoid mistakes. 84 */ 85 const struct pb_vtbl *vtbl; 86}; 87 88 89/** 90 * Virtual function table for the buffer storage operations. 91 * 92 * Note that creation is not done through this table. 93 */ 94struct pb_vtbl 95{ 96 void (*destroy)( struct pb_buffer *buf ); 97 98 /** 99 * Map the entire data store of a buffer object into the client's address. 100 * flags is bitmask of PIPE_BUFFER_FLAG_READ/WRITE. 101 */ 102 void *(*map)( struct pb_buffer *buf, 103 unsigned flags ); 104 105 void (*unmap)( struct pb_buffer *buf ); 106 107 /** 108 * Get the base buffer and the offset. 109 * 110 * A buffer can be subdivided in smaller buffers. This method should return 111 * the underlaying buffer, and the relative offset. 112 * 113 * Buffers without an underlaying base buffer should return themselves, with 114 * a zero offset. 115 * 116 * Note that this will increase the reference count of the base buffer. 117 */ 118 void (*get_base_buffer)( struct pb_buffer *buf, 119 struct pb_buffer **base_buf, 120 unsigned *offset ); 121}; 122 123 124static INLINE struct pipe_buffer * 125pb_pipe_buffer( struct pb_buffer *pbuf ) 126{ 127 assert(pbuf); 128 return &pbuf->base; 129} 130 131 132static INLINE struct pb_buffer * 133pb_buffer( struct pipe_buffer *buf ) 134{ 135 assert(buf); 136 /* Could add a magic cookie check on debug builds. 137 */ 138 return (struct pb_buffer *)buf; 139} 140 141 142/* Accessor functions for pb->vtbl: 143 */ 144static INLINE void * 145pb_map(struct pb_buffer *buf, 146 unsigned flags) 147{ 148 assert(buf); 149 if(!buf) 150 return NULL; 151 return buf->vtbl->map(buf, flags); 152} 153 154 155static INLINE void 156pb_unmap(struct pb_buffer *buf) 157{ 158 assert(buf); 159 if(!buf) 160 return; 161 buf->vtbl->unmap(buf); 162} 163 164 165static INLINE void 166pb_get_base_buffer( struct pb_buffer *buf, 167 struct pb_buffer **base_buf, 168 unsigned *offset ) 169{ 170 assert(buf); 171 if(!buf) { 172 base_buf = NULL; 173 offset = 0; 174 return; 175 } 176 buf->vtbl->get_base_buffer(buf, base_buf, offset); 177} 178 179 180static INLINE void 181pb_destroy(struct pb_buffer *buf) 182{ 183 assert(buf); 184 if(!buf) 185 return; 186 buf->vtbl->destroy(buf); 187} 188 189 190/* XXX: thread safety issues! 191 */ 192static INLINE void 193pb_reference(struct pb_buffer **dst, 194 struct pb_buffer *src) 195{ 196 if (src) 197 src->base.refcount++; 198 199 if (*dst && --(*dst)->base.refcount == 0) 200 pb_destroy( *dst ); 201 202 *dst = src; 203} 204 205 206/** 207 * Utility function to check whether a requested alignment is consistent with 208 * the provided alignment or not. 209 */ 210static INLINE int 211pb_check_alignment(size_t requested, size_t provided) 212{ 213 return requested <= provided && (provided % requested) == 0; 214} 215 216 217/** 218 * Malloc-based buffer to store data that can't be used by the graphics 219 * hardware. 220 */ 221struct pb_buffer * 222pb_malloc_buffer_create(size_t size, 223 const struct pb_desc *desc); 224 225 226void 227pb_init_winsys(struct pipe_winsys *winsys); 228 229 230#ifdef __cplusplus 231} 232#endif 233 234#endif /*PB_BUFFER_H_*/ 235