i965: Fix predicated-send-based discards with MRT.
[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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 /*
24 * DRI software rasterizer
25 *
26 * This is the mesa swrast module packaged into a DRI driver structure.
27 *
28 * The front-buffer is allocated by the loader. The loader provides read/write
29 * callbacks for access to the front-buffer. The driver uses a scratch row for
30 * front-buffer rendering to avoid repeated calls to the loader.
31 *
32 * The back-buffer is allocated by the driver and is private.
33 */
34
35 #include "main/api_exec.h"
36 #include "main/context.h"
37 #include "main/extensions.h"
38 #include "main/formats.h"
39 #include "main/framebuffer.h"
40 #include "main/imports.h"
41 #include "main/renderbuffer.h"
42 #include "main/version.h"
43 #include "main/vtxfmt.h"
44 #include "swrast/swrast.h"
45 #include "swrast/s_renderbuffer.h"
46 #include "swrast_setup/swrast_setup.h"
47 #include "tnl/tnl.h"
48 #include "tnl/t_context.h"
49 #include "tnl/t_pipeline.h"
50 #include "vbo/vbo.h"
51 #include "drivers/common/driverfuncs.h"
52 #include "drivers/common/meta.h"
53 #include "utils.h"
54
55 #include "main/teximage.h"
56 #include "main/texformat.h"
57 #include "main/texstate.h"
58
59 #include "swrast_priv.h"
60 #include "swrast/s_context.h"
61
62 const __DRIextension **__driDriverGetExtensions_swrast(void);
63
64 /**
65 * Screen and config-related functions
66 */
67
68 static void swrastSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
69 GLint texture_format, __DRIdrawable *dPriv)
70 {
71 struct dri_context *dri_ctx;
72 int x, y, w, h;
73 __DRIscreen *sPriv = dPriv->driScreenPriv;
74 struct gl_texture_object *texObj;
75 struct gl_texture_image *texImage;
76 struct swrast_texture_image *swImage;
77 uint32_t internalFormat;
78 mesa_format texFormat;
79
80 dri_ctx = pDRICtx->driverPrivate;
81
82 internalFormat = (texture_format == __DRI_TEXTURE_FORMAT_RGB ? 3 : 4);
83
84 texObj = _mesa_get_current_tex_object(&dri_ctx->Base, 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_B8G8R8X8_UNORM;
94 else
95 texFormat = MESA_FORMAT_B8G8R8A8_UNORM;
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 mesa_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_B5G6R5_UNORM;
168 break;
169 case 24:
170 format = MESA_FORMAT_B8G8R8X8_UNORM;
171 break;
172 case 32:
173 format = MESA_FORMAT_B8G8R8A8_UNORM;
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->max_gl_compat_version = 21;
203 psp->max_gl_es1_version = 11;
204 psp->max_gl_es2_version = 20;
205
206 psp->extensions = dri_screen_extensions;
207
208 configs16 = swrastFillInModes(psp, 16, 16, 0, 1);
209 configs24 = swrastFillInModes(psp, 24, 24, 8, 1);
210 configs32 = swrastFillInModes(psp, 32, 24, 8, 1);
211
212 configs24 = driConcatConfigs(configs16, configs24);
213 configs32 = driConcatConfigs(configs24, configs32);
214
215 return (const __DRIconfig **)configs32;
216 }
217
218 static void
219 dri_destroy_screen(__DRIscreen * sPriv)
220 {
221 TRACE;
222 (void) sPriv;
223 }
224
225
226 /**
227 * Framebuffer and renderbuffer-related functions.
228 */
229
230 static GLuint
231 choose_pixel_format(const struct gl_config *v)
232 {
233 int depth = v->rgbBits;
234
235 if (depth == 32
236 && v->redMask == 0xff0000
237 && v->greenMask == 0x00ff00
238 && v->blueMask == 0x0000ff)
239 return PF_A8R8G8B8;
240 else if (depth == 24
241 && v->redMask == 0xff0000
242 && v->greenMask == 0x00ff00
243 && v->blueMask == 0x0000ff)
244 return PF_X8R8G8B8;
245 else if (depth == 16
246 && v->redMask == 0xf800
247 && v->greenMask == 0x07e0
248 && v->blueMask == 0x001f)
249 return PF_R5G6B5;
250 else if (depth == 8
251 && v->redMask == 0x07
252 && v->greenMask == 0x38
253 && v->blueMask == 0xc0)
254 return PF_R3G3B2;
255
256 _mesa_problem( NULL, "unexpected format in %s", __FUNCTION__ );
257 return 0;
258 }
259
260 static void
261 swrast_delete_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
262 {
263 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
264
265 TRACE;
266
267 free(xrb->Base.Buffer);
268 _mesa_delete_renderbuffer(ctx, rb);
269 }
270
271 /* see bytes_per_line in libGL */
272 static INLINE int
273 bytes_per_line(unsigned pitch_bits, unsigned mul)
274 {
275 unsigned mask = mul - 1;
276
277 return ((pitch_bits + mask) & ~mask) / 8;
278 }
279
280 static GLboolean
281 swrast_alloc_front_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
282 GLenum internalFormat, GLuint width, GLuint height)
283 {
284 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
285
286 TRACE;
287
288 (void) ctx;
289 (void) internalFormat;
290
291 xrb->Base.Buffer = NULL;
292 rb->Width = width;
293 rb->Height = height;
294 xrb->pitch = bytes_per_line(width * xrb->bpp, 32);
295
296 return GL_TRUE;
297 }
298
299 static GLboolean
300 swrast_alloc_back_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
301 GLenum internalFormat, GLuint width, GLuint height)
302 {
303 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
304
305 TRACE;
306
307 free(xrb->Base.Buffer);
308
309 swrast_alloc_front_storage(ctx, rb, internalFormat, width, height);
310
311 xrb->Base.Buffer = malloc(height * xrb->pitch);
312
313 return GL_TRUE;
314 }
315
316 static struct dri_swrast_renderbuffer *
317 swrast_new_renderbuffer(const struct gl_config *visual, __DRIdrawable *dPriv,
318 GLboolean front)
319 {
320 struct dri_swrast_renderbuffer *xrb = calloc(1, sizeof *xrb);
321 struct gl_renderbuffer *rb;
322 GLuint pixel_format;
323
324 TRACE;
325
326 if (!xrb)
327 return NULL;
328
329 rb = &xrb->Base.Base;
330
331 _mesa_init_renderbuffer(rb, 0);
332
333 pixel_format = choose_pixel_format(visual);
334
335 xrb->dPriv = dPriv;
336 xrb->Base.Base.Delete = swrast_delete_renderbuffer;
337 if (front) {
338 rb->AllocStorage = swrast_alloc_front_storage;
339 }
340 else {
341 rb->AllocStorage = swrast_alloc_back_storage;
342 }
343
344 switch (pixel_format) {
345 case PF_A8R8G8B8:
346 rb->Format = MESA_FORMAT_B8G8R8A8_UNORM;
347 rb->InternalFormat = GL_RGBA;
348 rb->_BaseFormat = GL_RGBA;
349 xrb->bpp = 32;
350 break;
351 case PF_X8R8G8B8:
352 rb->Format = MESA_FORMAT_B8G8R8A8_UNORM; /* XXX */
353 rb->InternalFormat = GL_RGB;
354 rb->_BaseFormat = GL_RGB;
355 xrb->bpp = 32;
356 break;
357 case PF_R5G6B5:
358 rb->Format = MESA_FORMAT_B5G6R5_UNORM;
359 rb->InternalFormat = GL_RGB;
360 rb->_BaseFormat = GL_RGB;
361 xrb->bpp = 16;
362 break;
363 case PF_R3G3B2:
364 rb->Format = MESA_FORMAT_B2G3R3_UNORM;
365 rb->InternalFormat = GL_RGB;
366 rb->_BaseFormat = GL_RGB;
367 xrb->bpp = 8;
368 break;
369 default:
370 free(xrb);
371 return NULL;
372 }
373
374 return xrb;
375 }
376
377 static void
378 swrast_map_renderbuffer(struct gl_context *ctx,
379 struct gl_renderbuffer *rb,
380 GLuint x, GLuint y, GLuint w, GLuint h,
381 GLbitfield mode,
382 GLubyte **out_map,
383 GLint *out_stride)
384 {
385 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
386 GLubyte *map = xrb->Base.Buffer;
387 int cpp = _mesa_get_format_bytes(rb->Format);
388 int stride = rb->Width * cpp;
389
390 if (rb->AllocStorage == swrast_alloc_front_storage) {
391 __DRIdrawable *dPriv = xrb->dPriv;
392 __DRIscreen *sPriv = dPriv->driScreenPriv;
393
394 xrb->map_mode = mode;
395 xrb->map_x = x;
396 xrb->map_y = y;
397 xrb->map_w = w;
398 xrb->map_h = h;
399
400 stride = w * cpp;
401 xrb->Base.Buffer = malloc(h * stride);
402
403 sPriv->swrast_loader->getImage(dPriv, x, rb->Height - y - h, w, h,
404 (char *) xrb->Base.Buffer,
405 dPriv->loaderPrivate);
406
407 *out_map = xrb->Base.Buffer + (h - 1) * stride;
408 *out_stride = -stride;
409 return;
410 }
411
412 ASSERT(xrb->Base.Buffer);
413
414 if (rb->AllocStorage == swrast_alloc_back_storage) {
415 map += (rb->Height - 1) * stride;
416 stride = -stride;
417 }
418
419 map += (GLsizei)y * stride;
420 map += (GLsizei)x * cpp;
421
422 *out_map = map;
423 *out_stride = stride;
424 }
425
426 static void
427 swrast_unmap_renderbuffer(struct gl_context *ctx,
428 struct gl_renderbuffer *rb)
429 {
430 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
431
432 if (rb->AllocStorage == swrast_alloc_front_storage) {
433 __DRIdrawable *dPriv = xrb->dPriv;
434 __DRIscreen *sPriv = dPriv->driScreenPriv;
435
436 if (xrb->map_mode & GL_MAP_WRITE_BIT) {
437 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_DRAW,
438 xrb->map_x, xrb->map_y,
439 xrb->map_w, xrb->map_h,
440 (char *) xrb->Base.Buffer,
441 dPriv->loaderPrivate);
442 }
443
444 free(xrb->Base.Buffer);
445 xrb->Base.Buffer = NULL;
446 }
447 }
448
449 static GLboolean
450 dri_create_buffer(__DRIscreen * sPriv,
451 __DRIdrawable * dPriv,
452 const struct gl_config * visual, GLboolean isPixmap)
453 {
454 struct dri_drawable *drawable = NULL;
455 struct gl_framebuffer *fb;
456 struct dri_swrast_renderbuffer *frontrb, *backrb;
457
458 TRACE;
459
460 (void) sPriv;
461 (void) isPixmap;
462
463 drawable = CALLOC_STRUCT(dri_drawable);
464 if (drawable == NULL)
465 goto drawable_fail;
466
467 dPriv->driverPrivate = drawable;
468 drawable->dPriv = dPriv;
469
470 drawable->row = malloc(SWRAST_MAX_WIDTH * 4);
471 if (drawable->row == NULL)
472 goto drawable_fail;
473
474 fb = &drawable->Base;
475
476 /* basic framebuffer setup */
477 _mesa_initialize_window_framebuffer(fb, visual);
478
479 /* add front renderbuffer */
480 frontrb = swrast_new_renderbuffer(visual, dPriv, GL_TRUE);
481 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontrb->Base.Base);
482
483 /* add back renderbuffer */
484 if (visual->doubleBufferMode) {
485 backrb = swrast_new_renderbuffer(visual, dPriv, GL_FALSE);
486 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backrb->Base.Base);
487 }
488
489 /* add software renderbuffers */
490 _swrast_add_soft_renderbuffers(fb,
491 GL_FALSE, /* color */
492 visual->haveDepthBuffer,
493 visual->haveStencilBuffer,
494 visual->haveAccumBuffer,
495 GL_FALSE, /* alpha */
496 GL_FALSE /* aux bufs */);
497
498 return GL_TRUE;
499
500 drawable_fail:
501
502 if (drawable)
503 free(drawable->row);
504
505 free(drawable);
506
507 return GL_FALSE;
508 }
509
510 static void
511 dri_destroy_buffer(__DRIdrawable * dPriv)
512 {
513 TRACE;
514
515 if (dPriv) {
516 struct dri_drawable *drawable = dri_drawable(dPriv);
517 struct gl_framebuffer *fb;
518
519 free(drawable->row);
520
521 fb = &drawable->Base;
522
523 fb->DeletePending = GL_TRUE;
524 _mesa_reference_framebuffer(&fb, NULL);
525 }
526 }
527
528 static void
529 dri_swap_buffers(__DRIdrawable * dPriv)
530 {
531 __DRIscreen *sPriv = dPriv->driScreenPriv;
532
533 GET_CURRENT_CONTEXT(ctx);
534
535 struct dri_drawable *drawable = dri_drawable(dPriv);
536 struct gl_framebuffer *fb;
537 struct dri_swrast_renderbuffer *frontrb, *backrb;
538
539 TRACE;
540
541 fb = &drawable->Base;
542
543 frontrb =
544 dri_swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
545 backrb =
546 dri_swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
547
548 /* check for signle-buffered */
549 if (backrb == NULL)
550 return;
551
552 /* check if swapping currently bound buffer */
553 if (ctx && ctx->DrawBuffer == fb) {
554 /* flush pending rendering */
555 _mesa_notifySwapBuffers(ctx);
556 }
557
558 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
559 0, 0,
560 frontrb->Base.Base.Width,
561 frontrb->Base.Base.Height,
562 (char *) backrb->Base.Buffer,
563 dPriv->loaderPrivate);
564 }
565
566
567 /**
568 * General device driver functions.
569 */
570
571 static void
572 get_window_size( struct gl_framebuffer *fb, GLsizei *w, GLsizei *h )
573 {
574 __DRIdrawable *dPriv = swrast_drawable(fb)->dPriv;
575 __DRIscreen *sPriv = dPriv->driScreenPriv;
576 int x, y;
577
578 sPriv->swrast_loader->getDrawableInfo(dPriv,
579 &x, &y, w, h,
580 dPriv->loaderPrivate);
581 }
582
583 static void
584 swrast_check_and_update_window_size( struct gl_context *ctx, struct gl_framebuffer *fb )
585 {
586 GLsizei width, height;
587
588 get_window_size(fb, &width, &height);
589 if (fb->Width != width || fb->Height != height) {
590 _mesa_resize_framebuffer(ctx, fb, width, height);
591 }
592 }
593
594 static const GLubyte *
595 get_string(struct gl_context *ctx, GLenum pname)
596 {
597 (void) ctx;
598 switch (pname) {
599 case GL_VENDOR:
600 return (const GLubyte *) "Mesa Project";
601 case GL_RENDERER:
602 return (const GLubyte *) "Software Rasterizer";
603 default:
604 return NULL;
605 }
606 }
607
608 static void
609 update_state( struct gl_context *ctx, GLuint new_state )
610 {
611 /* not much to do here - pass it on */
612 _swrast_InvalidateState( ctx, new_state );
613 _swsetup_InvalidateState( ctx, new_state );
614 _vbo_InvalidateState( ctx, new_state );
615 _tnl_InvalidateState( ctx, new_state );
616 }
617
618 static void
619 viewport(struct gl_context *ctx)
620 {
621 struct gl_framebuffer *draw = ctx->WinSysDrawBuffer;
622 struct gl_framebuffer *read = ctx->WinSysReadBuffer;
623
624 swrast_check_and_update_window_size(ctx, draw);
625 swrast_check_and_update_window_size(ctx, read);
626 }
627
628 static mesa_format swrastChooseTextureFormat(struct gl_context * ctx,
629 GLenum target,
630 GLint internalFormat,
631 GLenum format,
632 GLenum type)
633 {
634 if (internalFormat == GL_RGB)
635 return MESA_FORMAT_B8G8R8X8_UNORM;
636 return _mesa_choose_tex_format(ctx, target, internalFormat, format, type);
637 }
638
639 static void
640 swrast_init_driver_functions(struct dd_function_table *driver)
641 {
642 driver->GetString = get_string;
643 driver->UpdateState = update_state;
644 driver->Viewport = viewport;
645 driver->ChooseTextureFormat = swrastChooseTextureFormat;
646 driver->MapRenderbuffer = swrast_map_renderbuffer;
647 driver->UnmapRenderbuffer = swrast_unmap_renderbuffer;
648 }
649
650 /**
651 * Context-related functions.
652 */
653
654 static GLboolean
655 dri_create_context(gl_api api,
656 const struct gl_config * visual,
657 __DRIcontext * cPriv,
658 unsigned major_version,
659 unsigned minor_version,
660 uint32_t flags,
661 bool notify_reset,
662 unsigned *error,
663 void *sharedContextPrivate)
664 {
665 struct dri_context *ctx = NULL;
666 struct dri_context *share = (struct dri_context *)sharedContextPrivate;
667 struct gl_context *mesaCtx = NULL;
668 struct gl_context *sharedCtx = NULL;
669 struct dd_function_table functions;
670
671 TRACE;
672
673 /* Flag filtering is handled in dri2CreateContextAttribs.
674 */
675 (void) flags;
676
677 ctx = CALLOC_STRUCT(dri_context);
678 if (ctx == NULL) {
679 *error = __DRI_CTX_ERROR_NO_MEMORY;
680 goto context_fail;
681 }
682
683 cPriv->driverPrivate = ctx;
684 ctx->cPriv = cPriv;
685
686 /* build table of device driver functions */
687 _mesa_init_driver_functions(&functions);
688 swrast_init_driver_functions(&functions);
689
690 if (share) {
691 sharedCtx = &share->Base;
692 }
693
694 mesaCtx = &ctx->Base;
695
696 /* basic context setup */
697 if (!_mesa_initialize_context(mesaCtx, api, visual, sharedCtx, &functions)) {
698 *error = __DRI_CTX_ERROR_NO_MEMORY;
699 goto context_fail;
700 }
701
702 driContextSetFlags(mesaCtx, flags);
703
704 /* do bounds checking to prevent segfaults and server crashes! */
705 mesaCtx->Const.CheckArrayBounds = GL_TRUE;
706
707 /* create module contexts */
708 _swrast_CreateContext( mesaCtx );
709 _vbo_CreateContext( mesaCtx );
710 _tnl_CreateContext( mesaCtx );
711 _swsetup_CreateContext( mesaCtx );
712 _swsetup_Wakeup( mesaCtx );
713
714 /* use default TCL pipeline */
715 {
716 TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
717 tnl->Driver.RunPipeline = _tnl_run_pipeline;
718 }
719
720 _mesa_meta_init(mesaCtx);
721 _mesa_enable_sw_extensions(mesaCtx);
722
723 _mesa_compute_version(mesaCtx);
724
725 _mesa_initialize_dispatch_tables(mesaCtx);
726 _mesa_initialize_vbo_vtxfmt(mesaCtx);
727
728 *error = __DRI_CTX_ERROR_SUCCESS;
729 return GL_TRUE;
730
731 context_fail:
732
733 free(ctx);
734
735 return GL_FALSE;
736 }
737
738 static void
739 dri_destroy_context(__DRIcontext * cPriv)
740 {
741 TRACE;
742
743 if (cPriv) {
744 struct dri_context *ctx = dri_context(cPriv);
745 struct gl_context *mesaCtx;
746
747 mesaCtx = &ctx->Base;
748
749 _mesa_meta_free(mesaCtx);
750 _swsetup_DestroyContext( mesaCtx );
751 _swrast_DestroyContext( mesaCtx );
752 _tnl_DestroyContext( mesaCtx );
753 _vbo_DestroyContext( mesaCtx );
754 _mesa_destroy_context( mesaCtx );
755 }
756 }
757
758 static GLboolean
759 dri_make_current(__DRIcontext * cPriv,
760 __DRIdrawable * driDrawPriv,
761 __DRIdrawable * driReadPriv)
762 {
763 struct gl_context *mesaCtx;
764 struct gl_framebuffer *mesaDraw;
765 struct gl_framebuffer *mesaRead;
766 TRACE;
767
768 if (cPriv) {
769 struct dri_context *ctx = dri_context(cPriv);
770 struct dri_drawable *draw;
771 struct dri_drawable *read;
772
773 if (!driDrawPriv || !driReadPriv)
774 return GL_FALSE;
775
776 draw = dri_drawable(driDrawPriv);
777 read = dri_drawable(driReadPriv);
778 mesaCtx = &ctx->Base;
779 mesaDraw = &draw->Base;
780 mesaRead = &read->Base;
781
782 /* check for same context and buffer */
783 if (mesaCtx == _mesa_get_current_context()
784 && mesaCtx->DrawBuffer == mesaDraw
785 && mesaCtx->ReadBuffer == mesaRead) {
786 return GL_TRUE;
787 }
788
789 _glapi_check_multithread();
790
791 swrast_check_and_update_window_size(mesaCtx, mesaDraw);
792 if (mesaRead != mesaDraw)
793 swrast_check_and_update_window_size(mesaCtx, mesaRead);
794
795 _mesa_make_current( mesaCtx,
796 mesaDraw,
797 mesaRead );
798 }
799 else {
800 /* unbind */
801 _mesa_make_current( NULL, NULL, NULL );
802 }
803
804 return GL_TRUE;
805 }
806
807 static GLboolean
808 dri_unbind_context(__DRIcontext * cPriv)
809 {
810 TRACE;
811 (void) cPriv;
812
813 /* Unset current context and dispath table */
814 _mesa_make_current(NULL, NULL, NULL);
815
816 return GL_TRUE;
817 }
818
819 static void
820 dri_copy_sub_buffer(__DRIdrawable *dPriv, int x, int y,
821 int w, int h)
822 {
823 __DRIscreen *sPriv = dPriv->driScreenPriv;
824 void *data;
825 int iy;
826 struct dri_drawable *drawable = dri_drawable(dPriv);
827 struct gl_framebuffer *fb;
828 struct dri_swrast_renderbuffer *frontrb, *backrb;
829
830 TRACE;
831
832 fb = &drawable->Base;
833
834 frontrb =
835 dri_swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
836 backrb =
837 dri_swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
838
839 /* check for signle-buffered */
840 if (backrb == NULL)
841 return;
842
843 iy = frontrb->Base.Base.Height - y - h;
844 data = (char *)backrb->Base.Buffer + (iy * backrb->pitch) + (x * ((backrb->bpp + 7) / 8));
845 sPriv->swrast_loader->putImage2(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
846 x, iy, w, h,
847 frontrb->pitch,
848 data,
849 dPriv->loaderPrivate);
850 }
851
852
853 static const struct __DriverAPIRec swrast_driver_api = {
854 .InitScreen = dri_init_screen,
855 .DestroyScreen = dri_destroy_screen,
856 .CreateContext = dri_create_context,
857 .DestroyContext = dri_destroy_context,
858 .CreateBuffer = dri_create_buffer,
859 .DestroyBuffer = dri_destroy_buffer,
860 .SwapBuffers = dri_swap_buffers,
861 .MakeCurrent = dri_make_current,
862 .UnbindContext = dri_unbind_context,
863 .CopySubBuffer = dri_copy_sub_buffer,
864 };
865
866 static const struct __DRIDriverVtableExtensionRec swrast_vtable = {
867 .base = { __DRI_DRIVER_VTABLE, 1 },
868 .vtable = &swrast_driver_api,
869 };
870
871 static const __DRIextension *swrast_driver_extensions[] = {
872 &driCoreExtension.base,
873 &driSWRastExtension.base,
874 &driCopySubBufferExtension.base,
875 &swrast_vtable.base,
876 NULL
877 };
878
879 PUBLIC const __DRIextension **__driDriverGetExtensions_swrast(void)
880 {
881 globalDriverAPI = &swrast_driver_api;
882
883 return swrast_driver_extensions;
884 }