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