Added refresh rate to GLFWvidmode.

This commit is contained in:
Camilla Berglund 2013-05-30 15:52:42 +02:00
parent 8a683c02aa
commit 731812cfc1
6 changed files with 80 additions and 53 deletions

View File

@ -802,6 +802,9 @@ typedef struct
/*! The bit depth of the blue channel of the video mode. /*! The bit depth of the blue channel of the video mode.
*/ */
int blueBits; int blueBits;
/*! The refresh rate, in Hz, of the video mode.
*/
int refreshRate;
} GLFWvidmode; } GLFWvidmode;
/*! @brief Gamma ramp. /*! @brief Gamma ramp.

View File

@ -102,6 +102,7 @@ static GLFWvidmode vidmodeFromCGDisplayMode(CGDisplayModeRef mode)
GLFWvidmode result; GLFWvidmode result;
result.width = CGDisplayModeGetWidth(mode); result.width = CGDisplayModeGetWidth(mode);
result.height = CGDisplayModeGetHeight(mode); result.height = CGDisplayModeGetHeight(mode);
result.refreshRate = (int) CGDisplayModeGetRefreshRate(mode);
CFStringRef format = CGDisplayModeCopyPixelEncoding(mode); CFStringRef format = CGDisplayModeCopyPixelEncoding(mode);
@ -157,7 +158,7 @@ GLboolean _glfwSetVideoMode(_GLFWmonitor* monitor, const GLFWvidmode* desired)
CGDisplayModeRef bestMode = NULL; CGDisplayModeRef bestMode = NULL;
CFArrayRef modes; CFArrayRef modes;
CFIndex count, i; CFIndex count, i;
unsigned int leastSizeDiff = UINT_MAX; unsigned int leastSizeDiff = UINT_MAX, leastRateDiff = UINT_MAX;
const int bpp = desired->redBits - desired->greenBits - desired->blueBits; const int bpp = desired->redBits - desired->greenBits - desired->blueBits;
modes = CGDisplayCopyAllDisplayModes(monitor->ns.displayID, NULL); modes = CGDisplayCopyAllDisplayModes(monitor->ns.displayID, NULL);
@ -183,17 +184,22 @@ GLboolean _glfwSetVideoMode(_GLFWmonitor* monitor, const GLFWvidmode* desired)
CFRelease(format); CFRelease(format);
} }
int modeWidth = (int) CGDisplayModeGetWidth(mode); const int modeWidth = (int) CGDisplayModeGetWidth(mode);
int modeHeight = (int) CGDisplayModeGetHeight(mode); const int modeHeight = (int) CGDisplayModeGetHeight(mode);
const int modeRate = (int) CGDisplayModeGetRefreshRate(mode);
unsigned int sizeDiff = (abs(modeBPP - bpp) << 25) | unsigned int sizeDiff = (abs(modeBPP - bpp) << 25) |
((modeWidth - desired->width) * (modeWidth - desired->width) + ((modeWidth - desired->width) * (modeWidth - desired->width) +
(modeHeight - desired->height) * (modeHeight - desired->height)); (modeHeight - desired->height) * (modeHeight - desired->height));
if (sizeDiff < leastSizeDiff) const unsigned int rateDiff = modeRate - desired->refreshRate;
if ((sizeDiff < leastSizeDiff) ||
(sizeDiff == leastSizeDiff && rateDiff < leastRateDiff))
{ {
bestMode = mode; bestMode = mode;
leastSizeDiff = sizeDiff; leastSizeDiff = sizeDiff;
leastRateDiff = rateDiff;
} }
} }

View File

@ -65,7 +65,12 @@ static int compareVideoModes(const void* firstPtr, const void* secondPtr)
firstSize = first->width * first->height; firstSize = first->width * first->height;
secondSize = second->width * second->height; secondSize = second->width * second->height;
return firstSize - secondSize; if (firstSize != secondSize)
return firstSize - secondSize;
// Lastly sort on refresh rate
return first->refreshRate - second->refreshRate;
} }
// Retrieves the available modes for the specified monitor // Retrieves the available modes for the specified monitor
@ -205,6 +210,7 @@ const GLFWvidmode* _glfwChooseVideoMode(_GLFWmonitor* monitor,
{ {
int i; int i;
unsigned int sizeDiff, leastSizeDiff = UINT_MAX; unsigned int sizeDiff, leastSizeDiff = UINT_MAX;
unsigned int rateDiff, leastRateDiff = UINT_MAX;
unsigned int colorDiff, leastColorDiff = UINT_MAX; unsigned int colorDiff, leastColorDiff = UINT_MAX;
const GLFWvidmode* current; const GLFWvidmode* current;
const GLFWvidmode* closest = NULL; const GLFWvidmode* closest = NULL;
@ -224,11 +230,15 @@ const GLFWvidmode* _glfwChooseVideoMode(_GLFWmonitor* monitor,
(current->height - desired->height) * (current->height - desired->height) *
(current->height - desired->height)); (current->height - desired->height));
rateDiff = abs(current->refreshRate - desired->refreshRate);
if ((colorDiff < leastColorDiff) || if ((colorDiff < leastColorDiff) ||
(colorDiff == leastColorDiff && sizeDiff < leastSizeDiff)) (colorDiff == leastColorDiff && sizeDiff < leastSizeDiff) ||
(colorDiff == leastColorDiff && sizeDiff == leastSizeDiff && rateDiff < leastRateDiff))
{ {
closest = current; closest = current;
leastSizeDiff = sizeDiff; leastSizeDiff = sizeDiff;
leastRateDiff = rateDiff;
leastColorDiff = colorDiff; leastColorDiff = colorDiff;
} }
} }

View File

@ -63,10 +63,12 @@ GLboolean _glfwSetVideoMode(_GLFWmonitor* monitor, const GLFWvidmode* desired)
return GL_TRUE; return GL_TRUE;
dm.dmSize = sizeof(DEVMODE); dm.dmSize = sizeof(DEVMODE);
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL; dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL |
dm.dmPelsWidth = best->width; DM_DISPLAYFREQUENCY;
dm.dmPelsHeight = best->height; dm.dmPelsWidth = best->width;
dm.dmBitsPerPel = best->redBits + best->greenBits + best->blueBits; dm.dmPelsHeight = best->height;
dm.dmBitsPerPel = best->redBits + best->greenBits + best->blueBits;
dm.dmDisplayFrequency = best->refreshRate;
if (dm.dmBitsPerPel < 15 || dm.dmBitsPerPel >= 24) if (dm.dmBitsPerPel < 15 || dm.dmBitsPerPel >= 24)
dm.dmBitsPerPel = 32; dm.dmBitsPerPel = 32;
@ -223,8 +225,9 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found)
continue; continue;
} }
mode.width = dm.dmPelsWidth; mode.width = dm.dmPelsWidth;
mode.height = dm.dmPelsHeight; mode.height = dm.dmPelsHeight;
mode.refreshRate = dm.dmDisplayFrequency;
_glfwSplitBPP(dm.dmBitsPerPel, _glfwSplitBPP(dm.dmBitsPerPel,
&mode.redBits, &mode.redBits,
&mode.greenBits, &mode.greenBits,
@ -270,6 +273,7 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
mode->width = dm.dmPelsWidth; mode->width = dm.dmPelsWidth;
mode->height = dm.dmPelsHeight; mode->height = dm.dmPelsHeight;
mode->refreshRate = dm.dmDisplayFrequency;
_glfwSplitBPP(dm.dmBitsPerPel, _glfwSplitBPP(dm.dmBitsPerPel,
&mode->redBits, &mode->redBits,
&mode->greenBits, &mode->greenBits,

View File

@ -35,6 +35,28 @@
#include <string.h> #include <string.h>
static int calculateRefreshRate(const XRRModeInfo* mi)
{
if (!mi->hTotal || !mi->vTotal)
return 0;
return (int) ((double) mi->dotClock / ((double) mi->hTotal * (double) mi->vTotal));
}
static const XRRModeInfo* getModeInfo(const XRRScreenResources* sr, RRMode id)
{
int i;
for (i = 0; i < sr->nmode; i++)
{
if (sr->modes[i].id == id)
return sr->modes + i;
}
return NULL;
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
////// GLFW internal API ////// ////// GLFW internal API //////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -45,52 +67,35 @@ void _glfwSetVideoMode(_GLFWmonitor* monitor, const GLFWvidmode* desired)
{ {
if (_glfw.x11.randr.available) if (_glfw.x11.randr.available)
{ {
int i, j, k; int i;
XRRScreenResources* sr; XRRScreenResources* sr;
XRRCrtcInfo* ci; XRRCrtcInfo* ci;
RRMode bestMode = 0; RRMode bestMode = 0;
unsigned int leastSizeDiff = UINT_MAX; unsigned int leastSizeDiff = UINT_MAX, leastRateDiff = UINT_MAX;
sr = XRRGetScreenResources(_glfw.x11.display, _glfw.x11.root); sr = XRRGetScreenResources(_glfw.x11.display, _glfw.x11.root);
ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc); ci = XRRGetCrtcInfo(_glfw.x11.display, sr, monitor->x11.crtc);
for (i = 0; i < sr->nmode; i++) for (i = 0; i < sr->nmode; i++)
{ {
GLboolean usable = GL_TRUE;
XRRModeInfo* mi = sr->modes + i; XRRModeInfo* mi = sr->modes + i;
for (j = 0; j < ci->noutput; j++)
{
XRROutputInfo* oi = XRRGetOutputInfo(_glfw.x11.display,
sr, ci->outputs[j]);
for (k = 0; k < oi->nmode; k++)
{
if (oi->modes[k] == mi->id)
break;
}
if (k == oi->nmode)
usable = GL_FALSE;
XRRFreeOutputInfo(oi);
}
if (!usable)
continue;
if (mi->modeFlags & RR_Interlace) if (mi->modeFlags & RR_Interlace)
continue; continue;
unsigned int sizeDiff = (mi->width - desired->width) * const unsigned int sizeDiff = (mi->width - desired->width) *
(mi->width - desired->width) + (mi->width - desired->width) +
(mi->height - desired->height) * (mi->height - desired->height) *
(mi->height - desired->height); (mi->height - desired->height);
if (sizeDiff < leastSizeDiff) const unsigned int rateDiff = abs(calculateRefreshRate(mi) - desired->refreshRate);
if ((sizeDiff < leastSizeDiff) ||
(sizeDiff == leastSizeDiff && rateDiff < leastRateDiff))
{ {
bestMode = mi->id; bestMode = mi->id;
leastSizeDiff = sizeDiff; leastSizeDiff = sizeDiff;
leastRateDiff = rateDiff;
} }
} }
@ -282,23 +287,17 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found)
for (i = 0; i < oi->nmode; i++) for (i = 0; i < oi->nmode; i++)
{ {
GLFWvidmode mode; GLFWvidmode mode;
const XRRModeInfo* mi = getModeInfo(sr, oi->modes[i]);
for (j = 0; j < sr->nmode; j++) mode.width = mi->width;
{ mode.height = mi->height;
if (sr->modes[j].id == oi->modes[i]) mode.refreshRate = calculateRefreshRate(mi);
break;
}
if (j == sr->nmode)
continue;
mode.width = sr->modes[j].width;
mode.height = sr->modes[j].height;
for (j = 0; j < *found; j++) for (j = 0; j < *found; j++)
{ {
if (result[j].width == mode.width && if (result[j].width == mode.width &&
result[j].height == mode.height) result[j].height == mode.height &&
result[j].refreshRate == mode.refreshRate)
{ {
break; break;
} }
@ -332,6 +331,7 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found)
result[0].redBits = r; result[0].redBits = r;
result[0].greenBits = g; result[0].greenBits = g;
result[0].blueBits = b; result[0].blueBits = b;
result[0].refreshRate = 0;
} }
return result; return result;
@ -350,6 +350,8 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
mode->width = ci->width; mode->width = ci->width;
mode->height = ci->height; mode->height = ci->height;
mode->refreshRate = calculateRefreshRate(getModeInfo(sr, ci->mode));
XRRFreeCrtcInfo(ci); XRRFreeCrtcInfo(ci);
XRRFreeScreenResources(sr); XRRFreeScreenResources(sr);
} }
@ -357,6 +359,7 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
{ {
mode->width = DisplayWidth(_glfw.x11.display, _glfw.x11.screen); mode->width = DisplayWidth(_glfw.x11.display, _glfw.x11.screen);
mode->height = DisplayHeight(_glfw.x11.display, _glfw.x11.screen); mode->height = DisplayHeight(_glfw.x11.display, _glfw.x11.screen);
mode->refreshRate = 0;
} }
_glfwSplitBPP(DefaultDepth(_glfw.x11.display, _glfw.x11.screen), _glfwSplitBPP(DefaultDepth(_glfw.x11.display, _glfw.x11.screen),

View File

@ -52,10 +52,11 @@ static const char* format_mode(const GLFWvidmode* mode)
static char buffer[512]; static char buffer[512];
sprintf(buffer, sprintf(buffer,
"%i x %i x %i (%i %i %i)", "%i x %i x %i (%i %i %i) %i Hz",
mode->width, mode->height, mode->width, mode->height,
mode->redBits + mode->greenBits + mode->blueBits, mode->redBits + mode->greenBits + mode->blueBits,
mode->redBits, mode->greenBits, mode->blueBits); mode->redBits, mode->greenBits, mode->blueBits,
mode->refreshRate);
buffer[sizeof(buffer) - 1] = '\0'; buffer[sizeof(buffer) - 1] = '\0';
return buffer; return buffer;