f4947daa063874f0df53b757cdb354106a803132
[mesa.git] / src / mesa / drivers / dri / swrast / swrast.c
1 /*
2 * Copyright (C) 2008 George Sapountzis <gsap7@yahoo.gr>
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/framebuffer.h"
37 #include "main/imports.h"
38 #include "main/renderbuffer.h"
39 #include "swrast/swrast.h"
40 #include "swrast_setup/swrast_setup.h"
41 #include "tnl/tnl.h"
42 #include "tnl/t_context.h"
43 #include "tnl/t_pipeline.h"
44 #include "vbo/vbo.h"
45 #include "drivers/common/driverfuncs.h"
46 #include "drivers/common/meta.h"
47 #include "utils.h"
48
49 #include "swrast_priv.h"
50
51
52 #define need_GL_VERSION_1_3
53 #define need_GL_VERSION_1_4
54 #define need_GL_VERSION_1_5
55 #define need_GL_VERSION_2_0
56 #define need_GL_VERSION_2_1
57
58 /* sw extensions for imaging */
59 #define need_GL_EXT_blend_color
60 #define need_GL_EXT_blend_minmax
61 #define need_GL_EXT_convolution
62 #define need_GL_EXT_histogram
63 #define need_GL_SGI_color_table
64
65 /* sw extensions not associated with some GL version */
66 #define need_GL_ARB_draw_elements_base_vertex
67 #define need_GL_ARB_shader_objects
68 #define need_GL_ARB_vertex_array_object
69 #define need_GL_ARB_vertex_program
70 #define need_GL_ARB_sync
71 #define need_GL_APPLE_vertex_array_object
72 #define need_GL_ATI_fragment_shader
73 #define need_GL_ATI_separate_stencil
74 #define need_GL_EXT_depth_bounds_test
75 #define need_GL_EXT_framebuffer_object
76 #define need_GL_EXT_framebuffer_blit
77 #define need_GL_EXT_gpu_program_parameters
78 #define need_GL_EXT_paletted_texture
79 #define need_GL_EXT_stencil_two_side
80 #define need_GL_MESA_resize_buffers
81 #define need_GL_NV_vertex_program
82 #define need_GL_NV_fragment_program
83
84 #include "extension_helper.h"
85
86 const struct dri_extension card_extensions[] =
87 {
88 { "GL_VERSION_1_3", GL_VERSION_1_3_functions },
89 { "GL_VERSION_1_4", GL_VERSION_1_4_functions },
90 { "GL_VERSION_1_5", GL_VERSION_1_5_functions },
91 { "GL_VERSION_2_0", GL_VERSION_2_0_functions },
92 { "GL_VERSION_2_1", GL_VERSION_2_1_functions },
93
94 { "GL_EXT_blend_color", GL_EXT_blend_color_functions },
95 { "GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions },
96 { "GL_EXT_convolution", GL_EXT_convolution_functions },
97 { "GL_EXT_histogram", GL_EXT_histogram_functions },
98 { "GL_SGI_color_table", GL_SGI_color_table_functions },
99
100 { "GL_ARB_depth_clamp", NULL },
101 { "GL_ARB_draw_elements_base_vertex", GL_ARB_draw_elements_base_vertex_functions },
102 { "GL_ARB_shader_objects", GL_ARB_shader_objects_functions },
103 { "GL_ARB_vertex_array_object", GL_ARB_vertex_array_object_functions },
104 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions },
105 { "GL_ARB_sync", GL_ARB_sync_functions },
106 { "GL_APPLE_vertex_array_object", GL_APPLE_vertex_array_object_functions },
107 { "GL_ATI_fragment_shader", GL_ATI_fragment_shader_functions },
108 { "GL_ATI_separate_stencil", GL_ATI_separate_stencil_functions },
109 { "GL_EXT_depth_bounds_test", GL_EXT_depth_bounds_test_functions },
110 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions },
111 { "GL_EXT_framebuffer_blit", GL_EXT_framebuffer_blit_functions },
112 { "GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions },
113 { "GL_EXT_paletted_texture", GL_EXT_paletted_texture_functions },
114 { "GL_EXT_stencil_two_side", GL_EXT_stencil_two_side_functions },
115 { "GL_MESA_resize_buffers", GL_MESA_resize_buffers_functions },
116 { "GL_NV_depth_clamp", NULL },
117 { "GL_NV_vertex_program", GL_NV_vertex_program_functions },
118 { "GL_NV_fragment_program", GL_NV_fragment_program_functions },
119 { "GL_NV_fragment_program_option", NULL },
120 { NULL, NULL }
121 };
122
123
124 /**
125 * Screen and config-related functions
126 */
127
128 static void
129 setupLoaderExtensions(__DRIscreen *psp,
130 const __DRIextension **extensions)
131 {
132 int i;
133
134 for (i = 0; extensions[i]; i++) {
135 if (strcmp(extensions[i]->name, __DRI_SWRAST_LOADER) == 0)
136 psp->swrast_loader = (__DRIswrastLoaderExtension *) extensions[i];
137 }
138 }
139
140 static __DRIconfig **
141 swrastFillInModes(__DRIscreen *psp,
142 unsigned pixel_bits, unsigned depth_bits,
143 unsigned stencil_bits, GLboolean have_back_buffer)
144 {
145 __DRIconfig **configs;
146 unsigned depth_buffer_factor;
147 unsigned back_buffer_factor;
148 GLenum fb_format;
149 GLenum fb_type;
150
151 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
152 * support pageflipping at all.
153 */
154 static const GLenum back_buffer_modes[] = {
155 GLX_NONE, GLX_SWAP_UNDEFINED_OML
156 };
157
158 uint8_t depth_bits_array[4];
159 uint8_t stencil_bits_array[4];
160 uint8_t msaa_samples_array[1];
161
162 depth_bits_array[0] = 0;
163 depth_bits_array[1] = 0;
164 depth_bits_array[2] = depth_bits;
165 depth_bits_array[3] = depth_bits;
166
167 /* Just like with the accumulation buffer, always provide some modes
168 * with a stencil buffer.
169 */
170 stencil_bits_array[0] = 0;
171 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
172 stencil_bits_array[2] = 0;
173 stencil_bits_array[3] = (stencil_bits == 0) ? 8 : stencil_bits;
174
175 msaa_samples_array[0] = 0;
176
177 depth_buffer_factor = 4;
178 back_buffer_factor = 2;
179
180 switch (pixel_bits) {
181 case 8:
182 fb_format = GL_RGB;
183 fb_type = GL_UNSIGNED_BYTE_2_3_3_REV;
184 break;
185 case 16:
186 fb_format = GL_RGB;
187 fb_type = GL_UNSIGNED_SHORT_5_6_5;
188 break;
189 case 24:
190 fb_format = GL_BGR;
191 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
192 break;
193 case 32:
194 fb_format = GL_BGRA;
195 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
196 break;
197 default:
198 fprintf(stderr, "[%s:%u] bad depth %d\n", __func__, __LINE__,
199 pixel_bits);
200 return NULL;
201 }
202
203 configs = driCreateConfigs(fb_format, fb_type,
204 depth_bits_array, stencil_bits_array,
205 depth_buffer_factor, back_buffer_modes,
206 back_buffer_factor, msaa_samples_array, 1);
207 if (configs == NULL) {
208 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
209 __LINE__);
210 return NULL;
211 }
212
213 return configs;
214 }
215
216 static __DRIscreen *
217 driCreateNewScreen(int scrn, const __DRIextension **extensions,
218 const __DRIconfig ***driver_configs, void *data)
219 {
220 static const __DRIextension *emptyExtensionList[] = { NULL };
221 __DRIscreen *psp;
222 __DRIconfig **configs8, **configs16, **configs24, **configs32;
223
224 (void) data;
225
226 TRACE;
227
228 psp = _mesa_calloc(sizeof(*psp));
229 if (!psp)
230 return NULL;
231
232 setupLoaderExtensions(psp, extensions);
233
234 psp->num = scrn;
235 psp->extensions = emptyExtensionList;
236
237 configs8 = swrastFillInModes(psp, 8, 8, 0, 1);
238 configs16 = swrastFillInModes(psp, 16, 16, 0, 1);
239 configs24 = swrastFillInModes(psp, 24, 24, 8, 1);
240 configs32 = swrastFillInModes(psp, 32, 24, 8, 1);
241
242 configs16 = driConcatConfigs(configs8, configs16);
243 configs24 = driConcatConfigs(configs16, configs24);
244 *driver_configs = (const __DRIconfig **)
245 driConcatConfigs(configs24, configs32);
246
247 driInitExtensions( NULL, card_extensions, GL_FALSE );
248
249 return psp;
250 }
251
252 static void driDestroyScreen(__DRIscreen *psp)
253 {
254 TRACE;
255
256 if (psp) {
257 _mesa_free(psp);
258 }
259 }
260
261 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
262 {
263 TRACE;
264
265 return psp->extensions;
266 }
267
268
269 /**
270 * Framebuffer and renderbuffer-related functions.
271 */
272
273 static GLuint
274 choose_pixel_format(const GLvisual *v)
275 {
276 if (v->rgbMode) {
277 int depth = v->rgbBits;
278
279 if (depth == 32
280 && v->redMask == 0xff0000
281 && v->greenMask == 0x00ff00
282 && v->blueMask == 0x0000ff)
283 return PF_A8R8G8B8;
284 else if (depth == 24
285 && v->redMask == 0xff0000
286 && v->greenMask == 0x00ff00
287 && v->blueMask == 0x0000ff)
288 return PF_X8R8G8B8;
289 else if (depth == 16
290 && v->redMask == 0xf800
291 && v->greenMask == 0x07e0
292 && v->blueMask == 0x001f)
293 return PF_R5G6B5;
294 else if (depth == 8
295 && v->redMask == 0x07
296 && v->greenMask == 0x38
297 && v->blueMask == 0xc0)
298 return PF_R3G3B2;
299 }
300 else {
301 if (v->indexBits == 8)
302 return PF_CI8;
303 }
304
305 _mesa_problem( NULL, "unexpected format in %s", __FUNCTION__ );
306 return 0;
307 }
308
309 static void
310 swrast_delete_renderbuffer(struct gl_renderbuffer *rb)
311 {
312 TRACE;
313
314 _mesa_free(rb->Data);
315 _mesa_free(rb);
316 }
317
318 static GLboolean
319 swrast_alloc_front_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
320 GLenum internalFormat, GLuint width, GLuint height)
321 {
322 struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
323 unsigned mask = PITCH_ALIGN_BITS - 1;
324
325 TRACE;
326
327 rb->Data = NULL;
328 rb->Width = width;
329 rb->Height = height;
330
331 /* always pad to PITCH_ALIGN_BITS */
332 xrb->pitch = ((width * xrb->bpp + mask) & ~mask) / 8;
333
334 return GL_TRUE;
335 }
336
337 static GLboolean
338 swrast_alloc_back_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
339 GLenum internalFormat, GLuint width, GLuint height)
340 {
341 struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
342
343 TRACE;
344
345 _mesa_free(rb->Data);
346
347 swrast_alloc_front_storage(ctx, rb, internalFormat, width, height);
348
349 rb->Data = _mesa_malloc(height * xrb->pitch);
350
351 return GL_TRUE;
352 }
353
354 static struct swrast_renderbuffer *
355 swrast_new_renderbuffer(const GLvisual *visual, GLboolean front)
356 {
357 struct swrast_renderbuffer *xrb = _mesa_calloc(sizeof *xrb);
358 GLuint pixel_format;
359
360 TRACE;
361
362 if (!xrb)
363 return NULL;
364
365 _mesa_init_renderbuffer(&xrb->Base, 0);
366
367 pixel_format = choose_pixel_format(visual);
368
369 xrb->Base.Delete = swrast_delete_renderbuffer;
370 if (front) {
371 xrb->Base.AllocStorage = swrast_alloc_front_storage;
372 swrast_set_span_funcs_front(xrb, pixel_format);
373 }
374 else {
375 xrb->Base.AllocStorage = swrast_alloc_back_storage;
376 swrast_set_span_funcs_back(xrb, pixel_format);
377 }
378
379 switch (pixel_format) {
380 case PF_A8R8G8B8:
381 xrb->Base.InternalFormat = GL_RGBA;
382 xrb->Base._BaseFormat = GL_RGBA;
383 xrb->Base.DataType = GL_UNSIGNED_BYTE;
384 xrb->Base.RedBits = 8 * sizeof(GLubyte);
385 xrb->Base.GreenBits = 8 * sizeof(GLubyte);
386 xrb->Base.BlueBits = 8 * sizeof(GLubyte);
387 xrb->Base.AlphaBits = 8 * sizeof(GLubyte);
388 xrb->bpp = 32;
389 break;
390 case PF_X8R8G8B8:
391 xrb->Base.InternalFormat = GL_RGB;
392 xrb->Base._BaseFormat = GL_RGB;
393 xrb->Base.DataType = GL_UNSIGNED_BYTE;
394 xrb->Base.RedBits = 8 * sizeof(GLubyte);
395 xrb->Base.GreenBits = 8 * sizeof(GLubyte);
396 xrb->Base.BlueBits = 8 * sizeof(GLubyte);
397 xrb->Base.AlphaBits = 0;
398 xrb->bpp = 32;
399 break;
400 case PF_R5G6B5:
401 xrb->Base.InternalFormat = GL_RGB;
402 xrb->Base._BaseFormat = GL_RGB;
403 xrb->Base.DataType = GL_UNSIGNED_BYTE;
404 xrb->Base.RedBits = 5 * sizeof(GLubyte);
405 xrb->Base.GreenBits = 6 * sizeof(GLubyte);
406 xrb->Base.BlueBits = 5 * sizeof(GLubyte);
407 xrb->Base.AlphaBits = 0;
408 xrb->bpp = 16;
409 break;
410 case PF_R3G3B2:
411 xrb->Base.InternalFormat = GL_RGB;
412 xrb->Base._BaseFormat = GL_RGB;
413 xrb->Base.DataType = GL_UNSIGNED_BYTE;
414 xrb->Base.RedBits = 3 * sizeof(GLubyte);
415 xrb->Base.GreenBits = 3 * sizeof(GLubyte);
416 xrb->Base.BlueBits = 2 * sizeof(GLubyte);
417 xrb->Base.AlphaBits = 0;
418 xrb->bpp = 8;
419 break;
420 case PF_CI8:
421 xrb->Base.InternalFormat = GL_COLOR_INDEX8_EXT;
422 xrb->Base._BaseFormat = GL_COLOR_INDEX;
423 xrb->Base.DataType = GL_UNSIGNED_BYTE;
424 xrb->Base.IndexBits = 8 * sizeof(GLubyte);
425 xrb->bpp = 8;
426 break;
427 default:
428 return NULL;
429 }
430
431 return xrb;
432 }
433
434 static __DRIdrawable *
435 driCreateNewDrawable(__DRIscreen *screen,
436 const __DRIconfig *config, void *data)
437 {
438 __DRIdrawable *buf;
439 struct swrast_renderbuffer *frontrb, *backrb;
440
441 TRACE;
442
443 buf = _mesa_calloc(sizeof *buf);
444 if (!buf)
445 return NULL;
446
447 buf->loaderPrivate = data;
448
449 buf->driScreenPriv = screen;
450
451 buf->row = _mesa_malloc(MAX_WIDTH * 4);
452
453 /* basic framebuffer setup */
454 _mesa_initialize_framebuffer(&buf->Base, &config->modes);
455
456 /* add front renderbuffer */
457 frontrb = swrast_new_renderbuffer(&config->modes, GL_TRUE);
458 _mesa_add_renderbuffer(&buf->Base, BUFFER_FRONT_LEFT, &frontrb->Base);
459
460 /* add back renderbuffer */
461 if (config->modes.doubleBufferMode) {
462 backrb = swrast_new_renderbuffer(&config->modes, GL_FALSE);
463 _mesa_add_renderbuffer(&buf->Base, BUFFER_BACK_LEFT, &backrb->Base);
464 }
465
466 /* add software renderbuffers */
467 _mesa_add_soft_renderbuffers(&buf->Base,
468 GL_FALSE, /* color */
469 config->modes.haveDepthBuffer,
470 config->modes.haveStencilBuffer,
471 config->modes.haveAccumBuffer,
472 GL_FALSE, /* alpha */
473 GL_FALSE /* aux bufs */);
474
475 return buf;
476 }
477
478 static void
479 driDestroyDrawable(__DRIdrawable *buf)
480 {
481 TRACE;
482
483 if (buf) {
484 struct gl_framebuffer *fb = &buf->Base;
485
486 _mesa_free(buf->row);
487
488 fb->DeletePending = GL_TRUE;
489 _mesa_reference_framebuffer(&fb, NULL);
490 }
491 }
492
493 static void driSwapBuffers(__DRIdrawable *buf)
494 {
495 GET_CURRENT_CONTEXT(ctx);
496
497 struct swrast_renderbuffer *frontrb =
498 swrast_renderbuffer(buf->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
499 struct swrast_renderbuffer *backrb =
500 swrast_renderbuffer(buf->Base.Attachment[BUFFER_BACK_LEFT].Renderbuffer);
501
502 __DRIscreen *screen = buf->driScreenPriv;
503
504 TRACE;
505
506 /* check for signle-buffered */
507 if (backrb == NULL)
508 return;
509
510 /* check if swapping currently bound buffer */
511 if (ctx && ctx->DrawBuffer == &(buf->Base)) {
512 /* flush pending rendering */
513 _mesa_notifySwapBuffers(ctx);
514 }
515
516 screen->swrast_loader->putImage(buf, __DRI_SWRAST_IMAGE_OP_SWAP,
517 0, 0,
518 frontrb->Base.Width,
519 frontrb->Base.Height,
520 backrb->Base.Data,
521 buf->loaderPrivate);
522 }
523
524
525 /**
526 * General device driver functions.
527 */
528
529 static void
530 get_window_size( GLframebuffer *fb, GLsizei *w, GLsizei *h )
531 {
532 __DRIdrawable *buf = swrast_drawable(fb);
533 __DRIscreen *screen = buf->driScreenPriv;
534 int x, y;
535
536 screen->swrast_loader->getDrawableInfo(buf,
537 &x, &y, w, h,
538 buf->loaderPrivate);
539 }
540
541 static void
542 swrast_check_and_update_window_size( GLcontext *ctx, GLframebuffer *fb )
543 {
544 GLsizei width, height;
545
546 get_window_size(fb, &width, &height);
547 if (fb->Width != width || fb->Height != height) {
548 _mesa_resize_framebuffer(ctx, fb, width, height);
549 }
550 }
551
552 static const GLubyte *
553 get_string(GLcontext *ctx, GLenum pname)
554 {
555 (void) ctx;
556 switch (pname) {
557 case GL_VENDOR:
558 return (const GLubyte *) "Mesa Project";
559 case GL_RENDERER:
560 return (const GLubyte *) "Software Rasterizer";
561 default:
562 return NULL;
563 }
564 }
565
566 static void
567 update_state( GLcontext *ctx, GLuint new_state )
568 {
569 /* not much to do here - pass it on */
570 _swrast_InvalidateState( ctx, new_state );
571 _swsetup_InvalidateState( ctx, new_state );
572 _vbo_InvalidateState( ctx, new_state );
573 _tnl_InvalidateState( ctx, new_state );
574 }
575
576 static void
577 viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
578 {
579 GLframebuffer *draw = ctx->WinSysDrawBuffer;
580 GLframebuffer *read = ctx->WinSysReadBuffer;
581
582 swrast_check_and_update_window_size(ctx, draw);
583 swrast_check_and_update_window_size(ctx, read);
584 }
585
586 static void
587 swrast_init_driver_functions(struct dd_function_table *driver)
588 {
589 driver->GetString = get_string;
590 driver->UpdateState = update_state;
591 driver->GetBufferSize = NULL;
592 driver->Viewport = viewport;
593 }
594
595
596 /**
597 * Context-related functions.
598 */
599
600 static __DRIcontext *
601 driCreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
602 __DRIcontext *shared, void *data)
603 {
604 __DRIcontext *ctx;
605 GLcontext *mesaCtx;
606 struct dd_function_table functions;
607
608 TRACE;
609
610 ctx = _mesa_calloc(sizeof *ctx);
611 if (!ctx)
612 return NULL;
613
614 ctx->loaderPrivate = data;
615
616 ctx->driScreenPriv = screen;
617
618 /* build table of device driver functions */
619 _mesa_init_driver_functions(&functions);
620 swrast_init_driver_functions(&functions);
621
622 if (!_mesa_initialize_context(&ctx->Base, &config->modes,
623 shared ? &shared->Base : NULL,
624 &functions, (void *) ctx)) {
625 _mesa_free(ctx);
626 return NULL;
627 }
628
629 mesaCtx = &ctx->Base;
630
631 /* do bounds checking to prevent segfaults and server crashes! */
632 mesaCtx->Const.CheckArrayBounds = GL_TRUE;
633
634 /* create module contexts */
635 _swrast_CreateContext( mesaCtx );
636 _vbo_CreateContext( mesaCtx );
637 _tnl_CreateContext( mesaCtx );
638 _swsetup_CreateContext( mesaCtx );
639 _swsetup_Wakeup( mesaCtx );
640
641 /* use default TCL pipeline */
642 {
643 TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
644 tnl->Driver.RunPipeline = _tnl_run_pipeline;
645 }
646
647 _mesa_enable_sw_extensions(mesaCtx);
648 _mesa_enable_1_3_extensions(mesaCtx);
649 _mesa_enable_1_4_extensions(mesaCtx);
650 _mesa_enable_1_5_extensions(mesaCtx);
651 _mesa_enable_2_0_extensions(mesaCtx);
652 _mesa_enable_2_1_extensions(mesaCtx);
653
654 _mesa_meta_init(mesaCtx);
655
656 return ctx;
657 }
658
659 static void
660 driDestroyContext(__DRIcontext *ctx)
661 {
662 GLcontext *mesaCtx;
663 TRACE;
664
665 if (ctx) {
666 mesaCtx = &ctx->Base;
667 _mesa_meta_free(mesaCtx);
668 _swsetup_DestroyContext( mesaCtx );
669 _swrast_DestroyContext( mesaCtx );
670 _tnl_DestroyContext( mesaCtx );
671 _vbo_DestroyContext( mesaCtx );
672 _mesa_destroy_context( mesaCtx );
673 }
674 }
675
676 static int
677 driCopyContext(__DRIcontext *dst, __DRIcontext *src, unsigned long mask)
678 {
679 TRACE;
680
681 _mesa_copy_context(&src->Base, &dst->Base, mask);
682 return GL_TRUE;
683 }
684
685 static int driBindContext(__DRIcontext *ctx,
686 __DRIdrawable *draw,
687 __DRIdrawable *read)
688 {
689 GLcontext *mesaCtx;
690 GLframebuffer *mesaDraw;
691 GLframebuffer *mesaRead;
692 TRACE;
693
694 if (ctx) {
695 if (!draw || !read)
696 return GL_FALSE;
697
698 mesaCtx = &ctx->Base;
699 mesaDraw = &draw->Base;
700 mesaRead = &read->Base;
701
702 /* check for same context and buffer */
703 if (mesaCtx == _mesa_get_current_context()
704 && mesaCtx->DrawBuffer == mesaDraw
705 && mesaCtx->ReadBuffer == mesaRead) {
706 return GL_TRUE;
707 }
708
709 _glapi_check_multithread();
710
711 swrast_check_and_update_window_size(mesaCtx, mesaDraw);
712 if (read != draw)
713 swrast_check_and_update_window_size(mesaCtx, mesaRead);
714
715 _mesa_make_current( mesaCtx,
716 mesaDraw,
717 mesaRead );
718 }
719 else {
720 /* unbind */
721 _mesa_make_current( NULL, NULL, NULL );
722 }
723
724 return GL_TRUE;
725 }
726
727 static int driUnbindContext(__DRIcontext *ctx)
728 {
729 TRACE;
730 (void) ctx;
731 return GL_TRUE;
732 }
733
734
735 static const __DRIcoreExtension driCoreExtension = {
736 { __DRI_CORE, __DRI_CORE_VERSION },
737 NULL, /* driCreateNewScreen */
738 driDestroyScreen,
739 driGetExtensions,
740 driGetConfigAttrib,
741 driIndexConfigAttrib,
742 NULL, /* driCreateNewDrawable */
743 driDestroyDrawable,
744 driSwapBuffers,
745 driCreateNewContext,
746 driCopyContext,
747 driDestroyContext,
748 driBindContext,
749 driUnbindContext
750 };
751
752 static const __DRIswrastExtension driSWRastExtension = {
753 { __DRI_SWRAST, __DRI_SWRAST_VERSION },
754 driCreateNewScreen,
755 driCreateNewDrawable
756 };
757
758 /* This is the table of extensions that the loader will dlsym() for. */
759 PUBLIC const __DRIextension *__driDriverExtensions[] = {
760 &driCoreExtension.base,
761 &driSWRastExtension.base,
762 NULL
763 };