Fix 24bpp software rendering
[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 depth_bits_array[0] = 0;
139 depth_bits_array[1] = 0;
140 depth_bits_array[2] = depth_bits;
141 depth_bits_array[3] = depth_bits;
142
143 /* Just like with the accumulation buffer, always provide some modes
144 * with a stencil buffer.
145 */
146 stencil_bits_array[0] = 0;
147 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
148 stencil_bits_array[2] = 0;
149 stencil_bits_array[3] = (stencil_bits == 0) ? 8 : stencil_bits;
150
151 msaa_samples_array[0] = 0;
152
153 depth_buffer_factor = 4;
154 back_buffer_factor = 2;
155
156 switch (pixel_bits) {
157 case 8:
158 fb_format = GL_RGB;
159 fb_type = GL_UNSIGNED_BYTE_2_3_3_REV;
160 break;
161 case 16:
162 fb_format = GL_RGB;
163 fb_type = GL_UNSIGNED_SHORT_5_6_5;
164 break;
165 case 24:
166 fb_format = GL_BGR;
167 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
168 break;
169 case 32:
170 fb_format = GL_BGRA;
171 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
172 break;
173 default:
174 fprintf(stderr, "[%s:%u] bad depth %d\n", __func__, __LINE__,
175 pixel_bits);
176 return NULL;
177 }
178
179 configs = driCreateConfigs(fb_format, fb_type,
180 depth_bits_array, stencil_bits_array,
181 depth_buffer_factor, back_buffer_modes,
182 back_buffer_factor, msaa_samples_array, 1,
183 GL_TRUE);
184 if (configs == NULL) {
185 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
186 __LINE__);
187 return NULL;
188 }
189
190 return configs;
191 }
192
193 static const __DRIconfig **
194 dri_init_screen(__DRIscreen * psp)
195 {
196 __DRIconfig **configs8, **configs16, **configs24, **configs32;
197
198 TRACE;
199
200 psp->extensions = dri_screen_extensions;
201
202 configs8 = swrastFillInModes(psp, 8, 8, 0, 1);
203 configs16 = swrastFillInModes(psp, 16, 16, 0, 1);
204 configs24 = swrastFillInModes(psp, 24, 24, 8, 1);
205 configs32 = swrastFillInModes(psp, 32, 24, 8, 1);
206
207 configs16 = driConcatConfigs(configs8, configs16);
208 configs24 = driConcatConfigs(configs16, configs24);
209 configs32 = driConcatConfigs(configs24, configs32);
210
211 return (const __DRIconfig **)configs32;
212 }
213
214 static void
215 dri_destroy_screen(__DRIscreen * sPriv)
216 {
217 TRACE;
218 }
219
220
221 /**
222 * Framebuffer and renderbuffer-related functions.
223 */
224
225 static GLuint
226 choose_pixel_format(const struct gl_config *v)
227 {
228 int depth = v->rgbBits;
229
230 if (depth == 32
231 && v->redMask == 0xff0000
232 && v->greenMask == 0x00ff00
233 && v->blueMask == 0x0000ff)
234 return PF_A8R8G8B8;
235 else if (depth == 24
236 && v->depthBits == 32
237 && v->redMask == 0xff0000
238 && v->greenMask == 0x00ff00
239 && v->blueMask == 0x0000ff)
240 return PF_X8R8G8B8;
241 else if (depth == 24
242 && v->depthBits == 24
243 && v->redMask == 0xff0000
244 && v->greenMask == 0x00ff00
245 && v->blueMask == 0x0000ff)
246 return PF_R8G8B8;
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
262 static void
263 swrast_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 */
272 static INLINE int
273 bytes_per_line(unsigned pitch_bits, unsigned mul)
274 {
275 unsigned mask = mul - 1;
276
277 return ((pitch_bits + mask) & ~mask) / 8;
278 }
279
280 static GLboolean
281 swrast_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 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_R8G8B8:
355 xrb->Base.Format = MESA_FORMAT_RGB888;
356 xrb->Base.InternalFormat = GL_RGB;
357 xrb->Base._BaseFormat = GL_RGB;
358 xrb->Base.DataType = GL_UNSIGNED_BYTE;
359 xrb->bpp = 24;
360 break;
361 case PF_R5G6B5:
362 xrb->Base.Format = MESA_FORMAT_RGB565;
363 xrb->Base.InternalFormat = GL_RGB;
364 xrb->Base._BaseFormat = GL_RGB;
365 xrb->Base.DataType = GL_UNSIGNED_BYTE;
366 xrb->bpp = 16;
367 break;
368 case PF_R3G3B2:
369 xrb->Base.Format = MESA_FORMAT_RGB332;
370 xrb->Base.InternalFormat = GL_RGB;
371 xrb->Base._BaseFormat = GL_RGB;
372 xrb->Base.DataType = GL_UNSIGNED_BYTE;
373 xrb->bpp = 8;
374 break;
375 default:
376 return NULL;
377 }
378
379 return xrb;
380 }
381
382 static GLboolean
383 dri_create_buffer(__DRIscreen * sPriv,
384 __DRIdrawable * dPriv,
385 const struct gl_config * visual, GLboolean isPixmap)
386 {
387 struct dri_drawable *drawable = NULL;
388 struct gl_framebuffer *fb;
389 struct swrast_renderbuffer *frontrb, *backrb;
390
391 TRACE;
392
393 drawable = CALLOC_STRUCT(dri_drawable);
394 if (drawable == NULL)
395 goto drawable_fail;
396
397 dPriv->driverPrivate = drawable;
398 drawable->dPriv = dPriv;
399
400 drawable->row = malloc(MAX_WIDTH * 4);
401 if (drawable->row == NULL)
402 goto drawable_fail;
403
404 fb = &drawable->Base;
405
406 /* basic framebuffer setup */
407 _mesa_initialize_window_framebuffer(fb, visual);
408
409 /* add front renderbuffer */
410 frontrb = swrast_new_renderbuffer(visual, GL_TRUE);
411 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontrb->Base);
412
413 /* add back renderbuffer */
414 if (visual->doubleBufferMode) {
415 backrb = swrast_new_renderbuffer(visual, GL_FALSE);
416 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backrb->Base);
417 }
418
419 /* add software renderbuffers */
420 _mesa_add_soft_renderbuffers(fb,
421 GL_FALSE, /* color */
422 visual->haveDepthBuffer,
423 visual->haveStencilBuffer,
424 visual->haveAccumBuffer,
425 GL_FALSE, /* alpha */
426 GL_FALSE /* aux bufs */);
427
428 return GL_TRUE;
429
430 drawable_fail:
431
432 if (drawable)
433 free(drawable->row);
434
435 FREE(drawable);
436
437 return GL_FALSE;
438 }
439
440 static void
441 dri_destroy_buffer(__DRIdrawable * dPriv)
442 {
443 TRACE;
444
445 if (dPriv) {
446 struct dri_drawable *drawable = dri_drawable(dPriv);
447 struct gl_framebuffer *fb;
448
449 free(drawable->row);
450
451 fb = &drawable->Base;
452
453 fb->DeletePending = GL_TRUE;
454 _mesa_reference_framebuffer(&fb, NULL);
455 }
456 }
457
458 static void
459 dri_swap_buffers(__DRIdrawable * dPriv)
460 {
461 __DRIscreen *sPriv = dPriv->driScreenPriv;
462
463 GET_CURRENT_CONTEXT(ctx);
464
465 struct dri_drawable *drawable = dri_drawable(dPriv);
466 struct gl_framebuffer *fb;
467 struct swrast_renderbuffer *frontrb, *backrb;
468
469 TRACE;
470
471 fb = &drawable->Base;
472
473 frontrb =
474 swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
475 backrb =
476 swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
477
478 /* check for signle-buffered */
479 if (backrb == NULL)
480 return;
481
482 /* check if swapping currently bound buffer */
483 if (ctx && ctx->DrawBuffer == fb) {
484 /* flush pending rendering */
485 _mesa_notifySwapBuffers(ctx);
486 }
487
488 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
489 0, 0,
490 frontrb->Base.Width,
491 frontrb->Base.Height,
492 backrb->Base.Data,
493 dPriv->loaderPrivate);
494 }
495
496
497 /**
498 * General device driver functions.
499 */
500
501 static void
502 get_window_size( struct gl_framebuffer *fb, GLsizei *w, GLsizei *h )
503 {
504 __DRIdrawable *dPriv = swrast_drawable(fb)->dPriv;
505 __DRIscreen *sPriv = dPriv->driScreenPriv;
506 int x, y;
507
508 sPriv->swrast_loader->getDrawableInfo(dPriv,
509 &x, &y, w, h,
510 dPriv->loaderPrivate);
511 }
512
513 static void
514 swrast_check_and_update_window_size( struct gl_context *ctx, struct gl_framebuffer *fb )
515 {
516 GLsizei width, height;
517
518 get_window_size(fb, &width, &height);
519 if (fb->Width != width || fb->Height != height) {
520 _mesa_resize_framebuffer(ctx, fb, width, height);
521 }
522 }
523
524 static const GLubyte *
525 get_string(struct gl_context *ctx, GLenum pname)
526 {
527 (void) ctx;
528 switch (pname) {
529 case GL_VENDOR:
530 return (const GLubyte *) "Mesa Project";
531 case GL_RENDERER:
532 return (const GLubyte *) "Software Rasterizer";
533 default:
534 return NULL;
535 }
536 }
537
538 static void
539 update_state( struct gl_context *ctx, GLuint new_state )
540 {
541 /* not much to do here - pass it on */
542 _swrast_InvalidateState( ctx, new_state );
543 _swsetup_InvalidateState( ctx, new_state );
544 _vbo_InvalidateState( ctx, new_state );
545 _tnl_InvalidateState( ctx, new_state );
546 }
547
548 static void
549 viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
550 {
551 struct gl_framebuffer *draw = ctx->WinSysDrawBuffer;
552 struct gl_framebuffer *read = ctx->WinSysReadBuffer;
553
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_multisample",
582 "GL_ARB_texture_compression",
583 "GL_ARB_transpose_matrix",
584 "GL_ARB_vertex_buffer_object",
585 "GL_ARB_window_pos",
586 "GL_EXT_blend_func_separate",
587 "GL_EXT_compiled_vertex_array",
588 "GL_EXT_framebuffer_blit",
589 "GL_EXT_multi_draw_arrays",
590 "GL_EXT_polygon_offset",
591 "GL_EXT_texture_object",
592 "GL_EXT_vertex_array",
593 "GL_IBM_multimode_draw_arrays",
594 "GL_MESA_window_pos",
595 "GL_NV_vertex_program",
596
597 /* Required by GLES2 */
598 "GL_ARB_fragment_program",
599 "GL_ARB_fragment_shader",
600 "GL_ARB_multitexture",
601 "GL_ARB_shader_objects",
602 "GL_ARB_texture_cube_map",
603 "GL_ARB_texture_mirrored_repeat",
604 "GL_ARB_texture_non_power_of_two",
605 "GL_ARB_vertex_shader",
606 "GL_EXT_blend_color",
607 "GL_EXT_blend_equation_separate",
608 "GL_EXT_blend_minmax",
609 "GL_EXT_blend_subtract",
610 "GL_EXT_stencil_wrap",
611
612 /* Optional GLES2 */
613 "GL_ARB_framebuffer_object",
614 "GL_EXT_texture_filter_anisotropic",
615 "GL_ARB_depth_texture",
616 "GL_EXT_packed_depth_stencil",
617 "GL_EXT_framebuffer_object",
618 NULL,
619 };
620
621 static void
622 InitExtensionsES2(struct gl_context *ctx)
623 {
624 int i;
625
626 /* Can't use driInitExtensions() since it uses extensions from
627 * main/remap_helper.h when called the first time. */
628
629 for (i = 0; es2_extensions[i]; i++)
630 _mesa_enable_extension(ctx, es2_extensions[i]);
631 }
632
633 /**
634 * Context-related functions.
635 */
636
637 static GLboolean
638 dri_create_context(gl_api api,
639 const struct gl_config * visual,
640 __DRIcontext * cPriv, void *sharedContextPrivate)
641 {
642 struct dri_context *ctx = NULL;
643 struct dri_context *share = (struct dri_context *)sharedContextPrivate;
644 struct gl_context *mesaCtx = NULL;
645 struct gl_context *sharedCtx = NULL;
646 struct dd_function_table functions;
647
648 TRACE;
649
650 ctx = CALLOC_STRUCT(dri_context);
651 if (ctx == NULL)
652 goto context_fail;
653
654 cPriv->driverPrivate = ctx;
655 ctx->cPriv = cPriv;
656
657 /* build table of device driver functions */
658 _mesa_init_driver_functions(&functions);
659 swrast_init_driver_functions(&functions);
660
661 if (share) {
662 sharedCtx = &share->Base;
663 }
664
665 mesaCtx = &ctx->Base;
666
667 /* basic context setup */
668 if (!_mesa_initialize_context(mesaCtx, api, visual, sharedCtx, &functions, (void *) cPriv)) {
669 goto context_fail;
670 }
671
672 /* do bounds checking to prevent segfaults and server crashes! */
673 mesaCtx->Const.CheckArrayBounds = GL_TRUE;
674
675 /* create module contexts */
676 _swrast_CreateContext( mesaCtx );
677 _vbo_CreateContext( mesaCtx );
678 _tnl_CreateContext( mesaCtx );
679 _swsetup_CreateContext( mesaCtx );
680 _swsetup_Wakeup( mesaCtx );
681
682 /* use default TCL pipeline */
683 {
684 TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
685 tnl->Driver.RunPipeline = _tnl_run_pipeline;
686 }
687
688 _mesa_meta_init(mesaCtx);
689 _mesa_enable_sw_extensions(mesaCtx);
690
691 switch (api) {
692 case API_OPENGL:
693 _mesa_enable_1_3_extensions(mesaCtx);
694 _mesa_enable_1_4_extensions(mesaCtx);
695 _mesa_enable_1_5_extensions(mesaCtx);
696 _mesa_enable_2_0_extensions(mesaCtx);
697 _mesa_enable_2_1_extensions(mesaCtx);
698
699 driInitExtensions( mesaCtx, NULL, GL_FALSE );
700 break;
701 case API_OPENGLES:
702 _mesa_enable_1_3_extensions(mesaCtx);
703 _mesa_enable_1_4_extensions(mesaCtx);
704 _mesa_enable_1_5_extensions(mesaCtx);
705
706 break;
707 case API_OPENGLES2:
708 InitExtensionsES2( mesaCtx);
709 break;
710 }
711
712 return GL_TRUE;
713
714 context_fail:
715
716 FREE(ctx);
717
718 return GL_FALSE;
719 }
720
721 static void
722 dri_destroy_context(__DRIcontext * cPriv)
723 {
724 TRACE;
725
726 if (cPriv) {
727 struct dri_context *ctx = dri_context(cPriv);
728 struct gl_context *mesaCtx;
729
730 mesaCtx = &ctx->Base;
731
732 _mesa_meta_free(mesaCtx);
733 _swsetup_DestroyContext( mesaCtx );
734 _swrast_DestroyContext( mesaCtx );
735 _tnl_DestroyContext( mesaCtx );
736 _vbo_DestroyContext( mesaCtx );
737 _mesa_destroy_context( mesaCtx );
738 }
739 }
740
741 static GLboolean
742 dri_make_current(__DRIcontext * cPriv,
743 __DRIdrawable * driDrawPriv,
744 __DRIdrawable * driReadPriv)
745 {
746 struct gl_context *mesaCtx;
747 struct gl_framebuffer *mesaDraw;
748 struct gl_framebuffer *mesaRead;
749 TRACE;
750
751 if (cPriv) {
752 struct dri_context *ctx = dri_context(cPriv);
753 struct dri_drawable *draw;
754 struct dri_drawable *read;
755
756 if (!driDrawPriv || !driReadPriv)
757 return GL_FALSE;
758
759 draw = dri_drawable(driDrawPriv);
760 read = dri_drawable(driReadPriv);
761 mesaCtx = &ctx->Base;
762 mesaDraw = &draw->Base;
763 mesaRead = &read->Base;
764
765 /* check for same context and buffer */
766 if (mesaCtx == _mesa_get_current_context()
767 && mesaCtx->DrawBuffer == mesaDraw
768 && mesaCtx->ReadBuffer == mesaRead) {
769 return GL_TRUE;
770 }
771
772 _glapi_check_multithread();
773
774 swrast_check_and_update_window_size(mesaCtx, mesaDraw);
775 if (mesaRead != mesaDraw)
776 swrast_check_and_update_window_size(mesaCtx, mesaRead);
777
778 _mesa_make_current( mesaCtx,
779 mesaDraw,
780 mesaRead );
781 }
782 else {
783 /* unbind */
784 _mesa_make_current( NULL, NULL, NULL );
785 }
786
787 return GL_TRUE;
788 }
789
790 static GLboolean
791 dri_unbind_context(__DRIcontext * cPriv)
792 {
793 TRACE;
794 (void) cPriv;
795
796 /* Unset current context and dispath table */
797 _mesa_make_current(NULL, NULL, NULL);
798
799 return GL_TRUE;
800 }
801
802
803 const struct __DriverAPIRec driDriverAPI = {
804 .InitScreen = dri_init_screen,
805 .DestroyScreen = dri_destroy_screen,
806 .CreateContext = dri_create_context,
807 .DestroyContext = dri_destroy_context,
808 .CreateBuffer = dri_create_buffer,
809 .DestroyBuffer = dri_destroy_buffer,
810 .SwapBuffers = dri_swap_buffers,
811 .MakeCurrent = dri_make_current,
812 .UnbindContext = dri_unbind_context,
813 };
814
815 /* This is the table of extensions that the loader will dlsym() for. */
816 PUBLIC const __DRIextension *__driDriverExtensions[] = {
817 &driCoreExtension.base,
818 &driSWRastExtension.base,
819 NULL
820 };