forked from ValveSoftware/steamos-compositor
-
Notifications
You must be signed in to change notification settings - Fork 180
/
steamcompmgr.cpp
7792 lines (6581 loc) · 233 KB
/
steamcompmgr.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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Based on xcompmgr by Keith Packard et al.
* http://cgit.freedesktop.org/xorg/app/xcompmgr/
* Original xcompmgr legal notices follow:
*
* Copyright © 2003 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Keith Packard not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Keith Packard makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* Modified by Matthew Hawn. I don't know what to say here so follow what it
* says above. Not that I can really do anything about it
*/
#include "xwayland_ctx.hpp"
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xcursor/Xcursor.h>
#include <X11/extensions/xfixeswire.h>
#include <X11/extensions/XInput2.h>
#include <cstdint>
#include <memory>
#include <thread>
#include <condition_variable>
#include <mutex>
#include <atomic>
#include <vector>
#include <algorithm>
#include <array>
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
#include <filesystem>
#include <variant>
#include <unordered_set>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/types.h>
#if defined(__linux__)
#include <sys/prctl.h>
#elif defined(__DragonFly__) || defined(__FreeBSD__)
#include <sys/procctl.h>
#endif
#include <sys/socket.h>
#include <sys/resource.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <spawn.h>
#include <signal.h>
#include <linux/input-event-codes.h>
#include <X11/Xmu/CurUtil.h>
#include "waitable.h"
#include "main.hpp"
#include "wlserver.hpp"
#include "rendervulkan.hpp"
#include "steamcompmgr.hpp"
#include "vblankmanager.hpp"
#include "log.hpp"
#include "defer.hpp"
#include "win32_styles.h"
#include "mwm_hints.h"
#include "edid.h"
#include "hdmi.h"
#include "convar.h"
#include "refresh_rate.h"
#include "commit.h"
#if HAVE_AVIF
#include "avif/avif.h"
#endif
static const int g_nBaseCursorScale = 36;
#if HAVE_PIPEWIRE
#include "pipewire.hpp"
#endif
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image.h>
#include <stb_image_write.h>
#include <stb_image_resize.h>
#define GPUVIS_TRACE_IMPLEMENTATION
#include "gpuvis_trace_utils.h"
LogScope xwm_log("xwm");
LogScope g_WaitableLog("waitable");
bool g_bWasPartialComposite = false;
///
// Color Mgmt
//
gamescope_color_mgmt_tracker_t g_ColorMgmt{};
static gamescope_color_mgmt_luts g_ColorMgmtLutsOverride[ EOTF_Count ];
static lut3d_t g_ColorMgmtLooks[ EOTF_Count ];
gamescope_color_mgmt_luts g_ColorMgmtLuts[ EOTF_Count ];
gamescope_color_mgmt_luts g_ScreenshotColorMgmtLuts[ EOTF_Count ];
gamescope_color_mgmt_luts g_ScreenshotColorMgmtLutsHDR[ EOTF_Count ];
static lut1d_t g_tmpLut1d;
static lut3d_t g_tmpLut3d;
bool g_bForceHDRSupportDebug = false;
extern float g_flInternalDisplayBrightnessNits;
extern float g_flHDRItmSdrNits;
extern float g_flHDRItmTargetNits;
uint64_t g_lastWinSeq = 0;
static std::shared_ptr<gamescope::BackendBlob> s_scRGB709To2020Matrix;
std::string clipboard;
std::string primarySelection;
std::string g_reshade_effect{};
uint32_t g_reshade_technique_idx = 0;
bool g_bSteamIsActiveWindow = false;
bool g_bForceInternal = false;
static std::vector< steamcompmgr_win_t* > GetGlobalPossibleFocusWindows();
static bool
pick_primary_focus_and_override(focus_t *out, Window focusControlWindow, const std::vector<steamcompmgr_win_t*>& vecPossibleFocusWindows, bool globalFocus, const std::vector<uint32_t>& ctxFocusControlAppIDs);
bool env_to_bool(const char *env)
{
if (!env || !*env)
return false;
return !!atoi(env);
}
uint64_t timespec_to_nanos(struct timespec& spec)
{
return spec.tv_sec * 1'000'000'000ul + spec.tv_nsec;
}
timespec nanos_to_timespec( uint64_t ulNanos )
{
timespec ts =
{
.tv_sec = time_t( ulNanos / 1'000'000'000ul ),
.tv_nsec = long( ulNanos % 1'000'000'000ul ),
};
return ts;
}
static void
update_runtime_info();
bool g_bAllowVRR = false;
static uint64_t g_SteamCompMgrLimitedAppRefreshCycle = 16'666'666;
static uint64_t g_SteamCompMgrAppRefreshCycle = 16'666'666;
static const gamescope_color_mgmt_t k_ScreenshotColorMgmt =
{
.enabled = true,
.displayColorimetry = displaycolorimetry_709,
.displayEOTF = EOTF_Gamma22,
.outputEncodingColorimetry = displaycolorimetry_709,
.outputEncodingEOTF = EOTF_Gamma22,
};
static const gamescope_color_mgmt_t k_ScreenshotColorMgmtHDR =
{
.enabled = true,
.displayColorimetry = displaycolorimetry_2020,
.displayEOTF = EOTF_PQ,
.outputEncodingColorimetry = displaycolorimetry_2020,
.outputEncodingEOTF = EOTF_PQ,
};
//#define COLOR_MGMT_MICROBENCH
// sudo cpupower frequency-set --governor performance
static void
create_color_mgmt_luts(const gamescope_color_mgmt_t& newColorMgmt, gamescope_color_mgmt_luts outColorMgmtLuts[ EOTF_Count ])
{
const displaycolorimetry_t& displayColorimetry = newColorMgmt.displayColorimetry;
const displaycolorimetry_t& outputEncodingColorimetry = newColorMgmt.outputEncodingColorimetry;
for ( uint32_t nInputEOTF = 0; nInputEOTF < EOTF_Count; nInputEOTF++ )
{
if (!outColorMgmtLuts[nInputEOTF].vk_lut1d)
outColorMgmtLuts[nInputEOTF].vk_lut1d = vulkan_create_1d_lut(s_nLutSize1d);
if (!outColorMgmtLuts[nInputEOTF].vk_lut3d)
outColorMgmtLuts[nInputEOTF].vk_lut3d = vulkan_create_3d_lut(s_nLutEdgeSize3d, s_nLutEdgeSize3d, s_nLutEdgeSize3d);
if ( g_ColorMgmtLutsOverride[nInputEOTF].HasLuts() )
{
memcpy(g_ColorMgmtLuts[nInputEOTF].lut1d, g_ColorMgmtLutsOverride[nInputEOTF].lut1d, sizeof(g_ColorMgmtLutsOverride[nInputEOTF].lut1d));
memcpy(g_ColorMgmtLuts[nInputEOTF].lut3d, g_ColorMgmtLutsOverride[nInputEOTF].lut3d, sizeof(g_ColorMgmtLutsOverride[nInputEOTF].lut3d));
}
else
{
displaycolorimetry_t inputColorimetry{};
colormapping_t colorMapping{};
tonemapping_t tonemapping{};
tonemapping.bUseShaper = true;
EOTF inputEOTF = static_cast<EOTF>( nInputEOTF );
float flGain = 1.f;
lut3d_t * pLook = g_ColorMgmtLooks[nInputEOTF].lutEdgeSize > 0 ? &g_ColorMgmtLooks[nInputEOTF] : nullptr;
if ( inputEOTF == EOTF_Gamma22 )
{
flGain = newColorMgmt.flSDRInputGain;
if ( newColorMgmt.outputEncodingEOTF == EOTF_Gamma22 )
{
// G22 -> G22. Does not matter what the g22 mult is
tonemapping.g22_luminance = 1.f;
// xwm_log.infof("G22 -> G22");
}
else if ( newColorMgmt.outputEncodingEOTF == EOTF_PQ )
{
// G22 -> PQ. SDR content going on an HDR output
tonemapping.g22_luminance = newColorMgmt.flSDROnHDRBrightness;
// xwm_log.infof("G22 -> PQ");
}
// The final display colorimetry is used to build the output mapping, as we want a gamut-aware handling
// for sdrGamutWideness indepdendent of the output encoding (for SDR data), and when mapping SDR -> PQ output
// we only want to utilize a portion of the gamut the actual display can reproduce
buildSDRColorimetry( &inputColorimetry, &colorMapping, newColorMgmt.sdrGamutWideness, displayColorimetry );
}
else if ( inputEOTF == EOTF_PQ )
{
flGain = newColorMgmt.flHDRInputGain;
if ( newColorMgmt.outputEncodingEOTF == EOTF_Gamma22 )
{
// PQ -> G22 Leverage the display's native brightness
tonemapping.g22_luminance = newColorMgmt.flInternalDisplayBrightness;
// Determine the tonemapping parameters
// Use the external atoms if provided
tonemap_info_t source = newColorMgmt.hdrTonemapSourceMetadata;
tonemap_info_t dest = newColorMgmt.hdrTonemapDisplayMetadata;
// Otherwise, rely on the Vulkan source info and the EDID
// TODO: If source is invalid, use the provided metadata.
// TODO: If hdrTonemapDisplayMetadata is invalid, use the one provided by the display
// Adjust the source brightness range by the requested HDR input gain
dest.flBlackPointNits /= flGain;
dest.flWhitePointNits /= flGain;
if ( source.BIsValid() && dest.BIsValid() )
{
tonemapping.eOperator = newColorMgmt.hdrTonemapOperator;
tonemapping.eetf2390.init( source, newColorMgmt.hdrTonemapDisplayMetadata );
}
else
{
tonemapping.eOperator = ETonemapOperator_None;
}
/*
xwm_log.infof("PQ -> 2.2 - g22_luminance %f gain %f", tonemapping.g22_luminance, flGain );
xwm_log.infof("source %f %f", source.flBlackPointNits, source.flWhitePointNits );
xwm_log.infof("dest %f %f", dest.flBlackPointNits, dest.flWhitePointNits );
xwm_log.infof("operator %d", (int) tonemapping.eOperator );*/
}
else if ( newColorMgmt.outputEncodingEOTF == EOTF_PQ )
{
// PQ -> PQ passthrough (though this does apply gain)
// TODO: should we manipulate the output static metadata to reflect the gain factor?
tonemapping.g22_luminance = 1.f;
// xwm_log.infof("PQ -> PQ");
}
buildPQColorimetry( &inputColorimetry, &colorMapping, displayColorimetry );
}
calcColorTransform( &g_tmpLut1d, s_nLutSize1d, &g_tmpLut3d, s_nLutEdgeSize3d, inputColorimetry, inputEOTF,
outputEncodingColorimetry, newColorMgmt.outputEncodingEOTF,
newColorMgmt.outputVirtualWhite, newColorMgmt.chromaticAdaptationMode,
colorMapping, newColorMgmt.nightmode, tonemapping, pLook, flGain );
// Create quantized output luts
for ( size_t i=0, end = g_tmpLut1d.dataR.size(); i<end; ++i )
{
outColorMgmtLuts[nInputEOTF].lut1d[4*i+0] = quantize_lut_value_16bit( g_tmpLut1d.dataR[i] );
outColorMgmtLuts[nInputEOTF].lut1d[4*i+1] = quantize_lut_value_16bit( g_tmpLut1d.dataG[i] );
outColorMgmtLuts[nInputEOTF].lut1d[4*i+2] = quantize_lut_value_16bit( g_tmpLut1d.dataB[i] );
outColorMgmtLuts[nInputEOTF].lut1d[4*i+3] = 0;
}
for ( size_t i=0, end = g_tmpLut3d.data.size(); i<end; ++i )
{
outColorMgmtLuts[nInputEOTF].lut3d[4*i+0] = quantize_lut_value_16bit( g_tmpLut3d.data[i].r );
outColorMgmtLuts[nInputEOTF].lut3d[4*i+1] = quantize_lut_value_16bit( g_tmpLut3d.data[i].g );
outColorMgmtLuts[nInputEOTF].lut3d[4*i+2] = quantize_lut_value_16bit( g_tmpLut3d.data[i].b );
outColorMgmtLuts[nInputEOTF].lut3d[4*i+3] = 0;
}
}
outColorMgmtLuts[nInputEOTF].bHasLut1D = true;
outColorMgmtLuts[nInputEOTF].bHasLut3D = true;
vulkan_update_luts(outColorMgmtLuts[nInputEOTF].vk_lut1d, outColorMgmtLuts[nInputEOTF].vk_lut3d, outColorMgmtLuts[nInputEOTF].lut1d, outColorMgmtLuts[nInputEOTF].lut3d);
}
}
int g_nAsyncFlipsEnabled = 0;
int g_nSteamMaxHeight = 0;
bool g_bVRRCapable_CachedValue = false;
bool g_bVRRInUse_CachedValue = false;
bool g_bSupportsHDR_CachedValue = false;
bool g_bForceHDR10OutputDebug = false;
gamescope::ConVar<bool> cv_hdr_enabled{ "hdr_enabled", false, "Whether or not HDR is enabled if it is available." };
bool g_bHDRItmEnable = false;
int g_nCurrentRefreshRate_CachedValue = 0;
static void
update_color_mgmt()
{
if ( !GetBackend()->GetCurrentConnector() )
return;
GetBackend()->GetCurrentConnector()->GetNativeColorimetry(
g_bOutputHDREnabled,
&g_ColorMgmt.pending.displayColorimetry, &g_ColorMgmt.pending.displayEOTF,
&g_ColorMgmt.pending.outputEncodingColorimetry, &g_ColorMgmt.pending.outputEncodingEOTF );
g_ColorMgmt.pending.flInternalDisplayBrightness =
GetBackend()->GetCurrentConnector()->GetHDRInfo().uMaxContentLightLevel;
#ifdef COLOR_MGMT_MICROBENCH
struct timespec t0, t1;
#else
// check if any part of our color mgmt stack is dirty
if ( g_ColorMgmt.pending == g_ColorMgmt.current && g_ColorMgmt.serial != 0 )
return;
#endif
#ifdef COLOR_MGMT_MICROBENCH
clock_gettime(CLOCK_MONOTONIC_RAW, &t0);
#endif
if (g_ColorMgmt.pending.enabled)
{
create_color_mgmt_luts(g_ColorMgmt.pending, g_ColorMgmtLuts);
}
else
{
for ( uint32_t i = 0; i < EOTF_Count; i++ )
g_ColorMgmtLuts[i].reset();
}
#ifdef COLOR_MGMT_MICROBENCH
clock_gettime(CLOCK_MONOTONIC_RAW, &t1);
#endif
#ifdef COLOR_MGMT_MICROBENCH
double delta = (timespec_to_nanos(t1) - timespec_to_nanos(t0)) / 1000000.0;
static uint32_t iter = 0;
static const uint32_t iter_count = 120;
static double accum = 0;
accum += delta;
if (iter++ == iter_count)
{
printf("update_color_mgmt: %.3fms\n", accum / iter_count);
iter = 0;
accum = 0;
}
#endif
static uint32_t s_NextColorMgmtSerial = 0;
g_ColorMgmt.serial = ++s_NextColorMgmtSerial;
g_ColorMgmt.current = g_ColorMgmt.pending;
}
static void
update_screenshot_color_mgmt()
{
create_color_mgmt_luts(k_ScreenshotColorMgmt, g_ScreenshotColorMgmtLuts);
create_color_mgmt_luts(k_ScreenshotColorMgmtHDR, g_ScreenshotColorMgmtLutsHDR);
}
bool set_color_sdr_gamut_wideness( float flVal )
{
if ( g_ColorMgmt.pending.sdrGamutWideness == flVal )
return false;
g_ColorMgmt.pending.sdrGamutWideness = flVal;
return g_ColorMgmt.pending.enabled;
}
bool set_internal_display_brightness( float flVal )
{
if ( flVal < 1.f )
{
flVal = 500.f;
}
if ( g_ColorMgmt.pending.flInternalDisplayBrightness == flVal )
return false;
g_ColorMgmt.pending.flInternalDisplayBrightness = flVal;
g_flInternalDisplayBrightnessNits = flVal;
return g_ColorMgmt.pending.enabled;
}
bool set_sdr_on_hdr_brightness( float flVal )
{
if ( flVal < 1.f )
{
flVal = 203.f;
}
if ( g_ColorMgmt.pending.flSDROnHDRBrightness == flVal )
return false;
g_ColorMgmt.pending.flSDROnHDRBrightness = flVal;
return g_ColorMgmt.pending.enabled;
}
bool set_hdr_input_gain( float flVal )
{
if ( flVal < 0.f )
{
flVal = 1.f;
}
if ( g_ColorMgmt.pending.flHDRInputGain == flVal )
return false;
g_ColorMgmt.pending.flHDRInputGain = flVal;
return g_ColorMgmt.pending.enabled;
}
bool set_sdr_input_gain( float flVal )
{
if ( flVal < 0.f )
{
flVal = 1.f;
}
if ( g_ColorMgmt.pending.flSDRInputGain == flVal )
return false;
g_ColorMgmt.pending.flSDRInputGain = flVal;
return g_ColorMgmt.pending.enabled;
}
bool set_color_nightmode( const nightmode_t &nightmode )
{
if ( g_ColorMgmt.pending.nightmode == nightmode )
return false;
g_ColorMgmt.pending.nightmode = nightmode;
return g_ColorMgmt.pending.enabled;
}
bool set_color_mgmt_enabled( bool bEnabled )
{
if ( g_ColorMgmt.pending.enabled == bEnabled )
return false;
g_ColorMgmt.pending.enabled = bEnabled;
return true;
}
static std::shared_ptr<CVulkanTexture> s_MuraCorrectionImage[gamescope::GAMESCOPE_SCREEN_TYPE_COUNT];
static std::shared_ptr<gamescope::BackendBlob> s_MuraCTMBlob[gamescope::GAMESCOPE_SCREEN_TYPE_COUNT];
static float g_flMuraScale = 1.0f;
static bool g_bMuraCompensationDisabled = false;
bool is_mura_correction_enabled()
{
if ( !GetBackend()->GetCurrentConnector() )
return false;
return s_MuraCorrectionImage[GetBackend()->GetCurrentConnector()->GetScreenType()] != nullptr && !g_bMuraCompensationDisabled;
}
void update_mura_ctm()
{
s_MuraCTMBlob[gamescope::GAMESCOPE_SCREEN_TYPE_INTERNAL] = nullptr;
if (s_MuraCorrectionImage[gamescope::GAMESCOPE_SCREEN_TYPE_INTERNAL] == nullptr)
return;
static constexpr float kMuraMapScale = 0.0625f;
static constexpr float kMuraOffset = -127.0f / 255.0f;
// Mura's influence scales non-linearly with brightness, so we have an additional scale
// on top of the scale factor for the underlying mura map.
const float flScale = g_flMuraScale * kMuraMapScale;
glm::mat3x4 mura_scale_offset = glm::mat3x4
{
flScale, 0, 0, kMuraOffset * flScale,
0, flScale, 0, kMuraOffset * flScale,
0, 0, 0, 0, // No mura comp for blue channel.
};
s_MuraCTMBlob[gamescope::GAMESCOPE_SCREEN_TYPE_INTERNAL] = GetBackend()->CreateBackendBlob( mura_scale_offset );
}
bool g_bMuraDebugFullColor = false;
bool set_mura_overlay( const char *path )
{
xwm_log.infof("[josh mura correction] Setting mura correction image to: %s", path);
s_MuraCorrectionImage[gamescope::GAMESCOPE_SCREEN_TYPE_INTERNAL] = nullptr;
update_mura_ctm();
std::string red_path = std::string(path) + "_red.png";
std::string green_path = std::string(path) + "_green.png";
int red_w, red_h, red_comp;
unsigned char *red_data = stbi_load(red_path.c_str(), &red_w, &red_h, &red_comp, 1);
int green_w, green_h, green_comp;
unsigned char *green_data = stbi_load(green_path.c_str(), &green_w, &green_h, &green_comp, 1);
if (!red_data || !green_data || red_w != green_w || red_h != green_h || red_comp != green_comp || red_comp != 1 || green_comp != 1)
{
xwm_log.infof("[josh mura correction] Couldn't load mura correction image, disabling mura correction.");
return true;
}
int w = red_w;
int h = red_h;
unsigned char *data = (unsigned char*)malloc(red_w * red_h * 4);
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
data[(y * w * 4) + (x * 4) + 0] = g_bMuraDebugFullColor ? 255 : red_data[y * w + x];
data[(y * w * 4) + (x * 4) + 1] = g_bMuraDebugFullColor ? 255 : green_data[y * w + x];
data[(y * w * 4) + (x * 4) + 2] = 127; // offset of 0.
data[(y * w * 4) + (x * 4) + 3] = 0; // Make alpha = 0 so we act as addtive.
}
}
free(red_data);
free(green_data);
CVulkanTexture::createFlags texCreateFlags;
texCreateFlags.bFlippable = true;
texCreateFlags.bSampled = true;
s_MuraCorrectionImage[gamescope::GAMESCOPE_SCREEN_TYPE_INTERNAL] = vulkan_create_texture_from_bits(w, h, w, h, DRM_FORMAT_ABGR8888, texCreateFlags, (void*)data);
free(data);
xwm_log.infof("[josh mura correction] Loaded new mura correction image!");
update_mura_ctm();
return true;
}
bool set_mura_scale(float new_scale)
{
bool diff = g_flMuraScale != new_scale;
g_flMuraScale = new_scale;
update_mura_ctm();
return diff;
}
bool set_color_3dlut_override(const char *path)
{
int nLutIndex = EOTF_Gamma22;
g_ColorMgmt.pending.externalDirtyCtr++;
g_ColorMgmtLutsOverride[nLutIndex].bHasLut3D = false;
FILE *f = fopen(path, "rb");
if (!f) {
return true;
}
defer( fclose(f) );
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
size_t elems = fsize / sizeof(uint16_t);
if (elems == 0) {
return true;
}
fread(g_ColorMgmtLutsOverride[nLutIndex].lut3d, elems, sizeof(uint16_t), f);
g_ColorMgmtLutsOverride[nLutIndex].bHasLut3D = true;
return true;
}
bool set_color_shaperlut_override(const char *path)
{
int nLutIndex = EOTF_Gamma22;
g_ColorMgmt.pending.externalDirtyCtr++;
g_ColorMgmtLutsOverride[nLutIndex].bHasLut1D = false;
FILE *f = fopen(path, "rb");
if (!f) {
return true;
}
defer( fclose(f) );
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
size_t elems = fsize / sizeof(uint16_t);
if (elems == 0) {
return true;
}
fread(g_ColorMgmtLutsOverride[nLutIndex].lut1d, elems, sizeof(uint16_t), f);
g_ColorMgmtLutsOverride[nLutIndex].bHasLut1D = true;
return true;
}
bool set_color_look_pq(const char *path)
{
LoadCubeLut( &g_ColorMgmtLooks[EOTF_PQ], path );
g_ColorMgmt.pending.externalDirtyCtr++;
return true;
}
bool set_color_look_g22(const char *path)
{
LoadCubeLut( &g_ColorMgmtLooks[EOTF_Gamma22], path );
g_ColorMgmt.pending.externalDirtyCtr++;
return true;
}
bool g_bColorSliderInUse = false;
template< typename T >
constexpr const T& clamp( const T& x, const T& min, const T& max )
{
return x < min ? min : max < x ? max : x;
}
extern bool g_bForceRelativeMouse;
bool bSteamCompMgrGrab = false;
CommitDoneList_t g_steamcompmgr_xdg_done_commits;
struct ignore {
struct ignore *next;
unsigned long sequence;
};
gamescope::CAsyncWaiter<gamescope::Rc<commit_t>> g_ImageWaiter{ "gamescope_img" };
gamescope::CWaiter g_SteamCompMgrWaiter;
Window x11_win(steamcompmgr_win_t *w) {
if (w == nullptr)
return None;
if (w->type != steamcompmgr_win_type_t::XWAYLAND)
return None;
return w->xwayland().id;
}
static uint64_t s_ulFocusSerial = 0ul;
static inline void MakeFocusDirty()
{
s_ulFocusSerial++;
}
static inline uint64_t GetFocusSerial()
{
return s_ulFocusSerial;
}
bool focus_t::IsDirty()
{
return ulCurrentFocusSerial != GetFocusSerial();
}
struct global_focus_t : public focus_t
{
steamcompmgr_win_t *keyboardFocusWindow;
steamcompmgr_win_t *fadeWindow;
MouseCursor *cursor;
} global_focus;
uint32_t currentOutputWidth, currentOutputHeight;
bool currentHDROutput = false;
bool currentHDRForce = false;
std::vector< uint32_t > vecFocuscontrolAppIDs;
bool gameFocused;
unsigned int gamesRunningCount;
float overscanScaleRatio = 1.0;
float zoomScaleRatio = 1.0;
float globalScaleRatio = 1.0f;
float focusedWindowScaleX = 1.0f;
float focusedWindowScaleY = 1.0f;
float focusedWindowOffsetX = 0.0f;
float focusedWindowOffsetY = 0.0f;
uint32_t inputCounter;
uint32_t lastPublishedInputCounter;
std::atomic<bool> hasRepaint = false;
bool hasRepaintNonBasePlane = false;
unsigned long damageSequence = 0;
uint64_t cursorHideTime = 10'000ul * 1'000'000ul;
bool gotXError = false;
unsigned int fadeOutStartTime = 0;
unsigned int g_FadeOutDuration = 0;
extern float g_flMaxWindowScale;
bool synchronize;
std::mutex g_SteamCompMgrXWaylandServerMutex;
gamescope::VBlankTime g_SteamCompMgrVBlankTime = {};
uint64_t g_uCurrentBasePlaneCommitID = 0;
bool g_bCurrentBasePlaneIsFifo = false;
static int g_nSteamCompMgrTargetFPS = 0;
static uint64_t g_uDynamicRefreshEqualityTime = 0;
static int g_nDynamicRefreshRate[gamescope::GAMESCOPE_SCREEN_TYPE_COUNT] = { 0, 0 };
// Delay to stop modes flickering back and forth.
static const uint64_t g_uDynamicRefreshDelay = 600'000'000; // 600ms
static int g_nCombinedAppRefreshCycleOverride[gamescope::GAMESCOPE_SCREEN_TYPE_COUNT] = { 0, 0 };
bool g_nCombinedAppRefreshCycleChangeRefresh[gamescope::GAMESCOPE_SCREEN_TYPE_COUNT] = { true, true };
bool g_nCombinedAppRefreshCycleChangeFPS[gamescope::GAMESCOPE_SCREEN_TYPE_COUNT] = { true, true };
static void _update_app_target_refresh_cycle()
{
if ( !GetBackend()->GetCurrentConnector() )
return;
static gamescope::GamescopeScreenType last_type;
static int last_target_fps;
static bool first = true;
gamescope::GamescopeScreenType type = GetBackend()->GetCurrentConnector()->GetScreenType();
int target_fps = g_nCombinedAppRefreshCycleOverride[type];
if ( !first && type == last_type && last_target_fps == target_fps )
{
return;
}
last_type = type;
last_target_fps = target_fps;
first = false;
if ( !target_fps )
{
g_nDynamicRefreshRate[ type ] = 0;
g_nSteamCompMgrTargetFPS = 0;
return;
}
auto rates = GetBackend()->GetCurrentConnector()->GetValidDynamicRefreshRates();
g_nDynamicRefreshRate[ type ] = 0;
if ( g_nCombinedAppRefreshCycleChangeFPS[ type ] )
{
g_nSteamCompMgrTargetFPS = target_fps;
}
if ( g_nCombinedAppRefreshCycleChangeRefresh[ type ] )
{
// Find highest mode to do refresh doubling with.
for ( auto rate = rates.rbegin(); rate != rates.rend(); rate++ )
{
if (*rate % target_fps == 0)
{
g_nDynamicRefreshRate[ type ] = *rate;
return;
}
}
}
}
static void update_app_target_refresh_cycle()
{
int nPrevFPSLimit = g_nSteamCompMgrTargetFPS;
_update_app_target_refresh_cycle();
if ( !!g_nSteamCompMgrTargetFPS != !!nPrevFPSLimit )
update_runtime_info();
}
void steamcompmgr_set_app_refresh_cycle_override( gamescope::GamescopeScreenType type, int override_fps, bool change_refresh, bool change_fps_cap )
{
g_nCombinedAppRefreshCycleOverride[ type ] = override_fps;
g_nCombinedAppRefreshCycleChangeRefresh[ type ] = change_refresh;
g_nCombinedAppRefreshCycleChangeFPS[ type ] = change_fps_cap;
update_app_target_refresh_cycle();
}
static int g_nRuntimeInfoFd = -1;
bool g_bFSRActive = false;
BlurMode g_BlurMode = BLUR_MODE_OFF;
BlurMode g_BlurModeOld = BLUR_MODE_OFF;
unsigned int g_BlurFadeDuration = 0;
int g_BlurRadius = 5;
unsigned int g_BlurFadeStartTime = 0;
pid_t focusWindow_pid;
std::vector<std::shared_ptr<steamcompmgr_win_t>> g_steamcompmgr_xdg_wins;
static bool
window_is_steam( steamcompmgr_win_t *w )
{
return w && ( w->isSteamLegacyBigPicture || w->appID == 769 );
}
bool g_bChangeDynamicRefreshBasedOnGameOpenRatherThanActive = false;
bool steamcompmgr_window_should_limit_fps( steamcompmgr_win_t *w )
{
return w && !window_is_steam( w ) && !w->isOverlay && !w->isExternalOverlay;
}
static bool
steamcompmgr_user_has_any_game_open()
{
gamescope_xwayland_server_t *server = NULL;
for (size_t i = 0; (server = wlserver_get_xwayland_server(i)); i++)
{
if (!server->ctx)
continue;
if (steamcompmgr_window_should_limit_fps( server->ctx->focus.focusWindow ))
return true;
}
return false;
}
bool steamcompmgr_window_should_refresh_switch( steamcompmgr_win_t *w )
{
if ( g_bChangeDynamicRefreshBasedOnGameOpenRatherThanActive )
return steamcompmgr_user_has_any_game_open();
return steamcompmgr_window_should_limit_fps( w );
}
enum HeldCommitTypes_t
{
HELD_COMMIT_BASE,
HELD_COMMIT_FADE,
HELD_COMMIT_COUNT,
};
std::array<gamescope::Rc<commit_t>, HELD_COMMIT_COUNT> g_HeldCommits;
bool g_bPendingFade = false;
/* opacity property name; sometime soon I'll write up an EWMH spec for it */
#define OPACITY_PROP "_NET_WM_WINDOW_OPACITY"
#define GAME_PROP "STEAM_GAME"
#define STEAM_PROP "STEAM_BIGPICTURE"
#define OVERLAY_PROP "STEAM_OVERLAY"
#define EXTERNAL_OVERLAY_PROP "GAMESCOPE_EXTERNAL_OVERLAY"
#define GAMES_RUNNING_PROP "STEAM_GAMES_RUNNING"
#define SCREEN_SCALE_PROP "STEAM_SCREEN_SCALE"
#define SCREEN_MAGNIFICATION_PROP "STEAM_SCREEN_MAGNIFICATION"
#define TRANSLUCENT 0x00000000
#define OPAQUE 0xffffffff
#define ICCCM_WITHDRAWN_STATE 0
#define ICCCM_NORMAL_STATE 1
#define ICCCM_ICONIC_STATE 3
#define NET_WM_STATE_REMOVE 0
#define NET_WM_STATE_ADD 1
#define NET_WM_STATE_TOGGLE 2
#define SYSTEM_TRAY_REQUEST_DOCK 0
#define SYSTEM_TRAY_BEGIN_MESSAGE 1
#define SYSTEM_TRAY_CANCEL_MESSAGE 2
#define FRAME_RATE_SAMPLING_PERIOD 160
unsigned int frameCounter;
unsigned int lastSampledFrameTime;
float currentFrameRate;
static bool debugFocus = false;
static bool drawDebugInfo = false;
static bool debugEvents = false;
bool steamMode = false;
bool alwaysComposite = false;
static bool useXRes = true;
struct wlr_buffer_map_entry {
struct wl_listener listener;
struct wlr_buffer *buf;
std::shared_ptr<CVulkanTexture> vulkanTex;
gamescope::OwningRc<gamescope::IBackendFb> pBackendFb;
};
static std::mutex wlr_buffer_map_lock;
static std::unordered_map<struct wlr_buffer*, wlr_buffer_map_entry> wlr_buffer_map;
namespace gamescope
{
CScreenshotManager &CScreenshotManager::Get()
{
static CScreenshotManager s_Instance;
return s_Instance;
}
}
static std::atomic<bool> g_bForceRepaint{false};
extern int g_nCursorScaleHeight;
// poor man's semaphore
class sem
{
public:
void wait( void )
{
std::unique_lock<std::mutex> lock(mtx);
while(count == 0){
cv.wait(lock);
}
count--;
}
void signal( void )
{
std::unique_lock<std::mutex> lock(mtx);
count++;
cv.notify_one();
}
private:
std::mutex mtx;
std::condition_variable cv;
int count = 0;
};