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 Jose Fonseca <jrfonseca@tungstengraphics.com>
47 #include "pipe/p_compiler.h"
48 #include "util/u_debug.h"
49 #include "util/u_inlines.h"
50 #include "pipe/p_defines.h"
51 #include "pipe/p_state.h"
66 * Used when allocating the buffer.
76 * Size. Regular (32bit) unsigned for now.
78 typedef unsigned pb_size
;
82 * Base class for all pb_* buffers.
86 struct pipe_buffer base
;
89 * Pointer to the virtual function table.
91 * Avoid accessing this table directly. Use the inline functions below
92 * instead to avoid mistakes.
94 const struct pb_vtbl
*vtbl
;
99 * Virtual function table for the buffer storage operations.
101 * Note that creation is not done through this table.
105 void (*destroy
)( struct pb_buffer
*buf
);
108 * Map the entire data store of a buffer object into the client's address.
109 * flags is bitmask of PIPE_BUFFER_FLAG_READ/WRITE.
111 void *(*map
)( struct pb_buffer
*buf
,
114 void (*unmap
)( struct pb_buffer
*buf
);
116 enum pipe_error (*validate
)( struct pb_buffer
*buf
,
117 struct pb_validate
*vl
,
120 void (*fence
)( struct pb_buffer
*buf
,
121 struct pipe_fence_handle
*fence
);
124 * Get the base buffer and the offset.
126 * A buffer can be subdivided in smaller buffers. This method should return
127 * the underlaying buffer, and the relative offset.
129 * Buffers without an underlaying base buffer should return themselves, with
132 * Note that this will increase the reference count of the base buffer.
134 void (*get_base_buffer
)( struct pb_buffer
*buf
,
135 struct pb_buffer
**base_buf
,
141 static INLINE
struct pipe_buffer
*
142 pb_pipe_buffer( struct pb_buffer
*pbuf
)
149 static INLINE
struct pb_buffer
*
150 pb_buffer( struct pipe_buffer
*buf
)
153 /* Could add a magic cookie check on debug builds.
155 return (struct pb_buffer
*)buf
;
159 /* Accessor functions for pb->vtbl:
162 pb_map(struct pb_buffer
*buf
,
168 assert(pipe_is_referenced(&buf
->base
.reference
));
169 return buf
->vtbl
->map(buf
, flags
);
174 pb_unmap(struct pb_buffer
*buf
)
179 assert(pipe_is_referenced(&buf
->base
.reference
));
180 buf
->vtbl
->unmap(buf
);
185 pb_get_base_buffer( struct pb_buffer
*buf
,
186 struct pb_buffer
**base_buf
,
195 assert(pipe_is_referenced(&buf
->base
.reference
));
196 assert(buf
->vtbl
->get_base_buffer
);
197 buf
->vtbl
->get_base_buffer(buf
, base_buf
, offset
);
199 assert(*offset
< (*base_buf
)->base
.size
);
203 static INLINE
enum pipe_error
204 pb_validate(struct pb_buffer
*buf
, struct pb_validate
*vl
, unsigned flags
)
209 assert(buf
->vtbl
->validate
);
210 return buf
->vtbl
->validate(buf
, vl
, flags
);
215 pb_fence(struct pb_buffer
*buf
, struct pipe_fence_handle
*fence
)
220 assert(buf
->vtbl
->fence
);
221 buf
->vtbl
->fence(buf
, fence
);
226 pb_destroy(struct pb_buffer
*buf
)
231 assert(!pipe_is_referenced(&buf
->base
.reference
));
232 buf
->vtbl
->destroy(buf
);
236 pb_reference(struct pb_buffer
**dst
,
237 struct pb_buffer
*src
)
239 struct pb_buffer
*old
= *dst
;
241 if (pipe_reference(&(*dst
)->base
.reference
, &src
->base
.reference
))
248 * Utility function to check whether the provided alignment is consistent with
249 * the requested or not.
251 static INLINE boolean
252 pb_check_alignment(pb_size requested
, pb_size provided
)
256 if(requested
> provided
)
258 if(provided
% requested
!= 0)
265 * Utility function to check whether the provided alignment is consistent with
266 * the requested or not.
268 static INLINE boolean
269 pb_check_usage(unsigned requested
, unsigned provided
)
271 return (requested
& provided
) == requested
? TRUE
: FALSE
;
276 * Malloc-based buffer to store data that can't be used by the graphics
280 pb_malloc_buffer_create(pb_size size
,
281 const struct pb_desc
*desc
);
288 #endif /*PB_BUFFER_H_*/