summaryrefslogtreecommitdiff
path: root/src/i830_xf86Rotate.c
blob: 12b209196e3d91e874421f517cbc2fb554da725a (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright © 2006 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

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

#include <stddef.h>
#include <string.h>
#include <stdio.h>

#include "xf86.h"
#include "xf86DDC.h"
/*#include "i830.h" */
#include "i830_xf86Crtc.h"
#include "i830_xf86Modes.h"
#include "i830_randr.h"
#include "X11/extensions/render.h"
#define DPMS_SERVER
#include "X11/extensions/dpms.h"
#include "X11/Xatom.h"

static int
mode_height (DisplayModePtr mode, Rotation rotation)
{
    switch (rotation & 0xf) {
    case RR_Rotate_0:
    case RR_Rotate_180:
	return mode->VDisplay;
    case RR_Rotate_90:
    case RR_Rotate_270:
	return mode->HDisplay;
    default:
	return 0;
    }
}

static int
mode_width (DisplayModePtr mode, Rotation rotation)
{
    switch (rotation & 0xf) {
    case RR_Rotate_0:
    case RR_Rotate_180:
	return mode->HDisplay;
    case RR_Rotate_90:
    case RR_Rotate_270:
	return mode->VDisplay;
    default:
	return 0;
    }
}

/* borrowed from composite extension, move to Render and publish? */

static VisualPtr
compGetWindowVisual (WindowPtr pWin)
{
    ScreenPtr	    pScreen = pWin->drawable.pScreen;
    VisualID	    vid = wVisual (pWin);
    int		    i;

    for (i = 0; i < pScreen->numVisuals; i++)
	if (pScreen->visuals[i].vid == vid)
	    return &pScreen->visuals[i];
    return 0;
}

static PictFormatPtr
compWindowFormat (WindowPtr pWin)
{
    ScreenPtr	pScreen = pWin->drawable.pScreen;
    
    return PictureMatchVisual (pScreen, pWin->drawable.depth,
			       compGetWindowVisual (pWin));
}

static void
xf86RotateCrtcRedisplay (xf86CrtcPtr crtc, RegionPtr region)
{
    ScrnInfoPtr		scrn = crtc->scrn;
    ScreenPtr		screen = scrn->pScreen;
    PixmapPtr		src_pixmap = (*screen->GetScreenPixmap) (screen);
    PixmapPtr		dst_pixmap = crtc->rotatedPixmap;
    PictFormatPtr	format = compWindowFormat (WindowTable[screen->myNum]);
    int			error;
    PicturePtr		src, dst;
    PictTransform	transform;
    
    src = CreatePicture (None,
			 &src_pixmap->drawable,
			 format,
			 0L,
			 NULL,
			 serverClient,
			 &error);
    if (!src)
	return;
    dst = CreatePicture (None,
			 &dst_pixmap->drawable,
			 format,
			 0L,
			 NULL,
			 serverClient,
			 &error);
    if (!dst)
	return;
    SetPictureClipRegion (src, 0, 0, region);
    memset (&transform, '\0', sizeof (transform));
    transform.matrix[2][2] = 1;
    transform.matrix[0][2] = crtc->x;
    transform.matrix[1][2] = crtc->y;
    switch (crtc->curRotation & 0xf) {
    case RR_Rotate_0:
	transform.matrix[0][0] = 1;
	transform.matrix[1][1] = 1;
	break;
    case RR_Rotate_90:
	/* XXX probably wrong */
	transform.matrix[0][1] = 1;
	transform.matrix[1][0] = -1;
	transform.matrix[1][2] += crtc->curMode.HDisplay;
	break;
    case RR_Rotate_180:
	/* XXX probably wrong */
	transform.matrix[0][0] = -1;
	transform.matrix[1][1] = -1;
	transform.matrix[0][2] += crtc->curMode.HDisplay;
	transform.matrix[1][2] += crtc->curMode.VDisplay;
	break;
    case RR_Rotate_270:
	/* XXX probably wrong */
	transform.matrix[0][1] = -1;
	transform.matrix[1][0] = 1;
	transform.matrix[0][2] += crtc->curMode.VDisplay;
	break;
    }
    /* handle reflection */
    if (crtc->curRotation & RR_Reflect_X)
    {
	/* XXX figure this out */
    }
    if (crtc->curRotation & RR_Reflect_Y)
    {
	/* XXX figure this out too */
    }
    SetPictureTransform (src, &transform);
    CompositePicture (PictOpSrc,
		      src, NULL, dst,
		      0, 0, 0, 0, 0, 0,
		      dst_pixmap->drawable.width,
		      dst_pixmap->drawable.height);
    FreePicture (src, None);
    FreePicture (dst, None);
}

static void
xf86RotateRedisplay(ScreenPtr pScreen)
{
    ScrnInfoPtr		pScrn = xf86Screens[pScreen->myNum];
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
    DamagePtr		damage = xf86_config->rotationDamage;
    RegionPtr		region;

    if (!damage)
	return;
    region = DamageRegion(damage);
    if (REGION_NOTEMPTY(pScreen, region)) 
    {
	int		    c;
	
	for (c = 0; c < xf86_config->num_crtc; c++)
	{
	    xf86CrtcPtr	    crtc = xf86_config->crtc[c];

	    if (crtc->curRotation != RR_Rotate_0)
	    {
		BoxRec	    box;
		RegionRec   crtc_damage;

		/* compute portion of damage that overlaps crtc */
		box.x1 = crtc->x;
		box.x2 = crtc->x + mode_width (&crtc->curMode, crtc->curRotation);
		box.y1 = crtc->y;
		box.y2 = crtc->y + mode_height (&crtc->curMode, crtc->curRotation);
		REGION_INIT(pScreen, &crtc_damage, &box, 1);
		REGION_INTERSECT (pScreen, &crtc_damage, &crtc_damage, region);
		
		/* update damaged region */
		if (REGION_NOTEMPTY(pScreen, &crtc_damage))
		    xf86RotateCrtcRedisplay (crtc, &crtc_damage);
		
		REGION_UNINIT (pScreen, &crtc_damage);
	    }
	}
	DamageEmpty(damage);
    }
}

static void
xf86RotateBlockHandler(pointer data, OSTimePtr pTimeout, pointer pRead)
{
    ScreenPtr pScreen = (ScreenPtr) data;

    xf86RotateRedisplay(pScreen);
}

static void
xf86RotateWakeupHandler(pointer data, int i, pointer LastSelectMask)
{
}

Bool
xf86CrtcRotate (xf86CrtcPtr crtc, DisplayModePtr mode, Rotation rotation)
{
    ScrnInfoPtr		pScrn = crtc->scrn;
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
    ScreenPtr		pScreen = pScrn->pScreen;
    
    if (rotation == RR_Rotate_0)
    {
	/* Free memory from rotation */
	if (crtc->rotatedPixmap)
	{
	    crtc->funcs->shadow_destroy (crtc, crtc->rotatedPixmap);
	    crtc->rotatedPixmap = NULL;
	}

	if (xf86_config->rotationDamage)
	{
	    /* Free damage structure */
	    DamageDestroy (xf86_config->rotationDamage);
	    xf86_config->rotationDamage = NULL;
	    /* Free block/wakeup handler */
	    RemoveBlockAndWakeupHandlers (xf86RotateBlockHandler,
					  xf86RotateWakeupHandler,
					  (pointer) pScreen);
	}
    }
    else
    {
	int	    width = mode_width (mode, rotation);
	int	    height = mode_height (mode, rotation);
	PixmapPtr   shadow = crtc->rotatedPixmap;
	int	    old_width = shadow ? shadow->drawable.width : 0;
	int	    old_height = shadow ? shadow->drawable.height : 0;
	
	/* Allocate memory for rotation */
	if (old_width != width || old_height != height)
	{
	    if (shadow)
	    {
		crtc->funcs->shadow_destroy (crtc, shadow);
		crtc->rotatedPixmap = NULL;
	    }
	    shadow = crtc->funcs->shadow_create (crtc, width, height);
	    if (!shadow)
		goto bail1;
	}
	
	if (!xf86_config->rotationDamage)
	{
	    /* Create damage structure */
	    xf86_config->rotationDamage = DamageCreate (NULL, NULL,
						DamageReportNone,
						TRUE, pScreen, pScreen);
	    if (!xf86_config->rotationDamage)
		goto bail2;
	    
	    /* Hook damage to screen pixmap */
	    DamageRegister (&(*pScreen->GetScreenPixmap)(pScreen)->drawable,
			    xf86_config->rotationDamage);
	    
	    /* Assign block/wakeup handler */
	    if (!RegisterBlockAndWakeupHandlers (xf86RotateBlockHandler,
						 xf86RotateWakeupHandler,
						 (pointer) pScreen))
	    {
		goto bail3;
	    }
	}
	if (0)
	{
bail3:
	    DamageDestroy (xf86_config->rotationDamage);
	    xf86_config->rotationDamage = NULL;
	    
bail2:
	    if (shadow)
	    {
		crtc->funcs->shadow_destroy (crtc, shadow);
		crtc->rotatedPixmap = NULL;
	    }
bail1:
	    if (old_width && old_height)
		crtc->rotatedPixmap = crtc->funcs->shadow_create (crtc,
								  old_width,
								  old_height);
	    return FALSE;
	}
    }
    
    /* All done */
    crtc->curRotation = rotation;
    return TRUE;
}