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