2 * DirectDraw XVidMode interface
4 * Copyright 2001 TransGaming Technologies, Inc.
10 /* FIXME: ChangeDisplaySettings ought to be able to use this */
12 #ifdef HAVE_LIBXXF86VM
15 #include "ts_xf86vmode.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(x11drv
);
27 static int xf86vm_event
, xf86vm_error
, xf86vm_major
, xf86vm_minor
;
29 LPDDHALMODEINFO xf86vm_modes
;
30 unsigned xf86vm_mode_count
;
31 XF86VidModeModeInfo
** modes
;
33 #define CONVERT_MODE(dotclock) \
34 info->dwWidth = mode->hdisplay; \
35 info->dwHeight = mode->vdisplay; \
36 info->wRefreshRate = dotclock * 1000 / (mode->htotal * mode->vtotal); \
37 TRACE(" width=%ld, height=%ld, refresh=%d\n", \
38 info->dwWidth, info->dwHeight, info->wRefreshRate); \
39 /* XVidMode cannot change display depths... */ \
40 /* let's not bother with filling out these then... */ \
44 info->dwRBitMask = 0; \
45 info->dwGBitMask = 0; \
46 info->dwBBitMask = 0; \
47 info->dwAlphaBitMask = 0;
49 static void convert_modeinfo(XF86VidModeModeInfo
*mode
, LPDDHALMODEINFO info
)
51 CONVERT_MODE(mode
->dotclock
)
54 static void convert_modeline(int dotclock
, XF86VidModeModeLine
*mode
, LPDDHALMODEINFO info
)
56 CONVERT_MODE(dotclock
)
59 void X11DRV_XF86VM_Init(void)
63 if (xf86vm_major
) return; /* already initialized? */
65 /* see if XVidMode is available */
66 if (!TSXF86VidModeQueryExtension(display
, &xf86vm_event
, &xf86vm_error
)) return;
67 if (!TSXF86VidModeQueryVersion(display
, &xf86vm_major
, &xf86vm_minor
)) return;
69 /* if in desktop mode, don't use XVidMode */
70 if (X11DRV_GetXRootWindow() != DefaultRootWindow(display
)) return;
73 if (!TSXF86VidModeGetAllModeLines(display
, DefaultScreen(display
), &nmodes
,
77 TRACE("XVidMode modes: count=%d\n", nmodes
);
79 xf86vm_mode_count
= nmodes
;
80 xf86vm_modes
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(DDHALMODEINFO
) * nmodes
);
82 /* convert modes to DDHALMODEINFO format */
83 for (i
=0; i
<nmodes
; i
++)
84 convert_modeinfo(modes
[i
], &xf86vm_modes
[i
]);
86 TRACE("Enabling XVidMode\n");
89 void X11DRV_XF86VM_Cleanup(void)
91 if (modes
) TSXFree(modes
);
94 int X11DRV_XF86VM_GetCurrentMode(void)
96 XF86VidModeModeLine line
;
100 if (!xf86vm_modes
) return 0; /* no XVidMode */
102 TRACE("Querying XVidMode current mode\n");
103 TSXF86VidModeGetModeLine(display
, DefaultScreen(display
), &dotclock
, &line
);
104 convert_modeline(dotclock
, &line
, &cmode
);
105 for (i
=0; i
<xf86vm_mode_count
; i
++)
106 if (memcmp(&xf86vm_modes
[i
], &cmode
, sizeof(cmode
)) == 0) {
107 TRACE("mode=%d\n", i
);
110 ERR("unknown mode, shouldn't happen\n");
111 return 0; /* return first mode */
114 void X11DRV_XF86VM_SetCurrentMode(int mode
)
116 if (!xf86vm_modes
) return; /* no XVidMode */
118 TSXF86VidModeSwitchToMode(display
, DefaultScreen(display
), modes
[mode
]);
119 TSXF86VidModeSetViewPort(display
, DefaultScreen(display
), 0, 0);
120 TSXSync(display
, False
);
123 void X11DRV_XF86VM_SetExclusiveMode(int lock
)
125 if (!xf86vm_modes
) return; /* no XVidMode */
127 TSXF86VidModeLockModeSwitch(display
, DefaultScreen(display
), lock
);
130 /* actual DirectDraw HAL stuff */
132 static DWORD PASCAL
X11DRV_XF86VM_SetMode(LPDDHAL_SETMODEDATA data
)
134 X11DRV_XF86VM_SetCurrentMode(data
->dwModeIndex
);
135 X11DRV_DDHAL_SwitchMode(data
->dwModeIndex
, NULL
);
136 data
->ddRVal
= DD_OK
;
137 return DDHAL_DRIVER_HANDLED
;
140 int X11DRV_XF86VM_CreateDriver(LPDDHALINFO info
)
142 if (!xf86vm_mode_count
) return 0; /* no XVidMode */
144 info
->dwNumModes
= xf86vm_mode_count
;
145 info
->lpModeInfo
= xf86vm_modes
;
146 X11DRV_DDHAL_SwitchMode(X11DRV_XF86VM_GetCurrentMode(), NULL
);
147 info
->lpDDCallbacks
->SetMode
= X11DRV_XF86VM_SetMode
;
152 /***** GAMMA CONTROL *****/
153 /* (only available in XF86VidMode 2.x) */
155 #ifdef X_XF86VidModeSetGamma
157 static void GenerateRampFromGamma(WORD ramp
[256], float gamma
)
159 float r_gamma
= 1/gamma
;
161 TRACE("gamma is %f\n", r_gamma
);
162 for (i
=0; i
<256; i
++)
163 ramp
[i
] = pow(i
/255.0, r_gamma
) * 65535.0;
166 static BOOL
ComputeGammaFromRamp(WORD ramp
[256], float *gamma
)
168 float r_x
, r_y
, r_lx
, r_ly
, r_d
, r_v
, r_e
, g_avg
, g_min
, g_max
;
169 unsigned i
, f
, l
, g_n
, c
;
173 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f
, l
);
177 g_min
= g_max
= g_avg
= 0.0;
178 /* check gamma ramp entries to estimate the gamma */
179 TRACE("analyzing gamma ramp (%d->%d)\n", f
, l
);
180 for (i
=1, g_n
=0; i
<255; i
++) {
181 if (ramp
[i
] < f
|| ramp
[i
] > l
) {
182 ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i
, ramp
[i
], f
, l
);
186 if (!c
) continue; /* avoid log(0) */
188 /* normalize entry values into 0..1 range */
189 r_x
= i
/255.0; r_y
= c
/ r_d
;
190 /* compute logarithms of values */
191 r_lx
= log(r_x
); r_ly
= log(r_y
);
192 /* compute gamma for this entry */
194 /* compute differential (error estimate) for this entry */
195 /* some games use table-based logarithms that magnifies the error by 128 */
196 r_e
= -r_lx
* 128 / (c
* r_lx
* r_lx
);
198 /* compute min & max while compensating for estimated error */
199 if (!g_n
|| g_min
> (r_v
+ r_e
)) g_min
= r_v
+ r_e
;
200 if (!g_n
|| g_max
< (r_v
- r_e
)) g_max
= r_v
- r_e
;
205 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
208 ERR("no gamma data, shouldn't happen\n");
212 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f
, 65535-l
, g_avg
);
213 /* the bias could be because the app wanted something like a "red shift"
214 * like when you're hit in Quake, but XVidMode doesn't support it,
215 * so we have to reject a significant bias */
216 if (f
&& f
> (pow(1/255.0, g_avg
) * 65536.0)) {
217 ERR("low-biased gamma ramp (%d), rejected\n", f
);
220 /* check that the gamma is reasonably uniform across the ramp */
221 if (g_max
- g_min
> 0.1) {
222 ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max
, g_min
, g_avg
);
225 /* ok, now we're pretty sure we can set the desired gamma ramp,
231 #endif /* X_XF86VidModeSetGamma */
233 /* Hmm... should gamma control be available in desktop mode or not?
234 * I'll assume that it should */
236 BOOL
X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp
)
238 #ifdef X_XF86VidModeSetGamma
239 XF86VidModeGamma gamma
;
242 if (xf86vm_major
< 2) return FALSE
; /* no gamma control */
244 ret
= XF86VidModeGetGamma(display
, DefaultScreen(display
), &gamma
);
247 GenerateRampFromGamma(ramp
->red
, gamma
.red
);
248 GenerateRampFromGamma(ramp
->green
, gamma
.green
);
249 GenerateRampFromGamma(ramp
->blue
, gamma
.blue
);
252 #endif /* X_XF86VidModeSetGamma */
256 BOOL
X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp
)
258 #ifdef X_XF86VidModeSetGamma
259 XF86VidModeGamma gamma
;
261 if (xf86vm_major
< 2) return FALSE
; /* no gamma control */
262 if (ComputeGammaFromRamp(ramp
->red
, &gamma
.red
) &&
263 ComputeGammaFromRamp(ramp
->green
, &gamma
.green
) &&
264 ComputeGammaFromRamp(ramp
->blue
, &gamma
.blue
)) {
267 ret
= XF86VidModeSetGamma(display
, DefaultScreen(display
), &gamma
);
271 #endif /* X_XF86VidModeSetGamma */
275 #endif /* HAVE_LIBXXF86VM */
277 /* FIXME: should move these somewhere appropriate, but probably not before
278 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
279 * they can include xvidmode.h directly */
280 BOOL
X11DRV_GetDeviceGammaRamp(DC
*dc
, LPVOID ramp
)
282 #ifdef HAVE_LIBXXF86VM
283 return X11DRV_XF86VM_GetGammaRamp(ramp
);
289 BOOL
X11DRV_SetDeviceGammaRamp(DC
*dc
, LPVOID ramp
)
291 #ifdef HAVE_LIBXXF86VM
292 return X11DRV_XF86VM_SetGammaRamp(ramp
);