DRI2: check for swapAvailable before using swap interval protocol
[mesa.git] / src / mesa / drivers / dri / swrast / swrast.c
1 /*
2 * Copyright (C) 2008 George Sapountzis <gsap7@yahoo.gr>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 /*
23 * DRI software rasterizer
24 *
25 * This is the mesa swrast module packaged into a DRI driver structure.
26 *
27 * The front-buffer is allocated by the loader. The loader provides read/write
28 * callbacks for access to the front-buffer. The driver uses a scratch row for
29 * front-buffer rendering to avoid repeated calls to the loader.
30 *
31 * The back-buffer is allocated by the driver and is private.
32 */
33
34 #include "main/context.h"
35 #include "main/extensions.h"
36 #include "main/formats.h"
37 #include "main/framebuffer.h"
38 #include "main/imports.h"
39 #include "main/renderbuffer.h"
40 #include "swrast/swrast.h"
41 #include "swrast_setup/swrast_setup.h"
42 #include "tnl/tnl.h"
43 #include "tnl/t_context.h"
44 #include "tnl/t_pipeline.h"
45 #include "vbo/vbo.h"
46 #include "drivers/common/driverfuncs.h"
47 #include "drivers/common/meta.h"
48 #include "utils.h"
49
50 #include "swrast_priv.h"
51
52
53 /**
54 * Screen and config-related functions
55 */
56
57 static void
58 setupLoaderExtensions(__DRIscreen *psp,
59 const __DRIextension **extensions)
60 {
61 int i;
62
63 for (i = 0; extensions[i]; i++) {
64 if (strcmp(extensions[i]->name, __DRI_SWRAST_LOADER) == 0)
65 psp->swrast_loader = (__DRIswrastLoaderExtension *) extensions[i];
66 }
67 }
68
69 static __DRIconfig **
70 swrastFillInModes(__DRIscreen *psp,
71 unsigned pixel_bits, unsigned depth_bits,
72 unsigned stencil_bits, GLboolean have_back_buffer)
73 {
74 __DRIconfig **configs;
75 unsigned depth_buffer_factor;
76 unsigned back_buffer_factor;
77 GLenum fb_format;
78 GLenum fb_type;
79
80 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
81 * support pageflipping at all.
82 */
83 static const GLenum back_buffer_modes[] = {
84 GLX_NONE, GLX_SWAP_UNDEFINED_OML
85 };
86
87 uint8_t depth_bits_array[4];
88 uint8_t stencil_bits_array[4];
89 uint8_t msaa_samples_array[1];
90
91 depth_bits_array[0] = 0;
92 depth_bits_array[1] = 0;
93 depth_bits_array[2] = depth_bits;
94 depth_bits_array[3] = depth_bits;
95
96 /* Just like with the accumulation buffer, always provide some modes
97 * with a stencil buffer.
98 */
99 stencil_bits_array[0] = 0;
100 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
101 stencil_bits_array[2] = 0;
102 stencil_bits_array[3] = (stencil_bits == 0) ? 8 : stencil_bits;
103
104 msaa_samples_array[0] = 0;
105
106 depth_buffer_factor = 4;
107 back_buffer_factor = 2;
108
109 switch (pixel_bits) {
110 case 8:
111 fb_format = GL_RGB;
112 fb_type = GL_UNSIGNED_BYTE_2_3_3_REV;
113 break;
114 case 16:
115 fb_format = GL_RGB;
116 fb_type = GL_UNSIGNED_SHORT_5_6_5;
117 break;
118 case 24:
119 fb_format = GL_BGR;
120 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
121 break;
122 case 32:
123 fb_format = GL_BGRA;
124 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
125 break;
126 default:
127 fprintf(stderr, "[%s:%u] bad depth %d\n", __func__, __LINE__,
128 pixel_bits);
129 return NULL;
130 }
131
132 configs = driCreateConfigs(fb_format, fb_type,
133 depth_bits_array, stencil_bits_array,
134 depth_buffer_factor, back_buffer_modes,
135 back_buffer_factor, msaa_samples_array, 1,
136 GL_TRUE);
137 if (configs == NULL) {
138 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
139 __LINE__);
140 return NULL;
141 }
142
143 return configs;
144 }
145
146 static __DRIscreen *
147 driCreateNewScreen(int scrn, const __DRIextension **extensions,
148 const __DRIconfig ***driver_configs, void *data)
149 {
150 static const __DRIextension *emptyExtensionList[] = { NULL };
151 __DRIscreen *psp;
152 __DRIconfig **configs8, **configs16, **configs24, **configs32;
153
154 (void) data;
155
156 TRACE;
157
158 psp = calloc(1, sizeof(*psp));
159 if (!psp)
160 return NULL;
161
162 setupLoaderExtensions(psp, extensions);
163
164 psp->num = scrn;
165 psp->extensions = emptyExtensionList;
166
167 configs8 = swrastFillInModes(psp, 8, 8, 0, 1);
168 configs16 = swrastFillInModes(psp, 16, 16, 0, 1);
169 configs24 = swrastFillInModes(psp, 24, 24, 8, 1);
170 configs32 = swrastFillInModes(psp, 32, 24, 8, 1);
171
172 configs16 = driConcatConfigs(configs8, configs16);
173 configs24 = driConcatConfigs(configs16, configs24);
174 *driver_configs = (const __DRIconfig **)
175 driConcatConfigs(configs24, configs32);
176
177 driInitExtensions( NULL, NULL, GL_FALSE );
178
179 return psp;
180 }
181
182 static void driDestroyScreen(__DRIscreen *psp)
183 {
184 TRACE;
185
186 if (psp) {
187 free(psp);
188 }
189 }
190
191 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
192 {
193 TRACE;
194
195 return psp->extensions;
196 }
197
198
199 /**
200 * Framebuffer and renderbuffer-related functions.
201 */
202
203 static GLuint
204 choose_pixel_format(const GLvisual *v)
205 {
206 int depth = v->rgbBits;
207
208 if (depth == 32
209 && v->redMask == 0xff0000
210 && v->greenMask == 0x00ff00
211 && v->blueMask == 0x0000ff)
212 return PF_A8R8G8B8;
213 else if (depth == 24
214 && v->redMask == 0xff0000
215 && v->greenMask == 0x00ff00
216 && v->blueMask == 0x0000ff)
217 return PF_X8R8G8B8;
218 else if (depth == 16
219 && v->redMask == 0xf800
220 && v->greenMask == 0x07e0
221 && v->blueMask == 0x001f)
222 return PF_R5G6B5;
223 else if (depth == 8
224 && v->redMask == 0x07
225 && v->greenMask == 0x38
226 && v->blueMask == 0xc0)
227 return PF_R3G3B2;
228
229 _mesa_problem( NULL, "unexpected format in %s", __FUNCTION__ );
230 return 0;
231 }
232
233 static void
234 swrast_delete_renderbuffer(struct gl_renderbuffer *rb)
235 {
236 TRACE;
237
238 free(rb->Data);
239 free(rb);
240 }
241
242 static GLboolean
243 swrast_alloc_front_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
244 GLenum internalFormat, GLuint width, GLuint height)
245 {
246 struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
247 unsigned mask = PITCH_ALIGN_BITS - 1;
248
249 TRACE;
250
251 rb->Data = NULL;
252 rb->Width = width;
253 rb->Height = height;
254
255 /* always pad to PITCH_ALIGN_BITS */
256 xrb->pitch = ((width * xrb->bpp + mask) & ~mask) / 8;
257
258 return GL_TRUE;
259 }
260
261 static GLboolean
262 swrast_alloc_back_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
263 GLenum internalFormat, GLuint width, GLuint height)
264 {
265 struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
266
267 TRACE;
268
269 free(rb->Data);
270
271 swrast_alloc_front_storage(ctx, rb, internalFormat, width, height);
272
273 rb->Data = malloc(height * xrb->pitch);
274
275 return GL_TRUE;
276 }
277
278 static struct swrast_renderbuffer *
279 swrast_new_renderbuffer(const GLvisual *visual, GLboolean front)
280 {
281 struct swrast_renderbuffer *xrb = calloc(1, sizeof *xrb);
282 GLuint pixel_format;
283
284 TRACE;
285
286 if (!xrb)
287 return NULL;
288
289 _mesa_init_renderbuffer(&xrb->Base, 0);
290
291 pixel_format = choose_pixel_format(visual);
292
293 xrb->Base.Delete = swrast_delete_renderbuffer;
294 if (front) {
295 xrb->Base.AllocStorage = swrast_alloc_front_storage;
296 swrast_set_span_funcs_front(xrb, pixel_format);
297 }
298 else {
299 xrb->Base.AllocStorage = swrast_alloc_back_storage;
300 swrast_set_span_funcs_back(xrb, pixel_format);
301 }
302
303 switch (pixel_format) {
304 case PF_A8R8G8B8:
305 xrb->Base.Format = MESA_FORMAT_ARGB8888;
306 xrb->Base.InternalFormat = GL_RGBA;
307 xrb->Base._BaseFormat = GL_RGBA;
308 xrb->Base.DataType = GL_UNSIGNED_BYTE;
309 xrb->bpp = 32;
310 break;
311 case PF_X8R8G8B8:
312 xrb->Base.Format = MESA_FORMAT_ARGB8888; /* XXX */
313 xrb->Base.InternalFormat = GL_RGB;
314 xrb->Base._BaseFormat = GL_RGB;
315 xrb->Base.DataType = GL_UNSIGNED_BYTE;
316 xrb->bpp = 32;
317 break;
318 case PF_R5G6B5:
319 xrb->Base.Format = MESA_FORMAT_RGB565;
320 xrb->Base.InternalFormat = GL_RGB;
321 xrb->Base._BaseFormat = GL_RGB;
322 xrb->Base.DataType = GL_UNSIGNED_BYTE;
323 xrb->bpp = 16;
324 break;
325 case PF_R3G3B2:
326 xrb->Base.Format = MESA_FORMAT_RGB332;
327 xrb->Base.InternalFormat = GL_RGB;
328 xrb->Base._BaseFormat = GL_RGB;
329 xrb->Base.DataType = GL_UNSIGNED_BYTE;
330 xrb->bpp = 8;
331 break;
332 default:
333 return NULL;
334 }
335
336 return xrb;
337 }
338
339 static __DRIdrawable *
340 driCreateNewDrawable(__DRIscreen *screen,
341 const __DRIconfig *config, void *data)
342 {
343 __DRIdrawable *buf;
344 struct swrast_renderbuffer *frontrb, *backrb;
345
346 TRACE;
347
348 buf = calloc(1, sizeof *buf);
349 if (!buf)
350 return NULL;
351
352 buf->loaderPrivate = data;
353
354 buf->driScreenPriv = screen;
355
356 buf->row = malloc(MAX_WIDTH * 4);
357
358 /* basic framebuffer setup */
359 _mesa_initialize_window_framebuffer(&buf->Base, &config->modes);
360
361 /* add front renderbuffer */
362 frontrb = swrast_new_renderbuffer(&config->modes, GL_TRUE);
363 _mesa_add_renderbuffer(&buf->Base, BUFFER_FRONT_LEFT, &frontrb->Base);
364
365 /* add back renderbuffer */
366 if (config->modes.doubleBufferMode) {
367 backrb = swrast_new_renderbuffer(&config->modes, GL_FALSE);
368 _mesa_add_renderbuffer(&buf->Base, BUFFER_BACK_LEFT, &backrb->Base);
369 }
370
371 /* add software renderbuffers */
372 _mesa_add_soft_renderbuffers(&buf->Base,
373 GL_FALSE, /* color */
374 config->modes.haveDepthBuffer,
375 config->modes.haveStencilBuffer,
376 config->modes.haveAccumBuffer,
377 GL_FALSE, /* alpha */
378 GL_FALSE /* aux bufs */);
379
380 return buf;
381 }
382
383 static void
384 driDestroyDrawable(__DRIdrawable *buf)
385 {
386 TRACE;
387
388 if (buf) {
389 struct gl_framebuffer *fb = &buf->Base;
390
391 free(buf->row);
392
393 fb->DeletePending = GL_TRUE;
394 _mesa_reference_framebuffer(&fb, NULL);
395 }
396 }
397
398 static void driSwapBuffers(__DRIdrawable *buf)
399 {
400 GET_CURRENT_CONTEXT(ctx);
401
402 struct swrast_renderbuffer *frontrb =
403 swrast_renderbuffer(buf->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
404 struct swrast_renderbuffer *backrb =
405 swrast_renderbuffer(buf->Base.Attachment[BUFFER_BACK_LEFT].Renderbuffer);
406
407 __DRIscreen *screen = buf->driScreenPriv;
408
409 TRACE;
410
411 /* check for signle-buffered */
412 if (backrb == NULL)
413 return;
414
415 /* check if swapping currently bound buffer */
416 if (ctx && ctx->DrawBuffer == &(buf->Base)) {
417 /* flush pending rendering */
418 _mesa_notifySwapBuffers(ctx);
419 }
420
421 screen->swrast_loader->putImage(buf, __DRI_SWRAST_IMAGE_OP_SWAP,
422 0, 0,
423 frontrb->Base.Width,
424 frontrb->Base.Height,
425 backrb->Base.Data,
426 buf->loaderPrivate);
427 }
428
429
430 /**
431 * General device driver functions.
432 */
433
434 static void
435 get_window_size( GLframebuffer *fb, GLsizei *w, GLsizei *h )
436 {
437 __DRIdrawable *buf = swrast_drawable(fb);
438 __DRIscreen *screen = buf->driScreenPriv;
439 int x, y;
440
441 screen->swrast_loader->getDrawableInfo(buf,
442 &x, &y, w, h,
443 buf->loaderPrivate);
444 }
445
446 static void
447 swrast_check_and_update_window_size( GLcontext *ctx, GLframebuffer *fb )
448 {
449 GLsizei width, height;
450
451 get_window_size(fb, &width, &height);
452 if (fb->Width != width || fb->Height != height) {
453 _mesa_resize_framebuffer(ctx, fb, width, height);
454 }
455 }
456
457 static const GLubyte *
458 get_string(GLcontext *ctx, GLenum pname)
459 {
460 (void) ctx;
461 switch (pname) {
462 case GL_VENDOR:
463 return (const GLubyte *) "Mesa Project";
464 case GL_RENDERER:
465 return (const GLubyte *) "Software Rasterizer";
466 default:
467 return NULL;
468 }
469 }
470
471 static void
472 update_state( GLcontext *ctx, GLuint new_state )
473 {
474 /* not much to do here - pass it on */
475 _swrast_InvalidateState( ctx, new_state );
476 _swsetup_InvalidateState( ctx, new_state );
477 _vbo_InvalidateState( ctx, new_state );
478 _tnl_InvalidateState( ctx, new_state );
479 }
480
481 static void
482 viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
483 {
484 GLframebuffer *draw = ctx->WinSysDrawBuffer;
485 GLframebuffer *read = ctx->WinSysReadBuffer;
486
487 swrast_check_and_update_window_size(ctx, draw);
488 swrast_check_and_update_window_size(ctx, read);
489 }
490
491 static void
492 swrast_init_driver_functions(struct dd_function_table *driver)
493 {
494 driver->GetString = get_string;
495 driver->UpdateState = update_state;
496 driver->GetBufferSize = NULL;
497 driver->Viewport = viewport;
498 }
499
500
501 /**
502 * Context-related functions.
503 */
504
505 static __DRIcontext *
506 driCreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
507 __DRIcontext *shared, void *data)
508 {
509 __DRIcontext *ctx;
510 GLcontext *mesaCtx;
511 struct dd_function_table functions;
512
513 TRACE;
514
515 ctx = calloc(1, sizeof *ctx);
516 if (!ctx)
517 return NULL;
518
519 ctx->loaderPrivate = data;
520
521 ctx->driScreenPriv = screen;
522
523 /* build table of device driver functions */
524 _mesa_init_driver_functions(&functions);
525 swrast_init_driver_functions(&functions);
526
527 if (!_mesa_initialize_context(&ctx->Base, &config->modes,
528 shared ? &shared->Base : NULL,
529 &functions, (void *) ctx)) {
530 free(ctx);
531 return NULL;
532 }
533
534 mesaCtx = &ctx->Base;
535
536 /* do bounds checking to prevent segfaults and server crashes! */
537 mesaCtx->Const.CheckArrayBounds = GL_TRUE;
538
539 /* create module contexts */
540 _swrast_CreateContext( mesaCtx );
541 _vbo_CreateContext( mesaCtx );
542 _tnl_CreateContext( mesaCtx );
543 _swsetup_CreateContext( mesaCtx );
544 _swsetup_Wakeup( mesaCtx );
545
546 /* use default TCL pipeline */
547 {
548 TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
549 tnl->Driver.RunPipeline = _tnl_run_pipeline;
550 }
551
552 _mesa_enable_sw_extensions(mesaCtx);
553 _mesa_enable_1_3_extensions(mesaCtx);
554 _mesa_enable_1_4_extensions(mesaCtx);
555 _mesa_enable_1_5_extensions(mesaCtx);
556 _mesa_enable_2_0_extensions(mesaCtx);
557 _mesa_enable_2_1_extensions(mesaCtx);
558
559 _mesa_meta_init(mesaCtx);
560
561 return ctx;
562 }
563
564 static void
565 driDestroyContext(__DRIcontext *ctx)
566 {
567 GLcontext *mesaCtx;
568 TRACE;
569
570 if (ctx) {
571 mesaCtx = &ctx->Base;
572 _mesa_meta_free(mesaCtx);
573 _swsetup_DestroyContext( mesaCtx );
574 _swrast_DestroyContext( mesaCtx );
575 _tnl_DestroyContext( mesaCtx );
576 _vbo_DestroyContext( mesaCtx );
577 _mesa_destroy_context( mesaCtx );
578 }
579 }
580
581 static int
582 driCopyContext(__DRIcontext *dst, __DRIcontext *src, unsigned long mask)
583 {
584 TRACE;
585
586 _mesa_copy_context(&src->Base, &dst->Base, mask);
587 return GL_TRUE;
588 }
589
590 static int driBindContext(__DRIcontext *ctx,
591 __DRIdrawable *draw,
592 __DRIdrawable *read)
593 {
594 GLcontext *mesaCtx;
595 GLframebuffer *mesaDraw;
596 GLframebuffer *mesaRead;
597 TRACE;
598
599 if (ctx) {
600 if (!draw || !read)
601 return GL_FALSE;
602
603 mesaCtx = &ctx->Base;
604 mesaDraw = &draw->Base;
605 mesaRead = &read->Base;
606
607 /* check for same context and buffer */
608 if (mesaCtx == _mesa_get_current_context()
609 && mesaCtx->DrawBuffer == mesaDraw
610 && mesaCtx->ReadBuffer == mesaRead) {
611 return GL_TRUE;
612 }
613
614 _glapi_check_multithread();
615
616 swrast_check_and_update_window_size(mesaCtx, mesaDraw);
617 if (read != draw)
618 swrast_check_and_update_window_size(mesaCtx, mesaRead);
619
620 _mesa_make_current( mesaCtx,
621 mesaDraw,
622 mesaRead );
623 }
624 else {
625 /* unbind */
626 _mesa_make_current( NULL, NULL, NULL );
627 }
628
629 return GL_TRUE;
630 }
631
632 static int driUnbindContext(__DRIcontext *ctx)
633 {
634 TRACE;
635 (void) ctx;
636 return GL_TRUE;
637 }
638
639
640 static const __DRIcoreExtension driCoreExtension = {
641 { __DRI_CORE, __DRI_CORE_VERSION },
642 NULL, /* driCreateNewScreen */
643 driDestroyScreen,
644 driGetExtensions,
645 driGetConfigAttrib,
646 driIndexConfigAttrib,
647 NULL, /* driCreateNewDrawable */
648 driDestroyDrawable,
649 driSwapBuffers,
650 driCreateNewContext,
651 driCopyContext,
652 driDestroyContext,
653 driBindContext,
654 driUnbindContext
655 };
656
657 static const __DRIswrastExtension driSWRastExtension = {
658 { __DRI_SWRAST, __DRI_SWRAST_VERSION },
659 driCreateNewScreen,
660 driCreateNewDrawable
661 };
662
663 /* This is the table of extensions that the loader will dlsym() for. */
664 PUBLIC const __DRIextension *__driDriverExtensions[] = {
665 &driCoreExtension.base,
666 &driSWRastExtension.base,
667 NULL
668 };