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  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Copyright © 2003 Keith Packard
24  *
25  * Permission to use, copy, modify, distribute, and sell this software and its
26  * documentation for any purpose is hereby granted without fee, provided that
27  * the above copyright notice appear in all copies and that both that
28  * copyright notice and this permission notice appear in supporting
29  * documentation, and that the name of Keith Packard not be used in
30  * advertising or publicity pertaining to distribution of the software without
31  * specific, written prior permission.  Keith Packard makes no
32  * representations about the suitability of this software for any purpose.  It
33  * is provided "as is" without express or implied warranty.
34  *
35  * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
36  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
37  * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
38  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
39  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
40  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
41  * PERFORMANCE OF THIS SOFTWARE.
42  */
43 
44 #ifdef HAVE_DIX_CONFIG_H
45 #include <dix-config.h>
46 #endif
47 
48 #include "compint.h"
49 #include "compositeext.h"
50 
51 DevPrivateKeyRec CompScreenPrivateKeyRec;
52 DevPrivateKeyRec CompWindowPrivateKeyRec;
53 DevPrivateKeyRec CompSubwindowsPrivateKeyRec;
54 
55 static Bool
compCloseScreen(ScreenPtr pScreen)56 compCloseScreen(ScreenPtr pScreen)
57 {
58     CompScreenPtr cs = GetCompScreen(pScreen);
59     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     pScreen->MoveWindow = cs->MoveWindow;
69     pScreen->ResizeWindow = cs->ResizeWindow;
70     pScreen->ChangeBorderWidth = cs->ChangeBorderWidth;
71 
72     pScreen->ClipNotify = cs->ClipNotify;
73     pScreen->UnrealizeWindow = cs->UnrealizeWindow;
74     pScreen->RealizeWindow = cs->RealizeWindow;
75     pScreen->DestroyWindow = cs->DestroyWindow;
76     pScreen->CreateWindow = cs->CreateWindow;
77     pScreen->CopyWindow = cs->CopyWindow;
78     pScreen->PositionWindow = cs->PositionWindow;
79 
80     pScreen->GetImage = cs->GetImage;
81     pScreen->GetSpans = cs->GetSpans;
82     pScreen->SourceValidate = cs->SourceValidate;
83 
84     free(cs);
85     dixSetPrivate(&pScreen->devPrivates, CompScreenPrivateKey, NULL);
86     ret = (*pScreen->CloseScreen) (pScreen);
87 
88     return ret;
89 }
90 
91 static void
compInstallColormap(ColormapPtr pColormap)92 compInstallColormap(ColormapPtr pColormap)
93 {
94     VisualPtr pVisual = pColormap->pVisual;
95     ScreenPtr pScreen = pColormap->pScreen;
96     CompScreenPtr cs = GetCompScreen(pScreen);
97     int a;
98 
99     for (a = 0; a < cs->numAlternateVisuals; a++)
100         if (pVisual->vid == cs->alternateVisuals[a])
101             return;
102     pScreen->InstallColormap = cs->InstallColormap;
103     (*pScreen->InstallColormap) (pColormap);
104     cs->InstallColormap = pScreen->InstallColormap;
105     pScreen->InstallColormap = compInstallColormap;
106 }
107 
108 static void
compCheckBackingStore(WindowPtr pWin)109 compCheckBackingStore(WindowPtr pWin)
110 {
111     if (pWin->backingStore != NotUseful && !pWin->backStorage) {
112         compRedirectWindow(serverClient, pWin, CompositeRedirectAutomatic);
113         pWin->backStorage = TRUE;
114     }
115     else if (pWin->backingStore == NotUseful && pWin->backStorage) {
116         compUnredirectWindow(serverClient, pWin,
117                              CompositeRedirectAutomatic);
118         pWin->backStorage = FALSE;
119     }
120 }
121 
122 /* Fake backing store via automatic redirection */
123 static Bool
compChangeWindowAttributes(WindowPtr pWin,unsigned long mask)124 compChangeWindowAttributes(WindowPtr pWin, unsigned long mask)
125 {
126     ScreenPtr pScreen = pWin->drawable.pScreen;
127     CompScreenPtr cs = GetCompScreen(pScreen);
128     Bool ret;
129 
130     pScreen->ChangeWindowAttributes = cs->ChangeWindowAttributes;
131     ret = pScreen->ChangeWindowAttributes(pWin, mask);
132 
133     if (ret && (mask & CWBackingStore) &&
134         pScreen->backingStoreSupport != NotUseful)
135         compCheckBackingStore(pWin);
136 
137     pScreen->ChangeWindowAttributes = compChangeWindowAttributes;
138 
139     return ret;
140 }
141 
142 static void
compGetImage(DrawablePtr pDrawable,int sx,int sy,int w,int h,unsigned int format,unsigned long planemask,char * pdstLine)143 compGetImage(DrawablePtr pDrawable,
144              int sx, int sy,
145              int w, int h,
146              unsigned int format, unsigned long planemask, char *pdstLine)
147 {
148     ScreenPtr pScreen = pDrawable->pScreen;
149     CompScreenPtr cs = GetCompScreen(pScreen);
150 
151     pScreen->GetImage = cs->GetImage;
152     if (pDrawable->type == DRAWABLE_WINDOW)
153         compPaintChildrenToWindow((WindowPtr) pDrawable);
154     (*pScreen->GetImage) (pDrawable, sx, sy, w, h, format, planemask, pdstLine);
155     cs->GetImage = pScreen->GetImage;
156     pScreen->GetImage = compGetImage;
157 }
158 
159 static void
compGetSpans(DrawablePtr pDrawable,int wMax,DDXPointPtr ppt,int * pwidth,int nspans,char * pdstStart)160 compGetSpans(DrawablePtr pDrawable, int wMax, DDXPointPtr ppt, int *pwidth,
161              int nspans, char *pdstStart)
162 {
163     ScreenPtr pScreen = pDrawable->pScreen;
164     CompScreenPtr cs = GetCompScreen(pScreen);
165 
166     pScreen->GetSpans = cs->GetSpans;
167     if (pDrawable->type == DRAWABLE_WINDOW)
168         compPaintChildrenToWindow((WindowPtr) pDrawable);
169     (*pScreen->GetSpans) (pDrawable, wMax, ppt, pwidth, nspans, pdstStart);
170     cs->GetSpans = pScreen->GetSpans;
171     pScreen->GetSpans = compGetSpans;
172 }
173 
174 static void
compSourceValidate(DrawablePtr pDrawable,int x,int y,int width,int height,unsigned int subWindowMode)175 compSourceValidate(DrawablePtr pDrawable,
176                    int x, int y,
177                    int width, int height, unsigned int subWindowMode)
178 {
179     ScreenPtr pScreen = pDrawable->pScreen;
180     CompScreenPtr cs = GetCompScreen(pScreen);
181 
182     pScreen->SourceValidate = cs->SourceValidate;
183     if (pDrawable->type == DRAWABLE_WINDOW && subWindowMode == IncludeInferiors)
184         compPaintChildrenToWindow((WindowPtr) pDrawable);
185     if (pScreen->SourceValidate)
186         (*pScreen->SourceValidate) (pDrawable, x, y, width, height,
187                                     subWindowMode);
188     cs->SourceValidate = pScreen->SourceValidate;
189     pScreen->SourceValidate = compSourceValidate;
190 }
191 
192 /*
193  * Add alternate visuals -- always expose an ARGB32 and RGB24 visual
194  */
195 
196 static DepthPtr
compFindVisuallessDepth(ScreenPtr pScreen,int d)197 compFindVisuallessDepth(ScreenPtr pScreen, int d)
198 {
199     int i;
200 
201     for (i = 0; i < pScreen->numDepths; i++) {
202         DepthPtr depth = &pScreen->allowedDepths[i];
203 
204         if (depth->depth == d) {
205             /*
206              * Make sure it doesn't have visuals already
207              */
208             if (depth->numVids)
209                 return 0;
210             /*
211              * looks fine
212              */
213             return depth;
214         }
215     }
216     /*
217      * If there isn't one, then it's gonna be hard to have
218      * an associated visual
219      */
220     return 0;
221 }
222 
223 /*
224  * Add a list of visual IDs to the list of visuals to implicitly redirect.
225  */
226 static Bool
compRegisterAlternateVisuals(CompScreenPtr cs,VisualID * vids,int nVisuals)227 compRegisterAlternateVisuals(CompScreenPtr cs, VisualID * vids, int nVisuals)
228 {
229     VisualID *p;
230 
231     p = reallocarray(cs->alternateVisuals,
232                      cs->numAlternateVisuals + nVisuals, sizeof(VisualID));
233     if (p == NULL)
234         return FALSE;
235 
236     memcpy(&p[cs->numAlternateVisuals], vids, sizeof(VisualID) * nVisuals);
237 
238     cs->alternateVisuals = p;
239     cs->numAlternateVisuals += nVisuals;
240 
241     return TRUE;
242 }
243 
244 Bool
CompositeRegisterAlternateVisuals(ScreenPtr pScreen,VisualID * vids,int nVisuals)245 CompositeRegisterAlternateVisuals(ScreenPtr pScreen, VisualID * vids,
246                                   int nVisuals)
247 {
248     CompScreenPtr cs = GetCompScreen(pScreen);
249 
250     return compRegisterAlternateVisuals(cs, vids, nVisuals);
251 }
252 
253 Bool
CompositeRegisterImplicitRedirectionException(ScreenPtr pScreen,VisualID parentVisual,VisualID winVisual)254 CompositeRegisterImplicitRedirectionException(ScreenPtr pScreen,
255                                               VisualID parentVisual,
256                                               VisualID winVisual)
257 {
258     CompScreenPtr cs = GetCompScreen(pScreen);
259     CompImplicitRedirectException *p;
260 
261     p = reallocarray(cs->implicitRedirectExceptions,
262                      cs->numImplicitRedirectExceptions + 1, sizeof(p[0]));
263     if (p == NULL)
264         return FALSE;
265 
266     p[cs->numImplicitRedirectExceptions].parentVisual = parentVisual;
267     p[cs->numImplicitRedirectExceptions].winVisual = winVisual;
268 
269     cs->implicitRedirectExceptions = p;
270     cs->numImplicitRedirectExceptions++;
271 
272     return TRUE;
273 }
274 
275 typedef struct _alternateVisual {
276     int depth;
277     CARD32 format;
278 } CompAlternateVisual;
279 
280 static CompAlternateVisual altVisuals[] = {
281 #if COMP_INCLUDE_RGB24_VISUAL
282     {24, PICT_r8g8b8},
283 #endif
284     {32, PICT_a8r8g8b8},
285 };
286 
287 static Bool
compAddAlternateVisual(ScreenPtr pScreen,CompScreenPtr cs,CompAlternateVisual * alt)288 compAddAlternateVisual(ScreenPtr pScreen, CompScreenPtr cs,
289                        CompAlternateVisual * alt)
290 {
291     VisualPtr visual;
292     DepthPtr depth;
293     PictFormatPtr pPictFormat;
294     unsigned long alphaMask;
295 
296     /*
297      * The ARGB32 visual is always available.  Other alternate depth visuals
298      * are only provided if their depth is less than the root window depth.
299      * There's no deep reason for this.
300      */
301     if (alt->depth >= pScreen->rootDepth && alt->depth != 32)
302         return FALSE;
303 
304     depth = compFindVisuallessDepth(pScreen, alt->depth);
305     if (!depth)
306         /* alt->depth doesn't exist or already has alternate visuals. */
307         return TRUE;
308 
309     pPictFormat = PictureMatchFormat(pScreen, alt->depth, alt->format);
310     if (!pPictFormat)
311         return FALSE;
312 
313     if (ResizeVisualArray(pScreen, 1, depth) == FALSE) {
314         return FALSE;
315     }
316 
317     visual = pScreen->visuals + (pScreen->numVisuals - 1);      /* the new one */
318 
319     /* Initialize the visual */
320     visual->bitsPerRGBValue = 8;
321     if (PICT_FORMAT_TYPE(alt->format) == PICT_TYPE_COLOR) {
322         visual->class = PseudoColor;
323         visual->nplanes = PICT_FORMAT_BPP(alt->format);
324         visual->ColormapEntries = 1 << visual->nplanes;
325     }
326     else {
327         DirectFormatRec *direct = &pPictFormat->direct;
328 
329         visual->class = TrueColor;
330         visual->redMask = ((unsigned long) direct->redMask) << direct->red;
331         visual->greenMask =
332             ((unsigned long) direct->greenMask) << direct->green;
333         visual->blueMask = ((unsigned long) direct->blueMask) << direct->blue;
334         alphaMask = ((unsigned long) direct->alphaMask) << direct->alpha;
335         visual->offsetRed = direct->red;
336         visual->offsetGreen = direct->green;
337         visual->offsetBlue = direct->blue;
338         /*
339          * Include A bits in this (unlike GLX which includes only RGB)
340          * This lets DIX compute suitable masks for colormap allocations
341          */
342         visual->nplanes = Ones(visual->redMask |
343                                visual->greenMask |
344                                visual->blueMask | alphaMask);
345         /* find widest component */
346         visual->ColormapEntries = (1 << max(Ones(visual->redMask),
347                                             max(Ones(visual->greenMask),
348                                                 Ones(visual->blueMask))));
349     }
350 
351     /* remember the visual ID to detect auto-update windows */
352     compRegisterAlternateVisuals(cs, &visual->vid, 1);
353 
354     return TRUE;
355 }
356 
357 static Bool
compAddAlternateVisuals(ScreenPtr pScreen,CompScreenPtr cs)358 compAddAlternateVisuals(ScreenPtr pScreen, CompScreenPtr cs)
359 {
360     int alt, ret = 0;
361 
362     for (alt = 0; alt < ARRAY_SIZE(altVisuals); alt++)
363         ret |= compAddAlternateVisual(pScreen, cs, altVisuals + alt);
364 
365     return ! !ret;
366 }
367 
368 Bool
compScreenInit(ScreenPtr pScreen)369 compScreenInit(ScreenPtr pScreen)
370 {
371     CompScreenPtr cs;
372 
373     if (!dixRegisterPrivateKey(&CompScreenPrivateKeyRec, PRIVATE_SCREEN, 0))
374         return FALSE;
375     if (!dixRegisterPrivateKey(&CompWindowPrivateKeyRec, PRIVATE_WINDOW, 0))
376         return FALSE;
377     if (!dixRegisterPrivateKey(&CompSubwindowsPrivateKeyRec, PRIVATE_WINDOW, 0))
378         return FALSE;
379 
380     if (GetCompScreen(pScreen))
381         return TRUE;
382     cs = (CompScreenPtr) malloc(sizeof(CompScreenRec));
383     if (!cs)
384         return FALSE;
385 
386     cs->overlayWid = FakeClientID(0);
387     cs->pOverlayWin = NULL;
388     cs->pOverlayClients = NULL;
389 
390     cs->pendingScreenUpdate = FALSE;
391 
392     cs->numAlternateVisuals = 0;
393     cs->alternateVisuals = NULL;
394     cs->numImplicitRedirectExceptions = 0;
395     cs->implicitRedirectExceptions = NULL;
396 
397     if (!compAddAlternateVisuals(pScreen, cs)) {
398         free(cs);
399         return FALSE;
400     }
401 
402     if (!disableBackingStore)
403         pScreen->backingStoreSupport = WhenMapped;
404 
405     cs->PositionWindow = pScreen->PositionWindow;
406     pScreen->PositionWindow = compPositionWindow;
407 
408     cs->CopyWindow = pScreen->CopyWindow;
409     pScreen->CopyWindow = compCopyWindow;
410 
411     cs->CreateWindow = pScreen->CreateWindow;
412     pScreen->CreateWindow = compCreateWindow;
413 
414     cs->DestroyWindow = pScreen->DestroyWindow;
415     pScreen->DestroyWindow = compDestroyWindow;
416 
417     cs->RealizeWindow = pScreen->RealizeWindow;
418     pScreen->RealizeWindow = compRealizeWindow;
419 
420     cs->UnrealizeWindow = pScreen->UnrealizeWindow;
421     pScreen->UnrealizeWindow = compUnrealizeWindow;
422 
423     cs->ClipNotify = pScreen->ClipNotify;
424     pScreen->ClipNotify = compClipNotify;
425 
426     cs->ConfigNotify = pScreen->ConfigNotify;
427     pScreen->ConfigNotify = compConfigNotify;
428 
429     cs->MoveWindow = pScreen->MoveWindow;
430     pScreen->MoveWindow = compMoveWindow;
431 
432     cs->ResizeWindow = pScreen->ResizeWindow;
433     pScreen->ResizeWindow = compResizeWindow;
434 
435     cs->ChangeBorderWidth = pScreen->ChangeBorderWidth;
436     pScreen->ChangeBorderWidth = compChangeBorderWidth;
437 
438     cs->ReparentWindow = pScreen->ReparentWindow;
439     pScreen->ReparentWindow = compReparentWindow;
440 
441     cs->InstallColormap = pScreen->InstallColormap;
442     pScreen->InstallColormap = compInstallColormap;
443 
444     cs->ChangeWindowAttributes = pScreen->ChangeWindowAttributes;
445     pScreen->ChangeWindowAttributes = compChangeWindowAttributes;
446 
447     cs->CloseScreen = pScreen->CloseScreen;
448     pScreen->CloseScreen = compCloseScreen;
449 
450     cs->GetImage = pScreen->GetImage;
451     pScreen->GetImage = compGetImage;
452 
453     cs->GetSpans = pScreen->GetSpans;
454     pScreen->GetSpans = compGetSpans;
455 
456     cs->SourceValidate = pScreen->SourceValidate;
457     pScreen->SourceValidate = compSourceValidate;
458 
459     dixSetPrivate(&pScreen->devPrivates, CompScreenPrivateKey, cs);
460 
461     RegisterRealChildHeadProc(CompositeRealChildHead);
462 
463     return TRUE;
464 }
465