Merge branch 'mesa_7_6_branch'
[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 struct pipe_format_block block;
142 unsigned nblocksx, nblocksy;
143
144 pf_get_block(format, &block);
145 nblocksx = pf_get_nblocksx(&block, width);
146 nblocksy = pf_get_nblocksy(&block, height);
147 *stride = align(nblocksx * block.size, ALIGNMENT);
148
149 return pws->buffer_create(pws, ALIGNMENT, usage,
150 *stride * nblocksy);
151 }
152
153 static void xsp_fence_reference(struct pipe_winsys *pws, struct pipe_fence_handle **ptr, struct pipe_fence_handle *fence)
154 {
155 assert(pws);
156 assert(ptr);
157 assert(fence);
158 }
159
160 static int xsp_fence_signalled(struct pipe_winsys *pws, struct pipe_fence_handle *fence, unsigned flag)
161 {
162 assert(pws);
163 assert(fence);
164
165 return 0;
166 }
167
168 static int xsp_fence_finish(struct pipe_winsys *pws, struct pipe_fence_handle *fence, unsigned flag)
169 {
170 assert(pws);
171 assert(fence);
172
173 return 0;
174 }
175
176 static void xsp_flush_frontbuffer(struct pipe_winsys *pws, struct pipe_surface *surface, void *context_private)
177 {
178 struct xsp_pipe_winsys *xsp_winsys;
179 struct xsp_context *xsp_context;
180
181 assert(pws);
182 assert(surface);
183 assert(context_private);
184
185 xsp_winsys = (struct xsp_pipe_winsys*)pws;
186 xsp_context = (struct xsp_context*)context_private;
187 xsp_winsys->fbimage->width = surface->width;
188 xsp_winsys->fbimage->height = surface->height;
189 xsp_winsys->fbimage->bytes_per_line = surface->width * (xsp_winsys->fbimage->bits_per_pixel >> 3);
190 xsp_winsys->fbimage->data = (char*)((struct xsp_buffer *)softpipe_texture(surface->texture)->buffer)->data + surface->offset;
191
192 XPutImage
193 (
194 xsp_winsys->display, xsp_context->drawable,
195 XDefaultGC(xsp_winsys->display, xsp_winsys->screen),
196 xsp_winsys->fbimage, 0, 0, 0, 0,
197 surface->width, surface->height
198 );
199 XFlush(xsp_winsys->display);
200 }
201
202 static const char* xsp_get_name(struct pipe_winsys *pws)
203 {
204 assert(pws);
205 return "X11 SoftPipe";
206 }
207
208 static void xsp_destroy(struct pipe_winsys *pws)
209 {
210 struct xsp_pipe_winsys *xsp_winsys = (struct xsp_pipe_winsys*)pws;
211
212 assert(pws);
213
214 /* XDestroyImage() wants to free the data as well */
215 xsp_winsys->fbimage->data = NULL;
216
217 XDestroyImage(xsp_winsys->fbimage);
218 FREE(xsp_winsys);
219 }
220
221 /* Called through pipe_video_context::destroy() */
222 static void xsp_pipe_destroy(struct pipe_video_context *vpipe)
223 {
224 struct xsp_context *xsp_context;
225
226 assert(vpipe);
227
228 xsp_context = vpipe->priv;
229
230 /* Call the original destroy */
231 xsp_context->pipe_destroy(vpipe);
232
233 FREE(xsp_context);
234 }
235
236 /* Show starts here */
237
238 Drawable
239 vl_video_bind_drawable(struct pipe_video_context *vpipe, Drawable drawable)
240 {
241 struct xsp_context *xsp_context;
242 Drawable old_drawable;
243
244 assert(vpipe);
245
246 xsp_context = vpipe->priv;
247 old_drawable = xsp_context->drawable;
248 xsp_context->drawable = drawable;
249
250 return old_drawable;
251 }
252
253 struct pipe_screen*
254 vl_screen_create(Display *display, int screen)
255 {
256 struct xsp_pipe_winsys *xsp_winsys;
257
258 assert(display);
259
260 xsp_winsys = CALLOC_STRUCT(xsp_pipe_winsys);
261 if (!xsp_winsys)
262 return NULL;
263
264 xsp_winsys->base.buffer_create = xsp_buffer_create;
265 xsp_winsys->base.user_buffer_create = xsp_user_buffer_create;
266 xsp_winsys->base.buffer_map = xsp_buffer_map;
267 xsp_winsys->base.buffer_unmap = xsp_buffer_unmap;
268 xsp_winsys->base.buffer_destroy = xsp_buffer_destroy;
269 xsp_winsys->base.surface_buffer_create = xsp_surface_buffer_create;
270 xsp_winsys->base.fence_reference = xsp_fence_reference;
271 xsp_winsys->base.fence_signalled = xsp_fence_signalled;
272 xsp_winsys->base.fence_finish = xsp_fence_finish;
273 xsp_winsys->base.flush_frontbuffer = xsp_flush_frontbuffer;
274 xsp_winsys->base.get_name = xsp_get_name;
275 xsp_winsys->base.destroy = xsp_destroy;
276 xsp_winsys->display = display;
277 xsp_winsys->screen = screen;
278 xsp_winsys->fbimage = XCreateImage
279 (
280 display,
281 XDefaultVisual(display, screen),
282 XDefaultDepth(display, screen),
283 ZPixmap,
284 0,
285 NULL,
286 0, /* Don't know the width and height until flush_frontbuffer */
287 0,
288 32,
289 0
290 );
291
292 if (!xsp_winsys->fbimage) {
293 FREE(xsp_winsys);
294 return NULL;
295 }
296
297 XInitImage(xsp_winsys->fbimage);
298
299 return softpipe_create_screen(&xsp_winsys->base);
300 }
301
302 struct pipe_video_context*
303 vl_video_create(struct pipe_screen *screen,
304 enum pipe_video_profile profile,
305 enum pipe_video_chroma_format chroma_format,
306 unsigned width, unsigned height)
307 {
308 struct pipe_video_context *vpipe;
309 struct xsp_context *xsp_context;
310
311 assert(screen);
312 assert(width && height);
313
314 vpipe = sp_video_create(screen, profile, chroma_format, width, height);
315 if (!vpipe)
316 return NULL;
317
318 xsp_context = CALLOC_STRUCT(xsp_context);
319 if (!xsp_context) {
320 vpipe->destroy(vpipe);
321 return NULL;
322 }
323
324 /* Override this so we can free our xsp_context when the pipe is freed */
325 xsp_context->pipe_destroy = vpipe->destroy;
326 vpipe->destroy = xsp_pipe_destroy;
327
328 vpipe->priv = xsp_context;
329
330 return vpipe;
331 }