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