1706f2543Smrg/*
2706f2543Smrg
3706f2543SmrgCopyright 1993, 1998  The Open Group
4706f2543Smrg
5706f2543SmrgPermission to use, copy, modify, distribute, and sell this software and its
6706f2543Smrgdocumentation for any purpose is hereby granted without fee, provided that
7706f2543Smrgthe above copyright notice appear in all copies and that both that
8706f2543Smrgcopyright notice and this permission notice appear in supporting
9706f2543Smrgdocumentation.
10706f2543Smrg
11706f2543SmrgThe above copyright notice and this permission notice shall be included
12706f2543Smrgin all copies or substantial portions of the Software.
13706f2543Smrg
14706f2543SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15706f2543SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16706f2543SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17706f2543SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18706f2543SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19706f2543SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20706f2543SmrgOTHER DEALINGS IN THE SOFTWARE.
21706f2543Smrg
22706f2543SmrgExcept as contained in this notice, the name of The Open Group shall
23706f2543Smrgnot be used in advertising or otherwise to promote the sale, use or
24706f2543Smrgother dealings in this Software without prior written authorization
25706f2543Smrgfrom The Open Group.
26706f2543Smrg
27706f2543Smrg*/
28706f2543Smrg
29706f2543Smrg#ifdef HAVE_DIX_CONFIG_H
30706f2543Smrg#include <dix-config.h>
31706f2543Smrg#endif
32706f2543Smrg
33706f2543Smrg#include <X11/X.h>
34706f2543Smrg#include "scrnintstr.h"
35706f2543Smrg#include "misc.h"
36706f2543Smrg#include "os.h"
37706f2543Smrg#include "windowstr.h"
38706f2543Smrg#include "resource.h"
39706f2543Smrg#include "dixstruct.h"
40706f2543Smrg#include "gcstruct.h"
41706f2543Smrg#include "servermd.h"
42706f2543Smrg#include "site.h"
43706f2543Smrg
44706f2543Smrg
45706f2543Smrg/*
46706f2543Smrg *  Scratch pixmap management and device independent pixmap allocation
47706f2543Smrg *  function.
48706f2543Smrg */
49706f2543Smrg
50706f2543Smrg
51706f2543Smrg/* callable by ddx */
52706f2543SmrgPixmapPtr
53706f2543SmrgGetScratchPixmapHeader(ScreenPtr pScreen, int width, int height, int depth,
54706f2543Smrg                       int bitsPerPixel, int devKind, pointer pPixData)
55706f2543Smrg{
56706f2543Smrg    PixmapPtr pPixmap = pScreen->pScratchPixmap;
57706f2543Smrg
58706f2543Smrg    if (pPixmap)
59706f2543Smrg	pScreen->pScratchPixmap = NULL;
60706f2543Smrg    else
61706f2543Smrg	/* width and height of 0 means don't allocate any pixmap data */
62706f2543Smrg	pPixmap = (*pScreen->CreatePixmap)(pScreen, 0, 0, depth, 0);
63706f2543Smrg
64706f2543Smrg    if (pPixmap) {
65706f2543Smrg	if ((*pScreen->ModifyPixmapHeader)(pPixmap, width, height, depth,
66706f2543Smrg					   bitsPerPixel, devKind, pPixData))
67706f2543Smrg	    return pPixmap;
68706f2543Smrg	(*pScreen->DestroyPixmap)(pPixmap);
69706f2543Smrg    }
70706f2543Smrg    return NullPixmap;
71706f2543Smrg}
72706f2543Smrg
73706f2543Smrg
74706f2543Smrg/* callable by ddx */
75706f2543Smrgvoid
76706f2543SmrgFreeScratchPixmapHeader(PixmapPtr pPixmap)
77706f2543Smrg{
78706f2543Smrg    if (pPixmap)
79706f2543Smrg    {
80706f2543Smrg	ScreenPtr pScreen = pPixmap->drawable.pScreen;
81706f2543Smrg
82706f2543Smrg	pPixmap->devPrivate.ptr = NULL; /* lest ddx chases bad ptr */
83706f2543Smrg	if (pScreen->pScratchPixmap)
84706f2543Smrg	    (*pScreen->DestroyPixmap)(pPixmap);
85706f2543Smrg	else
86706f2543Smrg	    pScreen->pScratchPixmap = pPixmap;
87706f2543Smrg    }
88706f2543Smrg}
89706f2543Smrg
90706f2543Smrg
91706f2543SmrgBool
92706f2543SmrgCreateScratchPixmapsForScreen(int scrnum)
93706f2543Smrg{
94706f2543Smrg    unsigned int	pixmap_size;
95706f2543Smrg
96706f2543Smrg    pixmap_size = sizeof(PixmapRec) + dixPrivatesSize(PRIVATE_PIXMAP);
97706f2543Smrg    screenInfo.screens[scrnum]->totalPixmapSize = BitmapBytePad(pixmap_size * 8);
98706f2543Smrg
99706f2543Smrg    /* let it be created on first use */
100706f2543Smrg    screenInfo.screens[scrnum]->pScratchPixmap = NULL;
101706f2543Smrg    return TRUE;
102706f2543Smrg}
103706f2543Smrg
104706f2543Smrg
105706f2543Smrgvoid
106706f2543SmrgFreeScratchPixmapsForScreen(int scrnum)
107706f2543Smrg{
108706f2543Smrg    FreeScratchPixmapHeader(screenInfo.screens[scrnum]->pScratchPixmap);
109706f2543Smrg}
110706f2543Smrg
111706f2543Smrg
112706f2543Smrg/* callable by ddx */
113706f2543SmrgPixmapPtr
114706f2543SmrgAllocatePixmap(ScreenPtr pScreen, int pixDataSize)
115706f2543Smrg{
116706f2543Smrg    PixmapPtr pPixmap;
117706f2543Smrg
118706f2543Smrg    assert(pScreen->totalPixmapSize > 0);
119706f2543Smrg
120706f2543Smrg    if (pScreen->totalPixmapSize > ((size_t)-1) - pixDataSize)
121706f2543Smrg	return NullPixmap;
122706f2543Smrg
12332414907Smaya    pPixmap = calloc(1, pScreen->totalPixmapSize + pixDataSize);
124706f2543Smrg    if (!pPixmap)
125706f2543Smrg	return NullPixmap;
126706f2543Smrg
127706f2543Smrg    dixInitPrivates(pPixmap, pPixmap + 1, PRIVATE_PIXMAP);
128706f2543Smrg    return pPixmap;
129706f2543Smrg}
130706f2543Smrg
131706f2543Smrg/* callable by ddx */
132706f2543Smrgvoid
133706f2543SmrgFreePixmap(PixmapPtr pPixmap)
134706f2543Smrg{
135706f2543Smrg    dixFiniPrivates(pPixmap, PRIVATE_PIXMAP);
136706f2543Smrg    free(pPixmap);
137706f2543Smrg}
138