/
displaymode.cpp
394 lines (326 loc) · 10.3 KB
/
displaymode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/** @file displaymode.cpp Platform-independent display mode management.
* @ingroup gl
*
* @authors Copyright (c) 2012-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/gui/displaymode.h"
#include "de/gui/displaymode_native.h"
#include <de/App>
#include <de/ArrayValue>
#include <de/DictionaryValue>
#include <de/FunctionValue>
#include <de/LogBuffer>
#include <de/NumberValue>
#include <de/Record>
#include <de/ScriptSystem>
#include <de/TextValue>
#include <vector>
#include <set>
#include <algorithm>
#include <de/math.h>
#include <de/Log>
namespace de {
static bool inited = false;
static Binder * binder = nullptr;
static DisplayColorTransfer originalColorTransfer;
static float differenceToOriginalHz(float hz);
namespace internal {
struct Mode : public DisplayMode
{
Mode()
{
de::zapPtr(static_cast<DisplayMode *>(this));
}
Mode(const DisplayMode& dm)
{
memcpy(static_cast<DisplayMode *>(this), &dm, sizeof(dm));
}
Mode(int i)
{
DisplayMode_Native_GetMode(i, this);
updateRatio();
}
static Mode fromCurrent()
{
Mode m;
DisplayMode_Native_GetCurrentMode(&m);
m.updateRatio();
return m;
}
bool operator == (const Mode& other) const
{
return width == other.width && height == other.height &&
depth == other.depth && fequal(refreshRate, other.refreshRate);
}
bool operator != (const Mode& other) const
{
return !(*this == other);
}
bool operator < (const Mode& b) const
{
if (width == b.width)
{
if (height == b.height)
{
if (depth == b.depth)
{
// The refresh rate that more closely matches the original is preferable.
return differenceToOriginalHz(refreshRate) <
differenceToOriginalHz(b.refreshRate);
}
return depth < b.depth;
}
return height < b.height;
}
return width < b.width;
}
void updateRatio()
{
ratioX = width;
ratioY = height;
float fx;
float fy;
if (width > height)
{
fx = width/float(height);
fy = 1.f;
}
else
{
fx = 1.f;
fy = height/float(width);
}
// Multiply until we arrive at a close enough integer ratio.
for (int mul = 2; mul < de::min(width, height); ++mul)
{
float rx = fx*mul;
float ry = fy*mul;
if (std::abs(rx - de::roundi(rx)) < .01f && std::abs(ry - de::roundi(ry)) < .01f)
{
// This seems good.
ratioX = de::roundi(rx);
ratioY = de::roundi(ry);
break;
}
}
if (ratioX == 8 && ratioY == 5)
{
// This is commonly referred to as 16:10.
ratioX *= 2;
ratioY *= 2;
}
}
void debugPrint() const
{
LOG_GL_VERBOSE("size: %i x %i x %i, rate: %.1f Hz, ratio: %i:%i")
<< width << height << depth << refreshRate << ratioX << ratioY;
}
};
} // namespace internal
using namespace internal;
typedef std::set<Mode> Modes; // note: no duplicates
static Modes modes;
static Mode originalMode;
static bool captured;
static float differenceToOriginalHz(float hz)
{
return std::abs(hz - originalMode.refreshRate);
}
static de::Value *Function_DisplayMode_OriginalMode(de::Context &, de::Function::ArgumentValues const &)
{
using de::NumberValue;
using de::TextValue;
DisplayMode const *mode = DisplayMode_OriginalMode();
de::DictionaryValue *dict = new de::DictionaryValue;
dict->add(new TextValue("width"), new NumberValue(mode->width));
dict->add(new TextValue("height"), new NumberValue(mode->height));
dict->add(new TextValue("depth"), new NumberValue(mode->depth));
dict->add(new TextValue("refreshRate"), new NumberValue(mode->refreshRate));
de::ArrayValue *ratio = new de::ArrayValue;
*ratio << NumberValue(mode->ratioX) << NumberValue(mode->ratioY);
dict->add(new TextValue("ratio"), ratio);
return dict;
}
} // namespace de
using namespace de;
#if defined (DE_DEBUG)
static void assertDisplayModeHasBeenDeinitialized()
{
DE_ASSERT(!inited);
}
#endif
int DisplayMode_Init(void)
{
DE_ASSERT(!inited); // don't redo it
if (inited) return true;
captured = false;
DisplayMode_Native_Init();
#if defined(MACOSX) || defined(UNIX)
DisplayMode_SaveOriginalColorTransfer();
#endif
// This is used for sorting the mode set (Hz).
originalMode = Mode::fromCurrent();
for (int i = 0; i < DisplayMode_Native_Count(); ++i)
{
Mode mode(i);
if (mode.depth < 16 || mode.width < 320 || mode.height < 240)
continue; // This mode is not good.
modes.insert(mode);
}
LOG_GL_VERBOSE("Current mode is:");
originalMode.debugPrint();
LOG_GL_VERBOSE("All available modes:");
for (auto i = modes.begin(); i != modes.end(); ++i)
{
i->debugPrint();
}
// Script bindings.
{
binder = new Binder;
binder->initNew() << DE_FUNC_NOARG(DisplayMode_OriginalMode, "originalMode");
de::App::scriptSystem().addNativeModule("DisplayMode", binder->module());
binder->module().addNumber("PIXEL_RATIO", 1.0);
}
#if defined (DE_DEBUG)
atexit(assertDisplayModeHasBeenDeinitialized);
#endif
inited = true;
return true;
}
void DisplayMode_Shutdown(void)
{
if (!inited) return;
delete binder;
binder = nullptr;
LOG_GL_NOTE("Restoring original display mode due to shutdown");
// Back to the original mode.
DisplayMode_Change(&originalMode, false /*release captured*/);
modes.clear();
DisplayMode_Native_Shutdown();
captured = false;
DisplayMode_Native_SetColorTransfer(&originalColorTransfer);
inited = false;
}
void DisplayMode_SaveOriginalColorTransfer(void)
{
DisplayMode_Native_GetColorTransfer(&originalColorTransfer);
}
DisplayMode const *DisplayMode_OriginalMode(void)
{
return &originalMode;
}
DisplayMode const *DisplayMode_Current(void)
{
static Mode currentMode;
// Update it with current mode.
currentMode = Mode::fromCurrent();
return ¤tMode;
}
int DisplayMode_Count(void)
{
return (int) modes.size();
}
DisplayMode const *DisplayMode_ByIndex(int index)
{
DE_ASSERT(index >= 0);
DE_ASSERT(index < (int) modes.size());
int pos = 0;
for (Modes::iterator i = modes.begin(); i != modes.end(); ++i, ++pos)
{
if (pos == index)
{
return &*i;
}
}
DE_ASSERT(false);
return 0; // unreachable
}
DisplayMode const *DisplayMode_FindClosest(int width, int height, int depth, float freq)
{
int bestScore = -1;
DisplayMode const *best = 0;
for (Modes::iterator i = modes.begin(); i != modes.end(); ++i)
{
int score = de::squared(i->width - width) +
de::squared(i->height - height) +
de::squared(i->depth - depth);
if (freq >= 1)
{
score += de::squared(i->refreshRate - freq);
}
// Note: The first mode to hit the lowest score wins; if there are many modes
// with the same score, the first one will be chosen. Particularly if the
// frequency has not been specified, the sort order of the modes defines which
// one is picked.
if (!best || score < bestScore)
{
bestScore = score;
best = &*i;
}
}
return best;
}
int DisplayMode_IsEqual(DisplayMode const *a, DisplayMode const *b)
{
if (!a || !b) return true; // Cannot compare against nothing.
return Mode(*a) == Mode(*b);
}
int DisplayMode_Change(DisplayMode const *mode, int shouldCapture)
{
if (Mode::fromCurrent() == *mode && !shouldCapture == !captured)
{
LOG_AS("DisplayMode");
LOGDEV_GL_XVERBOSE("Requested mode is the same as current, ignoring request", "");
// Already in this mode.
return false;
}
captured = shouldCapture;
return DisplayMode_Native_Change(mode, shouldCapture || (originalMode != *mode));
}
static inline de::duint16 intensity8To16(de::duint8 b)
{
return (b << 8) | b; // 0xFF => 0xFFFF
}
void DisplayMode_GetColorTransfer(DisplayColorTransfer *colors)
{
DisplayColorTransfer mapped;
DisplayMode_Native_GetColorTransfer(&mapped);
// Factor out the original color transfer function, which may be set up
// specifically by the user.
for (int i = 0; i < 256; ++i)
{
#define LINEAR_UNMAP(i, c) ( (unsigned short) \
de::clamp(0.f, float(mapped.table[i]) / float(originalColorTransfer.table[i]) * intensity8To16(c), 65535.f) )
colors->table[i] = LINEAR_UNMAP(i, i);
colors->table[i + 256] = LINEAR_UNMAP(i + 256, i);
colors->table[i + 512] = LINEAR_UNMAP(i + 512, i);
}
}
void DisplayMode_SetColorTransfer(DisplayColorTransfer const *colors)
{
DisplayColorTransfer mapped;
// Factor in the original color transfer function, which may be set up
// specifically by the user.
for (int i = 0; i < 256; ++i)
{
#define LINEAR_MAP(i, c) ( (unsigned short) \
de::clamp(0.f, float(colors->table[i]) / float(intensity8To16(c)) * originalColorTransfer.table[i], 65535.f) )
mapped.table[i] = LINEAR_MAP(i, i);
mapped.table[i + 256] = LINEAR_MAP(i + 256, i);
mapped.table[i + 512] = LINEAR_MAP(i + 512, i);
}
DisplayMode_Native_SetColorTransfer(&mapped);
}