• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <jfonseca@vmware.com>
41  */
42 
43 #ifndef PB_BUFFER_H_
44 #define PB_BUFFER_H_
45 
46 
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 
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.
110  */
111 struct pb_buffer
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     * Pointer to the virtual function table.
135     *
136     * Avoid accessing this table directly. Use the inline functions below
137     * instead to avoid mistakes.
138     */
139    const struct pb_vtbl *vtbl;
140 };
141 
142 
143 /**
144  * Virtual function table for the buffer storage operations.
145  *
146  * Note that creation is not done through this table.
147  */
148 struct pb_vtbl
149 {
150    void (*destroy)(void *winsys, struct pb_buffer *buf);
151 
152    /**
153     * Map the entire data store of a buffer object into the client's address.
154     * flags is bitmask of PB_USAGE_CPU_READ/WRITE.
155     */
156    void *(*map)(struct pb_buffer *buf,
157                 enum pb_usage_flags flags, void *flush_ctx);
158 
159    void (*unmap)(struct pb_buffer *buf);
160 
161    enum pipe_error (*validate)(struct pb_buffer *buf,
162                                struct pb_validate *vl,
163                                enum pb_usage_flags flags);
164 
165    void (*fence)(struct pb_buffer *buf,
166                  struct pipe_fence_handle *fence);
167 
168    /**
169     * Get the base buffer and the offset.
170     *
171     * A buffer can be subdivided in smaller buffers. This method should return
172     * the underlaying buffer, and the relative offset.
173     *
174     * Buffers without an underlaying base buffer should return themselves, with
175     * a zero offset.
176     *
177     * Note that this will increase the reference count of the base buffer.
178     */
179    void (*get_base_buffer)(struct pb_buffer *buf,
180                            struct pb_buffer **base_buf,
181                            pb_size *offset);
182 };
183 
184 
185 
186 /* Accessor functions for pb->vtbl:
187  */
188 static inline void *
pb_map(struct pb_buffer * buf,enum pb_usage_flags flags,void * flush_ctx)189 pb_map(struct pb_buffer *buf, enum pb_usage_flags flags, void *flush_ctx)
190 {
191    assert(buf);
192    if (!buf)
193       return NULL;
194    assert(pipe_is_referenced(&buf->reference));
195    return buf->vtbl->map(buf, flags, flush_ctx);
196 }
197 
198 
199 static inline void
pb_unmap(struct pb_buffer * buf)200 pb_unmap(struct pb_buffer *buf)
201 {
202    assert(buf);
203    if (!buf)
204       return;
205    assert(pipe_is_referenced(&buf->reference));
206    buf->vtbl->unmap(buf);
207 }
208 
209 
210 static inline void
pb_get_base_buffer(struct pb_buffer * buf,struct pb_buffer ** base_buf,pb_size * offset)211 pb_get_base_buffer(struct pb_buffer *buf,
212                    struct pb_buffer **base_buf,
213                    pb_size *offset)
214 {
215    assert(buf);
216    if (!buf) {
217       base_buf = NULL;
218       offset = NULL;
219       return;
220    }
221    assert(pipe_is_referenced(&buf->reference));
222    assert(buf->vtbl->get_base_buffer);
223    buf->vtbl->get_base_buffer(buf, base_buf, offset);
224    assert(*base_buf);
225    assert(*offset < (*base_buf)->size);
226 }
227 
228 
229 static inline enum pipe_error
pb_validate(struct pb_buffer * buf,struct pb_validate * vl,enum pb_usage_flags flags)230 pb_validate(struct pb_buffer *buf, struct pb_validate *vl,
231             enum pb_usage_flags flags)
232 {
233    assert(buf);
234    if (!buf)
235       return PIPE_ERROR;
236    assert(buf->vtbl->validate);
237    return buf->vtbl->validate(buf, vl, flags);
238 }
239 
240 
241 static inline void
pb_fence(struct pb_buffer * buf,struct pipe_fence_handle * fence)242 pb_fence(struct pb_buffer *buf, struct pipe_fence_handle *fence)
243 {
244    assert(buf);
245    if (!buf)
246       return;
247    assert(buf->vtbl->fence);
248    buf->vtbl->fence(buf, fence);
249 }
250 
251 
252 static inline void
pb_destroy(void * winsys,struct pb_buffer * buf)253 pb_destroy(void *winsys, struct pb_buffer *buf)
254 {
255    assert(buf);
256    if (!buf)
257       return;
258    assert(!pipe_is_referenced(&buf->reference));
259    buf->vtbl->destroy(winsys, buf);
260 }
261 
262 
263 static inline void
pb_reference(struct pb_buffer ** dst,struct pb_buffer * src)264 pb_reference(struct pb_buffer **dst,
265              struct pb_buffer *src)
266 {
267    struct pb_buffer *old = *dst;
268 
269    if (pipe_reference(&(*dst)->reference, &src->reference))
270       pb_destroy(NULL, old);
271    *dst = src;
272 }
273 
274 static inline void
pb_reference_with_winsys(void * winsys,struct pb_buffer ** dst,struct pb_buffer * src)275 pb_reference_with_winsys(void *winsys,
276                          struct pb_buffer **dst,
277                          struct pb_buffer *src)
278 {
279    struct pb_buffer *old = *dst;
280 
281    if (pipe_reference(&(*dst)->reference, &src->reference))
282       pb_destroy(winsys, old);
283    *dst = src;
284 }
285 
286 /**
287  * Utility function to check whether the provided alignment is consistent with
288  * the requested or not.
289  */
290 static inline boolean
pb_check_alignment(uint32_t requested,uint32_t provided)291 pb_check_alignment(uint32_t requested, uint32_t provided)
292 {
293    if (!requested)
294       return TRUE;
295    if (requested > provided)
296       return FALSE;
297    if (provided % requested != 0)
298       return FALSE;
299    return TRUE;
300 }
301 
302 
303 /**
304  * Utility function to check whether the provided alignment is consistent with
305  * the requested or not.
306  */
307 static inline boolean
pb_check_usage(unsigned requested,unsigned provided)308 pb_check_usage(unsigned requested, unsigned provided)
309 {
310    return (requested & provided) == requested ? TRUE : FALSE;
311 }
312 
313 #ifdef __cplusplus
314 }
315 #endif
316 
317 #endif /*PB_BUFFER_H_*/
318