Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / gallium / include / pipe / p_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 P_INLINES_H
29 #define P_INLINES_H
30
31 #include "p_context.h"
32 #include "p_defines.h"
33 #include "p_screen.h"
34 #include "p_winsys.h"
35
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41
42 static INLINE void *
43 pipe_surface_map(struct pipe_surface *surface)
44 {
45 return (char *)surface->winsys->buffer_map( surface->winsys, surface->buffer,
46 PIPE_BUFFER_USAGE_CPU_WRITE |
47 PIPE_BUFFER_USAGE_CPU_READ )
48 + surface->offset;
49 }
50
51 static INLINE void
52 pipe_surface_unmap(struct pipe_surface *surface)
53 {
54 surface->winsys->buffer_unmap( surface->winsys, surface->buffer );
55 }
56
57 /**
58 * Set 'ptr' to point to 'surf' and update reference counting.
59 * The old thing pointed to, if any, will be unreferenced first.
60 * 'surf' may be NULL.
61 */
62 static INLINE void
63 pipe_surface_reference(struct pipe_surface **ptr, struct pipe_surface *surf)
64 {
65 /* bump the refcount first */
66 if (surf)
67 surf->refcount++;
68
69 if (*ptr /* && --(*ptr)->refcount == 0 */) {
70 struct pipe_winsys *winsys = (*ptr)->winsys;
71 winsys->surface_release(winsys, ptr);
72 assert(!*ptr);
73 }
74
75 *ptr = surf;
76 }
77
78
79 /* XXX: thread safety issues!
80 */
81 static INLINE void
82 pipe_buffer_reference(struct pipe_winsys *winsys,
83 struct pipe_buffer **ptr,
84 struct pipe_buffer *buf)
85 {
86 if (buf)
87 buf->refcount++;
88
89 if (*ptr && --(*ptr)->refcount == 0)
90 winsys->buffer_destroy( winsys, *ptr );
91
92 *ptr = buf;
93 }
94
95
96
97 /**
98 * \sa pipe_surface_reference
99 */
100 static INLINE void
101 pipe_texture_reference(struct pipe_texture **ptr,
102 struct pipe_texture *pt)
103 {
104 assert(ptr);
105
106 if (pt)
107 pt->refcount++;
108
109 if (*ptr) {
110 struct pipe_screen *screen = (*ptr)->screen;
111 assert(screen);
112 screen->texture_release(screen, ptr);
113
114 assert(!*ptr);
115 }
116
117 *ptr = pt;
118 }
119
120
121 static INLINE void
122 pipe_texture_release(struct pipe_texture **ptr)
123 {
124 struct pipe_screen *screen;
125 assert(ptr);
126 screen = (*ptr)->screen;
127 screen->texture_release(screen, ptr);
128 *ptr = NULL;
129 }
130
131
132 /**
133 * Convenience wrappers for winsys buffer functions.
134 */
135
136 static INLINE struct pipe_buffer *
137 pipe_buffer_create( struct pipe_context *pipe,
138 unsigned alignment, unsigned usage, unsigned size )
139 {
140 return pipe->winsys->buffer_create(pipe->winsys, alignment, usage, size);
141 }
142
143 static INLINE struct pipe_buffer *
144 pipe_user_buffer_create( struct pipe_context *pipe, void *ptr, unsigned size )
145 {
146 return pipe->winsys->user_buffer_create(pipe->winsys, ptr, size);
147 }
148
149 static INLINE void
150 pipe_buffer_destroy( struct pipe_context *pipe, struct pipe_buffer *buf )
151 {
152 pipe->winsys->buffer_destroy(pipe->winsys, buf);
153 }
154
155 static INLINE void *
156 pipe_buffer_map(struct pipe_context *pipe,
157 struct pipe_buffer *buf,
158 unsigned usage)
159 {
160 return pipe->winsys->buffer_map(pipe->winsys, buf, usage);
161 }
162
163 static INLINE void
164 pipe_buffer_unmap(struct pipe_context *pipe,
165 struct pipe_buffer *buf)
166 {
167 pipe->winsys->buffer_unmap(pipe->winsys, buf);
168 }
169
170 /* XXX when we're using this everywhere, get rid of
171 * pipe_buffer_reference() above.
172 */
173 static INLINE void
174 pipe_reference_buffer(struct pipe_context *pipe,
175 struct pipe_buffer **ptr,
176 struct pipe_buffer *buf)
177 {
178 pipe_buffer_reference(pipe->winsys, ptr, buf);
179 }
180
181
182 #ifdef __cplusplus
183 }
184 #endif
185
186 #endif /* P_INLINES_H */