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