GrayPixmap.c revision 6c321187
16c321187Smrg/* $Xorg: GrayPixmap.c,v 1.4 2001/02/09 02:03:52 xorgcvs Exp $ */
26c321187Smrg
36c321187Smrg/*
46c321187Smrg
56c321187SmrgCopyright 1987, 1988, 1998  The Open Group
66c321187Smrg
76c321187SmrgPermission to use, copy, modify, distribute, and sell this software and its
86c321187Smrgdocumentation for any purpose is hereby granted without fee, provided that
96c321187Smrgthe above copyright notice appear in all copies and that both that
106c321187Smrgcopyright notice and this permission notice appear in supporting
116c321187Smrgdocumentation.
126c321187Smrg
136c321187SmrgThe above copyright notice and this permission notice shall be included in
146c321187Smrgall copies or substantial portions of the Software.
156c321187Smrg
166c321187SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
176c321187SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
186c321187SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
196c321187SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
206c321187SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
216c321187SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
226c321187Smrg
236c321187SmrgExcept as contained in this notice, the name of The Open Group shall not be
246c321187Smrgused in advertising or otherwise to promote the sale, use or other dealings
256c321187Smrgin this Software without prior written authorization from The Open Group.
266c321187Smrg
276c321187Smrg*/
286c321187Smrg
296c321187Smrg/***********************************************************
306c321187Smrg
316c321187SmrgCopyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
326c321187Smrg
336c321187Smrg                        All Rights Reserved
346c321187Smrg
356c321187SmrgPermission to use, copy, modify, and distribute this software and its
366c321187Smrgdocumentation for any purpose and without fee is hereby granted,
376c321187Smrgprovided that the above copyright notice appear in all copies and that
386c321187Smrgboth that copyright notice and this permission notice appear in
396c321187Smrgsupporting documentation, and that the name of Digital not be
406c321187Smrgused in advertising or publicity pertaining to distribution of the
416c321187Smrgsoftware without specific, written prior permission.
426c321187Smrg
436c321187SmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
446c321187SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
456c321187SmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
466c321187SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
476c321187SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
486c321187SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
496c321187SmrgSOFTWARE.
506c321187Smrg
516c321187Smrg******************************************************************/
526c321187Smrg/* $XFree86: xc/lib/Xmu/GrayPixmap.c,v 1.5 2001/01/17 19:42:55 dawes Exp $ */
536c321187Smrg
546c321187Smrg#ifdef HAVE_CONFIG_H
556c321187Smrg#include <config.h>
566c321187Smrg#endif
576c321187Smrg#include <stdio.h>
586c321187Smrg#include <X11/Intrinsic.h>
596c321187Smrg#include <X11/Xmu/Drawing.h>
606c321187Smrg
616c321187Smrgtypedef struct _PixmapCache {
626c321187Smrg    Screen *screen;
636c321187Smrg    Pixmap pixmap;
646c321187Smrg    Pixel foreground, background;
656c321187Smrg    unsigned int depth;
666c321187Smrg    int ref_count;
676c321187Smrg    struct _PixmapCache *next;
686c321187Smrg  } CacheEntry;
696c321187Smrg
706c321187Smrgstatic CacheEntry *pixmapCache = NULL;
716c321187Smrg
726c321187Smrg
736c321187Smrg
746c321187SmrgPixmap
756c321187SmrgXmuCreateStippledPixmap(Screen *screen, Pixel fore, Pixel back,
766c321187Smrg			unsigned int depth)
776c321187Smrg/*
786c321187Smrg *	Creates a stippled pixmap of specified depth
796c321187Smrg *	caches these so that multiple requests share the pixmap
806c321187Smrg */
816c321187Smrg{
826c321187Smrg    register Display *display = DisplayOfScreen(screen);
836c321187Smrg    CacheEntry *cachePtr;
846c321187Smrg    Pixmap stippled_pixmap;
856c321187Smrg    static unsigned char pixmap_bits[] = {
866c321187Smrg	0x02, 0x01,
876c321187Smrg    };
886c321187Smrg
896c321187Smrg/*
906c321187Smrg *	Creates a stippled pixmap of depth DefaultDepth(screen)
916c321187Smrg *	caches these so that multiple requests share the pixmap
926c321187Smrg */
936c321187Smrg
946c321187Smrg#define pixmap_width 2
956c321187Smrg#define pixmap_height 2
966c321187Smrg
976c321187Smrg    /* see if we already have a pixmap suitable for this screen */
986c321187Smrg    for (cachePtr = pixmapCache; cachePtr; cachePtr = cachePtr->next) {
996c321187Smrg	if (cachePtr->screen == screen && cachePtr->foreground == fore &&
1006c321187Smrg	    cachePtr->background == back && cachePtr->depth == depth)
1016c321187Smrg	    return( cachePtr->ref_count++, cachePtr->pixmap );
1026c321187Smrg    }
1036c321187Smrg
1046c321187Smrg    stippled_pixmap = XCreatePixmapFromBitmapData (display,
1056c321187Smrg			RootWindowOfScreen(screen), (char *)pixmap_bits,
1066c321187Smrg			pixmap_width, pixmap_height, fore, back, depth);
1076c321187Smrg
1086c321187Smrg    /* and insert it at the head of the cache */
1096c321187Smrg    cachePtr = XtNew(CacheEntry);
1106c321187Smrg    cachePtr->screen = screen;
1116c321187Smrg    cachePtr->foreground = fore;
1126c321187Smrg    cachePtr->background = back;
1136c321187Smrg    cachePtr->depth = depth;
1146c321187Smrg    cachePtr->pixmap = stippled_pixmap;
1156c321187Smrg    cachePtr->ref_count = 1;
1166c321187Smrg    cachePtr->next = pixmapCache;
1176c321187Smrg    pixmapCache = cachePtr;
1186c321187Smrg
1196c321187Smrg    return( stippled_pixmap );
1206c321187Smrg}
1216c321187Smrg
1226c321187Smrgvoid
1236c321187SmrgXmuReleaseStippledPixmap(Screen *screen, Pixmap pixmap)
1246c321187Smrg{
1256c321187Smrg    register Display *display = DisplayOfScreen(screen);
1266c321187Smrg    CacheEntry *cachePtr, **prevP;
1276c321187Smrg    for (prevP = &pixmapCache, cachePtr = pixmapCache; cachePtr;) {
1286c321187Smrg	if (cachePtr->screen == screen && cachePtr->pixmap == pixmap) {
1296c321187Smrg	    if (--cachePtr->ref_count == 0) {
1306c321187Smrg		XFreePixmap( display, pixmap );
1316c321187Smrg		*prevP = cachePtr->next;
1326c321187Smrg		XtFree( (char*)cachePtr );
1336c321187Smrg		break;
1346c321187Smrg	    }
1356c321187Smrg	}
1366c321187Smrg	prevP = &cachePtr->next;
1376c321187Smrg	cachePtr = *prevP;
1386c321187Smrg    }
1396c321187Smrg}
140