summaryrefslogtreecommitdiff
path: root/hw/dmx/glxProxy/glxswap.c
blob: 5f565010d3e1b2466d9db281ca6e90d113951564 (plain)
1
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * Copyright 2003 Red Hat Inc., Raleigh, North Carolina.
 *
 * 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 on 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 and this permission notice (including the
 * next paragraph) 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
 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
 * 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.
 */

/*
 * Authors:
 *   Kevin E. Martin <kem@redhat.com>
 *
 */

#ifdef HAVE_DMX_CONFIG_H
#include <dmx-config.h>
#endif

#include "dmx.h"
#include "dmxwindow.h"
#include "glxserver.h"
#include "glxswap.h"

extern int __glXDoSwapBuffers(__GLXclientState * cl, XID drawId,
                              GLXContextTag tag);

typedef struct _SwapGroup *SwapGroupPtr;

static Bool SwapBarrierIsReadyToSwap(GLuint barrier);
static void SwapSwapBarrier(GLuint barrier);
static void UpdateSwapBarrierList(GLuint barrier,
                                  SwapGroupPtr pOldSwap, SwapGroupPtr pNewSwap);

/************************************************************************
 *
 * Swap Groups
 *
 ************************************************************************/

typedef struct _SwapGroup {
    WindowPtr pWin;
    SwapGroupPtr pNext;

    Bool swapping;
    Bool sleeping;
    GLuint barrier;

    XID drawable;
    GLXContextTag tag;
    __GLXclientState *clState;
} SwapGroupRec;

static void
SwapSwapGroup(SwapGroupPtr pSwap)
{
    SwapGroupPtr pCur;

    /* All drawables in swap group are ready to swap, so just swap all
     * drawables buffers and then wake up those clients that were
     * previously sleeping */

    for (pCur = pSwap; pCur; pCur = pCur->pNext) {
        if (pCur->swapping) {
            /* Swap pCur's buffers */
            __glXDoSwapBuffers(pCur->clState, pCur->drawable, pCur->tag);
            pCur->swapping = FALSE;
        }

        /* Wakeup client */
        if (pCur->sleeping) {
            ClientWakeup(pCur->clState->client);
            pCur->sleeping = FALSE;
        }
    }
}

static Bool
SwapGroupIsReadyToSwap(SwapGroupPtr pSwap)
{
    Bool isReady = TRUE;

    /* The swap group is ready to swap when all drawables are ready to
     * swap.  NOTE: A drawable is also ready to swap if it is not
     * currently mapped */
    for (; pSwap; pSwap = pSwap->pNext) {
        isReady &= (pSwap->swapping || !pSwap->pWin->mapped);
        /* FIXME: Should we use pSwap->pWin->mapped or ...->realized ??? */
    }

    return isReady;
}

static Bool
SGSwapCleanup(ClientPtr client, void *closure)
{
    /* SwapGroupPtr  pSwap = (SwapGroupPtr)closure; */

    /* This should not be called unless the client has died in which
     * case we should remove the buffer from the swap list */

    return TRUE;
}

int
SGSwapBuffers(__GLXclientState * cl, XID drawId, GLXContextTag tag,
              DrawablePtr pDraw)
{
    WindowPtr pWin = (WindowPtr) pDraw;
    dmxWinPrivPtr pWinPriv = DMX_GET_WINDOW_PRIV(pWin);
    SwapGroupPtr pSwap = pWinPriv->swapGroup;
    SwapGroupPtr pCur;

    for (pCur = pSwap; pCur && pCur->pWin != pWin; pCur = pCur->pNext);
    if (!pCur)
        return BadDrawable;

    pCur->clState = cl;
    pCur->drawable = drawId;
    pCur->tag = tag;

    /* We are now in the process of swapping */
    pCur->swapping = TRUE;

    if (pSwap->barrier && SwapBarrierIsReadyToSwap(pSwap->barrier)) {
        /* The swap group is bound to a barrier and the barrier is ready
         * to swap, so swap all the swap groups that are bound to this
         * group's swap barrier */
        SwapSwapBarrier(pSwap->barrier);
    }
    else if (!pSwap->barrier && SwapGroupIsReadyToSwap(pSwap)) {
        /* Do the swap if the entire swap group is ready to swap and the
         * group is not bound to a swap barrier */
        SwapSwapGroup(pSwap);
    }
    else {
        /* The swap group/barrier is not yet ready to swap, so put
         * client to sleep until the rest are ready to swap */
        ClientSleep(cl->client, SGSwapCleanup, (void *) pWin);
        pCur->sleeping = TRUE;
    }

    return Success;
}

static void
SGWindowUnmapped(WindowPtr pWin)
{
    dmxWinPrivPtr pWinPriv = DMX_GET_WINDOW_PRIV(pWin);
    SwapGroupPtr pSwap = pWinPriv->swapGroup;

    /* Now that one of the windows in the swap group has been unmapped,
     * see if the entire swap group/barrier is ready to swap */

    if (pSwap->barrier && SwapBarrierIsReadyToSwap(pSwap->barrier)) {
        SwapSwapBarrier(pSwap->barrier);
    }
    else if (!pSwap->barrier && SwapGroupIsReadyToSwap(pSwap)) {
        SwapSwapGroup(pSwap);
    }
}

static void
SGWindowDestroyed(WindowPtr pWin)
{
    JoinSwapGroupSGIX((DrawablePtr) pWin, NULL);
}

static SwapGroupPtr
CreateSwapEntry(WindowPtr pWin)
{
    SwapGroupPtr pEntry;

    /* Allocate new swap group */
    pEntry = malloc(sizeof(*pEntry));
    if (!pEntry)
        return NULL;

    /* Initialize swap group */
    pEntry->pWin = pWin;
    pEntry->pNext = NULL;
    pEntry->swapping = FALSE;
    pEntry->sleeping = FALSE;
    pEntry->barrier = 0;
    /* The following are not initialized until SwapBuffers is called:
     *     pEntry->drawable
     *     pEntry->tag
     *     pEntry->clState
     */

    return pEntry;
}

static void
FreeSwapEntry(SwapGroupPtr pEntry)
{
    /* Since we have removed the drawable from its previous swap group
     * and it won't be added to another swap group, the only thing that
     * we need to do is to make sure that the drawable's client is not
     * sleeping.  This could happen if one thread is sleeping, while
     * another thread called glxJoinSwapGroup().  Note that all sleeping
     * threads should also be swapping, but there is a small window in
     * the SGSwapBuffer() logic, above, where swapping can be set but
     * sleeping is not.  We check both independently here just to be
     * pedantic. */

    /* Handle swap buffer request */
    if (pEntry->swapping)
        __glXDoSwapBuffers(pEntry->clState, pEntry->drawable, pEntry->tag);

    /* Wake up client */
    if (pEntry->sleeping)
        ClientWakeup(pEntry->clState->client);

    /* We can free the pEntry entry since it has already been removed
     * from the swap group list and it won't be needed any longer */
    free(pEntry);
}

int
JoinSwapGroupSGIX(DrawablePtr pDraw, DrawablePtr pMember)
{
    if (pDraw->type == DRAWABLE_WINDOW) {
        WindowPtr pWin = (WindowPtr) pDraw;
        dmxWinPrivPtr pWinPriv = DMX_GET_WINDOW_PRIV(pWin);
        SwapGroupPtr pOldSwap = NULL;
        SwapGroupPtr pEntry;

        /* If pDraw and pMember are already members of the same swap
         * group, just return Success since there is nothing to do */
        for (pEntry = pWinPriv->swapGroup; pEntry; pEntry = pEntry->pNext)
            if (pEntry->pWin == (WindowPtr) pMember)
                return Success;

        /* Remove pDraw from its current swap group */
        if (pWinPriv->swapGroup) {
            SwapGroupPtr pSwapGroup = pWinPriv->swapGroup;
            SwapGroupPtr pPrev;

            /* Find old swap entry in swap group and save in pOldSwap
             * for later use */
            for (pOldSwap = pWinPriv->swapGroup, pPrev = NULL;
                 pOldSwap && pOldSwap->pWin != pWin;
                 pPrev = pOldSwap, pOldSwap = pOldSwap->pNext);
            if (!pOldSwap)
                return BadDrawable;

            /* Remove pDraw's swap group entry from swap group list */
            if (pPrev) {
                pPrev->pNext = pOldSwap->pNext;
            }
            else {
                /* pWin is at the head of the swap group list, so we
                 * need to update all other members of this swap
                 * group */
                for (pEntry = pOldSwap->pNext; pEntry; pEntry = pEntry->pNext)
                    DMX_GET_WINDOW_PRIV(pEntry->pWin)->swapGroup
                        = pOldSwap->pNext;

                /* Update the barrier list as well */
                if (pOldSwap->barrier)
                    UpdateSwapBarrierList(pOldSwap->barrier,
                                          pOldSwap, pOldSwap->pNext);

                /* Set pSwapGroup to point to the swap group without
                 * pOldSwap */
                pSwapGroup = pOldSwap->pNext;
            }

            /* Check to see if current swap group can now swap since we
             * know at this point that pDraw and pMember are guaranteed
             * to previously be in different swap groups */
            if (pSwapGroup && SwapGroupIsReadyToSwap(pSwapGroup)) {
                SwapSwapGroup(pSwapGroup);
            }

            /* Make the old swap entry a standalone group */
            pOldSwap->pNext = NULL;
            pOldSwap->barrier = 0;

            /* Reset pWin's swap group */
            pWinPriv->swapGroup = NULL;
            pWinPriv->windowDestroyed = NULL;
            pWinPriv->windowUnmapped = NULL;
        }

        if (!pMember || pMember->type != DRAWABLE_WINDOW) {
            /* Free old swap group since it is no longer needed */
            if (pOldSwap)
                FreeSwapEntry(pOldSwap);
        }
        else if (pDraw == pMember && pOldSwap) {
            /* Special case where pDraw was previously created and we
             * are now just putting it to its own swap group */
            pWinPriv->swapGroup = pOldSwap;
            pWinPriv->windowDestroyed = SGWindowDestroyed;
            pWinPriv->windowUnmapped = SGWindowUnmapped;

            /* Check to see if pDraw is ready to swap */
            if (SwapGroupIsReadyToSwap(pOldSwap))
                SwapSwapGroup(pOldSwap);
        }
        else if (pMember->type == DRAWABLE_WINDOW) {
            WindowPtr pMemberWin = (WindowPtr) pMember;
            dmxWinPrivPtr pMemberPriv = DMX_GET_WINDOW_PRIV(pMemberWin);
            SwapGroupPtr pMemberSwapGroup = pMemberPriv->swapGroup;

            /* Finally, how we can add pDraw to pMember's swap group */

            /* If pMember is not currently in a swap group, then create
             * one for it since we are just about to add pDraw to it. */
            if (!pMemberSwapGroup) {
                /* Create new swap group */
                pMemberSwapGroup = CreateSwapEntry(pMemberWin);
                if (!pMemberSwapGroup) {
                    if (pOldSwap)
                        FreeSwapEntry(pOldSwap);
                    return BadAlloc;
                }

                /* Set pMember's swap group */
                pMemberPriv->swapGroup = pMemberSwapGroup;
                pMemberPriv->windowDestroyed = SGWindowDestroyed;
                pMemberPriv->windowUnmapped = SGWindowUnmapped;
            }

            /* If pDraw == pMember, that means pDraw was not a member of
             * a group previously (or it would have been handled by the
             * special case above), so no additional work is required
             * since we just created a new swap group for pMember (i.e.,
             * pDraw). */

            if (pDraw != pMember) {
                /* If pDraw was not previously in a swap group, then create
                 * an entry for it */
                if (!pOldSwap) {
                    /* Create new swap group */
                    pOldSwap = CreateSwapEntry(pWin);
                    if (!pOldSwap) {
                        /* If we just created a swap group for pMember, we
                         * need to free it here */
                        if (pMemberSwapGroup->pNext == NULL) {
                            FreeSwapEntry(pMemberSwapGroup);
                            pMemberPriv->swapGroup = NULL;
                        }
                        return BadAlloc;
                    }
                }

                /* Find last entry in pMember's swap group */
                for (pEntry = pMemberSwapGroup;
                     pEntry->pNext; pEntry = pEntry->pNext);

                /* Add pDraw's swap group entry to pMember's swap group list */
                pEntry->pNext = pOldSwap;

                /* Add pDraw to pMember's swap barrier */
                pOldSwap->barrier = pEntry->barrier;

                /* Set pDraw's swap group */
                pWinPriv->swapGroup = pMemberSwapGroup;
                pWinPriv->windowDestroyed = SGWindowDestroyed;
                pWinPriv->windowUnmapped = SGWindowUnmapped;
            }
        }
    }

    return Success;
}

/************************************************************************
 *
 * Swap Barriers
 *
 ************************************************************************/

#define GLX_MAX_SWAP_BARRIERS 10

typedef struct _SwapBarrier *SwapBarrierPtr;
typedef struct _SwapBarrier {
    SwapGroupPtr pSwap;
    SwapBarrierPtr pNext;
} SwapBarrierRec;

static SwapBarrierPtr SwapBarrierList[GLX_MAX_SWAP_BARRIERS + 1];

void
SwapBarrierInit(void)
{
    int i;

    for (i = 0; i <= GLX_MAX_SWAP_BARRIERS; i++)
        SwapBarrierList[i] = NULL;
}

void
SwapBarrierReset(void)
{
    int i;

    for (i = 0; i <= GLX_MAX_SWAP_BARRIERS; i++) {
        SwapBarrierPtr pBarrier, pNextBarrier;

        for (pBarrier = SwapBarrierList[i]; pBarrier; pBarrier = pNextBarrier) {
            pNextBarrier = pBarrier->pNext;
            free(pBarrier);
        }
        SwapBarrierList[i] = NULL;
    }
}

int
QueryMaxSwapBarriersSGIX(int screen)
{
    return GLX_MAX_SWAP_BARRIERS;
}

static Bool
BindSwapGroupToBarrier(GLuint barrier, SwapGroupPtr pSwapGroup)
{
    SwapBarrierPtr pBarrier;

    pBarrier = malloc(sizeof(*pBarrier));
    if (!pBarrier)
        return FALSE;

    /* Add the swap group to barrier's list */
    pBarrier->pSwap = pSwapGroup;
    pBarrier->pNext = SwapBarrierList[barrier];
    SwapBarrierList[barrier] = pBarrier;

    return TRUE;
}

static Bool
UnbindSwapGroupFromBarrier(GLuint barrier, SwapGroupPtr pSwapGroup)
{
    SwapBarrierPtr pBarrier, pPrevBarrier;

    /* Find the swap group in barrier's list */
    for (pBarrier = SwapBarrierList[barrier], pPrevBarrier = NULL;
         pBarrier && pBarrier->pSwap != pSwapGroup;
         pPrevBarrier = pBarrier, pBarrier = pBarrier->pNext);
    if (!pBarrier)
        return FALSE;

    /* Remove the swap group from barrier's list */
    if (pPrevBarrier)
        pPrevBarrier->pNext = pBarrier->pNext;
    else
        SwapBarrierList[barrier] = pBarrier->pNext;

    /* Free memory */
    free(pBarrier);

    return TRUE;
}

static void
UpdateSwapBarrierList(GLuint barrier,
                      SwapGroupPtr pOldSwap, SwapGroupPtr pNewSwap)
{
    SwapBarrierPtr pBarrier;

    /* If the old swap group is being destroyed, then we need to remove
     * the swap group from the list entirely */
    if (!pNewSwap) {
        UnbindSwapGroupFromBarrier(barrier, pOldSwap);
        return;
    }

    /* Otherwise, find the old swap group in the barrier list and change
     * it to the new swap group */
    for (pBarrier = SwapBarrierList[barrier];
         pBarrier; pBarrier = pBarrier->pNext) {
        if (pBarrier->pSwap == pOldSwap) {
            pBarrier->pSwap = pNewSwap;
            return;
        }
    }
}

static Bool
SwapBarrierIsReadyToSwap(GLuint barrier)
{
    SwapBarrierPtr pBarrier;
    Bool isReady = TRUE;

    /* The swap barier is ready to swap when swap groups that are bound
     * to barrier are ready to swap */
    for (pBarrier = SwapBarrierList[barrier];
         pBarrier; pBarrier = pBarrier->pNext)
        isReady &= SwapGroupIsReadyToSwap(pBarrier->pSwap);

    return isReady;
}

static void
SwapSwapBarrier(GLuint barrier)
{
    SwapBarrierPtr pBarrier;

    /* Swap each group that is a member of this barrier */
    for (pBarrier = SwapBarrierList[barrier];
         pBarrier; pBarrier = pBarrier->pNext)
        SwapSwapGroup(pBarrier->pSwap);
}

int
BindSwapBarrierSGIX(DrawablePtr pDraw, int barrier)
{
    /* FIXME: Check for errors when pDraw->type != DRAWABLE_WINDOW */

    if (barrier < 0 || barrier > GLX_MAX_SWAP_BARRIERS)
        return BadValue;

    if (pDraw->type == DRAWABLE_WINDOW) {
        WindowPtr pWin = (WindowPtr) pDraw;
        dmxWinPrivPtr pWinPriv = DMX_GET_WINDOW_PRIV(pWin);
        SwapGroupPtr pSwapGroup = pWinPriv->swapGroup;
        SwapGroupPtr pCur;

        if (!pSwapGroup)
            return BadDrawable;
        if (barrier && pSwapGroup->barrier)
            return BadValue;

        /* Update the swap barrier list */
        if (barrier) {
            if (!BindSwapGroupToBarrier(barrier, pSwapGroup))
                return BadAlloc;
        }
        else {
            if (!UnbindSwapGroupFromBarrier(pSwapGroup->barrier, pSwapGroup))
                return BadDrawable;
        }

        /* Set the barrier for each member of this swap group */
        for (pCur = pSwapGroup; pCur; pCur = pCur->pNext)
            pCur->barrier = barrier;
    }

    return Success;
}