Repository navigation
Expand file tree
/
Copy pathnv_color_state.cpp
More file actions
475 lines (422 loc) · 13 KB
/
Copy pathnv_color_state.cpp
File metadata and controls
475 lines (422 loc) · 13 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include "nv_color_state.h"
#include <windows.h>
#include <array>
#include <cstdint>
#include <string>
namespace {
// Minimal ABI-compatible declarations from NVIDIA's MIT-licensed NVAPI SDK.
// The implementation itself is supplied by the installed NVIDIA display driver.
using NvStatus = int;
constexpr NvStatus kNvOk = 0;
constexpr std::uint8_t kColorCommandGet = 1;
constexpr std::uint8_t kColorCommandSet = 2;
constexpr std::uint8_t kColorCommandIsSupported = 3;
constexpr std::int32_t kColorSelectionPolicyUser = 0;
constexpr std::uint32_t kNvApiInitializeId = 0x0150E828;
constexpr std::uint32_t kNvApiUnloadId = 0xD22BDD7E;
constexpr std::uint32_t kNvApiGetErrorMessageId = 0x6C2D048C;
constexpr std::uint32_t kNvApiGetPrimaryDisplayId = 0x1E9D8A31;
constexpr std::uint32_t kNvApiColorControlId = 0x92F9D80D;
template <typename T>
constexpr std::uint32_t MakeVersion(const std::uint32_t version) {
return static_cast<std::uint32_t>(sizeof(T)) | (version << 16);
}
struct NvColorDataV3 {
std::uint32_t version;
std::uint16_t size;
std::uint8_t command;
struct {
std::uint8_t colorFormat;
std::uint8_t colorimetry;
std::uint8_t dynamicRange;
std::int32_t bpc;
} data;
};
struct NvColorDataV4 {
std::uint32_t version;
std::uint16_t size;
std::uint8_t command;
struct {
std::uint8_t colorFormat;
std::uint8_t colorimetry;
std::uint8_t dynamicRange;
std::int32_t bpc;
std::int32_t colorSelectionPolicy;
} data;
};
struct NvColorDataV5 {
std::uint32_t version;
std::uint16_t size;
std::uint8_t command;
struct {
std::uint8_t colorFormat;
std::uint8_t colorimetry;
std::uint8_t dynamicRange;
std::int32_t bpc;
std::int32_t colorSelectionPolicy;
std::int32_t desktopDepth;
} data;
};
static_assert(sizeof(NvColorDataV3) == 16);
static_assert(sizeof(NvColorDataV4) == 20);
static_assert(sizeof(NvColorDataV5) == 24);
using QueryInterfaceFn = void*(__cdecl*)(std::uint32_t);
using InitializeFn = NvStatus(__cdecl*)();
using UnloadFn = NvStatus(__cdecl*)();
using GetErrorMessageFn = NvStatus(__cdecl*)(NvStatus, char*);
using GetPrimaryDisplayIdFn = NvStatus(__cdecl*)(std::uint32_t*);
using ColorControlFn = NvStatus(__cdecl*)(std::uint32_t, void*);
std::wstring ToWide(const char* text) {
if (text == nullptr || *text == '\0') {
return {};
}
const int required = MultiByteToWideChar(CP_ACP, 0, text, -1, nullptr, 0);
if (required <= 1) {
return {};
}
std::wstring result(static_cast<std::size_t>(required), L'\0');
MultiByteToWideChar(CP_ACP, 0, text, -1, result.data(), required);
result.pop_back();
return result;
}
class NvApiSession {
public:
NvApiSession() = default;
NvApiSession(const NvApiSession&) = delete;
NvApiSession& operator=(const NvApiSession&) = delete;
~NvApiSession() {
if (initialized_ && unload_ != nullptr) {
unload_();
}
if (module_ != nullptr) {
FreeLibrary(module_);
}
}
bool Open(std::wstring& error) {
#if defined(_WIN64)
module_ = LoadLibraryW(L"nvapi64.dll");
#else
module_ = LoadLibraryW(L"nvapi.dll");
#endif
if (module_ == nullptr) {
error = L"the NVIDIA NVAPI driver library is unavailable";
return false;
}
const auto queryInterface = reinterpret_cast<QueryInterfaceFn>(
GetProcAddress(module_, "nvapi_QueryInterface"));
if (queryInterface == nullptr) {
error = L"the NVIDIA driver does not export nvapi_QueryInterface";
return false;
}
initialize_ = Resolve<InitializeFn>(queryInterface, kNvApiInitializeId);
unload_ = Resolve<UnloadFn>(queryInterface, kNvApiUnloadId);
getErrorMessage_ = Resolve<GetErrorMessageFn>(
queryInterface,
kNvApiGetErrorMessageId);
getPrimaryDisplayId_ = Resolve<GetPrimaryDisplayIdFn>(
queryInterface,
kNvApiGetPrimaryDisplayId);
colorControl_ = Resolve<ColorControlFn>(
queryInterface,
kNvApiColorControlId);
if (initialize_ == nullptr || getPrimaryDisplayId_ == nullptr ||
colorControl_ == nullptr) {
error = L"the NVIDIA driver does not provide the required color-control APIs";
return false;
}
const NvStatus status = initialize_();
if (status != kNvOk) {
error = L"NVAPI initialization failed: " + DescribeStatus(status);
return false;
}
initialized_ = true;
return true;
}
NvStatus Execute(void* colorData) const {
std::uint32_t displayId = 0;
const NvStatus displayStatus = getPrimaryDisplayId_(&displayId);
if (displayStatus != kNvOk) {
return displayStatus;
}
return colorControl_(displayId, colorData);
}
std::wstring DescribeStatus(const NvStatus status) const {
std::array<char, 64> message{};
if (getErrorMessage_ != nullptr &&
getErrorMessage_(status, message.data()) == kNvOk &&
message[0] != '\0') {
return ToWide(message.data()) + L" (" + std::to_wstring(status) + L")";
}
return L"NVAPI status " + std::to_wstring(status);
}
private:
template <typename T>
static T Resolve(
const QueryInterfaceFn queryInterface,
const std::uint32_t id) {
return reinterpret_cast<T>(queryInterface(id));
}
HMODULE module_ = nullptr;
InitializeFn initialize_ = nullptr;
UnloadFn unload_ = nullptr;
GetErrorMessageFn getErrorMessage_ = nullptr;
GetPrimaryDisplayIdFn getPrimaryDisplayId_ = nullptr;
ColorControlFn colorControl_ = nullptr;
bool initialized_ = false;
};
template <typename T>
T MakeColorData(
const std::uint32_t version,
const std::uint8_t command,
const NvidiaColorState* state = nullptr) {
T data{};
data.version = MakeVersion<T>(version);
data.size = static_cast<std::uint16_t>(sizeof(T));
data.command = command;
if (state != nullptr) {
data.data.colorFormat = state->colorFormat;
data.data.colorimetry = state->colorimetry;
data.data.dynamicRange = state->dynamicRange;
data.data.bpc = state->bpc;
}
return data;
}
NvColorDataV4 MakeColorDataV4(
const std::uint8_t command,
const NvidiaColorState* state = nullptr) {
auto data = MakeColorData<NvColorDataV4>(4, command, state);
if (state != nullptr) {
data.data.colorSelectionPolicy = kColorSelectionPolicyUser;
}
return data;
}
NvColorDataV5 MakeColorDataV5(
const std::uint8_t command,
const NvidiaColorState* state = nullptr) {
auto data = MakeColorData<NvColorDataV5>(5, command, state);
if (state != nullptr) {
data.data.colorSelectionPolicy = kColorSelectionPolicyUser;
data.data.desktopDepth = state->desktopDepth;
}
return data;
}
NvidiaColorState ToState(const NvColorDataV3& data) {
return NvidiaColorState{
data.data.colorFormat,
data.data.colorimetry,
data.data.dynamicRange,
data.data.bpc,
0,
3,
false};
}
NvidiaColorState ToState(const NvColorDataV4& data) {
return NvidiaColorState{
data.data.colorFormat,
data.data.colorimetry,
data.data.dynamicRange,
data.data.bpc,
0,
4,
false};
}
NvidiaColorState ToState(const NvColorDataV5& data) {
return NvidiaColorState{
data.data.colorFormat,
data.data.colorimetry,
data.data.dynamicRange,
data.data.bpc,
data.data.desktopDepth,
5,
true};
}
bool ReadOnce(
const NvApiSession& session,
NvidiaColorState& state,
NvStatus& lastStatus) {
auto version5 = MakeColorDataV5(kColorCommandGet);
lastStatus = session.Execute(&version5);
if (lastStatus == kNvOk) {
state = ToState(version5);
return true;
}
auto version4 = MakeColorDataV4(kColorCommandGet);
lastStatus = session.Execute(&version4);
if (lastStatus == kNvOk) {
state = ToState(version4);
return true;
}
auto version3 = MakeColorData<NvColorDataV3>(3, kColorCommandGet);
lastStatus = session.Execute(&version3);
if (lastStatus == kNvOk) {
state = ToState(version3);
return true;
}
return false;
}
NvStatus ExecuteStateCommand(
const NvApiSession& session,
const NvidiaColorState& state,
const std::uint8_t command) {
if (state.apiVersion >= 5 && state.hasDesktopDepth) {
auto data = MakeColorDataV5(command, &state);
return session.Execute(&data);
}
if (state.apiVersion >= 4) {
auto data = MakeColorDataV4(command, &state);
return session.Execute(&data);
}
auto data = MakeColorData<NvColorDataV3>(3, command, &state);
return session.Execute(&data);
}
template <typename Action>
bool RunWithRetry(
const wchar_t* operation,
const unsigned int attempts,
Action action,
std::wstring& error) {
NvApiSession session;
if (!session.Open(error)) {
return false;
}
NvStatus lastStatus = kNvOk;
const unsigned int count = attempts == 0 ? 1 : attempts;
for (unsigned int attempt = 0; attempt < count; ++attempt) {
if (action(session, lastStatus)) {
return true;
}
if (attempt + 1 < count) {
Sleep(100);
}
}
error = std::wstring{operation} + L" failed: " + session.DescribeStatus(lastStatus);
return false;
}
std::wstring ColorFormatName(const std::uint8_t value) {
switch (value) {
case 0:
return L"RGB";
case 1:
return L"YCbCr 4:2:2";
case 2:
return L"YCbCr 4:4:4";
case 3:
return L"YCbCr 4:2:0";
case 0xFE:
return L"Default";
case 0xFF:
return L"Auto";
default:
return L"Format " + std::to_wstring(value);
}
}
std::wstring ColorimetryName(const std::uint8_t value) {
switch (value) {
case 0:
return L"RGB";
case 1:
return L"YCC 601";
case 2:
return L"YCC 709";
case 8:
return L"BT.2020 RGB";
case 9:
return L"BT.2020 YCC";
case 10:
return L"BT.2020 cYCC";
case 0xFE:
return L"Default";
case 0xFF:
return L"Auto";
default:
return L"Colorimetry " + std::to_wstring(value);
}
}
std::wstring DynamicRangeName(const std::uint8_t value) {
switch (value) {
case 0:
return L"Full";
case 1:
return L"Limited";
case 0xFF:
return L"Auto";
default:
return L"Range " + std::to_wstring(value);
}
}
std::wstring BpcName(const std::int32_t value) {
switch (value) {
case 0:
return L"Default bpc";
case 1:
return L"6 bpc";
case 2:
return L"8 bpc";
case 3:
return L"10 bpc";
case 4:
return L"12 bpc";
case 5:
return L"16 bpc";
default:
return L"BPC " + std::to_wstring(value);
}
}
} // namespace
bool NvidiaColorControl::ReadPrimary(
NvidiaColorState& state,
std::wstring& error,
const unsigned int attempts) {
return RunWithRetry(
L"reading NVIDIA color settings",
attempts,
[&state](const NvApiSession& session, NvStatus& status) {
return ReadOnce(session, state, status);
},
error);
}
bool NvidiaColorControl::IsSupportedOnPrimary(
const NvidiaColorState& state,
std::wstring& error,
const unsigned int attempts) {
return RunWithRetry(
L"checking NVIDIA color settings",
attempts,
[&state](const NvApiSession& session, NvStatus& status) {
status = ExecuteStateCommand(
session,
state,
kColorCommandIsSupported);
return status == kNvOk;
},
error);
}
bool NvidiaColorControl::ApplyToPrimary(
const NvidiaColorState& state,
std::wstring& error,
const unsigned int attempts) {
return RunWithRetry(
L"restoring NVIDIA color settings",
attempts,
[&state](const NvApiSession& session, NvStatus& status) {
status = ExecuteStateCommand(session, state, kColorCommandSet);
return status == kNvOk;
},
error);
}
bool NvidiaColorControl::Equivalent(
const NvidiaColorState& left,
const NvidiaColorState& right) {
return left.colorFormat == right.colorFormat &&
left.colorimetry == right.colorimetry &&
left.dynamicRange == right.dynamicRange &&
left.bpc == right.bpc &&
(!left.hasDesktopDepth || !right.hasDesktopDepth ||
left.desktopDepth == right.desktopDepth);
}
std::wstring NvidiaColorControl::Describe(const NvidiaColorState& state) {
return ColorFormatName(state.colorFormat) + L", " +
ColorimetryName(state.colorimetry) + L", " +
DynamicRangeName(state.dynamicRange) + L", " +
BpcName(state.bpc);
}