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