1280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis/*
2e60693efda7826bf26ffb9993c6dfba3b8c8a812George Sapountzis * Copyright 2008, 2010 George Sapountzis <gsapountzis@gmail.com>
3280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis *
4280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * Permission is hereby granted, free of charge, to any person obtaining a
5280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * copy of this software and associated documentation files (the "Software"),
6280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * to deal in the Software without restriction, including without limitation
7280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * and/or sell copies of the Software, and to permit persons to whom the
9280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * Software is furnished to do so, subject to the following conditions:
10280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis *
11280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * The above copyright notice and this permission notice shall be included
12280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * in all copies or substantial portions of the Software.
13280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis *
14280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis */
21280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
2295eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis/*
2395eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis * DRI software rasterizer
2495eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis *
2595eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis * This is the mesa swrast module packaged into a DRI driver structure.
2695eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis *
2795eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis * The front-buffer is allocated by the loader. The loader provides read/write
2895eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis * callbacks for access to the front-buffer. The driver uses a scratch row for
2995eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis * front-buffer rendering to avoid repeated calls to the loader.
3095eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis *
3195eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis * The back-buffer is allocated by the driver and is private.
3295eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis */
3395eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis
34ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/context.h"
35ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/extensions.h"
3645e76d2665b38ba3787548310efc59e969124c01Brian Paul#include "main/formats.h"
37ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/framebuffer.h"
38ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/imports.h"
39ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/renderbuffer.h"
40280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis#include "swrast/swrast.h"
41d0dc75c000d5af92648c7de901756400672b8447Brian Paul#include "swrast/s_renderbuffer.h"
42280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis#include "swrast_setup/swrast_setup.h"
43280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis#include "tnl/tnl.h"
448d41e4536dfde34b55228cab6ddb081ad6252184Chris Rankin#include "tnl/t_context.h"
45280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis#include "tnl/t_pipeline.h"
46280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis#include "vbo/vbo.h"
47280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis#include "drivers/common/driverfuncs.h"
484a4914e4146b78e99277ab494226136a4e68cdb4Brian Paul#include "drivers/common/meta.h"
49341987f68842d662168aa6386723ef9ac8080a93George Sapountzis#include "utils.h"
50280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
518d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie#include "main/teximage.h"
528d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie#include "main/texformat.h"
538d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie#include "main/texstate.h"
548d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
55280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis#include "swrast_priv.h"
566e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul#include "swrast/s_context.h"
57280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
58280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
5995eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis/**
6095eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis * Screen and config-related functions
6195eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis */
6295eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis
638d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airliestatic void swrastSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
648d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie				GLint texture_format, __DRIdrawable *dPriv)
658d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie{
668d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    struct dri_context *dri_ctx;
678d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    int x, y, w, h;
688d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    __DRIscreen *sPriv = dPriv->driScreenPriv;
698d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    struct gl_texture_unit *texUnit;
708d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    struct gl_texture_object *texObj;
718d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    struct gl_texture_image *texImage;
726e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul    struct swrast_texture_image *swImage;
738d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    uint32_t internalFormat;
7483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul    gl_format texFormat;
758d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
768d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    dri_ctx = pDRICtx->driverPrivate;
778d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
788d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    internalFormat = (texture_format == __DRI_TEXTURE_FORMAT_RGB ? 3 : 4);
798d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
808d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    texUnit = _mesa_get_current_tex_unit(&dri_ctx->Base);
818d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    texObj = _mesa_select_tex_object(&dri_ctx->Base, texUnit, target);
828d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    texImage = _mesa_get_tex_image(&dri_ctx->Base, texObj, target, 0);
836e0f9001fe3fb191c2928bd09aa9e9d05ddf4ea9Brian Paul    swImage = swrast_texture_image(texImage);
848d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
858d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    _mesa_lock_texture(&dri_ctx->Base, texObj);
868d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
878d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    sPriv->swrast_loader->getDrawableInfo(dPriv, &x, &y, &w, &h, dPriv->loaderPrivate);
888d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
898d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    if (texture_format == __DRI_TEXTURE_FORMAT_RGB)
9083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul	texFormat = MESA_FORMAT_XRGB8888;
918d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    else
9283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul	texFormat = MESA_FORMAT_ARGB8888;
938d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
94892a2542a3f0753a7064c710b96f077dd5490624Brian Paul    _mesa_init_teximage_fields(&dri_ctx->Base, texImage,
9583e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul			       w, h, 1, 0, internalFormat, texFormat);
968d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
97bd3c10c0f0c60ab3421c2da2eab814edc2296cb0Brian Paul    sPriv->swrast_loader->getImage(dPriv, x, y, w, h, (char *)swImage->Buffer,
988d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie				   dPriv->loaderPrivate);
998d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
1008d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    _mesa_unlock_texture(&dri_ctx->Base, texObj);
1018d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie}
1028d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
1038d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airliestatic void swrastSetTexBuffer(__DRIcontext *pDRICtx, GLint target,
1048d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie			       __DRIdrawable *dPriv)
1058d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie{
1068d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    swrastSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
1078d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie}
1088d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
1098d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airliestatic const __DRItexBufferExtension swrastTexBufferExtension = {
1108d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
1118d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    swrastSetTexBuffer,
1128d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    swrastSetTexBuffer2,
1138d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie};
1148d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
1156e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisstatic const __DRIextension *dri_screen_extensions[] = {
1168d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    &swrastTexBufferExtension.base,
1176e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    NULL
1186e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis};
119280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
1205d5ae371eaeca7e33e638af3aee1ae8e162db0a7Eric Anholtstatic __DRIconfig **
121280bf89bd4a6bb7977abb00b88d59234d2c80844George SapountzisswrastFillInModes(__DRIscreen *psp,
122280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis		  unsigned pixel_bits, unsigned depth_bits,
123280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis		  unsigned stencil_bits, GLboolean have_back_buffer)
124280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
125d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    __DRIconfig **configs;
126d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    unsigned depth_buffer_factor;
127d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    unsigned back_buffer_factor;
128d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    GLenum fb_format;
129d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    GLenum fb_type;
130d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis
131d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
132d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis     * support pageflipping at all.
133d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis     */
134d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    static const GLenum back_buffer_modes[] = {
135d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis	GLX_NONE, GLX_SWAP_UNDEFINED_OML
136d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    };
137d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis
1385a46e176715b0eae7b8a715e8aec42f5a27214fcKeith Whitwell    uint8_t depth_bits_array[4];
1395a46e176715b0eae7b8a715e8aec42f5a27214fcKeith Whitwell    uint8_t stencil_bits_array[4];
140eaf15db895e3a5c6c5ccc2f23a4f0fa522855868Michel Dänzer    uint8_t msaa_samples_array[1];
141d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis
1422d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick    (void) psp;
1432d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick    (void) have_back_buffer;
1442d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick
145d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    depth_bits_array[0] = 0;
14653784e786da69296020914c508905a9cf14b490dGeorge Sapountzis    depth_bits_array[1] = 0;
147d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    depth_bits_array[2] = depth_bits;
14853784e786da69296020914c508905a9cf14b490dGeorge Sapountzis    depth_bits_array[3] = depth_bits;
149d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis
15053784e786da69296020914c508905a9cf14b490dGeorge Sapountzis    /* Just like with the accumulation buffer, always provide some modes
15153784e786da69296020914c508905a9cf14b490dGeorge Sapountzis     * with a stencil buffer.
15253784e786da69296020914c508905a9cf14b490dGeorge Sapountzis     */
153d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    stencil_bits_array[0] = 0;
15453784e786da69296020914c508905a9cf14b490dGeorge Sapountzis    stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
15553784e786da69296020914c508905a9cf14b490dGeorge Sapountzis    stencil_bits_array[2] = 0;
15653784e786da69296020914c508905a9cf14b490dGeorge Sapountzis    stencil_bits_array[3] = (stencil_bits == 0) ? 8 : stencil_bits;
157d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis
158eaf15db895e3a5c6c5ccc2f23a4f0fa522855868Michel Dänzer    msaa_samples_array[0] = 0;
159eaf15db895e3a5c6c5ccc2f23a4f0fa522855868Michel Dänzer
16053784e786da69296020914c508905a9cf14b490dGeorge Sapountzis    depth_buffer_factor = 4;
161d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    back_buffer_factor = 2;
162d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis
163529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt    switch (pixel_bits) {
164529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt    case 8:
165c1d4644f487a3ff4dcb3ef32995fed30cd72eebaGeorge Sapountzis	fb_format = GL_RGB;
166c1d4644f487a3ff4dcb3ef32995fed30cd72eebaGeorge Sapountzis	fb_type = GL_UNSIGNED_BYTE_2_3_3_REV;
167529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	break;
168529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt    case 16:
169d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis	fb_format = GL_RGB;
170d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis	fb_type = GL_UNSIGNED_SHORT_5_6_5;
171529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	break;
172529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt    case 24:
173529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	fb_format = GL_BGR;
174529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
175529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	break;
176529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt    case 32:
177d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis	fb_format = GL_BGRA;
178d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis	fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
179529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	break;
180529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt    default:
181529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	fprintf(stderr, "[%s:%u] bad depth %d\n", __func__, __LINE__,
182529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt		pixel_bits);
183529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	return NULL;
184d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    }
185d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis
186d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    configs = driCreateConfigs(fb_format, fb_type,
187d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis			       depth_bits_array, stencil_bits_array,
188d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis			       depth_buffer_factor, back_buffer_modes,
1893cce4a1e10361458630511543b7a8a6438544775Ian Romanick			       back_buffer_factor, msaa_samples_array, 1,
1903cce4a1e10361458630511543b7a8a6438544775Ian Romanick			       GL_TRUE);
191d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    if (configs == NULL) {
192d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis	fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
193d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis		__LINE__);
194d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis	return NULL;
195d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis    }
196d0c7ef43585e96f523ad85c55cdc219e9c8bac95George Sapountzis
1975d5ae371eaeca7e33e638af3aee1ae8e162db0a7Eric Anholt    return configs;
198280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
199280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
2006e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisstatic const __DRIconfig **
2016e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisdri_init_screen(__DRIscreen * psp)
202280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
2035d5ae371eaeca7e33e638af3aee1ae8e162db0a7Eric Anholt    __DRIconfig **configs8, **configs16, **configs24, **configs32;
204280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
205280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
206280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
2076e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    psp->extensions = dri_screen_extensions;
208280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
209c1d4644f487a3ff4dcb3ef32995fed30cd72eebaGeorge Sapountzis    configs8  = swrastFillInModes(psp,  8,  8, 0, 1);
210c1d4644f487a3ff4dcb3ef32995fed30cd72eebaGeorge Sapountzis    configs16 = swrastFillInModes(psp, 16, 16, 0, 1);
211529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt    configs24 = swrastFillInModes(psp, 24, 24, 8, 1);
212c1d4644f487a3ff4dcb3ef32995fed30cd72eebaGeorge Sapountzis    configs32 = swrastFillInModes(psp, 32, 24, 8, 1);
213c1d4644f487a3ff4dcb3ef32995fed30cd72eebaGeorge Sapountzis
2143ee21f30cda27e0ee1357f930163526622ba9434Eric Anholt    configs16 = driConcatConfigs(configs8, configs16);
215529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt    configs24 = driConcatConfigs(configs16, configs24);
2166e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    configs32 = driConcatConfigs(configs24, configs32);
217280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
2186e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    return (const __DRIconfig **)configs32;
219280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
220280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
2216e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisstatic void
2226e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisdri_destroy_screen(__DRIscreen * sPriv)
223280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
224280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
2252d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick    (void) sPriv;
226280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
227280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
228280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
229280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis/**
23095eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis * Framebuffer and renderbuffer-related functions.
231280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis */
232280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
233280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic GLuint
234d3491e775fb07f891463b2185d74bbad62f3ed24Kristian Høgsbergchoose_pixel_format(const struct gl_config *v)
235280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
236718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick    int depth = v->rgbBits;
237718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick
238718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick    if (depth == 32
239718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	&& v->redMask   == 0xff0000
240718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	&& v->greenMask == 0x00ff00
241718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	&& v->blueMask  == 0x0000ff)
242718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	return PF_A8R8G8B8;
243718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick    else if (depth == 24
24442e7a13e7bc9def0bcdede2d3a34dad1718e105cBrian Paul	     && v->redMask   == 0xff0000
24542e7a13e7bc9def0bcdede2d3a34dad1718e105cBrian Paul	     && v->greenMask == 0x00ff00
24642e7a13e7bc9def0bcdede2d3a34dad1718e105cBrian Paul	     && v->blueMask  == 0x0000ff)
24742e7a13e7bc9def0bcdede2d3a34dad1718e105cBrian Paul	return PF_X8R8G8B8;
248718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick    else if (depth == 16
249718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	     && v->redMask   == 0xf800
250718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	     && v->greenMask == 0x07e0
251718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	     && v->blueMask  == 0x001f)
252718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	return PF_R5G6B5;
253718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick    else if (depth == 8
254718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	     && v->redMask   == 0x07
255718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	     && v->greenMask == 0x38
256718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	     && v->blueMask  == 0xc0)
257718ff7e87bf9b86517e88270cc984e364ace9506Ian Romanick	return PF_R3G3B2;
258280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
259280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    _mesa_problem( NULL, "unexpected format in %s", __FUNCTION__ );
260280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    return 0;
261280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
262280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
263280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic void
2649d4ab9a663d4088ec553edaae0eeafb746d2490dBrian Paulswrast_delete_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
265280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
266becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul    struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
267becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul
268280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
269280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
270becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul    free(xrb->Base.Buffer);
2719d4ab9a663d4088ec553edaae0eeafb746d2490dBrian Paul    _mesa_delete_renderbuffer(ctx, rb);
272280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
273280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
2743bfa23317c6b1b52ec637a03a0b623228ffc95efGeorge Sapountzis/* see bytes_per_line in libGL */
275f4e561ce127cf484d7c76c29b8cd026c9ad5cebcGeorge Sapountzisstatic INLINE int
276f4e561ce127cf484d7c76c29b8cd026c9ad5cebcGeorge Sapountzisbytes_per_line(unsigned pitch_bits, unsigned mul)
277f4e561ce127cf484d7c76c29b8cd026c9ad5cebcGeorge Sapountzis{
278f4e561ce127cf484d7c76c29b8cd026c9ad5cebcGeorge Sapountzis   unsigned mask = mul - 1;
279f4e561ce127cf484d7c76c29b8cd026c9ad5cebcGeorge Sapountzis
280f4e561ce127cf484d7c76c29b8cd026c9ad5cebcGeorge Sapountzis   return ((pitch_bits + mask) & ~mask) / 8;
281f4e561ce127cf484d7c76c29b8cd026c9ad5cebcGeorge Sapountzis}
282f4e561ce127cf484d7c76c29b8cd026c9ad5cebcGeorge Sapountzis
283280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic GLboolean
284f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergswrast_alloc_front_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
285280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis			   GLenum internalFormat, GLuint width, GLuint height)
286280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
2871048d55d5fa60d17dd99ac7394ff0572500625f9Brian Paul    struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
288280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
289280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
290280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
2912d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick    (void) ctx;
2922d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick    (void) internalFormat;
2932d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick
294becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul    xrb->Base.Buffer = NULL;
295280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    rb->Width = width;
296280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    rb->Height = height;
297f4e561ce127cf484d7c76c29b8cd026c9ad5cebcGeorge Sapountzis    xrb->pitch = bytes_per_line(width * xrb->bpp, 32);
298280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
299280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    return GL_TRUE;
300280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
301280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
302280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic GLboolean
303f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergswrast_alloc_back_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
30495eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis			  GLenum internalFormat, GLuint width, GLuint height)
305280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
3061048d55d5fa60d17dd99ac7394ff0572500625f9Brian Paul    struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
307280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
308280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
309280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
310becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul    free(xrb->Base.Buffer);
311280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
3124c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    swrast_alloc_front_storage(ctx, rb, internalFormat, width, height);
313280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
314becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul    xrb->Base.Buffer = malloc(height * xrb->pitch);
315280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
316280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    return GL_TRUE;
317280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
318280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
3191048d55d5fa60d17dd99ac7394ff0572500625f9Brian Paulstatic struct dri_swrast_renderbuffer *
32076d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholtswrast_new_renderbuffer(const struct gl_config *visual, __DRIdrawable *dPriv,
32176d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt			GLboolean front)
322280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
3231048d55d5fa60d17dd99ac7394ff0572500625f9Brian Paul    struct dri_swrast_renderbuffer *xrb = calloc(1, sizeof *xrb);
324becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul    struct gl_renderbuffer *rb;
325280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    GLuint pixel_format;
326280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
327280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
328280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
3294c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    if (!xrb)
3304c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis	return NULL;
3314c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis
332becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul    rb = &xrb->Base.Base;
333becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul
334becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul    _mesa_init_renderbuffer(rb, 0);
3354c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis
3364c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    pixel_format = choose_pixel_format(visual);
3374c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis
33876d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt    xrb->dPriv = dPriv;
339becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul    xrb->Base.Base.Delete = swrast_delete_renderbuffer;
3404c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    if (front) {
341becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul        rb->AllocStorage = swrast_alloc_front_storage;
342280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    }
3434c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    else {
344becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->AllocStorage = swrast_alloc_back_storage;
3454c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    }
3464c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis
3474c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    switch (pixel_format) {
3484c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    case PF_A8R8G8B8:
349becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->Format = MESA_FORMAT_ARGB8888;
350becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->InternalFormat = GL_RGBA;
351becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->_BaseFormat = GL_RGBA;
352529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	xrb->bpp = 32;
353529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	break;
354529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt    case PF_X8R8G8B8:
355becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->Format = MESA_FORMAT_ARGB8888; /* XXX */
356becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->InternalFormat = GL_RGB;
357becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->_BaseFormat = GL_RGB;
358529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	xrb->bpp = 32;
3594c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis	break;
3604c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    case PF_R5G6B5:
361becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->Format = MESA_FORMAT_RGB565;
362becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->InternalFormat = GL_RGB;
363becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->_BaseFormat = GL_RGB;
364529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	xrb->bpp = 16;
3654c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis	break;
3664c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    case PF_R3G3B2:
367becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->Format = MESA_FORMAT_RGB332;
368becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->InternalFormat = GL_RGB;
369becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	rb->_BaseFormat = GL_RGB;
370529d1d720e1422bad1880ef33fae1c9423112d2eEric Anholt	xrb->bpp = 8;
3714c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis	break;
3724c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    default:
3734c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis	return NULL;
3744c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis    }
3754c7e02298f68b84d929cd1d3af8b3120281c2e32George Sapountzis
376280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    return xrb;
377280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
378280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
37976d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholtstatic void
38076d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholtswrast_map_renderbuffer(struct gl_context *ctx,
38176d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt			struct gl_renderbuffer *rb,
38276d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt			GLuint x, GLuint y, GLuint w, GLuint h,
38376d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt			GLbitfield mode,
38476d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt			GLubyte **out_map,
38576d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt			GLint *out_stride)
38676d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt{
3871048d55d5fa60d17dd99ac7394ff0572500625f9Brian Paul   struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
388becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul   GLubyte *map = xrb->Base.Buffer;
38976d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt   int cpp = _mesa_get_format_bytes(rb->Format);
3901e1b5cb01a10e39d01923e3c7e989c44210950cdBrian Paul   int stride = rb->Width * cpp;
39176d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
39276d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt   if (rb->AllocStorage == swrast_alloc_front_storage) {
39376d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      __DRIdrawable *dPriv = xrb->dPriv;
39476d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      __DRIscreen *sPriv = dPriv->driScreenPriv;
39576d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
39676d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      xrb->map_mode = mode;
39776d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      xrb->map_x = x;
39876d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      xrb->map_y = y;
39976d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      xrb->map_w = w;
40076d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      xrb->map_h = h;
40176d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
40276d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      stride = w * cpp;
403becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul      xrb->Base.Buffer = malloc(h * stride);
40476d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
40576d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      sPriv->swrast_loader->getImage(dPriv, x, y, w, h,
406becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul				     (char *) xrb->Base.Buffer,
40776d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt				     dPriv->loaderPrivate);
40876d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
409becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul      *out_map = xrb->Base.Buffer;
41076d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      *out_stride = stride;
41176d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      return;
41276d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt   }
41376d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
414becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul   ASSERT(xrb->Base.Buffer);
41576d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
41676d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt   if (rb->AllocStorage == swrast_alloc_back_storage) {
41776d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      map += (rb->Height - 1) * stride;
41876d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      stride = -stride;
41976d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt   }
42076d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
421a4c952f36f0c6b55f1410bc678b21f75de253a74Mathias Fröhlich   map += (GLsizei)y * stride;
422a4c952f36f0c6b55f1410bc678b21f75de253a74Mathias Fröhlich   map += (GLsizei)x * cpp;
42376d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
42476d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt   *out_map = map;
42576d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt   *out_stride = stride;
42676d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt}
42776d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
42876d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholtstatic void
42976d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholtswrast_unmap_renderbuffer(struct gl_context *ctx,
43076d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt			  struct gl_renderbuffer *rb)
43176d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt{
4321048d55d5fa60d17dd99ac7394ff0572500625f9Brian Paul   struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
43376d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
43476d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt   if (rb->AllocStorage == swrast_alloc_front_storage) {
43576d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      __DRIdrawable *dPriv = xrb->dPriv;
43676d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      __DRIscreen *sPriv = dPriv->driScreenPriv;
43776d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
43876d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      if (xrb->map_mode & GL_MAP_WRITE_BIT) {
43976d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt	 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_DRAW,
44076d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt					xrb->map_x, xrb->map_y,
44176d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt					xrb->map_w, xrb->map_h,
442becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul					(char *) xrb->Base.Buffer,
44376d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt					dPriv->loaderPrivate);
44476d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt      }
44576d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
446becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul      free(xrb->Base.Buffer);
447becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul      xrb->Base.Buffer = NULL;
44876d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt   }
44976d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt}
45076d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt
4516e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisstatic GLboolean
4526e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisdri_create_buffer(__DRIscreen * sPriv,
4536e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis		  __DRIdrawable * dPriv,
454d3491e775fb07f891463b2185d74bbad62f3ed24Kristian Høgsberg		  const struct gl_config * visual, GLboolean isPixmap)
455280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
456c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    struct dri_drawable *drawable = NULL;
45731aca27c08d6a385c595d34fe4ee06390bf5b0e8Kristian Høgsberg    struct gl_framebuffer *fb;
4581048d55d5fa60d17dd99ac7394ff0572500625f9Brian Paul    struct dri_swrast_renderbuffer *frontrb, *backrb;
459280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
460280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
461280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
4622d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick    (void) sPriv;
4632d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick    (void) isPixmap;
4642d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick
465c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    drawable = CALLOC_STRUCT(dri_drawable);
466c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    if (drawable == NULL)
467c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis	goto drawable_fail;
468280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
469c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    dPriv->driverPrivate = drawable;
470c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    drawable->dPriv = dPriv;
471c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis
472df22829b9c984a826a0b2e39dd6badac2029996dBrian Paul    drawable->row = malloc(SWRAST_MAX_WIDTH * 4);
473c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    if (drawable->row == NULL)
474c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis	goto drawable_fail;
475c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis
476c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    fb = &drawable->Base;
477280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
478280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    /* basic framebuffer setup */
4796e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    _mesa_initialize_window_framebuffer(fb, visual);
480280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
481280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    /* add front renderbuffer */
48276d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt    frontrb = swrast_new_renderbuffer(visual, dPriv, GL_TRUE);
483becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul    _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontrb->Base.Base);
484280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
485280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    /* add back renderbuffer */
4866e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    if (visual->doubleBufferMode) {
48776d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt	backrb = swrast_new_renderbuffer(visual, dPriv, GL_FALSE);
488becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul	_mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backrb->Base.Base);
489280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    }
490280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
491280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    /* add software renderbuffers */
492d0dc75c000d5af92648c7de901756400672b8447Brian Paul    _swrast_add_soft_renderbuffers(fb,
493d0dc75c000d5af92648c7de901756400672b8447Brian Paul                                   GL_FALSE, /* color */
494d0dc75c000d5af92648c7de901756400672b8447Brian Paul                                   visual->haveDepthBuffer,
495d0dc75c000d5af92648c7de901756400672b8447Brian Paul                                   visual->haveStencilBuffer,
496d0dc75c000d5af92648c7de901756400672b8447Brian Paul                                   visual->haveAccumBuffer,
497d0dc75c000d5af92648c7de901756400672b8447Brian Paul                                   GL_FALSE, /* alpha */
498d0dc75c000d5af92648c7de901756400672b8447Brian Paul                                   GL_FALSE /* aux bufs */);
499280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
5006e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    return GL_TRUE;
501c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis
502c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzisdrawable_fail:
503c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis
504c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    if (drawable)
505c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis	free(drawable->row);
506c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis
507c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    FREE(drawable);
508c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis
509c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    return GL_FALSE;
510280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
511280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
512280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic void
5136e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisdri_destroy_buffer(__DRIdrawable * dPriv)
514280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
515280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
516280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
5176e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    if (dPriv) {
518c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis	struct dri_drawable *drawable = dri_drawable(dPriv);
51931aca27c08d6a385c595d34fe4ee06390bf5b0e8Kristian Høgsberg	struct gl_framebuffer *fb;
5206e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis
521c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis	free(drawable->row);
522280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
523c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis	fb = &drawable->Base;
524280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
525280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	fb->DeletePending = GL_TRUE;
5260f04a1d3f8989b0a391e6dad80abf06ce151d1f1Brian Paul	_mesa_reference_framebuffer(&fb, NULL);
527280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    }
528280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
529280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
5306e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisstatic void
5316e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisdri_swap_buffers(__DRIdrawable * dPriv)
532280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
5336e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    __DRIscreen *sPriv = dPriv->driScreenPriv;
534280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
5356e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    GET_CURRENT_CONTEXT(ctx);
536280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
537c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    struct dri_drawable *drawable = dri_drawable(dPriv);
53831aca27c08d6a385c595d34fe4ee06390bf5b0e8Kristian Høgsberg    struct gl_framebuffer *fb;
5391048d55d5fa60d17dd99ac7394ff0572500625f9Brian Paul    struct dri_swrast_renderbuffer *frontrb, *backrb;
540280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
541280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
542280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
543c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    fb = &drawable->Base;
5446e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis
545f4e561ce127cf484d7c76c29b8cd026c9ad5cebcGeorge Sapountzis    frontrb =
5461048d55d5fa60d17dd99ac7394ff0572500625f9Brian Paul	dri_swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
547f4e561ce127cf484d7c76c29b8cd026c9ad5cebcGeorge Sapountzis    backrb =
5481048d55d5fa60d17dd99ac7394ff0572500625f9Brian Paul	dri_swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
5496e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis
550280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    /* check for signle-buffered */
551280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    if (backrb == NULL)
552280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	return;
553280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
554280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    /* check if swapping currently bound buffer */
5556e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    if (ctx && ctx->DrawBuffer == fb) {
556280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	/* flush pending rendering */
557280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	_mesa_notifySwapBuffers(ctx);
558280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    }
559280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
5606e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
5616e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis				   0, 0,
562becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul				   frontrb->Base.Base.Width,
563becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul				   frontrb->Base.Base.Height,
564becbb643135ddccac5054bf138ca0cc7cc3fff15Brian Paul				   (char *) backrb->Base.Buffer,
5656e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis				   dPriv->loaderPrivate);
566280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
567280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
568280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
569280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis/**
57095eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis * General device driver functions.
571280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis */
572280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
573280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic void
57431aca27c08d6a385c595d34fe4ee06390bf5b0e8Kristian Høgsbergget_window_size( struct gl_framebuffer *fb, GLsizei *w, GLsizei *h )
575280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
576c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis    __DRIdrawable *dPriv = swrast_drawable(fb)->dPriv;
5776e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    __DRIscreen *sPriv = dPriv->driScreenPriv;
578280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    int x, y;
579280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
5806e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    sPriv->swrast_loader->getDrawableInfo(dPriv,
5816e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis					  &x, &y, w, h,
5826e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis					  dPriv->loaderPrivate);
583280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
584280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
585280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic void
586f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergswrast_check_and_update_window_size( struct gl_context *ctx, struct gl_framebuffer *fb )
587280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
588280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    GLsizei width, height;
589280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
590280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    get_window_size(fb, &width, &height);
591280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    if (fb->Width != width || fb->Height != height) {
592280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	_mesa_resize_framebuffer(ctx, fb, width, height);
593280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    }
594280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
595280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
596280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic const GLubyte *
597f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergget_string(struct gl_context *ctx, GLenum pname)
598280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
599280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    (void) ctx;
600280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    switch (pname) {
601280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	case GL_VENDOR:
602280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	    return (const GLubyte *) "Mesa Project";
603280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	case GL_RENDERER:
60495eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis	    return (const GLubyte *) "Software Rasterizer";
605280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	default:
606280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	    return NULL;
607280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    }
608280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
609280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
610280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic void
611f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergupdate_state( struct gl_context *ctx, GLuint new_state )
612280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
613280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    /* not much to do here - pass it on */
614280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    _swrast_InvalidateState( ctx, new_state );
615280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    _swsetup_InvalidateState( ctx, new_state );
616280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    _vbo_InvalidateState( ctx, new_state );
617280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    _tnl_InvalidateState( ctx, new_state );
618280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
619280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
620280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic void
621f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergviewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
622280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
62331aca27c08d6a385c595d34fe4ee06390bf5b0e8Kristian Høgsberg    struct gl_framebuffer *draw = ctx->WinSysDrawBuffer;
62431aca27c08d6a385c595d34fe4ee06390bf5b0e8Kristian Høgsberg    struct gl_framebuffer *read = ctx->WinSysReadBuffer;
625280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
6262d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick    (void) x;
6272d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick    (void) y;
6282d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick    (void) w;
6292d4b8e296f7f60e704bfcdec79c86cc1c013d7b9Ian Romanick    (void) h;
630280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    swrast_check_and_update_window_size(ctx, draw);
631280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    swrast_check_and_update_window_size(ctx, read);
632280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
633280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
634f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergstatic gl_format swrastChooseTextureFormat(struct gl_context * ctx,
635d47a6ada9ca9670c60fc141fabadf40c63031c08Brian Paul                                           GLenum target,
6368d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie					   GLint internalFormat,
6378d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie					   GLenum format,
6388d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie					   GLenum type)
6398d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie{
6408d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    if (internalFormat == GL_RGB)
6418d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie	return MESA_FORMAT_XRGB8888;
642d47a6ada9ca9670c60fc141fabadf40c63031c08Brian Paul    return _mesa_choose_tex_format(ctx, target, internalFormat, format, type);
6438d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie}
6448d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie
645280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic void
646280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisswrast_init_driver_functions(struct dd_function_table *driver)
647280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
648280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    driver->GetString = get_string;
649280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    driver->UpdateState = update_state;
650280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    driver->GetBufferSize = NULL;
651280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    driver->Viewport = viewport;
6528d789be03430b80e0ba2fef19d56dd0b8e699ea2Dave Airlie    driver->ChooseTextureFormat = swrastChooseTextureFormat;
65376d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt    driver->MapRenderbuffer = swrast_map_renderbuffer;
65476d1275474d7b5ac2f0edf5856ae1c5fc234f0d9Eric Anholt    driver->UnmapRenderbuffer = swrast_unmap_renderbuffer;
655280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
656280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
657b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Fengstatic const char *es2_extensions[] = {
658b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   /* Used by mesa internally (cf all_mesa_extensions in ../common/utils.c) */
659b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_ARB_transpose_matrix",
660b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_ARB_window_pos",
661b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_EXT_blend_func_separate",
662b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_EXT_compiled_vertex_array",
663b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_EXT_framebuffer_blit",
664b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_IBM_multimode_draw_arrays",
665b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_MESA_window_pos",
666b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_NV_vertex_program",
667b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng
668b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   /* Required by GLES2 */
669b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_ARB_fragment_program",
670b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_ARB_fragment_shader",
671b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_ARB_shader_objects",
672b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_ARB_texture_cube_map",
673b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_ARB_texture_non_power_of_two",
674b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_ARB_vertex_shader",
675b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_EXT_blend_color",
676b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_EXT_blend_equation_separate",
677b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_EXT_blend_minmax",
678b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng
679b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   /* Optional GLES2 */
680b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_ARB_framebuffer_object",
681b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_EXT_texture_filter_anisotropic",
682b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_ARB_depth_texture",
683b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_EXT_packed_depth_stencil",
684b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   "GL_EXT_framebuffer_object",
685b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   NULL,
686b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng};
687b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng
688b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Fengstatic void
689b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao FengInitExtensionsES2(struct gl_context *ctx)
690b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng{
691b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   int i;
692b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng
693b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng   for (i = 0; es2_extensions[i]; i++)
694b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng      _mesa_enable_extension(ctx, es2_extensions[i]);
695b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng}
696280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
697280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis/**
69895eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis * Context-related functions.
699280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis */
700280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
7016e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisstatic GLboolean
7029ab3df4ebbcb5fe7a65e6cf05b3a6671392ba929Kristian Høgsbergdri_create_context(gl_api api,
703d3491e775fb07f891463b2185d74bbad62f3ed24Kristian Høgsberg		   const struct gl_config * visual,
704e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick		   __DRIcontext * cPriv,
705e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick		   unsigned major_version,
706e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick		   unsigned minor_version,
707e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick		   uint32_t flags,
708e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick		   unsigned *error,
709e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick		   void *sharedContextPrivate)
710280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
711cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis    struct dri_context *ctx = NULL;
712cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis    struct dri_context *share = (struct dri_context *)sharedContextPrivate;
713f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg    struct gl_context *mesaCtx = NULL;
714f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg    struct gl_context *sharedCtx = NULL;
715280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    struct dd_function_table functions;
716280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
717280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
718280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
719e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick    /* Flag filtering is handled in dri2CreateContextAttribs.
720e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick     */
721e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick    (void) flags;
722e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick
72370f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick    switch (api) {
72470f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick    case API_OPENGL:
72570f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick        if (major_version > 2
72670f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick	    || (major_version == 2 && minor_version > 1)) {
72770f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick            *error = __DRI_CTX_ERROR_BAD_VERSION;
72870f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick            return GL_FALSE;
72970f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick        }
73070f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick        break;
73170f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick    case API_OPENGLES:
73270f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick    case API_OPENGLES2:
73370f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick        break;
73470f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick    case API_OPENGL_CORE:
73570f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick        *error = __DRI_CTX_ERROR_BAD_API;
73670f47505a2e5d4cf949b7c2650f3d9f6559bacb3Ian Romanick        return GL_FALSE;
737e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick    }
738e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick
739cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis    ctx = CALLOC_STRUCT(dri_context);
740e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick    if (ctx == NULL) {
741e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick	*error = __DRI_CTX_ERROR_NO_MEMORY;
742cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis	goto context_fail;
743e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick    }
744cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis
745cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis    cPriv->driverPrivate = ctx;
746cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis    ctx->cPriv = cPriv;
747cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis
748280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    /* build table of device driver functions */
749280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    _mesa_init_driver_functions(&functions);
750280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    swrast_init_driver_functions(&functions);
751280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
752cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis    if (share) {
753cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis	sharedCtx = &share->Base;
754cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis    }
755cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis
756cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis    mesaCtx = &ctx->Base;
7576e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis
7586e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    /* basic context setup */
7595e4ca1ccc9029fd75bb7676dc128e33d12da9665Brian Paul    if (!_mesa_initialize_context(mesaCtx, api, visual, sharedCtx, &functions, (void *) cPriv)) {
760e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick	*error = __DRI_CTX_ERROR_NO_MEMORY;
761cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis	goto context_fail;
762280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    }
763280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
764280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    /* do bounds checking to prevent segfaults and server crashes! */
765280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    mesaCtx->Const.CheckArrayBounds = GL_TRUE;
766280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
767280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    /* create module contexts */
768280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    _swrast_CreateContext( mesaCtx );
769280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    _vbo_CreateContext( mesaCtx );
770280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    _tnl_CreateContext( mesaCtx );
771280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    _swsetup_CreateContext( mesaCtx );
772280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    _swsetup_Wakeup( mesaCtx );
773280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
774280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    /* use default TCL pipeline */
775280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    {
776280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis       TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
777280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis       tnl->Driver.RunPipeline = _tnl_run_pipeline;
778280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    }
779280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
7804a4914e4146b78e99277ab494226136a4e68cdb4Brian Paul    _mesa_meta_init(mesaCtx);
781b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng    _mesa_enable_sw_extensions(mesaCtx);
7824a4914e4146b78e99277ab494226136a4e68cdb4Brian Paul
783b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng    switch (api) {
784ecac178aa2591afc36e5d34d11a773ff07cb71b1Brian Paul    case API_OPENGL_CORE:
785ecac178aa2591afc36e5d34d11a773ff07cb71b1Brian Paul        /* XXX fix me, fall-through for now */
786b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng    case API_OPENGL:
787b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        _mesa_enable_1_3_extensions(mesaCtx);
788b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        _mesa_enable_1_4_extensions(mesaCtx);
789b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        _mesa_enable_1_5_extensions(mesaCtx);
790b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        _mesa_enable_2_0_extensions(mesaCtx);
791b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        _mesa_enable_2_1_extensions(mesaCtx);
792b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        break;
793b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng    case API_OPENGLES:
794b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        _mesa_enable_1_3_extensions(mesaCtx);
795b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        _mesa_enable_1_4_extensions(mesaCtx);
796b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        _mesa_enable_1_5_extensions(mesaCtx);
797b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng
798b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        break;
799b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng    case API_OPENGLES2:
800b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        InitExtensionsES2( mesaCtx);
801b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng        break;
802b43a147128a1144a5fe4afcda412ccaba0c0793bHaitao Feng    }
8036e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis
804e532b6288f01b63d8d8ba8c8dc08292967e65490Ian Romanick    *error = __DRI_CTX_ERROR_SUCCESS;
8056e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    return GL_TRUE;
806cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis
807cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountziscontext_fail:
808cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis
809cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis    FREE(ctx);
810cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis
811cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis    return GL_FALSE;
812280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
813280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
814280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzisstatic void
8156e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisdri_destroy_context(__DRIcontext * cPriv)
816280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
817280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
818280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
8196e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    if (cPriv) {
820cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis	struct dri_context *ctx = dri_context(cPriv);
821f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg	struct gl_context *mesaCtx;
822cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis
823cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis	mesaCtx = &ctx->Base;
8246e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis
8254a4914e4146b78e99277ab494226136a4e68cdb4Brian Paul        _mesa_meta_free(mesaCtx);
826280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	_swsetup_DestroyContext( mesaCtx );
827280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	_swrast_DestroyContext( mesaCtx );
828280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	_tnl_DestroyContext( mesaCtx );
829280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	_vbo_DestroyContext( mesaCtx );
830280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	_mesa_destroy_context( mesaCtx );
831280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    }
832280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
833280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
8346e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisstatic GLboolean
8356e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisdri_make_current(__DRIcontext * cPriv,
8366e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis		 __DRIdrawable * driDrawPriv,
8376e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis		 __DRIdrawable * driReadPriv)
838280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
839f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg    struct gl_context *mesaCtx;
84031aca27c08d6a385c595d34fe4ee06390bf5b0e8Kristian Høgsberg    struct gl_framebuffer *mesaDraw;
84131aca27c08d6a385c595d34fe4ee06390bf5b0e8Kristian Høgsberg    struct gl_framebuffer *mesaRead;
842280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
843280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
8446e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    if (cPriv) {
845cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis	struct dri_context *ctx = dri_context(cPriv);
8468c456775887e192dd7464b94295f4410045c7c07Vinson Lee	struct dri_drawable *draw;
8478c456775887e192dd7464b94295f4410045c7c07Vinson Lee	struct dri_drawable *read;
848cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis
8496e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis	if (!driDrawPriv || !driReadPriv)
850280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	    return GL_FALSE;
851280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
8528c456775887e192dd7464b94295f4410045c7c07Vinson Lee	draw = dri_drawable(driDrawPriv);
8538c456775887e192dd7464b94295f4410045c7c07Vinson Lee	read = dri_drawable(driReadPriv);
854cf8a1caa231b748d3ba7c776ab076ad3de99e963George Sapountzis	mesaCtx = &ctx->Base;
855c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis	mesaDraw = &draw->Base;
856c1bde793598bcb5d5f8744c290a66ea6586eb29fGeorge Sapountzis	mesaRead = &read->Base;
85795eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis
85895eb6422c718d3d4ef34ec35bffa83307025793dGeorge Sapountzis	/* check for same context and buffer */
859280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	if (mesaCtx == _mesa_get_current_context()
860280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	    && mesaCtx->DrawBuffer == mesaDraw
861280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	    && mesaCtx->ReadBuffer == mesaRead) {
862280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	    return GL_TRUE;
863280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	}
864280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
865280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	_glapi_check_multithread();
866280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
867280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	swrast_check_and_update_window_size(mesaCtx, mesaDraw);
8686e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis	if (mesaRead != mesaDraw)
869280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	    swrast_check_and_update_window_size(mesaCtx, mesaRead);
870280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
871280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	_mesa_make_current( mesaCtx,
872280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis			    mesaDraw,
873280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis			    mesaRead );
874280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    }
875280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    else {
876280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	/* unbind */
877280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis	_mesa_make_current( NULL, NULL, NULL );
878280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    }
879280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
880280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    return GL_TRUE;
881280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
882280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
8836e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisstatic GLboolean
8846e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisdri_unbind_context(__DRIcontext * cPriv)
885280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis{
886280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    TRACE;
8876e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    (void) cPriv;
888b4bb6680200b5a898583392f4c831c02f41e63f7Kristian Høgsberg
889b4bb6680200b5a898583392f4c831c02f41e63f7Kristian Høgsberg    /* Unset current context and dispath table */
890b4bb6680200b5a898583392f4c831c02f41e63f7Kristian Høgsberg    _mesa_make_current(NULL, NULL, NULL);
891b4bb6680200b5a898583392f4c831c02f41e63f7Kristian Høgsberg
892280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    return GL_TRUE;
893280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis}
894280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
895280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
8966e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzisconst struct __DriverAPIRec driDriverAPI = {
8976e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    .InitScreen = dri_init_screen,
8986e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    .DestroyScreen = dri_destroy_screen,
8996e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    .CreateContext = dri_create_context,
9006e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    .DestroyContext = dri_destroy_context,
9016e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    .CreateBuffer = dri_create_buffer,
9026e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    .DestroyBuffer = dri_destroy_buffer,
9036e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    .SwapBuffers = dri_swap_buffers,
9046e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    .MakeCurrent = dri_make_current,
9056e376485c10896229f7bfaf5b0cce9c8b67f61b1George Sapountzis    .UnbindContext = dri_unbind_context,
906280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis};
907280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis
908280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis/* This is the table of extensions that the loader will dlsym() for. */
909280bf89bd4a6bb7977abb00b88d59234d2c80844George SapountzisPUBLIC const __DRIextension *__driDriverExtensions[] = {
910280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    &driCoreExtension.base,
911280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    &driSWRastExtension.base,
912280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis    NULL
913280bf89bd4a6bb7977abb00b88d59234d2c80844George Sapountzis};
914