pixmap.c revision 6747b715
1/*
2
3Copyright 1993, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28
29#ifdef HAVE_DIX_CONFIG_H
30#include <dix-config.h>
31#endif
32
33#include <X11/X.h>
34#include "scrnintstr.h"
35#include "misc.h"
36#include "os.h"
37#include "windowstr.h"
38#include "resource.h"
39#include "dixstruct.h"
40#include "gcstruct.h"
41#include "servermd.h"
42#include "site.h"
43
44
45/*
46 *  Scratch pixmap management and device independent pixmap allocation
47 *  function.
48 */
49
50
51/* callable by ddx */
52PixmapPtr
53GetScratchPixmapHeader(ScreenPtr pScreen, int width, int height, int depth,
54                       int bitsPerPixel, int devKind, pointer pPixData)
55{
56    PixmapPtr pPixmap = pScreen->pScratchPixmap;
57
58    if (pPixmap)
59	pScreen->pScratchPixmap = NULL;
60    else
61	/* width and height of 0 means don't allocate any pixmap data */
62	pPixmap = (*pScreen->CreatePixmap)(pScreen, 0, 0, depth, 0);
63
64    if (pPixmap) {
65	if ((*pScreen->ModifyPixmapHeader)(pPixmap, width, height, depth,
66					   bitsPerPixel, devKind, pPixData))
67	    return pPixmap;
68	(*pScreen->DestroyPixmap)(pPixmap);
69    }
70    return NullPixmap;
71}
72
73
74/* callable by ddx */
75void
76FreeScratchPixmapHeader(PixmapPtr pPixmap)
77{
78    if (pPixmap)
79    {
80	ScreenPtr pScreen = pPixmap->drawable.pScreen;
81
82	pPixmap->devPrivate.ptr = NULL; /* lest ddx chases bad ptr */
83	if (pScreen->pScratchPixmap)
84	    (*pScreen->DestroyPixmap)(pPixmap);
85	else
86	    pScreen->pScratchPixmap = pPixmap;
87    }
88}
89
90
91Bool
92CreateScratchPixmapsForScreen(int scrnum)
93{
94    unsigned int	pixmap_size;
95
96    pixmap_size = sizeof(PixmapRec) + dixPrivatesSize(PRIVATE_PIXMAP);
97    screenInfo.screens[scrnum]->totalPixmapSize = BitmapBytePad(pixmap_size * 8);
98
99    /* let it be created on first use */
100    screenInfo.screens[scrnum]->pScratchPixmap = NULL;
101    return TRUE;
102}
103
104
105void
106FreeScratchPixmapsForScreen(int scrnum)
107{
108    FreeScratchPixmapHeader(screenInfo.screens[scrnum]->pScratchPixmap);
109}
110
111
112/* callable by ddx */
113PixmapPtr
114AllocatePixmap(ScreenPtr pScreen, int pixDataSize)
115{
116    PixmapPtr pPixmap;
117
118    assert(pScreen->totalPixmapSize > 0);
119
120    if (pScreen->totalPixmapSize > ((size_t)-1) - pixDataSize)
121	return NullPixmap;
122
123    pPixmap = malloc(pScreen->totalPixmapSize + pixDataSize);
124    if (!pPixmap)
125	return NullPixmap;
126
127    dixInitPrivates(pPixmap, pPixmap + 1, PRIVATE_PIXMAP);
128    return pPixmap;
129}
130
131/* callable by ddx */
132void
133FreePixmap(PixmapPtr pPixmap)
134{
135    dixFiniPrivates(pPixmap, PRIVATE_PIXMAP);
136    free(pPixmap);
137}
138