Merge commit 'origin/gallium-0.1' into gallium-0.1
[mesa.git] / src / gallium / auxiliary / pipebuffer / pb_buffer.h
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * \file
30 * Generic code for buffers.
31 *
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.
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 struct pb_vtbl;
54
55 /**
56 * Buffer description.
57 *
58 * Used when allocating the buffer.
59 */
60 struct pb_desc
61 {
62 unsigned alignment;
63 unsigned usage;
64 };
65
66
67 /**
68 * Base class for all pb_* buffers.
69 */
70 struct pb_buffer
71 {
72 struct pipe_buffer base;
73
74 /**
75 * Pointer to the virtual function table.
76 *
77 * Avoid accessing this table directly. Use the inline functions below
78 * instead to avoid mistakes.
79 */
80 const struct pb_vtbl *vtbl;
81 };
82
83
84 /**
85 * Virtual function table for the buffer storage operations.
86 *
87 * Note that creation is not done through this table.
88 */
89 struct pb_vtbl
90 {
91 void (*destroy)( struct pb_buffer *buf );
92
93 /**
94 * Map the entire data store of a buffer object into the client's address.
95 * flags is bitmask of PIPE_BUFFER_FLAG_READ/WRITE.
96 */
97 void *(*map)( struct pb_buffer *buf,
98 unsigned flags );
99
100 void (*unmap)( struct pb_buffer *buf );
101
102 /**
103 * Get the base buffer and the offset.
104 *
105 * A buffer can be subdivided in smaller buffers. This method should return
106 * the underlaying buffer, and the relative offset.
107 *
108 * Buffers without an underlaying base buffer should return themselves, with
109 * a zero offset.
110 *
111 * Note that this will increase the reference count of the base buffer.
112 */
113 void (*get_base_buffer)( struct pb_buffer *buf,
114 struct pb_buffer **base_buf,
115 unsigned *offset );
116 };
117
118
119 static INLINE struct pipe_buffer *
120 pb_pipe_buffer( struct pb_buffer *pbuf )
121 {
122 assert(pbuf);
123 return &pbuf->base;
124 }
125
126
127 static INLINE struct pb_buffer *
128 pb_buffer( struct pipe_buffer *buf )
129 {
130 assert(buf);
131 /* Could add a magic cookie check on debug builds.
132 */
133 return (struct pb_buffer *)buf;
134 }
135
136
137 /* Accessor functions for pb->vtbl:
138 */
139 static INLINE void *
140 pb_map(struct pb_buffer *buf,
141 unsigned flags)
142 {
143 assert(buf);
144 return buf->vtbl->map(buf, flags);
145 }
146
147
148 static INLINE void
149 pb_unmap(struct pb_buffer *buf)
150 {
151 assert(buf);
152 buf->vtbl->unmap(buf);
153 }
154
155
156 static INLINE void
157 pb_get_base_buffer( struct pb_buffer *buf,
158 struct pb_buffer **base_buf,
159 unsigned *offset )
160 {
161 buf->vtbl->get_base_buffer(buf, base_buf, offset);
162 }
163
164
165 static INLINE void
166 pb_destroy(struct pb_buffer *buf)
167 {
168 assert(buf);
169 buf->vtbl->destroy(buf);
170 }
171
172
173 /* XXX: thread safety issues!
174 */
175 static INLINE void
176 pb_reference(struct pb_buffer **dst,
177 struct pb_buffer *src)
178 {
179 if (src)
180 src->base.refcount++;
181
182 if (*dst && --(*dst)->base.refcount == 0)
183 pb_destroy( *dst );
184
185 *dst = src;
186 }
187
188
189 /**
190 * Malloc-based buffer to store data that can't be used by the graphics
191 * hardware.
192 */
193 struct pb_buffer *
194 pb_malloc_buffer_create(size_t size,
195 const struct pb_desc *desc);
196
197
198 void
199 pb_init_winsys(struct pipe_winsys *winsys);
200
201
202 #endif /*PB_BUFFER_H_*/