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 free(save_crtcs);
1244 return xf86RandR12CrtcNotify(randr_crtc);
1245 }
1246
1247 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)1248 xf86RandR12CrtcComputeGamma(xf86CrtcPtr crtc, LOCO *palette,
1249 int palette_red_size, int palette_green_size,
1250 int palette_blue_size, CARD16 *gamma_red,
1251 CARD16 *gamma_green, CARD16 *gamma_blue,
1252 int gamma_size)
1253 {
1254 int gamma_slots;
1255 unsigned shift;
1256 CARD32 value;
1257 int i, j;
1258
1259 for (shift = 0; (gamma_size << shift) < (1 << 16); shift++);
1260
1261 if (crtc->gamma_size >= palette_red_size) {
1262 /* Upsampling of smaller palette to larger hw lut size */
1263 gamma_slots = crtc->gamma_size / palette_red_size;
1264 for (i = 0; i < palette_red_size; i++) {
1265 value = palette[i].red;
1266 if (gamma_red)
1267 value = gamma_red[value];
1268 else
1269 value <<= shift;
1270
1271 for (j = 0; j < gamma_slots; j++)
1272 crtc->gamma_red[i * gamma_slots + j] = value;
1273 }
1274 } else {
1275 /* Downsampling of larger palette to smaller hw lut size */
1276 for (i = 0; i < crtc->gamma_size; i++) {
1277 value = palette[i * (palette_red_size - 1) / (crtc->gamma_size - 1)].red;
1278 if (gamma_red)
1279 value = gamma_red[value];
1280 else
1281 value <<= shift;
1282
1283 crtc->gamma_red[i] = value;
1284 }
1285 }
1286
1287 if (crtc->gamma_size >= palette_green_size) {
1288 /* Upsampling of smaller palette to larger hw lut size */
1289 gamma_slots = crtc->gamma_size / palette_green_size;
1290 for (i = 0; i < palette_green_size; i++) {
1291 value = palette[i].green;
1292 if (gamma_green)
1293 value = gamma_green[value];
1294 else
1295 value <<= shift;
1296
1297 for (j = 0; j < gamma_slots; j++)
1298 crtc->gamma_green[i * gamma_slots + j] = value;
1299 }
1300 } else {
1301 /* Downsampling of larger palette to smaller hw lut size */
1302 for (i = 0; i < crtc->gamma_size; i++) {
1303 value = palette[i * (palette_green_size - 1) / (crtc->gamma_size - 1)].green;
1304 if (gamma_green)
1305 value = gamma_green[value];
1306 else
1307 value <<= shift;
1308
1309 crtc->gamma_green[i] = value;
1310 }
1311 }
1312
1313 if (crtc->gamma_size >= palette_blue_size) {
1314 /* Upsampling of smaller palette to larger hw lut size */
1315 gamma_slots = crtc->gamma_size / palette_blue_size;
1316 for (i = 0; i < palette_blue_size; i++) {
1317 value = palette[i].blue;
1318 if (gamma_blue)
1319 value = gamma_blue[value];
1320 else
1321 value <<= shift;
1322
1323 for (j = 0; j < gamma_slots; j++)
1324 crtc->gamma_blue[i * gamma_slots + j] = value;
1325 }
1326 } else {
1327 /* Downsampling of larger palette to smaller hw lut size */
1328 for (i = 0; i < crtc->gamma_size; i++) {
1329 value = palette[i * (palette_blue_size - 1) / (crtc->gamma_size - 1)].blue;
1330 if (gamma_blue)
1331 value = gamma_blue[value];
1332 else
1333 value <<= shift;
1334
1335 crtc->gamma_blue[i] = value;
1336 }
1337 }
1338 }
1339
1340 static void
xf86RandR12CrtcReloadGamma(xf86CrtcPtr crtc)1341 xf86RandR12CrtcReloadGamma(xf86CrtcPtr crtc)
1342 {
1343 if (!crtc->scrn->vtSema || !crtc->funcs->gamma_set)
1344 return;
1345
1346 /* Only set it when the crtc is actually running.
1347 * Otherwise it will be set when it's activated.
1348 */
1349 if (crtc->active)
1350 crtc->funcs->gamma_set(crtc, crtc->gamma_red, crtc->gamma_green,
1351 crtc->gamma_blue, crtc->gamma_size);
1352 }
1353
1354 static Bool
xf86RandR12CrtcSetGamma(ScreenPtr pScreen,RRCrtcPtr randr_crtc)1355 xf86RandR12CrtcSetGamma(ScreenPtr pScreen, RRCrtcPtr randr_crtc)
1356 {
1357 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
1358 xf86CrtcPtr crtc = randr_crtc->devPrivate;
1359
1360 if (crtc->funcs->gamma_set == NULL)
1361 return FALSE;
1362
1363 if (randrp->palette_size) {
1364 xf86RandR12CrtcComputeGamma(crtc, randrp->palette,
1365 randrp->palette_red_size,
1366 randrp->palette_green_size,
1367 randrp->palette_blue_size,
1368 randr_crtc->gammaRed,
1369 randr_crtc->gammaGreen,
1370 randr_crtc->gammaBlue,
1371 randr_crtc->gammaSize);
1372 } else {
1373 memcpy(crtc->gamma_red, randr_crtc->gammaRed,
1374 crtc->gamma_size * sizeof(crtc->gamma_red[0]));
1375 memcpy(crtc->gamma_green, randr_crtc->gammaGreen,
1376 crtc->gamma_size * sizeof(crtc->gamma_green[0]));
1377 memcpy(crtc->gamma_blue, randr_crtc->gammaBlue,
1378 crtc->gamma_size * sizeof(crtc->gamma_blue[0]));
1379 }
1380
1381 xf86RandR12CrtcReloadGamma(crtc);
1382
1383 return TRUE;
1384 }
1385
1386 static void
init_one_component(CARD16 * comp,unsigned size,float gamma)1387 init_one_component(CARD16 *comp, unsigned size, float gamma)
1388 {
1389 int i;
1390 unsigned shift;
1391
1392 for (shift = 0; (size << shift) < (1 << 16); shift++);
1393
1394 if (gamma == 1.0) {
1395 for (i = 0; i < size; i++)
1396 comp[i] = i << shift;
1397 } else {
1398 for (i = 0; i < size; i++)
1399 comp[i] = (CARD16) (pow((double) i / (double) (size - 1),
1400 1. / (double) gamma) *
1401 (double) (size - 1) * (1 << shift));
1402 }
1403 }
1404
1405 static Bool
xf86RandR12CrtcInitGamma(xf86CrtcPtr crtc,float gamma_red,float gamma_green,float gamma_blue)1406 xf86RandR12CrtcInitGamma(xf86CrtcPtr crtc, float gamma_red, float gamma_green,
1407 float gamma_blue)
1408 {
1409 unsigned size = crtc->randr_crtc->gammaSize;
1410 CARD16 *red, *green, *blue;
1411
1412 if (!crtc->funcs->gamma_set &&
1413 (gamma_red != 1.0f || gamma_green != 1.0f || gamma_blue != 1.0f))
1414 return FALSE;
1415
1416 red = xallocarray(size, 3 * sizeof(CARD16));
1417 if (!red)
1418 return FALSE;
1419
1420 green = red + size;
1421 blue = green + size;
1422
1423 init_one_component(red, size, gamma_red);
1424 init_one_component(green, size, gamma_green);
1425 init_one_component(blue, size, gamma_blue);
1426
1427 RRCrtcGammaSet(crtc->randr_crtc, red, green, blue);
1428 free(red);
1429
1430 return TRUE;
1431 }
1432
1433 static Bool
xf86RandR12OutputInitGamma(xf86OutputPtr output)1434 xf86RandR12OutputInitGamma(xf86OutputPtr output)
1435 {
1436 XF86ConfMonitorPtr mon = output->conf_monitor;
1437 float gamma_red = 1.0, gamma_green = 1.0, gamma_blue = 1.0;
1438
1439 if (!mon)
1440 return TRUE;
1441
1442 /* Get configured values, where they exist. */
1443 if (mon->mon_gamma_red >= GAMMA_MIN && mon->mon_gamma_red <= GAMMA_MAX)
1444 gamma_red = mon->mon_gamma_red;
1445
1446 if (mon->mon_gamma_green >= GAMMA_MIN && mon->mon_gamma_green <= GAMMA_MAX)
1447 gamma_green = mon->mon_gamma_green;
1448
1449 if (mon->mon_gamma_blue >= GAMMA_MIN && mon->mon_gamma_blue <= GAMMA_MAX)
1450 gamma_blue = mon->mon_gamma_blue;
1451
1452 /* Don't set gamma 1.0 if another cloned output on this CRTC already set a
1453 * different gamma
1454 */
1455 if (gamma_red != 1.0 || gamma_green != 1.0 || gamma_blue != 1.0) {
1456 if (!output->crtc->randr_crtc) {
1457 xf86DrvMsg(output->scrn->scrnIndex, X_WARNING,
1458 "Gamma correction for output %s not possible because "
1459 "RandR is disabled\n", output->name);
1460 return TRUE;
1461 }
1462
1463 xf86DrvMsg(output->scrn->scrnIndex, X_INFO,
1464 "Output %s wants gamma correction (%.1f, %.1f, %.1f)\n",
1465 output->name, gamma_red, gamma_green, gamma_blue);
1466 return xf86RandR12CrtcInitGamma(output->crtc, gamma_red, gamma_green,
1467 gamma_blue);
1468 }
1469
1470 return TRUE;
1471 }
1472
1473 Bool
xf86RandR12InitGamma(ScrnInfoPtr pScrn,unsigned gammaSize)1474 xf86RandR12InitGamma(ScrnInfoPtr pScrn, unsigned gammaSize) {
1475 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1476 int o, c;
1477
1478 /* Set default gamma for all CRTCs
1479 * This is done to avoid problems later on with cloned outputs
1480 */
1481 for (c = 0; c < config->num_crtc; c++) {
1482 xf86CrtcPtr crtc = config->crtc[c];
1483
1484 if (!crtc->randr_crtc)
1485 continue;
1486
1487 if (!RRCrtcGammaSetSize(crtc->randr_crtc, gammaSize) ||
1488 !xf86RandR12CrtcInitGamma(crtc, 1.0f, 1.0f, 1.0f))
1489 return FALSE;
1490 }
1491
1492 /* Set initial gamma per monitor configuration
1493 */
1494 for (o = 0; o < config->num_output; o++) {
1495 xf86OutputPtr output = config->output[o];
1496
1497 if (output->crtc &&
1498 !xf86RandR12OutputInitGamma(output))
1499 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1500 "Initial gamma correction for output %s: failed.\n",
1501 output->name);
1502 }
1503
1504 return TRUE;
1505 }
1506
1507 static Bool
xf86RandR12OutputSetProperty(ScreenPtr pScreen,RROutputPtr randr_output,Atom property,RRPropertyValuePtr value)1508 xf86RandR12OutputSetProperty(ScreenPtr pScreen,
1509 RROutputPtr randr_output,
1510 Atom property, RRPropertyValuePtr value)
1511 {
1512 xf86OutputPtr output = randr_output->devPrivate;
1513
1514 /* If we don't have any property handler, then we don't care what the
1515 * user is setting properties to.
1516 */
1517 if (output->funcs->set_property == NULL)
1518 return TRUE;
1519
1520 /*
1521 * This function gets called even when vtSema is FALSE, as
1522 * drivers will need to remember the correct value to apply
1523 * when the VT switch occurs
1524 */
1525 return output->funcs->set_property(output, property, value);
1526 }
1527
1528 static Bool
xf86RandR13OutputGetProperty(ScreenPtr pScreen,RROutputPtr randr_output,Atom property)1529 xf86RandR13OutputGetProperty(ScreenPtr pScreen,
1530 RROutputPtr randr_output, Atom property)
1531 {
1532 xf86OutputPtr output = randr_output->devPrivate;
1533
1534 if (output->funcs->get_property == NULL)
1535 return TRUE;
1536
1537 /* Should be safe even w/o vtSema */
1538 return output->funcs->get_property(output, property);
1539 }
1540
1541 static Bool
xf86RandR12OutputValidateMode(ScreenPtr pScreen,RROutputPtr randr_output,RRModePtr randr_mode)1542 xf86RandR12OutputValidateMode(ScreenPtr pScreen,
1543 RROutputPtr randr_output, RRModePtr randr_mode)
1544 {
1545 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1546 xf86OutputPtr output = randr_output->devPrivate;
1547 DisplayModeRec mode;
1548
1549 xf86RandRModeConvert(pScrn, randr_mode, &mode);
1550 /*
1551 * This function may be called when vtSema is FALSE, so
1552 * the underlying function must either avoid touching the hardware
1553 * or return FALSE when vtSema is FALSE
1554 */
1555 if (output->funcs->mode_valid(output, &mode) != MODE_OK)
1556 return FALSE;
1557 return TRUE;
1558 }
1559
1560 static void
xf86RandR12ModeDestroy(ScreenPtr pScreen,RRModePtr randr_mode)1561 xf86RandR12ModeDestroy(ScreenPtr pScreen, RRModePtr randr_mode)
1562 {
1563 }
1564
1565 /**
1566 * Given a list of xf86 modes and a RandR Output object, construct
1567 * RandR modes and assign them to the output
1568 */
1569 static Bool
xf86RROutputSetModes(RROutputPtr randr_output,DisplayModePtr modes)1570 xf86RROutputSetModes(RROutputPtr randr_output, DisplayModePtr modes)
1571 {
1572 DisplayModePtr mode;
1573 RRModePtr *rrmodes = NULL;
1574 int nmode = 0;
1575 int npreferred = 0;
1576 Bool ret = TRUE;
1577 int pref;
1578
1579 for (mode = modes; mode; mode = mode->next)
1580 nmode++;
1581
1582 if (nmode) {
1583 rrmodes = xallocarray(nmode, sizeof(RRModePtr));
1584
1585 if (!rrmodes)
1586 return FALSE;
1587 nmode = 0;
1588
1589 for (pref = 1; pref >= 0; pref--) {
1590 for (mode = modes; mode; mode = mode->next) {
1591 if ((pref != 0) == ((mode->type & M_T_PREFERRED) != 0)) {
1592 xRRModeInfo modeInfo;
1593 RRModePtr rrmode;
1594
1595 modeInfo.nameLength = strlen(mode->name);
1596 modeInfo.width = mode->HDisplay;
1597 modeInfo.dotClock = mode->Clock * 1000;
1598 modeInfo.hSyncStart = mode->HSyncStart;
1599 modeInfo.hSyncEnd = mode->HSyncEnd;
1600 modeInfo.hTotal = mode->HTotal;
1601 modeInfo.hSkew = mode->HSkew;
1602
1603 modeInfo.height = mode->VDisplay;
1604 modeInfo.vSyncStart = mode->VSyncStart;
1605 modeInfo.vSyncEnd = mode->VSyncEnd;
1606 modeInfo.vTotal = mode->VTotal;
1607 modeInfo.modeFlags = mode->Flags;
1608
1609 rrmode = RRModeGet(&modeInfo, mode->name);
1610 if (rrmode) {
1611 rrmodes[nmode++] = rrmode;
1612 npreferred += pref;
1613 }
1614 }
1615 }
1616 }
1617 }
1618
1619 ret = RROutputSetModes(randr_output, rrmodes, nmode, npreferred);
1620 free(rrmodes);
1621 return ret;
1622 }
1623
1624 /*
1625 * Mirror the current mode configuration to RandR
1626 */
1627 static Bool
xf86RandR12SetInfo12(ScreenPtr pScreen)1628 xf86RandR12SetInfo12(ScreenPtr pScreen)
1629 {
1630 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1631 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1632 RROutputPtr *clones;
1633 RRCrtcPtr *crtcs;
1634 int ncrtc;
1635 int o, c, l;
1636 int nclone;
1637
1638 clones = xallocarray(config->num_output, sizeof(RROutputPtr));
1639 crtcs = xallocarray(config->num_crtc, sizeof(RRCrtcPtr));
1640 for (o = 0; o < config->num_output; o++) {
1641 xf86OutputPtr output = config->output[o];
1642
1643 ncrtc = 0;
1644 for (c = 0; c < config->num_crtc; c++)
1645 if (output->possible_crtcs & (1 << c))
1646 crtcs[ncrtc++] = config->crtc[c]->randr_crtc;
1647
1648 if (!RROutputSetCrtcs(output->randr_output, crtcs, ncrtc)) {
1649 free(crtcs);
1650 free(clones);
1651 return FALSE;
1652 }
1653
1654 RROutputSetPhysicalSize(output->randr_output,
1655 output->mm_width, output->mm_height);
1656 xf86RROutputSetModes(output->randr_output, output->probed_modes);
1657
1658 switch (output->status) {
1659 case XF86OutputStatusConnected:
1660 RROutputSetConnection(output->randr_output, RR_Connected);
1661 break;
1662 case XF86OutputStatusDisconnected:
1663 RROutputSetConnection(output->randr_output, RR_Disconnected);
1664 break;
1665 case XF86OutputStatusUnknown:
1666 RROutputSetConnection(output->randr_output, RR_UnknownConnection);
1667 break;
1668 }
1669
1670 RROutputSetSubpixelOrder(output->randr_output, output->subpixel_order);
1671
1672 /*
1673 * Valid clones
1674 */
1675 nclone = 0;
1676 for (l = 0; l < config->num_output; l++) {
1677 xf86OutputPtr clone = config->output[l];
1678
1679 if (l != o && (output->possible_clones & (1 << l)))
1680 clones[nclone++] = clone->randr_output;
1681 }
1682 if (!RROutputSetClones(output->randr_output, clones, nclone)) {
1683 free(crtcs);
1684 free(clones);
1685 return FALSE;
1686 }
1687 }
1688 free(crtcs);
1689 free(clones);
1690 return TRUE;
1691 }
1692
1693 /*
1694 * Query the hardware for the current state, then mirror
1695 * that to RandR
1696 */
1697 static Bool
xf86RandR12GetInfo12(ScreenPtr pScreen,Rotation * rotations)1698 xf86RandR12GetInfo12(ScreenPtr pScreen, Rotation * rotations)
1699 {
1700 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1701
1702 if (!pScrn->vtSema)
1703 return TRUE;
1704 xf86ProbeOutputModes(pScrn, 0, 0);
1705 xf86SetScrnInfoModes(pScrn);
1706 return xf86RandR12SetInfo12(pScreen);
1707 }
1708
1709 static Bool
xf86RandR12CreateObjects12(ScreenPtr pScreen)1710 xf86RandR12CreateObjects12(ScreenPtr pScreen)
1711 {
1712 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1713 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1714 int c;
1715 int o;
1716
1717 if (!RRInit())
1718 return FALSE;
1719
1720 /*
1721 * Configure crtcs
1722 */
1723 for (c = 0; c < config->num_crtc; c++) {
1724 xf86CrtcPtr crtc = config->crtc[c];
1725
1726 crtc->randr_crtc = RRCrtcCreate(pScreen, crtc);
1727 }
1728 /*
1729 * Configure outputs
1730 */
1731 for (o = 0; o < config->num_output; o++) {
1732 xf86OutputPtr output = config->output[o];
1733
1734 output->randr_output = RROutputCreate(pScreen, output->name,
1735 strlen(output->name), output);
1736
1737 if (output->funcs->create_resources != NULL)
1738 output->funcs->create_resources(output);
1739 RRPostPendingProperties(output->randr_output);
1740 }
1741
1742 if (config->name) {
1743 config->randr_provider = RRProviderCreate(pScreen, config->name,
1744 strlen(config->name));
1745
1746 RRProviderSetCapabilities(config->randr_provider, pScrn->capabilities);
1747 }
1748
1749 return TRUE;
1750 }
1751
1752 static void
xf86RandR12CreateMonitors(ScreenPtr pScreen)1753 xf86RandR12CreateMonitors(ScreenPtr pScreen)
1754 {
1755 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1756 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1757 int o, ot;
1758 int ht, vt;
1759 int ret;
1760 char buf[25];
1761
1762 for (o = 0; o < config->num_output; o++) {
1763 xf86OutputPtr output = config->output[o];
1764 struct xf86CrtcTileInfo *tile_info = &output->tile_info, *this_tile;
1765 RRMonitorPtr monitor;
1766 int output_num, num_outputs;
1767 if (!tile_info->group_id)
1768 continue;
1769
1770 if (tile_info->tile_h_loc ||
1771 tile_info->tile_v_loc)
1772 continue;
1773
1774 num_outputs = tile_info->num_h_tile * tile_info->num_v_tile;
1775
1776 monitor = RRMonitorAlloc(num_outputs);
1777 if (!monitor)
1778 return;
1779 monitor->pScreen = pScreen;
1780 snprintf(buf, 25, "Auto-Monitor-%d", tile_info->group_id);
1781 monitor->name = MakeAtom(buf, strlen(buf), TRUE);
1782 monitor->primary = 0;
1783 monitor->automatic = TRUE;
1784 memset(&monitor->geometry.box, 0, sizeof(monitor->geometry.box));
1785
1786 output_num = 0;
1787 for (ht = 0; ht < tile_info->num_h_tile; ht++) {
1788 for (vt = 0; vt < tile_info->num_v_tile; vt++) {
1789
1790 for (ot = 0; ot < config->num_output; ot++) {
1791 this_tile = &config->output[ot]->tile_info;
1792
1793 if (this_tile->group_id != tile_info->group_id)
1794 continue;
1795
1796 if (this_tile->tile_h_loc != ht ||
1797 this_tile->tile_v_loc != vt)
1798 continue;
1799
1800 monitor->outputs[output_num] = config->output[ot]->randr_output->id;
1801 output_num++;
1802
1803 }
1804
1805 }
1806 }
1807
1808 ret = RRMonitorAdd(serverClient, pScreen, monitor);
1809 if (ret) {
1810 RRMonitorFree(monitor);
1811 return;
1812 }
1813 }
1814 }
1815
1816 static Bool
xf86RandR12CreateScreenResources12(ScreenPtr pScreen)1817 xf86RandR12CreateScreenResources12(ScreenPtr pScreen)
1818 {
1819 int c;
1820 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1821 rrScrPrivPtr rp = rrGetScrPriv(pScreen);
1822 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1823
1824 if (xf86RandR12Key == NULL)
1825 return TRUE;
1826
1827 for (c = 0; c < config->num_crtc; c++)
1828 xf86RandR12CrtcNotify(config->crtc[c]->randr_crtc);
1829
1830 RRScreenSetSizeRange(pScreen, config->minWidth, config->minHeight,
1831 config->maxWidth, config->maxHeight);
1832
1833 xf86RandR12CreateMonitors(pScreen);
1834
1835 if (!pScreen->isGPU) {
1836 rp->primaryOutput = config->output[0]->randr_output;
1837 RROutputChanged(rp->primaryOutput, FALSE);
1838 rp->layoutChanged = TRUE;
1839 }
1840
1841 return TRUE;
1842 }
1843
1844 /*
1845 * Something happened within the screen configuration due
1846 * to DGA, VidMode or hot key. Tell RandR
1847 */
1848
1849 void
xf86RandR12TellChanged(ScreenPtr pScreen)1850 xf86RandR12TellChanged(ScreenPtr pScreen)
1851 {
1852 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1853 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1854 int c;
1855
1856 if (xf86RandR12Key == NULL)
1857 return;
1858
1859 xf86RandR12SetInfo12(pScreen);
1860 for (c = 0; c < config->num_crtc; c++)
1861 xf86RandR12CrtcNotify(config->crtc[c]->randr_crtc);
1862
1863 RRTellChanged(pScreen);
1864 }
1865
1866 static void
xf86RandR12PointerMoved(ScrnInfoPtr pScrn,int x,int y)1867 xf86RandR12PointerMoved(ScrnInfoPtr pScrn, int x, int y)
1868 {
1869 ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
1870 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1871 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
1872 int c;
1873
1874 randrp->pointerX = x;
1875 randrp->pointerY = y;
1876 for (c = 0; c < config->num_crtc; c++)
1877 xf86RandR13Pan(config->crtc[c], x, y);
1878 }
1879
1880 static Bool
xf86RandR13GetPanning(ScreenPtr pScreen,RRCrtcPtr randr_crtc,BoxPtr totalArea,BoxPtr trackingArea,INT16 * border)1881 xf86RandR13GetPanning(ScreenPtr pScreen,
1882 RRCrtcPtr randr_crtc,
1883 BoxPtr totalArea, BoxPtr trackingArea, INT16 *border)
1884 {
1885 xf86CrtcPtr crtc = randr_crtc->devPrivate;
1886
1887 if (crtc->version < 2)
1888 return FALSE;
1889 if (totalArea)
1890 memcpy(totalArea, &crtc->panningTotalArea, sizeof(BoxRec));
1891 if (trackingArea)
1892 memcpy(trackingArea, &crtc->panningTrackingArea, sizeof(BoxRec));
1893 if (border)
1894 memcpy(border, crtc->panningBorder, 4 * sizeof(INT16));
1895
1896 return TRUE;
1897 }
1898
1899 static Bool
xf86RandR13SetPanning(ScreenPtr pScreen,RRCrtcPtr randr_crtc,BoxPtr totalArea,BoxPtr trackingArea,INT16 * border)1900 xf86RandR13SetPanning(ScreenPtr pScreen,
1901 RRCrtcPtr randr_crtc,
1902 BoxPtr totalArea, BoxPtr trackingArea, INT16 *border)
1903 {
1904 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
1905 xf86CrtcPtr crtc = randr_crtc->devPrivate;
1906 BoxRec oldTotalArea;
1907 BoxRec oldTrackingArea;
1908 INT16 oldBorder[4];
1909 Bool oldPanning = randrp->panning;
1910
1911 if (crtc->version < 2)
1912 return FALSE;
1913
1914 memcpy(&oldTotalArea, &crtc->panningTotalArea, sizeof(BoxRec));
1915 memcpy(&oldTrackingArea, &crtc->panningTrackingArea, sizeof(BoxRec));
1916 memcpy(oldBorder, crtc->panningBorder, 4 * sizeof(INT16));
1917
1918 if (totalArea)
1919 memcpy(&crtc->panningTotalArea, totalArea, sizeof(BoxRec));
1920 if (trackingArea)
1921 memcpy(&crtc->panningTrackingArea, trackingArea, sizeof(BoxRec));
1922 if (border)
1923 memcpy(crtc->panningBorder, border, 4 * sizeof(INT16));
1924
1925 if (xf86RandR13VerifyPanningArea(crtc, pScreen->width, pScreen->height)) {
1926 xf86RandR13Pan(crtc, randrp->pointerX, randrp->pointerY);
1927 randrp->panning = PANNING_ENABLED (crtc);
1928 return TRUE;
1929 }
1930 else {
1931 /* Restore old settings */
1932 memcpy(&crtc->panningTotalArea, &oldTotalArea, sizeof(BoxRec));
1933 memcpy(&crtc->panningTrackingArea, &oldTrackingArea, sizeof(BoxRec));
1934 memcpy(crtc->panningBorder, oldBorder, 4 * sizeof(INT16));
1935 randrp->panning = oldPanning;
1936 return FALSE;
1937 }
1938 }
1939
1940 /*
1941 * Compatibility with colormaps and XF86VidMode's gamma
1942 */
1943 void
xf86RandR12LoadPalette(ScrnInfoPtr pScrn,int numColors,int * indices,LOCO * colors,VisualPtr pVisual)1944 xf86RandR12LoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
1945 LOCO *colors, VisualPtr pVisual)
1946 {
1947 ScreenPtr pScreen = pScrn->pScreen;
1948 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1949 int reds, greens, blues, index, palette_size;
1950 int c, i;
1951
1952 if (pVisual->class == TrueColor || pVisual->class == DirectColor) {
1953 reds = (pVisual->redMask >> pVisual->offsetRed) + 1;
1954 greens = (pVisual->greenMask >> pVisual->offsetGreen) + 1;
1955 blues = (pVisual->blueMask >> pVisual->offsetBlue) + 1;
1956 } else {
1957 reds = greens = blues = pVisual->ColormapEntries;
1958 }
1959
1960 palette_size = max(reds, max(greens, blues));
1961
1962 if (dixPrivateKeyRegistered(rrPrivKey)) {
1963 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
1964
1965 if (randrp->palette_size != palette_size) {
1966 randrp->palette = reallocarray(randrp->palette, palette_size,
1967 sizeof(colors[0]));
1968 if (!randrp->palette) {
1969 randrp->palette_size = 0;
1970 return;
1971 }
1972
1973 randrp->palette_size = palette_size;
1974 }
1975 randrp->palette_red_size = reds;
1976 randrp->palette_green_size = greens;
1977 randrp->palette_blue_size = blues;
1978
1979 for (i = 0; i < numColors; i++) {
1980 index = indices[i];
1981
1982 if (index < reds)
1983 randrp->palette[index].red = colors[index].red;
1984 if (index < greens)
1985 randrp->palette[index].green = colors[index].green;
1986 if (index < blues)
1987 randrp->palette[index].blue = colors[index].blue;
1988 }
1989 }
1990
1991 for (c = 0; c < config->num_crtc; c++) {
1992 xf86CrtcPtr crtc = config->crtc[c];
1993 RRCrtcPtr randr_crtc = crtc->randr_crtc;
1994
1995 if (randr_crtc) {
1996 xf86RandR12CrtcComputeGamma(crtc, colors, reds, greens, blues,
1997 randr_crtc->gammaRed,
1998 randr_crtc->gammaGreen,
1999 randr_crtc->gammaBlue,
2000 randr_crtc->gammaSize);
2001 } else {
2002 xf86RandR12CrtcComputeGamma(crtc, colors, reds, greens, blues,
2003 NULL, NULL, NULL,
2004 xf86GetGammaRampSize(pScreen));
2005 }
2006 xf86RandR12CrtcReloadGamma(crtc);
2007 }
2008 }
2009
2010 /*
2011 * Compatibility pScrn->ChangeGamma provider for ddx drivers which do not call
2012 * xf86HandleColormaps(). Note such drivers really should be fixed to call
2013 * xf86HandleColormaps() as this clobbers the per-CRTC gamma ramp of the CRTC
2014 * assigned to the RandR compatibility output.
2015 */
2016 static int
xf86RandR12ChangeGamma(ScrnInfoPtr pScrn,Gamma gamma)2017 xf86RandR12ChangeGamma(ScrnInfoPtr pScrn, Gamma gamma)
2018 {
2019 RRCrtcPtr randr_crtc = xf86CompatRRCrtc(pScrn);
2020 int size;
2021
2022 if (!randr_crtc || pScrn->LoadPalette == xf86RandR12LoadPalette)
2023 return Success;
2024
2025 size = max(0, randr_crtc->gammaSize);
2026 if (!size)
2027 return Success;
2028
2029 init_one_component(randr_crtc->gammaRed, size, gamma.red);
2030 init_one_component(randr_crtc->gammaGreen, size, gamma.green);
2031 init_one_component(randr_crtc->gammaBlue, size, gamma.blue);
2032 xf86RandR12CrtcSetGamma(xf86ScrnToScreen(pScrn), randr_crtc);
2033
2034 pScrn->gamma = gamma;
2035
2036 return Success;
2037 }
2038
2039 static Bool
xf86RandR12EnterVT(ScrnInfoPtr pScrn)2040 xf86RandR12EnterVT(ScrnInfoPtr pScrn)
2041 {
2042 ScreenPtr pScreen = xf86ScrnToScreen(pScrn);
2043 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
2044 rrScrPrivPtr rp = rrGetScrPriv(pScreen);
2045 Bool ret;
2046 int i;
2047
2048 if (randrp->orig_EnterVT) {
2049 pScrn->EnterVT = randrp->orig_EnterVT;
2050 ret = pScrn->EnterVT(pScrn);
2051 randrp->orig_EnterVT = pScrn->EnterVT;
2052 pScrn->EnterVT = xf86RandR12EnterVT;
2053 if (!ret)
2054 return FALSE;
2055 }
2056
2057 /* reload gamma */
2058 for (i = 0; i < rp->numCrtcs; i++)
2059 xf86RandR12CrtcReloadGamma(rp->crtcs[i]->devPrivate);
2060
2061 return RRGetInfo(pScreen, TRUE); /* force a re-probe of outputs and notify clients about changes */
2062 }
2063
2064 static void
xf86DetachOutputGPU(ScreenPtr pScreen)2065 xf86DetachOutputGPU(ScreenPtr pScreen)
2066 {
2067 rrScrPrivPtr rp = rrGetScrPriv(pScreen);
2068 int i;
2069
2070 /* make sure there are no attached shared scanout pixmaps first */
2071 for (i = 0; i < rp->numCrtcs; i++)
2072 RRCrtcDetachScanoutPixmap(rp->crtcs[i]);
2073
2074 DetachOutputGPU(pScreen);
2075 }
2076
2077 static Bool
xf86RandR14ProviderSetOutputSource(ScreenPtr pScreen,RRProviderPtr provider,RRProviderPtr source_provider)2078 xf86RandR14ProviderSetOutputSource(ScreenPtr pScreen,
2079 RRProviderPtr provider,
2080 RRProviderPtr source_provider)
2081 {
2082 if (!source_provider) {
2083 if (provider->output_source) {
2084 xf86DetachOutputGPU(pScreen);
2085 }
2086 provider->output_source = NULL;
2087 return TRUE;
2088 }
2089
2090 if (provider->output_source == source_provider)
2091 return TRUE;
2092
2093 SetRootClip(source_provider->pScreen, ROOT_CLIP_NONE);
2094
2095 AttachOutputGPU(source_provider->pScreen, pScreen);
2096
2097 provider->output_source = source_provider;
2098 SetRootClip(source_provider->pScreen, ROOT_CLIP_FULL);
2099 return TRUE;
2100 }
2101
2102 static Bool
xf86RandR14ProviderSetOffloadSink(ScreenPtr pScreen,RRProviderPtr provider,RRProviderPtr sink_provider)2103 xf86RandR14ProviderSetOffloadSink(ScreenPtr pScreen,
2104 RRProviderPtr provider,
2105 RRProviderPtr sink_provider)
2106 {
2107 if (!sink_provider) {
2108 if (provider->offload_sink) {
2109 xf86DetachOutputGPU(pScreen);
2110 }
2111
2112 provider->offload_sink = NULL;
2113 return TRUE;
2114 }
2115
2116 if (provider->offload_sink == sink_provider)
2117 return TRUE;
2118
2119 AttachOffloadGPU(sink_provider->pScreen, pScreen);
2120
2121 provider->offload_sink = sink_provider;
2122 return TRUE;
2123 }
2124
2125 static Bool
xf86RandR14ProviderSetProperty(ScreenPtr pScreen,RRProviderPtr randr_provider,Atom property,RRPropertyValuePtr value)2126 xf86RandR14ProviderSetProperty(ScreenPtr pScreen,
2127 RRProviderPtr randr_provider,
2128 Atom property, RRPropertyValuePtr value)
2129 {
2130 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
2131 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
2132
2133 /* If we don't have any property handler, then we don't care what the
2134 * user is setting properties to.
2135 */
2136 if (config->provider_funcs->set_property == NULL)
2137 return TRUE;
2138
2139 /*
2140 * This function gets called even when vtSema is FALSE, as
2141 * drivers will need to remember the correct value to apply
2142 * when the VT switch occurs
2143 */
2144 return config->provider_funcs->set_property(pScrn, property, value);
2145 }
2146
2147 static Bool
xf86RandR14ProviderGetProperty(ScreenPtr pScreen,RRProviderPtr randr_provider,Atom property)2148 xf86RandR14ProviderGetProperty(ScreenPtr pScreen,
2149 RRProviderPtr randr_provider, Atom property)
2150 {
2151 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
2152 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
2153
2154 if (config->provider_funcs->get_property == NULL)
2155 return TRUE;
2156
2157 /* Should be safe even w/o vtSema */
2158 return config->provider_funcs->get_property(pScrn, property);
2159 }
2160
2161 static Bool
xf86CrtcSetScanoutPixmap(RRCrtcPtr randr_crtc,PixmapPtr pixmap)2162 xf86CrtcSetScanoutPixmap(RRCrtcPtr randr_crtc, PixmapPtr pixmap)
2163 {
2164 xf86CrtcPtr crtc = randr_crtc->devPrivate;
2165 if (!crtc->funcs->set_scanout_pixmap)
2166 return FALSE;
2167 return crtc->funcs->set_scanout_pixmap(crtc, pixmap);
2168 }
2169
2170 static void
xf86RandR13ConstrainCursorHarder(DeviceIntPtr dev,ScreenPtr screen,int mode,int * x,int * y)2171 xf86RandR13ConstrainCursorHarder(DeviceIntPtr dev, ScreenPtr screen, int mode, int *x, int *y)
2172 {
2173 XF86RandRInfoPtr randrp = XF86RANDRINFO(screen);
2174
2175 if (randrp->panning)
2176 return;
2177
2178 if (randrp->orig_ConstrainCursorHarder) {
2179 screen->ConstrainCursorHarder = randrp->orig_ConstrainCursorHarder;
2180 screen->ConstrainCursorHarder(dev, screen, mode, x, y);
2181 screen->ConstrainCursorHarder = xf86RandR13ConstrainCursorHarder;
2182 }
2183 }
2184
2185 static void
xf86RandR14ProviderDestroy(ScreenPtr screen,RRProviderPtr provider)2186 xf86RandR14ProviderDestroy(ScreenPtr screen, RRProviderPtr provider)
2187 {
2188 ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
2189 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2190
2191 if (config->randr_provider == provider) {
2192 if (config->randr_provider->offload_sink) {
2193 DetachOffloadGPU(screen);
2194 config->randr_provider->offload_sink = NULL;
2195 RRSetChanged(screen);
2196 }
2197 if (config->randr_provider->output_source) {
2198 xf86DetachOutputGPU(screen);
2199 config->randr_provider->output_source = NULL;
2200 RRSetChanged(screen);
2201 }
2202 if (screen->current_master)
2203 DetachUnboundGPU(screen);
2204 }
2205 config->randr_provider = NULL;
2206 }
2207
2208 static void
xf86CrtcCheckReset(xf86CrtcPtr crtc)2209 xf86CrtcCheckReset(xf86CrtcPtr crtc) {
2210 if (xf86CrtcInUse(crtc)) {
2211 RRTransformPtr transform;
2212
2213 if (crtc->desiredTransformPresent)
2214 transform = &crtc->desiredTransform;
2215 else
2216 transform = NULL;
2217 xf86CrtcSetModeTransform(crtc, &crtc->desiredMode,
2218 crtc->desiredRotation, transform,
2219 crtc->desiredX, crtc->desiredY);
2220 xf86_crtc_show_cursor(crtc);
2221 }
2222 }
2223
2224 void
xf86CrtcLeaseTerminated(RRLeasePtr lease)2225 xf86CrtcLeaseTerminated(RRLeasePtr lease)
2226 {
2227 int c;
2228 int o;
2229 ScrnInfoPtr scrn = xf86ScreenToScrn(lease->screen);
2230
2231 RRLeaseTerminated(lease);
2232 /*
2233 * Force a full mode set on any crtc in the expiring lease which
2234 * was running before the lease started
2235 */
2236 for (c = 0; c < lease->numCrtcs; c++) {
2237 RRCrtcPtr randr_crtc = lease->crtcs[c];
2238 xf86CrtcPtr crtc = randr_crtc->devPrivate;
2239
2240 xf86CrtcCheckReset(crtc);
2241 }
2242
2243 /* Check to see if any leased output is using a crtc which
2244 * was not reset in the above loop
2245 */
2246 for (o = 0; o < lease->numOutputs; o++) {
2247 RROutputPtr randr_output = lease->outputs[o];
2248 xf86OutputPtr output = randr_output->devPrivate;
2249 xf86CrtcPtr crtc = output->crtc;
2250
2251 if (crtc) {
2252 for (c = 0; c < lease->numCrtcs; c++)
2253 if (lease->crtcs[c] == crtc->randr_crtc)
2254 break;
2255 if (c != lease->numCrtcs)
2256 continue;
2257 xf86CrtcCheckReset(crtc);
2258 }
2259 }
2260
2261 /* Power off if necessary */
2262 xf86DisableUnusedFunctions(scrn);
2263
2264 RRLeaseFree(lease);
2265 }
2266
2267 static Bool
xf86CrtcSoleOutput(xf86CrtcPtr crtc,xf86OutputPtr output)2268 xf86CrtcSoleOutput(xf86CrtcPtr crtc, xf86OutputPtr output)
2269 {
2270 ScrnInfoPtr scrn = crtc->scrn;
2271 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2272 int o;
2273
2274 for (o = 0; o < config->num_output; o++) {
2275 xf86OutputPtr other = config->output[o];
2276
2277 if (other != output && other->crtc == crtc)
2278 return FALSE;
2279 }
2280 return TRUE;
2281 }
2282
2283 void
xf86CrtcLeaseStarted(RRLeasePtr lease)2284 xf86CrtcLeaseStarted(RRLeasePtr lease)
2285 {
2286 int c;
2287 int o;
2288
2289 for (c = 0; c < lease->numCrtcs; c++) {
2290 RRCrtcPtr randr_crtc = lease->crtcs[c];
2291 xf86CrtcPtr crtc = randr_crtc->devPrivate;
2292
2293 if (crtc->enabled) {
2294 /*
2295 * Leave the primary plane enabled so we can
2296 * flip without blanking the screen. Hide
2297 * the cursor so it doesn't remain on the screen
2298 * while the lease is active
2299 */
2300 xf86_crtc_hide_cursor(crtc);
2301 crtc->enabled = FALSE;
2302 }
2303 }
2304 for (o = 0; o < lease->numOutputs; o++) {
2305 RROutputPtr randr_output = lease->outputs[o];
2306 xf86OutputPtr output = randr_output->devPrivate;
2307 xf86CrtcPtr crtc = output->crtc;
2308
2309 if (crtc)
2310 if (xf86CrtcSoleOutput(crtc, output))
2311 crtc->enabled = FALSE;
2312 }
2313 }
2314
2315 static int
xf86RandR16CreateLease(ScreenPtr screen,RRLeasePtr randr_lease,int * fd)2316 xf86RandR16CreateLease(ScreenPtr screen, RRLeasePtr randr_lease, int *fd)
2317 {
2318 ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
2319 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2320
2321 if (config->funcs->create_lease)
2322 return config->funcs->create_lease(randr_lease, fd);
2323 else
2324 return BadMatch;
2325 }
2326
2327
2328 static void
xf86RandR16TerminateLease(ScreenPtr screen,RRLeasePtr randr_lease)2329 xf86RandR16TerminateLease(ScreenPtr screen, RRLeasePtr randr_lease)
2330 {
2331 ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
2332 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
2333
2334 if (config->funcs->terminate_lease)
2335 config->funcs->terminate_lease(randr_lease);
2336 }
2337
2338 static Bool
xf86RandR12Init12(ScreenPtr pScreen)2339 xf86RandR12Init12(ScreenPtr pScreen)
2340 {
2341 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
2342 rrScrPrivPtr rp = rrGetScrPriv(pScreen);
2343 XF86RandRInfoPtr randrp = XF86RANDRINFO(pScreen);
2344
2345 rp->rrGetInfo = xf86RandR12GetInfo12;
2346 rp->rrScreenSetSize = xf86RandR12ScreenSetSize;
2347 rp->rrCrtcSet = xf86RandR12CrtcSet;
2348 rp->rrCrtcSetGamma = xf86RandR12CrtcSetGamma;
2349 rp->rrOutputSetProperty = xf86RandR12OutputSetProperty;
2350 rp->rrOutputValidateMode = xf86RandR12OutputValidateMode;
2351 #if RANDR_13_INTERFACE
2352 rp->rrOutputGetProperty = xf86RandR13OutputGetProperty;
2353 rp->rrGetPanning = xf86RandR13GetPanning;
2354 rp->rrSetPanning = xf86RandR13SetPanning;
2355 #endif
2356 rp->rrModeDestroy = xf86RandR12ModeDestroy;
2357 rp->rrSetConfig = NULL;
2358
2359 rp->rrProviderSetOutputSource = xf86RandR14ProviderSetOutputSource;
2360 rp->rrProviderSetOffloadSink = xf86RandR14ProviderSetOffloadSink;
2361
2362 rp->rrProviderSetProperty = xf86RandR14ProviderSetProperty;
2363 rp->rrProviderGetProperty = xf86RandR14ProviderGetProperty;
2364 rp->rrCrtcSetScanoutPixmap = xf86CrtcSetScanoutPixmap;
2365 rp->rrProviderDestroy = xf86RandR14ProviderDestroy;
2366
2367 rp->rrCreateLease = xf86RandR16CreateLease;
2368 rp->rrTerminateLease = xf86RandR16TerminateLease;
2369
2370 pScrn->PointerMoved = xf86RandR12PointerMoved;
2371 pScrn->ChangeGamma = xf86RandR12ChangeGamma;
2372
2373 randrp->orig_EnterVT = pScrn->EnterVT;
2374 pScrn->EnterVT = xf86RandR12EnterVT;
2375
2376 randrp->panning = FALSE;
2377 randrp->orig_ConstrainCursorHarder = pScreen->ConstrainCursorHarder;
2378 pScreen->ConstrainCursorHarder = xf86RandR13ConstrainCursorHarder;
2379
2380 if (!xf86RandR12CreateObjects12(pScreen))
2381 return FALSE;
2382
2383 /*
2384 * Configure output modes
2385 */
2386 if (!xf86RandR12SetInfo12(pScreen))
2387 return FALSE;
2388
2389 if (!xf86RandR12InitGamma(pScrn, 256))
2390 return FALSE;
2391
2392 return TRUE;
2393 }
2394
2395 #endif
2396
2397 Bool
xf86RandR12PreInit(ScrnInfoPtr pScrn)2398 xf86RandR12PreInit(ScrnInfoPtr pScrn)
2399 {
2400 return TRUE;
2401 }
2402