1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
46 * Reference counting helper functions.
51 pipe_reference_init(struct pipe_reference *reference, unsigned count)
53 p_atomic_set(&reference->count, count);
57 pipe_is_referenced(struct pipe_reference *reference)
59 return p_atomic_read(&reference->count) != 0;
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.
69 pipe_reference(struct pipe_reference *ptr, struct pipe_reference *reference)
71 boolean destroy = FALSE;
73 if(ptr != reference) {
74 /* bump the reference.count first */
76 assert(pipe_is_referenced(reference));
77 p_atomic_inc(&reference->count);
81 assert(pipe_is_referenced(ptr));
82 if (p_atomic_dec_zero(&ptr->count)) {
93 pipe_surface_reference(struct pipe_surface **ptr, struct pipe_surface *surf)
95 struct pipe_surface *old_surf = *ptr;
97 if (pipe_reference(&(*ptr)->reference, &surf->reference))
98 old_surf->texture->screen->tex_surface_destroy(old_surf);
104 pipe_resource_reference(struct pipe_resource **ptr, struct pipe_resource *tex)
106 struct pipe_resource *old_tex = *ptr;
108 if (pipe_reference(&(*ptr)->reference, &tex->reference))
109 old_tex->screen->resource_destroy(old_tex->screen, old_tex);
115 pipe_sampler_view_reference(struct pipe_sampler_view **ptr, struct pipe_sampler_view *view)
117 struct pipe_sampler_view *old_view = *ptr;
119 if (pipe_reference(&(*ptr)->reference, &view->reference))
120 old_view->context->sampler_view_destroy(old_view->context, old_view);
126 * Convenience wrappers for screen buffer functions.
129 static INLINE struct pipe_resource *
130 pipe_buffer_create( struct pipe_screen *screen,
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 */
139 buffer._usage = PIPE_USAGE_DEFAULT;
141 buffer.width0 = size;
144 return screen->resource_create(screen, &buffer);
148 static INLINE struct pipe_resource *
149 pipe_user_buffer_create( struct pipe_screen *screen, void *ptr, unsigned size,
152 return screen->user_buffer_create(screen, ptr, size, usage);
156 pipe_buffer_map_range(struct pipe_context *pipe,
157 struct pipe_resource *buffer,
161 struct pipe_transfer **transfer)
166 assert(offset < buffer->width0);
167 assert(offset + length <= buffer->width0);
170 u_box_1d(offset, length, &box);
172 *transfer = pipe->get_transfer( pipe,
178 if (*transfer == NULL)
181 map = pipe->transfer_map( pipe, *transfer );
183 pipe->transfer_destroy( pipe, *transfer );
187 /* Match old screen->buffer_map_range() behaviour, return pointer
188 * to where the beginning of the buffer would be:
190 return (void *)((char *)map - offset);
195 pipe_buffer_map(struct pipe_context *pipe,
196 struct pipe_resource *buffer,
198 struct pipe_transfer **transfer)
200 return pipe_buffer_map_range(pipe, buffer, 0, buffer->width0, usage, transfer);
205 pipe_buffer_unmap(struct pipe_context *pipe,
206 struct pipe_resource *buf,
207 struct pipe_transfer *transfer)
210 pipe->transfer_unmap(pipe, transfer);
211 pipe->transfer_destroy(pipe, transfer);
216 pipe_buffer_flush_mapped_range(struct pipe_context *pipe,
217 struct pipe_transfer *transfer,
225 assert(transfer->box.x <= offset);
226 assert(offset + length <= transfer->box.x + transfer->box.width);
228 /* Match old screen->buffer_flush_mapped_range() behaviour, where
229 * offset parameter is relative to the start of the buffer, not the
232 transfer_offset = offset - transfer->box.x;
234 u_box_1d(transfer_offset, length, &box);
236 pipe->transfer_flush_region(pipe, transfer, &box);
240 pipe_buffer_write(struct pipe_context *pipe,
241 struct pipe_resource *buf,
248 u_box_1d(offset, size, &box);
250 pipe->transfer_inline_write( pipe,
261 * Special case for writing non-overlapping ranges.
263 * We can avoid GPU/CPU synchronization when writing range that has never
264 * been written before.
267 pipe_buffer_write_nooverlap(struct pipe_context *pipe,
268 struct pipe_resource *buf,
269 unsigned offset, unsigned size,
274 u_box_1d(offset, size, &box);
276 pipe->transfer_inline_write(pipe,
279 (PIPE_TRANSFER_WRITE |
280 PIPE_TRANSFER_NOOVERWRITE),
287 pipe_buffer_read(struct pipe_context *pipe,
288 struct pipe_resource *buf,
293 struct pipe_transfer *src_transfer;
296 map = (ubyte *) pipe_buffer_map_range(pipe,
303 memcpy(data, map + offset, size);
305 pipe_buffer_unmap(pipe, buf, src_transfer);
308 static INLINE struct pipe_transfer *
309 pipe_get_transfer( struct pipe_context *context,
310 struct pipe_resource *resource,
311 unsigned face, unsigned level,
313 enum pipe_transfer_usage usage,
314 unsigned x, unsigned y,
315 unsigned w, unsigned h)
318 u_box_2d_zslice( x, y, zslice, w, h, &box );
319 return context->get_transfer( context,
321 u_subresource(face, level),
327 pipe_transfer_map( struct pipe_context *context,
328 struct pipe_transfer *transfer )
330 return context->transfer_map( context, transfer );
334 pipe_transfer_unmap( struct pipe_context *context,
335 struct pipe_transfer *transfer )
337 context->transfer_unmap( context, transfer );
342 pipe_transfer_destroy( struct pipe_context *context,
343 struct pipe_transfer *transfer )
345 context->transfer_destroy(context, transfer);
353 #endif /* U_INLINES_H */