mesa: Make the drivers call a non-code-generated dispatch table setup.
[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 return NULL;
367 }
368
369 return xrb;
370 }
371
372 static void
373 swrast_map_renderbuffer(struct gl_context *ctx,
374 struct gl_renderbuffer *rb,
375 GLuint x, GLuint y, GLuint w, GLuint h,
376 GLbitfield mode,
377 GLubyte **out_map,
378 GLint *out_stride)
379 {
380 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
381 GLubyte *map = xrb->Base.Buffer;
382 int cpp = _mesa_get_format_bytes(rb->Format);
383 int stride = rb->Width * cpp;
384
385 if (rb->AllocStorage == swrast_alloc_front_storage) {
386 __DRIdrawable *dPriv = xrb->dPriv;
387 __DRIscreen *sPriv = dPriv->driScreenPriv;
388
389 xrb->map_mode = mode;
390 xrb->map_x = x;
391 xrb->map_y = y;
392 xrb->map_w = w;
393 xrb->map_h = h;
394
395 stride = w * cpp;
396 xrb->Base.Buffer = malloc(h * stride);
397
398 sPriv->swrast_loader->getImage(dPriv, x, y, w, h,
399 (char *) xrb->Base.Buffer,
400 dPriv->loaderPrivate);
401
402 *out_map = xrb->Base.Buffer;
403 *out_stride = stride;
404 return;
405 }
406
407 ASSERT(xrb->Base.Buffer);
408
409 if (rb->AllocStorage == swrast_alloc_back_storage) {
410 map += (rb->Height - 1) * stride;
411 stride = -stride;
412 }
413
414 map += (GLsizei)y * stride;
415 map += (GLsizei)x * cpp;
416
417 *out_map = map;
418 *out_stride = stride;
419 }
420
421 static void
422 swrast_unmap_renderbuffer(struct gl_context *ctx,
423 struct gl_renderbuffer *rb)
424 {
425 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
426
427 if (rb->AllocStorage == swrast_alloc_front_storage) {
428 __DRIdrawable *dPriv = xrb->dPriv;
429 __DRIscreen *sPriv = dPriv->driScreenPriv;
430
431 if (xrb->map_mode & GL_MAP_WRITE_BIT) {
432 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_DRAW,
433 xrb->map_x, xrb->map_y,
434 xrb->map_w, xrb->map_h,
435 (char *) xrb->Base.Buffer,
436 dPriv->loaderPrivate);
437 }
438
439 free(xrb->Base.Buffer);
440 xrb->Base.Buffer = NULL;
441 }
442 }
443
444 static GLboolean
445 dri_create_buffer(__DRIscreen * sPriv,
446 __DRIdrawable * dPriv,
447 const struct gl_config * visual, GLboolean isPixmap)
448 {
449 struct dri_drawable *drawable = NULL;
450 struct gl_framebuffer *fb;
451 struct dri_swrast_renderbuffer *frontrb, *backrb;
452
453 TRACE;
454
455 (void) sPriv;
456 (void) isPixmap;
457
458 drawable = CALLOC_STRUCT(dri_drawable);
459 if (drawable == NULL)
460 goto drawable_fail;
461
462 dPriv->driverPrivate = drawable;
463 drawable->dPriv = dPriv;
464
465 drawable->row = malloc(SWRAST_MAX_WIDTH * 4);
466 if (drawable->row == NULL)
467 goto drawable_fail;
468
469 fb = &drawable->Base;
470
471 /* basic framebuffer setup */
472 _mesa_initialize_window_framebuffer(fb, visual);
473
474 /* add front renderbuffer */
475 frontrb = swrast_new_renderbuffer(visual, dPriv, GL_TRUE);
476 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontrb->Base.Base);
477
478 /* add back renderbuffer */
479 if (visual->doubleBufferMode) {
480 backrb = swrast_new_renderbuffer(visual, dPriv, GL_FALSE);
481 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backrb->Base.Base);
482 }
483
484 /* add software renderbuffers */
485 _swrast_add_soft_renderbuffers(fb,
486 GL_FALSE, /* color */
487 visual->haveDepthBuffer,
488 visual->haveStencilBuffer,
489 visual->haveAccumBuffer,
490 GL_FALSE, /* alpha */
491 GL_FALSE /* aux bufs */);
492
493 return GL_TRUE;
494
495 drawable_fail:
496
497 if (drawable)
498 free(drawable->row);
499
500 free(drawable);
501
502 return GL_FALSE;
503 }
504
505 static void
506 dri_destroy_buffer(__DRIdrawable * dPriv)
507 {
508 TRACE;
509
510 if (dPriv) {
511 struct dri_drawable *drawable = dri_drawable(dPriv);
512 struct gl_framebuffer *fb;
513
514 free(drawable->row);
515
516 fb = &drawable->Base;
517
518 fb->DeletePending = GL_TRUE;
519 _mesa_reference_framebuffer(&fb, NULL);
520 }
521 }
522
523 static void
524 dri_swap_buffers(__DRIdrawable * dPriv)
525 {
526 __DRIscreen *sPriv = dPriv->driScreenPriv;
527
528 GET_CURRENT_CONTEXT(ctx);
529
530 struct dri_drawable *drawable = dri_drawable(dPriv);
531 struct gl_framebuffer *fb;
532 struct dri_swrast_renderbuffer *frontrb, *backrb;
533
534 TRACE;
535
536 fb = &drawable->Base;
537
538 frontrb =
539 dri_swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
540 backrb =
541 dri_swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
542
543 /* check for signle-buffered */
544 if (backrb == NULL)
545 return;
546
547 /* check if swapping currently bound buffer */
548 if (ctx && ctx->DrawBuffer == fb) {
549 /* flush pending rendering */
550 _mesa_notifySwapBuffers(ctx);
551 }
552
553 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
554 0, 0,
555 frontrb->Base.Base.Width,
556 frontrb->Base.Base.Height,
557 (char *) backrb->Base.Buffer,
558 dPriv->loaderPrivate);
559 }
560
561
562 /**
563 * General device driver functions.
564 */
565
566 static void
567 get_window_size( struct gl_framebuffer *fb, GLsizei *w, GLsizei *h )
568 {
569 __DRIdrawable *dPriv = swrast_drawable(fb)->dPriv;
570 __DRIscreen *sPriv = dPriv->driScreenPriv;
571 int x, y;
572
573 sPriv->swrast_loader->getDrawableInfo(dPriv,
574 &x, &y, w, h,
575 dPriv->loaderPrivate);
576 }
577
578 static void
579 swrast_check_and_update_window_size( struct gl_context *ctx, struct gl_framebuffer *fb )
580 {
581 GLsizei width, height;
582
583 get_window_size(fb, &width, &height);
584 if (fb->Width != width || fb->Height != height) {
585 _mesa_resize_framebuffer(ctx, fb, width, height);
586 }
587 }
588
589 static const GLubyte *
590 get_string(struct gl_context *ctx, GLenum pname)
591 {
592 (void) ctx;
593 switch (pname) {
594 case GL_VENDOR:
595 return (const GLubyte *) "Mesa Project";
596 case GL_RENDERER:
597 return (const GLubyte *) "Software Rasterizer";
598 default:
599 return NULL;
600 }
601 }
602
603 static void
604 update_state( struct gl_context *ctx, GLuint new_state )
605 {
606 /* not much to do here - pass it on */
607 _swrast_InvalidateState( ctx, new_state );
608 _swsetup_InvalidateState( ctx, new_state );
609 _vbo_InvalidateState( ctx, new_state );
610 _tnl_InvalidateState( ctx, new_state );
611 }
612
613 static void
614 viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
615 {
616 struct gl_framebuffer *draw = ctx->WinSysDrawBuffer;
617 struct gl_framebuffer *read = ctx->WinSysReadBuffer;
618
619 (void) x;
620 (void) y;
621 (void) w;
622 (void) h;
623 swrast_check_and_update_window_size(ctx, draw);
624 swrast_check_and_update_window_size(ctx, read);
625 }
626
627 static gl_format swrastChooseTextureFormat(struct gl_context * ctx,
628 GLenum target,
629 GLint internalFormat,
630 GLenum format,
631 GLenum type)
632 {
633 if (internalFormat == GL_RGB)
634 return MESA_FORMAT_XRGB8888;
635 return _mesa_choose_tex_format(ctx, target, internalFormat, format, type);
636 }
637
638 static void
639 swrast_init_driver_functions(struct dd_function_table *driver)
640 {
641 driver->GetString = get_string;
642 driver->UpdateState = update_state;
643 driver->GetBufferSize = NULL;
644 driver->Viewport = viewport;
645 driver->ChooseTextureFormat = swrastChooseTextureFormat;
646 driver->MapRenderbuffer = swrast_map_renderbuffer;
647 driver->UnmapRenderbuffer = swrast_unmap_renderbuffer;
648 }
649
650 static const char *es2_extensions[] = {
651 /* Used by mesa internally (cf all_mesa_extensions in ../common/utils.c) */
652 "GL_EXT_blend_func_separate",
653 "GL_EXT_framebuffer_blit",
654 "GL_MESA_window_pos",
655
656 /* Required by GLES2 */
657 "GL_ARB_fragment_program",
658 "GL_ARB_fragment_shader",
659 "GL_ARB_shader_objects",
660 "GL_ARB_texture_cube_map",
661 "GL_ARB_texture_non_power_of_two",
662 "GL_ARB_vertex_shader",
663 "GL_EXT_blend_color",
664 "GL_EXT_blend_equation_separate",
665 "GL_EXT_blend_minmax",
666
667 /* Optional GLES2 */
668 "GL_ARB_framebuffer_object",
669 "GL_EXT_texture_filter_anisotropic",
670 "GL_ARB_depth_texture",
671 "GL_EXT_packed_depth_stencil",
672 "GL_EXT_framebuffer_object",
673 NULL,
674 };
675
676 static void
677 InitExtensionsES2(struct gl_context *ctx)
678 {
679 int i;
680
681 for (i = 0; es2_extensions[i]; i++)
682 _mesa_enable_extension(ctx, es2_extensions[i]);
683 }
684
685 /**
686 * Context-related functions.
687 */
688
689 static GLboolean
690 dri_create_context(gl_api api,
691 const struct gl_config * visual,
692 __DRIcontext * cPriv,
693 unsigned major_version,
694 unsigned minor_version,
695 uint32_t flags,
696 unsigned *error,
697 void *sharedContextPrivate)
698 {
699 struct dri_context *ctx = NULL;
700 struct dri_context *share = (struct dri_context *)sharedContextPrivate;
701 struct gl_context *mesaCtx = NULL;
702 struct gl_context *sharedCtx = NULL;
703 struct dd_function_table functions;
704
705 TRACE;
706
707 /* Flag filtering is handled in dri2CreateContextAttribs.
708 */
709 (void) flags;
710
711 switch (api) {
712 case API_OPENGL_COMPAT:
713 if (major_version > 2
714 || (major_version == 2 && minor_version > 1)) {
715 *error = __DRI_CTX_ERROR_BAD_VERSION;
716 return GL_FALSE;
717 }
718 break;
719 case API_OPENGLES:
720 case API_OPENGLES2:
721 break;
722 case API_OPENGL_CORE:
723 *error = __DRI_CTX_ERROR_BAD_API;
724 return GL_FALSE;
725 }
726
727 ctx = CALLOC_STRUCT(dri_context);
728 if (ctx == NULL) {
729 *error = __DRI_CTX_ERROR_NO_MEMORY;
730 goto context_fail;
731 }
732
733 cPriv->driverPrivate = ctx;
734 ctx->cPriv = cPriv;
735
736 /* build table of device driver functions */
737 _mesa_init_driver_functions(&functions);
738 swrast_init_driver_functions(&functions);
739
740 if (share) {
741 sharedCtx = &share->Base;
742 }
743
744 mesaCtx = &ctx->Base;
745
746 /* basic context setup */
747 if (!_mesa_initialize_context(mesaCtx, api, visual, sharedCtx, &functions)) {
748 *error = __DRI_CTX_ERROR_NO_MEMORY;
749 goto context_fail;
750 }
751
752 /* do bounds checking to prevent segfaults and server crashes! */
753 mesaCtx->Const.CheckArrayBounds = GL_TRUE;
754
755 /* create module contexts */
756 _swrast_CreateContext( mesaCtx );
757 _vbo_CreateContext( mesaCtx );
758 _tnl_CreateContext( mesaCtx );
759 _swsetup_CreateContext( mesaCtx );
760 _swsetup_Wakeup( mesaCtx );
761
762 /* use default TCL pipeline */
763 {
764 TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
765 tnl->Driver.RunPipeline = _tnl_run_pipeline;
766 }
767
768 _mesa_meta_init(mesaCtx);
769 _mesa_enable_sw_extensions(mesaCtx);
770
771 switch (api) {
772 case API_OPENGL_CORE:
773 /* XXX fix me, fall-through for now */
774 case API_OPENGL_COMPAT:
775 _mesa_enable_1_3_extensions(mesaCtx);
776 _mesa_enable_1_4_extensions(mesaCtx);
777 _mesa_enable_1_5_extensions(mesaCtx);
778 _mesa_enable_2_0_extensions(mesaCtx);
779 _mesa_enable_2_1_extensions(mesaCtx);
780 break;
781 case API_OPENGLES:
782 _mesa_enable_1_3_extensions(mesaCtx);
783 _mesa_enable_1_4_extensions(mesaCtx);
784 _mesa_enable_1_5_extensions(mesaCtx);
785
786 break;
787 case API_OPENGLES2:
788 InitExtensionsES2( mesaCtx);
789 break;
790 }
791
792 _mesa_compute_version(mesaCtx);
793
794 _mesa_initialize_dispatch_tables(mesaCtx);
795 _mesa_initialize_vbo_vtxfmt(mesaCtx);
796
797 *error = __DRI_CTX_ERROR_SUCCESS;
798 return GL_TRUE;
799
800 context_fail:
801
802 free(ctx);
803
804 return GL_FALSE;
805 }
806
807 static void
808 dri_destroy_context(__DRIcontext * cPriv)
809 {
810 TRACE;
811
812 if (cPriv) {
813 struct dri_context *ctx = dri_context(cPriv);
814 struct gl_context *mesaCtx;
815
816 mesaCtx = &ctx->Base;
817
818 _mesa_meta_free(mesaCtx);
819 _swsetup_DestroyContext( mesaCtx );
820 _swrast_DestroyContext( mesaCtx );
821 _tnl_DestroyContext( mesaCtx );
822 _vbo_DestroyContext( mesaCtx );
823 _mesa_destroy_context( mesaCtx );
824 }
825 }
826
827 static GLboolean
828 dri_make_current(__DRIcontext * cPriv,
829 __DRIdrawable * driDrawPriv,
830 __DRIdrawable * driReadPriv)
831 {
832 struct gl_context *mesaCtx;
833 struct gl_framebuffer *mesaDraw;
834 struct gl_framebuffer *mesaRead;
835 TRACE;
836
837 if (cPriv) {
838 struct dri_context *ctx = dri_context(cPriv);
839 struct dri_drawable *draw;
840 struct dri_drawable *read;
841
842 if (!driDrawPriv || !driReadPriv)
843 return GL_FALSE;
844
845 draw = dri_drawable(driDrawPriv);
846 read = dri_drawable(driReadPriv);
847 mesaCtx = &ctx->Base;
848 mesaDraw = &draw->Base;
849 mesaRead = &read->Base;
850
851 /* check for same context and buffer */
852 if (mesaCtx == _mesa_get_current_context()
853 && mesaCtx->DrawBuffer == mesaDraw
854 && mesaCtx->ReadBuffer == mesaRead) {
855 return GL_TRUE;
856 }
857
858 _glapi_check_multithread();
859
860 swrast_check_and_update_window_size(mesaCtx, mesaDraw);
861 if (mesaRead != mesaDraw)
862 swrast_check_and_update_window_size(mesaCtx, mesaRead);
863
864 _mesa_make_current( mesaCtx,
865 mesaDraw,
866 mesaRead );
867 }
868 else {
869 /* unbind */
870 _mesa_make_current( NULL, NULL, NULL );
871 }
872
873 return GL_TRUE;
874 }
875
876 static GLboolean
877 dri_unbind_context(__DRIcontext * cPriv)
878 {
879 TRACE;
880 (void) cPriv;
881
882 /* Unset current context and dispath table */
883 _mesa_make_current(NULL, NULL, NULL);
884
885 return GL_TRUE;
886 }
887
888
889 const struct __DriverAPIRec driDriverAPI = {
890 .InitScreen = dri_init_screen,
891 .DestroyScreen = dri_destroy_screen,
892 .CreateContext = dri_create_context,
893 .DestroyContext = dri_destroy_context,
894 .CreateBuffer = dri_create_buffer,
895 .DestroyBuffer = dri_destroy_buffer,
896 .SwapBuffers = dri_swap_buffers,
897 .MakeCurrent = dri_make_current,
898 .UnbindContext = dri_unbind_context,
899 };
900
901 /* This is the table of extensions that the loader will dlsym() for. */
902 PUBLIC const __DRIextension *__driDriverExtensions[] = {
903 &driCoreExtension.base,
904 &driSWRastExtension.base,
905 NULL
906 };