Initial implementation of experimental gamma ramp API.

This commit is contained in:
Camilla Berglund 2010-10-13 04:04:43 +02:00
parent ffba674dbf
commit 2630d4968c
16 changed files with 647 additions and 31 deletions

View File

@ -20,6 +20,7 @@ set(common_SOURCES
${GLFW_SOURCE_DIR}/src/enable.c ${GLFW_SOURCE_DIR}/src/enable.c
${GLFW_SOURCE_DIR}/src/error.c ${GLFW_SOURCE_DIR}/src/error.c
${GLFW_SOURCE_DIR}/src/fullscreen.c ${GLFW_SOURCE_DIR}/src/fullscreen.c
${GLFW_SOURCE_DIR}/src/gamma.c
${GLFW_SOURCE_DIR}/src/glext.c ${GLFW_SOURCE_DIR}/src/glext.c
${GLFW_SOURCE_DIR}/src/init.c ${GLFW_SOURCE_DIR}/src/init.c
${GLFW_SOURCE_DIR}/src/input.c ${GLFW_SOURCE_DIR}/src/input.c
@ -67,14 +68,12 @@ if (UNIX AND NOT APPLE AND NOT CYGWIN)
endif(X11_XRANDR_FOUND) endif(X11_XRANDR_FOUND)
# Check for Xf86VidMode (fallback legacy resolution switching extension) # Check for Xf86VidMode (fallback legacy resolution switching extension)
if (NOT X11_XRANDR_FOUND) CHECK_X11_XF86VIDMODE()
CHECK_X11_XF86VIDMODE() if (X11_XF86VIDMODE_FOUND)
if (X11_XF86VIDMODE_FOUND) set(_GLFW_HAS_XF86VIDMODE 1)
set(_GLFW_HAS_XF86VIDMODE 1) list(APPEND GLFW_INCLUDE_DIR ${X11_XF86VIDMODE_INCLUDE_DIR})
list(APPEND GLFW_INCLUDE_DIR ${X11_XF86VIDMODE_INCLUDE_DIR}) list(APPEND GLFW_LIBRARIES ${X11_XF86VIDMODE_LIBRARIES})
list(APPEND GLFW_LIBRARIES ${X11_XF86VIDMODE_LIBRARIES}) endif(X11_XF86VIDMODE_FOUND)
endif(X11_XF86VIDMODE_FOUND)
endif (NOT X11_XRANDR_FOUND)
CHECK_FUNCTION_EXISTS(glXGetProcAddress _GLFW_HAS_GLXGETPROCADDRESS) CHECK_FUNCTION_EXISTS(glXGetProcAddress _GLFW_HAS_GLXGETPROCADDRESS)

View File

@ -361,6 +361,8 @@ extern "C" {
#define GLFW_VERSION_UNAVAILABLE 0x00070007 #define GLFW_VERSION_UNAVAILABLE 0x00070007
#define GLFW_PLATFORM_ERROR 0x00070008 #define GLFW_PLATFORM_ERROR 0x00070008
/* Gamma ramps */
#define GLFW_GAMMA_RAMP_SIZE 256
/************************************************************************* /*************************************************************************
* Typedefs * Typedefs
@ -379,6 +381,14 @@ typedef struct
int greenBits; int greenBits;
} GLFWvidmode; } GLFWvidmode;
/* Gamma ramp */
typedef struct
{
unsigned short red[GLFW_GAMMA_RAMP_SIZE];
unsigned short green[GLFW_GAMMA_RAMP_SIZE];
unsigned short blue[GLFW_GAMMA_RAMP_SIZE];
} GLFWgammaramp;
/* Function pointer types */ /* Function pointer types */
typedef void (* GLFWwindowsizefun)(GLFWwindow,int,int); typedef void (* GLFWwindowsizefun)(GLFWwindow,int,int);
typedef int (* GLFWwindowclosefun)(GLFWwindow); typedef int (* GLFWwindowclosefun)(GLFWwindow);
@ -410,6 +420,11 @@ GLFWAPI const char* glfwErrorString(int error);
GLFWAPI int glfwGetVideoModes(GLFWvidmode* list, int maxcount); GLFWAPI int glfwGetVideoModes(GLFWvidmode* list, int maxcount);
GLFWAPI void glfwGetDesktopMode(GLFWvidmode* mode); GLFWAPI void glfwGetDesktopMode(GLFWvidmode* mode);
/* Gamma ramp functions */
GLFWAPI void glfwSetGammaFormula(float gamma, float blacklevel, float gain);
GLFWAPI void glfwGetGammaRamp(GLFWgammaramp* ramp);
GLFWAPI void glfwSetGammaRamp(const GLFWgammaramp* ramp);
/* Window handling */ /* Window handling */
GLFWAPI GLFWwindow glfwOpenWindow(int width, int height, int mode, const char* title, GLFWwindow share); GLFWAPI GLFWwindow glfwOpenWindow(int width, int height, int mode, const char* title, GLFWwindow share);
GLFWAPI void glfwOpenWindowHint(int target, int hint); GLFWAPI void glfwOpenWindowHint(int target, int hint);

View File

@ -802,6 +802,9 @@ their skills. Special thanks go out to:</p>
<li>Jonathan Dummer, for submitting a patch fixing an input bug on Win32 and <li>Jonathan Dummer, for submitting a patch fixing an input bug on Win32 and
adding logic for the <code>GLFW_ICON</code> resource</li> adding logic for the <code>GLFW_ICON</code> resource</li>
<li>Ralph Eastwood, for the initial design of the gamma correction API and
the Win32 and X11 implementations of it</li>
<li>Gerald Franz, who made GLFW compile under IRIX, and supplied patches <li>Gerald Franz, who made GLFW compile under IRIX, and supplied patches
for the X11 keyboard translation routine</li> for the X11 keyboard translation routine</li>

115
src/gamma.c Normal file
View File

@ -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;
}

View File

@ -215,12 +215,16 @@ struct _GLFWwindow
//------------------------------------------------------------------------ //------------------------------------------------------------------------
struct _GLFWlibrary struct _GLFWlibrary
{ {
_GLFWhints hints; _GLFWhints hints;
_GLFWwindow* windowListHead; _GLFWwindow* windowListHead;
_GLFWwindow* currentWindow; _GLFWwindow* currentWindow;
_GLFWwindow* activeWindow; _GLFWwindow* activeWindow;
_GLFWwindow* cursorLockWindow; _GLFWwindow* cursorLockWindow;
GLFWgammaramp currentRamp;
GLFWgammaramp originalRamp;
int originalRampSize;
_GLFW_PLATFORM_LIBRARY_STATE; _GLFW_PLATFORM_LIBRARY_STATE;
}; };
@ -257,6 +261,10 @@ void _glfwPlatformDisableSystemKeys(_GLFWwindow* window);
int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount); int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount);
void _glfwPlatformGetDesktopMode(GLFWvidmode* mode); void _glfwPlatformGetDesktopMode(GLFWvidmode* mode);
// Gamma ramp
void _glfwPlatformSaveGammaRamp(void);
void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp);
// Joystick // Joystick
int _glfwPlatformGetJoystickParam(int joy, int param); int _glfwPlatformGetJoystickParam(int joy, int param);
int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes); int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes);

View File

@ -23,6 +23,7 @@ set(libglfw_SOURCES
${common_SOURCES} ${common_SOURCES}
win32_enable.c win32_enable.c
win32_fullscreen.c win32_fullscreen.c
win32_gamma.c
win32_glext.c win32_glext.c
win32_init.c win32_init.c
win32_joystick.c win32_joystick.c

View File

@ -156,6 +156,8 @@ typedef int (WINAPI * DESCRIBEPIXELFORMAT_T) (HDC,int,UINT,LPPIXELFORMATDESCRIP
typedef int (WINAPI * GETPIXELFORMAT_T) (HDC); typedef int (WINAPI * GETPIXELFORMAT_T) (HDC);
typedef BOOL (WINAPI * SETPIXELFORMAT_T) (HDC,int,const PIXELFORMATDESCRIPTOR*); typedef BOOL (WINAPI * SETPIXELFORMAT_T) (HDC,int,const PIXELFORMATDESCRIPTOR*);
typedef BOOL (WINAPI * SWAPBUFFERS_T) (HDC); 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 #endif // _GLFW_NO_DLOAD_GDI32
// winmm.dll function pointer typedefs // winmm.dll function pointer typedefs
@ -174,12 +176,16 @@ typedef DWORD (WINAPI * TIMEGETTIME_T) (void);
#define _glfw_GetPixelFormat _glfwLibrary.Win32.gdi.GetPixelFormat #define _glfw_GetPixelFormat _glfwLibrary.Win32.gdi.GetPixelFormat
#define _glfw_SetPixelFormat _glfwLibrary.Win32.gdi.SetPixelFormat #define _glfw_SetPixelFormat _glfwLibrary.Win32.gdi.SetPixelFormat
#define _glfw_SwapBuffers _glfwLibrary.Win32.gdi.SwapBuffers #define _glfw_SwapBuffers _glfwLibrary.Win32.gdi.SwapBuffers
#define _glfw_GetDeviceGammaRamp _glfwLibrary.Win32.gdi.GetDeviceGammaRamp
#define _glfw_SetDeviceGammaRamp _glfwLibrary.Win32.gdi.SetDeviceGammaRamp
#else #else
#define _glfw_ChoosePixelFormat ChoosePixelFormat #define _glfw_ChoosePixelFormat ChoosePixelFormat
#define _glfw_DescribePixelFormat DescribePixelFormat #define _glfw_DescribePixelFormat DescribePixelFormat
#define _glfw_GetPixelFormat GetPixelFormat #define _glfw_GetPixelFormat GetPixelFormat
#define _glfw_SetPixelFormat SetPixelFormat #define _glfw_SetPixelFormat SetPixelFormat
#define _glfw_SwapBuffers SwapBuffers #define _glfw_SwapBuffers SwapBuffers
#define _glfw_GetDeviceGammaRamp GetDeviceGammaRamp
#define _glfw_SetDeviceGammaRamp SetDeviceGammaRamp
#endif // _GLFW_NO_DLOAD_GDI32 #endif // _GLFW_NO_DLOAD_GDI32
// winmm.dll shortcuts // winmm.dll shortcuts
@ -264,6 +270,7 @@ typedef struct _GLFWlibraryWin32
ATOM classAtom; // Window class atom ATOM classAtom; // Window class atom
HHOOK keyboardHook; // Keyboard hook handle HHOOK keyboardHook; // Keyboard hook handle
DWORD foregroundLockTimeout; DWORD foregroundLockTimeout;
HDC desktopDC;
// Default monitor // Default monitor
struct { struct {
@ -291,6 +298,8 @@ typedef struct _GLFWlibraryWin32
GETPIXELFORMAT_T GetPixelFormat; GETPIXELFORMAT_T GetPixelFormat;
SETPIXELFORMAT_T SetPixelFormat; SETPIXELFORMAT_T SetPixelFormat;
SWAPBUFFERS_T SwapBuffers; SWAPBUFFERS_T SwapBuffers;
GETDEVICEGAMMARAMP_T GetDeviceGammaRamp;
SETDEVICEGAMMARAMP_T SetDeviceGammaRamp;
} gdi; } gdi;
#endif // _GLFW_NO_DLOAD_GDI32 #endif // _GLFW_NO_DLOAD_GDI32

67
src/win32/win32_gamma.c Normal file
View File

@ -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]);
}

View File

@ -30,6 +30,8 @@
#include "internal.h" #include "internal.h"
#include <stdlib.h>
#ifdef __BORLANDC__ #ifdef __BORLANDC__
// With the Borland C++ compiler, we want to disable FPU exceptions // With the Borland C++ compiler, we want to disable FPU exceptions
#include <float.h> #include <float.h>
@ -59,12 +61,18 @@ static GLboolean initLibraries(void)
GetProcAddress(_glfwLibrary.Win32.gdi.instance, "SetPixelFormat"); GetProcAddress(_glfwLibrary.Win32.gdi.instance, "SetPixelFormat");
_glfwLibrary.Win32.gdi.SwapBuffers = (SWAPBUFFERS_T) _glfwLibrary.Win32.gdi.SwapBuffers = (SWAPBUFFERS_T)
GetProcAddress(_glfwLibrary.Win32.gdi.instance, "SwapBuffers"); 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 || if (!_glfwLibrary.Win32.gdi.ChoosePixelFormat ||
!_glfwLibrary.Win32.gdi.DescribePixelFormat || !_glfwLibrary.Win32.gdi.DescribePixelFormat ||
!_glfwLibrary.Win32.gdi.GetPixelFormat || !_glfwLibrary.Win32.gdi.GetPixelFormat ||
!_glfwLibrary.Win32.gdi.SetPixelFormat || !_glfwLibrary.Win32.gdi.SetPixelFormat ||
!_glfwLibrary.Win32.gdi.SwapBuffers) !_glfwLibrary.Win32.gdi.SwapBuffers ||
!_glfwLibrary.Win32.gdi.GetDeviceGammaRamp ||
!_glfwLibrary.Win32.gdi.SetDeviceGammaRamp)
{ {
return GL_FALSE; return GL_FALSE;
} }
@ -152,6 +160,19 @@ int _glfwPlatformInit(void)
_glfwLibrary.Win32.instance = GetModuleHandle(NULL); _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(); _glfwInitTimer();
return GL_TRUE; return GL_TRUE;
@ -164,6 +185,13 @@ int _glfwPlatformInit(void)
int _glfwPlatformTerminate(void) 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) if (_glfwLibrary.Win32.classAtom)
{ {
UnregisterClass(_GLFW_WNDCLASSNAME, _glfwLibrary.Win32.instance); UnregisterClass(_GLFW_WNDCLASSNAME, _glfwLibrary.Win32.instance);

View File

@ -12,6 +12,7 @@ set(libglfw_SOURCES
${common_SOURCES} ${common_SOURCES}
x11_enable.c x11_enable.c
x11_fullscreen.c x11_fullscreen.c
x11_gamma.c
x11_glext.c x11_glext.c
x11_init.c x11_init.c
x11_joystick.c x11_joystick.c

View File

@ -52,10 +52,6 @@
#error "GLX header version 1.3 or above is required" #error "GLX header version 1.3 or above is required"
#endif #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 // With XFree86, we can use the XF86VidMode extension
#if defined(_GLFW_HAS_XF86VIDMODE) #if defined(_GLFW_HAS_XF86VIDMODE)
#include <X11/extensions/xf86vmode.h> #include <X11/extensions/xf86vmode.h>
@ -156,15 +152,18 @@ typedef struct _GLFWlibraryX11
int glxMajor, glxMinor; int glxMajor, glxMinor;
struct { struct {
int available; GLboolean available;
int eventBase; int eventBase;
int errorBase; int errorBase;
} XF86VidMode; } XF86VidMode;
struct { struct {
int available; GLboolean available;
int eventBase; int eventBase;
int errorBase; int errorBase;
int majorVersion;
int minorVersion;
GLboolean gammaBroken;
} XRandR; } XRandR;
// Screensaver data // Screensaver data
@ -179,14 +178,13 @@ typedef struct _GLFWlibraryX11
// Fullscreen data // Fullscreen data
struct { struct {
int modeChanged; int modeChanged;
#if defined(_GLFW_HAS_XF86VIDMODE)
XF86VidModeModeInfo oldMode;
#endif
#if defined(_GLFW_HAS_XRANDR) #if defined(_GLFW_HAS_XRANDR)
SizeID oldSizeID; SizeID oldSizeID;
int oldWidth; int oldWidth;
int oldHeight; int oldHeight;
Rotation oldRotation; Rotation oldRotation;
#elif defined(_GLFW_HAS_XF86VIDMODE)
XF86VidModeModeInfo oldMode;
#endif #endif
} FS; } FS;

View File

@ -480,7 +480,7 @@ void _glfwPlatformGetDesktopMode(GLFWvidmode* mode)
{ {
Display* dpy; Display* dpy;
int bpp, screen; int bpp, screen;
#if defined(_GLFW_HAS_XF86VIDMODE) #if !defined(_GLFW_HAS_XRANDR) && defined(_GLFW_HAS_XF86VIDMODE)
XF86VidModeModeInfo** modelist; XF86VidModeModeInfo** modelist;
int modecount; int modecount;
#endif #endif

124
src/x11/x11_gamma.c Normal file
View File

@ -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
}
}

View File

@ -31,6 +31,7 @@
#include "internal.h" #include "internal.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
//======================================================================== //========================================================================
@ -62,7 +63,7 @@ static void initLibraries(void)
//======================================================================== //========================================================================
// Initialize X11 display // Initialize X11 display and look for supported X11 extensions
//======================================================================== //========================================================================
static GLboolean initDisplay(void) static GLboolean initDisplay(void)
@ -88,7 +89,7 @@ static GLboolean initDisplay(void)
&_glfwLibrary.X11.XF86VidMode.eventBase, &_glfwLibrary.X11.XF86VidMode.eventBase,
&_glfwLibrary.X11.XF86VidMode.errorBase); &_glfwLibrary.X11.XF86VidMode.errorBase);
#else #else
_glfwLibrary.X11.XF86VidMode.available = 0; _glfwLibrary.X11.XF86VidMode.available = GL_FALSE;
#endif #endif
// Check for XRandR extension // Check for XRandR extension
@ -97,11 +98,17 @@ static GLboolean initDisplay(void)
XRRQueryExtension(_glfwLibrary.X11.display, XRRQueryExtension(_glfwLibrary.X11.display,
&_glfwLibrary.X11.XRandR.eventBase, &_glfwLibrary.X11.XRandR.eventBase,
&_glfwLibrary.X11.XRandR.errorBase); &_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 #else
_glfwLibrary.X11.XRandR.available = 0; _glfwLibrary.X11.XRandR.available = GL_FALSE;
#endif #endif
// Fullscreen & screen saver settings
// Check if GLX is supported on this display // Check if GLX is supported on this display
if (!glXQueryExtension(_glfwLibrary.X11.display, NULL, NULL)) if (!glXQueryExtension(_glfwLibrary.X11.display, NULL, NULL))
{ {
@ -123,6 +130,58 @@ static GLboolean initDisplay(void)
} }
//========================================================================
// 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) // Create a blank cursor (for locked mouse mode)
//======================================================================== //========================================================================
@ -159,8 +218,15 @@ static Cursor createNULLCursor(void)
static void terminateDisplay(void) static void terminateDisplay(void)
{ {
if (_glfwLibrary.originalRampSize)
{
_glfwPlatformSetGammaRamp(&_glfwLibrary.originalRamp);
}
if (_glfwLibrary.X11.display) if (_glfwLibrary.X11.display)
{ {
_glfwPlatformSetGammaRamp(&_glfwLibrary.originalRamp);
XCloseDisplay(_glfwLibrary.X11.display); XCloseDisplay(_glfwLibrary.X11.display);
_glfwLibrary.X11.display = NULL; _glfwLibrary.X11.display = NULL;
} }
@ -180,6 +246,8 @@ int _glfwPlatformInit(void)
if (!initDisplay()) if (!initDisplay())
return GL_FALSE; return GL_FALSE;
initGammaRamp();
_glfwLibrary.X11.cursor = createNULLCursor(); _glfwLibrary.X11.cursor = createNULLCursor();
// Try to load libGL.so if necessary // Try to load libGL.so if necessary
@ -232,7 +300,8 @@ const char* _glfwPlatformGetVersionString(void)
const char* version = "GLFW " _GLFW_VERSION_FULL const char* version = "GLFW " _GLFW_VERSION_FULL
#if defined(_GLFW_HAS_XRANDR) #if defined(_GLFW_HAS_XRANDR)
" XRandR" " XRandR"
#elif defined(_GLFW_HAS_XF86VIDMODE) #endif
#if defined(_GLFW_HAS_XF86VIDMODE)
" Xf86VidMode" " Xf86VidMode"
#else #else
" (no mode switching support)" " (no mode switching support)"

View File

@ -6,6 +6,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include ${OPENGL_INCLUDE_DIR}
add_executable(defaults defaults.c) add_executable(defaults defaults.c)
add_executable(events events.c) add_executable(events events.c)
add_executable(fsfocus fsfocus.c) add_executable(fsfocus fsfocus.c)
add_executable(gamma gamma.c getopt.c)
add_executable(iconify iconify.c getopt.c) add_executable(iconify iconify.c getopt.c)
add_executable(joysticks joysticks.c) add_executable(joysticks joysticks.c)
add_executable(listmodes listmodes.c) add_executable(listmodes listmodes.c)
@ -30,8 +31,8 @@ else()
endif(APPLE) endif(APPLE)
set(WINDOWS_BINARIES accuracy sharing tearing windows) set(WINDOWS_BINARIES accuracy sharing tearing windows)
set(CONSOLE_BINARIES defaults events fsaa fsfocus iconify joysticks listmodes set(CONSOLE_BINARIES defaults events fsaa fsfocus gamma iconify joysticks
peter reopen version) listmodes peter reopen version)
if(MSVC) if(MSVC)
# Tell MSVC to use main instead of WinMain for Windows subsystem executables # Tell MSVC to use main instead of WinMain for Windows subsystem executables

178
tests/gamma.c Normal file
View File

@ -0,0 +1,178 @@
//========================================================================
// Gamma correction test program
// Copyright (c) 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.
//
//========================================================================
//
// This program is used to test the gamma correction functionality for
// both fullscreen and windowed mode windows
//
//========================================================================
#include <GL/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
#include "getopt.h"
static GLfloat ggamma = 1.0f;
static GLfloat ggain = 1.0f;
static GLfloat gblacklevel = 0.0f;
static void usage(void)
{
printf("Usage: gammatest [-h] [-f]\n");
}
static void key_callback(GLFWwindow window, int key, int action)
{
if (action != GLFW_PRESS)
return;
switch (key)
{
case GLFW_KEY_ESC:
glfwCloseWindow(window);
break;
case 'Q':
ggamma += 0.1f;
printf("Gamma: %f\n", ggamma);
glfwSetGammaFormula( ggamma, gblacklevel, ggain );
break;
case 'W':
ggamma -= 0.1f;
printf("Gamma: %f\n", ggamma);
glfwSetGammaFormula( ggamma, gblacklevel, ggain );
break;
case 'A':
ggain += 0.1f;
printf("Gain: %f\n", ggain);
glfwSetGammaFormula( ggamma, gblacklevel, ggain );
break;
case 'S':
ggain -= 0.1f;
printf("Gain: %f\n", ggain);
glfwSetGammaFormula( ggamma, gblacklevel, ggain );
break;
case 'Z':
gblacklevel += 0.1f;
printf("Black Level: %f\n", gblacklevel);
glfwSetGammaFormula( ggamma, gblacklevel, ggain );
break;
case 'X':
gblacklevel -= 0.1f;
printf("Black Level: %f\n", gblacklevel);
glfwSetGammaFormula( ggamma, gblacklevel, ggain );
break;
}
}
static void size_callback(GLFWwindow window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main(int argc, char** argv)
{
int width, height, ch;
int mode = GLFW_WINDOWED;
GLFWwindow window;
while ((ch = getopt(argc, argv, "fh")) != -1)
{
switch (ch)
{
case 'h':
usage();
exit(EXIT_SUCCESS);
case 'f':
mode = GLFW_FULLSCREEN;
break;
default:
usage();
exit(EXIT_FAILURE);
}
}
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW: %s\n", glfwErrorString(glfwGetError()));
exit(EXIT_FAILURE);
}
if (mode == GLFW_FULLSCREEN)
{
GLFWvidmode mode;
glfwGetDesktopMode(&mode);
width = mode.width;
height = mode.height;
}
else
{
width = 0;
height = 0;
}
window = glfwOpenWindow(width, height, mode, "Gamma Test", NULL);
if (!window)
{
glfwTerminate();
fprintf(stderr, "Failed to open GLFW window: %s\n", glfwErrorString(glfwGetError()));
exit(EXIT_FAILURE);
}
printf("Gamma: %f\nGain: %f\nBlack Level: %f\n",
ggamma, ggain, gblacklevel);
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
glfwSetWindowSizeCallback(window, size_callback);
glEnable(GL_SCISSOR_TEST);
while (glfwIsWindow(window))
{
int width, height;
glfwGetWindowSize(window, &width, &height);
glScissor(0, 0, width, height);
glClearColor(0.5f, 0.5f, 0.5f, 0);
glClear(GL_COLOR_BUFFER_BIT);
glScissor(0, 0, 640, 480);
glClearColor(0.8f, 0.2f, 0.4f, 0);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers();
glfwPollEvents();
}
glfwTerminate();
exit(EXIT_SUCCESS);
}