1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 Jose Fonseca <[email protected]>
41 */
42
43 #ifndef PB_BUFFER_H_
44 #define PB_BUFFER_H_
45
46
47 #include "util/compiler.h"
48 #include "util/u_debug.h"
49 #include "util/u_inlines.h"
50 #include "pipe/p_defines.h"
51
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56
57
58 struct pb_vtbl;
59 struct pb_validate;
60 struct pipe_fence_handle;
61
62 enum pb_usage_flags {
63 PB_USAGE_CPU_READ = (1 << 0),
64 PB_USAGE_CPU_WRITE = (1 << 1),
65 PB_USAGE_GPU_READ = (1 << 2),
66 PB_USAGE_GPU_WRITE = (1 << 3),
67 PB_USAGE_DONTBLOCK = (1 << 4),
68 PB_USAGE_UNSYNCHRONIZED = (1 << 5),
69 /* Persistent mappings may remain across a flush. Note that contrary
70 * to OpenGL persistent maps, there is no requirement at the pipebuffer
71 * api level to explicitly enforce coherency by barriers or range flushes.
72 */
73 PB_USAGE_PERSISTENT = (1 << 8)
74 };
75
76 /* For error checking elsewhere */
77 #define PB_USAGE_ALL (PB_USAGE_CPU_READ | \
78 PB_USAGE_CPU_WRITE | \
79 PB_USAGE_GPU_READ | \
80 PB_USAGE_GPU_WRITE | \
81 PB_USAGE_DONTBLOCK | \
82 PB_USAGE_UNSYNCHRONIZED | \
83 PB_USAGE_PERSISTENT)
84
85 #define PB_USAGE_CPU_READ_WRITE (PB_USAGE_CPU_READ | PB_USAGE_CPU_WRITE)
86 #define PB_USAGE_GPU_READ_WRITE (PB_USAGE_GPU_READ | PB_USAGE_GPU_WRITE)
87 #define PB_USAGE_WRITE (PB_USAGE_CPU_WRITE | PB_USAGE_GPU_WRITE)
88
89
90 /**
91 * Buffer description.
92 *
93 * Used when allocating the buffer.
94 */
95 struct pb_desc
96 {
97 unsigned alignment;
98 enum pb_usage_flags usage;
99 };
100
101
102 /**
103 * 64-bit type for GPU buffer sizes and offsets.
104 */
105 typedef uint64_t pb_size;
106
107
108 /**
109 * Base class for all pb_* buffers without the vtbl pointer.
110 */
111 struct pb_buffer_lean
112 {
113 struct pipe_reference reference;
114
115 /* For internal driver use. It's here so as not to waste space due to
116 * type alignment. (pahole)
117 */
118 uint8_t placement;
119
120 /* Alignments are powers of two, so store only the bit position.
121 * alignment_log2 = util_logbase2(alignment);
122 * alignment = 1 << alignment_log2;
123 */
124 uint8_t alignment_log2;
125
126 /**
127 * Used with pb_usage_flags or driver-specific flags, depending on drivers.
128 */
129 uint16_t usage;
130
131 pb_size size;
132 };
133
134
135 /**
136 * Base class for all pb_* buffers with the vtbl pointer.
137 */
138 struct pb_buffer
139 {
140 struct pb_buffer_lean base;
141
142 /**
143 * Pointer to the virtual function table.
144 *
145 * Avoid accessing this table directly. Use the inline functions below
146 * instead to avoid mistakes.
147 */
148 const struct pb_vtbl *vtbl;
149 };
150
151
152 /**
153 * Virtual function table for the buffer storage operations.
154 *
155 * Note that creation is not done through this table.
156 */
157 struct pb_vtbl
158 {
159 void (*destroy)(void *winsys, struct pb_buffer *buf);
160
161 /**
162 * Map the entire data store of a buffer object into the client's address.
163 * flags is bitmask of PB_USAGE_CPU_READ/WRITE.
164 */
165 void *(*map)(struct pb_buffer *buf,
166 enum pb_usage_flags flags, void *flush_ctx);
167
168 void (*unmap)(struct pb_buffer *buf);
169
170 enum pipe_error (*validate)(struct pb_buffer *buf,
171 struct pb_validate *vl,
172 enum pb_usage_flags flags);
173
174 void (*fence)(struct pb_buffer *buf,
175 struct pipe_fence_handle *fence);
176
177 /**
178 * Get the base buffer and the offset.
179 *
180 * A buffer can be subdivided in smaller buffers. This method should return
181 * the underlaying buffer, and the relative offset.
182 *
183 * Buffers without an underlaying base buffer should return themselves, with
184 * a zero offset.
185 *
186 * Note that this will increase the reference count of the base buffer.
187 */
188 void (*get_base_buffer)(struct pb_buffer *buf,
189 struct pb_buffer **base_buf,
190 pb_size *offset);
191 };
192
193
194
195 /* Accessor functions for pb->vtbl:
196 */
197 static inline void *
pb_map(struct pb_buffer * buf,enum pb_usage_flags flags,void * flush_ctx)198 pb_map(struct pb_buffer *buf, enum pb_usage_flags flags, void *flush_ctx)
199 {
200 assert(buf);
201 if (!buf)
202 return NULL;
203 assert(pipe_is_referenced(&buf->base.reference));
204 return buf->vtbl->map(buf, flags, flush_ctx);
205 }
206
207
208 static inline void
pb_unmap(struct pb_buffer * buf)209 pb_unmap(struct pb_buffer *buf)
210 {
211 assert(buf);
212 if (!buf)
213 return;
214 assert(pipe_is_referenced(&buf->base.reference));
215 buf->vtbl->unmap(buf);
216 }
217
218
219 static inline void
pb_get_base_buffer(struct pb_buffer * buf,struct pb_buffer ** base_buf,pb_size * offset)220 pb_get_base_buffer(struct pb_buffer *buf,
221 struct pb_buffer **base_buf,
222 pb_size *offset)
223 {
224 assert(buf);
225 if (!buf) {
226 base_buf = NULL;
227 offset = NULL;
228 return;
229 }
230 assert(pipe_is_referenced(&buf->base.reference));
231 assert(buf->vtbl->get_base_buffer);
232 buf->vtbl->get_base_buffer(buf, base_buf, offset);
233 assert(*base_buf);
234 assert(*offset < (*base_buf)->base.size);
235 }
236
237
238 static inline enum pipe_error
pb_validate(struct pb_buffer * buf,struct pb_validate * vl,enum pb_usage_flags flags)239 pb_validate(struct pb_buffer *buf, struct pb_validate *vl,
240 enum pb_usage_flags flags)
241 {
242 assert(buf);
243 if (!buf)
244 return PIPE_ERROR;
245 assert(buf->vtbl->validate);
246 return buf->vtbl->validate(buf, vl, flags);
247 }
248
249
250 static inline void
pb_fence(struct pb_buffer * buf,struct pipe_fence_handle * fence)251 pb_fence(struct pb_buffer *buf, struct pipe_fence_handle *fence)
252 {
253 assert(buf);
254 if (!buf)
255 return;
256 assert(buf->vtbl->fence);
257 buf->vtbl->fence(buf, fence);
258 }
259
260
261 static inline void
pb_destroy(void * winsys,struct pb_buffer * buf)262 pb_destroy(void *winsys, struct pb_buffer *buf)
263 {
264 assert(buf);
265 if (!buf)
266 return;
267
268 /* we can't assert(!pipe_is_referenced(&buf->reference)) because the winsys
269 * might have means to revive a buf whose refcount reaches 0, such as when
270 * destroy and import race against each other
271 */
272 buf->vtbl->destroy(winsys, buf);
273 }
274
275
276 static inline void
pb_reference(struct pb_buffer ** dst,struct pb_buffer * src)277 pb_reference(struct pb_buffer **dst,
278 struct pb_buffer *src)
279 {
280 struct pb_buffer *old = *dst;
281
282 if (pipe_reference(&(*dst)->base.reference, &src->base.reference))
283 pb_destroy(NULL, old);
284 *dst = src;
285 }
286
287 static inline void
pb_reference_with_winsys(void * winsys,struct pb_buffer ** dst,struct pb_buffer * src)288 pb_reference_with_winsys(void *winsys,
289 struct pb_buffer **dst,
290 struct pb_buffer *src)
291 {
292 struct pb_buffer *old = *dst;
293
294 if (pipe_reference(&(*dst)->base.reference, &src->base.reference))
295 pb_destroy(winsys, old);
296 *dst = src;
297 }
298
299 /**
300 * Utility function to check whether the provided alignment is consistent with
301 * the requested or not.
302 */
303 static inline bool
pb_check_alignment(uint32_t requested,uint32_t provided)304 pb_check_alignment(uint32_t requested, uint32_t provided)
305 {
306 if (!requested)
307 return true;
308 if (requested > provided)
309 return false;
310 if (provided % requested != 0)
311 return false;
312 return true;
313 }
314
315
316 /**
317 * Utility function to check whether the provided alignment is consistent with
318 * the requested or not.
319 */
320 static inline bool
pb_check_usage(unsigned requested,unsigned provided)321 pb_check_usage(unsigned requested, unsigned provided)
322 {
323 return (requested & provided) == requested ? true : false;
324 }
325
326 #ifdef __cplusplus
327 }
328 #endif
329
330 #endif /*PB_BUFFER_H_*/
331