b76cd2631320d84d4e898f1b55b5d631647db192
[mesa.git] / src / mesa / drivers / dri / swrast / swrast.c
1 /*
2 * Copyright 2008, 2010 George Sapountzis <gsapountzis@gmail.com>
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 "main/teximage.h"
51 #include "main/texformat.h"
52 #include "main/texstate.h"
53
54 #include "swrast_priv.h"
55
56
57 /**
58 * Screen and config-related functions
59 */
60
61 static void swrastSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
62 GLint texture_format, __DRIdrawable *dPriv)
63 {
64 struct dri_context *dri_ctx;
65 int x, y, w, h;
66 __DRIscreen *sPriv = dPriv->driScreenPriv;
67 struct gl_texture_unit *texUnit;
68 struct gl_texture_object *texObj;
69 struct gl_texture_image *texImage;
70 uint32_t internalFormat;
71 gl_format texFormat;
72
73 dri_ctx = pDRICtx->driverPrivate;
74
75 internalFormat = (texture_format == __DRI_TEXTURE_FORMAT_RGB ? 3 : 4);
76
77 texUnit = _mesa_get_current_tex_unit(&dri_ctx->Base);
78 texObj = _mesa_select_tex_object(&dri_ctx->Base, texUnit, target);
79 texImage = _mesa_get_tex_image(&dri_ctx->Base, texObj, target, 0);
80
81 _mesa_lock_texture(&dri_ctx->Base, texObj);
82
83 sPriv->swrast_loader->getDrawableInfo(dPriv, &x, &y, &w, &h, dPriv->loaderPrivate);
84
85 if (texture_format == __DRI_TEXTURE_FORMAT_RGB)
86 texFormat = MESA_FORMAT_XRGB8888;
87 else
88 texFormat = MESA_FORMAT_ARGB8888;
89
90 _mesa_init_teximage_fields(&dri_ctx->Base, target, texImage,
91 w, h, 1, 0, internalFormat, texFormat);
92
93 sPriv->swrast_loader->getImage(dPriv, x, y, w, h, (char *)texImage->Data,
94 dPriv->loaderPrivate);
95
96 _mesa_unlock_texture(&dri_ctx->Base, texObj);
97 }
98
99 static void swrastSetTexBuffer(__DRIcontext *pDRICtx, GLint target,
100 __DRIdrawable *dPriv)
101 {
102 swrastSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
103 }
104
105 static const __DRItexBufferExtension swrastTexBufferExtension = {
106 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
107 swrastSetTexBuffer,
108 swrastSetTexBuffer2,
109 };
110
111 static const __DRIextension *dri_screen_extensions[] = {
112 &swrastTexBufferExtension.base,
113 NULL
114 };
115
116 static __DRIconfig **
117 swrastFillInModes(__DRIscreen *psp,
118 unsigned pixel_bits, unsigned depth_bits,
119 unsigned stencil_bits, GLboolean have_back_buffer)
120 {
121 __DRIconfig **configs;
122 unsigned depth_buffer_factor;
123 unsigned back_buffer_factor;
124 GLenum fb_format;
125 GLenum fb_type;
126
127 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
128 * support pageflipping at all.
129 */
130 static const GLenum back_buffer_modes[] = {
131 GLX_NONE, GLX_SWAP_UNDEFINED_OML
132 };
133
134 uint8_t depth_bits_array[4];
135 uint8_t stencil_bits_array[4];
136 uint8_t msaa_samples_array[1];
137
138 (void) psp;
139 (void) have_back_buffer;
140
141 depth_bits_array[0] = 0;
142 depth_bits_array[1] = 0;
143 depth_bits_array[2] = depth_bits;
144 depth_bits_array[3] = depth_bits;
145
146 /* Just like with the accumulation buffer, always provide some modes
147 * with a stencil buffer.
148 */
149 stencil_bits_array[0] = 0;
150 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
151 stencil_bits_array[2] = 0;
152 stencil_bits_array[3] = (stencil_bits == 0) ? 8 : stencil_bits;
153
154 msaa_samples_array[0] = 0;
155
156 depth_buffer_factor = 4;
157 back_buffer_factor = 2;
158
159 switch (pixel_bits) {
160 case 8:
161 fb_format = GL_RGB;
162 fb_type = GL_UNSIGNED_BYTE_2_3_3_REV;
163 break;
164 case 16:
165 fb_format = GL_RGB;
166 fb_type = GL_UNSIGNED_SHORT_5_6_5;
167 break;
168 case 24:
169 fb_format = GL_BGR;
170 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
171 break;
172 case 32:
173 fb_format = GL_BGRA;
174 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
175 break;
176 default:
177 fprintf(stderr, "[%s:%u] bad depth %d\n", __func__, __LINE__,
178 pixel_bits);
179 return NULL;
180 }
181
182 configs = driCreateConfigs(fb_format, fb_type,
183 depth_bits_array, stencil_bits_array,
184 depth_buffer_factor, back_buffer_modes,
185 back_buffer_factor, msaa_samples_array, 1,
186 GL_TRUE);
187 if (configs == NULL) {
188 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
189 __LINE__);
190 return NULL;
191 }
192
193 return configs;
194 }
195
196 static const __DRIconfig **
197 dri_init_screen(__DRIscreen * psp)
198 {
199 __DRIconfig **configs8, **configs16, **configs24, **configs32;
200
201 TRACE;
202
203 psp->extensions = dri_screen_extensions;
204
205 configs8 = swrastFillInModes(psp, 8, 8, 0, 1);
206 configs16 = swrastFillInModes(psp, 16, 16, 0, 1);
207 configs24 = swrastFillInModes(psp, 24, 24, 8, 1);
208 configs32 = swrastFillInModes(psp, 32, 24, 8, 1);
209
210 configs16 = driConcatConfigs(configs8, configs16);
211 configs24 = driConcatConfigs(configs16, configs24);
212 configs32 = driConcatConfigs(configs24, configs32);
213
214 return (const __DRIconfig **)configs32;
215 }
216
217 static void
218 dri_destroy_screen(__DRIscreen * sPriv)
219 {
220 TRACE;
221 (void) sPriv;
222 }
223
224
225 /**
226 * Framebuffer and renderbuffer-related functions.
227 */
228
229 static GLuint
230 choose_pixel_format(const struct gl_config *v)
231 {
232 int depth = v->rgbBits;
233
234 if (depth == 32
235 && v->redMask == 0xff0000
236 && v->greenMask == 0x00ff00
237 && v->blueMask == 0x0000ff)
238 return PF_A8R8G8B8;
239 else if (depth == 24
240 && v->redMask == 0xff0000
241 && v->greenMask == 0x00ff00
242 && v->blueMask == 0x0000ff)
243 return PF_X8R8G8B8;
244 else if (depth == 16
245 && v->redMask == 0xf800
246 && v->greenMask == 0x07e0
247 && v->blueMask == 0x001f)
248 return PF_R5G6B5;
249 else if (depth == 8
250 && v->redMask == 0x07
251 && v->greenMask == 0x38
252 && v->blueMask == 0xc0)
253 return PF_R3G3B2;
254
255 _mesa_problem( NULL, "unexpected format in %s", __FUNCTION__ );
256 return 0;
257 }
258
259 static void
260 swrast_delete_renderbuffer(struct gl_renderbuffer *rb)
261 {
262 TRACE;
263
264 free(rb->Data);
265 free(rb);
266 }
267
268 /* see bytes_per_line in libGL */
269 static INLINE int
270 bytes_per_line(unsigned pitch_bits, unsigned mul)
271 {
272 unsigned mask = mul - 1;
273
274 return ((pitch_bits + mask) & ~mask) / 8;
275 }
276
277 static GLboolean
278 swrast_alloc_front_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
279 GLenum internalFormat, GLuint width, GLuint height)
280 {
281 struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
282
283 TRACE;
284
285 (void) ctx;
286 (void) internalFormat;
287
288 rb->Data = NULL;
289 rb->Width = width;
290 rb->Height = height;
291
292 xrb->pitch = bytes_per_line(width * xrb->bpp, 32);
293
294 return GL_TRUE;
295 }
296
297 static GLboolean
298 swrast_alloc_back_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
299 GLenum internalFormat, GLuint width, GLuint height)
300 {
301 struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
302
303 TRACE;
304
305 free(rb->Data);
306
307 swrast_alloc_front_storage(ctx, rb, internalFormat, width, height);
308
309 rb->Data = malloc(height * xrb->pitch);
310
311 return GL_TRUE;
312 }
313
314 static struct swrast_renderbuffer *
315 swrast_new_renderbuffer(const struct gl_config *visual, GLboolean front)
316 {
317 struct swrast_renderbuffer *xrb = calloc(1, sizeof *xrb);
318 GLuint pixel_format;
319
320 TRACE;
321
322 if (!xrb)
323 return NULL;
324
325 _mesa_init_renderbuffer(&xrb->Base, 0);
326
327 pixel_format = choose_pixel_format(visual);
328
329 xrb->Base.Delete = swrast_delete_renderbuffer;
330 if (front) {
331 xrb->Base.AllocStorage = swrast_alloc_front_storage;
332 swrast_set_span_funcs_front(xrb, pixel_format);
333 }
334 else {
335 xrb->Base.AllocStorage = swrast_alloc_back_storage;
336 swrast_set_span_funcs_back(xrb, pixel_format);
337 }
338
339 switch (pixel_format) {
340 case PF_A8R8G8B8:
341 xrb->Base.Format = MESA_FORMAT_ARGB8888;
342 xrb->Base.InternalFormat = GL_RGBA;
343 xrb->Base._BaseFormat = GL_RGBA;
344 xrb->Base.DataType = GL_UNSIGNED_BYTE;
345 xrb->bpp = 32;
346 break;
347 case PF_X8R8G8B8:
348 xrb->Base.Format = MESA_FORMAT_ARGB8888; /* XXX */
349 xrb->Base.InternalFormat = GL_RGB;
350 xrb->Base._BaseFormat = GL_RGB;
351 xrb->Base.DataType = GL_UNSIGNED_BYTE;
352 xrb->bpp = 32;
353 break;
354 case PF_R5G6B5:
355 xrb->Base.Format = MESA_FORMAT_RGB565;
356 xrb->Base.InternalFormat = GL_RGB;
357 xrb->Base._BaseFormat = GL_RGB;
358 xrb->Base.DataType = GL_UNSIGNED_BYTE;
359 xrb->bpp = 16;
360 break;
361 case PF_R3G3B2:
362 xrb->Base.Format = MESA_FORMAT_RGB332;
363 xrb->Base.InternalFormat = GL_RGB;
364 xrb->Base._BaseFormat = GL_RGB;
365 xrb->Base.DataType = GL_UNSIGNED_BYTE;
366 xrb->bpp = 8;
367 break;
368 default:
369 return NULL;
370 }
371
372 return xrb;
373 }
374
375 static GLboolean
376 dri_create_buffer(__DRIscreen * sPriv,
377 __DRIdrawable * dPriv,
378 const struct gl_config * visual, GLboolean isPixmap)
379 {
380 struct dri_drawable *drawable = NULL;
381 struct gl_framebuffer *fb;
382 struct swrast_renderbuffer *frontrb, *backrb;
383
384 TRACE;
385
386 (void) sPriv;
387 (void) isPixmap;
388
389 drawable = CALLOC_STRUCT(dri_drawable);
390 if (drawable == NULL)
391 goto drawable_fail;
392
393 dPriv->driverPrivate = drawable;
394 drawable->dPriv = dPriv;
395
396 drawable->row = malloc(MAX_WIDTH * 4);
397 if (drawable->row == NULL)
398 goto drawable_fail;
399
400 fb = &drawable->Base;
401
402 /* basic framebuffer setup */
403 _mesa_initialize_window_framebuffer(fb, visual);
404
405 /* add front renderbuffer */
406 frontrb = swrast_new_renderbuffer(visual, GL_TRUE);
407 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontrb->Base);
408
409 /* add back renderbuffer */
410 if (visual->doubleBufferMode) {
411 backrb = swrast_new_renderbuffer(visual, GL_FALSE);
412 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backrb->Base);
413 }
414
415 /* add software renderbuffers */
416 _mesa_add_soft_renderbuffers(fb,
417 GL_FALSE, /* color */
418 visual->haveDepthBuffer,
419 visual->haveStencilBuffer,
420 visual->haveAccumBuffer,
421 GL_FALSE, /* alpha */
422 GL_FALSE /* aux bufs */);
423
424 return GL_TRUE;
425
426 drawable_fail:
427
428 if (drawable)
429 free(drawable->row);
430
431 FREE(drawable);
432
433 return GL_FALSE;
434 }
435
436 static void
437 dri_destroy_buffer(__DRIdrawable * dPriv)
438 {
439 TRACE;
440
441 if (dPriv) {
442 struct dri_drawable *drawable = dri_drawable(dPriv);
443 struct gl_framebuffer *fb;
444
445 free(drawable->row);
446
447 fb = &drawable->Base;
448
449 fb->DeletePending = GL_TRUE;
450 _mesa_reference_framebuffer(&fb, NULL);
451 }
452 }
453
454 static void
455 dri_swap_buffers(__DRIdrawable * dPriv)
456 {
457 __DRIscreen *sPriv = dPriv->driScreenPriv;
458
459 GET_CURRENT_CONTEXT(ctx);
460
461 struct dri_drawable *drawable = dri_drawable(dPriv);
462 struct gl_framebuffer *fb;
463 struct swrast_renderbuffer *frontrb, *backrb;
464
465 TRACE;
466
467 fb = &drawable->Base;
468
469 frontrb =
470 swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
471 backrb =
472 swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
473
474 /* check for signle-buffered */
475 if (backrb == NULL)
476 return;
477
478 /* check if swapping currently bound buffer */
479 if (ctx && ctx->DrawBuffer == fb) {
480 /* flush pending rendering */
481 _mesa_notifySwapBuffers(ctx);
482 }
483
484 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
485 0, 0,
486 frontrb->Base.Width,
487 frontrb->Base.Height,
488 backrb->Base.Data,
489 dPriv->loaderPrivate);
490 }
491
492
493 /**
494 * General device driver functions.
495 */
496
497 static void
498 get_window_size( struct gl_framebuffer *fb, GLsizei *w, GLsizei *h )
499 {
500 __DRIdrawable *dPriv = swrast_drawable(fb)->dPriv;
501 __DRIscreen *sPriv = dPriv->driScreenPriv;
502 int x, y;
503
504 sPriv->swrast_loader->getDrawableInfo(dPriv,
505 &x, &y, w, h,
506 dPriv->loaderPrivate);
507 }
508
509 static void
510 swrast_check_and_update_window_size( struct gl_context *ctx, struct gl_framebuffer *fb )
511 {
512 GLsizei width, height;
513
514 get_window_size(fb, &width, &height);
515 if (fb->Width != width || fb->Height != height) {
516 _mesa_resize_framebuffer(ctx, fb, width, height);
517 }
518 }
519
520 static const GLubyte *
521 get_string(struct gl_context *ctx, GLenum pname)
522 {
523 (void) ctx;
524 switch (pname) {
525 case GL_VENDOR:
526 return (const GLubyte *) "Mesa Project";
527 case GL_RENDERER:
528 return (const GLubyte *) "Software Rasterizer";
529 default:
530 return NULL;
531 }
532 }
533
534 static void
535 update_state( struct gl_context *ctx, GLuint new_state )
536 {
537 /* not much to do here - pass it on */
538 _swrast_InvalidateState( ctx, new_state );
539 _swsetup_InvalidateState( ctx, new_state );
540 _vbo_InvalidateState( ctx, new_state );
541 _tnl_InvalidateState( ctx, new_state );
542 }
543
544 static void
545 viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
546 {
547 struct gl_framebuffer *draw = ctx->WinSysDrawBuffer;
548 struct gl_framebuffer *read = ctx->WinSysReadBuffer;
549
550 (void) x;
551 (void) y;
552 (void) w;
553 (void) h;
554 swrast_check_and_update_window_size(ctx, draw);
555 swrast_check_and_update_window_size(ctx, read);
556 }
557
558 static gl_format swrastChooseTextureFormat(struct gl_context * ctx,
559 GLint internalFormat,
560 GLenum format,
561 GLenum type)
562 {
563 if (internalFormat == GL_RGB)
564 return MESA_FORMAT_XRGB8888;
565 return _mesa_choose_tex_format(ctx, internalFormat, format, type);
566 }
567
568 static void
569 swrast_init_driver_functions(struct dd_function_table *driver)
570 {
571 driver->GetString = get_string;
572 driver->UpdateState = update_state;
573 driver->GetBufferSize = NULL;
574 driver->Viewport = viewport;
575 driver->ChooseTextureFormat = swrastChooseTextureFormat;
576 }
577
578 static const char *es2_extensions[] = {
579 /* Used by mesa internally (cf all_mesa_extensions in ../common/utils.c) */
580 "GL_ARB_draw_buffers",
581 "GL_ARB_transpose_matrix",
582 "GL_ARB_window_pos",
583 "GL_EXT_blend_func_separate",
584 "GL_EXT_compiled_vertex_array",
585 "GL_EXT_framebuffer_blit",
586 "GL_IBM_multimode_draw_arrays",
587 "GL_MESA_window_pos",
588 "GL_NV_vertex_program",
589
590 /* Required by GLES2 */
591 "GL_ARB_fragment_program",
592 "GL_ARB_fragment_shader",
593 "GL_ARB_shader_objects",
594 "GL_ARB_texture_cube_map",
595 "GL_ARB_texture_mirrored_repeat",
596 "GL_ARB_texture_non_power_of_two",
597 "GL_ARB_vertex_shader",
598 "GL_EXT_blend_color",
599 "GL_EXT_blend_equation_separate",
600 "GL_EXT_blend_minmax",
601 "GL_EXT_blend_subtract",
602
603 /* Optional GLES2 */
604 "GL_ARB_framebuffer_object",
605 "GL_EXT_texture_filter_anisotropic",
606 "GL_ARB_depth_texture",
607 "GL_EXT_packed_depth_stencil",
608 "GL_EXT_framebuffer_object",
609 NULL,
610 };
611
612 static void
613 InitExtensionsES2(struct gl_context *ctx)
614 {
615 int i;
616
617 for (i = 0; es2_extensions[i]; i++)
618 _mesa_enable_extension(ctx, es2_extensions[i]);
619 }
620
621 /**
622 * Context-related functions.
623 */
624
625 static GLboolean
626 dri_create_context(gl_api api,
627 const struct gl_config * visual,
628 __DRIcontext * cPriv, void *sharedContextPrivate)
629 {
630 struct dri_context *ctx = NULL;
631 struct dri_context *share = (struct dri_context *)sharedContextPrivate;
632 struct gl_context *mesaCtx = NULL;
633 struct gl_context *sharedCtx = NULL;
634 struct dd_function_table functions;
635
636 TRACE;
637
638 ctx = CALLOC_STRUCT(dri_context);
639 if (ctx == NULL)
640 goto context_fail;
641
642 cPriv->driverPrivate = ctx;
643 ctx->cPriv = cPriv;
644
645 /* build table of device driver functions */
646 _mesa_init_driver_functions(&functions);
647 swrast_init_driver_functions(&functions);
648
649 if (share) {
650 sharedCtx = &share->Base;
651 }
652
653 mesaCtx = &ctx->Base;
654
655 /* basic context setup */
656 if (!_mesa_initialize_context(mesaCtx, api, visual, sharedCtx, &functions, (void *) cPriv)) {
657 goto context_fail;
658 }
659
660 /* do bounds checking to prevent segfaults and server crashes! */
661 mesaCtx->Const.CheckArrayBounds = GL_TRUE;
662
663 /* create module contexts */
664 _swrast_CreateContext( mesaCtx );
665 _vbo_CreateContext( mesaCtx );
666 _tnl_CreateContext( mesaCtx );
667 _swsetup_CreateContext( mesaCtx );
668 _swsetup_Wakeup( mesaCtx );
669
670 /* use default TCL pipeline */
671 {
672 TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
673 tnl->Driver.RunPipeline = _tnl_run_pipeline;
674 }
675
676 _mesa_meta_init(mesaCtx);
677 _mesa_enable_sw_extensions(mesaCtx);
678
679 switch (api) {
680 case API_OPENGL:
681 _mesa_enable_1_3_extensions(mesaCtx);
682 _mesa_enable_1_4_extensions(mesaCtx);
683 _mesa_enable_1_5_extensions(mesaCtx);
684 _mesa_enable_2_0_extensions(mesaCtx);
685 _mesa_enable_2_1_extensions(mesaCtx);
686 break;
687 case API_OPENGLES:
688 _mesa_enable_1_3_extensions(mesaCtx);
689 _mesa_enable_1_4_extensions(mesaCtx);
690 _mesa_enable_1_5_extensions(mesaCtx);
691
692 break;
693 case API_OPENGLES2:
694 InitExtensionsES2( mesaCtx);
695 break;
696 }
697
698 return GL_TRUE;
699
700 context_fail:
701
702 FREE(ctx);
703
704 return GL_FALSE;
705 }
706
707 static void
708 dri_destroy_context(__DRIcontext * cPriv)
709 {
710 TRACE;
711
712 if (cPriv) {
713 struct dri_context *ctx = dri_context(cPriv);
714 struct gl_context *mesaCtx;
715
716 mesaCtx = &ctx->Base;
717
718 _mesa_meta_free(mesaCtx);
719 _swsetup_DestroyContext( mesaCtx );
720 _swrast_DestroyContext( mesaCtx );
721 _tnl_DestroyContext( mesaCtx );
722 _vbo_DestroyContext( mesaCtx );
723 _mesa_destroy_context( mesaCtx );
724 }
725 }
726
727 static GLboolean
728 dri_make_current(__DRIcontext * cPriv,
729 __DRIdrawable * driDrawPriv,
730 __DRIdrawable * driReadPriv)
731 {
732 struct gl_context *mesaCtx;
733 struct gl_framebuffer *mesaDraw;
734 struct gl_framebuffer *mesaRead;
735 TRACE;
736
737 if (cPriv) {
738 struct dri_context *ctx = dri_context(cPriv);
739 struct dri_drawable *draw;
740 struct dri_drawable *read;
741
742 if (!driDrawPriv || !driReadPriv)
743 return GL_FALSE;
744
745 draw = dri_drawable(driDrawPriv);
746 read = dri_drawable(driReadPriv);
747 mesaCtx = &ctx->Base;
748 mesaDraw = &draw->Base;
749 mesaRead = &read->Base;
750
751 /* check for same context and buffer */
752 if (mesaCtx == _mesa_get_current_context()
753 && mesaCtx->DrawBuffer == mesaDraw
754 && mesaCtx->ReadBuffer == mesaRead) {
755 return GL_TRUE;
756 }
757
758 _glapi_check_multithread();
759
760 swrast_check_and_update_window_size(mesaCtx, mesaDraw);
761 if (mesaRead != mesaDraw)
762 swrast_check_and_update_window_size(mesaCtx, mesaRead);
763
764 _mesa_make_current( mesaCtx,
765 mesaDraw,
766 mesaRead );
767 }
768 else {
769 /* unbind */
770 _mesa_make_current( NULL, NULL, NULL );
771 }
772
773 return GL_TRUE;
774 }
775
776 static GLboolean
777 dri_unbind_context(__DRIcontext * cPriv)
778 {
779 TRACE;
780 (void) cPriv;
781
782 /* Unset current context and dispath table */
783 _mesa_make_current(NULL, NULL, NULL);
784
785 return GL_TRUE;
786 }
787
788
789 const struct __DriverAPIRec driDriverAPI = {
790 .InitScreen = dri_init_screen,
791 .DestroyScreen = dri_destroy_screen,
792 .CreateContext = dri_create_context,
793 .DestroyContext = dri_destroy_context,
794 .CreateBuffer = dri_create_buffer,
795 .DestroyBuffer = dri_destroy_buffer,
796 .SwapBuffers = dri_swap_buffers,
797 .MakeCurrent = dri_make_current,
798 .UnbindContext = dri_unbind_context,
799 };
800
801 /* This is the table of extensions that the loader will dlsym() for. */
802 PUBLIC const __DRIextension *__driDriverExtensions[] = {
803 &driCoreExtension.base,
804 &driSWRastExtension.base,
805 NULL
806 };