summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/xfree86/xf24_32bpp/cfbpixmap.c
blob: 8250c8c5b24e9fa8f6f421ca867015f5cefeaca1 (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
/* $XFree86: xc/programs/Xserver/hw/xfree86/xf24_32bpp/cfbpixmap.c,v 1.3 2000/04/01 00:17:19 mvojkovi Exp $ */

#include "Xmd.h"
#include "servermd.h"
#include "scrnintstr.h"
#include "pixmapstr.h"
#include "mi.h"
#define PSZ 8
#include "cfb.h"
#undef PSZ
#include "cfb24_32.h"


PixmapPtr
cfb24_32CreatePixmap (
    ScreenPtr	pScreen,
    int		width,
    int		height,
    int		depth
){
    cfb24_32PixmapPtr pPixPriv;
    PixmapPtr pPix;
    int size, bpp, pitch;

    /* All depth 24 pixmaps are 24bpp unless the caller is allocating
	its own data (width == 0) */

    if(depth == 1) {
	pitch = ((width + 31) >> 5) << 2;
	bpp = 1;
    } else {  /* depth == 24 */
	pitch = ((width * 3) + 3) & ~3L;
	bpp = (width && height) ? 24 : 32;
    }

    size = height * pitch;
    pPix = AllocatePixmap(pScreen, size);
    if (!pPix)
	return NullPixmap;
    pPix->drawable.type = DRAWABLE_PIXMAP;
    pPix->drawable.class = 0;
    pPix->drawable.pScreen = pScreen;
    pPix->drawable.depth = depth;
    pPix->drawable.bitsPerPixel = bpp;
    pPix->drawable.id = 0;
    pPix->drawable.serialNumber = NEXT_SERIAL_NUMBER;
    pPix->drawable.x = 0;
    pPix->drawable.y = 0;
    pPix->drawable.width = width;
    pPix->drawable.height = height;
    pPix->devKind = pitch;
    pPix->refcnt = 1;
    pPix->devPrivate.ptr = size ? 
	(pointer)((char*)pPix + pScreen->totalPixmapSize) : NULL;

    pPixPriv = CFB24_32_GET_PIXMAP_PRIVATE(pPix);
    pPixPriv->pix = NULL;           /* no clone yet */
    pPixPriv->freePrivate = FALSE;
    pPixPriv->isRefPix = FALSE;

    return pPix;
}

Bool
cfb24_32DestroyPixmap(PixmapPtr pPix)
{
    cfb24_32PixmapPtr pPriv = CFB24_32_GET_PIXMAP_PRIVATE(pPix);
    PixmapPtr pClone = pPriv->pix;

    if(pClone) {
	cfb24_32PixmapPtr cPriv = CFB24_32_GET_PIXMAP_PRIVATE(pClone);
	int refcnt = pPix->refcnt;
	cPriv->pix = NULL; /* avoid looping back */
	
	if(refcnt != pClone->refcnt)
	   ErrorF("Pixmap refcnt mismatch in DestroyPixmap()\n");

	(*pPix->drawable.pScreen->DestroyPixmap)(pClone);

	if(refcnt > 1)
	   cPriv->pix = pPix;
    }

    if(--pPix->refcnt)
	return TRUE;

    if(pPriv->freePrivate)
	xfree(pPix->devPrivate.ptr);
    xfree(pPix);

    return TRUE;
}

PixmapPtr
cfb24_32RefreshPixmap(PixmapPtr pPix) 
{
    cfb24_32PixmapPtr newPixPriv, pixPriv = CFB24_32_GET_PIXMAP_PRIVATE(pPix);
    ScreenPtr pScreen = pPix->drawable.pScreen;
    int width = pPix->drawable.width;
    int height = pPix->drawable.height;
    GCPtr pGC;

    if(pPix->drawable.bitsPerPixel == 24) {
        if(!pixPriv->pix) {	/* need to make a 32bpp clone */
	    int pitch = width << 2;
	    unsigned char* data;
	    PixmapPtr newPix;

	    if(!(data = (unsigned char*)xalloc(pitch * height)))
		FatalError("Out of memory\n");

	    /* cfb24_32CreatePixmap will make a 32bpp header for us */
	    newPix = (*pScreen->CreatePixmap)(pScreen, 0, 0, 24);
	    newPix->devKind = pitch;
	    newPix->devPrivate.ptr = (pointer)data;
	    newPix->drawable.width = width;
	    newPix->drawable.height = height;
	    newPix->refcnt = pPix->refcnt;
	    pixPriv->pix = newPix;
	    newPixPriv = CFB24_32_GET_PIXMAP_PRIVATE(newPix);
	    newPixPriv->pix = pPix;
	    newPixPriv->freePrivate = TRUE;
	    pixPriv->isRefPix = TRUE;
	}
    } else { /* bitsPerPixel == 32 */
        if(!pixPriv->pix) {	/* need to make a 32bpp clone */

	    /* cfb24_32CreatePixmap will make a 24bpp pixmap for us */
	    pixPriv->pix = (*pScreen->CreatePixmap)(pScreen, width, height, 24);
	    pixPriv->pix->refcnt = pPix->refcnt;
	    newPixPriv = CFB24_32_GET_PIXMAP_PRIVATE(pixPriv->pix);
	    newPixPriv->pix = pPix;
	    pixPriv->isRefPix = TRUE;
	}
    }

    if(pPix->refcnt != pixPriv->pix->refcnt)
	ErrorF("Pixmap refcnt mismatch in RefreshPixmap()\n");

    /* make sure copies only go from the real to the clone */
    if(pixPriv->isRefPix) {
	pGC = GetScratchGC(24, pScreen);
	ValidateGC((DrawablePtr)pixPriv->pix, pGC);
	(*pGC->ops->CopyArea)((DrawablePtr)pPix, (DrawablePtr)pixPriv->pix,
					pGC, 0, 0, width, height, 0, 0);
	FreeScratchGC(pGC);
    }

    return pixPriv->pix;
}