Merge branch 'gallium-embedded'
[mesa.git] / src / gallium / auxiliary / util / u_inlines.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 #ifndef U_INLINES_H
29 #define U_INLINES_H
30
31 #include "pipe/p_context.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_state.h"
34 #include "pipe/p_screen.h"
35 #include "util/u_debug.h"
36 #include "util/u_atomic.h"
37
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43
44 /*
45 * Reference counting helper functions.
46 */
47
48
49 static INLINE void
50 pipe_reference_init(struct pipe_reference *reference, unsigned count)
51 {
52 p_atomic_set(&reference->count, count);
53 }
54
55 static INLINE boolean
56 pipe_is_referenced(struct pipe_reference *reference)
57 {
58 return p_atomic_read(&reference->count) != 0;
59 }
60
61 /**
62 * Update reference counting.
63 * The old thing pointed to, if any, will be unreferenced.
64 * Both 'ptr' and 'reference' may be NULL.
65 * \return TRUE if the object's refcount hits zero and should be destroyed.
66 */
67 static INLINE boolean
68 pipe_reference(struct pipe_reference *ptr, struct pipe_reference *reference)
69 {
70 boolean destroy = FALSE;
71
72 if(ptr != reference) {
73 /* bump the reference.count first */
74 if (reference) {
75 assert(pipe_is_referenced(reference));
76 p_atomic_inc(&reference->count);
77 }
78
79 if (ptr) {
80 assert(pipe_is_referenced(ptr));
81 if (p_atomic_dec_zero(&ptr->count)) {
82 destroy = TRUE;
83 }
84 }
85 }
86
87 return destroy;
88 }
89
90 static INLINE void
91 pipe_buffer_reference(struct pipe_buffer **ptr, struct pipe_buffer *buf)
92 {
93 struct pipe_buffer *old_buf = *ptr;
94
95 if (pipe_reference(&(*ptr)->reference, &buf->reference))
96 old_buf->screen->buffer_destroy(old_buf);
97 *ptr = buf;
98 }
99
100 static INLINE void
101 pipe_surface_reference(struct pipe_surface **ptr, struct pipe_surface *surf)
102 {
103 struct pipe_surface *old_surf = *ptr;
104
105 if (pipe_reference(&(*ptr)->reference, &surf->reference))
106 old_surf->texture->screen->tex_surface_destroy(old_surf);
107 *ptr = surf;
108 }
109
110 static INLINE void
111 pipe_texture_reference(struct pipe_texture **ptr, struct pipe_texture *tex)
112 {
113 struct pipe_texture *old_tex = *ptr;
114
115 if (pipe_reference(&(*ptr)->reference, &tex->reference))
116 old_tex->screen->texture_destroy(old_tex);
117 *ptr = tex;
118 }
119
120
121 /*
122 * Convenience wrappers for screen buffer functions.
123 */
124
125 static INLINE struct pipe_buffer *
126 pipe_buffer_create( struct pipe_screen *screen,
127 unsigned alignment, unsigned usage, unsigned size )
128 {
129 return screen->buffer_create(screen, alignment, usage, size);
130 }
131
132 static INLINE struct pipe_buffer *
133 pipe_user_buffer_create( struct pipe_screen *screen, void *ptr, unsigned size )
134 {
135 return screen->user_buffer_create(screen, ptr, size);
136 }
137
138 static INLINE void *
139 pipe_buffer_map(struct pipe_screen *screen,
140 struct pipe_buffer *buf,
141 unsigned usage)
142 {
143 if(screen->buffer_map_range) {
144 unsigned offset = 0;
145 unsigned length = buf->size;
146 return screen->buffer_map_range(screen, buf, offset, length, usage);
147 }
148 else
149 return screen->buffer_map(screen, buf, usage);
150 }
151
152 static INLINE void
153 pipe_buffer_unmap(struct pipe_screen *screen,
154 struct pipe_buffer *buf)
155 {
156 screen->buffer_unmap(screen, buf);
157 }
158
159 static INLINE void *
160 pipe_buffer_map_range(struct pipe_screen *screen,
161 struct pipe_buffer *buf,
162 unsigned offset,
163 unsigned length,
164 unsigned usage)
165 {
166 assert(offset < buf->size);
167 assert(offset + length <= buf->size);
168 assert(length);
169 if(screen->buffer_map_range)
170 return screen->buffer_map_range(screen, buf, offset, length, usage);
171 else
172 return screen->buffer_map(screen, buf, usage);
173 }
174
175 static INLINE void
176 pipe_buffer_flush_mapped_range(struct pipe_screen *screen,
177 struct pipe_buffer *buf,
178 unsigned offset,
179 unsigned length)
180 {
181 assert(offset < buf->size);
182 assert(offset + length <= buf->size);
183 assert(length);
184 if(screen->buffer_flush_mapped_range)
185 screen->buffer_flush_mapped_range(screen, buf, offset, length);
186 }
187
188 static INLINE void
189 pipe_buffer_write(struct pipe_screen *screen,
190 struct pipe_buffer *buf,
191 unsigned offset, unsigned size,
192 const void *data)
193 {
194 void *map;
195
196 assert(offset < buf->size);
197 assert(offset + size <= buf->size);
198 assert(size);
199
200 map = pipe_buffer_map_range(screen, buf, offset, size,
201 PIPE_BUFFER_USAGE_CPU_WRITE |
202 PIPE_BUFFER_USAGE_FLUSH_EXPLICIT |
203 PIPE_BUFFER_USAGE_DISCARD);
204 assert(map);
205 if(map) {
206 memcpy((uint8_t *)map + offset, data, size);
207 pipe_buffer_flush_mapped_range(screen, buf, offset, size);
208 pipe_buffer_unmap(screen, buf);
209 }
210 }
211
212 /**
213 * Special case for writing non-overlapping ranges.
214 *
215 * We can avoid GPU/CPU synchronization when writing range that has never
216 * been written before.
217 */
218 static INLINE void
219 pipe_buffer_write_nooverlap(struct pipe_screen *screen,
220 struct pipe_buffer *buf,
221 unsigned offset, unsigned size,
222 const void *data)
223 {
224 void *map;
225
226 assert(offset < buf->size);
227 assert(offset + size <= buf->size);
228 assert(size);
229
230 map = pipe_buffer_map_range(screen, buf, offset, size,
231 PIPE_BUFFER_USAGE_CPU_WRITE |
232 PIPE_BUFFER_USAGE_FLUSH_EXPLICIT |
233 PIPE_BUFFER_USAGE_DISCARD |
234 PIPE_BUFFER_USAGE_UNSYNCHRONIZED);
235 assert(map);
236 if(map) {
237 memcpy((uint8_t *)map + offset, data, size);
238 pipe_buffer_flush_mapped_range(screen, buf, offset, size);
239 pipe_buffer_unmap(screen, buf);
240 }
241 }
242
243 static INLINE void
244 pipe_buffer_read(struct pipe_screen *screen,
245 struct pipe_buffer *buf,
246 unsigned offset, unsigned size,
247 void *data)
248 {
249 void *map;
250
251 assert(offset < buf->size);
252 assert(offset + size <= buf->size);
253 assert(size);
254
255 map = pipe_buffer_map_range(screen, buf, offset, size, PIPE_BUFFER_USAGE_CPU_READ);
256 assert(map);
257 if(map) {
258 memcpy(data, (const uint8_t *)map + offset, size);
259 pipe_buffer_unmap(screen, buf);
260 }
261 }
262
263 static INLINE void *
264 pipe_transfer_map( struct pipe_transfer *transf )
265 {
266 struct pipe_screen *screen = transf->texture->screen;
267 return screen->transfer_map(screen, transf);
268 }
269
270 static INLINE void
271 pipe_transfer_unmap( struct pipe_transfer *transf )
272 {
273 struct pipe_screen *screen = transf->texture->screen;
274 screen->transfer_unmap(screen, transf);
275 }
276
277 static INLINE void
278 pipe_transfer_destroy( struct pipe_transfer *transf )
279 {
280 struct pipe_screen *screen = transf->texture->screen;
281 screen->tex_transfer_destroy(transf);
282 }
283
284 static INLINE unsigned
285 pipe_transfer_buffer_flags( struct pipe_transfer *transf )
286 {
287 switch (transf->usage & PIPE_TRANSFER_READ_WRITE) {
288 case PIPE_TRANSFER_READ_WRITE:
289 return PIPE_BUFFER_USAGE_CPU_READ | PIPE_BUFFER_USAGE_CPU_WRITE;
290 case PIPE_TRANSFER_READ:
291 return PIPE_BUFFER_USAGE_CPU_READ;
292 case PIPE_TRANSFER_WRITE:
293 return PIPE_BUFFER_USAGE_CPU_WRITE;
294 default:
295 debug_assert(0);
296 return 0;
297 }
298 }
299
300 #ifdef __cplusplus
301 }
302 #endif
303
304 #endif /* U_INLINES_H */