Merge branch 'i965g-restart'
[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/internal/p_winsys_screen.h"/* port to just p_screen */
40 #include "pipe/p_format.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_inlines.h"
43 #include "util/u_format.h"
44 #include "util/u_math.h"
45 #include "util/u_memory.h"
46 #include "softpipe/sp_winsys.h"
47 #include "st_winsys.h"
48
49
50 struct st_softpipe_buffer
51 {
52 struct pipe_buffer base;
53 boolean userBuffer; /** Is this a user-space buffer? */
54 void *data;
55 void *mapped;
56 };
57
58
59 /** Cast wrapper */
60 static INLINE struct st_softpipe_buffer *
61 st_softpipe_buffer( struct pipe_buffer *buf )
62 {
63 return (struct st_softpipe_buffer *)buf;
64 }
65
66
67 static void *
68 st_softpipe_buffer_map(struct pipe_winsys *winsys,
69 struct pipe_buffer *buf,
70 unsigned flags)
71 {
72 struct st_softpipe_buffer *st_softpipe_buf = st_softpipe_buffer(buf);
73 st_softpipe_buf->mapped = st_softpipe_buf->data;
74 return st_softpipe_buf->mapped;
75 }
76
77
78 static void
79 st_softpipe_buffer_unmap(struct pipe_winsys *winsys,
80 struct pipe_buffer *buf)
81 {
82 struct st_softpipe_buffer *st_softpipe_buf = st_softpipe_buffer(buf);
83 st_softpipe_buf->mapped = NULL;
84 }
85
86
87 static void
88 st_softpipe_buffer_destroy(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 pipe_reference_init(&buffer->base.reference, 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 pipe_reference_init(&buffer->base.reference, 1);
153 buffer->base.size = bytes;
154 buffer->userBuffer = TRUE;
155 buffer->data = ptr;
156
157 return &buffer->base;
158 }
159
160
161 static struct pipe_buffer *
162 st_softpipe_surface_buffer_create(struct pipe_winsys *winsys,
163 unsigned width, unsigned height,
164 enum pipe_format format,
165 unsigned usage,
166 unsigned tex_usage,
167 unsigned *stride)
168 {
169 const unsigned alignment = 64;
170 unsigned nblocksy;
171
172 nblocksy = util_format_get_nblocksy(format, height);
173 *stride = align(util_format_get_stride(format, width), alignment);
174
175 return winsys->buffer_create(winsys, alignment,
176 usage,
177 *stride * nblocksy);
178 }
179
180
181 static void
182 st_softpipe_fence_reference(struct pipe_winsys *winsys,
183 struct pipe_fence_handle **ptr,
184 struct pipe_fence_handle *fence)
185 {
186 }
187
188
189 static int
190 st_softpipe_fence_signalled(struct pipe_winsys *winsys,
191 struct pipe_fence_handle *fence,
192 unsigned flag)
193 {
194 return 0;
195 }
196
197
198 static int
199 st_softpipe_fence_finish(struct pipe_winsys *winsys,
200 struct pipe_fence_handle *fence,
201 unsigned flag)
202 {
203 return 0;
204 }
205
206
207 static void
208 st_softpipe_destroy(struct pipe_winsys *winsys)
209 {
210 FREE(winsys);
211 }
212
213
214 static struct pipe_screen *
215 st_softpipe_screen_create(void)
216 {
217 static struct pipe_winsys *winsys;
218 struct pipe_screen *screen;
219
220 winsys = CALLOC_STRUCT(pipe_winsys);
221 if(!winsys)
222 return NULL;
223
224 winsys->destroy = st_softpipe_destroy;
225
226 winsys->buffer_create = st_softpipe_buffer_create;
227 winsys->user_buffer_create = st_softpipe_user_buffer_create;
228 winsys->buffer_map = st_softpipe_buffer_map;
229 winsys->buffer_unmap = st_softpipe_buffer_unmap;
230 winsys->buffer_destroy = st_softpipe_buffer_destroy;
231
232 winsys->surface_buffer_create = st_softpipe_surface_buffer_create;
233
234 winsys->fence_reference = st_softpipe_fence_reference;
235 winsys->fence_signalled = st_softpipe_fence_signalled;
236 winsys->fence_finish = st_softpipe_fence_finish;
237
238 winsys->flush_frontbuffer = st_softpipe_flush_frontbuffer;
239 winsys->get_name = st_softpipe_get_name;
240
241 screen = softpipe_create_screen(winsys);
242 if(!screen)
243 st_softpipe_destroy(winsys);
244
245 return screen;
246 }
247
248
249 static struct pipe_context *
250 st_softpipe_context_create(struct pipe_screen *screen)
251 {
252 return softpipe_create(screen);
253 }
254
255
256 const struct st_winsys st_softpipe_winsys = {
257 &st_softpipe_screen_create,
258 &st_softpipe_context_create,
259 };