Initial implementation of experimental gamma ramp API.
diff --git a/src/gamma.c b/src/gamma.c new file mode 100644 index 0000000..ff97d60 --- /dev/null +++ b/src/gamma.c
@@ -0,0 +1,115 @@ +//======================================================================== +// GLFW - An OpenGL framework +// Platform: Any +// API version: 3.0 +// WWW: http://www.glfw.org/ +//------------------------------------------------------------------------ +// Copyright (c) 2010 Camilla Berglund <elmindreda@elmindreda.org> +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include "internal.h" + +#include <math.h> +#include <string.h> + + +////////////////////////////////////////////////////////////////////////// +////// GLFW public API ////// +////////////////////////////////////////////////////////////////////////// + +//======================================================================== +// Calculate the gamma ramp table from specified values +//======================================================================== + +GLFWAPI void glfwSetGammaFormula(float gamma, float blacklevel, float gain) +{ + int i, size = 256; + GLFWgammaramp ramp; + + if (!_glfwInitialized) + { + _glfwSetError(GLFW_NOT_INITIALIZED); + return; + } + + for (i = 0; i < size; i++) + { + float value = (float) i / ((float) (size - 1)); + + // Apply gamma + value = pow(value, gamma) * 65535.f + 0.5f; + + // Apply gain + value = gain * (value - 32767.5f) + 32767.5f; + + // Apply black-level + value += blacklevel * 65535.f; + + // Clamp values + if (value < 0.f) + value = 0.f; + else if (value > 65535.f) + value = 65535.f; + + // Set the gamma ramp values + ramp.red[i] = (unsigned short) value; + ramp.green[i] = (unsigned short) value; + ramp.blue[i] = (unsigned short) value; + } + + glfwSetGammaRamp(&ramp); +} + + +//======================================================================== +// Return the currently set gamma ramp +//======================================================================== + +GLFWAPI void glfwGetGammaRamp(GLFWgammaramp* ramp) +{ + if (!_glfwInitialized) + { + _glfwSetError(GLFW_NOT_INITIALIZED); + return; + } + + *ramp = _glfwLibrary.currentRamp; +} + + +//======================================================================== +// Make the specified gamma ramp current +//======================================================================== + +GLFWAPI void glfwSetGammaRamp(const GLFWgammaramp* ramp) +{ + if (!_glfwInitialized) + { + _glfwSetError(GLFW_NOT_INITIALIZED); + return; + } + + _glfwPlatformSetGammaRamp(ramp); + _glfwLibrary.currentRamp = *ramp; +} +
diff --git a/src/internal.h b/src/internal.h index d073d44..fb7fef9 100644 --- a/src/internal.h +++ b/src/internal.h
@@ -215,12 +215,16 @@ //------------------------------------------------------------------------ struct _GLFWlibrary { - _GLFWhints hints; + _GLFWhints hints; - _GLFWwindow* windowListHead; - _GLFWwindow* currentWindow; - _GLFWwindow* activeWindow; - _GLFWwindow* cursorLockWindow; + _GLFWwindow* windowListHead; + _GLFWwindow* currentWindow; + _GLFWwindow* activeWindow; + _GLFWwindow* cursorLockWindow; + + GLFWgammaramp currentRamp; + GLFWgammaramp originalRamp; + int originalRampSize; _GLFW_PLATFORM_LIBRARY_STATE; }; @@ -257,6 +261,10 @@ int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount); void _glfwPlatformGetDesktopMode(GLFWvidmode* mode); +// Gamma ramp +void _glfwPlatformSaveGammaRamp(void); +void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp); + // Joystick int _glfwPlatformGetJoystickParam(int joy, int param); int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes);
diff --git a/src/win32/CMakeLists.txt b/src/win32/CMakeLists.txt index 50b160d..f7df1ff 100644 --- a/src/win32/CMakeLists.txt +++ b/src/win32/CMakeLists.txt
@@ -23,6 +23,7 @@ ${common_SOURCES} win32_enable.c win32_fullscreen.c + win32_gamma.c win32_glext.c win32_init.c win32_joystick.c
diff --git a/src/win32/platform.h b/src/win32/platform.h index 8a2a2ff..fb9107b 100644 --- a/src/win32/platform.h +++ b/src/win32/platform.h
@@ -156,6 +156,8 @@ typedef int (WINAPI * GETPIXELFORMAT_T) (HDC); typedef BOOL (WINAPI * SETPIXELFORMAT_T) (HDC,int,const PIXELFORMATDESCRIPTOR*); typedef BOOL (WINAPI * SWAPBUFFERS_T) (HDC); +typedef BOOL (WINAPI * GETDEVICEGAMMARAMP_T) (HDC,PVOID); +typedef BOOL (WINAPI * SETDEVICEGAMMARAMP_T) (HDC,PVOID); #endif // _GLFW_NO_DLOAD_GDI32 // winmm.dll function pointer typedefs @@ -174,12 +176,16 @@ #define _glfw_GetPixelFormat _glfwLibrary.Win32.gdi.GetPixelFormat #define _glfw_SetPixelFormat _glfwLibrary.Win32.gdi.SetPixelFormat #define _glfw_SwapBuffers _glfwLibrary.Win32.gdi.SwapBuffers +#define _glfw_GetDeviceGammaRamp _glfwLibrary.Win32.gdi.GetDeviceGammaRamp +#define _glfw_SetDeviceGammaRamp _glfwLibrary.Win32.gdi.SetDeviceGammaRamp #else #define _glfw_ChoosePixelFormat ChoosePixelFormat #define _glfw_DescribePixelFormat DescribePixelFormat #define _glfw_GetPixelFormat GetPixelFormat #define _glfw_SetPixelFormat SetPixelFormat #define _glfw_SwapBuffers SwapBuffers +#define _glfw_GetDeviceGammaRamp GetDeviceGammaRamp +#define _glfw_SetDeviceGammaRamp SetDeviceGammaRamp #endif // _GLFW_NO_DLOAD_GDI32 // winmm.dll shortcuts @@ -264,6 +270,7 @@ ATOM classAtom; // Window class atom HHOOK keyboardHook; // Keyboard hook handle DWORD foregroundLockTimeout; + HDC desktopDC; // Default monitor struct { @@ -291,6 +298,8 @@ GETPIXELFORMAT_T GetPixelFormat; SETPIXELFORMAT_T SetPixelFormat; SWAPBUFFERS_T SwapBuffers; + GETDEVICEGAMMARAMP_T GetDeviceGammaRamp; + SETDEVICEGAMMARAMP_T SetDeviceGammaRamp; } gdi; #endif // _GLFW_NO_DLOAD_GDI32
diff --git a/src/win32/win32_gamma.c b/src/win32/win32_gamma.c new file mode 100644 index 0000000..71d2645 --- /dev/null +++ b/src/win32/win32_gamma.c
@@ -0,0 +1,67 @@ +//======================================================================== +// GLFW - An OpenGL framework +// Platform: Win32/WGL +// API version: 2.7 +// WWW: http://www.glfw.org/ +//------------------------------------------------------------------------ +// Copyright (c) 2002-2006 Marcus Geelnard +// Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org> +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include "internal.h" + +#include <limits.h> + + +//************************************************************************ +//**** GLFW internal functions **** +//************************************************************************ + +//======================================================================== +// Save the gamma ramp to our internal copy +//======================================================================== + +void _glfwPlatformSaveGammaRamp(int ramp) +{ + if (!_glfwLibrary.gammaSize) + { + return; + } + _glfw_GetDeviceGammaRamp(_glfwLibrary.Win32.desktopDC, + _glfwLibrary.gammaRamp[ramp]); +} + + +//======================================================================== +// Restore the gamma ramp to our internal copy of the gamma ramp +//======================================================================== + +void _glfwPlatformRestoreGammaRamp(int ramp) +{ + if (!_glfwLibrary.gammaSize) + { + return; + } + _glfw_SetDeviceGammaRamp(_glfwLibrary.Win32.desktopDC, + _glfwLibrary.gammaRamp[ramp]); +}
diff --git a/src/win32/win32_init.c b/src/win32/win32_init.c index 3e71ad6..54939cc 100644 --- a/src/win32/win32_init.c +++ b/src/win32/win32_init.c
@@ -30,6 +30,8 @@ #include "internal.h" +#include <stdlib.h> + #ifdef __BORLANDC__ // With the Borland C++ compiler, we want to disable FPU exceptions #include <float.h> @@ -59,12 +61,18 @@ GetProcAddress(_glfwLibrary.Win32.gdi.instance, "SetPixelFormat"); _glfwLibrary.Win32.gdi.SwapBuffers = (SWAPBUFFERS_T) GetProcAddress(_glfwLibrary.Win32.gdi.instance, "SwapBuffers"); + _glfwLibrary.Win32.gdi.GetDeviceGammaRamp = (GETDEVICEGAMMARAMP_T) + GetProcAddress(_glfwLibrary.Win32.gdi.instance, "GetDeviceGammaRamp"); + _glfwLibrary.Win32.gdi.SetDeviceGammaRamp = (SETDEVICEGAMMARAMP_T) + GetProcAddress(_glfwLibrary.Win32.gdi.instance, "SetDeviceGammaRamp"); if (!_glfwLibrary.Win32.gdi.ChoosePixelFormat || !_glfwLibrary.Win32.gdi.DescribePixelFormat || !_glfwLibrary.Win32.gdi.GetPixelFormat || !_glfwLibrary.Win32.gdi.SetPixelFormat || - !_glfwLibrary.Win32.gdi.SwapBuffers) + !_glfwLibrary.Win32.gdi.SwapBuffers || + !_glfwLibrary.Win32.gdi.GetDeviceGammaRamp || + !_glfwLibrary.Win32.gdi.SetDeviceGammaRamp) { return GL_FALSE; } @@ -152,6 +160,19 @@ _glfwLibrary.Win32.instance = GetModuleHandle(NULL); + // Initialise the internal gamma ramp + _glfwLibrary.gammaSize = 256; + _glfwLibrary.gammaRamp[GLFW_GAMMA_ORIG] = + malloc(256 * sizeof(unsigned short) * 3); + _glfwLibrary.gammaRamp[GLFW_GAMMA_CURR] = + malloc(256 * sizeof(unsigned short) * 3); + + // Get the desktop DC + _glfwLibrary.Win32.desktopDC = GetDC(GetDesktopWindow()); + + // Save the original gamma ramp + _glfwPlatformSaveGammaRamp(GLFW_GAMMA_ORIG); + _glfwInitTimer(); return GL_TRUE; @@ -164,6 +185,13 @@ int _glfwPlatformTerminate(void) { + // Restore the original gamma ramp + _glfwPlatformRestoreGammaRamp(GLFW_GAMMA_ORIG); + + // Free the gamma ramps + free(_glfwLibrary.gammaRamp[GLFW_GAMMA_ORIG]); + free(_glfwLibrary.gammaRamp[GLFW_GAMMA_CURR]); + if (_glfwLibrary.Win32.classAtom) { UnregisterClass(_GLFW_WNDCLASSNAME, _glfwLibrary.Win32.instance);
diff --git a/src/x11/CMakeLists.txt b/src/x11/CMakeLists.txt index 2e20dad..a75adf7 100644 --- a/src/x11/CMakeLists.txt +++ b/src/x11/CMakeLists.txt
@@ -12,6 +12,7 @@ ${common_SOURCES} x11_enable.c x11_fullscreen.c + x11_gamma.c x11_glext.c x11_init.c x11_joystick.c
diff --git a/src/x11/platform.h b/src/x11/platform.h index 1e3668b..5c49777 100644 --- a/src/x11/platform.h +++ b/src/x11/platform.h
@@ -52,10 +52,6 @@ #error "GLX header version 1.3 or above is required" #endif -#if defined(_GLFW_HAS_XF86VIDMODE) && defined(_GLFW_HAS_XRANDR) - #error "Xf86VidMode and RandR extensions cannot both be enabled" -#endif - // With XFree86, we can use the XF86VidMode extension #if defined(_GLFW_HAS_XF86VIDMODE) #include <X11/extensions/xf86vmode.h> @@ -156,15 +152,18 @@ int glxMajor, glxMinor; struct { - int available; + GLboolean available; int eventBase; int errorBase; } XF86VidMode; struct { - int available; + GLboolean available; int eventBase; int errorBase; + int majorVersion; + int minorVersion; + GLboolean gammaBroken; } XRandR; // Screensaver data @@ -179,14 +178,13 @@ // Fullscreen data struct { int modeChanged; -#if defined(_GLFW_HAS_XF86VIDMODE) - XF86VidModeModeInfo oldMode; -#endif #if defined(_GLFW_HAS_XRANDR) SizeID oldSizeID; int oldWidth; int oldHeight; Rotation oldRotation; +#elif defined(_GLFW_HAS_XF86VIDMODE) + XF86VidModeModeInfo oldMode; #endif } FS;
diff --git a/src/x11/x11_fullscreen.c b/src/x11/x11_fullscreen.c index a80ed91..17ce658 100644 --- a/src/x11/x11_fullscreen.c +++ b/src/x11/x11_fullscreen.c
@@ -480,7 +480,7 @@ { Display* dpy; int bpp, screen; -#if defined(_GLFW_HAS_XF86VIDMODE) +#if !defined(_GLFW_HAS_XRANDR) && defined(_GLFW_HAS_XF86VIDMODE) XF86VidModeModeInfo** modelist; int modecount; #endif
diff --git a/src/x11/x11_gamma.c b/src/x11/x11_gamma.c new file mode 100644 index 0000000..779859f --- /dev/null +++ b/src/x11/x11_gamma.c
@@ -0,0 +1,124 @@ +//======================================================================== +// GLFW - An OpenGL framework +// Platform: X11/GLX +// API version: 3.0 +// WWW: http://www.glfw.org/ +//------------------------------------------------------------------------ +// Copyright (c) 2010 Camilla Berglund <elmindreda@elmindreda.org> +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#include "internal.h" + +#include <limits.h> +#include <string.h> + + +//************************************************************************ +//**** GLFW internal functions **** +//************************************************************************ + +//======================================================================== +// Save the original gamma ramp so that we can restore it later +//======================================================================== + +void _glfwPlatformSaveGammaRamp(void) +{ + if (_glfwLibrary.X11.XRandR.available && + !_glfwLibrary.X11.XRandR.gammaBroken) + { +#if defined (_GLFW_HAS_XRANDR) + size_t size = GLFW_GAMMA_RAMP_SIZE * sizeof(unsigned short); + + XRRScreenResources* rr = XRRGetScreenResources(_glfwLibrary.X11.display, + _glfwLibrary.X11.root); + + XRRCrtcGamma* gamma = XRRGetCrtcGamma(_glfwLibrary.X11.display, + rr->crtcs[0]); + + memcpy(_glfwLibrary.originalRamp.red, gamma->red, size); + memcpy(_glfwLibrary.originalRamp.green, gamma->green, size); + memcpy(_glfwLibrary.originalRamp.blue, gamma->blue, size); + + XRRFreeGamma(gamma); + XRRFreeScreenResources(rr); + } +#endif + else if (_glfwLibrary.X11.XF86VidMode.available) + { +#if defined (_GLFW_HAS_XF86VIDMODE) + XF86VidModeGetGammaRamp(_glfwLibrary.X11.display, + _glfwLibrary.X11.screen, + GLFW_GAMMA_RAMP_SIZE, + _glfwLibrary.originalRamp.red, + _glfwLibrary.originalRamp.green, + _glfwLibrary.originalRamp.blue); +#endif + } +} + + +//======================================================================== +// Make the specified gamma ramp current +//======================================================================== + +void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp) +{ + if (_glfwLibrary.X11.XRandR.available && + !_glfwLibrary.X11.XRandR.gammaBroken) + { +#if defined (_GLFW_HAS_XRANDR) + int i; + size_t size = GLFW_GAMMA_RAMP_SIZE * sizeof(unsigned short); + + XRRScreenResources* rr = XRRGetScreenResources(_glfwLibrary.X11.display, + _glfwLibrary.X11.root); + + // Update gamma per monitor + for (i = 0; i < rr->ncrtc; i++) + { + XRRCrtcGamma* gamma = XRRAllocGamma(GLFW_GAMMA_RAMP_SIZE); + + memcpy(gamma->red, ramp->red, size); + memcpy(gamma->green, ramp->green, size); + memcpy(gamma->blue, ramp->blue, size); + + XRRSetCrtcGamma(_glfwLibrary.X11.display, rr->crtcs[i], gamma); + XRRFreeGamma(gamma); + } + + XRRFreeScreenResources(rr); + } +#endif + else if (_glfwLibrary.X11.XF86VidMode.available) + { +#if defined (_GLFW_HAS_XF86VIDMODE) + XF86VidModeSetGammaRamp(_glfwLibrary.X11.display, + _glfwLibrary.X11.screen, + GLFW_GAMMA_RAMP_SIZE, + (unsigned short*) ramp->red, + (unsigned short*) ramp->green, + (unsigned short*) ramp->blue); +#endif + } +} +
diff --git a/src/x11/x11_init.c b/src/x11/x11_init.c index 6eab3c5..b652916 100644 --- a/src/x11/x11_init.c +++ b/src/x11/x11_init.c
@@ -31,6 +31,7 @@ #include "internal.h" #include <stdio.h> +#include <stdlib.h> //======================================================================== @@ -62,7 +63,7 @@ //======================================================================== -// Initialize X11 display +// Initialize X11 display and look for supported X11 extensions //======================================================================== static GLboolean initDisplay(void) @@ -88,7 +89,7 @@ &_glfwLibrary.X11.XF86VidMode.eventBase, &_glfwLibrary.X11.XF86VidMode.errorBase); #else - _glfwLibrary.X11.XF86VidMode.available = 0; + _glfwLibrary.X11.XF86VidMode.available = GL_FALSE; #endif // Check for XRandR extension @@ -97,11 +98,17 @@ XRRQueryExtension(_glfwLibrary.X11.display, &_glfwLibrary.X11.XRandR.eventBase, &_glfwLibrary.X11.XRandR.errorBase); + + if (!XRRQueryVersion(_glfwLibrary.X11.display, + &_glfwLibrary.X11.XRandR.majorVersion, + &_glfwLibrary.X11.XRandR.minorVersion)) + { + fprintf(stderr, "Unable to query RandR version number\n"); + } #else - _glfwLibrary.X11.XRandR.available = 0; + _glfwLibrary.X11.XRandR.available = GL_FALSE; #endif - // Fullscreen & screen saver settings // Check if GLX is supported on this display if (!glXQueryExtension(_glfwLibrary.X11.display, NULL, NULL)) { @@ -124,6 +131,58 @@ //======================================================================== +// Detect gamma ramp support and save original gamma ramp, if available +//======================================================================== + +static void initGammaRamp(void) +{ +#ifdef _GLFW_HAS_XRANDR + // RandR gamma support is only available with version 1.2 and above + if (_glfwLibrary.X11.XRandR.available && + (_glfwLibrary.X11.XRandR.majorVersion > 1 || + _glfwLibrary.X11.XRandR.majorVersion == 1 && + _glfwLibrary.X11.XRandR.minorVersion >= 2)) + { + // FIXME: Assumes that all monitors have the same size gamma tables + // This is reasonable as I suspect the that if they did differ, it + // would imply that setting the gamma size to an arbitary size is + // possible as well. + XRRScreenResources* rr = XRRGetScreenResources(_glfwLibrary.X11.display, + _glfwLibrary.X11.root); + + _glfwLibrary.originalRampSize = XRRGetCrtcGammaSize(_glfwLibrary.X11.display, + rr->crtcs[0]); + if (!_glfwLibrary.originalRampSize) + { + // This is probably Nvidia RandR with broken gamma support + // Flag it as useless and try Xf86VidMode below, if available + _glfwLibrary.X11.XRandR.gammaBroken = GL_TRUE; + } + + XRRFreeScreenResources(rr); + } +#endif + +#if defined(_GLFW_HAS_XF86VIDMODE) + if (_glfwLibrary.X11.XF86VidMode.available && + !_glfwLibrary.originalRampSize) + { + // Get the gamma size using XF86VidMode + XF86VidModeGetGammaRampSize(_glfwLibrary.X11.display, + _glfwLibrary.X11.screen, + &_glfwLibrary.originalRampSize); + } +#endif + + if (!_glfwLibrary.originalRampSize) + fprintf(stderr, "Gamma ramp setting unsupported\n"); + + // Save the original gamma ramp + _glfwPlatformSaveGammaRamp(); +} + + +//======================================================================== // Create a blank cursor (for locked mouse mode) //======================================================================== @@ -159,8 +218,15 @@ static void terminateDisplay(void) { + if (_glfwLibrary.originalRampSize) + { + _glfwPlatformSetGammaRamp(&_glfwLibrary.originalRamp); + } + if (_glfwLibrary.X11.display) { + _glfwPlatformSetGammaRamp(&_glfwLibrary.originalRamp); + XCloseDisplay(_glfwLibrary.X11.display); _glfwLibrary.X11.display = NULL; } @@ -180,6 +246,8 @@ if (!initDisplay()) return GL_FALSE; + initGammaRamp(); + _glfwLibrary.X11.cursor = createNULLCursor(); // Try to load libGL.so if necessary @@ -232,7 +300,8 @@ const char* version = "GLFW " _GLFW_VERSION_FULL #if defined(_GLFW_HAS_XRANDR) " XRandR" -#elif defined(_GLFW_HAS_XF86VIDMODE) +#endif +#if defined(_GLFW_HAS_XF86VIDMODE) " Xf86VidMode" #else " (no mode switching support)"