summaryrefslogtreecommitdiff
path: root/src/rhd_shadow.c
blob: ecb670950dfc94750bf2b7ea4a819de0b00f2e5b (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright 2007  Luc Verhaegen <libv@exsuse.de>
 * Copyright 2007  Matthias Hopf <mhopf@novell.com>
 * Copyright 2007  Egbert Eich   <eich@novell.com>
 * Copyright 2007  Advanced Micro Devices, Inc.
 *
 * 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, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) 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.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "scrnintstr.h"
#include "shadow.h"
#include "fb.h"
#include "xf86.h"
#include "rhd.h"

typedef struct _rhdShadowRec
{
    unsigned char *shadow;
    CreateScreenResourcesProcPtr CreateScreenResources;
} rhdShadowRec;

Bool
RHDShadowPreInit(ScrnInfoPtr pScrn)
{
    RHDPtr rhdPtr = RHDPTR(pScrn);

    RHDFUNC(pScrn);

    if (!xf86LoadSubModule(pScrn, "shadow")) {
	return FALSE;
    }
    if (!(rhdPtr->shadowPtr = (rhdShadowPtr)xalloc(sizeof(rhdShadowRec))))
	return FALSE;

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Using ShadowFB\n");

    return TRUE;
}

static void *
rhdShadowWindow(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
	  CARD32 *size, void *closure)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    RHDPtr rhdPtr = RHDPTR(pScrn);

    DEBUGP(RHDDebugVerb(pScrn->scrnIndex, 1, "FUNCTION: %s row: %i\n",__func__,row));

    *size = pScrn->displayWidth * (pScrn->bitsPerPixel >> 3);
    return ((CARD8 *)rhdPtr->FbBase + rhdPtr->FbScanoutStart
	    + row * (*size) + offset);
}

static Bool
rhdShadowCreateScreenResources(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    RHDPtr rhdPtr = RHDPTR(pScrn);
    rhdShadowPtr shadowPtr = rhdPtr->shadowPtr;
    Bool ret;
    shadowUpdateProc update;

    RHDFUNC(pScrn);

    update = shadowUpdatePackedWeak();
    pScreen->CreateScreenResources = shadowPtr->CreateScreenResources;
    ret = pScreen->CreateScreenResources(pScreen);
    pScreen->CreateScreenResources = rhdShadowCreateScreenResources;

    shadowAdd(pScreen, pScreen->GetScreenPixmap(pScreen),
	      update,
	      rhdShadowWindow, 0, 0);

    return ret;
}

Bool
RHDShadowScreenInit(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    RHDPtr rhdPtr = RHDPTR(pScrn);
    rhdShadowPtr shadowPtr = rhdPtr->shadowPtr;
    Bool ret;

    RHDFUNC(pScrn);

    if (shadowPtr) {
	if (!(shadowPtr->shadow
	      = xcalloc(1, pScrn->displayWidth * pScrn->virtualY
			* ((pScrn->bitsPerPixel + 7) >> 3)))) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		       "Failed to allocate shadow FB buffer\n");
	    return FALSE;
	}
	ret =  fbScreenInit(pScreen,
			    (CARD8 *) shadowPtr->shadow,
			    pScrn->virtualX, pScrn->virtualY,
			    pScrn->xDpi, pScrn->yDpi,
			    pScrn->displayWidth, pScrn->bitsPerPixel);
	if (!ret) {
	    xfree(shadowPtr->shadow);
	    shadowPtr->shadow = NULL;
	}
	return ret;
    }
    return FALSE;
}

Bool
RHDShadowSetup(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    RHDPtr rhdPtr = RHDPTR(pScrn);
    rhdShadowPtr shadowPtr = rhdPtr->shadowPtr;

    RHDFUNC(pScrn);

    if (!shadowPtr || !shadowPtr->shadow)
	return TRUE;

    if (!shadowSetup(pScreen))
	return FALSE;

    shadowPtr->CreateScreenResources = pScreen->CreateScreenResources;
    pScreen->CreateScreenResources = rhdShadowCreateScreenResources;

    return TRUE;
}

Bool
RHDShadowCloseScreen(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    RHDPtr rhdPtr = RHDPTR(pScrn);
    rhdShadowPtr shadowPtr = rhdPtr->shadowPtr;

    RHDFUNC(pScrn);

    if (!shadowPtr)
	return TRUE;

    xfree(shadowPtr->shadow);
    shadowPtr->shadow = NULL;

    return TRUE;
}

void
RHDShadowDestroy(RHDPtr rhdPtr)
{
    RHDFUNC(rhdPtr);

    if (rhdPtr->shadowPtr) {
	xfree(rhdPtr->shadowPtr);
	rhdPtr->shadowPtr = NULL;
    }
}