forked from ValveSoftware/steamos-compositor
-
Notifications
You must be signed in to change notification settings - Fork 183
/
steamcompmgr.cpp
7330 lines (6204 loc) · 209 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 <X11/Xlib.h>
#include <cstdint>
#include <drm_mode.h>
#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 <variant>
#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 "steamcompmgr_shared.hpp"
#include "main.hpp"
#include "wlserver.hpp"
#include "drm.hpp"
#include "rendervulkan.hpp"
#include "steamcompmgr.hpp"
#include "vblankmanager.hpp"
#include "sdlwindow.hpp"
#include "log.hpp"
#include "defer.hpp"
#if HAVE_PIPEWIRE
#include "pipewire.hpp"
#endif
#if HAVE_OPENVR
#include "vr_session.hpp"
#endif
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image.h>
#include <stb_image_write.h>
#define GPUVIS_TRACE_IMPLEMENTATION
#include "gpuvis_trace_utils.h"
static LogScope xwm_log("xwm");
///
// 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 ];
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;
extern std::atomic<uint64_t> g_lastVblank;
uint64_t timespec_to_nanos(struct timespec& spec)
{
return spec.tv_sec * 1'000'000'000ul + spec.tv_nsec;
}
static const gamescope_color_mgmt_t k_ScreenshotColorMgmt =
{
.enabled = true,
.displayColorimetry = displaycolorimetry_709,
.displayEOTF = EOTF_Gamma22,
.outputEncodingColorimetry = displaycolorimetry_709,
.outputEncodingEOTF = EOTF_Gamma22,
};
//#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,
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] = drm_quantize_lut_value( g_tmpLut1d.dataR[i] );
outColorMgmtLuts[nInputEOTF].lut1d[4*i+1] = drm_quantize_lut_value( g_tmpLut1d.dataG[i] );
outColorMgmtLuts[nInputEOTF].lut1d[4*i+2] = drm_quantize_lut_value( 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] = drm_quantize_lut_value( g_tmpLut3d.data[i].r );
outColorMgmtLuts[nInputEOTF].lut3d[4*i+1] = drm_quantize_lut_value( g_tmpLut3d.data[i].g );
outColorMgmtLuts[nInputEOTF].lut3d[4*i+2] = drm_quantize_lut_value( 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);
}
}
static void
update_color_mgmt()
{
// update pending native display colorimetry
if ( !BIsNested() )
{
drm_get_native_colorimetry( &g_DRM,
&g_ColorMgmt.pending.displayColorimetry, &g_ColorMgmt.pending.displayEOTF,
&g_ColorMgmt.pending.outputEncodingColorimetry, &g_ColorMgmt.pending.outputEncodingEOTF );
}
else
{
g_ColorMgmt.pending.displayColorimetry = displaycolorimetry_709;
g_ColorMgmt.pending.displayEOTF = EOTF_Gamma22;
g_ColorMgmt.pending.outputEncodingColorimetry = displaycolorimetry_709;
g_ColorMgmt.pending.outputEncodingEOTF = EOTF_Gamma22;
}
#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);
}
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;
}
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;
}
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;
}
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;
//
//
//
const uint32_t WS_OVERLAPPED = 0x00000000u;
const uint32_t WS_POPUP = 0x80000000u;
const uint32_t WS_CHILD = 0x40000000u;
const uint32_t WS_MINIMIZE = 0x20000000u;
const uint32_t WS_VISIBLE = 0x10000000u;
const uint32_t WS_DISABLED = 0x08000000u;
const uint32_t WS_CLIPSIBLINGS = 0x04000000u;
const uint32_t WS_CLIPCHILDREN = 0x02000000u;
const uint32_t WS_MAXIMIZE = 0x01000000u;
const uint32_t WS_BORDER = 0x00800000u;
const uint32_t WS_DLGFRAME = 0x00400000u;
const uint32_t WS_VSCROLL = 0x00200000u;
const uint32_t WS_HSCROLL = 0x00100000u;
const uint32_t WS_SYSMENU = 0x00080000u;
const uint32_t WS_THICKFRAME = 0x00040000u;
const uint32_t WS_GROUP = 0x00020000u;
const uint32_t WS_TABSTOP = 0x00010000u;
const uint32_t WS_MINIMIZEBOX = 0x00020000u;
const uint32_t WS_MAXIMIZEBOX = 0x00010000u;
const uint32_t WS_CAPTION = WS_BORDER | WS_DLGFRAME;
const uint32_t WS_TILED = WS_OVERLAPPED;
const uint32_t WS_ICONIC = WS_MINIMIZE;
const uint32_t WS_SIZEBOX = WS_THICKFRAME;
const uint32_t WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME| WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
const uint32_t WS_POPUPWINDOW = WS_POPUP | WS_BORDER | WS_SYSMENU;
const uint32_t WS_CHILDWINDOW = WS_CHILD;
const uint32_t WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW;
const uint32_t WS_EX_DLGMODALFRAME = 0x00000001u;
const uint32_t WS_EX_DRAGDETECT = 0x00000002u; // Undocumented
const uint32_t WS_EX_NOPARENTNOTIFY = 0x00000004u;
const uint32_t WS_EX_TOPMOST = 0x00000008u;
const uint32_t WS_EX_ACCEPTFILES = 0x00000010u;
const uint32_t WS_EX_TRANSPARENT = 0x00000020u;
const uint32_t WS_EX_MDICHILD = 0x00000040u;
const uint32_t WS_EX_TOOLWINDOW = 0x00000080u;
const uint32_t WS_EX_WINDOWEDGE = 0x00000100u;
const uint32_t WS_EX_CLIENTEDGE = 0x00000200u;
const uint32_t WS_EX_CONTEXTHELP = 0x00000400u;
const uint32_t WS_EX_RIGHT = 0x00001000u;
const uint32_t WS_EX_LEFT = 0x00000000u;
const uint32_t WS_EX_RTLREADING = 0x00002000u;
const uint32_t WS_EX_LTRREADING = 0x00000000u;
const uint32_t WS_EX_LEFTSCROLLBAR = 0x00004000u;
const uint32_t WS_EX_RIGHTSCROLLBAR = 0x00000000u;
const uint32_t WS_EX_CONTROLPARENT = 0x00010000u;
const uint32_t WS_EX_STATICEDGE = 0x00020000u;
const uint32_t WS_EX_APPWINDOW = 0x00040000u;
const uint32_t WS_EX_LAYERED = 0x00080000u;
const uint32_t WS_EX_NOINHERITLAYOUT = 0x00100000u;
const uint32_t WS_EX_NOREDIRECTIONBITMAP = 0x00200000u;
const uint32_t WS_EX_LAYOUTRTL = 0x00400000u;
const uint32_t WS_EX_COMPOSITED = 0x02000000u;
const uint32_t WS_EX_NOACTIVATE = 0x08000000u;
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;
};
struct commit_t
{
commit_t()
{
static uint64_t maxCommmitID = 0;
commitID = ++maxCommmitID;
}
~commit_t()
{
if ( fb_id != 0 )
{
drm_unlock_fbid( &g_DRM, fb_id );
fb_id = 0;
}
wlserver_lock();
if (!presentation_feedbacks.empty())
{
wlserver_presentation_feedback_discard(surf, presentation_feedbacks);
// presentation_feedbacks cleared by wlserver_presentation_feedback_discard
}
wlr_buffer_unlock( buf );
wlserver_unlock();
}
GamescopeAppTextureColorspace colorspace() const
{
VkColorSpaceKHR colorspace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
if (feedback && vulkanTex)
colorspace = feedback->vk_colorspace;
if (!vulkanTex)
return GAMESCOPE_APP_TEXTURE_COLORSPACE_LINEAR;
return VkColorSpaceToGamescopeAppTextureColorSpace(vulkanTex->format(), colorspace);
}
struct wlr_buffer *buf = nullptr;
uint32_t fb_id = 0;
std::shared_ptr<CVulkanTexture> vulkanTex;
uint64_t commitID = 0;
bool done = false;
bool async = false;
std::optional<wlserver_vk_swapchain_feedback> feedback = std::nullopt;
struct wlr_surface *surf = nullptr;
std::vector<struct wl_resource*> presentation_feedbacks;
};
static std::vector<pollfd> pollfds;
#define MWM_HINTS_FUNCTIONS 1
#define MWM_HINTS_DECORATIONS 2
#define MWM_HINTS_INPUT_MODE 4
#define MWM_HINTS_STATUS 8
#define MWM_FUNC_ALL 0x01
#define MWM_FUNC_RESIZE 0x02
#define MWM_FUNC_MOVE 0x04
#define MWM_FUNC_MINIMIZE 0x08
#define MWM_FUNC_MAXIMIZE 0x10
#define MWM_FUNC_CLOSE 0x20
#define MWM_DECOR_ALL 0x01
#define MWM_DECOR_BORDER 0x02
#define MWM_DECOR_RESIZEH 0x04
#define MWM_DECOR_TITLE 0x08
#define MWM_DECOR_MENU 0x10
#define MWM_DECOR_MINIMIZE 0x20
#define MWM_DECOR_MAXIMIZE 0x40
#define MWM_INPUT_MODELESS 0
#define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
#define MWM_INPUT_SYSTEM_MODAL 2
#define MWM_INPUT_FULL_APPLICATION_MODAL 3
#define MWM_INPUT_APPLICATION_MODAL 1
#define MWM_TEAROFF_WINDOW 1
int g_nAsyncFlipsEnabled = 0;
int g_nSteamMaxHeight = 0;
bool g_bVRRCapable_CachedValue = false;
bool g_bVRRInUse_CachedValue = false;
bool g_bSupportsST2084_CachedValue = false;
bool g_bForceHDR10OutputDebug = false;
bool g_bHDREnabled = false;
bool g_bHDRItmEnable = false;
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;
}
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;
bool focusDirty = false;
bool hasRepaint = false;
bool hasRepaintNonBasePlane = false;
unsigned long damageSequence = 0;
unsigned int cursorHideTime = 10'000;
bool gotXError = false;
unsigned int fadeOutStartTime = 0;
unsigned int g_FadeOutDuration = 0;
extern float g_flMaxWindowScale;
bool synchronize;
std::mutex g_SteamCompMgrXWaylandServerMutex;
uint64_t g_SteamCompMgrVBlankTime = 0;
static int g_nSteamCompMgrTargetFPS = 0;
static uint64_t g_uDynamicRefreshEqualityTime = 0;
static int g_nDynamicRefreshRate[DRM_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_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 steamcompmgr_window_should_limit_fps( steamcompmgr_win_t *w )
{
return w && !window_is_steam( w ) && !w->isOverlay && !w->isExternalOverlay;
}
enum HeldCommitTypes_t
{
HELD_COMMIT_BASE,
HELD_COMMIT_FADE,
HELD_COMMIT_COUNT,
};
std::array<std::shared_ptr<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;
static 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;
uint32_t fb_id;
};
static std::mutex wlr_buffer_map_lock;
static std::unordered_map<struct wlr_buffer*, wlr_buffer_map_entry> wlr_buffer_map;
static std::atomic< bool > g_bTakeScreenshot{false};
static bool g_bPropertyRequestedScreenshot;
static std::atomic<bool> g_bForceRepaint{false};
static int g_nudgePipe[2] = {-1, -1};
static int g_nCursorScaleHeight = -1;
// 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;
};
struct WaitListEntry_t
{
CommitDoneList_t *doneCommits;
int fence;
// Josh: Whether or not to nudge mangoapp that we got
// a frame as soon as we know this commit is done.
// This could technically be out of date if we change windows
// but for a max couple frames of inaccuracy when switching windows
// compared to being all over the place from handling in the
// steamcompmgr thread in handle_done_commits, it is worth it.
bool mangoapp_nudge;
uint64_t commitID;
};
sem waitListSem;
std::mutex waitListLock;
std::vector< WaitListEntry_t > waitList;
bool imageWaitThreadRun = true;
void imageWaitThreadMain( void )
{
pthread_setname_np( pthread_self(), "gamescope-img" );
wait:
waitListSem.wait();
if ( imageWaitThreadRun == false )
{
return;
}
bool bFound = false;
WaitListEntry_t entry;
retry:
{
std::unique_lock< std::mutex > lock( waitListLock );
if( waitList.empty() )
{
goto wait;
}
entry = waitList[ 0 ];
bFound = true;
waitList.erase( waitList.begin() );
}
assert( bFound == true );
gpuvis_trace_begin_ctx_printf( entry.commitID, "wait fence" );
struct pollfd fd = { entry.fence, POLLIN, 0 };
int ret = poll( &fd, 1, 100 );
if ( ret < 0 )
{
xwm_log.errorf_errno( "failed to poll fence FD" );
}
gpuvis_trace_end_ctx_printf( entry.commitID, "wait fence" );
close( entry.fence );
uint64_t frametime;
if ( entry.mangoapp_nudge )
{
uint64_t now = get_time_in_nanos();
static uint64_t lastFrameTime = now;
frametime = now - lastFrameTime;
lastFrameTime = now;
}
{
std::unique_lock< std::mutex > lock( entry.doneCommits->listCommitsDoneLock );
entry.doneCommits->listCommitsDone.push_back( entry.commitID );
}
nudge_steamcompmgr();
if ( entry.mangoapp_nudge )
mangoapp_update( frametime, frametime, uint64_t(~0ull) );
goto retry;
}
sem statsThreadSem;
std::mutex statsEventQueueLock;
std::vector< std::string > statsEventQueue;
std::string statsThreadPath;
int statsPipeFD = -1;
bool statsThreadRun;
void statsThreadMain( void )
{
pthread_setname_np( pthread_self(), "gamescope-stats" );
signal(SIGPIPE, SIG_IGN);
while ( statsPipeFD == -1 )
{
statsPipeFD = open( statsThreadPath.c_str(), O_WRONLY | O_CLOEXEC );
if ( statsPipeFD == -1 )
{
sleep( 10 );
}
}
wait:
statsThreadSem.wait();
if ( statsThreadRun == false )
{
return;
}
std::string event;
retry:
{
std::unique_lock< std::mutex > lock( statsEventQueueLock );
if( statsEventQueue.empty() )
{
goto wait;
}
event = statsEventQueue[ 0 ];
statsEventQueue.erase( statsEventQueue.begin() );
}
dprintf( statsPipeFD, "%s", event.c_str() );
goto retry;
}
static inline void stats_printf( const char* format, ...)
{
static char buffer[256];
static std::string eventstr;
va_list args;
va_start(args, format);
vsprintf(buffer,format, args);
va_end(args);