Skip to content

Commit

Permalink
Refactor: Moved gamma tables to DisplayMode class
Browse files Browse the repository at this point in the history
Since SDL is going away, we cannot rely on it for setting display color
transfer tables. Moved the old gamma code from gl_main.c and added
new native OS X color table routines.
  • Loading branch information
skyjake committed Apr 1, 2012
1 parent 7608ed4 commit 2b4ff61
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 136 deletions.
36 changes: 36 additions & 0 deletions doomsday/engine/mac/src/displaymode_macx.mm
Expand Up @@ -195,3 +195,39 @@ void DisplayMode_Native_Raise(void* nativeHandle)
NSWindow* wnd = [handle window];
[wnd setLevel:CGShieldingWindowLevel()];
}

void DisplayMode_Native_GetColorTransfer(displaycolortransfer_t* colors)
{
const uint size = 256;
CGGammaValue red[size];
CGGammaValue green[size];
CGGammaValue blue[size];
uint32_t count = 0;

CGGetDisplayTransferByTable(kCGDirectMainDisplay, size, red, green, blue, &count);
assert(count == size);

for(uint i = 0; i < size; ++i)
{
colors->table[i] = (unsigned short) (red[i] * 0xffff);
colors->table[i + size] = (unsigned short) (green[i] * 0xffff);
colors->table[i + 2*size] = (unsigned short) (blue[i] * 0xffff);
}
}

void DisplayMode_Native_SetColorTransfer(const displaycolortransfer_t* colors)
{
const uint size = 256;
CGGammaValue red[size];
CGGammaValue green[size];
CGGammaValue blue[size];

for(uint i = 0; i < size; ++i)
{
red[i] = colors->table[i]/float(0xffff);
green[i] = colors->table[i + size]/float(0xffff);
blue[i] = colors->table[i + 2*size]/float(0xffff);
}

CGSetDisplayTransferByTable(kCGDirectMainDisplay, size, red, green, blue);
}
18 changes: 18 additions & 0 deletions doomsday/engine/portable/include/displaymode.h
Expand Up @@ -42,6 +42,10 @@ typedef struct displaymode_s {
int ratioY;
} DisplayMode;

typedef struct displaycolortransfer_s {
unsigned short table[3 * 256]; // 0-255:red, 256-511:green, 512-767:blue (range: 0..ffff)
} displaycolortransfer_t;

/**
* Initializes the DisplayMode class. Enumerates all available display modes and
* saves the current display mode. Must be called at engine startup.
Expand Down Expand Up @@ -114,6 +118,20 @@ boolean DisplayMode_IsEqual(const DisplayMode* a, const DisplayMode* b);
*/
int DisplayMode_Change(const DisplayMode* mode, boolean shouldCapture);

/**
* Gets the current color transfer table.
*
* @param colors Color transfer.
*/
void DisplayMode_GetColorTransfer(displaycolortransfer_t* colors);

/**
* Sets the color transfer table.
*
* @param colors Color transfer.
*/
void DisplayMode_SetColorTransfer(const displaycolortransfer_t* colors);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
6 changes: 5 additions & 1 deletion doomsday/engine/portable/include/displaymode_native.h
Expand Up @@ -34,6 +34,8 @@ extern "C" {

void DisplayMode_Native_Init(void);

void DisplayMode_Native_Shutdown(void);

int DisplayMode_Native_Count(void);

void DisplayMode_Native_GetMode(int index, DisplayMode* mode);
Expand All @@ -42,7 +44,9 @@ void DisplayMode_Native_GetCurrentMode(DisplayMode* mode);

int DisplayMode_Native_Change(const DisplayMode* mode, boolean shouldCapture);

void DisplayMode_Native_Shutdown(void);
void DisplayMode_Native_GetColorTransfer(displaycolortransfer_t* colors);

void DisplayMode_Native_SetColorTransfer(const displaycolortransfer_t* colors);

#ifdef MACOSX
void DisplayMode_Native_Raise(void* handle);
Expand Down
20 changes: 20 additions & 0 deletions doomsday/engine/portable/src/displaymode.cpp
Expand Up @@ -28,6 +28,7 @@
#include <de/Log>

static bool inited = false;
static displaycolortransfer_t originalColorTransfer;

static float differenceToOriginalHz(float hz);

Expand Down Expand Up @@ -149,6 +150,7 @@ int DisplayMode_Init(void)

captured = false;
DisplayMode_Native_Init();
DisplayMode_Native_GetColorTransfer(&originalColorTransfer);

// This is used for sorting the mode set (Hz).
originalMode = Mode::fromCurrent();
Expand Down Expand Up @@ -183,6 +185,8 @@ void DisplayMode_Shutdown(void)
// Back to the original mode.
DisplayMode_Change(&originalMode, false /*release captured*/);

DisplayMode_Native_SetColorTransfer(&originalColorTransfer);

modes.clear();

DisplayMode_Native_Shutdown();
Expand Down Expand Up @@ -272,3 +276,19 @@ int DisplayMode_Change(const DisplayMode* mode, boolean shouldCapture)
captured = shouldCapture;
return DisplayMode_Native_Change(mode, shouldCapture || (originalMode != *mode));
}

void DisplayMode_GetColorTransfer(displaycolortransfer_t* colors)
{
/// @todo Factor in the original color transfer function, which may be
/// set up specifically by the user.

DisplayMode_Native_GetColorTransfer(colors);
}

void DisplayMode_SetColorTransfer(const displaycolortransfer_t* colors)
{
/// @todo Factor in the original color transfer function, which may be
/// set up specifically by the user.

DisplayMode_Native_SetColorTransfer(colors);
}

0 comments on commit 2b4ff61

Please sign in to comment.