glxcmds.c 76.6 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
/**
 * Create a GL context with the given properties.  This routine is used
 * to implement \c glXCreateContext, \c glXCreateNewContext, and
227
 * \c glXCreateContextWithConfigSGIX.  This works because of the hack way
Eric Anholt's avatar
Eric Anholt committed
228 229 230 231
 * 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;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
563

564 565 566
    /* Drawables but no context makes no sense */
    if (!contextId && (drawId || readId))
        return BadMatch;
567

568 569
    /* If either drawable is null, the other must be too */
    if ((drawId == None) != (readId == None))
570 571
        return BadMatch;

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

    /*
594 595
     ** Lookup new context.  It must not be current for someone else.
     */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
596
    if (contextId != None) {
597
        int status;
598

599 600
        if (!validGlxContext(client, contextId, DixUseAccess, &glxc, &error))
            return error;
601
        if ((glxc != prevglxc) && glxc->currentClient) {
602 603 604
            /* Context is current to somebody else */
            return BadAccess;
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
605

606 607 608 609 610
        if (drawId) {
            drawPriv = __glXGetDrawable(glxc, drawId, client, &status);
            if (drawPriv == NULL)
                return status;
        }
611

612 613 614 615 616 617
        if (readId) {
            readPriv = __glXGetDrawable(glxc, readId, client, &status);
            if (readPriv == NULL)
                return status;
        }
    } else {
618
        /* Switching to no context.  Ignore new drawable. */
619

620 621 622 623
        glxc = 0;
        drawPriv = 0;
        readPriv = 0;
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
624 625

    if (prevglxc) {
626 627 628
        /*
         ** Flush the previous context if needed.
         */
629 630 631 632 633
        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
634
        if (need_flush) {
635
            if (__glXForceCurrent(cl, tag, (int *) &error)) {
636
                glFlush();
637 638 639 640 641 642 643 644 645 646 647 648
            }
            else {
                return error;
            }
        }

        /*
         ** Make the previous context not current.
         */
        if (!(*prevglxc->loseCurrent) (prevglxc)) {
            return __glXError(GLXBadContext);
        }
649
        lastGLContext = NULL;
650 651 652 653 654
        if (!prevglxc->isDirect) {
            prevglxc->drawPriv = NULL;
            prevglxc->readPriv = NULL;
        }
    }
655

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

658 659
        glxc->drawPriv = drawPriv;
        glxc->readPriv = readPriv;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
660

661
        /* make the context current */
662
        lastGLContext = glxc;
663
        if (!(*glxc->makeCurrent) (glxc)) {
664
            lastGLContext = NULL;
665 666 667 668
            glxc->drawPriv = NULL;
            glxc->readPriv = NULL;
            return __glXError(GLXBadContext);
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
669

670
        glxc->currentClient = client;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
671 672
    }

Adam Jackson's avatar
Adam Jackson committed
673
    StopUsingContext(prevglxc);
674

675 676 677 678 679 680 681
    reply = (xGLXMakeCurrentReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .contextTag = 0
    };

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
682
    if (glxc) {
683 684 685
        StartUsingContext(cl, glxc);
        reply.contextTag = glxc->id;
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
686 687

    if (client->swapped) {
688 689 690 691
        __GLX_DECLARE_SWAP_VARIABLES;
        __GLX_SWAP_SHORT(&reply.sequenceNumber);
        __GLX_SWAP_INT(&reply.length);
        __GLX_SWAP_INT(&reply.contextTag);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
692
    }
693
    WriteToClient(client, sz_xGLXMakeCurrentReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
694 695 696
    return Success;
}

697 698
int
__glXDisp_MakeCurrent(__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
    xGLXMakeCurrentReq *req = (xGLXMakeCurrentReq *) pc;

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

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

709 710
int
__glXDisp_MakeContextCurrent(__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
    xGLXMakeContextCurrentReq *req = (xGLXMakeContextCurrentReq *) pc;

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

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

721 722
int
__glXDisp_MakeCurrentReadSGI(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
723
{
Julien Cristau's avatar
Julien Cristau committed
724
    ClientPtr client = cl->client;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
725 726
    xGLXMakeCurrentReadSGIReq *req = (xGLXMakeCurrentReadSGIReq *) pc;

Julien Cristau's avatar
Julien Cristau committed
727 728
    REQUEST_SIZE_MATCH(xGLXMakeCurrentReadSGIReq);

729 730
    return DoMakeCurrent(cl, req->drawable, req->readable,
                         req->context, req->oldContextTag);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
731 732
}

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

Julien Cristau's avatar
Julien Cristau committed
742 743
    REQUEST_SIZE_MATCH(xGLXIsDirectReq);

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

747 748 749 750 751 752
    reply = (xGLXIsDirectReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .isDirect = glxc->isDirect
    };
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
753 754

    if (client->swapped) {
755 756 757
        __GLX_DECLARE_SWAP_VARIABLES;
        __GLX_SWAP_SHORT(&reply.sequenceNumber);
        __GLX_SWAP_INT(&reply.length);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
758
    }
759
    WriteToClient(client, sz_xGLXIsDirectReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
760 761 762 763

    return Success;
}

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

Julien Cristau's avatar
Julien Cristau committed
772 773
    REQUEST_SIZE_MATCH(xGLXQueryVersionReq);

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
774 775
    major = req->majorVersion;
    minor = req->minorVersion;
776 777
    (void) major;
    (void) minor;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
778 779

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

    if (client->swapped) {
793 794 795 796 797
        __GLX_DECLARE_SWAP_VARIABLES;
        __GLX_SWAP_SHORT(&reply.sequenceNumber);
        __GLX_SWAP_INT(&reply.length);
        __GLX_SWAP_INT(&reply.majorVersion);
        __GLX_SWAP_INT(&reply.minorVersion);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
798
    }
799 800

    WriteToClient(client, sz_xGLXQueryVersionReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
801 802 803
    return Success;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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