1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 TUNGSTEN GRAPHICS 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.
26 **************************************************************************/
30 * Generic code for buffers.
32 * Behind a pipe buffle handle there can be DMA buffers, client (or user)
33 * buffers, regular malloced buffers, etc. This file provides an abstract base
34 * buffer handle that allows the driver to cope with all those kinds of buffers
35 * in a more flexible way.
37 * There is no obligation of a winsys driver to use this library. And a pipe
38 * driver should be completly agnostic about it.
40 * \author Jos� Fonseca <jrfonseca@tungstengraphics.com>
47 #include "pipe/p_compiler.h"
48 #include "pipe/p_debug.h"
49 #include "pipe/p_state.h"
50 #include "pipe/p_inlines.h"
63 * Used when allocating the buffer.
73 * Base class for all pb_* buffers.
77 struct pipe_buffer base
;
80 * Pointer to the virtual function table.
82 * Avoid accessing this table directly. Use the inline functions below
83 * instead to avoid mistakes.
85 const struct pb_vtbl
*vtbl
;
90 * Virtual function table for the buffer storage operations.
92 * Note that creation is not done through this table.
96 void (*destroy
)( struct pb_buffer
*buf
);
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.
102 void *(*map
)( struct pb_buffer
*buf
,
105 void (*unmap
)( struct pb_buffer
*buf
);
108 * Get the base buffer and the offset.
110 * A buffer can be subdivided in smaller buffers. This method should return
111 * the underlaying buffer, and the relative offset.
113 * Buffers without an underlaying base buffer should return themselves, with
116 * Note that this will increase the reference count of the base buffer.
118 void (*get_base_buffer
)( struct pb_buffer
*buf
,
119 struct pb_buffer
**base_buf
,
124 static INLINE
struct pipe_buffer
*
125 pb_pipe_buffer( struct pb_buffer
*pbuf
)
132 static INLINE
struct pb_buffer
*
133 pb_buffer( struct pipe_buffer
*buf
)
136 /* Could add a magic cookie check on debug builds.
138 return (struct pb_buffer
*)buf
;
142 /* Accessor functions for pb->vtbl:
145 pb_map(struct pb_buffer
*buf
,
151 assert(buf
->base
.refcount
> 0);
152 return buf
->vtbl
->map(buf
, flags
);
157 pb_unmap(struct pb_buffer
*buf
)
162 assert(buf
->base
.refcount
> 0);
163 buf
->vtbl
->unmap(buf
);
168 pb_get_base_buffer( struct pb_buffer
*buf
,
169 struct pb_buffer
**base_buf
,
178 assert(buf
->base
.refcount
> 0);
179 buf
->vtbl
->get_base_buffer(buf
, base_buf
, offset
);
181 assert(*offset
< (*base_buf
)->base
.size
);
186 pb_destroy(struct pb_buffer
*buf
)
191 assert(buf
->base
.refcount
== 0);
192 buf
->vtbl
->destroy(buf
);
196 /* XXX: thread safety issues!
199 pb_reference(struct pb_buffer
**dst
,
200 struct pb_buffer
*src
)
203 assert(src
->base
.refcount
);
204 src
->base
.refcount
++;
208 assert((*dst
)->base
.refcount
);
209 if(--(*dst
)->base
.refcount
== 0)
218 * Utility function to check whether the provided alignment is consistent with
219 * the requested or not.
221 static INLINE boolean
222 pb_check_alignment(size_t requested
, size_t provided
)
224 return requested
<= provided
&& (provided
% requested
) == 0 ? TRUE
: FALSE
;
229 * Utility function to check whether the provided alignment is consistent with
230 * the requested or not.
232 static INLINE boolean
233 pb_check_usage(unsigned requested
, unsigned provided
)
235 return (requested
& provided
) == requested
? TRUE
: FALSE
;
240 * Malloc-based buffer to store data that can't be used by the graphics
244 pb_malloc_buffer_create(size_t size
,
245 const struct pb_desc
*desc
);
252 #endif /*PB_BUFFER_H_*/