1 /*
2 * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23 #ifdef HAVE_XORG_CONFIG_H
24 #include <xorg-config.h>
25 #endif
26
27 #include "xf86.h"
28 #include "os.h"
29 #include "globals.h"
30 #include "xf86Modes.h"
31 #include "xf86Priv.h"
32 #include "xf86DDC.h"
33 #include "mipointer.h"
34 #include "windowstr.h"
35 #include "inputstr.h"
36 #include <randrstr.h>
37 #include <X11/extensions/render.h>
38
39 #include "xf86cmap.h"
40 #include "xf86Crtc.h"
41 #include "xf86RandR12.h"
42
43 typedef struct _xf86RandR12Info {
44 int virtualX;
45 int virtualY;
46 int mmWidth;
47 int mmHeight;
48 int maxX;
49 int maxY;
50 int pointerX;
51 int pointerY;
52 Rotation rotation; /* current mode */
53 Rotation supported_rotations; /* driver supported */
54
55 /* Compatibility with colormaps and XF86VidMode's gamma */
56 int palette_red_size;
57 int palette_green_size;
58 int palette_blue_size;
59 int palette_size;
60 LOCO *palette;
61
62 /* Used to wrap EnterVT so we can re-probe the outputs when a laptop unsuspends
63 * (actually, any time that we switch back into our VT).
64 *
65 * See https://bugs.freedesktop.org/show_bug.cgi?id=21554
66 */
67 xf86EnterVTProc *orig_EnterVT;
68
69 Bool panning;
70 ConstrainCursorHarderProcPtr orig_ConstrainCursorHarder;
71 } XF86RandRInfoRec, *XF86RandRInfoPtr;
72
73 #ifdef RANDR_12_INTERFACE
74 static Bool xf86RandR12Init12(ScreenPtr pScreen);
75 static Bool xf86RandR12CreateScreenResources12(ScreenPtr pScreen);
76 #endif
77
78 static int xf86RandR12Generation;
79
80 static DevPrivateKeyRec xf86RandR12KeyRec;
81 static DevPrivateKey xf86RandR12Key;
82
83 #define XF86RANDRINFO(p) ((XF86RandRInfoPtr) \
84 dixLookupPrivate(&(p)->devPrivates, xf86RandR12Key))
85
86 static int
xf86RandR12ModeRefresh(DisplayModePtr mode)87 xf86RandR12ModeRefresh(DisplayModePtr mode)
88 {
89 if (mode->VRefresh)
90 return (int) (mode->VRefresh + 0.5);
91 else
92 return (int) (mode->Clock * 1000.0 / mode->HTotal / mode->VTotal + 0.5);
93 }
94
95 /* Adapt panning area; return TRUE if panning area was valid without adaption */
96 static int
xf86RandR13VerifyPanningArea(xf86CrtcPtr crtc,int screenWidth,int screenHeight)97 xf86RandR13VerifyPanningArea(xf86CrtcPtr crtc, int screenWidth,
98 int screenHeight)
99 {
100 int ret = TRUE;
101
102 if (crtc->version < 2)
103 return FALSE;
104
105 if (crtc->panningTotalArea.x2 <= crtc->panningTotalArea.x1) {
106 /* Panning in X is disabled */
107 if (crtc->panningTotalArea.x1 || crtc->panningTotalArea.x2)
108 /* Illegal configuration -> fail/disable */
109 ret = FALSE;
110 crtc->panningTotalArea.x1 = crtc->panningTotalArea.x2 = 0;
111 crtc->panningTrackingArea.x1 = crtc->panningTrackingArea.x2 = 0;
112 crtc->panningBorder[0] = crtc->panningBorder[2] = 0;
113 }
114 else {
115 /* Panning in X is enabled */
116 if (crtc->panningTotalArea.x1 < 0) {
117 /* Panning region outside screen -> move inside */
118 crtc->panningTotalArea.x2 -= crtc->panningTotalArea.x1;
119 crtc->panningTotalArea.x1 = 0;
120 ret = FALSE;
121 }
122 if (crtc->panningTotalArea.x2 <
123 crtc->panningTotalArea.x1 + crtc->mode.HDisplay) {
124 /* Panning region smaller than displayed area -> crop to displayed area */
125 crtc->panningTotalArea.x2 =
126 crtc->panningTotalArea.x1 + crtc->mode.HDisplay;
127 ret = FALSE;
128 }
129 if (crtc->panningTotalArea.x2 > screenWidth) {
130 /* Panning region larger than screen -> move inside, then crop to screen */
131 crtc->panningTotalArea.x1 -=
132 crtc->panningTotalArea.x2 - screenWidth;
133 crtc->panningTotalArea.x2 = screenWidth;
134 ret = FALSE;
135 if (crtc->panningTotalArea.x1 < 0)
136 crtc->panningTotalArea.x1 = 0;
137 }
138 if (crtc->panningBorder[0] + crtc->panningBorder[2] >
139 crtc->mode.HDisplay) {
140 /* Borders too large -> set to 0 */
141 crtc->panningBorder[0] = crtc->panningBorder[2] = 0;
142 ret = FALSE;
143 }
144 }
145
146 if (crtc->panningTotalArea.y2 <= crtc->panningTotalArea.y1) {
147 /* Panning in Y is disabled */
148 if (crtc->panningTotalArea.y1 || crtc->panningTotalArea.y2)
149 /* Illegal configuration -> fail/disable */
150 ret = FALSE;
151 crtc->panningTotalArea.y1 = crtc->panningTotalArea.y2 = 0;
152 crtc->panningTrackingArea.y1 = crtc->panningTrackingArea.y2 = 0;
153 crtc->panningBorder[1] = crtc->panningBorder[3] = 0;
154 }
155 else {
156 /* Panning in Y is enabled */
157 if (crtc->panningTotalArea.y1 < 0) {
158 /* Panning region outside screen -> move inside */
159 crtc->panningTotalArea.y2 -= crtc->panningTotalArea.y1;
160 crtc->panningTotalArea.y1 = 0;
161 ret = FALSE;
162 }
163 if (crtc->panningTotalArea.y2 <
164 crtc->panningTotalArea.y1 + crtc->mode.VDisplay) {
165 /* Panning region smaller than displayed area -> crop to displayed area */
166 crtc->panningTotalArea.y2 =
167 crtc->panningTotalArea.y1 + crtc->mode.VDisplay;
168 ret = FALSE;
169 }
170 if (crtc->panningTotalArea.y2 > screenHeight) {
171 /* Panning region larger than screen -> move inside, then crop to screen */
172 crtc->panningTotalArea.y1 -=
173 crtc->panningTotalArea.y2 - screenHeight;
174 crtc->panningTotalArea.y2 = screenHeight;
175 ret = FALSE;
176 if (crtc->panningTotalArea.y1 < 0)
177 crtc->panningTotalArea.y1 = 0;
178 }
179 if (crtc->panningBorder[1] + crtc->panningBorder[3] >
180 crtc->mode.VDisplay) {
181 /* Borders too large -> set to 0 */
182 crtc->panningBorder[1] = crtc->panningBorder[3] = 0;
183 ret = FALSE;
184 }
185 }
186
187 return ret;
188 }
189
190 /*
191 * The heart of the panning operation:
192 *
193 * Given a frame buffer position (fb_x, fb_y),
194 * and a crtc position (crtc_x, crtc_y),
195 * and a transform matrix which maps frame buffer to crtc,
196 * compute a panning position (pan_x, pan_y) that
197 * makes the resulting transform line those two up
198 */
199
200 static void
xf86ComputeCrtcPan(Bool transform_in_use,struct pixman_f_transform * m,double screen_x,double screen_y,double crtc_x,double crtc_y,int old_pan_x,int old_pan_y,int * new_pan_x,int * new_pan_y)201 xf86ComputeCrtcPan(Bool transform_in_use,
202 struct pixman_f_transform *m,
203 double screen_x, double screen_y,
204 double crtc_x, double crtc_y,
205 int old_pan_x, int old_pan_y, int *new_pan_x, int *new_pan_y)
206 {
207 if (transform_in_use) {
208 /*
209 * Given the current transform, M, the current position
210 * on the Screen, S, and the desired position on the CRTC,
211 * C, compute a translation, T, such that:
212 *
213 * M T S = C
214 *
215 * where T is of the form
216 *
217 * | 1 0 dx |
218 * | 0 1 dy |
219 * | 0 0 1 |
220 *
221 * M T S =
222 * | M00 Sx + M01 Sy + M00 dx + M01 dy + M02 | | Cx F |
223 * | M10 Sx + M11 Sy + M10 dx + M11 dy + M12 | = | Cy F |
224 * | M20 Sx + M21 Sy + M20 dx + M21 dy + M22 | | F |
225 *
226 * R = M S
227 *
228 * Cx F = M00 dx + M01 dy + R0
229 * Cy F = M10 dx + M11 dy + R1
230 * F = M20 dx + M21 dy + R2
231 *
232 * Zero out dx, then dy
233 *
234 * F (Cx M10 - Cy M00) =
235 * (M10 M01 - M00 M11) dy + M10 R0 - M00 R1
236 * F (M10 - Cy M20) =
237 * (M10 M21 - M20 M11) dy + M10 R2 - M20 R1
238 *
239 * F (Cx M11 - Cy M01) =
240 * (M11 M00 - M01 M10) dx + M11 R0 - M01 R1
241 * F (M11 - Cy M21) =
242 * (M11 M20 - M21 M10) dx + M11 R2 - M21 R1
243 *
244 * Make some temporaries
245 *
246 * T = | Cx M10 - Cy M00 |
247 * | Cx M11 - Cy M01 |
248 *
249 * U = | M10 M01 - M00 M11 |
250 * | M11 M00 - M01 M10 |
251 *
252 * Q = | M10 R0 - M00 R1 |
253 * | M11 R0 - M01 R1 |
254 *
255 * P = | M10 - Cy M20 |
256 * | M11 - Cy M21 |
257 *
258 * W = | M10 M21 - M20 M11 |
259 * | M11 M20 - M21 M10 |
260 *
261 * V = | M10 R2 - M20 R1 |
262 * | M11 R2 - M21 R1 |
263 *
264 * Rewrite:
265 *
266 * F T0 = U0 dy + Q0
267 * F P0 = W0 dy + V0
268 * F T1 = U1 dx + Q1
269 * F P1 = W1 dx + V1
270 *
271 * Solve for F (two ways)
272 *
273 * F (W0 T0 - U0 P0) = W0 Q0 - U0 V0
274 *
275 * W0 Q0 - U0 V0
276 * F = -------------
277 * W0 T0 - U0 P0
278 *
279 * F (W1 T1 - U1 P1) = W1 Q1 - U1 V1
280 *
281 * W1 Q1 - U1 V1
282 * F = -------------
283 * W1 T1 - U1 P1
284 *
285 * We'll use which ever solution works (denominator != 0)
286 *
287 * Finally, solve for dx and dy:
288 *
289 * dx = (F T1 - Q1) / U1
290 * dx = (F P1 - V1) / W1
291 *
292 * dy = (F T0 - Q0) / U0
293 * dy = (F P0 - V0) / W0
294 */
295 double r[3];
296 double q[2], u[2], t[2], v[2], w[2], p[2];
297 double f;
298 struct pict_f_vector d;
299 int i;
300
301 /* Get the un-normalized crtc coordinates again */
302 for (i = 0; i < 3; i++)
303 r[i] = m->m[i][0] * screen_x + m->m[i][1] * screen_y + m->m[i][2];
304
305 /* Combine values into temporaries */
306 for (i = 0; i < 2; i++) {
307 q[i] = m->m[1][i] * r[0] - m->m[0][i] * r[1];
308 u[i] = m->m[1][i] * m->m[0][1 - i] - m->m[0][i] * m->m[1][1 - i];
309 t[i] = m->m[1][i] * crtc_x - m->m[0][i] * crtc_y;
310
311 v[i] = m->m[1][i] * r[2] - m->m[2][i] * r[1];
312 w[i] = m->m[1][i] * m->m[2][1 - i] - m->m[2][i] * m->m[1][1 - i];
313 p[i] = m->m[1][i] - m->m[2][i] * crtc_y;
314 }
315
316 /* Find a way to compute f */
317 f = 0;
318 for (i = 0; i < 2; i++) {
319 double a = w[i] * q[i] - u[i] * v[i];
320 double b = w[i] * t[i] - u[i] * p[i];
321
322 if (b != 0) {
323 f = a / b;
324 break;
325 }
326 }
327
328 /* Solve for the resulting transform vector */
329 for (i = 0; i < 2; i++) {
330 if (u[i])
331 d.v[1 - i] = (t[i] * f - q[i]) / u[i];
332 else if (w[1])
333 d.v[1 - i] = (p[i] * f - v[i]) / w[i];
334 else
335 d.v[1 - i] = 0;
336 }
337 *new_pan_x = old_pan_x - floor(d.v[0] + 0.5);
338 *new_pan_y = old_pan_y - floor(d.v[1] + 0.5);
339 }
340 else {
341 *new_pan_x = screen_x - crtc_x;
342 *new_pan_y = screen_y - crtc_y;
343 }
344 }
345
346 static void
xf86RandR13Pan(xf86CrtcPtr crtc,int x,int y)347 xf86RandR13Pan(xf86CrtcPtr crtc, int x, int y)
348 {
349 int newX, newY;
350 int width, height;
351 Bool panned = FALSE;
352
353 if (crtc->version < 2)
354 return;
355
356 if (!crtc->enabled ||
357 (crtc->panningTotalArea.x2 <= crtc->panningTotalArea.x1 &&
358 crtc->panningTotalArea.y2 <= crtc->panningTotalArea.y1))
359 return;
360
361 newX = crtc->x;
362 newY = crtc->y;
363 width = crtc->mode.HDisplay;
364 height = crtc->mode.VDisplay;
365
366 if ((crtc->panningTrackingArea.x2 <= crtc->panningTrackingArea.x1 ||
367 (x >= crtc->panningTrackingArea.x1 &&
368 x < crtc->panningTrackingArea.x2)) &&
369 (crtc->panningTrackingArea.y2 <= crtc->panningTrackingArea.y1 ||
370 (y >= crtc->panningTrackingArea.y1 &&
371 y < crtc->panningTrackingArea.y2))) {
372 struct pict_f_vector c;
373
374 /*
375 * Pre-clip the mouse position to the panning area so that we don't
376 * push the crtc outside. This doesn't deal with changes to the
377 * panning values, only mouse position changes.
378 */
379 if (crtc->panningTotalArea.x2 > crtc->panningTotalArea.x1) {
380 if (x < crtc->panningTotalArea.x1)
381 x = crtc->panningTotalArea.x1;
382 if (x >= crtc->panningTotalArea.x2)
383 x = crtc->panningTotalArea.x2 - 1;
384 }
385 if (crtc->panningTotalArea.y2 > crtc->panningTotalArea.y1) {
386 if (y < crtc->panningTotalArea.y1)
387 y = crtc->panningTotalArea.y1;
388 if (y >= crtc->panningTotalArea.y2)
389 y = crtc->panningTotalArea.y2 - 1;
390 }
391
392 c.v[0] = x;
393 c.v[1] = y;
394 c.v[2] = 1.0;
395 if (crtc->transform_in_use) {
396 pixman_f_transform_point(&crtc->f_framebuffer_to_crtc, &c);
397 }
398 else {
399 c.v[0] -= crtc->x;
400 c.v[1] -= crtc->y;
401 }
402
403 if (crtc->panningTotalArea.x2 > crtc->panningTotalArea.x1) {
404 if (c.v[0] < crtc->panningBorder[0]) {
405 c.v[0] = crtc->panningBorder[0];
406 panned = TRUE;
407 }
408 if (c.v[0] >= width - crtc->panningBorder[2]) {
409 c.v[0] = width - crtc->panningBorder[2] - 1;
410 panned = TRUE;
411 }
412 }
413 if (crtc->panningTotalArea.y2 > crtc->panningTotalArea.y1) {
414 if (c.v[1] < crtc->panningBorder[1]) {
415 c.v[1] = crtc->panningBorder[1];
416 panned = TRUE;
417 }
418 if (c.v[1] >= height - crtc->panningBorder[3]) {
419 c.v[1] = height - crtc->panningBorder[3] - 1;
420 panned = TRUE;
421 }
422 }
423 if (panned)
424 xf86ComputeCrtcPan(crtc->transform_in_use,
425 &crtc->f_framebuffer_to_crtc,
426 x, y, c.v[0], c.v[1], newX, newY, &newX, &newY);
427 }
428
429 /*
430 * Ensure that the crtc is within the panning region.
431 *
432 * XXX This computation only works when we do not have a transform
433 * in use.
434 */
435 if (!crtc->transform_in_use) {
436 /* Validate against [xy]1 after [xy]2, to be sure that results are > 0 for [xy]1 > 0 */
437 if (crtc->panningTotalArea.x2 > crtc->panningTotalArea.x1) {
438 if (newX > crtc->panningTotalArea.x2 - width)
439 newX = crtc->panningTotalArea.x2 - width;
440 if (newX < crtc->panningTotalArea.x1)
441 newX = crtc->panningTotalArea.x1;
442 }
443 if (crtc->panningTotalArea.y2 > crtc->panningTotalArea.y1) {
444 if (newY > crtc->panningTotalArea.y2 - height)
445 newY = crtc->panningTotalArea.y2 - height;
446 if (newY < crtc->panningTotalArea.y1)
447 newY = crtc->panningTotalArea.y1;
448 }
449 }
450 if (newX != crtc->x || newY != crtc->y)
451 xf86CrtcSetOrigin(crtc, newX, newY);
452 }
453
454 static Bool
xf86RandR12GetInfo(ScreenPtr pScreen,Rotation * rotations)455 xf86RandR12GetInfo(ScreenPtr pScreen, Rotation * rotations)
456 {
457 RRScreenSizePtr pSize;
458 ScrnInfoPtr scrp = xf86ScreenToScrn(pScreen);
459 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
460 DisplayModePtr mode;
461 int maxX = 0, maxY = 0;
462
463 *rotations = randrp->supported_rotations;
464
465 if (randrp->virtualX == -1 || randrp->virtualY == -1) {
466 randrp->virtualX = scrp->virtualX;
467 randrp->virtualY = scrp->virtualY;
468 }
469
470 /* Re-probe the outputs for new monitors or modes */
471 if (scrp->vtSema) {
472 xf86ProbeOutputModes(scrp, 0, 0);
473 xf86SetScrnInfoModes(scrp);
474 }
475
476 for (mode = scrp->modes;; mode = mode->next) {
477 int refresh = xf86RandR12ModeRefresh(mode);
478
479 if (randrp->maxX == 0 || randrp->maxY == 0) {
480 if (maxX < mode->HDisplay)
481 maxX = mode->HDisplay;
482 if (maxY < mode->VDisplay)
483 maxY = mode->VDisplay;
484 }
485 pSize = RRRegisterSize(pScreen,
486 mode->HDisplay, mode->VDisplay,
487 randrp->mmWidth, randrp->mmHeight);
488 if (!pSize)
489 return FALSE;
490 RRRegisterRate(pScreen, pSize, refresh);
491
492 if (xf86ModesEqual(mode, scrp->currentMode)) {
493 RRSetCurrentConfig(pScreen, randrp->rotation, refresh, pSize);
494 }
495 if (mode->next == scrp->modes)
496 break;
497 }
498
499 if (randrp->maxX == 0 || randrp->maxY == 0) {
500 randrp->maxX = maxX;
501 randrp->maxY = maxY;
502 }
503
504 return TRUE;
505 }
506
507 static Bool
xf86RandR12SetMode(ScreenPtr pScreen,DisplayModePtr mode,Bool useVirtual,int mmWidth,int mmHeight)508 xf86RandR12SetMode(ScreenPtr pScreen,
509 DisplayModePtr mode,
510 Bool useVirtual, int mmWidth, int mmHeight)
511 {
512 ScrnInfoPtr scrp = xf86ScreenToScrn(pScreen);
513 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
514 int oldWidth = pScreen->width;
515 int oldHeight = pScreen->height;
516 int oldmmWidth = pScreen->mmWidth;
517 int oldmmHeight = pScreen->mmHeight;
518 WindowPtr pRoot = pScreen->root;
519 DisplayModePtr currentMode = NULL;
520 Bool ret = TRUE;
521
522 if (pRoot)
523 (*scrp->EnableDisableFBAccess) (scrp, FALSE);
524 if (useVirtual) {
525 scrp->virtualX = randrp->virtualX;
526 scrp->virtualY = randrp->virtualY;
527 }
528 else {
529 scrp->virtualX = mode->HDisplay;
530 scrp->virtualY = mode->VDisplay;
531 }
532
533 if (randrp->rotation & (RR_Rotate_90 | RR_Rotate_270)) {
534 /* If the screen is rotated 90 or 270 degrees, swap the sizes. */
535 pScreen->width = scrp->virtualY;
536 pScreen->height = scrp->virtualX;
537 pScreen->mmWidth = mmHeight;
538 pScreen->mmHeight = mmWidth;
539 }
540 else {
541 pScreen->width = scrp->virtualX;
542 pScreen->height = scrp->virtualY;
543 pScreen->mmWidth = mmWidth;
544 pScreen->mmHeight = mmHeight;
545 }
546 if (scrp->currentMode == mode) {
547 /* Save current mode */
548 currentMode = scrp->currentMode;
549 /* Reset, just so we ensure the drivers SwitchMode is called */
550 scrp->currentMode = NULL;
551 }
552 /*
553 * We know that if the driver failed to SwitchMode to the rotated
554 * version, then it should revert back to it's prior mode.
555 */
556 if (!xf86SwitchMode(pScreen, mode)) {
557 ret = FALSE;
558 scrp->virtualX = pScreen->width = oldWidth;
559 scrp->virtualY = pScreen->height = oldHeight;
560 pScreen->mmWidth = oldmmWidth;
561 pScreen->mmHeight = oldmmHeight;
562 scrp->currentMode = currentMode;
563 }
564
565 /*
566 * Make sure the layout is correct
567 */
568 xf86ReconfigureLayout();
569
570 /*
571 * Make sure the whole screen is visible
572 */
573 xf86SetViewport(pScreen, pScreen->width, pScreen->height);
574 xf86SetViewport(pScreen, 0, 0);
575 if (pRoot)
576 (*scrp->EnableDisableFBAccess) (scrp, TRUE);
577 return ret;
578 }
579
580 Bool
xf86RandR12SetConfig(ScreenPtr pScreen,Rotation rotation,int rate,RRScreenSizePtr pSize)581 xf86RandR12SetConfig(ScreenPtr pScreen,
582 Rotation rotation, int rate, RRScreenSizePtr pSize)
583 {
584 ScrnInfoPtr scrp = xf86ScreenToScrn(pScreen);
585 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
586 DisplayModePtr mode;
587 int pos[MAXDEVICES][2];
588 Bool useVirtual = FALSE;
589 int maxX = 0, maxY = 0;
590 Rotation oldRotation = randrp->rotation;
591 DeviceIntPtr dev;
592 Bool view_adjusted = FALSE;
593
594 randrp->rotation = rotation;
595
596 if (randrp->virtualX == -1 || randrp->virtualY == -1) {
597 randrp->virtualX = scrp->virtualX;
598 randrp->virtualY = scrp->virtualY;
599 }
600
601 for (dev = inputInfo.devices; dev; dev = dev->next) {
602 if (!IsMaster(dev) && !IsFloating(dev))
603 continue;
604
605 miPointerGetPosition(dev, &pos[dev->id][0], &pos[dev->id][1]);
606 }
607
608 for (mode = scrp->modes;; mode = mode->next) {
609 if (randrp->maxX == 0 || randrp->maxY == 0) {
610 if (maxX < mode->HDisplay)
611 maxX = mode->HDisplay;
612 if (maxY < mode->VDisplay)
613 maxY = mode->VDisplay;
614 }
615 if (mode->HDisplay == pSize->width &&
616 mode->VDisplay == pSize->height &&
617 (rate == 0 || xf86RandR12ModeRefresh(mode) == rate))
618 break;
619 if (mode->next == scrp->modes) {
620 if (pSize->width == randrp->virtualX &&
621 pSize->height == randrp->virtualY) {
622 mode = scrp->modes;
623 useVirtual = TRUE;
624 break;
625 }
626 if (randrp->maxX == 0 || randrp->maxY == 0) {
627 randrp->maxX = maxX;
628 randrp->maxY = maxY;
629 }
630 return FALSE;
631 }
632 }
633
634 if (randrp->maxX == 0 || randrp->maxY == 0) {
635 randrp->maxX = maxX;
636 randrp->maxY = maxY;
637 }
638
639 if (!xf86RandR12SetMode(pScreen, mode, useVirtual, pSize->mmWidth,
640 pSize->mmHeight)) {
641 randrp->rotation = oldRotation;
642 return FALSE;
643 }
644
645 /*
646 * Move the cursor back where it belongs; SwitchMode repositions it
647 * FIXME: duplicated code, see modes/xf86RandR12.c
648 */
649 for (dev = inputInfo.devices; dev; dev = dev->next) {
650 if (!IsMaster(dev) && !IsFloating(dev))
651 continue;
652
653 if (pScreen == miPointerGetScreen(dev)) {
654 int px = pos[dev->id][0];
655 int py = pos[dev->id][1];
656
657 px = (px >= pScreen->width ? (pScreen->width - 1) : px);
658 py = (py >= pScreen->height ? (pScreen->height - 1) : py);
659
660 /* Setting the viewpoint makes only sense on one device */
661 if (!view_adjusted && IsMaster(dev)) {
662 xf86SetViewport(pScreen, px, py);
663 view_adjusted = TRUE;
664 }
665
666 (*pScreen->SetCursorPosition) (dev, pScreen, px, py, FALSE);
667 }
668 }
669
670 return TRUE;
671 }
672
673 #define PANNING_ENABLED(crtc) \
674 ((crtc)->panningTotalArea.x2 > (crtc)->panningTotalArea.x1 || \
675 (crtc)->panningTotalArea.y2 > (crtc)->panningTotalArea.y1)
676
677 static Bool
xf86RandR12ScreenSetSize(ScreenPtr pScreen,CARD16 width,CARD16 height,CARD32 mmWidth,CARD32 mmHeight)678 xf86RandR12ScreenSetSize(ScreenPtr pScreen,
679 CARD16 width,
680 CARD16 height, CARD32 mmWidth, CARD32 mmHeight)
681 {
682 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
683 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
684 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
685 WindowPtr pRoot = pScreen->root;
686 PixmapPtr pScrnPix;
687 Bool ret = FALSE;
688 int c;
689
690 if (randrp->virtualX == -1 || randrp->virtualY == -1) {
691 randrp->virtualX = pScrn->virtualX;
692 randrp->virtualY = pScrn->virtualY;
693 }
694 if (pRoot && pScrn->vtSema)
695 (*pScrn->EnableDisableFBAccess) (pScrn, FALSE);
696
697 /* Let the driver update virtualX and virtualY */
698 if (!(*config->funcs->resize) (pScrn, width, height))
699 goto finish;
700
701 ret = TRUE;
702 /* Update panning information */
703 for (c = 0; c < config->num_crtc; c++) {
704 xf86CrtcPtr crtc = config->crtc[c];
705
706 if (PANNING_ENABLED (crtc)) {
707 if (crtc->panningTotalArea.x2 > crtc->panningTrackingArea.x1)
708 crtc->panningTotalArea.x2 += width - pScreen->width;
709 if (crtc->panningTotalArea.y2 > crtc->panningTrackingArea.y1)
710 crtc->panningTotalArea.y2 += height - pScreen->height;
711 if (crtc->panningTrackingArea.x2 > crtc->panningTrackingArea.x1)
712 crtc->panningTrackingArea.x2 += width - pScreen->width;
713 if (crtc->panningTrackingArea.y2 > crtc->panningTrackingArea.y1)
714 crtc->panningTrackingArea.y2 += height - pScreen->height;
715 xf86RandR13VerifyPanningArea(crtc, width, height);
716 xf86RandR13Pan(crtc, randrp->pointerX, randrp->pointerY);
717 }
718 }
719
720 pScrnPix = (*pScreen->GetScreenPixmap) (pScreen);
721 pScreen->width = pScrnPix->drawable.width = width;
722 pScreen->height = pScrnPix->drawable.height = height;
723 randrp->mmWidth = pScreen->mmWidth = mmWidth;
724 randrp->mmHeight = pScreen->mmHeight = mmHeight;
725
726 xf86SetViewport(pScreen, pScreen->width - 1, pScreen->height - 1);
727 xf86SetViewport(pScreen, 0, 0);
728
729 finish:
730 update_desktop_dimensions();
731
732 if (pRoot && pScrn->vtSema)
733 (*pScrn->EnableDisableFBAccess) (pScrn, TRUE);
734 #if RANDR_12_INTERFACE
735 if (pScreen->root && ret)
736 RRScreenSizeNotify(pScreen);
737 #endif
738 return ret;
739 }
740
741 Rotation
xf86RandR12GetRotation(ScreenPtr pScreen)742 xf86RandR12GetRotation(ScreenPtr pScreen)
743 {
744 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
745
746 return randrp->rotation;
747 }
748
749 Bool
xf86RandR12CreateScreenResources(ScreenPtr pScreen)750 xf86RandR12CreateScreenResources(ScreenPtr pScreen)
751 {
752 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
753 xf86CrtcConfigPtr config;
754 XF86RandRInfoPtr randrp;
755 int c;
756 int width, height;
757 int mmWidth, mmHeight;
758
759 #ifdef PANORAMIX
760 /* XXX disable RandR when using Xinerama */
761 if (!noPanoramiXExtension)
762 return TRUE;
763 #endif
764
765 config = XF86_CRTC_CONFIG_PTR(pScrn);
766 randrp = XF86RANDRINFO(pScreen);
767 /*
768 * Compute size of screen
769 */
770 width = 0;
771 height = 0;
772 for (c = 0; c < config->num_crtc; c++) {
773 xf86CrtcPtr crtc = config->crtc[c];
774 int crtc_width = crtc->x + xf86ModeWidth(&crtc->mode, crtc->rotation);
775 int crtc_height = crtc->y + xf86ModeHeight(&crtc->mode, crtc->rotation);
776
777 if (crtc->enabled) {
778 if (crtc_width > width)
779 width = crtc_width;
780 if (crtc_height > height)
781 height = crtc_height;
782 if (crtc->panningTotalArea.x2 > width)
783 width = crtc->panningTotalArea.x2;
784 if (crtc->panningTotalArea.y2 > height)
785 height = crtc->panningTotalArea.y2;
786 }
787 }
788
789 if (width && height) {
790 /*
791 * Compute physical size of screen
792 */
793 if (monitorResolution) {
794 mmWidth = width * 25.4 / monitorResolution;
795 mmHeight = height * 25.4 / monitorResolution;
796 }
797 else {
798 xf86OutputPtr output = xf86CompatOutput(pScrn);
799
800 if (output &&
801 output->conf_monitor &&
802 (output->conf_monitor->mon_width > 0 &&
803 output->conf_monitor->mon_height > 0)) {
804 /*
805 * Prefer user configured DisplaySize
806 */
807 mmWidth = output->conf_monitor->mon_width;
808 mmHeight = output->conf_monitor->mon_height;
809 }
810 else {
811 /*
812 * Otherwise, just set the screen to DEFAULT_DPI
813 */
814 mmWidth = width * 25.4 / DEFAULT_DPI;
815 mmHeight = height * 25.4 / DEFAULT_DPI;
816 }
817 }
818 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
819 "Setting screen physical size to %d x %d\n",
820 mmWidth, mmHeight);
821 /*
822 * This is the initial setting of the screen size.
823 * We have to pre-set it here, otherwise panning would be adapted
824 * to the new screen size.
825 */
826 pScreen->width = width;
827 pScreen->height = height;
828 xf86RandR12ScreenSetSize(pScreen, width, height, mmWidth, mmHeight);
829 }
830
831 if (randrp->virtualX == -1 || randrp->virtualY == -1) {
832 randrp->virtualX = pScrn->virtualX;
833 randrp->virtualY = pScrn->virtualY;
834 }
835 xf86CrtcSetScreenSubpixelOrder(pScreen);
836 #if RANDR_12_INTERFACE
837 if (xf86RandR12CreateScreenResources12(pScreen))
838 return TRUE;
839 #endif
840 return TRUE;
841 }
842
843 Bool
xf86RandR12Init(ScreenPtr pScreen)844 xf86RandR12Init(ScreenPtr pScreen)
845 {
846 rrScrPrivPtr rp;
847 XF86RandRInfoPtr randrp;
848
849 #ifdef PANORAMIX
850 /* XXX disable RandR when using Xinerama */
851 if (!noPanoramiXExtension) {
852 if (xf86NumScreens == 1)
853 noPanoramiXExtension = TRUE;
854 else
855 return TRUE;
856 }
857 #endif
858
859 if (xf86RandR12Generation != serverGeneration)
860 xf86RandR12Generation = serverGeneration;
861
862 xf86RandR12Key = &xf86RandR12KeyRec;
863 if (!dixRegisterPrivateKey(&xf86RandR12KeyRec, PRIVATE_SCREEN, 0))
864 return FALSE;
865
866 randrp = malloc(sizeof(XF86RandRInfoRec));
867 if (!randrp)
868 return FALSE;
869
870 if (!RRScreenInit(pScreen)) {
871 free(randrp);
872 return FALSE;
873 }
874 rp = rrGetScrPriv(pScreen);
875 rp->rrGetInfo = xf86RandR12GetInfo;
876 rp->rrSetConfig = xf86RandR12SetConfig;
877
878 randrp->virtualX = -1;
879 randrp->virtualY = -1;
880 randrp->mmWidth = pScreen->mmWidth;
881 randrp->mmHeight = pScreen->mmHeight;
882
883 randrp->rotation = RR_Rotate_0; /* initial rotated mode */
884
885 randrp->supported_rotations = RR_Rotate_0;
886
887 randrp->maxX = randrp->maxY = 0;
888
889 randrp->palette_size = 0;
890 randrp->palette = NULL;
891
892 dixSetPrivate(&pScreen->devPrivates, xf86RandR12Key, randrp);
893
894 #if RANDR_12_INTERFACE
895 if (!xf86RandR12Init12(pScreen))
896 return FALSE;
897 #endif
898 return TRUE;
899 }
900
901 void
xf86RandR12CloseScreen(ScreenPtr pScreen)902 xf86RandR12CloseScreen(ScreenPtr pScreen)
903 {
904 XF86RandRInfoPtr randrp;
905
906 if (xf86RandR12Key == NULL)
907 return;
908
909 randrp = XF86RANDRINFO(pScreen);
910 #if RANDR_12_INTERFACE
911 xf86ScreenToScrn(pScreen)->EnterVT = randrp->orig_EnterVT;
912 pScreen->ConstrainCursorHarder = randrp->orig_ConstrainCursorHarder;
913 #endif
914
915 free(randrp->palette);
916 free(randrp);
917 }
918
919 void
xf86RandR12SetRotations(ScreenPtr pScreen,Rotation rotations)920 xf86RandR12SetRotations(ScreenPtr pScreen, Rotation rotations)
921 {
922 XF86RandRInfoPtr randrp;
923
924 #if RANDR_12_INTERFACE
925 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
926 int c;
927 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
928 #endif
929
930 if (xf86RandR12Key == NULL)
931 return;
932
933 randrp = XF86RANDRINFO(pScreen);
934 #if RANDR_12_INTERFACE
935 for (c = 0; c < config->num_crtc; c++) {
936 xf86CrtcPtr crtc = config->crtc[c];
937
938 RRCrtcSetRotations(crtc->randr_crtc, rotations);
939 }
940 #endif
941 randrp->supported_rotations = rotations;
942 }
943
944 void
xf86RandR12SetTransformSupport(ScreenPtr pScreen,Bool transforms)945 xf86RandR12SetTransformSupport(ScreenPtr pScreen, Bool transforms)
946 {
947 #if RANDR_13_INTERFACE
948 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
949 int c;
950 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
951 #endif
952
953 if (xf86RandR12Key == NULL)
954 return;
955
956 #if RANDR_13_INTERFACE
957 for (c = 0; c < config->num_crtc; c++) {
958 xf86CrtcPtr crtc = config->crtc[c];
959
960 RRCrtcSetTransformSupport(crtc->randr_crtc, transforms);
961 }
962 #endif
963 }
964
965 void
xf86RandR12GetOriginalVirtualSize(ScrnInfoPtr pScrn,int * x,int * y)966 xf86RandR12GetOriginalVirtualSize(ScrnInfoPtr pScrn, int *x, int *y)
967 {
968 ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
969
970 if (xf86RandR12Generation != serverGeneration ||
971 XF86RANDRINFO(pScreen)->virtualX == -1) {
972 *x = pScrn->virtualX;
973 *y = pScrn->virtualY;
974 }
975 else {
976 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
977
978 *x = randrp->virtualX;
979 *y = randrp->virtualY;
980 }
981 }
982
983 #if RANDR_12_INTERFACE
984
985 #define FLAG_BITS (RR_HSyncPositive | \
986 RR_HSyncNegative | \
987 RR_VSyncPositive | \
988 RR_VSyncNegative | \
989 RR_Interlace | \
990 RR_DoubleScan | \
991 RR_CSync | \
992 RR_CSyncPositive | \
993 RR_CSyncNegative | \
994 RR_HSkewPresent | \
995 RR_BCast | \
996 RR_PixelMultiplex | \
997 RR_DoubleClock | \
998 RR_ClockDivideBy2)
999
1000 static Bool
xf86RandRModeMatches(RRModePtr randr_mode,DisplayModePtr mode)1001 xf86RandRModeMatches(RRModePtr randr_mode, DisplayModePtr mode)
1002 {
1003 #if 0
1004 if (match_name) {
1005 /* check for same name */
1006 int len = strlen(mode->name);
1007
1008 if (randr_mode->mode.nameLength != len)
1009 return FALSE;
1010 if (memcmp(randr_mode->name, mode->name, len) != 0)
1011 return FALSE;
1012 }
1013 #endif
1014
1015 /* check for same timings */
1016 if (randr_mode->mode.dotClock / 1000 != mode->Clock)
1017 return FALSE;
1018 if (randr_mode->mode.width != mode->HDisplay)
1019 return FALSE;
1020 if (randr_mode->mode.hSyncStart != mode->HSyncStart)
1021 return FALSE;
1022 if (randr_mode->mode.hSyncEnd != mode->HSyncEnd)
1023 return FALSE;
1024 if (randr_mode->mode.hTotal != mode->HTotal)
1025 return FALSE;
1026 if (randr_mode->mode.hSkew != mode->HSkew)
1027 return FALSE;
1028 if (randr_mode->mode.height != mode->VDisplay)
1029 return FALSE;
1030 if (randr_mode->mode.vSyncStart != mode->VSyncStart)
1031 return FALSE;
1032 if (randr_mode->mode.vSyncEnd != mode->VSyncEnd)
1033 return FALSE;
1034 if (randr_mode->mode.vTotal != mode->VTotal)
1035 return FALSE;
1036
1037 /* check for same flags (using only the XF86 valid flag bits) */
1038 if ((randr_mode->mode.modeFlags & FLAG_BITS) != (mode->Flags & FLAG_BITS))
1039 return FALSE;
1040
1041 /* everything matches */
1042 return TRUE;
1043 }
1044
1045 static Bool
xf86RandR12CrtcNotify(RRCrtcPtr randr_crtc)1046 xf86RandR12CrtcNotify(RRCrtcPtr randr_crtc)
1047 {
1048 ScreenPtr pScreen = randr_crtc->pScreen;
1049 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1050 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1051 RRModePtr randr_mode = NULL;
1052 int x;
1053 int y;
1054 Rotation rotation;
1055 int numOutputs;
1056 RROutputPtr *randr_outputs;
1057 RROutputPtr randr_output;
1058 xf86CrtcPtr crtc = randr_crtc->devPrivate;
1059 xf86OutputPtr output;
1060 int i, j;
1061 DisplayModePtr mode = &crtc->mode;
1062 Bool ret;
1063
1064 randr_outputs = xallocarray(config->num_output, sizeof(RROutputPtr));
1065 if (!randr_outputs)
1066 return FALSE;
1067 x = crtc->x;
1068 y = crtc->y;
1069 rotation = crtc->rotation;
1070 numOutputs = 0;
1071 randr_mode = NULL;
1072 for (i = 0; i < config->num_output; i++) {
1073 output = config->output[i];
1074 if (output->crtc == crtc) {
1075 randr_output = output->randr_output;
1076 randr_outputs[numOutputs++] = randr_output;
1077 /*
1078 * We make copies of modes, so pointer equality
1079 * isn't sufficient
1080 */
1081 for (j = 0; j < randr_output->numModes + randr_output->numUserModes;
1082 j++) {
1083 RRModePtr m =
1084 (j <
1085 randr_output->numModes ? randr_output->
1086 modes[j] : randr_output->userModes[j -
1087 randr_output->
1088 numModes]);
1089
1090 if (xf86RandRModeMatches(m, mode)) {
1091 randr_mode = m;
1092 break;
1093 }
1094 }
1095 }
1096 }
1097 ret = RRCrtcNotify(randr_crtc, randr_mode, x, y,
1098 rotation,
1099 crtc->transformPresent ? &crtc->transform : NULL,
1100 numOutputs, randr_outputs);
1101 free(randr_outputs);
1102 return ret;
1103 }
1104
1105 /*
1106 * Convert a RandR mode to a DisplayMode
1107 */
1108 static void
xf86RandRModeConvert(ScrnInfoPtr scrn,RRModePtr randr_mode,DisplayModePtr mode)1109 xf86RandRModeConvert(ScrnInfoPtr scrn,
1110 RRModePtr randr_mode, DisplayModePtr mode)
1111 {
1112 memset(mode, 0, sizeof(DisplayModeRec));
1113 mode->status = MODE_OK;
1114
1115 mode->Clock = randr_mode->mode.dotClock / 1000;
1116
1117 mode->HDisplay = randr_mode->mode.width;
1118 mode->HSyncStart = randr_mode->mode.hSyncStart;
1119 mode->HSyncEnd = randr_mode->mode.hSyncEnd;
1120 mode->HTotal = randr_mode->mode.hTotal;
1121 mode->HSkew = randr_mode->mode.hSkew;
1122
1123 mode->VDisplay = randr_mode->mode.height;
1124 mode->VSyncStart = randr_mode->mode.vSyncStart;
1125 mode->VSyncEnd = randr_mode->mode.vSyncEnd;
1126 mode->VTotal = randr_mode->mode.vTotal;
1127 mode->VScan = 0;
1128
1129 mode->Flags = randr_mode->mode.modeFlags & FLAG_BITS;
1130
1131 xf86SetModeCrtc(mode, scrn->adjustFlags);
1132 }
1133
1134 static Bool
xf86RandR12CrtcSet(ScreenPtr pScreen,RRCrtcPtr randr_crtc,RRModePtr randr_mode,int x,int y,Rotation rotation,int num_randr_outputs,RROutputPtr * randr_outputs)1135 xf86RandR12CrtcSet(ScreenPtr pScreen,
1136 RRCrtcPtr randr_crtc,
1137 RRModePtr randr_mode,
1138 int x,
1139 int y,
1140 Rotation rotation,
1141 int num_randr_outputs, RROutputPtr * randr_outputs)
1142 {
1143 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
1144 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1145 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1146 xf86CrtcPtr crtc = randr_crtc->devPrivate;
1147 RRTransformPtr transform;
1148 Bool changed = FALSE;
1149 int o, ro;
1150 xf86CrtcPtr *save_crtcs;
1151 Bool save_enabled = crtc->enabled;
1152
1153 if (!crtc->scrn->vtSema)
1154 return FALSE;
1155
1156 save_crtcs = xallocarray(config->num_output, sizeof(xf86CrtcPtr));
1157 if ((randr_mode != NULL) != crtc->enabled)
1158 changed = TRUE;
1159 else if (randr_mode && !xf86RandRModeMatches(randr_mode, &crtc->mode))
1160 changed = TRUE;
1161
1162 if (rotation != crtc->rotation)
1163 changed = TRUE;
1164
1165 if (crtc->current_scanout != randr_crtc->scanout_pixmap ||
1166 crtc->current_scanout_back != randr_crtc->scanout_pixmap_back)
1167 changed = TRUE;
1168
1169 transform = RRCrtcGetTransform(randr_crtc);
1170 if ((transform != NULL) != crtc->transformPresent)
1171 changed = TRUE;
1172 else if (transform &&
1173 !RRTransformEqual(transform, &crtc->transform))
1174 changed = TRUE;
1175
1176 if (x != crtc->x || y != crtc->y)
1177 changed = TRUE;
1178 for (o = 0; o < config->num_output; o++) {
1179 xf86OutputPtr output = config->output[o];
1180 xf86CrtcPtr new_crtc;
1181
1182 save_crtcs[o] = output->crtc;
1183
1184 if (output->crtc == crtc)
1185 new_crtc = NULL;
1186 else
1187 new_crtc = output->crtc;
1188 for (ro = 0; ro < num_randr_outputs; ro++)
1189 if (output->randr_output == randr_outputs[ro]) {
1190 new_crtc = crtc;
1191 break;
1192 }
1193 if (new_crtc != output->crtc) {
1194 changed = TRUE;
1195 output->crtc = new_crtc;
1196 }
1197 }
1198 for (ro = 0; ro < num_randr_outputs; ro++)
1199 if (randr_outputs[ro]->pendingProperties)
1200 changed = TRUE;
1201
1202 /* XXX need device-independent mode setting code through an API */
1203 if (changed) {
1204 crtc->enabled = randr_mode != NULL;
1205
1206 if (randr_mode) {
1207 DisplayModeRec mode;
1208
1209 xf86RandRModeConvert(pScrn, randr_mode, &mode);
1210 if (!xf86CrtcSetModeTransform
1211 (crtc, &mode, rotation, transform, x, y)) {
1212 crtc->enabled = save_enabled;
1213 for (o = 0; o < config->num_output; o++) {
1214 xf86OutputPtr output = config->output[o];
1215
1216 output->crtc = save_crtcs[o];
1217 }
1218 free(save_crtcs);
1219 return FALSE;
1220 }
1221 xf86RandR13VerifyPanningArea(crtc, pScreen->width, pScreen->height);
1222 xf86RandR13Pan(crtc, randrp->pointerX, randrp->pointerY);
1223 randrp->panning = PANNING_ENABLED (crtc);
1224 /*
1225 * Save the last successful setting for EnterVT
1226 */
1227 xf86SaveModeContents(&crtc->desiredMode, &mode);
1228 crtc->desiredRotation = rotation;
1229 crtc->current_scanout = randr_crtc->scanout_pixmap;
1230 crtc->current_scanout_back = randr_crtc->scanout_pixmap_back;
1231 if (transform) {
1232 crtc->desiredTransform = *transform;
1233 crtc->desiredTransformPresent = TRUE;
1234 }
1235 else
1236 crtc->desiredTransformPresent = FALSE;
1237
1238 crtc->desiredX = x;
1239 crtc->desiredY = y;
1240 }
1241 xf86DisableUnusedFunctions(pScrn);
1242
1243 /*
1244 * Make sure the layout is correct
1245 */
1246 xf86ReconfigureLayout();
1247 }
1248 free(save_crtcs);
1249 return xf86RandR12CrtcNotify(randr_crtc);
1250 }
1251
1252 static void
xf86RandR12CrtcComputeGamma(xf86CrtcPtr crtc,LOCO * palette,int palette_red_size,int palette_green_size,int palette_blue_size,CARD16 * gamma_red,CARD16 * gamma_green,CARD16 * gamma_blue,int gamma_size)1253 xf86RandR12CrtcComputeGamma(xf86CrtcPtr crtc, LOCO *palette,
1254 int palette_red_size, int palette_green_size,
1255 int palette_blue_size, CARD16 *gamma_red,
1256 CARD16 *gamma_green, CARD16 *gamma_blue,
1257 int gamma_size)
1258 {
1259 int gamma_slots;
1260 unsigned shift;
1261 CARD32 value;
1262 int i, j;
1263
1264 for (shift = 0; (gamma_size << shift) < (1 << 16); shift++);
1265
1266 if (crtc->gamma_size >= palette_red_size) {
1267 /* Upsampling of smaller palette to larger hw lut size */
1268 gamma_slots = crtc->gamma_size / palette_red_size;
1269 for (i = 0; i < palette_red_size; i++) {
1270 value = palette[i].red;
1271 if (gamma_red)
1272 value = gamma_red[value];
1273 else
1274 value <<= shift;
1275
1276 for (j = 0; j < gamma_slots; j++)
1277 crtc->gamma_red[i * gamma_slots + j] = value;
1278 }
1279 } else {
1280 /* Downsampling of larger palette to smaller hw lut size */
1281 for (i = 0; i < crtc->gamma_size; i++) {
1282 value = palette[i * (palette_red_size - 1) / (crtc->gamma_size - 1)].red;
1283 if (gamma_red)
1284 value = gamma_red[value];
1285 else
1286 value <<= shift;
1287
1288 crtc->gamma_red[i] = value;
1289 }
1290 }
1291
1292 if (crtc->gamma_size >= palette_green_size) {
1293 /* Upsampling of smaller palette to larger hw lut size */
1294 gamma_slots = crtc->gamma_size / palette_green_size;
1295 for (i = 0; i < palette_green_size; i++) {
1296 value = palette[i].green;
1297 if (gamma_green)
1298 value = gamma_green[value];
1299 else
1300 value <<= shift;
1301
1302 for (j = 0; j < gamma_slots; j++)
1303 crtc->gamma_green[i * gamma_slots + j] = value;
1304 }
1305 } else {
1306 /* Downsampling of larger palette to smaller hw lut size */
1307 for (i = 0; i < crtc->gamma_size; i++) {
1308 value = palette[i * (palette_green_size - 1) / (crtc->gamma_size - 1)].green;
1309 if (gamma_green)
1310 value = gamma_green[value];
1311 else
1312 value <<= shift;
1313
1314 crtc->gamma_green[i] = value;
1315 }
1316 }
1317
1318 if (crtc->gamma_size >= palette_blue_size) {
1319 /* Upsampling of smaller palette to larger hw lut size */
1320 gamma_slots = crtc->gamma_size / palette_blue_size;
1321 for (i = 0; i < palette_blue_size; i++) {
1322 value = palette[i].blue;
1323 if (gamma_blue)
1324 value = gamma_blue[value];
1325 else
1326 value <<= shift;
1327
1328 for (j = 0; j < gamma_slots; j++)
1329 crtc->gamma_blue[i * gamma_slots + j] = value;
1330 }
1331 } else {
1332 /* Downsampling of larger palette to smaller hw lut size */
1333 for (i = 0; i < crtc->gamma_size; i++) {
1334 value = palette[i * (palette_blue_size - 1) / (crtc->gamma_size - 1)].blue;
1335 if (gamma_blue)
1336 value = gamma_blue[value];
1337 else
1338 value <<= shift;
1339
1340 crtc->gamma_blue[i] = value;
1341 }
1342 }
1343 }
1344
1345 static void
xf86RandR12CrtcReloadGamma(xf86CrtcPtr crtc)1346 xf86RandR12CrtcReloadGamma(xf86CrtcPtr crtc)
1347 {
1348 if (!crtc->scrn->vtSema || !crtc->funcs->gamma_set)
1349 return;
1350
1351 /* Only set it when the crtc is actually running.
1352 * Otherwise it will be set when it's activated.
1353 */
1354 if (crtc->active)
1355 crtc->funcs->gamma_set(crtc, crtc->gamma_red, crtc->gamma_green,
1356 crtc->gamma_blue, crtc->gamma_size);
1357 }
1358
1359 static Bool
xf86RandR12CrtcSetGamma(ScreenPtr pScreen,RRCrtcPtr randr_crtc)1360 xf86RandR12CrtcSetGamma(ScreenPtr pScreen, RRCrtcPtr randr_crtc)
1361 {
1362 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
1363 xf86CrtcPtr crtc = randr_crtc->devPrivate;
1364
1365 if (crtc->funcs->gamma_set == NULL)
1366 return FALSE;
1367
1368 if (randrp->palette_size) {
1369 xf86RandR12CrtcComputeGamma(crtc, randrp->palette,
1370 randrp->palette_red_size,
1371 randrp->palette_green_size,
1372 randrp->palette_blue_size,
1373 randr_crtc->gammaRed,
1374 randr_crtc->gammaGreen,
1375 randr_crtc->gammaBlue,
1376 randr_crtc->gammaSize);
1377 } else {
1378 memcpy(crtc->gamma_red, randr_crtc->gammaRed,
1379 crtc->gamma_size * sizeof(crtc->gamma_red[0]));
1380 memcpy(crtc->gamma_green, randr_crtc->gammaGreen,
1381 crtc->gamma_size * sizeof(crtc->gamma_green[0]));
1382 memcpy(crtc->gamma_blue, randr_crtc->gammaBlue,
1383 crtc->gamma_size * sizeof(crtc->gamma_blue[0]));
1384 }
1385
1386 xf86RandR12CrtcReloadGamma(crtc);
1387
1388 return TRUE;
1389 }
1390
1391 static void
init_one_component(CARD16 * comp,unsigned size,float gamma)1392 init_one_component(CARD16 *comp, unsigned size, float gamma)
1393 {
1394 int i;
1395 unsigned shift;
1396
1397 for (shift = 0; (size << shift) < (1 << 16); shift++);
1398
1399 if (gamma == 1.0) {
1400 for (i = 0; i < size; i++)
1401 comp[i] = i << shift;
1402 } else {
1403 for (i = 0; i < size; i++)
1404 comp[i] = (CARD16) (pow((double) i / (double) (size - 1),
1405 1. / (double) gamma) *
1406 (double) (size - 1) * (1 << shift));
1407 }
1408 }
1409
1410 static Bool
xf86RandR12CrtcInitGamma(xf86CrtcPtr crtc,float gamma_red,float gamma_green,float gamma_blue)1411 xf86RandR12CrtcInitGamma(xf86CrtcPtr crtc, float gamma_red, float gamma_green,
1412 float gamma_blue)
1413 {
1414 unsigned size = crtc->randr_crtc->gammaSize;
1415 CARD16 *red, *green, *blue;
1416
1417 if (!crtc->funcs->gamma_set &&
1418 (gamma_red != 1.0f || gamma_green != 1.0f || gamma_blue != 1.0f))
1419 return FALSE;
1420
1421 red = xallocarray(size, 3 * sizeof(CARD16));
1422 if (!red)
1423 return FALSE;
1424
1425 green = red + size;
1426 blue = green + size;
1427
1428 init_one_component(red, size, gamma_red);
1429 init_one_component(green, size, gamma_green);
1430 init_one_component(blue, size, gamma_blue);
1431
1432 RRCrtcGammaSet(crtc->randr_crtc, red, green, blue);
1433 free(red);
1434
1435 return TRUE;
1436 }
1437
1438 static Bool
xf86RandR12OutputInitGamma(xf86OutputPtr output)1439 xf86RandR12OutputInitGamma(xf86OutputPtr output)
1440 {
1441 XF86ConfMonitorPtr mon = output->conf_monitor;
1442 float gamma_red = 1.0, gamma_green = 1.0, gamma_blue = 1.0;
1443
1444 if (!mon)
1445 return TRUE;
1446
1447 /* Get configured values, where they exist. */
1448 if (mon->mon_gamma_red >= GAMMA_MIN && mon->mon_gamma_red <= GAMMA_MAX)
1449 gamma_red = mon->mon_gamma_red;
1450
1451 if (mon->mon_gamma_green >= GAMMA_MIN && mon->mon_gamma_green <= GAMMA_MAX)
1452 gamma_green = mon->mon_gamma_green;
1453
1454 if (mon->mon_gamma_blue >= GAMMA_MIN && mon->mon_gamma_blue <= GAMMA_MAX)
1455 gamma_blue = mon->mon_gamma_blue;
1456
1457 /* Don't set gamma 1.0 if another cloned output on this CRTC already set a
1458 * different gamma
1459 */
1460 if (gamma_red != 1.0 || gamma_green != 1.0 || gamma_blue != 1.0) {
1461 if (!output->crtc->randr_crtc) {
1462 xf86DrvMsg(output->scrn->scrnIndex, X_WARNING,
1463 "Gamma correction for output %s not possible because "
1464 "RandR is disabled\n", output->name);
1465 return TRUE;
1466 }
1467
1468 xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
1469 "Output %s wants gamma correction (%.1f, %.1f, %.1f)\n",
1470 output->name, gamma_red, gamma_green, gamma_blue);
1471 return xf86RandR12CrtcInitGamma(output->crtc, gamma_red, gamma_green,
1472 gamma_blue);
1473 }
1474
1475 return TRUE;
1476 }
1477
1478 Bool
xf86RandR12InitGamma(ScrnInfoPtr pScrn,unsigned gammaSize)1479 xf86RandR12InitGamma(ScrnInfoPtr pScrn, unsigned gammaSize) {
1480 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1481 int o, c;
1482
1483 /* Set default gamma for all CRTCs
1484 * This is done to avoid problems later on with cloned outputs
1485 */
1486 for (c = 0; c < config->num_crtc; c++) {
1487 xf86CrtcPtr crtc = config->crtc[c];
1488
1489 if (!crtc->randr_crtc)
1490 continue;
1491
1492 if (!RRCrtcGammaSetSize(crtc->randr_crtc, gammaSize) ||
1493 !xf86RandR12CrtcInitGamma(crtc, 1.0f, 1.0f, 1.0f))
1494 return FALSE;
1495 }
1496
1497 /* Set initial gamma per monitor configuration
1498 */
1499 for (o = 0; o < config->num_output; o++) {
1500 xf86OutputPtr output = config->output[o];
1501
1502 if (output->crtc &&
1503 !xf86RandR12OutputInitGamma(output))
1504 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1505 "Initial gamma correction for output %s: failed.\n",
1506 output->name);
1507 }
1508
1509 return TRUE;
1510 }
1511
1512 static Bool
xf86RandR12OutputSetProperty(ScreenPtr pScreen,RROutputPtr randr_output,Atom property,RRPropertyValuePtr value)1513 xf86RandR12OutputSetProperty(ScreenPtr pScreen,
1514 RROutputPtr randr_output,
1515 Atom property, RRPropertyValuePtr value)
1516 {
1517 xf86OutputPtr output = randr_output->devPrivate;
1518
1519 /* If we don't have any property handler, then we don't care what the
1520 * user is setting properties to.
1521 */
1522 if (output->funcs->set_property == NULL)
1523 return TRUE;
1524
1525 /*
1526 * This function gets called even when vtSema is FALSE, as
1527 * drivers will need to remember the correct value to apply
1528 * when the VT switch occurs
1529 */
1530 return output->funcs->set_property(output, property, value);
1531 }
1532
1533 static Bool
xf86RandR13OutputGetProperty(ScreenPtr pScreen,RROutputPtr randr_output,Atom property)1534 xf86RandR13OutputGetProperty(ScreenPtr pScreen,
1535 RROutputPtr randr_output, Atom property)
1536 {
1537 xf86OutputPtr output = randr_output->devPrivate;
1538
1539 if (output->funcs->get_property == NULL)
1540 return TRUE;
1541
1542 /* Should be safe even w/o vtSema */
1543 return output->funcs->get_property(output, property);
1544 }
1545
1546 static Bool
xf86RandR12OutputValidateMode(ScreenPtr pScreen,RROutputPtr randr_output,RRModePtr randr_mode)1547 xf86RandR12OutputValidateMode(ScreenPtr pScreen,
1548 RROutputPtr randr_output, RRModePtr randr_mode)
1549 {
1550 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1551 xf86OutputPtr output = randr_output->devPrivate;
1552 DisplayModeRec mode;
1553
1554 xf86RandRModeConvert(pScrn, randr_mode, &mode);
1555 /*
1556 * This function may be called when vtSema is FALSE, so
1557 * the underlying function must either avoid touching the hardware
1558 * or return FALSE when vtSema is FALSE
1559 */
1560 if (output->funcs->mode_valid(output, &mode) != MODE_OK)
1561 return FALSE;
1562 return TRUE;
1563 }
1564
1565 static void
xf86RandR12ModeDestroy(ScreenPtr pScreen,RRModePtr randr_mode)1566 xf86RandR12ModeDestroy(ScreenPtr pScreen, RRModePtr randr_mode)
1567 {
1568 }
1569
1570 /**
1571 * Given a list of xf86 modes and a RandR Output object, construct
1572 * RandR modes and assign them to the output
1573 */
1574 static Bool
xf86RROutputSetModes(RROutputPtr randr_output,DisplayModePtr modes)1575 xf86RROutputSetModes(RROutputPtr randr_output, DisplayModePtr modes)
1576 {
1577 DisplayModePtr mode;
1578 RRModePtr *rrmodes = NULL;
1579 int nmode = 0;
1580 int npreferred = 0;
1581 Bool ret = TRUE;
1582 int pref;
1583
1584 for (mode = modes; mode; mode = mode->next)
1585 nmode++;
1586
1587 if (nmode) {
1588 rrmodes = xallocarray(nmode, sizeof(RRModePtr));
1589
1590 if (!rrmodes)
1591 return FALSE;
1592 nmode = 0;
1593
1594 for (pref = 1; pref >= 0; pref--) {
1595 for (mode = modes; mode; mode = mode->next) {
1596 if ((pref != 0) == ((mode->type & M_T_PREFERRED) != 0)) {
1597 xRRModeInfo modeInfo;
1598 RRModePtr rrmode;
1599
1600 modeInfo.nameLength = strlen(mode->name);
1601 modeInfo.width = mode->HDisplay;
1602 modeInfo.dotClock = mode->Clock * 1000;
1603 modeInfo.hSyncStart = mode->HSyncStart;
1604 modeInfo.hSyncEnd = mode->HSyncEnd;
1605 modeInfo.hTotal = mode->HTotal;
1606 modeInfo.hSkew = mode->HSkew;
1607
1608 modeInfo.height = mode->VDisplay;
1609 modeInfo.vSyncStart = mode->VSyncStart;
1610 modeInfo.vSyncEnd = mode->VSyncEnd;
1611 modeInfo.vTotal = mode->VTotal;
1612 modeInfo.modeFlags = mode->Flags;
1613
1614 rrmode = RRModeGet(&modeInfo, mode->name);
1615 if (rrmode) {
1616 rrmodes[nmode++] = rrmode;
1617 npreferred += pref;
1618 }
1619 }
1620 }
1621 }
1622 }
1623
1624 ret = RROutputSetModes(randr_output, rrmodes, nmode, npreferred);
1625 free(rrmodes);
1626 return ret;
1627 }
1628
1629 /*
1630 * Mirror the current mode configuration to RandR
1631 */
1632 static Bool
xf86RandR12SetInfo12(ScreenPtr pScreen)1633 xf86RandR12SetInfo12(ScreenPtr pScreen)
1634 {
1635 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1636 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1637 RROutputPtr *clones;
1638 RRCrtcPtr *crtcs;
1639 int ncrtc;
1640 int o, c, l;
1641 int nclone;
1642
1643 clones = xallocarray(config->num_output, sizeof(RROutputPtr));
1644 crtcs = xallocarray(config->num_crtc, sizeof(RRCrtcPtr));
1645 for (o = 0; o < config->num_output; o++) {
1646 xf86OutputPtr output = config->output[o];
1647
1648 ncrtc = 0;
1649 for (c = 0; c < config->num_crtc; c++)
1650 if (output->possible_crtcs & (1 << c))
1651 crtcs[ncrtc++] = config->crtc[c]->randr_crtc;
1652
1653 if (!RROutputSetCrtcs(output->randr_output, crtcs, ncrtc)) {
1654 free(crtcs);
1655 free(clones);
1656 return FALSE;
1657 }
1658
1659 RROutputSetPhysicalSize(output->randr_output,
1660 output->mm_width, output->mm_height);
1661 xf86RROutputSetModes(output->randr_output, output->probed_modes);
1662
1663 switch (output->status) {
1664 case XF86OutputStatusConnected:
1665 RROutputSetConnection(output->randr_output, RR_Connected);
1666 break;
1667 case XF86OutputStatusDisconnected:
1668 RROutputSetConnection(output->randr_output, RR_Disconnected);
1669 break;
1670 case XF86OutputStatusUnknown:
1671 RROutputSetConnection(output->randr_output, RR_UnknownConnection);
1672 break;
1673 }
1674
1675 RROutputSetSubpixelOrder(output->randr_output, output->subpixel_order);
1676
1677 /*
1678 * Valid clones
1679 */
1680 nclone = 0;
1681 for (l = 0; l < config->num_output; l++) {
1682 xf86OutputPtr clone = config->output[l];
1683
1684 if (l != o && (output->possible_clones & (1 << l)))
1685 clones[nclone++] = clone->randr_output;
1686 }
1687 if (!RROutputSetClones(output->randr_output, clones, nclone)) {
1688 free(crtcs);
1689 free(clones);
1690 return FALSE;
1691 }
1692 }
1693 free(crtcs);
1694 free(clones);
1695 return TRUE;
1696 }
1697
1698 /*
1699 * Query the hardware for the current state, then mirror
1700 * that to RandR
1701 */
1702 static Bool
xf86RandR12GetInfo12(ScreenPtr pScreen,Rotation * rotations)1703 xf86RandR12GetInfo12(ScreenPtr pScreen, Rotation * rotations)
1704 {
1705 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1706
1707 if (!pScrn->vtSema)
1708 return TRUE;
1709 xf86ProbeOutputModes(pScrn, 0, 0);
1710 xf86SetScrnInfoModes(pScrn);
1711 return xf86RandR12SetInfo12(pScreen);
1712 }
1713
1714 static Bool
xf86RandR12CreateObjects12(ScreenPtr pScreen)1715 xf86RandR12CreateObjects12(ScreenPtr pScreen)
1716 {
1717 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1718 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1719 int c;
1720 int o;
1721
1722 if (!RRInit())
1723 return FALSE;
1724
1725 /*
1726 * Configure crtcs
1727 */
1728 for (c = 0; c < config->num_crtc; c++) {
1729 xf86CrtcPtr crtc = config->crtc[c];
1730
1731 crtc->randr_crtc = RRCrtcCreate(pScreen, crtc);
1732 }
1733 /*
1734 * Configure outputs
1735 */
1736 for (o = 0; o < config->num_output; o++) {
1737 xf86OutputPtr output = config->output[o];
1738
1739 output->randr_output = RROutputCreate(pScreen, output->name,
1740 strlen(output->name), output);
1741
1742 if (output->funcs->create_resources != NULL)
1743 output->funcs->create_resources(output);
1744 RRPostPendingProperties(output->randr_output);
1745 }
1746
1747 if (config->name) {
1748 config->randr_provider = RRProviderCreate(pScreen, config->name,
1749 strlen(config->name));
1750
1751 RRProviderSetCapabilities(config->randr_provider, pScrn->capabilities);
1752 }
1753
1754 return TRUE;
1755 }
1756
1757 static void
xf86RandR12CreateMonitors(ScreenPtr pScreen)1758 xf86RandR12CreateMonitors(ScreenPtr pScreen)
1759 {
1760 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1761 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1762 int o, ot;
1763 int ht, vt;
1764 int ret;
1765 char buf[25];
1766
1767 for (o = 0; o < config->num_output; o++) {
1768 xf86OutputPtr output = config->output[o];
1769 struct xf86CrtcTileInfo *tile_info = &output->tile_info, *this_tile;
1770 RRMonitorPtr monitor;
1771 int output_num, num_outputs;
1772 if (!tile_info->group_id)
1773 continue;
1774
1775 if (tile_info->tile_h_loc ||
1776 tile_info->tile_v_loc)
1777 continue;
1778
1779 num_outputs = tile_info->num_h_tile * tile_info->num_v_tile;
1780
1781 monitor = RRMonitorAlloc(num_outputs);
1782 if (!monitor)
1783 return;
1784 monitor->pScreen = pScreen;
1785 snprintf(buf, 25, "Auto-Monitor-%d", tile_info->group_id);
1786 monitor->name = MakeAtom(buf, strlen(buf), TRUE);
1787 monitor->primary = 0;
1788 monitor->automatic = TRUE;
1789 memset(&monitor->geometry.box, 0, sizeof(monitor->geometry.box));
1790
1791 output_num = 0;
1792 for (ht = 0; ht < tile_info->num_h_tile; ht++) {
1793 for (vt = 0; vt < tile_info->num_v_tile; vt++) {
1794
1795 for (ot = 0; ot < config->num_output; ot++) {
1796 this_tile = &config->output[ot]->tile_info;
1797
1798 if (this_tile->group_id != tile_info->group_id)
1799 continue;
1800
1801 if (this_tile->tile_h_loc != ht ||
1802 this_tile->tile_v_loc != vt)
1803 continue;
1804
1805 monitor->outputs[output_num] = config->output[ot]->randr_output->id;
1806 output_num++;
1807
1808 }
1809
1810 }
1811 }
1812
1813 ret = RRMonitorAdd(serverClient, pScreen, monitor);
1814 if (ret) {
1815 RRMonitorFree(monitor);
1816 return;
1817 }
1818 }
1819 }
1820
1821 static Bool
xf86RandR12CreateScreenResources12(ScreenPtr pScreen)1822 xf86RandR12CreateScreenResources12(ScreenPtr pScreen)
1823 {
1824 int c;
1825 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1826 rrScrPrivPtr rp = rrGetScrPriv(pScreen);
1827 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1828
1829 if (xf86RandR12Key == NULL)
1830 return TRUE;
1831
1832 for (c = 0; c < config->num_crtc; c++)
1833 xf86RandR12CrtcNotify(config->crtc[c]->randr_crtc);
1834
1835 RRScreenSetSizeRange(pScreen, config->minWidth, config->minHeight,
1836 config->maxWidth, config->maxHeight);
1837
1838 xf86RandR12CreateMonitors(pScreen);
1839
1840 if (!pScreen->isGPU) {
1841 rp->primaryOutput = config->output[0]->randr_output;
1842 RROutputChanged(rp->primaryOutput, FALSE);
1843 rp->layoutChanged = TRUE;
1844 }
1845
1846 return TRUE;
1847 }
1848
1849 /*
1850 * Something happened within the screen configuration due
1851 * to DGA, VidMode or hot key. Tell RandR
1852 */
1853
1854 void
xf86RandR12TellChanged(ScreenPtr pScreen)1855 xf86RandR12TellChanged(ScreenPtr pScreen)
1856 {
1857 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1858 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1859 int c;
1860
1861 if (xf86RandR12Key == NULL)
1862 return;
1863
1864 xf86RandR12SetInfo12(pScreen);
1865 for (c = 0; c < config->num_crtc; c++)
1866 xf86RandR12CrtcNotify(config->crtc[c]->randr_crtc);
1867
1868 RRTellChanged(pScreen);
1869 }
1870
1871 static void
xf86RandR12PointerMoved(ScrnInfoPtr pScrn,int x,int y)1872 xf86RandR12PointerMoved(ScrnInfoPtr pScrn, int x, int y)
1873 {
1874 ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
1875 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1876 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
1877 int c;
1878
1879 randrp->pointerX = x;
1880 randrp->pointerY = y;
1881 for (c = 0; c < config->num_crtc; c++)
1882 xf86RandR13Pan(config->crtc[c], x, y);
1883 }
1884
1885 static Bool
xf86RandR13GetPanning(ScreenPtr pScreen,RRCrtcPtr randr_crtc,BoxPtr totalArea,BoxPtr trackingArea,INT16 * border)1886 xf86RandR13GetPanning(ScreenPtr pScreen,
1887 RRCrtcPtr randr_crtc,
1888 BoxPtr totalArea, BoxPtr trackingArea, INT16 *border)
1889 {
1890 xf86CrtcPtr crtc = randr_crtc->devPrivate;
1891
1892 if (crtc->version < 2)
1893 return FALSE;
1894 if (totalArea)
1895 memcpy(totalArea, &crtc->panningTotalArea, sizeof(BoxRec));
1896 if (trackingArea)
1897 memcpy(trackingArea, &crtc->panningTrackingArea, sizeof(BoxRec));
1898 if (border)
1899 memcpy(border, crtc->panningBorder, 4 * sizeof(INT16));
1900
1901 return TRUE;
1902 }
1903
1904 static Bool
xf86RandR13SetPanning(ScreenPtr pScreen,RRCrtcPtr randr_crtc,BoxPtr totalArea,BoxPtr trackingArea,INT16 * border)1905 xf86RandR13SetPanning(ScreenPtr pScreen,
1906 RRCrtcPtr randr_crtc,
1907 BoxPtr totalArea, BoxPtr trackingArea, INT16 *border)
1908 {
1909 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
1910 xf86CrtcPtr crtc = randr_crtc->devPrivate;
1911 BoxRec oldTotalArea;
1912 BoxRec oldTrackingArea;
1913 INT16 oldBorder[4];
1914 Bool oldPanning = randrp->panning;
1915
1916 if (crtc->version < 2)
1917 return FALSE;
1918
1919 memcpy(&oldTotalArea, &crtc->panningTotalArea, sizeof(BoxRec));
1920 memcpy(&oldTrackingArea, &crtc->panningTrackingArea, sizeof(BoxRec));
1921 memcpy(oldBorder, crtc->panningBorder, 4 * sizeof(INT16));
1922
1923 if (totalArea)
1924 memcpy(&crtc->panningTotalArea, totalArea, sizeof(BoxRec));
1925 if (trackingArea)
1926 memcpy(&crtc->panningTrackingArea, trackingArea, sizeof(BoxRec));
1927 if (border)
1928 memcpy(crtc->panningBorder, border, 4 * sizeof(INT16));
1929
1930 if (xf86RandR13VerifyPanningArea(crtc, pScreen->width, pScreen->height)) {
1931 xf86RandR13Pan(crtc, randrp->pointerX, randrp->pointerY);
1932 randrp->panning = PANNING_ENABLED (crtc);
1933 return TRUE;
1934 }
1935 else {
1936 /* Restore old settings */
1937 memcpy(&crtc->panningTotalArea, &oldTotalArea, sizeof(BoxRec));
1938 memcpy(&crtc->panningTrackingArea, &oldTrackingArea, sizeof(BoxRec));
1939 memcpy(crtc->panningBorder, oldBorder, 4 * sizeof(INT16));
1940 randrp->panning = oldPanning;
1941 return FALSE;
1942 }
1943 }
1944
1945 /*
1946 * Compatibility with colormaps and XF86VidMode's gamma
1947 */
1948 void
xf86RandR12LoadPalette(ScrnInfoPtr pScrn,int numColors,int * indices,LOCO * colors,VisualPtr pVisual)1949 xf86RandR12LoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
1950 LOCO *colors, VisualPtr pVisual)
1951 {
1952 ScreenPtr pScreen = pScrn->pScreen;
1953 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1954 int reds, greens, blues, index, palette_size;
1955 int c, i;
1956
1957 if (pVisual->class == TrueColor || pVisual->class == DirectColor) {
1958 reds = (pVisual->redMask >> pVisual->offsetRed) + 1;
1959 greens = (pVisual->greenMask >> pVisual->offsetGreen) + 1;
1960 blues = (pVisual->blueMask >> pVisual->offsetBlue) + 1;
1961 } else {
1962 reds = greens = blues = pVisual->ColormapEntries;
1963 }
1964
1965 palette_size = max(reds, max(greens, blues));
1966
1967 if (dixPrivateKeyRegistered(rrPrivKey)) {
1968 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
1969
1970 if (randrp->palette_size != palette_size) {
1971 randrp->palette = reallocarray(randrp->palette, palette_size,
1972 sizeof(colors[0]));
1973 if (!randrp->palette) {
1974 randrp->palette_size = 0;
1975 return;
1976 }
1977
1978 randrp->palette_size = palette_size;
1979 }
1980 randrp->palette_red_size = reds;
1981 randrp->palette_green_size = greens;
1982 randrp->palette_blue_size = blues;
1983
1984 for (i = 0; i < numColors; i++) {
1985 index = indices[i];
1986
1987 if (index < reds)
1988 randrp->palette[index].red = colors[index].red;
1989 if (index < greens)
1990 randrp->palette[index].green = colors[index].green;
1991 if (index < blues)
1992 randrp->palette[index].blue = colors[index].blue;
1993 }
1994 }
1995
1996 for (c = 0; c < config->num_crtc; c++) {
1997 xf86CrtcPtr crtc = config->crtc[c];
1998 RRCrtcPtr randr_crtc = crtc->randr_crtc;
1999
2000 if (randr_crtc) {
2001 xf86RandR12CrtcComputeGamma(crtc, colors, reds, greens, blues,
2002 randr_crtc->gammaRed,
2003 randr_crtc->gammaGreen,
2004 randr_crtc->gammaBlue,
2005 randr_crtc->gammaSize);
2006 } else {
2007 xf86RandR12CrtcComputeGamma(crtc, colors, reds, greens, blues,
2008 NULL, NULL, NULL,
2009 xf86GetGammaRampSize(pScreen));
2010 }
2011 xf86RandR12CrtcReloadGamma(crtc);
2012 }
2013 }
2014
2015 /*
2016 * Compatibility pScrn->ChangeGamma provider for ddx drivers which do not call
2017 * xf86HandleColormaps(). Note such drivers really should be fixed to call
2018 * xf86HandleColormaps() as this clobbers the per-CRTC gamma ramp of the CRTC
2019 * assigned to the RandR compatibility output.
2020 */
2021 static int
xf86RandR12ChangeGamma(ScrnInfoPtr pScrn,Gamma gamma)2022 xf86RandR12ChangeGamma(ScrnInfoPtr pScrn, Gamma gamma)
2023 {
2024 RRCrtcPtr randr_crtc = xf86CompatRRCrtc(pScrn);
2025 int size;
2026
2027 if (!randr_crtc || pScrn->LoadPalette == xf86RandR12LoadPalette)
2028 return Success;
2029
2030 size = max(0, randr_crtc->gammaSize);
2031 if (!size)
2032 return Success;
2033
2034 init_one_component(randr_crtc->gammaRed, size, gamma.red);
2035 init_one_component(randr_crtc->gammaGreen, size, gamma.green);
2036 init_one_component(randr_crtc->gammaBlue, size, gamma.blue);
2037 xf86RandR12CrtcSetGamma(xf86ScrnToScreen(pScrn), randr_crtc);
2038
2039 pScrn->gamma = gamma;
2040
2041 return Success;
2042 }
2043
2044 static Bool
xf86RandR12EnterVT(ScrnInfoPtr pScrn)2045 xf86RandR12EnterVT(ScrnInfoPtr pScrn)
2046 {
2047 ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
2048 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
2049 rrScrPrivPtr rp = rrGetScrPriv(pScreen);
2050 Bool ret;
2051 int i;
2052
2053 if (randrp->orig_EnterVT) {
2054 pScrn->EnterVT = randrp->orig_EnterVT;
2055 ret = pScrn->EnterVT(pScrn);
2056 randrp->orig_EnterVT = pScrn->EnterVT;
2057 pScrn->EnterVT = xf86RandR12EnterVT;
2058 if (!ret)
2059 return FALSE;
2060 }
2061
2062 /* reload gamma */
2063 for (i = 0; i < rp->numCrtcs; i++)
2064 xf86RandR12CrtcReloadGamma(rp->crtcs[i]->devPrivate);
2065
2066 return RRGetInfo(pScreen, TRUE); /* force a re-probe of outputs and notify clients about changes */
2067 }
2068
2069 static void
xf86DetachOutputGPU(ScreenPtr pScreen)2070 xf86DetachOutputGPU(ScreenPtr pScreen)
2071 {
2072 rrScrPrivPtr rp = rrGetScrPriv(pScreen);
2073 int i;
2074
2075 /* make sure there are no attached shared scanout pixmaps first */
2076 for (i = 0; i < rp->numCrtcs; i++)
2077 RRCrtcDetachScanoutPixmap(rp->crtcs[i]);
2078
2079 DetachOutputGPU(pScreen);
2080 }
2081
2082 static Bool
xf86RandR14ProviderSetOutputSource(ScreenPtr pScreen,RRProviderPtr provider,RRProviderPtr source_provider)2083 xf86RandR14ProviderSetOutputSource(ScreenPtr pScreen,
2084 RRProviderPtr provider,
2085 RRProviderPtr source_provider)
2086 {
2087 if (!source_provider) {
2088 if (provider->output_source) {
2089 xf86DetachOutputGPU(pScreen);
2090 }
2091 provider->output_source = NULL;
2092 return TRUE;
2093 }
2094
2095 if (provider->output_source == source_provider)
2096 return TRUE;
2097
2098 SetRootClip(source_provider->pScreen, ROOT_CLIP_NONE);
2099
2100 AttachOutputGPU(source_provider->pScreen, pScreen);
2101
2102 provider->output_source = source_provider;
2103 SetRootClip(source_provider->pScreen, ROOT_CLIP_FULL);
2104 return TRUE;
2105 }
2106
2107 static Bool
xf86RandR14ProviderSetOffloadSink(ScreenPtr pScreen,RRProviderPtr provider,RRProviderPtr sink_provider)2108 xf86RandR14ProviderSetOffloadSink(ScreenPtr pScreen,
2109 RRProviderPtr provider,
2110 RRProviderPtr sink_provider)
2111 {
2112 if (!sink_provider) {
2113 if (provider->offload_sink) {
2114 xf86DetachOutputGPU(pScreen);
2115 }
2116
2117 provider->offload_sink = NULL;
2118 return TRUE;
2119 }
2120
2121 if (provider->offload_sink == sink_provider)
2122 return TRUE;
2123
2124 AttachOffloadGPU(sink_provider->pScreen, pScreen);
2125
2126 provider->offload_sink = sink_provider;
2127 return TRUE;
2128 }
2129
2130 static Bool
xf86RandR14ProviderSetProperty(ScreenPtr pScreen,RRProviderPtr randr_provider,Atom property,RRPropertyValuePtr value)2131 xf86RandR14ProviderSetProperty(ScreenPtr pScreen,
2132 RRProviderPtr randr_provider,
2133 Atom property, RRPropertyValuePtr value)
2134 {
2135 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
2136 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
2137
2138 /* If we don't have any property handler, then we don't care what the
2139 * user is setting properties to.
2140 */
2141 if (config->provider_funcs->set_property == NULL)
2142 return TRUE;
2143
2144 /*
2145 * This function gets called even when vtSema is FALSE, as
2146 * drivers will need to remember the correct value to apply
2147 * when the VT switch occurs
2148 */
2149 return config->provider_funcs->set_property(pScrn, property, value);
2150 }
2151
2152 static Bool
xf86RandR14ProviderGetProperty(ScreenPtr pScreen,RRProviderPtr randr_provider,Atom property)2153 xf86RandR14ProviderGetProperty(ScreenPtr pScreen,
2154 RRProviderPtr randr_provider, Atom property)
2155 {
2156 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
2157 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
2158
2159 if (config->provider_funcs->get_property == NULL)
2160 return TRUE;
2161
2162 /* Should be safe even w/o vtSema */
2163 return config->provider_funcs->get_property(pScrn, property);
2164 }
2165
2166 static Bool
xf86CrtcSetScanoutPixmap(RRCrtcPtr randr_crtc,PixmapPtr pixmap)2167 xf86CrtcSetScanoutPixmap(RRCrtcPtr randr_crtc, PixmapPtr pixmap)
2168 {
2169 xf86CrtcPtr crtc = randr_crtc->devPrivate;
2170 if (!crtc->funcs->set_scanout_pixmap)
2171 return FALSE;
2172 return crtc->funcs->set_scanout_pixmap(crtc, pixmap);
2173 }
2174
2175 static void
xf86RandR13ConstrainCursorHarder(DeviceIntPtr dev,ScreenPtr screen,int mode,int * x,int * y)2176 xf86RandR13ConstrainCursorHarder(DeviceIntPtr dev, ScreenPtr screen, int mode, int *x, int *y)
2177 {
2178 XF86RandRInfoPtr randrp = XF86RANDRINFO(screen);
2179
2180 if (randrp->panning)
2181 return;
2182
2183 if (randrp->orig_ConstrainCursorHarder) {
2184 screen->ConstrainCursorHarder = randrp->orig_ConstrainCursorHarder;
2185 screen->ConstrainCursorHarder(dev, screen, mode, x, y);
2186 screen->ConstrainCursorHarder = xf86RandR13ConstrainCursorHarder;
2187 }
2188 }
2189
2190 static void
xf86RandR14ProviderDestroy(ScreenPtr screen,RRProviderPtr provider)2191 xf86RandR14ProviderDestroy(ScreenPtr screen, RRProviderPtr provider)
2192 {
2193 ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
2194 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2195
2196 if (config->randr_provider == provider) {
2197 if (config->randr_provider->offload_sink) {
2198 DetachOffloadGPU(screen);
2199 config->randr_provider->offload_sink = NULL;
2200 RRSetChanged(screen);
2201 }
2202 if (config->randr_provider->output_source) {
2203 xf86DetachOutputGPU(screen);
2204 config->randr_provider->output_source = NULL;
2205 RRSetChanged(screen);
2206 }
2207 if (screen->current_master)
2208 DetachUnboundGPU(screen);
2209 }
2210 config->randr_provider = NULL;
2211 }
2212
2213 static void
xf86CrtcCheckReset(xf86CrtcPtr crtc)2214 xf86CrtcCheckReset(xf86CrtcPtr crtc) {
2215 if (xf86CrtcInUse(crtc)) {
2216 RRTransformPtr transform;
2217
2218 if (crtc->desiredTransformPresent)
2219 transform = &crtc->desiredTransform;
2220 else
2221 transform = NULL;
2222 xf86CrtcSetModeTransform(crtc, &crtc->desiredMode,
2223 crtc->desiredRotation, transform,
2224 crtc->desiredX, crtc->desiredY);
2225 xf86_crtc_show_cursor(crtc);
2226 }
2227 }
2228
2229 void
xf86CrtcLeaseTerminated(RRLeasePtr lease)2230 xf86CrtcLeaseTerminated(RRLeasePtr lease)
2231 {
2232 int c;
2233 int o;
2234 ScrnInfoPtr scrn = xf86ScreenToScrn(lease->screen);
2235
2236 RRLeaseTerminated(lease);
2237 /*
2238 * Force a full mode set on any crtc in the expiring lease which
2239 * was running before the lease started
2240 */
2241 for (c = 0; c < lease->numCrtcs; c++) {
2242 RRCrtcPtr randr_crtc = lease->crtcs[c];
2243 xf86CrtcPtr crtc = randr_crtc->devPrivate;
2244
2245 xf86CrtcCheckReset(crtc);
2246 }
2247
2248 /* Check to see if any leased output is using a crtc which
2249 * was not reset in the above loop
2250 */
2251 for (o = 0; o < lease->numOutputs; o++) {
2252 RROutputPtr randr_output = lease->outputs[o];
2253 xf86OutputPtr output = randr_output->devPrivate;
2254 xf86CrtcPtr crtc = output->crtc;
2255
2256 if (crtc) {
2257 for (c = 0; c < lease->numCrtcs; c++)
2258 if (lease->crtcs[c] == crtc->randr_crtc)
2259 break;
2260 if (c != lease->numCrtcs)
2261 continue;
2262 xf86CrtcCheckReset(crtc);
2263 }
2264 }
2265
2266 /* Power off if necessary */
2267 xf86DisableUnusedFunctions(scrn);
2268
2269 RRLeaseFree(lease);
2270 }
2271
2272 static Bool
xf86CrtcSoleOutput(xf86CrtcPtr crtc,xf86OutputPtr output)2273 xf86CrtcSoleOutput(xf86CrtcPtr crtc, xf86OutputPtr output)
2274 {
2275 ScrnInfoPtr scrn = crtc->scrn;
2276 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2277 int o;
2278
2279 for (o = 0; o < config->num_output; o++) {
2280 xf86OutputPtr other = config->output[o];
2281
2282 if (other != output && other->crtc == crtc)
2283 return FALSE;
2284 }
2285 return TRUE;
2286 }
2287
2288 void
xf86CrtcLeaseStarted(RRLeasePtr lease)2289 xf86CrtcLeaseStarted(RRLeasePtr lease)
2290 {
2291 int c;
2292 int o;
2293
2294 for (c = 0; c < lease->numCrtcs; c++) {
2295 RRCrtcPtr randr_crtc = lease->crtcs[c];
2296 xf86CrtcPtr crtc = randr_crtc->devPrivate;
2297
2298 if (crtc->enabled) {
2299 /*
2300 * Leave the primary plane enabled so we can
2301 * flip without blanking the screen. Hide
2302 * the cursor so it doesn't remain on the screen
2303 * while the lease is active
2304 */
2305 xf86_crtc_hide_cursor(crtc);
2306 crtc->enabled = FALSE;
2307 }
2308 }
2309 for (o = 0; o < lease->numOutputs; o++) {
2310 RROutputPtr randr_output = lease->outputs[o];
2311 xf86OutputPtr output = randr_output->devPrivate;
2312 xf86CrtcPtr crtc = output->crtc;
2313
2314 if (crtc)
2315 if (xf86CrtcSoleOutput(crtc, output))
2316 crtc->enabled = FALSE;
2317 }
2318 }
2319
2320 static int
xf86RandR16CreateLease(ScreenPtr screen,RRLeasePtr randr_lease,int * fd)2321 xf86RandR16CreateLease(ScreenPtr screen, RRLeasePtr randr_lease, int *fd)
2322 {
2323 ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
2324 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2325
2326 if (config->funcs->create_lease)
2327 return config->funcs->create_lease(randr_lease, fd);
2328 else
2329 return BadMatch;
2330 }
2331
2332
2333 static void
xf86RandR16TerminateLease(ScreenPtr screen,RRLeasePtr randr_lease)2334 xf86RandR16TerminateLease(ScreenPtr screen, RRLeasePtr randr_lease)
2335 {
2336 ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
2337 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2338
2339 if (config->funcs->terminate_lease)
2340 config->funcs->terminate_lease(randr_lease);
2341 }
2342
2343 static Bool
xf86RandR12Init12(ScreenPtr pScreen)2344 xf86RandR12Init12(ScreenPtr pScreen)
2345 {
2346 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
2347 rrScrPrivPtr rp = rrGetScrPriv(pScreen);
2348 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
2349
2350 rp->rrGetInfo = xf86RandR12GetInfo12;
2351 rp->rrScreenSetSize = xf86RandR12ScreenSetSize;
2352 rp->rrCrtcSet = xf86RandR12CrtcSet;
2353 rp->rrCrtcSetGamma = xf86RandR12CrtcSetGamma;
2354 rp->rrOutputSetProperty = xf86RandR12OutputSetProperty;
2355 rp->rrOutputValidateMode = xf86RandR12OutputValidateMode;
2356 #if RANDR_13_INTERFACE
2357 rp->rrOutputGetProperty = xf86RandR13OutputGetProperty;
2358 rp->rrGetPanning = xf86RandR13GetPanning;
2359 rp->rrSetPanning = xf86RandR13SetPanning;
2360 #endif
2361 rp->rrModeDestroy = xf86RandR12ModeDestroy;
2362 rp->rrSetConfig = NULL;
2363
2364 rp->rrProviderSetOutputSource = xf86RandR14ProviderSetOutputSource;
2365 rp->rrProviderSetOffloadSink = xf86RandR14ProviderSetOffloadSink;
2366
2367 rp->rrProviderSetProperty = xf86RandR14ProviderSetProperty;
2368 rp->rrProviderGetProperty = xf86RandR14ProviderGetProperty;
2369 rp->rrCrtcSetScanoutPixmap = xf86CrtcSetScanoutPixmap;
2370 rp->rrProviderDestroy = xf86RandR14ProviderDestroy;
2371
2372 rp->rrCreateLease = xf86RandR16CreateLease;
2373 rp->rrTerminateLease = xf86RandR16TerminateLease;
2374
2375 pScrn->PointerMoved = xf86RandR12PointerMoved;
2376 pScrn->ChangeGamma = xf86RandR12ChangeGamma;
2377
2378 randrp->orig_EnterVT = pScrn->EnterVT;
2379 pScrn->EnterVT = xf86RandR12EnterVT;
2380
2381 randrp->panning = FALSE;
2382 randrp->orig_ConstrainCursorHarder = pScreen->ConstrainCursorHarder;
2383 pScreen->ConstrainCursorHarder = xf86RandR13ConstrainCursorHarder;
2384
2385 if (!xf86RandR12CreateObjects12(pScreen))
2386 return FALSE;
2387
2388 /*
2389 * Configure output modes
2390 */
2391 if (!xf86RandR12SetInfo12(pScreen))
2392 return FALSE;
2393
2394 if (!xf86RandR12InitGamma(pScrn, 256))
2395 return FALSE;
2396
2397 return TRUE;
2398 }
2399
2400 #endif
2401
2402 Bool
xf86RandR12PreInit(ScrnInfoPtr pScrn)2403 xf86RandR12PreInit(ScrnInfoPtr pScrn)
2404 {
2405 return TRUE;
2406 }
2407