python: Simplify st_winsys.
[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_util.h"
43 #include "pipe/p_inlines.h"
44 #include "softpipe/sp_winsys.h"
45 #include "st_winsys.h"
46
47
48 struct st_softpipe_buffer
49 {
50 struct pipe_buffer base;
51 boolean userBuffer; /** Is this a user-space buffer? */
52 void *data;
53 void *mapped;
54 };
55
56
57 /** Cast wrapper */
58 static INLINE struct st_softpipe_buffer *
59 st_softpipe_buffer( struct pipe_buffer *buf )
60 {
61 return (struct st_softpipe_buffer *)buf;
62 }
63
64
65 static void *
66 st_softpipe_buffer_map(struct pipe_winsys *winsys,
67 struct pipe_buffer *buf,
68 unsigned flags)
69 {
70 struct st_softpipe_buffer *st_softpipe_buf = st_softpipe_buffer(buf);
71 st_softpipe_buf->mapped = st_softpipe_buf->data;
72 return st_softpipe_buf->mapped;
73 }
74
75
76 static void
77 st_softpipe_buffer_unmap(struct pipe_winsys *winsys,
78 struct pipe_buffer *buf)
79 {
80 struct st_softpipe_buffer *st_softpipe_buf = st_softpipe_buffer(buf);
81 st_softpipe_buf->mapped = NULL;
82 }
83
84
85 static void
86 st_softpipe_buffer_destroy(struct pipe_winsys *winsys,
87 struct pipe_buffer *buf)
88 {
89 struct st_softpipe_buffer *oldBuf = st_softpipe_buffer(buf);
90
91 if (oldBuf->data) {
92 if (!oldBuf->userBuffer)
93 align_free(oldBuf->data);
94
95 oldBuf->data = NULL;
96 }
97
98 FREE(oldBuf);
99 }
100
101
102 static void
103 st_softpipe_flush_frontbuffer(struct pipe_winsys *winsys,
104 struct pipe_surface *surf,
105 void *context_private)
106 {
107 }
108
109
110
111 static const char *
112 st_softpipe_get_name(struct pipe_winsys *winsys)
113 {
114 return "softpipe";
115 }
116
117
118 static struct pipe_buffer *
119 st_softpipe_buffer_create(struct pipe_winsys *winsys,
120 unsigned alignment,
121 unsigned usage,
122 unsigned size)
123 {
124 struct st_softpipe_buffer *buffer = CALLOC_STRUCT(st_softpipe_buffer);
125
126 buffer->base.refcount = 1;
127 buffer->base.alignment = alignment;
128 buffer->base.usage = usage;
129 buffer->base.size = size;
130
131 buffer->data = align_malloc(size, alignment);
132
133 return &buffer->base;
134 }
135
136
137 /**
138 * Create buffer which wraps user-space data.
139 */
140 static struct pipe_buffer *
141 st_softpipe_user_buffer_create(struct pipe_winsys *winsys,
142 void *ptr,
143 unsigned bytes)
144 {
145 struct st_softpipe_buffer *buffer;
146
147 buffer = CALLOC_STRUCT(st_softpipe_buffer);
148 if(!buffer)
149 return NULL;
150
151 buffer->base.refcount = 1;
152 buffer->base.size = bytes;
153 buffer->userBuffer = TRUE;
154 buffer->data = ptr;
155
156 return &buffer->base;
157 }
158
159
160 /**
161 * Round n up to next multiple.
162 */
163 static INLINE unsigned
164 round_up(unsigned n, unsigned multiple)
165 {
166 return (n + multiple - 1) & ~(multiple - 1);
167 }
168
169
170 static int
171 st_softpipe_surface_alloc_storage(struct pipe_winsys *winsys,
172 struct pipe_surface *surf,
173 unsigned width, unsigned height,
174 enum pipe_format format,
175 unsigned flags,
176 unsigned tex_usage)
177 {
178 const unsigned alignment = 64;
179
180 surf->width = width;
181 surf->height = height;
182 surf->format = format;
183 pf_get_block(format, &surf->block);
184 surf->nblocksx = pf_get_nblocksx(&surf->block, width);
185 surf->nblocksy = pf_get_nblocksy(&surf->block, height);
186 surf->stride = round_up(surf->nblocksx * surf->block.size, alignment);
187 surf->usage = flags;
188
189 assert(!surf->buffer);
190 surf->buffer = winsys->buffer_create(winsys, alignment,
191 PIPE_BUFFER_USAGE_PIXEL,
192 surf->stride * surf->nblocksy);
193 if(!surf->buffer)
194 return -1;
195
196 return 0;
197 }
198
199
200 static struct pipe_surface *
201 st_softpipe_surface_alloc(struct pipe_winsys *winsys)
202 {
203 struct pipe_surface *surface = CALLOC_STRUCT(pipe_surface);
204
205 assert(winsys);
206
207 surface->refcount = 1;
208 surface->winsys = winsys;
209
210 return surface;
211 }
212
213
214 static void
215 st_softpipe_surface_release(struct pipe_winsys *winsys,
216 struct pipe_surface **s)
217 {
218 struct pipe_surface *surf = *s;
219 assert(!surf->texture);
220 surf->refcount--;
221 if (surf->refcount == 0) {
222 if (surf->buffer)
223 pipe_buffer_reference(winsys, &surf->buffer, NULL);
224 free(surf);
225 }
226 *s = NULL;
227 }
228
229
230 static void
231 st_softpipe_fence_reference(struct pipe_winsys *winsys,
232 struct pipe_fence_handle **ptr,
233 struct pipe_fence_handle *fence)
234 {
235 }
236
237
238 static int
239 st_softpipe_fence_signalled(struct pipe_winsys *winsys,
240 struct pipe_fence_handle *fence,
241 unsigned flag)
242 {
243 return 0;
244 }
245
246
247 static int
248 st_softpipe_fence_finish(struct pipe_winsys *winsys,
249 struct pipe_fence_handle *fence,
250 unsigned flag)
251 {
252 return 0;
253 }
254
255
256 static void
257 st_softpipe_destroy(struct pipe_winsys *winsys)
258 {
259 FREE(winsys);
260 }
261
262
263 static struct pipe_screen *
264 st_softpipe_screen_create(void)
265 {
266 static struct pipe_winsys *winsys;
267 struct pipe_screen *screen;
268
269 winsys = CALLOC_STRUCT(pipe_winsys);
270 if(!winsys)
271 return NULL;
272
273 winsys->destroy = st_softpipe_destroy;
274
275 winsys->buffer_create = st_softpipe_buffer_create;
276 winsys->user_buffer_create = st_softpipe_user_buffer_create;
277 winsys->buffer_map = st_softpipe_buffer_map;
278 winsys->buffer_unmap = st_softpipe_buffer_unmap;
279 winsys->buffer_destroy = st_softpipe_buffer_destroy;
280
281 winsys->surface_alloc = st_softpipe_surface_alloc;
282 winsys->surface_alloc_storage = st_softpipe_surface_alloc_storage;
283 winsys->surface_release = st_softpipe_surface_release;
284
285 winsys->fence_reference = st_softpipe_fence_reference;
286 winsys->fence_signalled = st_softpipe_fence_signalled;
287 winsys->fence_finish = st_softpipe_fence_finish;
288
289 winsys->flush_frontbuffer = st_softpipe_flush_frontbuffer;
290 winsys->get_name = st_softpipe_get_name;
291
292 screen = softpipe_create_screen(winsys);
293 if(!screen)
294 st_softpipe_destroy(winsys);
295
296 return screen;
297 }
298
299
300 static struct pipe_context *
301 st_softpipe_context_create(struct pipe_screen *screen)
302 {
303 return softpipe_create(screen, screen->winsys, NULL);
304 }
305
306
307 const struct st_winsys st_softpipe_winsys = {
308 &st_softpipe_screen_create,
309 &st_softpipe_context_create,
310 };