Squashed commit of the following:
[profile/ivi/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 #include "util/u_box.h"
38
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44
45 /*
46  * Reference counting helper functions.
47  */
48
49
50 static INLINE void
51 pipe_reference_init(struct pipe_reference *reference, unsigned count)
52 {
53    p_atomic_set(&reference->count, count);
54 }
55
56 static INLINE boolean
57 pipe_is_referenced(struct pipe_reference *reference)
58 {
59    return p_atomic_read(&reference->count) != 0;
60 }
61
62 /**
63  * Update reference counting.
64  * The old thing pointed to, if any, will be unreferenced.
65  * Both 'ptr' and 'reference' may be NULL.
66  * \return TRUE if the object's refcount hits zero and should be destroyed.
67  */
68 static INLINE boolean
69 pipe_reference(struct pipe_reference *ptr, struct pipe_reference *reference)
70 {
71    boolean destroy = FALSE;
72
73    if(ptr != reference) {
74       /* bump the reference.count first */
75       if (reference) {
76          assert(pipe_is_referenced(reference));
77          p_atomic_inc(&reference->count);
78       }
79
80       if (ptr) {
81          assert(pipe_is_referenced(ptr));
82          if (p_atomic_dec_zero(&ptr->count)) {
83             destroy = TRUE;
84          }
85       }
86    }
87
88    return destroy;
89 }
90
91
92 static INLINE void
93 pipe_surface_reference(struct pipe_surface **ptr, struct pipe_surface *surf)
94 {
95    struct pipe_surface *old_surf = *ptr;
96
97    if (pipe_reference(&(*ptr)->reference, &surf->reference))
98       old_surf->texture->screen->tex_surface_destroy(old_surf);
99    *ptr = surf;
100 }
101
102
103 static INLINE void
104 pipe_resource_reference(struct pipe_resource **ptr, struct pipe_resource *tex)
105 {
106    struct pipe_resource *old_tex = *ptr;
107
108    if (pipe_reference(&(*ptr)->reference, &tex->reference))
109       old_tex->screen->resource_destroy(old_tex->screen, old_tex);
110    *ptr = tex;
111 }
112
113
114 static INLINE void
115 pipe_sampler_view_reference(struct pipe_sampler_view **ptr, struct pipe_sampler_view *view)
116 {
117    struct pipe_sampler_view *old_view = *ptr;
118
119    if (pipe_reference(&(*ptr)->reference, &view->reference))
120       old_view->context->sampler_view_destroy(old_view->context, old_view);
121    *ptr = view;
122 }
123
124
125 /*
126  * Convenience wrappers for screen buffer functions.
127  */
128
129 static INLINE struct pipe_resource *
130 pipe_buffer_create( struct pipe_screen *screen,
131                     unsigned bind,
132                     unsigned size )
133 {
134    struct pipe_resource buffer;
135    memset(&buffer, 0, sizeof buffer);
136    buffer.target = PIPE_BUFFER;
137    buffer.format = PIPE_FORMAT_R8_UNORM; /* want TYPELESS or similar */
138    buffer.bind = bind;
139    buffer._usage = PIPE_USAGE_DEFAULT;
140    buffer.flags = 0;
141    buffer.width0 = size;
142    buffer.height0 = 1;
143    buffer.depth0 = 1;
144    return screen->resource_create(screen, &buffer);
145 }
146
147
148 static INLINE struct pipe_resource *
149 pipe_user_buffer_create( struct pipe_screen *screen, void *ptr, unsigned size,
150                          unsigned usage )
151 {
152    return screen->user_buffer_create(screen, ptr, size, usage);
153 }
154
155 static INLINE void *
156 pipe_buffer_map_range(struct pipe_context *pipe,
157                       struct pipe_resource *buffer,
158                       unsigned offset,
159                       unsigned length,
160                       unsigned usage,
161                       struct pipe_transfer **transfer)
162 {
163    struct pipe_box box;
164    void *map;
165
166    assert(offset < buffer->width0);
167    assert(offset + length <= buffer->width0);
168    assert(length);
169    
170    u_box_1d(offset, length, &box);
171
172    *transfer = pipe->get_transfer( pipe,
173                                    buffer,
174                                    u_subresource(0, 0),
175                                    usage,
176                                    &box);
177    
178    if (*transfer == NULL)
179       return NULL;
180
181    map = pipe->transfer_map( pipe, *transfer );
182    if (map == NULL) {
183       pipe->transfer_destroy( pipe, *transfer );
184       return NULL;
185    }
186
187    /* Match old screen->buffer_map_range() behaviour, return pointer
188     * to where the beginning of the buffer would be:
189     */
190    return (void *)((char *)map - offset);
191 }
192
193
194 static INLINE void *
195 pipe_buffer_map(struct pipe_context *pipe,
196                 struct pipe_resource *buffer,
197                 unsigned usage,
198                 struct pipe_transfer **transfer)
199 {
200    return pipe_buffer_map_range(pipe, buffer, 0, buffer->width0, usage, transfer);
201 }
202
203
204 static INLINE void
205 pipe_buffer_unmap(struct pipe_context *pipe,
206                   struct pipe_resource *buf,
207                   struct pipe_transfer *transfer)
208 {
209    if (transfer) {
210       pipe->transfer_unmap(pipe, transfer);
211       pipe->transfer_destroy(pipe, transfer);
212    }
213 }
214
215 static INLINE void
216 pipe_buffer_flush_mapped_range(struct pipe_context *pipe,
217                                struct pipe_transfer *transfer,
218                                unsigned offset,
219                                unsigned length)
220 {
221    struct pipe_box box;
222    int transfer_offset;
223
224    assert(length);
225    assert(transfer->box.x <= offset);
226    assert(offset + length <= transfer->box.x + transfer->box.width);
227
228    /* Match old screen->buffer_flush_mapped_range() behaviour, where
229     * offset parameter is relative to the start of the buffer, not the
230     * mapped range.
231     */
232    transfer_offset = offset - transfer->box.x;
233    
234    u_box_1d(transfer_offset, length, &box);
235
236    pipe->transfer_flush_region(pipe, transfer, &box);
237 }
238
239 static INLINE void
240 pipe_buffer_write(struct pipe_context *pipe,
241                   struct pipe_resource *buf,
242                   unsigned offset,
243                   unsigned size,
244                   const void *data)
245 {
246    struct pipe_box box;
247
248    u_box_1d(offset, size, &box);
249
250    pipe->transfer_inline_write( pipe,
251                                 buf,
252                                 u_subresource(0,0),
253                                 PIPE_TRANSFER_WRITE,
254                                 &box,
255                                 data,
256                                 size,
257                                 0);
258 }
259
260 /**
261  * Special case for writing non-overlapping ranges.
262  *
263  * We can avoid GPU/CPU synchronization when writing range that has never
264  * been written before.
265  */
266 static INLINE void
267 pipe_buffer_write_nooverlap(struct pipe_context *pipe,
268                             struct pipe_resource *buf,
269                             unsigned offset, unsigned size,
270                             const void *data)
271 {
272    struct pipe_box box;
273
274    u_box_1d(offset, size, &box);
275
276    pipe->transfer_inline_write(pipe, 
277                                buf,
278                                u_subresource(0,0),
279                                (PIPE_TRANSFER_WRITE |
280                                 PIPE_TRANSFER_NOOVERWRITE),
281                                &box,
282                                data,
283                                0, 0);
284 }
285
286 static INLINE void
287 pipe_buffer_read(struct pipe_context *pipe,
288                  struct pipe_resource *buf,
289                  unsigned offset,
290                  unsigned size,
291                  void *data)
292 {
293    struct pipe_transfer *src_transfer;
294    ubyte *map;
295
296    map = (ubyte *) pipe_buffer_map_range(pipe,
297                                          buf,
298                                          offset, size,
299                                          PIPE_TRANSFER_READ,
300                                          &src_transfer);
301
302    if (map)
303       memcpy(data, map + offset, size);
304
305    pipe_buffer_unmap(pipe, buf, src_transfer);
306 }
307
308 static INLINE struct pipe_transfer *
309 pipe_get_transfer( struct pipe_context *context,
310                        struct pipe_resource *resource,
311                        unsigned face, unsigned level,
312                        unsigned zslice,
313                        enum pipe_transfer_usage usage,
314                        unsigned x, unsigned y,
315                        unsigned w, unsigned h)
316 {
317    struct pipe_box box;
318    u_box_2d_zslice( x, y, zslice, w, h, &box );
319    return context->get_transfer( context,
320                                  resource,
321                                  u_subresource(face, level),
322                                  usage,
323                                  &box );
324 }
325
326 static INLINE void *
327 pipe_transfer_map( struct pipe_context *context,
328                    struct pipe_transfer *transfer )
329 {
330    return context->transfer_map( context, transfer );
331 }
332
333 static INLINE void
334 pipe_transfer_unmap( struct pipe_context *context,
335                      struct pipe_transfer *transfer )
336 {
337    context->transfer_unmap( context, transfer );
338 }
339
340
341 static INLINE void
342 pipe_transfer_destroy( struct pipe_context *context, 
343                        struct pipe_transfer *transfer )
344 {
345    context->transfer_destroy(context, transfer);
346 }
347
348
349 #ifdef __cplusplus
350 }
351 #endif
352
353 #endif /* U_INLINES_H */