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