1 /* Copied from hw/xwayland/xwayland-cvt.c */
2 
3 /*
4  * Copyright 2005-2006 Luc Verhaegen.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /*
26  * The reason for having this function in a file of its own is
27  * so that ../utils/cvt/cvt can link to it, and that xf86CVTMode
28  * code is shared directly.
29  */
30 
31 #ifdef HAVE_DIX_CONFIG_H
32 #include <dix-config.h>
33 #endif
34 
35 #include <string.h>
36 #include <randrstr.h>
37 #include "arcan.h"
38 
39 /*
40  * Generate a CVT standard mode from HDisplay, VDisplay and VRefresh.
41  *
42  * These calculations are stolen from the CVT calculation spreadsheet written
43  * by Graham Loveridge. He seems to be claiming no copyright and there seems to
44  * be no license attached to this. He apparently just wants to see his name
45  * mentioned.
46  *
47  * This file can be found at http://www.vesa.org/Public/CVT/CVTd6r1.xls
48  *
49  * Comments and structure corresponds to the comments and structure of the xls.
50  * This should ease importing of future changes to the standard (not very
51  * likely though).
52  *
53  * About margins; i'm sure that they are to be the bit between HDisplay and
54  * HBlankStart, HBlankEnd and HTotal, VDisplay and VBlankStart, VBlankEnd and
55  * VTotal, where the overscan colour is shown. FB seems to call _all_ blanking
56  * outside sync "margin" for some reason. Since we prefer seeing proper
57  * blanking instead of the overscan colour, and since the Crtc* values will
58  * probably get altered after us, we will disable margins altogether. With
59  * these calculations, Margins will plainly expand H/VDisplay, and we don't
60  * want that. -- libv
61  *
62  */
63 RRModePtr
arcan_cvt(int HDisplay,int VDisplay,float VRefresh,Bool Reduced,Bool Interlaced)64 arcan_cvt(int HDisplay, int VDisplay, float VRefresh, Bool Reduced,
65              Bool Interlaced)
66 {
67     /* 1) top/bottom margin size (% of height) - default: 1.8 */
68 #define CVT_MARGIN_PERCENTAGE 1.8
69 
70     /* 2) character cell horizontal granularity (pixels) - default 8 */
71 #define CVT_H_GRANULARITY 8
72 
73     /* 4) Minimum vertical porch (lines) - default 3 */
74 #define CVT_MIN_V_PORCH 3
75 
76     /* 4) Minimum number of vertical back porch lines - default 6 */
77 #define CVT_MIN_V_BPORCH 6
78 
79     /* Pixel Clock step (kHz) */
80 #define CVT_CLOCK_STEP 250
81 
82     Bool Margins = FALSE;
83     float VFieldRate, HPeriod;
84     int HDisplayRnd, HMargin;
85     int VDisplayRnd, VMargin, VSync;
86     float Interlace;            /* Please rename this */
87     char name[128];
88     xRRModeInfo modeinfo;
89 
90     memset(&modeinfo, 0, sizeof modeinfo);
91 
92     /* CVT default is 60.0Hz */
93     if (!VRefresh)
94         VRefresh = 60.0;
95 
96     /* 1. Required field rate */
97     if (Interlaced)
98         VFieldRate = VRefresh * 2;
99     else
100         VFieldRate = VRefresh;
101 
102     /* 2. Horizontal pixels */
103     HDisplayRnd = HDisplay - (HDisplay % CVT_H_GRANULARITY);
104 
105     /* 3. Determine left and right borders */
106     if (Margins) {
107         /* right margin is actually exactly the same as left */
108         HMargin = (((float) HDisplayRnd) * CVT_MARGIN_PERCENTAGE / 100.0);
109         HMargin -= HMargin % CVT_H_GRANULARITY;
110     }
111     else
112         HMargin = 0;
113 
114     /* 4. Find total active pixels */
115     modeinfo.width = HDisplayRnd + 2 * HMargin;
116 
117     /* 5. Find number of lines per field */
118     if (Interlaced)
119         VDisplayRnd = VDisplay / 2;
120     else
121         VDisplayRnd = VDisplay;
122 
123     /* 6. Find top and bottom margins */
124     /* nope. */
125     if (Margins)
126         /* top and bottom margins are equal again. */
127         VMargin = (((float) VDisplayRnd) * CVT_MARGIN_PERCENTAGE / 100.0);
128     else
129         VMargin = 0;
130 
131     modeinfo.height = VDisplay + 2 * VMargin;
132 
133     /* 7. Interlace */
134     if (Interlaced)
135         Interlace = 0.5;
136     else
137         Interlace = 0.0;
138 
139     /* Determine VSync Width from aspect ratio */
140     if (!(VDisplay % 3) && ((VDisplay * 4 / 3) == HDisplay))
141         VSync = 4;
142     else if (!(VDisplay % 9) && ((VDisplay * 16 / 9) == HDisplay))
143         VSync = 5;
144     else if (!(VDisplay % 10) && ((VDisplay * 16 / 10) == HDisplay))
145         VSync = 6;
146     else if (!(VDisplay % 4) && ((VDisplay * 5 / 4) == HDisplay))
147         VSync = 7;
148     else if (!(VDisplay % 9) && ((VDisplay * 15 / 9) == HDisplay))
149         VSync = 7;
150     else                        /* Custom */
151         VSync = 10;
152 
153     if (!Reduced) {             /* simplified GTF calculation */
154 
155         /* 4) Minimum time of vertical sync + back porch interval (µs)
156          * default 550.0 */
157 #define CVT_MIN_VSYNC_BP 550.0
158 
159         /* 3) Nominal HSync width (% of line period) - default 8 */
160 #define CVT_HSYNC_PERCENTAGE 8
161 
162         float HBlankPercentage;
163         int VSyncAndBackPorch, VBackPorch;
164         int HBlank;
165 
166         /* 8. Estimated Horizontal period */
167         HPeriod = ((float) (1000000.0 / VFieldRate - CVT_MIN_VSYNC_BP)) /
168             (VDisplayRnd + 2 * VMargin + CVT_MIN_V_PORCH + Interlace);
169 
170         /* 9. Find number of lines in sync + backporch */
171         if (((int) (CVT_MIN_VSYNC_BP / HPeriod) + 1) <
172             (VSync + CVT_MIN_V_PORCH))
173             VSyncAndBackPorch = VSync + CVT_MIN_V_PORCH;
174         else
175             VSyncAndBackPorch = (int) (CVT_MIN_VSYNC_BP / HPeriod) + 1;
176 
177         /* 10. Find number of lines in back porch */
178         VBackPorch = VSyncAndBackPorch - VSync;
179         (void) VBackPorch;
180 
181         /* 11. Find total number of lines in vertical field */
182         modeinfo.vTotal =
183             VDisplayRnd + 2 * VMargin + VSyncAndBackPorch + Interlace +
184             CVT_MIN_V_PORCH;
185 
186         /* 5) Definition of Horizontal blanking time limitation */
187         /* Gradient (%/kHz) - default 600 */
188 #define CVT_M_FACTOR 600
189 
190         /* Offset (%) - default 40 */
191 #define CVT_C_FACTOR 40
192 
193         /* Blanking time scaling factor - default 128 */
194 #define CVT_K_FACTOR 128
195 
196         /* Scaling factor weighting - default 20 */
197 #define CVT_J_FACTOR 20
198 
199 #define CVT_M_PRIME CVT_M_FACTOR * CVT_K_FACTOR / 256
200 #define CVT_C_PRIME (CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
201         CVT_J_FACTOR
202 
203         /* 12. Find ideal blanking duty cycle from formula */
204         HBlankPercentage = CVT_C_PRIME - CVT_M_PRIME * HPeriod / 1000.0;
205 
206         /* 13. Blanking time */
207         if (HBlankPercentage < 20)
208             HBlankPercentage = 20;
209 
210         HBlank = modeinfo.width * HBlankPercentage / (100.0 - HBlankPercentage);
211         HBlank -= HBlank % (2 * CVT_H_GRANULARITY);
212 
213         /* 14. Find total number of pixels in a line. */
214         modeinfo.hTotal = modeinfo.width + HBlank;
215 
216         /* Fill in HSync values */
217         modeinfo.hSyncEnd = modeinfo.width + HBlank / 2;
218 
219         modeinfo.hSyncStart = modeinfo.hSyncEnd -
220             (modeinfo.hTotal * CVT_HSYNC_PERCENTAGE) / 100;
221         modeinfo.hSyncStart += CVT_H_GRANULARITY -
222             modeinfo.hSyncStart % CVT_H_GRANULARITY;
223 
224         /* Fill in VSync values */
225         modeinfo.vSyncStart = modeinfo.height + CVT_MIN_V_PORCH;
226         modeinfo.vSyncEnd = modeinfo.vSyncStart + VSync;
227 
228     }
229     else {                      /* Reduced blanking */
230         /* Minimum vertical blanking interval time (µs) - default 460 */
231 #define CVT_RB_MIN_VBLANK 460.0
232 
233         /* Fixed number of clocks for horizontal sync */
234 #define CVT_RB_H_SYNC 32.0
235 
236         /* Fixed number of clocks for horizontal blanking */
237 #define CVT_RB_H_BLANK 160.0
238 
239         /* Fixed number of lines for vertical front porch - default 3 */
240 #define CVT_RB_VFPORCH 3
241 
242         int VBILines;
243 
244         /* 8. Estimate Horizontal period. */
245         HPeriod = ((float) (1000000.0 / VFieldRate - CVT_RB_MIN_VBLANK)) /
246             (VDisplayRnd + 2 * VMargin);
247 
248         /* 9. Find number of lines in vertical blanking */
249         VBILines = ((float) CVT_RB_MIN_VBLANK) / HPeriod + 1;
250 
251         /* 10. Check if vertical blanking is sufficient */
252         if (VBILines < (CVT_RB_VFPORCH + VSync + CVT_MIN_V_BPORCH))
253             VBILines = CVT_RB_VFPORCH + VSync + CVT_MIN_V_BPORCH;
254 
255         /* 11. Find total number of lines in vertical field */
256         modeinfo.vTotal = VDisplayRnd + 2 * VMargin + Interlace + VBILines;
257 
258         /* 12. Find total number of pixels in a line */
259         modeinfo.hTotal = modeinfo.width + CVT_RB_H_BLANK;
260 
261         /* Fill in HSync values */
262         modeinfo.hSyncEnd = modeinfo.width + CVT_RB_H_BLANK / 2;
263         modeinfo.hSyncStart = modeinfo.hSyncEnd - CVT_RB_H_SYNC;
264 
265         /* Fill in VSync values */
266         modeinfo.vSyncStart = modeinfo.height + CVT_RB_VFPORCH;
267         modeinfo.vSyncEnd = modeinfo.vSyncStart + VSync;
268     }
269 
270     /* 15/13. Find pixel clock frequency (kHz for xf86) */
271     modeinfo.dotClock = modeinfo.hTotal * 1000.0 / HPeriod;
272     modeinfo.dotClock -= modeinfo.dotClock % CVT_CLOCK_STEP;
273     modeinfo.dotClock *= 1000.0;
274 #if 0
275     /* 16/14. Find actual Horizontal Frequency (kHz) */
276     modeinfo.hSync = ((float) modeinfo.dotClock) / ((float) modeinfo.hTotal);
277 #endif
278 
279 #if 0
280     /* 17/15. Find actual Field rate */
281     modeinfo.vRefresh = (1000.0 * ((float) modeinfo.dotClock)) /
282         ((float) (modeinfo.hTotal * modeinfo.vTotal));
283 #endif
284 
285     /* 18/16. Find actual vertical frame frequency */
286     /* ignore - just set the mode flag for interlaced */
287     if (Interlaced)
288         modeinfo.vTotal *= 2;
289 
290     if (Reduced)
291         modeinfo.modeFlags |= RR_HSyncPositive | RR_VSyncNegative;
292     else
293         modeinfo.modeFlags |= RR_HSyncNegative | RR_VSyncPositive;
294 
295     if (Interlaced)
296         modeinfo.modeFlags |= RR_Interlace;
297 
298     /* FWXGA hack adapted from hw/xfree86/modes/xf86EdidModes.c, because you can't say 1366 */
299     if (HDisplay == 1366 && VDisplay == 768) {
300          modeinfo.width = 1366;
301          modeinfo.hSyncStart--;
302          modeinfo.hSyncEnd--;
303     }
304 
305     snprintf(name, sizeof name, "%dx%d",
306              modeinfo.width, modeinfo.height);
307     modeinfo.nameLength = strlen(name);
308 
309     return RRModeGet(&modeinfo, name);
310 }
311