swrast.c revision 763b00f2c596e167299e81684763b52422956dfc
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
61static 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
99static void swrastSetTexBuffer(__DRIcontext *pDRICtx, GLint target,
100			       __DRIdrawable *dPriv)
101{
102    swrastSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
103}
104
105static const __DRItexBufferExtension swrastTexBufferExtension = {
106    { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
107    swrastSetTexBuffer,
108    swrastSetTexBuffer2,
109};
110
111static const __DRIextension *dri_screen_extensions[] = {
112    &swrastTexBufferExtension.base,
113    NULL
114};
115
116static __DRIconfig **
117swrastFillInModes(__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
196static const __DRIconfig **
197dri_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
217static void
218dri_destroy_screen(__DRIscreen * sPriv)
219{
220    TRACE;
221    (void) sPriv;
222}
223
224
225/**
226 * Framebuffer and renderbuffer-related functions.
227 */
228
229static GLuint
230choose_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
259static void
260swrast_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 */
269static INLINE int
270bytes_per_line(unsigned pitch_bits, unsigned mul)
271{
272   unsigned mask = mul - 1;
273
274   return ((pitch_bits + mask) & ~mask) / 8;
275}
276
277static GLboolean
278swrast_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
297static GLboolean
298swrast_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
314static struct swrast_renderbuffer *
315swrast_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
375static GLboolean
376dri_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
426drawable_fail:
427
428    if (drawable)
429	free(drawable->row);
430
431    FREE(drawable);
432
433    return GL_FALSE;
434}
435
436static void
437dri_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
454static void
455dri_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
497static void
498get_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
509static void
510swrast_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
520static const GLubyte *
521get_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
534static void
535update_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
544static void
545viewport(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
558static 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
568static void
569swrast_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
578static const char *es2_extensions[] = {
579   /* Used by mesa internally (cf all_mesa_extensions in ../common/utils.c) */
580   "GL_ARB_transpose_matrix",
581   "GL_ARB_window_pos",
582   "GL_EXT_blend_func_separate",
583   "GL_EXT_compiled_vertex_array",
584   "GL_EXT_framebuffer_blit",
585   "GL_IBM_multimode_draw_arrays",
586   "GL_MESA_window_pos",
587   "GL_NV_vertex_program",
588
589   /* Required by GLES2 */
590   "GL_ARB_fragment_program",
591   "GL_ARB_fragment_shader",
592   "GL_ARB_shader_objects",
593   "GL_ARB_texture_cube_map",
594   "GL_ARB_texture_non_power_of_two",
595   "GL_ARB_vertex_shader",
596   "GL_EXT_blend_color",
597   "GL_EXT_blend_equation_separate",
598   "GL_EXT_blend_minmax",
599
600   /* Optional GLES2 */
601   "GL_ARB_framebuffer_object",
602   "GL_EXT_texture_filter_anisotropic",
603   "GL_ARB_depth_texture",
604   "GL_EXT_packed_depth_stencil",
605   "GL_EXT_framebuffer_object",
606   NULL,
607};
608
609static void
610InitExtensionsES2(struct gl_context *ctx)
611{
612   int i;
613
614   for (i = 0; es2_extensions[i]; i++)
615      _mesa_enable_extension(ctx, es2_extensions[i]);
616}
617
618/**
619 * Context-related functions.
620 */
621
622static GLboolean
623dri_create_context(gl_api api,
624		   const struct gl_config * visual,
625		   __DRIcontext * cPriv, void *sharedContextPrivate)
626{
627    struct dri_context *ctx = NULL;
628    struct dri_context *share = (struct dri_context *)sharedContextPrivate;
629    struct gl_context *mesaCtx = NULL;
630    struct gl_context *sharedCtx = NULL;
631    struct dd_function_table functions;
632
633    TRACE;
634
635    ctx = CALLOC_STRUCT(dri_context);
636    if (ctx == NULL)
637	goto context_fail;
638
639    cPriv->driverPrivate = ctx;
640    ctx->cPriv = cPriv;
641
642    /* build table of device driver functions */
643    _mesa_init_driver_functions(&functions);
644    swrast_init_driver_functions(&functions);
645
646    if (share) {
647	sharedCtx = &share->Base;
648    }
649
650    mesaCtx = &ctx->Base;
651
652    /* basic context setup */
653    if (!_mesa_initialize_context(mesaCtx, api, visual, sharedCtx, &functions, (void *) cPriv)) {
654	goto context_fail;
655    }
656
657    /* do bounds checking to prevent segfaults and server crashes! */
658    mesaCtx->Const.CheckArrayBounds = GL_TRUE;
659
660    /* create module contexts */
661    _swrast_CreateContext( mesaCtx );
662    _vbo_CreateContext( mesaCtx );
663    _tnl_CreateContext( mesaCtx );
664    _swsetup_CreateContext( mesaCtx );
665    _swsetup_Wakeup( mesaCtx );
666
667    /* use default TCL pipeline */
668    {
669       TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
670       tnl->Driver.RunPipeline = _tnl_run_pipeline;
671    }
672
673    _mesa_meta_init(mesaCtx);
674    _mesa_enable_sw_extensions(mesaCtx);
675
676    switch (api) {
677    case API_OPENGL:
678        _mesa_enable_1_3_extensions(mesaCtx);
679        _mesa_enable_1_4_extensions(mesaCtx);
680        _mesa_enable_1_5_extensions(mesaCtx);
681        _mesa_enable_2_0_extensions(mesaCtx);
682        _mesa_enable_2_1_extensions(mesaCtx);
683        break;
684    case API_OPENGLES:
685        _mesa_enable_1_3_extensions(mesaCtx);
686        _mesa_enable_1_4_extensions(mesaCtx);
687        _mesa_enable_1_5_extensions(mesaCtx);
688
689        break;
690    case API_OPENGLES2:
691        InitExtensionsES2( mesaCtx);
692        break;
693    }
694
695    return GL_TRUE;
696
697context_fail:
698
699    FREE(ctx);
700
701    return GL_FALSE;
702}
703
704static void
705dri_destroy_context(__DRIcontext * cPriv)
706{
707    TRACE;
708
709    if (cPriv) {
710	struct dri_context *ctx = dri_context(cPriv);
711	struct gl_context *mesaCtx;
712
713	mesaCtx = &ctx->Base;
714
715        _mesa_meta_free(mesaCtx);
716	_swsetup_DestroyContext( mesaCtx );
717	_swrast_DestroyContext( mesaCtx );
718	_tnl_DestroyContext( mesaCtx );
719	_vbo_DestroyContext( mesaCtx );
720	_mesa_destroy_context( mesaCtx );
721    }
722}
723
724static GLboolean
725dri_make_current(__DRIcontext * cPriv,
726		 __DRIdrawable * driDrawPriv,
727		 __DRIdrawable * driReadPriv)
728{
729    struct gl_context *mesaCtx;
730    struct gl_framebuffer *mesaDraw;
731    struct gl_framebuffer *mesaRead;
732    TRACE;
733
734    if (cPriv) {
735	struct dri_context *ctx = dri_context(cPriv);
736	struct dri_drawable *draw;
737	struct dri_drawable *read;
738
739	if (!driDrawPriv || !driReadPriv)
740	    return GL_FALSE;
741
742	draw = dri_drawable(driDrawPriv);
743	read = dri_drawable(driReadPriv);
744	mesaCtx = &ctx->Base;
745	mesaDraw = &draw->Base;
746	mesaRead = &read->Base;
747
748	/* check for same context and buffer */
749	if (mesaCtx == _mesa_get_current_context()
750	    && mesaCtx->DrawBuffer == mesaDraw
751	    && mesaCtx->ReadBuffer == mesaRead) {
752	    return GL_TRUE;
753	}
754
755	_glapi_check_multithread();
756
757	swrast_check_and_update_window_size(mesaCtx, mesaDraw);
758	if (mesaRead != mesaDraw)
759	    swrast_check_and_update_window_size(mesaCtx, mesaRead);
760
761	_mesa_make_current( mesaCtx,
762			    mesaDraw,
763			    mesaRead );
764    }
765    else {
766	/* unbind */
767	_mesa_make_current( NULL, NULL, NULL );
768    }
769
770    return GL_TRUE;
771}
772
773static GLboolean
774dri_unbind_context(__DRIcontext * cPriv)
775{
776    TRACE;
777    (void) cPriv;
778
779    /* Unset current context and dispath table */
780    _mesa_make_current(NULL, NULL, NULL);
781
782    return GL_TRUE;
783}
784
785
786const struct __DriverAPIRec driDriverAPI = {
787    .InitScreen = dri_init_screen,
788    .DestroyScreen = dri_destroy_screen,
789    .CreateContext = dri_create_context,
790    .DestroyContext = dri_destroy_context,
791    .CreateBuffer = dri_create_buffer,
792    .DestroyBuffer = dri_destroy_buffer,
793    .SwapBuffers = dri_swap_buffers,
794    .MakeCurrent = dri_make_current,
795    .UnbindContext = dri_unbind_context,
796};
797
798/* This is the table of extensions that the loader will dlsym() for. */
799PUBLIC const __DRIextension *__driDriverExtensions[] = {
800    &driCoreExtension.base,
801    &driSWRastExtension.base,
802    NULL
803};
804