glxcmds.c 77.7 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->config = modes;
219
    context->destroy = __glXdirectContextDestroy;
220
    context->loseCurrent = __glXdirectContextLoseCurrent;
221 222 223 224

    return context;
}

Eric Anholt's avatar
Eric Anholt committed
225 226 227
/**
 * Create a GL context with the given properties.  This routine is used
 * to implement \c glXCreateContext, \c glXCreateNewContext, and
228
 * \c glXCreateContextWithConfigSGIX.  This works because of the hack way
Eric Anholt's avatar
Eric Anholt committed
229 230 231 232
 * that GLXFBConfigs are implemented.  Basically, the FBConfigID is the
 * same as the VisualID.
 */

Kristian Høgsberg's avatar
Kristian Høgsberg committed
233
static int
234 235 236
DoCreateContext(__GLXclientState * cl, GLXContextID gcId,
                GLXContextID shareList, __GLXconfig * config,
                __GLXscreen * pGlxScreen, GLboolean isDirect)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
237 238 239
{
    ClientPtr client = cl->client;
    __GLXcontext *glxc, *shareglxc;
240
    int err;
241

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

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

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

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

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

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

321 322 323 324 325 326 327 328 329 330
    /* 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;

331 332 333 334 335 336 337 338 339 340 341 342 343
#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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

477 478 479 480 481 482 483 484 485 486 487 488
static __GLXconfig *
inferConfigForWindow(__GLXscreen *pGlxScreen, WindowPtr pWin)
{
    int i, vid = wVisual(pWin);

    for (i = 0; i < pGlxScreen->numVisuals; i++)
        if (pGlxScreen->visuals[i]->visualID == vid)
            return pGlxScreen->visuals[i];

    return NULL;
}

489
/**
490 491 492 493
 * 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.
494
 */
Kristian Høgsberg's avatar
Kristian Høgsberg committed
495
static __GLXdrawable *
496 497
__glXGetDrawable(__GLXcontext * glxc, GLXDrawable drawId, ClientPtr client,
                 int *error)
498 499
{
    DrawablePtr pDraw;
500
    __GLXdrawable *pGlxDraw;
501 502
    __GLXconfig *config;
    __GLXscreen *pGlxScreen;
503
    int rc;
504

505
    if (validGlxDrawable(client, drawId, GLX_DRAWABLE_ANY,
506
                         DixWriteAccess, &pGlxDraw, &rc)) {
507 508 509
        if (glxc != NULL &&
            glxc->config != NULL &&
            glxc->config != pGlxDraw->config) {
510 511 512 513
            client->errorValue = drawId;
            *error = BadMatch;
            return NULL;
        }
514

515
        return pGlxDraw;
516 517
    }

518 519
    /* No active context and an unknown drawable, bail. */
    if (glxc == NULL) {
520 521 522
        client->errorValue = drawId;
        *error = BadMatch;
        return NULL;
523 524
    }

525 526 527 528
    /* 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. */
529

530
    rc = dixLookupDrawable(&pDraw, drawId, client, 0, DixGetAttrAccess);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
531
    if (rc != Success || pDraw->type != DRAWABLE_WINDOW) {
532 533 534
        client->errorValue = drawId;
        *error = __glXError(GLXBadDrawable);
        return NULL;
535 536
    }

537 538
    pGlxScreen = glxc->pGlxScreen;
    if (pDraw->pScreen != pGlxScreen->pScreen) {
539 540 541
        client->errorValue = pDraw->pScreen->myNum;
        *error = BadMatch;
        return NULL;
542 543
    }

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    config = glxc->config;
    if (!config)
        config = inferConfigForWindow(pGlxScreen, (WindowPtr)pDraw);
    if (!config) {
        /*
         * If we get here, we've tried to bind a no-config context to a
         * window without a corresponding fbconfig, presumably because
         * we don't support GL on it (PseudoColor perhaps). From GLX Section
         * 3.3.7 "Rendering Contexts":
         *
         * "If draw or read are not compatible with ctx a BadMatch error
         * is generated."
         */
        *error = BadMatch;
        return NULL;
    }

    if (!validGlxFBConfigForWindow(client, config, pDraw, error))
562
        return NULL;
563

564 565
    pGlxDraw = pGlxScreen->createDrawable(client, pGlxScreen, pDraw, drawId,
                                          GLX_DRAWABLE_WINDOW, drawId, config);
566 567 568 569
    if (!pGlxDraw) {
	*error = BadAlloc;
	return NULL;
    }
570 571 572

    /* since we are creating the drawablePrivate, drawId should be new */
    if (!AddResource(drawId, __glXDrawableRes, pGlxDraw)) {
573 574
        *error = BadAlloc;
        return NULL;
575 576
    }

Kristian Høgsberg's avatar
Kristian Høgsberg committed
577
    return pGlxDraw;
578 579
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
580 581 582 583
/*****************************************************************************/
/*
** Make an OpenGL context and drawable current.
*/
584

Kristian Høgsberg's avatar
Kristian Høgsberg committed
585
static int
586 587 588
DoMakeCurrent(__GLXclientState * cl,
              GLXDrawable drawId, GLXDrawable readId,
              GLXContextID contextId, GLXContextTag tag)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
589 590 591 592
{
    ClientPtr client = cl->client;
    xGLXMakeCurrentReply reply;
    __GLXcontext *glxc, *prevglxc;
593 594
    __GLXdrawable *drawPriv = NULL;
    __GLXdrawable *readPriv = NULL;
595
    int error;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
596

597 598 599
    /* Drawables but no context makes no sense */
    if (!contextId && (drawId || readId))
        return BadMatch;
600

601 602
    /* If either drawable is null, the other must be too */
    if ((drawId == None) != (readId == None))
603 604
        return BadMatch;

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
605
    /*
606 607
     ** Lookup old context.  If we have one, it must be in a usable state.
     */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
608
    if (tag != 0) {
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
        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
624 625 626
    }

    /*
627 628
     ** Lookup new context.  It must not be current for someone else.
     */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
629
    if (contextId != None) {
630
        int status;
631

632 633
        if (!validGlxContext(client, contextId, DixUseAccess, &glxc, &error))
            return error;
634
        if ((glxc != prevglxc) && glxc->currentClient) {
635 636 637
            /* Context is current to somebody else */
            return BadAccess;
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
638

639 640 641 642 643
        if (drawId) {
            drawPriv = __glXGetDrawable(glxc, drawId, client, &status);
            if (drawPriv == NULL)
                return status;
        }
644

645 646 647 648 649 650
        if (readId) {
            readPriv = __glXGetDrawable(glxc, readId, client, &status);
            if (readPriv == NULL)
                return status;
        }
    } else {
651
        /* Switching to no context.  Ignore new drawable. */
652

653 654 655 656
        glxc = 0;
        drawPriv = 0;
        readPriv = 0;
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
657 658

    if (prevglxc) {
659 660 661
        /*
         ** Flush the previous context if needed.
         */
662
        Bool need_flush = !prevglxc->isDirect;
663 664 665 666
#ifdef GLX_CONTEXT_RELEASE_BEHAVIOR_ARB
        if (prevglxc->releaseBehavior == GLX_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB)
            need_flush = GL_FALSE;
#endif
667
        if (need_flush) {
668
            if (__glXForceCurrent(cl, tag, (int *) &error)) {
669
                glFlush();
670 671 672 673 674 675 676 677 678 679 680 681
            }
            else {
                return error;
            }
        }

        /*
         ** Make the previous context not current.
         */
        if (!(*prevglxc->loseCurrent) (prevglxc)) {
            return __glXError(GLXBadContext);
        }
682
        lastGLContext = NULL;
683 684 685 686 687
        if (!prevglxc->isDirect) {
            prevglxc->drawPriv = NULL;
            prevglxc->readPriv = NULL;
        }
    }
688

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

691 692
        glxc->drawPriv = drawPriv;
        glxc->readPriv = readPriv;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
693

694
        /* make the context current */
695
        lastGLContext = glxc;
696
        if (!(*glxc->makeCurrent) (glxc)) {
697
            lastGLContext = NULL;
698 699 700 701
            glxc->drawPriv = NULL;
            glxc->readPriv = NULL;
            return __glXError(GLXBadContext);
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
702

703
        glxc->currentClient = client;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
704 705
    }

Adam Jackson's avatar
Adam Jackson committed
706
    StopUsingContext(prevglxc);
707

708 709 710 711 712 713 714
    reply = (xGLXMakeCurrentReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .contextTag = 0
    };

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
715
    if (glxc) {
716 717 718
        StartUsingContext(cl, glxc);
        reply.contextTag = glxc->id;
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
719 720

    if (client->swapped) {
721 722 723 724
        __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
725
    }
726
    WriteToClient(client, sz_xGLXMakeCurrentReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
727 728 729
    return Success;
}

730 731
int
__glXDisp_MakeCurrent(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
732
{
Julien Cristau's avatar
Julien Cristau committed
733
    ClientPtr client = cl->client;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
734 735
    xGLXMakeCurrentReq *req = (xGLXMakeCurrentReq *) pc;

Julien Cristau's avatar
Julien Cristau committed
736 737
    REQUEST_SIZE_MATCH(xGLXMakeCurrentReq);

738 739
    return DoMakeCurrent(cl, req->drawable, req->drawable,
                         req->context, req->oldContextTag);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
740 741
}

742 743
int
__glXDisp_MakeContextCurrent(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
744
{
Julien Cristau's avatar
Julien Cristau committed
745
    ClientPtr client = cl->client;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
746 747
    xGLXMakeContextCurrentReq *req = (xGLXMakeContextCurrentReq *) pc;

Julien Cristau's avatar
Julien Cristau committed
748 749
    REQUEST_SIZE_MATCH(xGLXMakeContextCurrentReq);

750 751
    return DoMakeCurrent(cl, req->drawable, req->readdrawable,
                         req->context, req->oldContextTag);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
752 753
}

754 755
int
__glXDisp_MakeCurrentReadSGI(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
756
{
Julien Cristau's avatar
Julien Cristau committed
757
    ClientPtr client = cl->client;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
758 759
    xGLXMakeCurrentReadSGIReq *req = (xGLXMakeCurrentReadSGIReq *) pc;

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

762 763
    return DoMakeCurrent(cl, req->drawable, req->readable,
                         req->context, req->oldContextTag);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
764 765
}

766 767
int
__glXDisp_IsDirect(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
768 769 770 771 772
{
    ClientPtr client = cl->client;
    xGLXIsDirectReq *req = (xGLXIsDirectReq *) pc;
    xGLXIsDirectReply reply;
    __GLXcontext *glxc;
773
    int err;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
774

Julien Cristau's avatar
Julien Cristau committed
775 776
    REQUEST_SIZE_MATCH(xGLXIsDirectReq);

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

780 781 782 783 784 785
    reply = (xGLXIsDirectReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .isDirect = glxc->isDirect
    };
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
786 787

    if (client->swapped) {
788 789 790
        __GLX_DECLARE_SWAP_VARIABLES;
        __GLX_SWAP_SHORT(&reply.sequenceNumber);
        __GLX_SWAP_INT(&reply.length);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
791
    }
792
    WriteToClient(client, sz_xGLXIsDirectReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
793 794 795 796

    return Success;
}

797 798
int
__glXDisp_QueryVersion(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
799 800 801 802 803 804
{
    ClientPtr client = cl->client;
    xGLXQueryVersionReq *req = (xGLXQueryVersionReq *) pc;
    xGLXQueryVersionReply reply;
    GLuint major, minor;

Julien Cristau's avatar
Julien Cristau committed
805 806
    REQUEST_SIZE_MATCH(xGLXQueryVersionReq);

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
807 808
    major = req->majorVersion;
    minor = req->minorVersion;
809 810
    (void) major;
    (void) minor;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
811 812

    /*
813 814 815 816
     ** 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.
     */
817 818 819 820
    reply = (xGLXQueryVersionReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
821 822
        .majorVersion = SERVER_GLX_MAJOR_VERSION,
        .minorVersion = SERVER_GLX_MINOR_VERSION
823
    };
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
824 825

    if (client->swapped) {
826 827 828 829 830
        __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
831
    }
832 833

    WriteToClient(client, sz_xGLXQueryVersionReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
834 835 836
    return Success;
}

837 838
int
__glXDisp_WaitGL(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
839
{
Julien Cristau's avatar
Julien Cristau committed
840
    ClientPtr client = cl->client;
841
    xGLXWaitGLReq *req = (xGLXWaitGLReq *) pc;
Julien Cristau's avatar
Julien Cristau committed
842
    GLXContextTag tag;
843
    __GLXcontext *glxc = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
844
    int error;
845

Julien Cristau's avatar
Julien Cristau committed
846 847 848
    REQUEST_SIZE_MATCH(xGLXWaitGLReq);

    tag = req->contextTag;
849
    if (tag) {
850 851 852 853 854 855
        glxc = __glXLookupContextByTag(cl, tag);
        if (!glxc)
            return __glXError(GLXBadContextTag);

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

857
        glFinish();
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
858
    }
859 860

    if (glxc && glxc->drawPriv->waitGL)
861
        (*glxc->drawPriv->waitGL) (glxc->drawPriv);
862

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
863 864 865
    return Success;
}

866 867
int
__glXDisp_WaitX(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
868
{
Julien Cristau's avatar
Julien Cristau committed
869
    ClientPtr client = cl->client;
870
    xGLXWaitXReq *req = (xGLXWaitXReq *) pc;
Julien Cristau's avatar
Julien Cristau committed
871
    GLXContextTag tag;
872
    __GLXcontext *glxc = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
873
    int error;
874

Julien Cristau's avatar
Julien Cristau committed
875 876 877
    REQUEST_SIZE_MATCH(xGLXWaitXReq);

    tag = req->contextTag;
878
    if (tag) {
879 880 881 882 883 884
        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
885
    }
886

Pierre Willenbrock's avatar
Pierre Willenbrock committed
887
    if (glxc && glxc->drawPriv->waitX)
888
        (*glxc->drawPriv->waitX) (glxc->drawPriv);