miexpose.c 19.9 KB
Newer Older
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
/***********************************************************

Copyright 1987, 1998  The Open Group

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.

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
OPEN GROUP 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 The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.


Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
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 Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL 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.

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
******************************************************************/
/*****************************************************************

Copyright (c) 1991, 1997 Digital Equipment Corporation, Maynard, Massachusetts.

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.

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
DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL 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 Digital Equipment Corporation
shall not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from Digital
Equipment Corporation.

73 74 75
******************************************************************/


76 77 78 79
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

80 81 82
#include <X11/X.h>
#include <X11/Xproto.h>
#include <X11/Xprotostr.h>
83

84
#include "misc.h"
85 86 87 88 89 90 91 92 93
#include "regionstr.h"
#include "scrnintstr.h"
#include "gcstruct.h"
#include "windowstr.h"
#include "pixmap.h"
#include "input.h"

#include "dixstruct.h"
#include "mi.h"
94
#include <X11/Xmd.h>
95

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
96 97 98 99 100 101
#include "globals.h"

#ifdef PANORAMIX
#include "panoramiX.h"
#include "panoramiXsrv.h"
#endif
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

/*
    machine-independent graphics exposure code.  any device that uses
the region package can call this.
*/

#ifndef RECTLIMIT
#define RECTLIMIT 25		/* pick a number, any number > 8 */
#endif

/* miHandleExposures 
    generate a region for exposures for areas that were copied from obscured or
non-existent areas to non-obscured areas of the destination.  Paint the
background for the region, if the destination is a window.

NOTE:
     this should generally be called, even if graphicsExposures is false,
because this is where bits get recovered from backing store.

NOTE:
     added argument 'plane' is used to indicate how exposures from backing
store should be accomplished. If plane is 0 (i.e. no bit plane), CopyArea
should be used, else a CopyPlane of the indicated plane will be used. The
exposing is done by the backing store's GraphicsExpose function, of course.

*/

129
RegionPtr
Peter Hutterer's avatar
Peter Hutterer committed
130 131 132
miHandleExposures(DrawablePtr pSrcDrawable, DrawablePtr pDstDrawable,
		  GCPtr pGC, int srcx, int srcy, int width, int height,
		  int dstx, int dsty, unsigned long plane)
133
{
134
    ScreenPtr pscr;
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    RegionPtr prgnSrcClip;	/* drawable-relative source clip */
    RegionRec rgnSrcRec;
    RegionPtr prgnDstClip;	/* drawable-relative dest clip */
    RegionRec rgnDstRec;
    BoxRec srcBox;		/* unclipped source */
    RegionRec rgnExposed;	/* exposed region, calculated source-
				   relative, made dst relative to
				   intersect with visible parts of
				   dest and send events to client, 
				   and then screen relative to paint 
				   the window background
				*/
    WindowPtr pSrcWin;
    BoxRec expBox;
    Bool extents;

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
151 152 153
    /* This prevents warning about pscr not being used. */
    pGC->pScreen = pscr = pGC->pScreen;

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
    /* avoid work if we can */
    if (!pGC->graphicsExposures &&
	(pDstDrawable->type == DRAWABLE_PIXMAP) &&
	((pSrcDrawable->type == DRAWABLE_PIXMAP) ||
	 (((WindowPtr)pSrcDrawable)->backStorage == NULL)))
	return NULL;
	
    srcBox.x1 = srcx;
    srcBox.y1 = srcy;
    srcBox.x2 = srcx+width;
    srcBox.y2 = srcy+height;

    if (pSrcDrawable->type != DRAWABLE_PIXMAP)
    {
	BoxRec TsrcBox;

	TsrcBox.x1 = srcx + pSrcDrawable->x;
	TsrcBox.y1 = srcy + pSrcDrawable->y;
	TsrcBox.x2 = TsrcBox.x1 + width;
	TsrcBox.y2 = TsrcBox.y1 + height;
	pSrcWin = (WindowPtr) pSrcDrawable;
	if (pGC->subWindowMode == IncludeInferiors)
 	{
	    prgnSrcClip = NotClippedByChildren (pSrcWin);
	    if ((RECT_IN_REGION(pscr, prgnSrcClip, &TsrcBox)) == rgnIN)
	    {
		REGION_DESTROY(pscr, prgnSrcClip);
		return NULL;
	    }
	}
 	else
 	{
	    if ((RECT_IN_REGION(pscr, &pSrcWin->clipList, &TsrcBox)) == rgnIN)
		return NULL;
	    prgnSrcClip = &rgnSrcRec;
189
	    REGION_NULL(pscr, prgnSrcClip);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	    REGION_COPY(pscr, prgnSrcClip, &pSrcWin->clipList);
	}
	REGION_TRANSLATE(pscr, prgnSrcClip,
				-pSrcDrawable->x, -pSrcDrawable->y);
    }
    else
    {
	BoxRec	box;

	if ((srcBox.x1 >= 0) && (srcBox.y1 >= 0) &&
	    (srcBox.x2 <= pSrcDrawable->width) &&
 	    (srcBox.y2 <= pSrcDrawable->height))
	    return NULL;

	box.x1 = 0;
	box.y1 = 0;
	box.x2 = pSrcDrawable->width;
	box.y2 = pSrcDrawable->height;
	prgnSrcClip = &rgnSrcRec;
	REGION_INIT(pscr, prgnSrcClip, &box, 1);
210
	pSrcWin = NULL;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    }

    if (pDstDrawable == pSrcDrawable)
    {
	prgnDstClip = prgnSrcClip;
    }
    else if (pDstDrawable->type != DRAWABLE_PIXMAP)
    {
	if (pGC->subWindowMode == IncludeInferiors)
	{
	    prgnDstClip = NotClippedByChildren((WindowPtr)pDstDrawable);
	}
	else
	{
	    prgnDstClip = &rgnDstRec;
226
	    REGION_NULL(pscr, prgnDstClip);
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
	    REGION_COPY(pscr, prgnDstClip,
				&((WindowPtr)pDstDrawable)->clipList);
	}
	REGION_TRANSLATE(pscr, prgnDstClip,
				 -pDstDrawable->x, -pDstDrawable->y);
    }
    else
    {
	BoxRec	box;

	box.x1 = 0;
	box.y1 = 0;
	box.x2 = pDstDrawable->width;
	box.y2 = pDstDrawable->height;
	prgnDstClip = &rgnDstRec;
	REGION_INIT(pscr, prgnDstClip, &box, 1);
    }

    /* drawable-relative source region */
    REGION_INIT(pscr, &rgnExposed, &srcBox, 1);

    /* now get the hidden parts of the source box*/
    REGION_SUBTRACT(pscr, &rgnExposed, &rgnExposed, prgnSrcClip);

    /* move them over the destination */
    REGION_TRANSLATE(pscr, &rgnExposed, dstx-srcx, dsty-srcy);

    /* intersect with visible areas of dest */
    REGION_INTERSECT(pscr, &rgnExposed, &rgnExposed, prgnDstClip);

257 258 259 260
    /* intersect with client clip region. */
    if (pGC->clientClipType == CT_REGION)
	REGION_INTERSECT(pscr, &rgnExposed, &rgnExposed, pGC->clientClip);

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
    /*
     * If we have LOTS of rectangles, we decide to take the extents
     * and force an exposure on that.  This should require much less
     * work overall, on both client and server.  This is cheating, but
     * isn't prohibited by the protocol ("spontaneous combustion" :-)
     * for windows.
     */
    extents = pGC->graphicsExposures &&
	      (REGION_NUM_RECTS(&rgnExposed) > RECTLIMIT) &&
	      (pDstDrawable->type != DRAWABLE_PIXMAP);
    if (pSrcWin)
    {
	RegionPtr	region;
    	if (!(region = wClipShape (pSrcWin)))
    	    region = wBoundingShape (pSrcWin);
    	/*
     	 * If you try to CopyArea the extents of a shaped window, compacting the
     	 * exposed region will undo all our work!
     	 */
    	if (extents && pSrcWin && region &&
    	    (RECT_IN_REGION(pscr, region, &srcBox) != rgnIN))
	    	extents = FALSE;
    }
    if (extents)
    {
	expBox = *REGION_EXTENTS(pscr, &rgnExposed);
	REGION_RESET(pscr, &rgnExposed, &expBox);
    }
    if ((pDstDrawable->type != DRAWABLE_PIXMAP) &&
	(((WindowPtr)pDstDrawable)->backgroundState != None))
    {
	WindowPtr pWin = (WindowPtr)pDstDrawable;

	/* make the exposed area screen-relative */
	REGION_TRANSLATE(pscr, &rgnExposed, 
				 pDstDrawable->x, pDstDrawable->y);

	if (extents)
	{
300
	    /* miPaintWindow doesn't clip, so we have to */
301 302
	    REGION_INTERSECT(pscr, &rgnExposed, &rgnExposed, &pWin->clipList);
	}
303
	miPaintWindow((WindowPtr)pDstDrawable, &rgnExposed, PW_BACKGROUND);
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

	if (extents)
	{
	    REGION_RESET(pscr, &rgnExposed, &expBox);
	}
	else
	    REGION_TRANSLATE(pscr, &rgnExposed,
				     -pDstDrawable->x, -pDstDrawable->y);
    }
    if (prgnDstClip == &rgnDstRec)
    {
	REGION_UNINIT(pscr, prgnDstClip);
    }
    else if (prgnDstClip != prgnSrcClip)
    {
	REGION_DESTROY(pscr, prgnDstClip);
    }

    if (prgnSrcClip == &rgnSrcRec)
    {
	REGION_UNINIT(pscr, prgnSrcClip);
    }
    else
    {
	REGION_DESTROY(pscr, prgnSrcClip);
    }

    if (pGC->graphicsExposures)
    {
	/* don't look */
	RegionPtr exposed = REGION_CREATE(pscr, NullBox, 0);
	*exposed = rgnExposed;
	return exposed;
    }
    else
    {
	REGION_UNINIT(pscr, &rgnExposed);
	return NULL;
    }
}

/* send GraphicsExpose events, or a NoExpose event, based on the region */

347
void
Peter Hutterer's avatar
Peter Hutterer committed
348 349
miSendGraphicsExpose (ClientPtr client, RegionPtr pRgn, XID drawable,
                      int major, int minor)
350 351 352 353
{
    if (pRgn && !REGION_NIL(pRgn))
    {
        xEvent *pEvent;
354 355 356
	xEvent *pe;
	BoxPtr pBox;
	int i;
357 358 359 360
	int numRects;

	numRects = REGION_NUM_RECTS(pRgn);
	pBox = REGION_RECTS(pRgn);
361
	if(!(pEvent = malloc(numRects * sizeof(xEvent))))
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
		return;
	pe = pEvent;

	for (i=1; i<=numRects; i++, pe++, pBox++)
	{
	    pe->u.u.type = GraphicsExpose;
	    pe->u.graphicsExposure.drawable = drawable;
	    pe->u.graphicsExposure.x = pBox->x1;
	    pe->u.graphicsExposure.y = pBox->y1;
	    pe->u.graphicsExposure.width = pBox->x2 - pBox->x1;
	    pe->u.graphicsExposure.height = pBox->y2 - pBox->y1;
	    pe->u.graphicsExposure.count = numRects - i;
	    pe->u.graphicsExposure.majorEvent = major;
	    pe->u.graphicsExposure.minorEvent = minor;
	}
377 378
	/* GraphicsExpose is a "critical event", which TryClientEvents
	 * handles specially. */
379
	TryClientEvents(client, NULL, pEvent, numRects,
380
			    (Mask)0, NoEventMask, NullGrab);
381
	free(pEvent);
382 383 384 385
    }
    else
    {
        xEvent event;
386
	memset(&event, 0, sizeof(xEvent));
387 388 389 390
	event.u.u.type = NoExpose;
	event.u.noExposure.drawable = drawable;
	event.u.noExposure.majorEvent = major;
	event.u.noExposure.minorEvent = minor;
391
	WriteEventsToClient(client, 1, &event);
392 393 394
    }
}

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
395

396
void
Peter Hutterer's avatar
Peter Hutterer committed
397
miSendExposures( WindowPtr pWin, RegionPtr pRgn, int dx, int dy)
398
{
399
    BoxPtr pBox;
400
    int numRects;
401 402
    xEvent *pEvent, *pe;
    int i;
403 404 405

    pBox = REGION_RECTS(pRgn);
    numRects = REGION_NUM_RECTS(pRgn);
406
    if(!(pEvent = calloc(1, numRects * sizeof(xEvent))))
407 408 409 410 411 412 413 414 415 416 417 418
	return;

    for (i=numRects, pe = pEvent; --i >= 0; pe++, pBox++)
    {
	pe->u.u.type = Expose;
	pe->u.expose.window = pWin->drawable.id;
	pe->u.expose.x = pBox->x1 - dx;
	pe->u.expose.y = pBox->y1 - dy;
	pe->u.expose.width = pBox->x2 - pBox->x1;
	pe->u.expose.height = pBox->y2 - pBox->y1;
	pe->u.expose.count = i;
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

#ifdef PANORAMIX
    if(!noPanoramiXExtension) {
	int scrnum = pWin->drawable.pScreen->myNum;
	int x = 0, y = 0;
	XID realWin = 0;

	if(!pWin->parent) {
	    x = panoramiXdataPtr[scrnum].x;
	    y = panoramiXdataPtr[scrnum].y;
	    pWin = WindowTable[0];
	    realWin = pWin->drawable.id;
	} else if (scrnum) {
	    PanoramiXRes *win;
	    win = PanoramiXFindIDByScrnum(XRT_WINDOW, 
			pWin->drawable.id, scrnum);
	    if(!win) {
436
		free(pEvent);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
437 438 439
		return;
	    }
	    realWin = win->info[0].id;
440
	    dixLookupWindow(&pWin, realWin, serverClient, DixSendAccess);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
441 442 443 444 445 446 447 448 449 450
	}
	if(x || y || scrnum)
	  for (i = 0; i < numRects; i++) {
	      pEvent[i].u.expose.window = realWin;
	      pEvent[i].u.expose.x += x;
	      pEvent[i].u.expose.y += y;
	  }
    }
#endif

451
    DeliverEvents(pWin, pEvent, numRects, NullWindow);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
452

453
    free(pEvent);
454 455
}

456
void
Peter Hutterer's avatar
Peter Hutterer committed
457
miWindowExposures( WindowPtr pWin, RegionPtr prgn, RegionPtr other_exposed)
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
{
    RegionPtr   exposures = prgn;
    if ((prgn && !REGION_NIL(prgn)) || 
	(exposures && !REGION_NIL(exposures)) || other_exposed)
    {
	RegionRec   expRec;
	int	    clientInterested;

	/*
	 * Restore from backing-store FIRST.
	 */
	clientInterested = (pWin->eventMask|wOtherEventMasks(pWin)) & ExposureMask;
	if (other_exposed)
	{
	    if (exposures)
	    {
		REGION_UNION(pWin->drawable.pScreen, other_exposed,
						  exposures,
					          other_exposed);
		if (exposures != prgn)
		    REGION_DESTROY(pWin->drawable.pScreen, exposures);
	    }
	    exposures = other_exposed;
	}
	if (clientInterested && exposures && (REGION_NUM_RECTS(exposures) > RECTLIMIT))
	{
	    /*
	     * If we have LOTS of rectangles, we decide to take the extents
	     * and force an exposure on that.  This should require much less
	     * work overall, on both client and server.  This is cheating, but
	     * isn't prohibited by the protocol ("spontaneous combustion" :-).
	     */
	    BoxRec box;

	    box = *REGION_EXTENTS( pWin->drawable.pScreen, exposures);
	    if (exposures == prgn) {
		exposures = &expRec;
		REGION_INIT( pWin->drawable.pScreen, exposures, &box, 1);
		REGION_RESET( pWin->drawable.pScreen, prgn, &box);
	    } else {
		REGION_RESET( pWin->drawable.pScreen, exposures, &box);
		REGION_UNION( pWin->drawable.pScreen, prgn, prgn, exposures);
	    }
501
	    /* miPaintWindow doesn't clip, so we have to */
502 503 504
	    REGION_INTERSECT( pWin->drawable.pScreen, prgn, prgn, &pWin->clipList);
	}
	if (prgn && !REGION_NIL(prgn))
505
	    miPaintWindow(pWin, prgn, PW_BACKGROUND);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	if (clientInterested && exposures && !REGION_NIL(exposures))
	    miSendExposures(pWin, exposures,
			    pWin->drawable.x, pWin->drawable.y);
	if (exposures == &expRec)
	{
	    REGION_UNINIT( pWin->drawable.pScreen, exposures);
	}
	else if (exposures && exposures != prgn && exposures != other_exposed)
	    REGION_DESTROY( pWin->drawable.pScreen, exposures);
	if (prgn)
	    REGION_EMPTY( pWin->drawable.pScreen, prgn);
    }
    else if (exposures && exposures != prgn)
	REGION_DESTROY( pWin->drawable.pScreen, exposures);
}

522 523 524 525 526 527 528 529
#ifdef ROOTLESS
/* Ugly, ugly, but we lost our hooks into miPaintWindow... =/ */
void RootlessSetPixmapOfAncestors(WindowPtr pWin);
void RootlessStartDrawing(WindowPtr pWin);
void RootlessDamageRegion(WindowPtr pWin, RegionPtr prgn);
Bool IsFramedWindow(WindowPtr pWin);
#endif 

530
void
531
miPaintWindow(WindowPtr pWin, RegionPtr prgn, int what)
532
{
533
    ScreenPtr	pScreen = pWin->drawable.pScreen;
534
    ChangeGCVal gcval[6];
535 536 537 538 539 540
    BITS32	gcmask;
    GCPtr	pGC;
    int		i;
    BoxPtr	pbox;
    xRectangle	*prect;
    int		numRects;
541 542 543 544 545 546 547 548 549 550
    /*
     * Distance from screen to destination drawable, use this
     * to adjust rendering coordinates which come in in screen space
     */
    int		draw_x_off, draw_y_off;
    /*
     * Tile offset for drawing; these need to align the tile
     * to the appropriate window origin
     */
    int		tile_x_off, tile_y_off;
551 552 553
    PixUnion	fill;
    Bool	solid = TRUE;
    DrawablePtr	drawable = &pWin->drawable;
554

555
#ifdef ROOTLESS
556 557 558
    if(!drawable || drawable->type == UNDRAWABLE_WINDOW)
	return;

559 560 561 562 563 564 565 566 567 568 569 570
    if(IsFramedWindow(pWin)) {
        RootlessStartDrawing(pWin);
        RootlessDamageRegion(pWin, prgn);
    
        if(pWin->backgroundState == ParentRelative) {
            if((what == PW_BACKGROUND) || 
               (what == PW_BORDER && !pWin->borderIsPixel))
                RootlessSetPixmapOfAncestors(pWin);
        }
    }
#endif
    
571 572
    if (what == PW_BACKGROUND)
    {
573 574 575
	while (pWin->backgroundState == ParentRelative)
	    pWin = pWin->parent;

576 577 578
	draw_x_off = drawable->x;
	draw_y_off = drawable->y;

579 580
	tile_x_off = pWin->drawable.x - draw_x_off;
	tile_y_off = pWin->drawable.y - draw_y_off;
581
	fill = pWin->background;
582 583 584 585
	switch (pWin->backgroundState) {
	case None:
	    return;
	case BackgroundPixmap:
586
	    solid = FALSE;
587 588 589 590 591
	    break;
	}
    }
    else
    {
592
	PixmapPtr   pixmap;
593

594 595 596
	tile_x_off = drawable->x;
	tile_y_off = drawable->y;
	
597 598 599
	/* servers without pixmaps draw their own borders */
	if (!pScreen->GetWindowPixmap)
	    return;
600 601
	pixmap = (*pScreen->GetWindowPixmap) ((WindowPtr) drawable);
	drawable = &pixmap->drawable;
602
#ifdef COMPOSITE
603 604 605 606
	draw_x_off = pixmap->screen_x;
	draw_y_off = pixmap->screen_y;
	tile_x_off -= draw_x_off;
	tile_y_off -= draw_y_off;
607 608 609 610 611 612 613 614 615 616 617
#else
	draw_x_off = 0;
	draw_y_off = 0;
#endif
	fill = pWin->border;
	solid = pWin->borderIsPixel;
    }
    
    gcval[0].val = GXcopy;
    gcmask = GCFunction;

618 619 620 621 622 623 624 625
#ifdef ROOTLESS_SAFEALPHA
/* Bit mask for alpha channel with a particular number of bits per
 * pixel. Note that we only care for 32bpp data. Mac OS X uses planar
 * alpha for 16bpp.
 */
#define RootlessAlphaMask(bpp) ((bpp) == 32 ? 0xFF000000 : 0)
#endif
    
626 627
    if (solid)
    {
628 629 630
#ifdef ROOTLESS_SAFEALPHA
	gcval[1].val = fill.pixel | RootlessAlphaMask(pWin->drawable.bitsPerPixel);
#else
631
	gcval[1].val = fill.pixel;
632
#endif
633 634 635 636 637
	gcval[2].val  = FillSolid;
	gcmask |= GCForeground | GCFillStyle;
    }
    else
    {
638 639 640 641 642 643 644 645 646
	int c=1;
#ifdef ROOTLESS_SAFEALPHA
	gcval[c++].val = ((CARD32)-1) & ~RootlessAlphaMask(pWin->drawable.bitsPerPixel);
	gcmask |= GCPlaneMask;
#endif
	gcval[c++].val = FillTiled;
	gcval[c++].ptr = (pointer)fill.pixmap;
	gcval[c++].val = tile_x_off;
	gcval[c++].val = tile_y_off;
647
	gcmask |= GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin;
648 649
    }

650
    prect = malloc(REGION_NUM_RECTS(prgn) * sizeof(xRectangle));
651 652 653
    if (!prect)
	return;

654
    pGC = GetScratchGC(drawable->depth, drawable->pScreen);
655
    if (!pGC)
656
    {
657
	free(prect);
658
	return;
659 660
    }
    
661
    ChangeGC (NullClient, pGC, gcmask, gcval);
662
    ValidateGC (drawable, pGC);
663 664 665 666 667

    numRects = REGION_NUM_RECTS(prgn);
    pbox = REGION_RECTS(prgn);
    for (i= numRects; --i >= 0; pbox++, prect++)
    {
668 669
	prect->x = pbox->x1 - draw_x_off;
	prect->y = pbox->y1 - draw_y_off;
670 671 672 673
	prect->width = pbox->x2 - pbox->x1;
	prect->height = pbox->y2 - pbox->y1;
    }
    prect -= numRects;
674
    (*pGC->ops->PolyFillRect)(drawable, pGC, numRects, prect);
675
    free(prect);
676

677
    FreeScratchGC(pGC);
678 679 680 681 682 683
}


/* MICLEARDRAWABLE -- sets the entire drawable to the background color of
 * the GC.  Useful when we have a scratch drawable and need to initialize 
 * it. */
684
void
Peter Hutterer's avatar
Peter Hutterer committed
685
miClearDrawable(DrawablePtr pDraw, GCPtr pGC)
686
{
687
    ChangeGCVal fg, bg;
688 689
    xRectangle rect;

690 691
    fg.val = pGC->fgPixel;
    bg.val = pGC->bgPixel;
692 693 694 695
    rect.x = 0;
    rect.y = 0;
    rect.width = pDraw->width;
    rect.height = pDraw->height;
696
    ChangeGC(NullClient, pGC, GCForeground, &bg);
697 698
    ValidateGC(pDraw, pGC);
    (*pGC->ops->PolyFillRect)(pDraw, pGC, 1, &rect);
699
    ChangeGC(NullClient, pGC, GCForeground, &fg);
700 701
    ValidateGC(pDraw, pGC);
}