Merge branch 'gallium-strict-aliasing'
[mesa.git] / src / gallium / winsys / g3dvl / xlib / xsp_winsys.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
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 #include <vl_winsys.h>
29 #include <X11/Xutil.h>
30 #include <pipe/internal/p_winsys_screen.h>
31 #include <pipe/p_state.h>
32 #include <pipe/p_inlines.h>
33 #include <util/u_memory.h>
34 #include <util/u_math.h>
35 #include <softpipe/sp_winsys.h>
36 #include <softpipe/sp_video_context.h>
37 #include <softpipe/sp_texture.h>
38
39 /* pipe_winsys implementation */
40
41 struct xsp_pipe_winsys
42 {
43 struct pipe_winsys base;
44 Display *display;
45 int screen;
46 XImage *fbimage;
47 };
48
49 struct xsp_context
50 {
51 Drawable drawable;
52
53 void (*pipe_destroy)(struct pipe_video_context *vpipe);
54 };
55
56 struct xsp_buffer
57 {
58 struct pipe_buffer base;
59 boolean is_user_buffer;
60 void *data;
61 void *mapped_data;
62 };
63
64 static struct pipe_buffer* xsp_buffer_create(struct pipe_winsys *pws, unsigned alignment, unsigned usage, unsigned size)
65 {
66 struct xsp_buffer *buffer;
67
68 assert(pws);
69
70 buffer = calloc(1, sizeof(struct xsp_buffer));
71 pipe_reference_init(&buffer->base.reference, 1);
72 buffer->base.alignment = alignment;
73 buffer->base.usage = usage;
74 buffer->base.size = size;
75 buffer->data = align_malloc(size, alignment);
76
77 return (struct pipe_buffer*)buffer;
78 }
79
80 static struct pipe_buffer* xsp_user_buffer_create(struct pipe_winsys *pws, void *data, unsigned size)
81 {
82 struct xsp_buffer *buffer;
83
84 assert(pws);
85
86 buffer = calloc(1, sizeof(struct xsp_buffer));
87 pipe_reference_init(&buffer->base.reference, 1);
88 buffer->base.size = size;
89 buffer->is_user_buffer = TRUE;
90 buffer->data = data;
91
92 return (struct pipe_buffer*)buffer;
93 }
94
95 static void* xsp_buffer_map(struct pipe_winsys *pws, struct pipe_buffer *buffer, unsigned flags)
96 {
97 struct xsp_buffer *xsp_buf = (struct xsp_buffer*)buffer;
98
99 assert(pws);
100 assert(buffer);
101
102 xsp_buf->mapped_data = xsp_buf->data;
103
104 return xsp_buf->mapped_data;
105 }
106
107 static void xsp_buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer *buffer)
108 {
109 struct xsp_buffer *xsp_buf = (struct xsp_buffer*)buffer;
110
111 assert(pws);
112 assert(buffer);
113
114 xsp_buf->mapped_data = NULL;
115 }
116
117 static void xsp_buffer_destroy(struct pipe_buffer *buffer)
118 {
119 struct xsp_buffer *xsp_buf = (struct xsp_buffer*)buffer;
120
121 assert(buffer);
122
123 if (!xsp_buf->is_user_buffer)
124 align_free(xsp_buf->data);
125
126 free(xsp_buf);
127 }
128
129 static struct pipe_buffer* xsp_surface_buffer_create
130 (
131 struct pipe_winsys *pws,
132 unsigned width,
133 unsigned height,
134 enum pipe_format format,
135 unsigned usage,
136 unsigned tex_usage,
137 unsigned *stride
138 )
139 {
140 const unsigned int ALIGNMENT = 1;
141 unsigned nblocksy;
142
143 nblocksy = pf_get_nblocksy(format, height);
144 *stride = align(pf_get_stride(format, width), ALIGNMENT);
145
146 return pws->buffer_create(pws, ALIGNMENT, usage,
147 *stride * nblocksy);
148 }
149
150 static void xsp_fence_reference(struct pipe_winsys *pws, struct pipe_fence_handle **ptr, struct pipe_fence_handle *fence)
151 {
152 assert(pws);
153 assert(ptr);
154 assert(fence);
155 }
156
157 static int xsp_fence_signalled(struct pipe_winsys *pws, struct pipe_fence_handle *fence, unsigned flag)
158 {
159 assert(pws);
160 assert(fence);
161
162 return 0;
163 }
164
165 static int xsp_fence_finish(struct pipe_winsys *pws, struct pipe_fence_handle *fence, unsigned flag)
166 {
167 assert(pws);
168 assert(fence);
169
170 return 0;
171 }
172
173 static void xsp_flush_frontbuffer(struct pipe_winsys *pws, struct pipe_surface *surface, void *context_private)
174 {
175 struct xsp_pipe_winsys *xsp_winsys;
176 struct xsp_context *xsp_context;
177
178 assert(pws);
179 assert(surface);
180 assert(context_private);
181
182 xsp_winsys = (struct xsp_pipe_winsys*)pws;
183 xsp_context = (struct xsp_context*)context_private;
184 xsp_winsys->fbimage->width = surface->width;
185 xsp_winsys->fbimage->height = surface->height;
186 xsp_winsys->fbimage->bytes_per_line = surface->width * (xsp_winsys->fbimage->bits_per_pixel >> 3);
187 xsp_winsys->fbimage->data = (char*)((struct xsp_buffer *)softpipe_texture(surface->texture)->buffer)->data + surface->offset;
188
189 XPutImage
190 (
191 xsp_winsys->display, xsp_context->drawable,
192 XDefaultGC(xsp_winsys->display, xsp_winsys->screen),
193 xsp_winsys->fbimage, 0, 0, 0, 0,
194 surface->width, surface->height
195 );
196 XFlush(xsp_winsys->display);
197 }
198
199 static const char* xsp_get_name(struct pipe_winsys *pws)
200 {
201 assert(pws);
202 return "X11 SoftPipe";
203 }
204
205 static void xsp_destroy(struct pipe_winsys *pws)
206 {
207 struct xsp_pipe_winsys *xsp_winsys = (struct xsp_pipe_winsys*)pws;
208
209 assert(pws);
210
211 /* XDestroyImage() wants to free the data as well */
212 xsp_winsys->fbimage->data = NULL;
213
214 XDestroyImage(xsp_winsys->fbimage);
215 FREE(xsp_winsys);
216 }
217
218 /* Called through pipe_video_context::destroy() */
219 static void xsp_pipe_destroy(struct pipe_video_context *vpipe)
220 {
221 struct xsp_context *xsp_context;
222
223 assert(vpipe);
224
225 xsp_context = vpipe->priv;
226
227 /* Call the original destroy */
228 xsp_context->pipe_destroy(vpipe);
229
230 FREE(xsp_context);
231 }
232
233 /* Show starts here */
234
235 Drawable
236 vl_video_bind_drawable(struct pipe_video_context *vpipe, Drawable drawable)
237 {
238 struct xsp_context *xsp_context;
239 Drawable old_drawable;
240
241 assert(vpipe);
242
243 xsp_context = vpipe->priv;
244 old_drawable = xsp_context->drawable;
245 xsp_context->drawable = drawable;
246
247 return old_drawable;
248 }
249
250 struct pipe_screen*
251 vl_screen_create(Display *display, int screen)
252 {
253 struct xsp_pipe_winsys *xsp_winsys;
254
255 assert(display);
256
257 xsp_winsys = CALLOC_STRUCT(xsp_pipe_winsys);
258 if (!xsp_winsys)
259 return NULL;
260
261 xsp_winsys->base.buffer_create = xsp_buffer_create;
262 xsp_winsys->base.user_buffer_create = xsp_user_buffer_create;
263 xsp_winsys->base.buffer_map = xsp_buffer_map;
264 xsp_winsys->base.buffer_unmap = xsp_buffer_unmap;
265 xsp_winsys->base.buffer_destroy = xsp_buffer_destroy;
266 xsp_winsys->base.surface_buffer_create = xsp_surface_buffer_create;
267 xsp_winsys->base.fence_reference = xsp_fence_reference;
268 xsp_winsys->base.fence_signalled = xsp_fence_signalled;
269 xsp_winsys->base.fence_finish = xsp_fence_finish;
270 xsp_winsys->base.flush_frontbuffer = xsp_flush_frontbuffer;
271 xsp_winsys->base.get_name = xsp_get_name;
272 xsp_winsys->base.destroy = xsp_destroy;
273 xsp_winsys->display = display;
274 xsp_winsys->screen = screen;
275 xsp_winsys->fbimage = XCreateImage
276 (
277 display,
278 XDefaultVisual(display, screen),
279 XDefaultDepth(display, screen),
280 ZPixmap,
281 0,
282 NULL,
283 0, /* Don't know the width and height until flush_frontbuffer */
284 0,
285 32,
286 0
287 );
288
289 if (!xsp_winsys->fbimage) {
290 FREE(xsp_winsys);
291 return NULL;
292 }
293
294 XInitImage(xsp_winsys->fbimage);
295
296 return softpipe_create_screen(&xsp_winsys->base);
297 }
298
299 struct pipe_video_context*
300 vl_video_create(Display *display, int screen,
301 struct pipe_screen *p_screen,
302 enum pipe_video_profile profile,
303 enum pipe_video_chroma_format chroma_format,
304 unsigned width, unsigned height)
305 {
306 struct pipe_video_context *vpipe;
307 struct xsp_context *xsp_context;
308
309 assert(p_screen);
310 assert(width && height);
311
312 vpipe = sp_video_create(p_screen, profile, chroma_format, width, height);
313 if (!vpipe)
314 return NULL;
315
316 xsp_context = CALLOC_STRUCT(xsp_context);
317 if (!xsp_context) {
318 vpipe->destroy(vpipe);
319 return NULL;
320 }
321
322 /* Override this so we can free our xsp_context when the pipe is freed */
323 xsp_context->pipe_destroy = vpipe->destroy;
324 vpipe->destroy = xsp_pipe_destroy;
325
326 vpipe->priv = xsp_context;
327
328 return vpipe;
329 }