glxcmds.c 74.4 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
#include "glxserver.h"
#include <GL/glxtokens.h>
Adam Jackson's avatar
Adam Jackson committed
40
#include <X11/extensions/presenttokens.h>
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
41 42 43 44 45
#include <unpack.h>
#include <pixmapstr.h>
#include <windowstr.h>
#include "glxutil.h"
#include "glxext.h"
46
#include "indirect_dispatch.h"
47 48
#include "indirect_table.h"
#include "indirect_util.h"
49
#include "protocol-versions.h"
50
#include "glxvndabi.h"
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
51

52 53
static char GLXServerVendorName[] = "SGI";

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

68 69 70
    return TRUE;
}

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

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

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

    return FALSE;
}

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

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

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

    return FALSE;
}

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

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

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

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

135
_X_HIDDEN int
136
validGlxContext(ClientPtr client, XID id, int access_mode,
137
                __GLXcontext ** context, int *err)
138
{
139 140 141 142
    /* no ghost contexts */
    if (id & SERVER_BIT)
        return FALSE;

143
    *err = dixLookupResourceByType((void **) context, id,
144
                                   __glXContextRes, client, access_mode);
Adam Jackson's avatar
Adam Jackson committed
145
    if (*err != Success || (*context)->idExists == GL_FALSE) {
146 147 148 149
        client->errorValue = id;
        if (*err == BadValue || *err == Success)
            *err = __glXError(GLXBadContext);
        return FALSE;
150 151 152 153 154
    }

    return TRUE;
}

Jon Turney's avatar
Jon Turney committed
155
int
156
validGlxDrawable(ClientPtr client, XID id, int type, int access_mode,
157
                 __GLXdrawable ** drawable, int *err)
158 159 160
{
    int rc;

161
    rc = dixLookupResourceByType((void **) drawable, id,
162
                                 __glXDrawableRes, client, access_mode);
163
    if (rc != Success && rc != BadValue) {
164 165 166
        *err = rc;
        client->errorValue = id;
        return FALSE;
167 168
    }

169 170 171
    /* 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). */
172
    if (rc == BadValue ||
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        (*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;
        }
190 191 192 193 194
    }

    return TRUE;
}

195
void
196
__glXContextDestroy(__GLXcontext * context)
197
{
198
    lastGLContext = NULL;
199 200
}

201 202
static void
__glXdirectContextDestroy(__GLXcontext * context)
203
{
204
    __glXContextDestroy(context);
205
    free(context);
206 207
}

208 209 210 211 212 213
static int
__glXdirectContextLoseCurrent(__GLXcontext * context)
{
    return GL_TRUE;
}

214
_X_HIDDEN __GLXcontext *
215 216
__glXdirectContextCreate(__GLXscreen * screen,
                         __GLXconfig * modes, __GLXcontext * shareContext)
217 218 219
{
    __GLXcontext *context;

220
    context = calloc(1, sizeof(__GLXcontext));
221
    if (context == NULL)
222
        return NULL;
223

224
    context->config = modes;
225
    context->destroy = __glXdirectContextDestroy;
226
    context->loseCurrent = __glXdirectContextLoseCurrent;
227 228 229 230

    return context;
}

Eric Anholt's avatar
Eric Anholt committed
231 232 233
/**
 * Create a GL context with the given properties.  This routine is used
 * to implement \c glXCreateContext, \c glXCreateNewContext, and
234
 * \c glXCreateContextWithConfigSGIX.  This works because of the hack way
Eric Anholt's avatar
Eric Anholt committed
235 236 237 238
 * that GLXFBConfigs are implemented.  Basically, the FBConfigID is the
 * same as the VisualID.
 */

Kristian Høgsberg's avatar
Kristian Høgsberg committed
239
static int
240 241 242
DoCreateContext(__GLXclientState * cl, GLXContextID gcId,
                GLXContextID shareList, __GLXconfig * config,
                __GLXscreen * pGlxScreen, GLboolean isDirect)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
243 244 245
{
    ClientPtr client = cl->client;
    __GLXcontext *glxc, *shareglxc;
246
    int err;
247

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

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

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

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

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

325 326 327 328 329 330 331 332 333 334
    /* 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;

335 336 337 338 339 340 341 342 343 344 345 346 347
#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

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

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
356 357 358
    return Success;
}

359 360
int
__glXDisp_CreateContext(__GLXclientState * cl, GLbyte * pc)
Eric Anholt's avatar
Eric Anholt committed
361 362
{
    xGLXCreateContextReq *req = (xGLXCreateContextReq *) pc;
363
    __GLXconfig *config;
364 365
    __GLXscreen *pGlxScreen;
    int err;
Eric Anholt's avatar
Eric Anholt committed
366

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

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

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

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

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

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

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

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

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

414 415
int
__glXDisp_DestroyContext(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
416 417 418
{
    xGLXDestroyContextReq *req = (xGLXDestroyContextReq *) pc;
    __GLXcontext *glxc;
419 420 421
    int err;

    if (!validGlxContext(cl->client, req->context, DixDestroyAccess,
422 423
                         &glxc, &err))
        return err;
424

425
    glxc->idExists = GL_FALSE;
426 427 428 429 430 431 432 433 434 435
    if (glxc->currentClient) {
        XID ghost = FakeClientID(glxc->currentClient->index);

        if (!AddResource(ghost, __glXContextRes, glxc))
            return BadAlloc;
        ChangeResourceValue(glxc->id, __glXContextRes, NULL);
        glxc->id = ghost;
    }

    FreeResourceByType(req->context, __glXContextRes, FALSE);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
436

Adam Jackson's avatar
Adam Jackson committed
437
    return Success;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
438 439
}

440 441
__GLXcontext *
__glXLookupContextByTag(__GLXclientState * cl, GLXContextTag tag)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
442
{
443
    return glxServer.getContextTagPrivate(cl->client, tag);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
444 445
}

446 447 448 449 450 451 452 453 454 455 456 457
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;
}

458
/**
459 460 461 462
 * 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.
463
 */
Kristian Høgsberg's avatar
Kristian Høgsberg committed
464
static __GLXdrawable *
465 466
__glXGetDrawable(__GLXcontext * glxc, GLXDrawable drawId, ClientPtr client,
                 int *error)
467 468
{
    DrawablePtr pDraw;
469
    __GLXdrawable *pGlxDraw;
470 471
    __GLXconfig *config;
    __GLXscreen *pGlxScreen;
472
    int rc;
473

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

484
        return pGlxDraw;
485 486
    }

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

494 495 496 497
    /* 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. */
498

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

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

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    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))
531
        return NULL;
532

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

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

Kristian Høgsberg's avatar
Kristian Høgsberg committed
546
    return pGlxDraw;
547 548
}

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

554 555 556
int
xorgGlxMakeCurrent(ClientPtr client, GLXContextTag tag, XID drawId, XID readId,
                   XID contextId, GLXContextTag newContextTag)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
557
{
558 559
    __GLXclientState *cl = glxGetClient(client);
    __GLXcontext *glxc = NULL, *prevglxc = NULL;
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;

572
    /* Look up old context. If we have one, it must be in a usable state. */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
573
    if (tag != 0) {
574 575
        prevglxc = glxServer.getContextTagPrivate(client, tag);
        if (!prevglxc)
576
            return __glXError(GLXBadContextTag);
577

578 579 580 581 582 583
        if (prevglxc->renderMode != GL_RENDER) {
            /* Oops.  Not in render mode render. */
            client->errorValue = prevglxc->id;
            return __glXError(GLXBadContextState);
        }
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
584

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

589 590
        if (!validGlxContext(client, contextId, DixUseAccess, &glxc, &error))
            return error;
591 592

        if ((glxc != prevglxc) && glxc->currentClient)
593
            return BadAccess;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
594

595 596 597 598 599
        if (drawId) {
            drawPriv = __glXGetDrawable(glxc, drawId, client, &status);
            if (drawPriv == NULL)
                return status;
        }
600

601 602 603 604 605
        if (readId) {
            readPriv = __glXGetDrawable(glxc, readId, client, &status);
            if (readPriv == NULL)
                return status;
        }
606
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
607 608

    if (prevglxc) {
609
        /* Flush the previous context if needed. */
610
        Bool need_flush = !prevglxc->isDirect;
611 612 613 614
#ifdef GLX_CONTEXT_RELEASE_BEHAVIOR_ARB
        if (prevglxc->releaseBehavior == GLX_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB)
            need_flush = GL_FALSE;
#endif
615
        if (need_flush) {
616
            if (!__glXForceCurrent(cl, tag, (int *) &error))
617
                return error;
618
            glFlush();
619 620
        }

621 622
        /* Make the previous context not current. */
        if (!(*prevglxc->loseCurrent) (prevglxc))
623
            return __glXError(GLXBadContext);
624

625
        lastGLContext = NULL;
626 627 628 629 630
        if (!prevglxc->isDirect) {
            prevglxc->drawPriv = NULL;
            prevglxc->readPriv = NULL;
        }
    }
631

632
    if (glxc && !glxc->isDirect) {
633 634
        glxc->drawPriv = drawPriv;
        glxc->readPriv = readPriv;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
635

636
        /* make the context current */
637
        lastGLContext = glxc;
638
        if (!(*glxc->makeCurrent) (glxc)) {
639
            lastGLContext = NULL;
640 641 642 643
            glxc->drawPriv = NULL;
            glxc->readPriv = NULL;
            return __glXError(GLXBadContext);
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
644

645
        glxc->currentClient = client;
646
        glxServer.setContextTagPrivate(client, newContextTag, glxc);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
647 648
    }

649 650 651 652 653
    if (prevglxc) {
        prevglxc->currentClient = NULL;
        if (!prevglxc->idExists) {
            FreeResourceByType(prevglxc->id, __glXContextRes, FALSE);
        }
654
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
655 656 657 658

    return Success;
}

659 660
int
__glXDisp_MakeCurrent(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
661
{
662
    return BadImplementation;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
663 664
}

665 666
int
__glXDisp_MakeContextCurrent(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
667
{
668
    return BadImplementation;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
669 670
}

671 672
int
__glXDisp_MakeCurrentReadSGI(__GLXclientState * cl, GLbyte * pc)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
673
{
674
    return BadImplementation;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
675 676
}

677 678
int
__glXDisp_IsDirect(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
679 680 681 682 683
{
    ClientPtr client = cl->client;
    xGLXIsDirectReq *req = (xGLXIsDirectReq *) pc;
    xGLXIsDirectReply reply;
    __GLXcontext *glxc;
684
    int err;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
685

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

689 690 691 692 693 694
    reply = (xGLXIsDirectReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
        .isDirect = glxc->isDirect
    };
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
695 696

    if (client->swapped) {
697 698 699
        __GLX_DECLARE_SWAP_VARIABLES;
        __GLX_SWAP_SHORT(&reply.sequenceNumber);
        __GLX_SWAP_INT(&reply.length);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
700
    }
701
    WriteToClient(client, sz_xGLXIsDirectReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
702 703 704 705

    return Success;
}

706 707
int
__glXDisp_QueryVersion(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
708 709 710 711 712 713
{
    ClientPtr client = cl->client;
    xGLXQueryVersionReq *req = (xGLXQueryVersionReq *) pc;
    xGLXQueryVersionReply reply;
    GLuint major, minor;

Julien Cristau's avatar
Julien Cristau committed
714 715
    REQUEST_SIZE_MATCH(xGLXQueryVersionReq);

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
716 717
    major = req->majorVersion;
    minor = req->minorVersion;
718 719
    (void) major;
    (void) minor;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
720 721

    /*
722 723 724 725
     ** 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.
     */
726 727 728 729
    reply = (xGLXQueryVersionReply) {
        .type = X_Reply,
        .sequenceNumber = client->sequence,
        .length = 0,
730 731
        .majorVersion = SERVER_GLX_MAJOR_VERSION,
        .minorVersion = SERVER_GLX_MINOR_VERSION
732
    };
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
733 734

    if (client->swapped) {
735 736 737 738 739
        __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
740
    }
741 742

    WriteToClient(client, sz_xGLXQueryVersionReply, &reply);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
743 744 745
    return Success;
}

746 747
int
__glXDisp_WaitGL(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
748
{
749
    xGLXWaitGLReq *req = (xGLXWaitGLReq *) pc;
Julien Cristau's avatar
Julien Cristau committed
750
    GLXContextTag tag;
751
    __GLXcontext *glxc = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
752
    int error;
753

Julien Cristau's avatar
Julien Cristau committed
754
    tag = req->contextTag;
755
    if (tag) {
756 757 758 759 760 761
        glxc = __glXLookupContextByTag(cl, tag);
        if (!glxc)
            return __glXError(GLXBadContextTag);

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

763
        glFinish();
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
764
    }
765 766

    if (glxc && glxc->drawPriv->waitGL)
767
        (*glxc->drawPriv->waitGL) (glxc->drawPriv);
768

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
769 770 771
    return Success;
}

772 773
int
__glXDisp_WaitX(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
774
{
775
    xGLXWaitXReq *req = (xGLXWaitXReq *) pc;
Julien Cristau's avatar
Julien Cristau committed
776
    GLXContextTag tag;
777
    __GLXcontext *glxc = NULL;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
778
    int error;
779

Julien Cristau's avatar
Julien Cristau committed
780
    tag = req->contextTag;
781
    if (tag) {
782 783 784 785 786 787
        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
788
    }
789

Pierre Willenbrock's avatar
Pierre Willenbrock committed
790
    if (glxc && glxc->drawPriv->waitX)
791
        (*glxc->drawPriv->waitX) (glxc->drawPriv);
792

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
793 794 795
    return Success;
}

796 797
int
__glXDisp_CopyContext(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
798 799 800
{
    ClientPtr client = cl->client;
    xGLXCopyContextReq *req = (xGLXCopyContextReq *) pc;
Julien Cristau's avatar
Julien Cristau committed
801 802 803 804
    GLXContextID source;
    GLXContextID dest;
    GLXContextTag tag;
    unsigned long mask;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
805 806 807
    __GLXcontext *src, *dst;
    int error;

Julien Cristau's avatar
Julien Cristau committed
808 809 810 811
    source = req->source;
    dest = req->dest;
    tag = req->contextTag;
    mask = req->mask;
812
    if (!validGlxContext(cl->client, source, DixReadAccess, &src, &error))
813
        return error;
814
    if (!validGlxContext(cl->client, dest, DixWriteAccess, &dst, &error))
815
        return error;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
816 817

    /*
818 819 820 821 822 823
     ** They must be in the same address space, and same screen.
     ** NOTE: no support for direct rendering contexts here.
     */
    if (src->isDirect || dst->isDirect || (src->pGlxScreen != dst->pGlxScreen)) {
        client->errorValue = source;
        return BadMatch;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
824 825 826
    }

    /*
827 828
     ** The destination context must not be current for any client.
     */
829
    if (dst->currentClient) {
830 831
        client->errorValue = dest;
        return BadAccess;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
832 833 834
    }

    if (tag) {
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
        __GLXcontext *tagcx = __glXLookupContextByTag(cl, tag);

        if (!tagcx) {
            return __glXError(GLXBadContextTag);
        }
        if (tagcx != src) {
            /*
             ** This would be caused by a faulty implementation of the client
             ** library.
             */
            return BadMatch;
        }
        /*
         ** In this case, glXCopyContext is in both GL and X streams, in terms
         ** of sequentiality.
         */
        if (__glXForceCurrent(cl, tag, &error)) {
            /*
             ** Do whatever is needed to make sure that all preceding requests
             ** in both streams are completed before the copy is executed.
             */
856
            glFinish();
857 858 859 860
        }
        else {
            return error;
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
861 862
    }
    /*
863 864 865 866 867
     ** Issue copy.  The only reason for failure is a bad mask.
     */
    if (!(*dst->copy) (dst, src, mask)) {
        client->errorValue = mask;
        return BadValue;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
868 869 870 871
    }
    return Success;
}

872 873
enum {
    GLX_VIS_CONFIG_UNPAIRED = 18,
874
    GLX_VIS_CONFIG_PAIRED = 22
875 876 877 878 879
};

enum {
    GLX_VIS_CONFIG_TOTAL = GLX_VIS_CONFIG_UNPAIRED + GLX_VIS_CONFIG_PAIRED
};
Eric Anholt's avatar
Eric Anholt committed
880

881 882
int
__glXDisp_GetVisualConfigs(__GLXclientState * cl, GLbyte * pc)
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
883
{
884
    xGLXGetVisualConfigsReq *req = (xGLXGetVisualConfigsReq *) pc;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
885 886
    ClientPtr client = cl->client;
    xGLXGetVisualConfigsReply reply;
887
    __GLXscreen *pGlxScreen;
888
    __GLXconfig *modes;
889
    CARD32 buf[GLX_VIS_CONFIG_TOTAL];