glxcmds.c 75.2 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"
48
#include "protocol-versions.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
49

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

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

66 67 68
    return TRUE;
}

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

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

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

    return FALSE;
}

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

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

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

    return FALSE;
}

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

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

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

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

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

    return TRUE;
}

Jon Turney's avatar
Jon Turney committed
149
int
150
validGlxDrawable(ClientPtr client, XID id, int type, int access_mode,
151
                 __GLXdrawable ** drawable, int *err)
152 153 154
{
    int rc;

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

163 164 165
    /* 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). */
166
    if (rc == BadValue ||
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        (*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;
        }
184 185 186 187 188
    }

    return TRUE;
}

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

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

202 203 204 205 206 207
static int
__glXdirectContextLoseCurrent(__GLXcontext * context)
{
    return GL_TRUE;
}

208
_X_HIDDEN __GLXcontext *
209 210
__glXdirectContextCreate(__GLXscreen * screen,
                         __GLXconfig * modes, __GLXcontext * shareContext)
211 212 213
{
    __GLXcontext *context;

214
    context = calloc(1, sizeof(__GLXcontext));
215
    if (context == NULL)
216
        return NULL;
217 218

    context->destroy = __glXdirectContextDestroy;
219
    context->loseCurrent = __glXdirectContextLoseCurrent;
220 221 222 223

    return context;
}

Eric Anholt's avatar
Eric Anholt committed
224 225 226 227 228 229 230 231
/**
 * 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
232
static int
233 234 235
DoCreateContext(__GLXclientState * cl, GLXContextID gcId,
                GLXContextID shareList, __GLXconfig * config,
                __GLXscreen * pGlxScreen, GLboolean isDirect)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
236 237 238
{
    ClientPtr client = cl->client;
    __GLXcontext *glxc, *shareglxc;
239
    int err;
240

241
    LEGAL_NEW_RESOURCE(gcId, client);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
242 243

    /*
Peter Hutterer's avatar
Peter Hutterer committed
244
     ** Find the display list space that we want to share.
245 246
     **
     ** NOTE: In a multithreaded X server, we would need to keep a reference
Peter Hutterer's avatar
Peter Hutterer committed
247 248 249 250
     ** 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.
251
     */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
252
    if (shareList == None) {
253 254 255 256 257 258 259
        shareglxc = 0;
    }
    else {
        if (!validGlxContext(client, shareList, DixReadAccess,
                             &shareglxc, &err))
            return err;

260 261 262 263 264 265 266 267 268 269
        /* 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) {
270 271 272
            client->errorValue = shareList;
            return BadMatch;
        }
273
        else if (!shareglxc->isDirect) {
274 275 276 277 278 279
            /*
             ** 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
280 281 282
    }

    /*
283 284
     ** Allocate memory for the new context
     */
285
    if (!isDirect) {
286 287 288 289 290 291 292 293 294 295 296
        /* 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;
        }

297 298 299 300 301 302 303
        /* 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);
    }
304
    else
305
        glxc = __glXdirectContextCreate(pGlxScreen, config, shareglxc);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
306
    if (!glxc) {
307
        return BadAlloc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
308 309
    }

310
    /* Initialize the GLXcontext structure.
311
     */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
312
    glxc->pGlxScreen = pGlxScreen;
313
    glxc->config = config;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
314 315 316 317 318 319
    glxc->id = gcId;
    glxc->share_id = shareList;
    glxc->idExists = GL_TRUE;
    glxc->isDirect = isDirect;
    glxc->renderMode = GL_RENDER;

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 331 332 333 334 335 336 337 338 339 340 341 342
#ifdef GLX_CONTEXT_RELEASE_BEHAVIOR_ARB
    /* The GLX_ARB_context_flush_control spec says:
     *
     *     "The default value [for GLX_CONTEXT_RELEASE_BEHAVIOR] is
     *     CONTEXT_RELEASE_BEHAVIOR_FLUSH, and may in some cases be changed
     *     using platform-specific context creation extensions."
     *
     * Without using glXCreateContextAttribsARB, there is no way to specify a
     * non-default release behavior.
     */
    glxc->releaseBehavior = GLX_CONTEXT_RELEASE_BEHAVIOR_FLUSH_ARB;
#endif

343
    /* Add the new context to the various global tables of GLX contexts.
344
     */
345
    if (!__glXAddContext(glxc)) {
346 347 348 349 350
        (*glxc->destroy) (glxc);
        client->errorValue = gcId;
        return BadAlloc;
    }

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
351 352 353
    return Success;
}

354 355
int
__glXDisp_CreateContext(__GLXclientState * cl, GLbyte * pc)
Eric Anholt's avatar
Eric Anholt committed
356
{
Julien Cristau's avatar
Julien Cristau committed
357
    ClientPtr client = cl->client;
Eric Anholt's avatar
Eric Anholt committed
358
    xGLXCreateContextReq *req = (xGLXCreateContextReq *) pc;
359
    __GLXconfig *config;
360 361
    __GLXscreen *pGlxScreen;
    int err;
Eric Anholt's avatar
Eric Anholt committed
362

Julien Cristau's avatar
Julien Cristau committed
363 364
    REQUEST_SIZE_MATCH(xGLXCreateContextReq);

365
    if (!validGlxScreen(cl->client, req->screen, &pGlxScreen, &err))
366
        return err;
367
    if (!validGlxVisual(cl->client, pGlxScreen, req->visual, &config, &err))
368
        return err;
369 370

    return DoCreateContext(cl, req->context, req->shareList,
371
                           config, pGlxScreen, req->isDirect);
372
}
Eric Anholt's avatar
Eric Anholt committed
373

374 375
int
__glXDisp_CreateNewContext(__GLXclientState * cl, GLbyte * pc)
Eric Anholt's avatar
Eric Anholt committed
376
{
Julien Cristau's avatar
Julien Cristau committed
377
    ClientPtr client = cl->client;
Eric Anholt's avatar
Eric Anholt committed
378
    xGLXCreateNewContextReq *req = (xGLXCreateNewContextReq *) pc;
379
    __GLXconfig *config;
380 381 382
    __GLXscreen *pGlxScreen;
    int err;

Julien Cristau's avatar
Julien Cristau committed
383 384
    REQUEST_SIZE_MATCH(xGLXCreateNewContextReq);

385
    if (!validGlxScreen(cl->client, req->screen, &pGlxScreen, &err))
386
        return err;
387
    if (!validGlxFBConfig(cl->client, pGlxScreen, req->fbconfig, &config, &err))
388
        return err;
Eric Anholt's avatar
Eric Anholt committed
389

390
    return DoCreateContext(cl, req->context, req->shareList,
391
                           config, pGlxScreen, req->isDirect);
392
}
Eric Anholt's avatar
Eric Anholt committed
393

394 395
int
__glXDisp_CreateContextWithConfigSGIX(__GLXclientState * cl, GLbyte * pc)
Eric Anholt's avatar
Eric Anholt committed
396
{
Julien Cristau's avatar
Julien Cristau committed
397
    ClientPtr client = cl->client;
398 399
    xGLXCreateContextWithConfigSGIXReq *req =
        (xGLXCreateContextWithConfigSGIXReq *) pc;
400
    __GLXconfig *config;
401 402
    __GLXscreen *pGlxScreen;
    int err;
Eric Anholt's avatar
Eric Anholt committed
403

Julien Cristau's avatar
Julien Cristau committed
404 405
    REQUEST_SIZE_MATCH(xGLXCreateContextWithConfigSGIXReq);

406
    if (!validGlxScreen(cl->client, req->screen, &pGlxScreen, &err))
407
        return err;
408
    if (!validGlxFBConfig(cl->client, pGlxScreen, req->fbconfig, &config, &err))
409
        return err;
410 411

    return DoCreateContext(cl, req->context, req->shareList,
412
                           config, pGlxScreen, req->isDirect);
413
}
Adam Jackson's avatar
Adam Jackson committed
414

415 416
int
__glXDisp_DestroyContext(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
417
{
Julien Cristau's avatar
Julien Cristau committed
418
    ClientPtr client = cl->client;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
419 420
    xGLXDestroyContextReq *req = (xGLXDestroyContextReq *) pc;
    __GLXcontext *glxc;
421 422
    int err;

Julien Cristau's avatar
Julien Cristau committed
423 424
    REQUEST_SIZE_MATCH(xGLXDestroyContextReq);

425
    if (!validGlxContext(cl->client, req->context, DixDestroyAccess,
426 427
                         &glxc, &err))
        return err;
428

429 430 431
    glxc->idExists = GL_FALSE;
    if (!glxc->currentClient)
        FreeResourceByType(req->context, __glXContextRes, FALSE);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
432

Adam Jackson's avatar
Adam Jackson committed
433
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
434 435 436
}

/*
Adam Jackson's avatar
Adam Jackson committed
437 438 439 440 441 442 443 444
 * 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.
 */
445 446
__GLXcontext *
__glXLookupContextByTag(__GLXclientState * cl, GLXContextTag tag)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
447
{
Adam Jackson's avatar
Adam Jackson committed
448
    __GLXcontext *ret;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
449

450
    if (dixLookupResourceByType((void **) &ret, tag, __glXContextRes,
Adam Jackson's avatar
Adam Jackson committed
451 452 453 454
                                cl->client, DixUseAccess) == Success)
        return ret;

    return NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
455 456 457 458
}

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

459 460
static void
StopUsingContext(__GLXcontext * glxc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
461 462
{
    if (glxc) {
463
        glxc->currentClient = NULL;
464
        if (!glxc->idExists) {
Adam Jackson's avatar
Adam Jackson committed
465
            FreeResourceByType(glxc->id, __glXContextRes, FALSE);
466
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
467 468 469
    }
}

470 471
static void
StartUsingContext(__GLXclientState * cl, __GLXcontext * glxc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
472
{
473
    glxc->currentClient = cl->client;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
474 475
}

476
/**
477 478 479 480
 * 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.
481
 */
Kristian Høgsberg's avatar
Kristian Høgsberg committed
482
static __GLXdrawable *
483 484
__glXGetDrawable(__GLXcontext * glxc, GLXDrawable drawId, ClientPtr client,
                 int *error)
485 486
{
    DrawablePtr pDraw;
487
    __GLXdrawable *pGlxDraw;
488
    int rc;
489

490
    if (validGlxDrawable(client, drawId, GLX_DRAWABLE_ANY,
491 492 493 494 495 496
                         DixWriteAccess, &pGlxDraw, &rc)) {
        if (glxc != NULL && pGlxDraw->config != glxc->config) {
            client->errorValue = drawId;
            *error = BadMatch;
            return NULL;
        }
497

498
        return pGlxDraw;
499 500
    }

501 502
    /* No active context and an unknown drawable, bail. */
    if (glxc == NULL) {
503 504 505
        client->errorValue = drawId;
        *error = BadMatch;
        return NULL;
506 507
    }

508 509 510 511
    /* 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. */
512

513
    rc = dixLookupDrawable(&pDraw, drawId, client, 0, DixGetAttrAccess);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
514
    if (rc != Success || pDraw->type != DRAWABLE_WINDOW) {
515 516 517
        client->errorValue = drawId;
        *error = __glXError(GLXBadDrawable);
        return NULL;
518 519
    }

520
    if (pDraw->pScreen != glxc->pGlxScreen->pScreen) {
521 522 523
        client->errorValue = pDraw->pScreen->myNum;
        *error = BadMatch;
        return NULL;
524 525
    }

526
    if (!validGlxFBConfigForWindow(client, glxc->config, pDraw, error))
527
        return NULL;
528

529
    pGlxDraw = glxc->pGlxScreen->createDrawable(client, glxc->pGlxScreen,
530 531 532
                                                pDraw, drawId,
                                                GLX_DRAWABLE_WINDOW,
                                                drawId, glxc->config);
533 534 535 536
    if (!pGlxDraw) {
	*error = BadAlloc;
	return NULL;
    }
537 538 539

    /* since we are creating the drawablePrivate, drawId should be new */
    if (!AddResource(drawId, __glXDrawableRes, pGlxDraw)) {
540 541
        *error = BadAlloc;
        return NULL;
542 543
    }

Kristian Høgsberg's avatar
Kristian Høgsberg committed
544
    return pGlxDraw;
545 546
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
547 548 549 550
/*****************************************************************************/
/*
** Make an OpenGL context and drawable current.
*/
551

Kristian Høgsberg's avatar
Kristian Høgsberg committed
552
static int
553 554 555
DoMakeCurrent(__GLXclientState * cl,
              GLXDrawable drawId, GLXDrawable readId,
              GLXContextID contextId, GLXContextTag tag)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
556 557 558 559
{
    ClientPtr client = cl->client;
    xGLXMakeCurrentReply reply;
    __GLXcontext *glxc, *prevglxc;
560 561
    __GLXdrawable *drawPriv = NULL;
    __GLXdrawable *readPriv = NULL;
562
    int error;
563
    GLuint mask;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
564 565

    /*
566 567
     ** If one is None and the other isn't, it's a bad match.
     */
568

569 570
    mask = (drawId == None) ? (1 << 0) : 0;
    mask |= (readId == None) ? (1 << 1) : 0;
571 572
    mask |= (contextId == None) ? (1 << 2) : 0;

573 574
    if ((mask != 0x00) && (mask != 0x07)) {
        return BadMatch;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
575
    }
576

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
577
    /*
578 579
     ** Lookup old context.  If we have one, it must be in a usable state.
     */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
580
    if (tag != 0) {
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
        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
596 597 598
    }

    /*
599 600
     ** Lookup new context.  It must not be current for someone else.
     */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
601
    if (contextId != None) {
602
        int status;
603

604 605
        if (!validGlxContext(client, contextId, DixUseAccess, &glxc, &error))
            return error;
606
        if ((glxc != prevglxc) && glxc->currentClient) {
607 608 609
            /* Context is current to somebody else */
            return BadAccess;
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
610

611 612
        assert(drawId != None);
        assert(readId != None);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
613

614 615 616
        drawPriv = __glXGetDrawable(glxc, drawId, client, &status);
        if (drawPriv == NULL)
            return status;
617

618 619 620
        readPriv = __glXGetDrawable(glxc, readId, client, &status);
        if (readPriv == NULL)
            return status;
621

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
622
    }
623 624 625 626 627 628
    else {
        /* Switching to no context.  Ignore new drawable. */
        glxc = 0;
        drawPriv = 0;
        readPriv = 0;
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
629 630

    if (prevglxc) {
631 632 633
        /*
         ** Flush the previous context if needed.
         */
634 635 636 637 638
        Bool need_flush = GL_TRUE;
#ifdef GLX_CONTEXT_RELEASE_BEHAVIOR_ARB
        if (prevglxc->releaseBehavior == GLX_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB)
            need_flush = GL_FALSE;
#endif
639
        if (need_flush) {
640
            if (__glXForceCurrent(cl, tag, (int *) &error)) {
641
                glFlush();
642 643 644 645 646 647 648 649 650 651 652 653
            }
            else {
                return error;
            }
        }

        /*
         ** Make the previous context not current.
         */
        if (!(*prevglxc->loseCurrent) (prevglxc)) {
            return __glXError(GLXBadContext);
        }
654
        lastGLContext = NULL;
655 656 657 658 659
        if (!prevglxc->isDirect) {
            prevglxc->drawPriv = NULL;
            prevglxc->readPriv = NULL;
        }
    }
660

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

663 664
        glxc->drawPriv = drawPriv;
        glxc->readPriv = readPriv;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
665

666
        /* make the context current */
667
        lastGLContext = glxc;
668
        if (!(*glxc->makeCurrent) (glxc)) {
669
            lastGLContext = NULL;
670 671 672 673
            glxc->drawPriv = NULL;
            glxc->readPriv = NULL;
            return __glXError(GLXBadContext);
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
674

675
        glxc->currentClient = client;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
676 677
    }

Adam Jackson's avatar
Adam Jackson committed
678
    StopUsingContext(prevglxc);
679

680 681 682 683 684 685 686
    reply = (xGLXMakeCurrentReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .contextTag = 0
    };

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
687
    if (glxc) {
688 689 690
        StartUsingContext(cl, glxc);
        reply.contextTag = glxc->id;
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
691 692

    if (client->swapped) {
693 694 695
        __glXSwapMakeCurrentReply(client, &reply);
    }
    else {
696
        WriteToClient(client, sz_xGLXMakeCurrentReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
697 698 699 700
    }
    return Success;
}

701 702
int
__glXDisp_MakeCurrent(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
703
{
Julien Cristau's avatar
Julien Cristau committed
704
    ClientPtr client = cl->client;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
705 706
    xGLXMakeCurrentReq *req = (xGLXMakeCurrentReq *) pc;

Julien Cristau's avatar
Julien Cristau committed
707 708
    REQUEST_SIZE_MATCH(xGLXMakeCurrentReq);

709 710
    return DoMakeCurrent(cl, req->drawable, req->drawable,
                         req->context, req->oldContextTag);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
711 712
}

713 714
int
__glXDisp_MakeContextCurrent(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
715
{
Julien Cristau's avatar
Julien Cristau committed
716
    ClientPtr client = cl->client;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
717 718
    xGLXMakeContextCurrentReq *req = (xGLXMakeContextCurrentReq *) pc;

Julien Cristau's avatar
Julien Cristau committed
719 720
    REQUEST_SIZE_MATCH(xGLXMakeContextCurrentReq);

721 722
    return DoMakeCurrent(cl, req->drawable, req->readdrawable,
                         req->context, req->oldContextTag);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
723 724
}

725 726
int
__glXDisp_MakeCurrentReadSGI(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
727
{
Julien Cristau's avatar
Julien Cristau committed
728
    ClientPtr client = cl->client;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
729 730
    xGLXMakeCurrentReadSGIReq *req = (xGLXMakeCurrentReadSGIReq *) pc;

Julien Cristau's avatar
Julien Cristau committed
731 732
    REQUEST_SIZE_MATCH(xGLXMakeCurrentReadSGIReq);

733 734
    return DoMakeCurrent(cl, req->drawable, req->readable,
                         req->context, req->oldContextTag);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
735 736
}

737 738
int
__glXDisp_IsDirect(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
739 740 741 742 743
{
    ClientPtr client = cl->client;
    xGLXIsDirectReq *req = (xGLXIsDirectReq *) pc;
    xGLXIsDirectReply reply;
    __GLXcontext *glxc;
744
    int err;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
745

Julien Cristau's avatar
Julien Cristau committed
746 747
    REQUEST_SIZE_MATCH(xGLXIsDirectReq);

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

751 752 753 754 755 756
    reply = (xGLXIsDirectReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .isDirect = glxc->isDirect
    };
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
757 758

    if (client->swapped) {
759 760 761
        __glXSwapIsDirectReply(client, &reply);
    }
    else {
762
        WriteToClient(client, sz_xGLXIsDirectReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
763 764 765 766 767
    }

    return Success;
}

768 769
int
__glXDisp_QueryVersion(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
770 771 772 773 774 775
{
    ClientPtr client = cl->client;
    xGLXQueryVersionReq *req = (xGLXQueryVersionReq *) pc;
    xGLXQueryVersionReply reply;
    GLuint major, minor;

Julien Cristau's avatar
Julien Cristau committed
776 777
    REQUEST_SIZE_MATCH(xGLXQueryVersionReq);

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
778 779
    major = req->majorVersion;
    minor = req->minorVersion;
780 781
    (void) major;
    (void) minor;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
782 783

    /*
784 785 786 787
     ** 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.
     */
788 789 790 791
    reply = (xGLXQueryVersionReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
792 793
        .majorVersion = SERVER_GLX_MAJOR_VERSION,
        .minorVersion = SERVER_GLX_MINOR_VERSION
794
    };
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
795 796

    if (client->swapped) {
797 798 799
        __glXSwapQueryVersionReply(client, &reply);
    }
    else {
800
        WriteToClient(client, sz_xGLXQueryVersionReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
801 802 803 804
    }
    return Success;
}

805 806
int
__glXDisp_WaitGL(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
807
{
Julien Cristau's avatar
Julien Cristau committed
808
    ClientPtr client = cl->client;
809
    xGLXWaitGLReq *req = (xGLXWaitGLReq *) pc;
Julien Cristau's avatar
Julien Cristau committed
810
    GLXContextTag tag;
811
    __GLXcontext *glxc = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
812
    int error;
813

Julien Cristau's avatar
Julien Cristau committed
814 815 816
    REQUEST_SIZE_MATCH(xGLXWaitGLReq);

    tag = req->contextTag;
817
    if (tag) {
818 819 820 821 822 823
        glxc = __glXLookupContextByTag(cl, tag);
        if (!glxc)
            return __glXError(GLXBadContextTag);

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

825
        glFinish();
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
826
    }
827 828

    if (glxc && glxc->drawPriv->waitGL)
829
        (*glxc->drawPriv->waitGL) (glxc->drawPriv);
830

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
831 832 833
    return Success;
}

834 835
int
__glXDisp_WaitX(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
836
{
Julien Cristau's avatar
Julien Cristau committed
837
    ClientPtr client = cl->client;
838
    xGLXWaitXReq *req = (xGLXWaitXReq *) pc;
Julien Cristau's avatar
Julien Cristau committed
839
    GLXContextTag tag;
840
    __GLXcontext *glxc = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
841
    int error;
842

Julien Cristau's avatar
Julien Cristau committed
843 844 845
    REQUEST_SIZE_MATCH(xGLXWaitXReq);

    tag = req->contextTag;
846
    if (tag) {
847 848 849 850 851 852
        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
853
    }
854

Pierre Willenbrock's avatar
Pierre Willenbrock committed
855
    if (glxc && glxc->drawPriv->waitX)
856
        (*glxc->drawPriv->waitX) (glxc->drawPriv);
857

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
858 859 860
    return Success;
}

861 862
int
__glXDisp_CopyContext(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
863 864 865
{
    ClientPtr client = cl->client;
    xGLXCopyContextReq *req = (xGLXCopyContextReq *) pc;
Julien Cristau's avatar
Julien Cristau committed
866 867 868 869
    GLXContextID source;
    GLXContextID dest;
    GLXContextTag tag;
    unsigned long mask;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
870 871 872
    __GLXcontext *src, *dst;
    int error;

Julien Cristau's avatar
Julien Cristau committed
873 874 875 876 877 878
    REQUEST_SIZE_MATCH(xGLXCopyContextReq);

    source = req->source;
    dest = req->dest;
    tag = req->contextTag;
    mask = req->mask;
879
    if (!validGlxContext(cl->client, source, DixReadAccess, &src, &error))