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