Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / state_trackers / python / st_softpipe_winsys.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Bismarck, ND., USA
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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 *
27 **************************************************************************/
28
29 /**
30 * @file
31 * Softpipe support.
32 *
33 * @author Keith Whitwell
34 * @author Brian Paul
35 * @author Jose Fonseca
36 */
37
38
39 #include "pipe/p_winsys.h"
40 #include "pipe/p_format.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_inlines.h"
43 #include "util/u_math.h"
44 #include "util/u_memory.h"
45 #include "softpipe/sp_winsys.h"
46 #include "st_winsys.h"
47
48
49 struct st_softpipe_buffer
50 {
51 struct pipe_buffer base;
52 boolean userBuffer; /** Is this a user-space buffer? */
53 void *data;
54 void *mapped;
55 };
56
57
58 /** Cast wrapper */
59 static INLINE struct st_softpipe_buffer *
60 st_softpipe_buffer( struct pipe_buffer *buf )
61 {
62 return (struct st_softpipe_buffer *)buf;
63 }
64
65
66 static void *
67 st_softpipe_buffer_map(struct pipe_winsys *winsys,
68 struct pipe_buffer *buf,
69 unsigned flags)
70 {
71 struct st_softpipe_buffer *st_softpipe_buf = st_softpipe_buffer(buf);
72 st_softpipe_buf->mapped = st_softpipe_buf->data;
73 return st_softpipe_buf->mapped;
74 }
75
76
77 static void
78 st_softpipe_buffer_unmap(struct pipe_winsys *winsys,
79 struct pipe_buffer *buf)
80 {
81 struct st_softpipe_buffer *st_softpipe_buf = st_softpipe_buffer(buf);
82 st_softpipe_buf->mapped = NULL;
83 }
84
85
86 static void
87 st_softpipe_buffer_destroy(struct pipe_winsys *winsys,
88 struct pipe_buffer *buf)
89 {
90 struct st_softpipe_buffer *oldBuf = st_softpipe_buffer(buf);
91
92 if (oldBuf->data) {
93 if (!oldBuf->userBuffer)
94 align_free(oldBuf->data);
95
96 oldBuf->data = NULL;
97 }
98
99 FREE(oldBuf);
100 }
101
102
103 static void
104 st_softpipe_flush_frontbuffer(struct pipe_winsys *winsys,
105 struct pipe_surface *surf,
106 void *context_private)
107 {
108 }
109
110
111
112 static const char *
113 st_softpipe_get_name(struct pipe_winsys *winsys)
114 {
115 return "softpipe";
116 }
117
118
119 static struct pipe_buffer *
120 st_softpipe_buffer_create(struct pipe_winsys *winsys,
121 unsigned alignment,
122 unsigned usage,
123 unsigned size)
124 {
125 struct st_softpipe_buffer *buffer = CALLOC_STRUCT(st_softpipe_buffer);
126
127 buffer->base.refcount = 1;
128 buffer->base.alignment = alignment;
129 buffer->base.usage = usage;
130 buffer->base.size = size;
131
132 buffer->data = align_malloc(size, alignment);
133
134 return &buffer->base;
135 }
136
137
138 /**
139 * Create buffer which wraps user-space data.
140 */
141 static struct pipe_buffer *
142 st_softpipe_user_buffer_create(struct pipe_winsys *winsys,
143 void *ptr,
144 unsigned bytes)
145 {
146 struct st_softpipe_buffer *buffer;
147
148 buffer = CALLOC_STRUCT(st_softpipe_buffer);
149 if(!buffer)
150 return NULL;
151
152 buffer->base.refcount = 1;
153 buffer->base.size = bytes;
154 buffer->userBuffer = TRUE;
155 buffer->data = ptr;
156
157 return &buffer->base;
158 }
159
160
161 /**
162 * Round n up to next multiple.
163 */
164 static INLINE unsigned
165 round_up(unsigned n, unsigned multiple)
166 {
167 return (n + multiple - 1) & ~(multiple - 1);
168 }
169
170
171 static int
172 st_softpipe_surface_alloc_storage(struct pipe_winsys *winsys,
173 struct pipe_surface *surf,
174 unsigned width, unsigned height,
175 enum pipe_format format,
176 unsigned flags,
177 unsigned tex_usage)
178 {
179 const unsigned alignment = 64;
180
181 surf->width = width;
182 surf->height = height;
183 surf->format = format;
184 pf_get_block(format, &surf->block);
185 surf->nblocksx = pf_get_nblocksx(&surf->block, width);
186 surf->nblocksy = pf_get_nblocksy(&surf->block, height);
187 surf->stride = round_up(surf->nblocksx * surf->block.size, alignment);
188 surf->usage = flags;
189
190 assert(!surf->buffer);
191 surf->buffer = winsys->buffer_create(winsys, alignment,
192 PIPE_BUFFER_USAGE_PIXEL,
193 surf->stride * surf->nblocksy);
194 if(!surf->buffer)
195 return -1;
196
197 return 0;
198 }
199
200
201 static struct pipe_surface *
202 st_softpipe_surface_alloc(struct pipe_winsys *winsys)
203 {
204 struct pipe_surface *surface = CALLOC_STRUCT(pipe_surface);
205
206 assert(winsys);
207
208 surface->refcount = 1;
209 surface->winsys = winsys;
210
211 return surface;
212 }
213
214
215 static void
216 st_softpipe_surface_release(struct pipe_winsys *winsys,
217 struct pipe_surface **s)
218 {
219 struct pipe_surface *surf = *s;
220 assert(!surf->texture);
221 surf->refcount--;
222 if (surf->refcount == 0) {
223 if (surf->buffer)
224 winsys_buffer_reference(winsys, &surf->buffer, NULL);
225 free(surf);
226 }
227 *s = NULL;
228 }
229
230
231 static void
232 st_softpipe_fence_reference(struct pipe_winsys *winsys,
233 struct pipe_fence_handle **ptr,
234 struct pipe_fence_handle *fence)
235 {
236 }
237
238
239 static int
240 st_softpipe_fence_signalled(struct pipe_winsys *winsys,
241 struct pipe_fence_handle *fence,
242 unsigned flag)
243 {
244 return 0;
245 }
246
247
248 static int
249 st_softpipe_fence_finish(struct pipe_winsys *winsys,
250 struct pipe_fence_handle *fence,
251 unsigned flag)
252 {
253 return 0;
254 }
255
256
257 static void
258 st_softpipe_destroy(struct pipe_winsys *winsys)
259 {
260 FREE(winsys);
261 }
262
263
264 static struct pipe_screen *
265 st_softpipe_screen_create(void)
266 {
267 static struct pipe_winsys *winsys;
268 struct pipe_screen *screen;
269
270 winsys = CALLOC_STRUCT(pipe_winsys);
271 if(!winsys)
272 return NULL;
273
274 winsys->destroy = st_softpipe_destroy;
275
276 winsys->buffer_create = st_softpipe_buffer_create;
277 winsys->user_buffer_create = st_softpipe_user_buffer_create;
278 winsys->buffer_map = st_softpipe_buffer_map;
279 winsys->buffer_unmap = st_softpipe_buffer_unmap;
280 winsys->buffer_destroy = st_softpipe_buffer_destroy;
281
282 winsys->surface_alloc = st_softpipe_surface_alloc;
283 winsys->surface_alloc_storage = st_softpipe_surface_alloc_storage;
284 winsys->surface_release = st_softpipe_surface_release;
285
286 winsys->fence_reference = st_softpipe_fence_reference;
287 winsys->fence_signalled = st_softpipe_fence_signalled;
288 winsys->fence_finish = st_softpipe_fence_finish;
289
290 winsys->flush_frontbuffer = st_softpipe_flush_frontbuffer;
291 winsys->get_name = st_softpipe_get_name;
292
293 screen = softpipe_create_screen(winsys);
294 if(!screen)
295 st_softpipe_destroy(winsys);
296
297 return screen;
298 }
299
300
301 static struct pipe_context *
302 st_softpipe_context_create(struct pipe_screen *screen)
303 {
304 return softpipe_create(screen, screen->winsys, NULL);
305 }
306
307
308 const struct st_winsys st_softpipe_winsys = {
309 &st_softpipe_screen_create,
310 &st_softpipe_context_create,
311 };