summaryrefslogtreecommitdiff
path: root/miext/rootless/rootlessCommon.c
blob: 85655a9e116f3dc216cb0d0763a6bc95b0171678 (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
/*
 * Common rootless definitions and code
 */
/*
 * Copyright (c) 2001 Greg Parker. All Rights Reserved.
 * Copyright (c) 2002-2003 Torrey T. Lyons. All Rights Reserved.
 * Copyright (c) 2002 Apple Computer, 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 and this permission notice 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
 * THE ABOVE LISTED COPYRIGHT HOLDER(S) 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(s) of the above copyright
 * holders shall not be used in advertising or otherwise to promote the sale,
 * use or other dealings in this Software without prior written authorization.
 */
/* $XFree86: xc/programs/Xserver/miext/rootless/rootlessCommon.c,v 1.4tsi Exp $ */

#include "rootlessCommon.h"

unsigned int rootless_CopyBytes_threshold = 0;
unsigned int rootless_FillBytes_threshold = 0;
unsigned int rootless_CompositePixels_threshold = 0;
unsigned int rootless_CopyWindow_threshold = 0;
#ifdef ROOTLESS_GLOBAL_COORDS
int rootlessGlobalOffsetX = 0;
int rootlessGlobalOffsetY = 0;
#endif

RegionRec rootlessHugeRoot = {{-32767, -32767, 32767, 32767}, NULL};

/* Following macro from miregion.c */

/*  true iff two Boxes overlap */
#define EXTENTCHECK(r1,r2) \
      (!( ((r1)->x2 <= (r2)->x1)  || \
          ((r1)->x1 >= (r2)->x2)  || \
          ((r1)->y2 <= (r2)->y1)  || \
          ((r1)->y1 >= (r2)->y2) ) )


/*
 * TopLevelParent
 *  Returns the top-level parent of pWindow.
 *  The root is the top-level parent of itself, even though the root is
 *  not otherwise considered to be a top-level window.
 */
WindowPtr
TopLevelParent(WindowPtr pWindow)
{
    WindowPtr top;

    if (IsRoot(pWindow))
        return pWindow;

    top = pWindow;
    while (top && ! IsTopLevel(top))
        top = top->parent;

    return top;
}


/*
 * IsFramedWindow
 *  Returns TRUE if this window is visible inside a frame
 *  (e.g. it is visible and has a top-level or root parent)
 */
Bool
IsFramedWindow(WindowPtr pWin)
{
    WindowPtr top;

    if (!pWin->realized)
        return FALSE;
    top = TopLevelParent(pWin);

    return (top && WINREC(top));
}


/*
 * RootlessStartDrawing
 *  Prepare a window for direct access to its backing buffer.
 *  Each top-level parent has a Pixmap representing its backing buffer,
 *  which all of its children inherit.
 */
void RootlessStartDrawing(WindowPtr pWindow)
{
    ScreenPtr pScreen = pWindow->drawable.pScreen;
    WindowPtr top = TopLevelParent(pWindow);
    RootlessWindowRec *winRec;

    if (top == NULL)
        return;
    winRec = WINREC(top);
    if (winRec == NULL)
        return;

    // Make sure the window's top-level parent is prepared for drawing.
    if (!winRec->is_drawing) {
        int bw = wBorderWidth(top);

        SCREENREC(pScreen)->imp->StartDrawing(winRec->wid, &winRec->pixelData,
                                              &winRec->bytesPerRow);

        winRec->pixmap =
            GetScratchPixmapHeader(pScreen, winRec->width, winRec->height,
                                   top->drawable.depth,
                                   top->drawable.bitsPerPixel,
                                   winRec->bytesPerRow,
                                   winRec->pixelData);
        SetPixmapBaseToScreen(winRec->pixmap,
                              top->drawable.x - bw, top->drawable.y - bw);

        winRec->is_drawing = TRUE;
    }

    winRec->oldPixmap = pScreen->GetWindowPixmap(pWindow);
    pScreen->SetWindowPixmap(pWindow, winRec->pixmap);
}


/*
 * RootlessStopDrawing
 *  Stop drawing to a window's backing buffer. If flush is true,
 *  damaged regions are flushed to the screen.
 */
void RootlessStopDrawing(WindowPtr pWindow, Bool flush)
{
    ScreenPtr pScreen = pWindow->drawable.pScreen;
    WindowPtr top = TopLevelParent(pWindow);
    RootlessWindowRec *winRec;

    if (top == NULL)
        return;
    winRec = WINREC(top);
    if (winRec == NULL)
        return;

    if (winRec->is_drawing) {
        SCREENREC(pScreen)->imp->StopDrawing(winRec->wid, flush);

        FreeScratchPixmapHeader(winRec->pixmap);
        pScreen->SetWindowPixmap(pWindow, winRec->oldPixmap);
        winRec->pixmap = NULL;

        winRec->is_drawing = FALSE;
    }
    else if (flush) {
        SCREENREC(pScreen)->imp->UpdateRegion(winRec->wid, NULL);
    }

    if (flush && winRec->is_reorder_pending) {
	winRec->is_reorder_pending = FALSE;
	RootlessReorderWindow(pWindow);
    }
}


/*
 * RootlessDamageRegion
 *  Mark a damaged region as requiring redisplay to screen.
 *  pRegion is in GLOBAL coordinates.
 */
void
RootlessDamageRegion(WindowPtr pWindow, RegionPtr pRegion)
{
    ScreenPtr pScreen = pWindow->drawable.pScreen;
    RootlessWindowRec *winRec;
    RegionRec clipped;
    WindowPtr pTop;
    BoxPtr b1, b2;

    RL_DEBUG_MSG("Damaged win 0x%x ", pWindow);

    pTop = TopLevelParent(pWindow);
    if (pTop == NULL)
	return;

    winRec = WINREC(pTop);
    if (winRec == NULL)
	return;

    /* We need to intersect the drawn region with the clip of the window
       to avoid marking places we didn't actually draw (which can cause
       problems when the window has an extra client-side backing store)

       But this is a costly operation and since we'll normally just be
       drawing inside the clip, go to some lengths to avoid the general
       case intersection. */

    b1 = REGION_EXTENTS(pScreen, &pWindow->borderClip);
    b2 = REGION_EXTENTS(pScreen, pRegion);

    if (EXTENTCHECK(b1, b2)) {
	/* Regions may overlap. */

	if (REGION_NUM_RECTS(pRegion) == 1) {
	    int in;

	    /* Damaged region only has a single rect, so we can
	       just compare that against the region */

	    in = RECT_IN_REGION(pScreen, &pWindow->borderClip,
                                REGION_RECTS (pRegion));
	    if (in == rgnIN) {
		/* clip totally contains pRegion */

#ifdef ROOTLESS_TRACK_DAMAGE
                REGION_UNION(pScreen, &winRec->damage,
                             &winRec->damage, (pRegion));
#else
		SCREENREC(pScreen)->imp->DamageRects(winRec->wid,
                                REGION_NUM_RECTS(pRegion),
				REGION_RECTS(pRegion),
				-winRec->x, -winRec->y);
#endif

		RootlessQueueRedisplay(pTop->drawable.pScreen);
		goto out;
	    }
	    else if (in == rgnOUT) {
		/* clip doesn't contain pRegion */

		goto out;
	    }
	}

	/* clip overlaps pRegion, need to intersect */

	REGION_NULL(pScreen, &clipped);
	REGION_INTERSECT(pScreen, &clipped, &pWindow->borderClip, pRegion);

#ifdef ROOTLESS_TRACK_DAMAGE
        REGION_UNION(pScreen, &winRec->damage,
                     &winRec->damage, (pRegion));
#else
        SCREENREC(pScreen)->imp->DamageRects(winRec->wid,
                        REGION_NUM_RECTS(&clipped),
                        REGION_RECTS(&clipped),
                        -winRec->x, -winRec->y);
#endif

	REGION_UNINIT(pScreen, &clipped);

	RootlessQueueRedisplay(pTop->drawable.pScreen);
    }

out:
#ifdef ROOTLESSDEBUG
    {
        BoxRec *box = REGION_RECTS(pRegion), *end;
        int numBox = REGION_NUM_RECTS(pRegion);

        for (end = box+numBox; box < end; box++) {
            RL_DEBUG_MSG("Damage rect: %i, %i, %i, %i\n",
                         box->x1, box->x2, box->y1, box->y2);
        }
    }
#endif
    return;
}


/*
 * RootlessDamageBox
 *  Mark a damaged box as requiring redisplay to screen.
 *  pRegion is in GLOBAL coordinates.
 */
void
RootlessDamageBox(WindowPtr pWindow, BoxPtr pBox)
{
    RegionRec region;

    REGION_INIT(pWindow->drawable.pScreen, &region, pBox, 1);

    RootlessDamageRegion(pWindow, &region);

    REGION_UNINIT(pWindow->drawable.pScreen, &region);	/* no-op */
}


/*
 * RootlessDamageRect
 *  Mark a damaged rectangle as requiring redisplay to screen.
 *  (x, y, w, h) is in window-local coordinates.
 */
void
RootlessDamageRect(WindowPtr pWindow, int x, int y, int w, int h)
{
    BoxRec box;
    RegionRec region;

    x += pWindow->drawable.x;
    y += pWindow->drawable.y;

    box.x1 = x;
    box.x2 = x + w;
    box.y1 = y;
    box.y2 = y + h;

    REGION_INIT(pWindow->drawable.pScreen, &region, &box, 1);

    RootlessDamageRegion(pWindow, &region);

    REGION_UNINIT(pWindow->drawable.pScreen, &region);	/* no-op */
}


/*
 * RootlessRedisplay
 *  Stop drawing and redisplay the damaged region of a window.
 */
void
RootlessRedisplay(WindowPtr pWindow)
{
#ifdef ROOTLESS_TRACK_DAMAGE

    RootlessWindowRec *winRec = WINREC(pWindow);
    ScreenPtr pScreen = pWindow->drawable.pScreen;

    RootlessStopDrawing(pWindow, FALSE);

    if (REGION_NOTEMPTY(pScreen, &winRec->damage)) {
        RL_DEBUG_MSG("Redisplay Win 0x%x, %i x %i @ (%i, %i)\n",
                     pWindow, winRec->width, winRec->height,
                     winRec->x, winRec->y);

        // move region to window local coords
        REGION_TRANSLATE(pScreen, &winRec->damage,
                         -winRec->x, -winRec->y);

        SCREENREC(pScreen)->imp->UpdateRegion(winRec->wid, &winRec->damage);

        REGION_EMPTY(pScreen, &winRec->damage);
    }

#else	/* !ROOTLESS_TRACK_DAMAGE */

    RootlessStopDrawing(pWindow, TRUE);

#endif
}


/*
 * RootlessRedisplayScreen
 *  Walk every window on a screen and redisplay the damaged regions.
 */
void
RootlessRedisplayScreen(ScreenPtr pScreen)
{
    WindowPtr root = WindowTable[pScreen->myNum];

    if (root != NULL) {
        WindowPtr win;

        RootlessRedisplay(root);
        for (win = root->firstChild; win; win = win->nextSib) {
            if (WINREC(win) != NULL) {
                RootlessRedisplay(win);
            }
        }
    }
}