glxcmds.c 74.3 KB
Newer Older
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1
/*
Adam Jackson's avatar
Adam Jackson committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice including the dates of first publication and
 * either this permission notice or a reference to
 * http://oss.sgi.com/projects/FreeB/
 * shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Except as contained in this notice, the name of Silicon Graphics, Inc.
 * shall not be used in advertising or otherwise to promote the sale, use or
 * other dealings in this Software without prior written authorization from
 * Silicon Graphics, Inc.
 */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
30

31 32 33 34
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

35
#include <string.h>
36
#include <assert.h>
37

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
38 39 40 41 42 43 44
#include "glxserver.h"
#include <GL/glxtokens.h>
#include <unpack.h>
#include <pixmapstr.h>
#include <windowstr.h>
#include "glxutil.h"
#include "glxext.h"
45
#include "indirect_dispatch.h"
46 47
#include "indirect_table.h"
#include "indirect_util.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
48

49 50
static char GLXServerVendorName[] = "SGI";

51
_X_HIDDEN int
52 53
validGlxScreen(ClientPtr client, int screen, __GLXscreen ** pGlxScreen,
               int *err)
54 55
{
    /*
56 57
     ** Check if screen exists.
     */
58
    if (screen < 0 || screen >= screenInfo.numScreens) {
59 60 61
        client->errorValue = screen;
        *err = BadValue;
        return FALSE;
62 63
    }
    *pGlxScreen = glxGetScreen(screenInfo.screens[screen]);
64

65 66 67
    return TRUE;
}

68
_X_HIDDEN int
69 70
validGlxFBConfig(ClientPtr client, __GLXscreen * pGlxScreen, XID id,
                 __GLXconfig ** config, int *err)
71
{
72
    __GLXconfig *m;
73 74

    for (m = pGlxScreen->fbconfigs; m != NULL; m = m->next)
75 76 77 78
        if (m->fbconfigID == id) {
            *config = m;
            return TRUE;
        }
79 80 81 82 83 84 85 86

    client->errorValue = id;
    *err = __glXError(GLXBadFBConfig);

    return FALSE;
}

static int
87 88
validGlxVisual(ClientPtr client, __GLXscreen * pGlxScreen, XID id,
               __GLXconfig ** config, int *err)
89 90 91 92
{
    int i;

    for (i = 0; i < pGlxScreen->numVisuals; i++)
93 94 95 96
        if (pGlxScreen->visuals[i]->visualID == id) {
            *config = pGlxScreen->visuals[i];
            return TRUE;
        }
97 98 99 100 101 102 103 104

    client->errorValue = id;
    *err = BadValue;

    return FALSE;
}

static int
105 106
validGlxFBConfigForWindow(ClientPtr client, __GLXconfig * config,
                          DrawablePtr pDraw, int *err)
107 108 109 110 111 112
{
    ScreenPtr pScreen = pDraw->pScreen;
    VisualPtr pVisual = NULL;
    XID vid;
    int i;

113
    vid = wVisual((WindowPtr) pDraw);
114
    for (i = 0; i < pScreen->numVisuals; i++) {
115 116 117 118
        if (pScreen->visuals[i].vid == vid) {
            pVisual = &pScreen->visuals[i];
            break;
        }
119 120 121
    }

    /* FIXME: What exactly should we check here... */
122
    if (pVisual->class != glxConvertToXVisualType(config->visualType) ||
123 124 125 126
        !(config->drawableType & GLX_WINDOW_BIT)) {
        client->errorValue = pDraw->id;
        *err = BadMatch;
        return FALSE;
127 128 129 130
    }

    return TRUE;
}
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
131

132
_X_HIDDEN int
133
validGlxContext(ClientPtr client, XID id, int access_mode,
134
                __GLXcontext ** context, int *err)
135
{
136
    *err = dixLookupResourceByType((void **) context, id,
137
                                   __glXContextRes, client, access_mode);
Adam Jackson's avatar
Adam Jackson committed
138
    if (*err != Success || (*context)->idExists == GL_FALSE) {
139 140 141 142
        client->errorValue = id;
        if (*err == BadValue || *err == Success)
            *err = __glXError(GLXBadContext);
        return FALSE;
143 144 145 146 147
    }

    return TRUE;
}

148 149
static int
validGlxDrawable(ClientPtr client, XID id, int type, int access_mode,
150
                 __GLXdrawable ** drawable, int *err)
151 152 153
{
    int rc;

154
    rc = dixLookupResourceByType((void **) drawable, id,
155
                                 __glXDrawableRes, client, access_mode);
156
    if (rc != Success && rc != BadValue) {
157 158 159
        *err = rc;
        client->errorValue = id;
        return FALSE;
160 161
    }

162 163 164
    /* If the ID of the glx drawable we looked up doesn't match the id
     * we looked for, it's because we looked it up under the X
     * drawable ID (see DoCreateGLXDrawable). */
165
    if (rc == BadValue ||
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        (*drawable)->drawId != id ||
        (type != GLX_DRAWABLE_ANY && type != (*drawable)->type)) {
        client->errorValue = id;
        switch (type) {
        case GLX_DRAWABLE_WINDOW:
            *err = __glXError(GLXBadWindow);
            return FALSE;
        case GLX_DRAWABLE_PIXMAP:
            *err = __glXError(GLXBadPixmap);
            return FALSE;
        case GLX_DRAWABLE_PBUFFER:
            *err = __glXError(GLXBadPbuffer);
            return FALSE;
        case GLX_DRAWABLE_ANY:
            *err = __glXError(GLXBadDrawable);
            return FALSE;
        }
183 184 185 186 187
    }

    return TRUE;
}

188
void
189
__glXContextDestroy(__GLXcontext * context)
190
{
191
    lastGLContext = NULL;
192 193
}

194 195
static void
__glXdirectContextDestroy(__GLXcontext * context)
196
{
197
    __glXContextDestroy(context);
198
    free(context);
199 200
}

201
_X_HIDDEN __GLXcontext *
202 203
__glXdirectContextCreate(__GLXscreen * screen,
                         __GLXconfig * modes, __GLXcontext * shareContext)
204 205 206
{
    __GLXcontext *context;

207
    context = calloc(1, sizeof(__GLXcontext));
208
    if (context == NULL)
209
        return NULL;
210 211 212 213 214 215

    context->destroy = __glXdirectContextDestroy;

    return context;
}

Eric Anholt's avatar
Eric Anholt committed
216 217 218 219 220 221 222 223
/**
 * Create a GL context with the given properties.  This routine is used
 * to implement \c glXCreateContext, \c glXCreateNewContext, and
 * \c glXCreateContextWithConfigSGIX.  This works becuase of the hack way
 * that GLXFBConfigs are implemented.  Basically, the FBConfigID is the
 * same as the VisualID.
 */

Kristian Høgsberg's avatar
Kristian Høgsberg committed
224
static int
225 226 227
DoCreateContext(__GLXclientState * cl, GLXContextID gcId,
                GLXContextID shareList, __GLXconfig * config,
                __GLXscreen * pGlxScreen, GLboolean isDirect)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
228 229 230
{
    ClientPtr client = cl->client;
    __GLXcontext *glxc, *shareglxc;
231
    int err;
232

233
    LEGAL_NEW_RESOURCE(gcId, client);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
234 235

    /*
Peter Hutterer's avatar
Peter Hutterer committed
236
     ** Find the display list space that we want to share.
237 238
     **
     ** NOTE: In a multithreaded X server, we would need to keep a reference
Peter Hutterer's avatar
Peter Hutterer committed
239 240 241 242
     ** count for each display list so that if one client detroyed a list that
     ** another client was using, the list would not really be freed until it
     ** was no longer in use.  Since this sample implementation has no support
     ** for multithreaded servers, we don't do this.
243
     */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
244
    if (shareList == None) {
245 246 247 248 249 250 251
        shareglxc = 0;
    }
    else {
        if (!validGlxContext(client, shareList, DixReadAccess,
                             &shareglxc, &err))
            return err;

252 253 254 255 256 257 258 259 260 261
        /* Page 26 (page 32 of the PDF) of the GLX 1.4 spec says:
         *
         *     "The server context state for all sharing contexts must exist
         *     in a single address space or a BadMatch error is generated."
         *
         * If the share context is indirect, force the new context to also be
         * indirect.  If the shard context is direct but the new context
         * cannot be direct, generate BadMatch.
         */
        if (shareglxc->isDirect && !isDirect) {
262 263 264
            client->errorValue = shareList;
            return BadMatch;
        }
265
        else if (!shareglxc->isDirect) {
266 267 268 269 270 271
            /*
             ** Create an indirect context regardless of what the client asked
             ** for; this way we can share display list space with shareList.
             */
            isDirect = GL_FALSE;
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
272 273 274
    }

    /*
275 276
     ** Allocate memory for the new context
     */
277
    if (!isDirect) {
278 279 280 281 282 283 284 285 286 287 288
        /* Only allow creating indirect GLX contexts if allowed by
         * server command line.  Indirect GLX is of limited use (since
         * it's only GL 1.4), it's slower than direct contexts, and
         * it's a massive attack surface for buffer overflow type
         * errors.
         */
        if (!enableIndirectGLX) {
            client->errorValue = isDirect;
            return BadValue;
        }

289 290 291 292 293 294 295
        /* Without any attributes, the only error that the driver should be
         * able to generate is BadAlloc.  As result, just drop the error
         * returned from the driver on the floor.
         */
        glxc = pGlxScreen->createContext(pGlxScreen, config, shareglxc,
                                         0, NULL, &err);
    }
296
    else
297
        glxc = __glXdirectContextCreate(pGlxScreen, config, shareglxc);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
298
    if (!glxc) {
299
        return BadAlloc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
300 301
    }

302
    /* Initialize the GLXcontext structure.
303
     */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
304
    glxc->pGlxScreen = pGlxScreen;
305
    glxc->config = config;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
306 307 308
    glxc->id = gcId;
    glxc->share_id = shareList;
    glxc->idExists = GL_TRUE;
309
    glxc->currentClient = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
310
    glxc->isDirect = isDirect;
311
    glxc->hasUnflushedCommands = GL_FALSE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
312
    glxc->renderMode = GL_RENDER;
313 314 315 316 317 318
    glxc->feedbackBuf = NULL;
    glxc->feedbackBufSize = 0;
    glxc->selectBuf = NULL;
    glxc->selectBufSize = 0;
    glxc->drawPriv = NULL;
    glxc->readPriv = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
319

320 321 322 323 324 325 326 327 328 329
    /* The GLX_ARB_create_context_robustness spec says:
     *
     *     "The default value for GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB
     *     is GLX_NO_RESET_NOTIFICATION_ARB."
     *
     * Without using glXCreateContextAttribsARB, there is no way to specify a
     * non-default reset notification strategy.
     */
    glxc->resetNotificationStrategy = GLX_NO_RESET_NOTIFICATION_ARB;

330
    /* Add the new context to the various global tables of GLX contexts.
331
     */
332
    if (!__glXAddContext(glxc)) {
333 334 335 336 337
        (*glxc->destroy) (glxc);
        client->errorValue = gcId;
        return BadAlloc;
    }

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
338 339 340
    return Success;
}

341 342
int
__glXDisp_CreateContext(__GLXclientState * cl, GLbyte * pc)
Eric Anholt's avatar
Eric Anholt committed
343
{
Julien Cristau's avatar
Julien Cristau committed
344
    ClientPtr client = cl->client;
Eric Anholt's avatar
Eric Anholt committed
345
    xGLXCreateContextReq *req = (xGLXCreateContextReq *) pc;
346
    __GLXconfig *config;
347 348
    __GLXscreen *pGlxScreen;
    int err;
Eric Anholt's avatar
Eric Anholt committed
349

Julien Cristau's avatar
Julien Cristau committed
350 351
    REQUEST_SIZE_MATCH(xGLXCreateContextReq);

352
    if (!validGlxScreen(cl->client, req->screen, &pGlxScreen, &err))
353
        return err;
354
    if (!validGlxVisual(cl->client, pGlxScreen, req->visual, &config, &err))
355
        return err;
356 357

    return DoCreateContext(cl, req->context, req->shareList,
358
                           config, pGlxScreen, req->isDirect);
359
}
Eric Anholt's avatar
Eric Anholt committed
360

361 362
int
__glXDisp_CreateNewContext(__GLXclientState * cl, GLbyte * pc)
Eric Anholt's avatar
Eric Anholt committed
363
{
Julien Cristau's avatar
Julien Cristau committed
364
    ClientPtr client = cl->client;
Eric Anholt's avatar
Eric Anholt committed
365
    xGLXCreateNewContextReq *req = (xGLXCreateNewContextReq *) pc;
366
    __GLXconfig *config;
367 368 369
    __GLXscreen *pGlxScreen;
    int err;

Julien Cristau's avatar
Julien Cristau committed
370 371
    REQUEST_SIZE_MATCH(xGLXCreateNewContextReq);

372
    if (!validGlxScreen(cl->client, req->screen, &pGlxScreen, &err))
373
        return err;
374
    if (!validGlxFBConfig(cl->client, pGlxScreen, req->fbconfig, &config, &err))
375
        return err;
Eric Anholt's avatar
Eric Anholt committed
376

377
    return DoCreateContext(cl, req->context, req->shareList,
378
                           config, pGlxScreen, req->isDirect);
379
}
Eric Anholt's avatar
Eric Anholt committed
380

381 382
int
__glXDisp_CreateContextWithConfigSGIX(__GLXclientState * cl, GLbyte * pc)
Eric Anholt's avatar
Eric Anholt committed
383
{
Julien Cristau's avatar
Julien Cristau committed
384
    ClientPtr client = cl->client;
385 386
    xGLXCreateContextWithConfigSGIXReq *req =
        (xGLXCreateContextWithConfigSGIXReq *) pc;
387
    __GLXconfig *config;
388 389
    __GLXscreen *pGlxScreen;
    int err;
Eric Anholt's avatar
Eric Anholt committed
390

Julien Cristau's avatar
Julien Cristau committed
391 392
    REQUEST_SIZE_MATCH(xGLXCreateContextWithConfigSGIXReq);

393
    if (!validGlxScreen(cl->client, req->screen, &pGlxScreen, &err))
394
        return err;
395
    if (!validGlxFBConfig(cl->client, pGlxScreen, req->fbconfig, &config, &err))
396
        return err;
397 398

    return DoCreateContext(cl, req->context, req->shareList,
399
                           config, pGlxScreen, req->isDirect);
400
}
Adam Jackson's avatar
Adam Jackson committed
401

402 403
int
__glXDisp_DestroyContext(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
404
{
Julien Cristau's avatar
Julien Cristau committed
405
    ClientPtr client = cl->client;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
406 407
    xGLXDestroyContextReq *req = (xGLXDestroyContextReq *) pc;
    __GLXcontext *glxc;
408 409
    int err;

Julien Cristau's avatar
Julien Cristau committed
410 411
    REQUEST_SIZE_MATCH(xGLXDestroyContextReq);

412
    if (!validGlxContext(cl->client, req->context, DixDestroyAccess,
413 414
                         &glxc, &err))
        return err;
415

416 417 418
    glxc->idExists = GL_FALSE;
    if (!glxc->currentClient)
        FreeResourceByType(req->context, __glXContextRes, FALSE);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
419

Adam Jackson's avatar
Adam Jackson committed
420
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
421 422 423
}

/*
Adam Jackson's avatar
Adam Jackson committed
424 425 426 427 428 429 430 431
 * This will return "deleted" contexts, ie, where idExists is GL_FALSE.
 * Contrast validGlxContext, which will not.  We're cheating here and
 * using the XID as the context tag, which is fine as long as we defer
 * actually destroying the context until it's no longer referenced, and
 * block clients from trying to MakeCurrent on contexts that are on the
 * way to destruction.  Notice that DoMakeCurrent calls validGlxContext
 * for new contexts but __glXLookupContextByTag for previous contexts.
 */
432 433
__GLXcontext *
__glXLookupContextByTag(__GLXclientState * cl, GLXContextTag tag)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
434
{
Adam Jackson's avatar
Adam Jackson committed
435
    __GLXcontext *ret;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
436

437
    if (dixLookupResourceByType((void **) &ret, tag, __glXContextRes,
Adam Jackson's avatar
Adam Jackson committed
438 439 440 441
                                cl->client, DixUseAccess) == Success)
        return ret;

    return NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
442 443 444 445
}

/*****************************************************************************/

446 447
static void
StopUsingContext(__GLXcontext * glxc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
448 449
{
    if (glxc) {
450
        glxc->currentClient = NULL;
451
        if (!glxc->idExists) {
Adam Jackson's avatar
Adam Jackson committed
452
            FreeResourceByType(glxc->id, __glXContextRes, FALSE);
453
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
454 455 456
    }
}

457 458
static void
StartUsingContext(__GLXclientState * cl, __GLXcontext * glxc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
459
{
460
    glxc->currentClient = cl->client;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
461 462
}

463
/**
464 465 466 467
 * This is a helper function to handle the legacy (pre GLX 1.3) cases
 * where passing an X window to glXMakeCurrent is valid.  Given a
 * resource ID, look up the GLX drawable if available, otherwise, make
 * sure it's an X window and create a GLX drawable one the fly.
468
 */
Kristian Høgsberg's avatar
Kristian Høgsberg committed
469
static __GLXdrawable *
470 471
__glXGetDrawable(__GLXcontext * glxc, GLXDrawable drawId, ClientPtr client,
                 int *error)
472 473
{
    DrawablePtr pDraw;
474
    __GLXdrawable *pGlxDraw;
475
    int rc;
476

477
    if (validGlxDrawable(client, drawId, GLX_DRAWABLE_ANY,
478 479 480 481 482 483
                         DixWriteAccess, &pGlxDraw, &rc)) {
        if (glxc != NULL && pGlxDraw->config != glxc->config) {
            client->errorValue = drawId;
            *error = BadMatch;
            return NULL;
        }
484

485
        return pGlxDraw;
486 487
    }

488 489
    /* No active context and an unknown drawable, bail. */
    if (glxc == NULL) {
490 491 492
        client->errorValue = drawId;
        *error = BadMatch;
        return NULL;
493 494
    }

495 496 497 498
    /* The drawId wasn't a GLX drawable.  Make sure it's a window and
     * create a GLXWindow for it.  Check that the drawable screen
     * matches the context screen and that the context fbconfig is
     * compatible with the window visual. */
499

500
    rc = dixLookupDrawable(&pDraw, drawId, client, 0, DixGetAttrAccess);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
501
    if (rc != Success || pDraw->type != DRAWABLE_WINDOW) {
502 503 504
        client->errorValue = drawId;
        *error = __glXError(GLXBadDrawable);
        return NULL;
505 506
    }

507
    if (pDraw->pScreen != glxc->pGlxScreen->pScreen) {
508 509 510
        client->errorValue = pDraw->pScreen->myNum;
        *error = BadMatch;
        return NULL;
511 512
    }

513
    if (!validGlxFBConfigForWindow(client, glxc->config, pDraw, error))
514
        return NULL;
515

516
    pGlxDraw = glxc->pGlxScreen->createDrawable(client, glxc->pGlxScreen,
517 518 519
                                                pDraw, drawId,
                                                GLX_DRAWABLE_WINDOW,
                                                drawId, glxc->config);
520 521 522 523
    if (!pGlxDraw) {
	*error = BadAlloc;
	return NULL;
    }
524 525 526

    /* since we are creating the drawablePrivate, drawId should be new */
    if (!AddResource(drawId, __glXDrawableRes, pGlxDraw)) {
527 528 529
        pGlxDraw->destroy(pGlxDraw);
        *error = BadAlloc;
        return NULL;
530 531
    }

Kristian Høgsberg's avatar
Kristian Høgsberg committed
532
    return pGlxDraw;
533 534
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
535 536 537 538
/*****************************************************************************/
/*
** Make an OpenGL context and drawable current.
*/
539

Kristian Høgsberg's avatar
Kristian Høgsberg committed
540
static int
541 542 543
DoMakeCurrent(__GLXclientState * cl,
              GLXDrawable drawId, GLXDrawable readId,
              GLXContextID contextId, GLXContextTag tag)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
544 545 546 547
{
    ClientPtr client = cl->client;
    xGLXMakeCurrentReply reply;
    __GLXcontext *glxc, *prevglxc;
548 549
    __GLXdrawable *drawPriv = NULL;
    __GLXdrawable *readPriv = NULL;
550
    int error;
551
    GLuint mask;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
552 553

    /*
554 555
     ** If one is None and the other isn't, it's a bad match.
     */
556

557 558
    mask = (drawId == None) ? (1 << 0) : 0;
    mask |= (readId == None) ? (1 << 1) : 0;
559 560
    mask |= (contextId == None) ? (1 << 2) : 0;

561 562
    if ((mask != 0x00) && (mask != 0x07)) {
        return BadMatch;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
563
    }
564

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
565
    /*
566 567
     ** Lookup old context.  If we have one, it must be in a usable state.
     */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
568
    if (tag != 0) {
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
        prevglxc = __glXLookupContextByTag(cl, tag);
        if (!prevglxc) {
            /*
             ** Tag for previous context is invalid.
             */
            return __glXError(GLXBadContextTag);
        }
        if (prevglxc->renderMode != GL_RENDER) {
            /* Oops.  Not in render mode render. */
            client->errorValue = prevglxc->id;
            return __glXError(GLXBadContextState);
        }
    }
    else {
        prevglxc = 0;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
584 585 586
    }

    /*
587 588
     ** Lookup new context.  It must not be current for someone else.
     */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
589
    if (contextId != None) {
590
        int status;
591

592 593
        if (!validGlxContext(client, contextId, DixUseAccess, &glxc, &error))
            return error;
594
        if ((glxc != prevglxc) && glxc->currentClient) {
595 596 597
            /* Context is current to somebody else */
            return BadAccess;
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
598

599 600
        assert(drawId != None);
        assert(readId != None);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
601

602 603 604
        drawPriv = __glXGetDrawable(glxc, drawId, client, &status);
        if (drawPriv == NULL)
            return status;
605

606 607 608
        readPriv = __glXGetDrawable(glxc, readId, client, &status);
        if (readPriv == NULL)
            return status;
609

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
610
    }
611 612 613 614 615 616
    else {
        /* Switching to no context.  Ignore new drawable. */
        glxc = 0;
        drawPriv = 0;
        readPriv = 0;
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
617 618

    if (prevglxc) {
619 620 621 622 623
        /*
         ** Flush the previous context if needed.
         */
        if (prevglxc->hasUnflushedCommands) {
            if (__glXForceCurrent(cl, tag, (int *) &error)) {
624
                glFlush();
625 626 627 628 629 630 631 632 633 634 635 636 637
                prevglxc->hasUnflushedCommands = GL_FALSE;
            }
            else {
                return error;
            }
        }

        /*
         ** Make the previous context not current.
         */
        if (!(*prevglxc->loseCurrent) (prevglxc)) {
            return __glXError(GLXBadContext);
        }
638
        lastGLContext = NULL;
639 640 641 642 643
        if (!prevglxc->isDirect) {
            prevglxc->drawPriv = NULL;
            prevglxc->readPriv = NULL;
        }
    }
644

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
645 646
    if ((glxc != 0) && !glxc->isDirect) {

647 648
        glxc->drawPriv = drawPriv;
        glxc->readPriv = readPriv;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
649

650
        /* make the context current */
651
        lastGLContext = glxc;
652
        if (!(*glxc->makeCurrent) (glxc)) {
653
            lastGLContext = NULL;
654 655 656 657
            glxc->drawPriv = NULL;
            glxc->readPriv = NULL;
            return __glXError(GLXBadContext);
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
658

659
        glxc->currentClient = client;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
660 661
    }

Adam Jackson's avatar
Adam Jackson committed
662
    StopUsingContext(prevglxc);
663

664 665 666 667 668 669 670
    reply = (xGLXMakeCurrentReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .contextTag = 0
    };

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
671
    if (glxc) {
672 673 674
        StartUsingContext(cl, glxc);
        reply.contextTag = glxc->id;
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
675 676

    if (client->swapped) {
677 678 679
        __glXSwapMakeCurrentReply(client, &reply);
    }
    else {
680
        WriteToClient(client, sz_xGLXMakeCurrentReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
681 682 683 684
    }
    return Success;
}

685 686
int
__glXDisp_MakeCurrent(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
687
{
Julien Cristau's avatar
Julien Cristau committed
688
    ClientPtr client = cl->client;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
689 690
    xGLXMakeCurrentReq *req = (xGLXMakeCurrentReq *) pc;

Julien Cristau's avatar
Julien Cristau committed
691 692
    REQUEST_SIZE_MATCH(xGLXMakeCurrentReq);

693 694
    return DoMakeCurrent(cl, req->drawable, req->drawable,
                         req->context, req->oldContextTag);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
695 696
}

697 698
int
__glXDisp_MakeContextCurrent(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
699
{
Julien Cristau's avatar
Julien Cristau committed
700
    ClientPtr client = cl->client;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
701 702
    xGLXMakeContextCurrentReq *req = (xGLXMakeContextCurrentReq *) pc;

Julien Cristau's avatar
Julien Cristau committed
703 704
    REQUEST_SIZE_MATCH(xGLXMakeContextCurrentReq);

705 706
    return DoMakeCurrent(cl, req->drawable, req->readdrawable,
                         req->context, req->oldContextTag);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
707 708
}

709 710
int
__glXDisp_MakeCurrentReadSGI(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
711
{
Julien Cristau's avatar
Julien Cristau committed
712
    ClientPtr client = cl->client;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
713 714
    xGLXMakeCurrentReadSGIReq *req = (xGLXMakeCurrentReadSGIReq *) pc;

Julien Cristau's avatar
Julien Cristau committed
715 716
    REQUEST_SIZE_MATCH(xGLXMakeCurrentReadSGIReq);

717 718
    return DoMakeCurrent(cl, req->drawable, req->readable,
                         req->context, req->oldContextTag);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
719 720
}

721 722
int
__glXDisp_IsDirect(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
723 724 725 726 727
{
    ClientPtr client = cl->client;
    xGLXIsDirectReq *req = (xGLXIsDirectReq *) pc;
    xGLXIsDirectReply reply;
    __GLXcontext *glxc;
728
    int err;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
729

Julien Cristau's avatar
Julien Cristau committed
730 731
    REQUEST_SIZE_MATCH(xGLXIsDirectReq);

732
    if (!validGlxContext(cl->client, req->context, DixReadAccess, &glxc, &err))
733
        return err;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
734

735 736 737 738 739 740
    reply = (xGLXIsDirectReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .isDirect = glxc->isDirect
    };
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
741 742

    if (client->swapped) {
743 744 745
        __glXSwapIsDirectReply(client, &reply);
    }
    else {
746
        WriteToClient(client, sz_xGLXIsDirectReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
747 748 749 750 751
    }

    return Success;
}

752 753
int
__glXDisp_QueryVersion(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
754 755 756 757 758 759
{
    ClientPtr client = cl->client;
    xGLXQueryVersionReq *req = (xGLXQueryVersionReq *) pc;
    xGLXQueryVersionReply reply;
    GLuint major, minor;

Julien Cristau's avatar
Julien Cristau committed
760 761
    REQUEST_SIZE_MATCH(xGLXQueryVersionReq);

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
762 763
    major = req->majorVersion;
    minor = req->minorVersion;
764 765
    (void) major;
    (void) minor;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
766 767

    /*
768 769 770 771
     ** Server should take into consideration the version numbers sent by the
     ** client if it wants to work with older clients; however, in this
     ** implementation the server just returns its version number.
     */
772 773 774 775 776 777 778
    reply = (xGLXQueryVersionReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .majorVersion = glxMajorVersion,
        .minorVersion = glxMinorVersion
    };
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
779 780

    if (client->swapped) {
781 782 783
        __glXSwapQueryVersionReply(client, &reply);
    }
    else {
784
        WriteToClient(client, sz_xGLXQueryVersionReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
785 786 787 788
    }
    return Success;
}

789 790
int
__glXDisp_WaitGL(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
791
{
Julien Cristau's avatar
Julien Cristau committed
792
    ClientPtr client = cl->client;
793
    xGLXWaitGLReq *req = (xGLXWaitGLReq *) pc;
Julien Cristau's avatar
Julien Cristau committed
794
    GLXContextTag tag;
795
    __GLXcontext *glxc = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
796
    int error;
797

Julien Cristau's avatar
Julien Cristau committed
798 799 800
    REQUEST_SIZE_MATCH(xGLXWaitGLReq);

    tag = req->contextTag;
801
    if (tag) {
802 803 804 805 806 807
        glxc = __glXLookupContextByTag(cl, tag);
        if (!glxc)
            return __glXError(GLXBadContextTag);

        if (!__glXForceCurrent(cl, req->contextTag, &error))
            return error;
808

809
        glFinish();
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
810
    }
811 812

    if (glxc && glxc->drawPriv->waitGL)
813
        (*glxc->drawPriv->waitGL) (glxc->drawPriv);
814

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
815 816 817
    return Success;
}

818 819
int
__glXDisp_WaitX(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
820
{
Julien Cristau's avatar
Julien Cristau committed
821
    ClientPtr client = cl->client;
822
    xGLXWaitXReq *req = (xGLXWaitXReq *) pc;
Julien Cristau's avatar
Julien Cristau committed
823
    GLXContextTag tag;
824
    __GLXcontext *glxc = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
825
    int error;
826

Julien Cristau's avatar
Julien Cristau committed
827 828 829
    REQUEST_SIZE_MATCH(xGLXWaitXReq);

    tag = req->contextTag;
830
    if (tag) {
831 832 833 834 835 836
        glxc = __glXLookupContextByTag(cl, tag);
        if (!glxc)
            return __glXError(GLXBadContextTag);

        if (!__glXForceCurrent(cl, req->contextTag, &error))
            return error;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
837
    }
838

Pierre Willenbrock's avatar
Pierre Willenbrock committed
839
    if (glxc && glxc->drawPriv->waitX)
840
        (*glxc->drawPriv->waitX) (glxc->drawPriv);
841

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
842 843 844
    return Success;
}

845 846
int
__glXDisp_CopyContext(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
847 848 849
{
    ClientPtr client = cl->client;
    xGLXCopyContextReq *req = (xGLXCopyContextReq *) pc;
Julien Cristau's avatar
Julien Cristau committed
850 851 852 853
    GLXContextID source;
    GLXContextID dest;
    GLXContextTag tag;
    unsigned long mask;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
854 855 856
    __GLXcontext *src, *dst;
    int error;

Julien Cristau's avatar
Julien Cristau committed
857 858 859 860 861 862
    REQUEST_SIZE_MATCH(xGLXCopyContextReq);

    source = req->source;
    dest = req->dest;
    tag = req->contextTag;
    mask = req->mask;
863
    if (!validGlxContext(cl->client, source, DixReadAccess, &src, &error))
864
        return error;
865
    if (!validGlxContext(cl->client, dest, DixWriteAccess, &dst, &error))
866
        return error;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
867 868

    /*
869 870 871 872 873 874
     ** They must be in the same address space, and same screen.
     ** NOTE: no support for direct rendering contexts here.
     */
    if (src->isDirect || dst->isDirect || (src->pGlxScreen != dst->pGlxScreen)) {
        client->errorValue = source;
        return BadMatch;
Kaleb Keithley Keithley's avatar