compinit.c 14.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a
5
6
7
8
9
 * 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:
10
 *
11
12
13
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
14
 *
15
16
17
18
19
20
21
 * 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
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
22
 *
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 * Copyright © 2003 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

44
45
46
47
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

48
#include "compint.h"
49
#include "compositeext.h"
50

51
52
53
DevPrivateKeyRec CompScreenPrivateKeyRec;
DevPrivateKeyRec CompWindowPrivateKeyRec;
DevPrivateKeyRec CompSubwindowsPrivateKeyRec;
54

55
static Bool
56
compCloseScreen(ScreenPtr pScreen)
57
{
58
59
    CompScreenPtr cs = GetCompScreen(pScreen);
    Bool ret;
60

61
    free(cs->alternateVisuals);
62

63
    pScreen->CloseScreen = cs->CloseScreen;
64
    pScreen->InstallColormap = cs->InstallColormap;
65
    pScreen->ChangeWindowAttributes = cs->ChangeWindowAttributes;
66
    pScreen->ReparentWindow = cs->ReparentWindow;
67
    pScreen->ConfigNotify = cs->ConfigNotify;
68
69
70
    pScreen->MoveWindow = cs->MoveWindow;
    pScreen->ResizeWindow = cs->ResizeWindow;
    pScreen->ChangeBorderWidth = cs->ChangeBorderWidth;
71

72
73
74
75
76
77
78
    pScreen->ClipNotify = cs->ClipNotify;
    pScreen->UnrealizeWindow = cs->UnrealizeWindow;
    pScreen->RealizeWindow = cs->RealizeWindow;
    pScreen->DestroyWindow = cs->DestroyWindow;
    pScreen->CreateWindow = cs->CreateWindow;
    pScreen->CopyWindow = cs->CopyWindow;
    pScreen->PositionWindow = cs->PositionWindow;
79

80
    pScreen->GetImage = cs->GetImage;
81
    pScreen->SourceValidate = cs->SourceValidate;
82

83
    free(cs);
84
    dixSetPrivate(&pScreen->devPrivates, CompScreenPrivateKey, NULL);
85
    ret = (*pScreen->CloseScreen) (pScreen);
86

87
88
89
    return ret;
}

90
static void
91
compInstallColormap(ColormapPtr pColormap)
92
{
93
94
95
96
    VisualPtr pVisual = pColormap->pVisual;
    ScreenPtr pScreen = pColormap->pScreen;
    CompScreenPtr cs = GetCompScreen(pScreen);
    int a;
97

98
    for (a = 0; a < cs->numAlternateVisuals; a++)
99
100
        if (pVisual->vid == cs->alternateVisuals[a])
            return;
101
102
103
104
105
106
    pScreen->InstallColormap = cs->InstallColormap;
    (*pScreen->InstallColormap) (pColormap);
    cs->InstallColormap = pScreen->InstallColormap;
    pScreen->InstallColormap = compInstallColormap;
}

107
108
109
110
111
/* Fake backing store via automatic redirection */
static Bool
compChangeWindowAttributes(WindowPtr pWin, unsigned long mask)
{
    ScreenPtr pScreen = pWin->drawable.pScreen;
112
    CompScreenPtr cs = GetCompScreen(pScreen);
113
114
115
116
117
    Bool ret;

    pScreen->ChangeWindowAttributes = cs->ChangeWindowAttributes;
    ret = pScreen->ChangeWindowAttributes(pWin, mask);

118
    if (ret && (mask & CWBackingStore) &&
119
        pScreen->backingStoreSupport != NotUseful) {
120
        if (pWin->backingStore != NotUseful && !pWin->backStorage) {
121
            compRedirectWindow(serverClient, pWin, CompositeRedirectAutomatic);
122
            pWin->backStorage = (void *) (intptr_t) 1;
123
        }
124
        else if (pWin->backingStore == NotUseful && pWin->backStorage) {
125
126
127
128
            compUnredirectWindow(serverClient, pWin,
                                 CompositeRedirectAutomatic);
            pWin->backStorage = NULL;
        }
129
130
131
132
133
134
135
    }

    pScreen->ChangeWindowAttributes = compChangeWindowAttributes;

    return ret;
}

136
static void
137
138
139
140
compGetImage(DrawablePtr pDrawable,
             int sx, int sy,
             int w, int h,
             unsigned int format, unsigned long planemask, char *pdstLine)
141
142
{
    ScreenPtr pScreen = pDrawable->pScreen;
143
    CompScreenPtr cs = GetCompScreen(pScreen);
144
145
146

    pScreen->GetImage = cs->GetImage;
    if (pDrawable->type == DRAWABLE_WINDOW)
147
        compPaintChildrenToWindow((WindowPtr) pDrawable);
148
149
150
151
152
    (*pScreen->GetImage) (pDrawable, sx, sy, w, h, format, planemask, pdstLine);
    cs->GetImage = pScreen->GetImage;
    pScreen->GetImage = compGetImage;
}

153
154
155
156
static void
compSourceValidate(DrawablePtr pDrawable,
                   int x, int y,
                   int width, int height, unsigned int subWindowMode)
157
158
{
    ScreenPtr pScreen = pDrawable->pScreen;
159
    CompScreenPtr cs = GetCompScreen(pScreen);
160
161
162

    pScreen->SourceValidate = cs->SourceValidate;
    if (pDrawable->type == DRAWABLE_WINDOW && subWindowMode == IncludeInferiors)
163
        compPaintChildrenToWindow((WindowPtr) pDrawable);
164
    if (pScreen->SourceValidate)
165
166
        (*pScreen->SourceValidate) (pDrawable, x, y, width, height,
                                    subWindowMode);
167
168
169
170
    cs->SourceValidate = pScreen->SourceValidate;
    pScreen->SourceValidate = compSourceValidate;
}

171
172
173
174
175
/*
 * Add alternate visuals -- always expose an ARGB32 and RGB24 visual
 */

static DepthPtr
176
compFindVisuallessDepth(ScreenPtr pScreen, int d)
177
{
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    int i;

    for (i = 0; i < pScreen->numDepths; i++) {
        DepthPtr depth = &pScreen->allowedDepths[i];

        if (depth->depth == d) {
            /*
             * Make sure it doesn't have visuals already
             */
            if (depth->numVids)
                return 0;
            /*
             * looks fine
             */
            return depth;
        }
194
195
196
197
198
199
200
201
    }
    /*
     * If there isn't one, then it's gonna be hard to have 
     * an associated visual
     */
    return 0;
}

202
203
204
205
/*
 * Add a list of visual IDs to the list of visuals to implicitly redirect.
 */
static Bool
206
compRegisterAlternateVisuals(CompScreenPtr cs, VisualID * vids, int nVisuals)
207
208
209
{
    VisualID *p;

210
    p = realloc(cs->alternateVisuals,
211
212
213
                sizeof(VisualID) * (cs->numAlternateVisuals + nVisuals));
    if (p == NULL)
        return FALSE;
214
215
216
217
218
219
220
221
222

    memcpy(&p[cs->numAlternateVisuals], vids, sizeof(VisualID) * nVisuals);

    cs->alternateVisuals = p;
    cs->numAlternateVisuals += nVisuals;

    return TRUE;
}

223
224
225
Bool
CompositeRegisterAlternateVisuals(ScreenPtr pScreen, VisualID * vids,
                                  int nVisuals)
226
{
227
228
    CompScreenPtr cs = GetCompScreen(pScreen);

229
230
231
    return compRegisterAlternateVisuals(cs, vids, nVisuals);
}

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
Bool
CompositeRegisterImplicitRedirectionException(ScreenPtr pScreen,
                                              VisualID parentVisual,
                                              VisualID winVisual)
{
    CompScreenPtr cs = GetCompScreen(pScreen);
    CompImplicitRedirectException *p;

    p = realloc(cs->implicitRedirectExceptions,
                sizeof(p[0]) * (cs->numImplicitRedirectExceptions + 1));
    if (p == NULL)
        return FALSE;

    p[cs->numImplicitRedirectExceptions].parentVisual = parentVisual;
    p[cs->numImplicitRedirectExceptions].winVisual = winVisual;

    cs->implicitRedirectExceptions = p;
    cs->numImplicitRedirectExceptions++;

    return TRUE;
}

254
typedef struct _alternateVisual {
255
256
    int depth;
    CARD32 format;
257
258
} CompAlternateVisual;

259
static CompAlternateVisual altVisuals[] = {
260
#if COMP_INCLUDE_RGB24_VISUAL
261
    {24, PICT_r8g8b8},
262
#endif
263
    {32, PICT_a8r8g8b8},
264
265
};

266
static const int NUM_COMP_ALTERNATE_VISUALS = sizeof(altVisuals) /
267
    sizeof(CompAlternateVisual);
268

269
static Bool
270
compAddAlternateVisual(ScreenPtr pScreen, CompScreenPtr cs,
271
                       CompAlternateVisual * alt)
272
{
273
274
275
276
    VisualPtr visual;
    DepthPtr depth;
    PictFormatPtr pPictFormat;
    unsigned long alphaMask;
277
278

    /*
279
280
281
     * The ARGB32 visual is always available.  Other alternate depth visuals
     * are only provided if their depth is less than the root window depth.
     * There's no deep reason for this.
282
     */
283
    if (alt->depth >= pScreen->rootDepth && alt->depth != 32)
284
        return FALSE;
285

286
    depth = compFindVisuallessDepth(pScreen, alt->depth);
287
    if (!depth)
288
289
        /* alt->depth doesn't exist or already has alternate visuals. */
        return TRUE;
290

291
    pPictFormat = PictureMatchFormat(pScreen, alt->depth, alt->format);
292
    if (!pPictFormat)
293
        return FALSE;
294

295
296
    if (ResizeVisualArray(pScreen, 1, depth) == FALSE) {
        return FALSE;
297
    }
298

299
    visual = pScreen->visuals + (pScreen->numVisuals - 1);      /* the new one */
300
301
302
303

    /* Initialize the visual */
    visual->bitsPerRGBValue = 8;
    if (PICT_FORMAT_TYPE(alt->format) == PICT_TYPE_COLOR) {
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
        visual->class = PseudoColor;
        visual->nplanes = PICT_FORMAT_BPP(alt->format);
        visual->ColormapEntries = 1 << visual->nplanes;
    }
    else {
        DirectFormatRec *direct = &pPictFormat->direct;

        visual->class = TrueColor;
        visual->redMask = ((unsigned long) direct->redMask) << direct->red;
        visual->greenMask =
            ((unsigned long) direct->greenMask) << direct->green;
        visual->blueMask = ((unsigned long) direct->blueMask) << direct->blue;
        alphaMask = ((unsigned long) direct->alphaMask) << direct->alpha;
        visual->offsetRed = direct->red;
        visual->offsetGreen = direct->green;
        visual->offsetBlue = direct->blue;
        /*
         * Include A bits in this (unlike GLX which includes only RGB)
         * This lets DIX compute suitable masks for colormap allocations
         */
        visual->nplanes = Ones(visual->redMask |
                               visual->greenMask |
                               visual->blueMask | alphaMask);
        /* find widest component */
        visual->ColormapEntries = (1 << max(Ones(visual->redMask),
                                            max(Ones(visual->greenMask),
                                                Ones(visual->blueMask))));
331
    }
332
333
334
335

    /* remember the visual ID to detect auto-update windows */
    compRegisterAlternateVisuals(cs, &visual->vid, 1);

336
337
338
    return TRUE;
}

339
static Bool
340
compAddAlternateVisuals(ScreenPtr pScreen, CompScreenPtr cs)
341
342
343
344
{
    int alt, ret = 0;

    for (alt = 0; alt < NUM_COMP_ALTERNATE_VISUALS; alt++)
345
        ret |= compAddAlternateVisual(pScreen, cs, altVisuals + alt);
346

347
    return ! !ret;
348
349
}

350
Bool
351
compScreenInit(ScreenPtr pScreen)
352
{
353
    CompScreenPtr cs;
354

355
    if (!dixRegisterPrivateKey(&CompScreenPrivateKeyRec, PRIVATE_SCREEN, 0))
356
        return FALSE;
357
    if (!dixRegisterPrivateKey(&CompWindowPrivateKeyRec, PRIVATE_WINDOW, 0))
358
        return FALSE;
359
    if (!dixRegisterPrivateKey(&CompSubwindowsPrivateKeyRec, PRIVATE_WINDOW, 0))
360
        return FALSE;
361

362
363
364
    if (GetCompScreen(pScreen))
        return TRUE;
    cs = (CompScreenPtr) malloc(sizeof(CompScreenRec));
365
    if (!cs)
366
        return FALSE;
367

368
    cs->overlayWid = FakeClientID(0);
369
370
    cs->pOverlayWin = NULL;
    cs->pOverlayClients = NULL;
371

372
373
    cs->numAlternateVisuals = 0;
    cs->alternateVisuals = NULL;
374
375
    cs->numImplicitRedirectExceptions = 0;
    cs->implicitRedirectExceptions = NULL;
376

377
378
379
    if (!compAddAlternateVisuals(pScreen, cs)) {
        free(cs);
        return FALSE;
380
381
    }

382
383
384
    if (!disableBackingStore)
        pScreen->backingStoreSupport = WhenMapped;

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
    cs->PositionWindow = pScreen->PositionWindow;
    pScreen->PositionWindow = compPositionWindow;

    cs->CopyWindow = pScreen->CopyWindow;
    pScreen->CopyWindow = compCopyWindow;

    cs->CreateWindow = pScreen->CreateWindow;
    pScreen->CreateWindow = compCreateWindow;

    cs->DestroyWindow = pScreen->DestroyWindow;
    pScreen->DestroyWindow = compDestroyWindow;

    cs->RealizeWindow = pScreen->RealizeWindow;
    pScreen->RealizeWindow = compRealizeWindow;

    cs->UnrealizeWindow = pScreen->UnrealizeWindow;
    pScreen->UnrealizeWindow = compUnrealizeWindow;

    cs->ClipNotify = pScreen->ClipNotify;
    pScreen->ClipNotify = compClipNotify;

406
407
    cs->ConfigNotify = pScreen->ConfigNotify;
    pScreen->ConfigNotify = compConfigNotify;
408

409
410
411
412
413
414
415
416
417
    cs->MoveWindow = pScreen->MoveWindow;
    pScreen->MoveWindow = compMoveWindow;

    cs->ResizeWindow = pScreen->ResizeWindow;
    pScreen->ResizeWindow = compResizeWindow;

    cs->ChangeBorderWidth = pScreen->ChangeBorderWidth;
    pScreen->ChangeBorderWidth = compChangeBorderWidth;

418
419
420
    cs->ReparentWindow = pScreen->ReparentWindow;
    pScreen->ReparentWindow = compReparentWindow;

421
422
423
    cs->InstallColormap = pScreen->InstallColormap;
    pScreen->InstallColormap = compInstallColormap;

424
425
426
    cs->ChangeWindowAttributes = pScreen->ChangeWindowAttributes;
    pScreen->ChangeWindowAttributes = compChangeWindowAttributes;

427
    cs->BlockHandler = NULL;
428
429
430
431

    cs->CloseScreen = pScreen->CloseScreen;
    pScreen->CloseScreen = compCloseScreen;

432
433
434
    cs->GetImage = pScreen->GetImage;
    pScreen->GetImage = compGetImage;

435
436
437
    cs->SourceValidate = pScreen->SourceValidate;
    pScreen->SourceValidate = compSourceValidate;

438
    dixSetPrivate(&pScreen->devPrivates, CompScreenPrivateKey, cs);
439

440
441
    RegisterRealChildHeadProc(CompositeRealChildHead);

442
443
    return TRUE;
}