Merge branch 'nouveau-gallium-0.1' into darktama-gallium-0.1
[mesa.git] / src / mesa / pipe / xlib / xm_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 * Authors:
31 * Keith Whitwell
32 * Brian Paul
33 */
34
35
36 #include "glxheader.h"
37 #include "xmesaP.h"
38 #include "main/macros.h"
39
40 #include "pipe/p_winsys.h"
41 #include "pipe/p_format.h"
42 #include "pipe/p_context.h"
43 #include "pipe/softpipe/sp_winsys.h"
44
45
46 /**
47 * Low-level OS/window system memory buffer
48 */
49 struct xm_buffer
50 {
51 boolean userBuffer; /** Is this a user-space buffer? */
52 int refcount;
53 unsigned size;
54 void *data;
55 void *mapped;
56 };
57
58
59 struct xmesa_surface
60 {
61 struct pipe_surface surface;
62 /* no extra fields for now */
63 };
64
65
66 /**
67 * Derived from softpipe_winsys.
68 * We just need one extra field which indicates the pixel format to use for
69 * drawing surfaces so that we're compatible with the XVisual/window format.
70 */
71 struct xmesa_softpipe_winsys
72 {
73 struct softpipe_winsys spws;
74 uint pixelformat;
75 };
76
77
78
79 /** Cast wrapper */
80 static INLINE struct xmesa_surface *
81 xmesa_surface(struct pipe_surface *ps)
82 {
83 assert(0);
84 return (struct xmesa_surface *) ps;
85 }
86
87 /** cast wrapper */
88 static INLINE struct xmesa_softpipe_winsys *
89 xmesa_softpipe_winsys(struct softpipe_winsys *spws)
90 {
91 return (struct xmesa_softpipe_winsys *) spws;
92 }
93
94 /**
95 * Turn the softpipe opaque buffer pointer into a dri_bufmgr opaque
96 * buffer pointer...
97 */
98 static INLINE struct xm_buffer *
99 xm_bo( struct pipe_buffer_handle *bo )
100 {
101 return (struct xm_buffer *) bo;
102 }
103
104 static INLINE struct pipe_buffer_handle *
105 pipe_bo( struct xm_buffer *bo )
106 {
107 return (struct pipe_buffer_handle *) bo;
108 }
109
110
111 /* Most callbacks map direcly onto dri_bufmgr operations:
112 */
113 static void *
114 xm_buffer_map(struct pipe_winsys *pws, struct pipe_buffer_handle *buf,
115 unsigned flags)
116 {
117 struct xm_buffer *xm_buf = xm_bo(buf);
118 xm_buf->mapped = xm_buf->data;
119 return xm_buf->mapped;
120 }
121
122 static void
123 xm_buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer_handle *buf)
124 {
125 struct xm_buffer *xm_buf = xm_bo(buf);
126 xm_buf->mapped = NULL;
127 }
128
129 static void
130 xm_buffer_reference(struct pipe_winsys *pws,
131 struct pipe_buffer_handle **ptr,
132 struct pipe_buffer_handle *buf)
133 {
134 if (*ptr) {
135 struct xm_buffer *oldBuf = xm_bo(*ptr);
136 oldBuf->refcount--;
137 assert(oldBuf->refcount >= 0);
138 if (oldBuf->refcount == 0) {
139 if (oldBuf->data) {
140 if (!oldBuf->userBuffer)
141 free(oldBuf->data);
142 oldBuf->data = NULL;
143 }
144 free(oldBuf);
145 }
146 *ptr = NULL;
147 }
148
149 assert(!(*ptr));
150
151 if (buf) {
152 struct xm_buffer *newBuf = xm_bo(buf);
153 newBuf->refcount++;
154 *ptr = buf;
155 }
156 }
157
158 static void
159 xm_buffer_data(struct pipe_winsys *pws, struct pipe_buffer_handle *buf,
160 unsigned size, const void *data, unsigned usage )
161 {
162 struct xm_buffer *xm_buf = xm_bo(buf);
163 assert(!xm_buf->userBuffer);
164 if (xm_buf->size != size) {
165 if (xm_buf->data)
166 free(xm_buf->data);
167 xm_buf->data = malloc(size);
168 xm_buf->size = size;
169 }
170 if (data)
171 memcpy(xm_buf->data, data, size);
172 }
173
174 static void
175 xm_buffer_subdata(struct pipe_winsys *pws, struct pipe_buffer_handle *buf,
176 unsigned long offset, unsigned long size, const void *data)
177 {
178 struct xm_buffer *xm_buf = xm_bo(buf);
179 GLubyte *b = (GLubyte *) xm_buf->data;
180 assert(!xm_buf->userBuffer);
181 assert(b);
182 memcpy(b + offset, data, size);
183 }
184
185 static void
186 xm_buffer_get_subdata(struct pipe_winsys *pws, struct pipe_buffer_handle *buf,
187 unsigned long offset, unsigned long size, void *data)
188 {
189 const struct xm_buffer *xm_buf = xm_bo(buf);
190 const GLubyte *b = (GLubyte *) xm_buf->data;
191 assert(!xm_buf->userBuffer);
192 assert(b);
193 memcpy(data, b + offset, size);
194 }
195
196
197 /**
198 * Display/copy the image in the surface into the X window specified
199 * by the XMesaBuffer.
200 */
201 void
202 xmesa_display_surface(XMesaBuffer b, const struct pipe_surface *surf)
203 {
204 XImage *ximage = b->tempImage;
205 struct xm_buffer *xm_buf = xm_bo(surf->buffer);
206
207 /* check that the XImage has been previously initialized */
208 assert(ximage->format);
209 assert(ximage->bitmap_unit);
210
211 /* update XImage's fields */
212 ximage->width = surf->width;
213 ximage->height = surf->height;
214 ximage->bytes_per_line = surf->pitch * (ximage->bits_per_pixel / 8);
215 ximage->data = xm_buf->data;
216
217 /* display image in Window */
218 XPutImage(b->xm_visual->display, b->drawable, b->gc,
219 ximage, 0, 0, 0, 0, surf->width, surf->height);
220 }
221
222
223 static void
224 xm_flush_frontbuffer(struct pipe_winsys *pws,
225 struct pipe_surface *surf,
226 void *context_private)
227 {
228 /* The Xlib driver's front color surfaces are actually X Windows so
229 * this flush is a no-op.
230 * If we instead did front buffer rendering to a temporary XImage,
231 * this would be the place to copy the Ximage to the on-screen Window.
232 */
233 XMesaContext xmctx = (XMesaContext) context_private;
234
235 xmesa_display_surface(xmctx->xm_buffer, surf);
236 }
237
238
239
240 static void
241 xm_printf(struct pipe_winsys *pws, const char *fmtString, ...)
242 {
243 va_list args;
244 va_start( args, fmtString );
245 vfprintf(stderr, fmtString, args);
246 va_end( args );
247 }
248
249
250 static const char *
251 xm_get_name(struct pipe_winsys *pws)
252 {
253 return "Xlib";
254 }
255
256
257 static struct pipe_buffer_handle *
258 xm_buffer_create(struct pipe_winsys *pws, unsigned flags)
259 {
260 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
261 buffer->refcount = 1;
262 return pipe_bo(buffer);
263 }
264
265
266 /**
267 * Create buffer which wraps user-space data.
268 */
269 static struct pipe_buffer_handle *
270 xm_user_buffer_create(struct pipe_winsys *pws, void *ptr, unsigned bytes)
271 {
272 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
273 buffer->userBuffer = TRUE;
274 buffer->refcount = 1;
275 buffer->data = ptr;
276 buffer->size = bytes;
277 return pipe_bo(buffer);
278 }
279
280
281
282 /**
283 * Round n up to next multiple.
284 */
285 static INLINE unsigned
286 round_up(unsigned n, unsigned multiple)
287 {
288 return (n + multiple - 1) & ~(multiple - 1);
289 }
290
291 static unsigned
292 xm_surface_pitch(struct pipe_winsys *winsys, unsigned cpp, unsigned width,
293 unsigned flags)
294 {
295 return round_up(width, 64 / cpp);
296 }
297
298
299 /**
300 * Called via pipe->surface_alloc() to create new surfaces (textures,
301 * renderbuffers, etc.
302 */
303 static struct pipe_surface *
304 xm_surface_alloc(struct pipe_winsys *ws, enum pipe_format pipeFormat)
305 {
306 struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
307
308 assert(ws);
309 assert(pipeFormat);
310
311 xms->surface.format = pipeFormat;
312 xms->surface.refcount = 1;
313 xms->surface.winsys = ws;
314
315 return &xms->surface;
316 }
317
318
319
320 static void
321 xm_surface_release(struct pipe_winsys *winsys, struct pipe_surface **s)
322 {
323 struct pipe_surface *surf = *s;
324 surf->refcount--;
325 if (surf->refcount == 0) {
326 if (surf->buffer)
327 winsys->buffer_reference(winsys, &surf->buffer, NULL);
328 free(surf);
329 }
330 *s = NULL;
331 }
332
333
334
335 /**
336 * Return pointer to a pipe_winsys object.
337 * For Xlib, this is a singleton object.
338 * Nothing special for the Xlib driver so no subclassing or anything.
339 */
340 static struct pipe_winsys *
341 xmesa_get_pipe_winsys(void)
342 {
343 static struct pipe_winsys *ws = NULL;
344
345 if (!ws) {
346 ws = CALLOC_STRUCT(pipe_winsys);
347
348 /* Fill in this struct with callbacks that pipe will need to
349 * communicate with the window system, buffer manager, etc.
350 */
351 ws->buffer_create = xm_buffer_create;
352 ws->user_buffer_create = xm_user_buffer_create;
353 ws->buffer_map = xm_buffer_map;
354 ws->buffer_unmap = xm_buffer_unmap;
355 ws->buffer_reference = xm_buffer_reference;
356 ws->buffer_data = xm_buffer_data;
357 ws->buffer_subdata = xm_buffer_subdata;
358 ws->buffer_get_subdata = xm_buffer_get_subdata;
359
360 ws->surface_pitch = xm_surface_pitch;
361 ws->surface_alloc = xm_surface_alloc;
362 ws->surface_release = xm_surface_release;
363
364 ws->flush_frontbuffer = xm_flush_frontbuffer;
365 ws->printf = xm_printf;
366 ws->get_name = xm_get_name;
367 }
368
369 return ws;
370 }
371
372
373 /**
374 * The winsys being queried will have been created at glXCreateContext
375 * time, with a pixel format corresponding to the context's visual.
376 *
377 * XXX we should pass a flag indicating if the format is going to be
378 * use for a drawing surface vs. a texture. In the later case, we
379 * can support any format.
380 */
381 static boolean
382 xmesa_is_format_supported(struct softpipe_winsys *sws,
383 enum pipe_format format)
384 {
385 struct xmesa_softpipe_winsys *xmws = xmesa_softpipe_winsys(sws);
386
387 if (format == xmws->pixelformat) {
388 return TRUE;
389 }
390 else {
391 /* non-color / window surface format */
392 switch (format) {
393 case PIPE_FORMAT_R16G16B16A16_SNORM:
394 case PIPE_FORMAT_S8Z24_UNORM:
395 case PIPE_FORMAT_U_S8:
396 case PIPE_FORMAT_Z16_UNORM:
397 case PIPE_FORMAT_Z32_UNORM:
398 return TRUE;
399 default:
400 return FALSE;
401 }
402 }
403 }
404
405
406 /**
407 * Return pointer to a softpipe_winsys object.
408 */
409 static struct softpipe_winsys *
410 xmesa_get_softpipe_winsys(uint pixelformat)
411 {
412 struct xmesa_softpipe_winsys *xmws
413 = CALLOC_STRUCT(xmesa_softpipe_winsys);
414 if (!xmws)
415 return NULL;
416
417 xmws->spws.is_format_supported = xmesa_is_format_supported;
418 xmws->pixelformat = pixelformat;
419
420 return &xmws->spws;
421 }
422
423
424 struct pipe_context *
425 xmesa_create_pipe_context(XMesaContext xmesa, uint pixelformat)
426 {
427 struct pipe_winsys *pws = xmesa_get_pipe_winsys();
428 struct softpipe_winsys *spws = xmesa_get_softpipe_winsys(pixelformat);
429 struct pipe_context *pipe;
430
431 pipe = softpipe_create( pws, spws );
432 if (pipe)
433 pipe->priv = xmesa;
434
435 return pipe;
436 }