-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupstream.patch
More file actions
3602 lines (3376 loc) · 144 KB
/
upstream.patch
File metadata and controls
3602 lines (3376 loc) · 144 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
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
From 261dc46d54aa1c7f5a7363807c39ae1f26b3ab49 Mon Sep 17 00:00:00 2001
From: Joshua Ashton <joshua@froggi.es>
Date: Mon, 15 Jul 2024 14:51:16 +0100
Subject: [PATCH 01/19] Utils: Add small Algorithm helper file
---
src/Utils/Algorithm.h | 173 ++++++++++++++++++++++++++++++++++++++++++
src/color_bench.cpp | 137 +++++++++++++++++++++++++++++++++
2 files changed, 310 insertions(+)
create mode 100644 src/Utils/Algorithm.h
diff --git a/src/Utils/Algorithm.h b/src/Utils/Algorithm.h
new file mode 100644
index 0000000..7290367
--- /dev/null
+++ b/src/Utils/Algorithm.h
@@ -0,0 +1,173 @@
+#pragma once
+
+#include <iterator>
+#include <span>
+
+namespace gamescope::Algorithm
+{
+ template <typename TObj>
+ constexpr TObj *Begin( std::span<TObj> span )
+ {
+ return span.data();
+ }
+
+ template <typename TObj>
+ constexpr TObj *End( std::span<TObj> span )
+ {
+ return Begin( span ) + span.size();
+ }
+
+ template <typename TIter, typename TObj>
+ constexpr TIter FindSimple( TIter pFirst, TIter pEnd, const TObj &obj )
+ {
+ while ( pFirst != pEnd && *pFirst != obj )
+ ++pFirst;
+
+ return pFirst;
+ }
+
+ template <typename TIter, typename TObj>
+ constexpr TIter FindByFour( TIter pFirst, TIter pEnd, const TObj &obj )
+ {
+ typename std::iterator_traits< TIter >::difference_type ulTripCount = ( pEnd - pFirst ) >> 2;
+
+ while ( ulTripCount-- > 0 )
+ {
+ if ( pFirst[0] == obj )
+ return &pFirst[0];
+
+ if ( pFirst[1] == obj )
+ return &pFirst[1];
+
+ if ( pFirst[2] == obj )
+ return &pFirst[2];
+
+ if ( pFirst[3] == obj )
+ return &pFirst[3];
+
+ pFirst += 4;
+ }
+
+ switch ( pEnd - pFirst )
+ {
+ case 3:
+ {
+ if ( pFirst[0] == obj )
+ return &pFirst[0];
+
+ if ( pFirst[1] == obj )
+ return &pFirst[1];
+
+ if ( pFirst[2] == obj )
+ return &pFirst[2];
+
+ return pEnd;
+ }
+ case 2:
+ {
+ if ( pFirst[0] == obj )
+ return &pFirst[0];
+
+ if ( pFirst[1] == obj )
+ return &pFirst[1];
+
+ return pEnd;
+ }
+ case 1:
+ {
+ if ( pFirst[0] == obj )
+ return &pFirst[0];
+
+ return pEnd;
+ }
+ case 0:
+ {
+ return pEnd;
+ }
+ default:
+ {
+ __builtin_unreachable();
+ }
+ }
+ }
+
+ template <typename TIter, typename TObj>
+ constexpr TIter Find( TIter pFirst, TIter pEnd, const TObj &obj )
+ {
+ return FindSimple( pFirst, pEnd, obj );
+ }
+
+ template <typename TIter, typename TObj>
+ constexpr TIter Find( std::span<TObj> span, const TObj &obj )
+ {
+ return Find( Begin( span ), End( span ), obj );
+ }
+
+ template <typename TIter, typename TObj>
+ constexpr bool ContainsShortcut( TIter pFirst, TIter pEnd, const TObj &obj )
+ {
+ return Find( pFirst, pEnd, obj ) != pEnd;
+ }
+
+ template <typename TIter, typename TObj>
+ constexpr bool ContainsNoShortcut( TIter pFirst, TIter pEnd, const TObj &obj )
+ {
+ bool bFound = false;
+
+ typename std::iterator_traits< TIter >::difference_type ulTripCount = ( pEnd - pFirst ) >> 2;
+
+ while ( ulTripCount-- > 0 )
+ {
+ bFound |= pFirst[0] == obj ||
+ pFirst[1] == obj ||
+ pFirst[2] == obj ||
+ pFirst[3] == obj;
+
+ pFirst += 4;
+ }
+
+ switch ( pEnd - pFirst )
+ {
+ case 3:
+ {
+ bFound |= pFirst[0] == obj ||
+ pFirst[1] == obj ||
+ pFirst[2] == obj;
+ break;
+ }
+ case 2:
+ {
+ bFound |= pFirst[0] == obj ||
+ pFirst[1] == obj;
+ break;
+ }
+ case 1:
+ {
+ bFound |= pFirst[0] == obj;
+ break;
+ }
+ case 0:
+ {
+ break;
+ }
+ default:
+ {
+ __builtin_unreachable();
+ }
+ }
+
+ return bFound;
+ }
+
+ template <typename TIter, typename TObj>
+ constexpr bool Contains( TIter pFirst, TIter pEnd, const TObj &obj )
+ {
+ return ContainsNoShortcut( pFirst, pEnd, obj );
+ }
+
+ template <typename TSpanObj, typename TObj>
+ constexpr bool Contains( std::span<TSpanObj> span, const TObj &obj )
+ {
+ return Contains( Begin( span ), End( span ), obj );
+ }
+}
diff --git a/src/color_bench.cpp b/src/color_bench.cpp
index 33dff78..9c9d986 100644
--- a/src/color_bench.cpp
+++ b/src/color_bench.cpp
@@ -1,5 +1,9 @@
+#include <array>
#include <benchmark/benchmark.h>
+#include <algorithm>
+#include "Utils/Algorithm.h"
+
#include "color_helpers.h"
const uint32_t nLutSize1d = 4096;
@@ -74,4 +78,137 @@ static void BenchmarkCalcColorTransforms(benchmark::State &state)
}
BENCHMARK(BenchmarkCalcColorTransforms);
+static constexpr uint32_t k_uFindTestValueCountLarge = 524288;
+static constexpr uint32_t k_uFindTestValueCountMedium = 16;
+static constexpr uint32_t k_uFindTestValueCountSmall = 5;
+
+template <uint32_t uSize>
+static __attribute__((noinline)) std::array<int, uSize> GetFindTestValues()
+{
+ static std::array<int, uSize> s_Values = []()
+ {
+ std::array<int, uSize> values;
+ for ( uint32_t i = 0; i < uSize; i++ )
+ values[i] = rand() % 255;
+
+ return values;
+ }();
+
+ return s_Values;
+}
+
+// Large
+
+static void Benchmark_Find_Large_Gamescope(benchmark::State &state)
+{
+ std::array<int, k_uFindTestValueCountLarge> values = GetFindTestValues<k_uFindTestValueCountLarge>();
+
+ for (auto _ : state)
+ {
+ auto iter = gamescope::Algorithm::Find( values.begin(), values.end(), 765678478 );
+ benchmark::DoNotOptimize( iter );
+ }
+}
+BENCHMARK(Benchmark_Find_Large_Gamescope);
+
+static void Benchmark_Find_Large_Std(benchmark::State &state)
+{
+ std::array<int, k_uFindTestValueCountLarge> values = GetFindTestValues<k_uFindTestValueCountLarge>();
+
+ for (auto _ : state)
+ {
+ auto iter = std::find( values.begin(), values.end(), 765678478 );
+ benchmark::DoNotOptimize( iter );
+ }
+}
+BENCHMARK(Benchmark_Find_Large_Std);
+
+static void Benchmark_Contains_Large_Gamescope(benchmark::State &state)
+{
+ std::array<int, k_uFindTestValueCountLarge> values = GetFindTestValues<k_uFindTestValueCountLarge>();
+
+ for (auto _ : state)
+ {
+ bool bContains = gamescope::Algorithm::ContainsNoShortcut( values.begin(), values.end(), 765678478 );
+ benchmark::DoNotOptimize( bContains );
+ }
+}
+BENCHMARK(Benchmark_Contains_Large_Gamescope);
+
+//
+
+static void Benchmark_Find_Medium_Gamescope(benchmark::State &state)
+{
+ std::array<int, k_uFindTestValueCountMedium> values = GetFindTestValues<k_uFindTestValueCountMedium>();
+
+ for (auto _ : state)
+ {
+ auto iter = gamescope::Algorithm::Find( values.begin(), values.end(), 765678478 );
+ benchmark::DoNotOptimize( iter );
+ }
+}
+BENCHMARK(Benchmark_Find_Medium_Gamescope);
+
+static void Benchmark_Find_Medium_Std(benchmark::State &state)
+{
+ std::array<int, k_uFindTestValueCountMedium> values = GetFindTestValues<k_uFindTestValueCountMedium>();
+
+ for (auto _ : state)
+ {
+ auto iter = std::find( values.begin(), values.end(), 765678478 );
+ benchmark::DoNotOptimize( iter );
+ }
+}
+BENCHMARK(Benchmark_Find_Medium_Std);
+
+static void Benchmark_Contains_Medium_Gamescope(benchmark::State &state)
+{
+ std::array<int, k_uFindTestValueCountMedium> values = GetFindTestValues<k_uFindTestValueCountMedium>();
+
+ for (auto _ : state)
+ {
+ bool bContains = gamescope::Algorithm::ContainsNoShortcut( values.begin(), values.end(), 765678478 );
+ benchmark::DoNotOptimize( bContains );
+ }
+}
+BENCHMARK(Benchmark_Contains_Medium_Gamescope);
+
+//
+
+static void Benchmark_Find_Small_Gamescope(benchmark::State &state)
+{
+ std::array<int, k_uFindTestValueCountSmall> values = GetFindTestValues<k_uFindTestValueCountSmall>();
+
+ for (auto _ : state)
+ {
+ auto iter = gamescope::Algorithm::Find( values.begin(), values.end(), 765678478 );
+ benchmark::DoNotOptimize( iter );
+ }
+}
+BENCHMARK(Benchmark_Find_Small_Gamescope);
+
+static void Benchmark_Find_Small_Std(benchmark::State &state)
+{
+ std::array<int, k_uFindTestValueCountSmall> values = GetFindTestValues<k_uFindTestValueCountSmall>();
+
+ for (auto _ : state)
+ {
+ auto iter = std::find( values.begin(), values.end(), 765678478 );
+ benchmark::DoNotOptimize( iter );
+ }
+}
+BENCHMARK(Benchmark_Find_Small_Std);
+
+static void Benchmark_Contains_Small_Gamescope(benchmark::State &state)
+{
+ std::array<int, k_uFindTestValueCountSmall> values = GetFindTestValues<k_uFindTestValueCountSmall>();
+
+ for (auto _ : state)
+ {
+ bool bContains = gamescope::Algorithm::ContainsNoShortcut( values.begin(), values.end(), 765678478 );
+ benchmark::DoNotOptimize( bContains );
+ }
+}
+BENCHMARK(Benchmark_Contains_Small_Gamescope);
+
BENCHMARK_MAIN();
--
2.45.2
From 74a020865906414f9d4374fa5568fffce71e4027 Mon Sep 17 00:00:00 2001
From: Joshua Ashton <joshua@froggi.es>
Date: Mon, 15 Jul 2024 14:52:02 +0100
Subject: [PATCH 02/19] Utils: Use Contains in CloseAllFds
---
src/Utils/Process.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/Utils/Process.cpp b/src/Utils/Process.cpp
index 5688fb1..32c52f1 100644
--- a/src/Utils/Process.cpp
+++ b/src/Utils/Process.cpp
@@ -1,4 +1,5 @@
#include "Process.h"
+#include "../Utils/Algorithm.h"
#include "../convar.h"
#include "../log.hpp"
#include "../Utils/Defer.h"
@@ -268,7 +269,7 @@ namespace gamescope::Process
int nFd = *onFd;
- bool bExcluded = std::find( nExcludedFds.begin(), nExcludedFds.end(), nFd ) != nExcludedFds.end();
+ bool bExcluded = Algorithm::Contains( nExcludedFds, nFd );
if ( bExcluded )
continue;
--
2.45.2
From 3c5a232269ef8adef56423bf9b2bb48cb5c0cd1d Mon Sep 17 00:00:00 2001
From: Joshua Ashton <joshua@froggi.es>
Date: Mon, 15 Jul 2024 15:52:33 +0100
Subject: [PATCH 03/19] steamcompmgr: Only forward relative mouse mode if we
have a cursor constraint
---
src/steamcompmgr.cpp | 13 ++++++-------
src/wlserver.cpp | 32 +++++++++++++++++++-------------
src/wlserver.hpp | 26 +++++++++++++++++++++++++-
3 files changed, 50 insertions(+), 21 deletions(-)
diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp
index 9ee265d..f051463 100644
--- a/src/steamcompmgr.cpp
+++ b/src/steamcompmgr.cpp
@@ -682,7 +682,6 @@ constexpr const T& clamp( const T& x, const T& min, const T& max )
}
extern bool g_bForceRelativeMouse;
-bool bSteamCompMgrGrab = false;
CommitDoneList_t g_steamcompmgr_xdg_done_commits;
@@ -7116,8 +7115,6 @@ steamcompmgr_main(int argc, char **argv)
// Reset getopt() state
optind = 1;
- bSteamCompMgrGrab = GetBackend()->GetNestedHints() && g_bForceRelativeMouse;
-
int o;
int opt_index = -1;
bool bForceWindowsFullscreen = false;
@@ -7574,13 +7571,15 @@ steamcompmgr_main(int argc, char **argv)
if ( GetBackend()->GetNestedHints() && !g_bForceRelativeMouse )
{
- bool bImageEmpty =
+ const bool bImageEmpty =
( global_focus.cursor && global_focus.cursor->imageEmpty() ) &&
( !window_is_steam( global_focus.inputFocusWindow ) );
- if ( GetBackend()->GetNestedHints() )
- GetBackend()->GetNestedHints()->SetRelativeMouseMode( bImageEmpty );
- bSteamCompMgrGrab = GetBackend()->GetNestedHints() && bImageEmpty;
+ const bool bHasPointerConstraint = wlserver.HasMouseConstraint(); // atomic, no lock needed
+
+ const bool bRelativeMouseMode = bImageEmpty && bHasPointerConstraint;
+
+ GetBackend()->GetNestedHints()->SetRelativeMouseMode( bRelativeMouseMode );
}
static int nIgnoredOverlayRepaints = 0;
diff --git a/src/wlserver.cpp b/src/wlserver.cpp
index ee6891d..1852be9 100644
--- a/src/wlserver.cpp
+++ b/src/wlserver.cpp
@@ -225,7 +225,8 @@ std::optional<ResListEntry_t> PrepareCommit( struct wlr_surface *surf, struct wl
struct wlr_surface *pConstraintSurface = wlserver_surface_to_main_surface( surf );
- if ( wlserver.mouse_constraint && wlserver.mouse_constraint->surface == pConstraintSurface )
+ struct wlr_pointer_constraint_v1 *pConstraint = wlserver.GetCursorConstraint();
+ if ( pConstraint && pConstraint->surface == pConstraintSurface )
wlserver_update_cursor_constraint();
return oNewEntry;
@@ -2235,7 +2236,7 @@ struct GamescopePointerConstraint
static void wlserver_warp_to_constraint_hint()
{
- struct wlr_pointer_constraint_v1 *pConstraint = wlserver.mouse_constraint;
+ struct wlr_pointer_constraint_v1 *pConstraint = wlserver.GetCursorConstraint();
if (pConstraint->current.cursor_hint.enabled)
{
@@ -2248,7 +2249,7 @@ static void wlserver_warp_to_constraint_hint()
static void wlserver_update_cursor_constraint()
{
- struct wlr_pointer_constraint_v1 *pConstraint = wlserver.mouse_constraint;
+ struct wlr_pointer_constraint_v1 *pConstraint = wlserver.GetCursorConstraint();
pixman_region32_t *pRegion = &pConstraint->region;
if ( wlserver.mouse_constraint_requires_warp && pConstraint->surface )
@@ -2278,27 +2279,29 @@ static void wlserver_update_cursor_constraint()
static void wlserver_constrain_cursor( struct wlr_pointer_constraint_v1 *pNewConstraint )
{
- if ( wlserver.mouse_constraint == pNewConstraint )
+ struct wlr_pointer_constraint_v1 *pOldConstraint = wlserver.GetCursorConstraint();
+
+ if ( pOldConstraint == pNewConstraint )
return;
- if ( wlserver.mouse_constraint )
+ if ( pOldConstraint )
{
if ( !pNewConstraint )
wlserver_warp_to_constraint_hint();
- wlr_pointer_constraint_v1_send_deactivated(wlserver.mouse_constraint);
+ wlr_pointer_constraint_v1_send_deactivated( pOldConstraint );
}
- wlserver.mouse_constraint = pNewConstraint;
+ wlserver.SetMouseConstraint( pNewConstraint );
- if ( !wlserver.mouse_constraint )
+ if ( !pNewConstraint )
return;
wlserver.mouse_constraint_requires_warp = true;
wlserver_update_cursor_constraint();
- wlr_pointer_constraint_v1_send_activated( wlserver.mouse_constraint );
+ wlr_pointer_constraint_v1_send_activated( pNewConstraint );
}
static void handle_pointer_constraint_set_region(struct wl_listener *listener, void *data)
@@ -2316,11 +2319,12 @@ void handle_constraint_destroy(struct wl_listener *listener, void *data)
wl_list_remove(&pGamescopeConstraint->set_region.link);
wl_list_remove(&pGamescopeConstraint->destroy.link);
- if (wlserver.mouse_constraint == pGamescopeConstraint->pConstraint)
+ struct wlr_pointer_constraint_v1 *pCurrentConstraint = wlserver.GetCursorConstraint();
+ if ( pCurrentConstraint == pGamescopeConstraint->pConstraint )
{
wlserver_warp_to_constraint_hint();
- wlserver.mouse_constraint = nullptr;
+ wlserver.SetMouseConstraint( nullptr );
}
delete pGamescopeConstraint;
@@ -2345,9 +2349,11 @@ static void handle_pointer_constraint(struct wl_listener *listener, void *data)
static bool wlserver_apply_constraint( double *dx, double *dy )
{
- if ( wlserver.mouse_constraint )
+ struct wlr_pointer_constraint_v1 *pConstraint = wlserver.GetCursorConstraint();
+
+ if ( pConstraint )
{
- if ( wlserver.mouse_constraint->type == WLR_POINTER_CONSTRAINT_V1_LOCKED )
+ if ( pConstraint->type == WLR_POINTER_CONSTRAINT_V1_LOCKED )
return false;
double sx = wlserver.mouse_surface_cursorx;
diff --git a/src/wlserver.hpp b/src/wlserver.hpp
index db7d491..467c54d 100644
--- a/src/wlserver.hpp
+++ b/src/wlserver.hpp
@@ -70,6 +70,8 @@ struct ResListEntry_t {
struct wlserver_content_override;
+bool wlserver_is_lock_held(void);
+
class gamescope_xwayland_server_t
{
public:
@@ -149,7 +151,29 @@ struct wlserver_t {
double mouse_surface_cursory = 0.0f;
bool mouse_constraint_requires_warp = false;
pixman_region32_t confine;
- struct wlr_pointer_constraint_v1 *mouse_constraint = nullptr;
+ std::atomic<struct wlr_pointer_constraint_v1 *> mouse_constraint = { nullptr };
+
+ void SetMouseConstraint( struct wlr_pointer_constraint_v1 *pConstraint )
+ {
+ assert( wlserver_is_lock_held() );
+ // Set by wlserver only. Read by both wlserver + steamcompmgr with no
+ // need to actually be sequentially consistent.
+ mouse_constraint.store( pConstraint, std::memory_order_relaxed );
+ }
+
+ struct wlr_pointer_constraint_v1 *GetCursorConstraint() const
+ {
+ assert( wlserver_is_lock_held() );
+ return mouse_constraint.load( std::memory_order_relaxed );
+ }
+
+ bool HasMouseConstraint() const
+ {
+ // Does not need to be sequentially consistent.
+ // Used by the steamcompmgr thread to check if there is currently a mouse constraint.
+ return mouse_constraint.load( std::memory_order_relaxed ) != nullptr;
+ }
+
uint64_t ulLastMovedCursorTime = 0;
bool bCursorHidden = true;
bool bCursorHasImage = true;
--
2.45.2
From 745d0d6a09e53a8481bc94c67e3a3ae5c86c8796 Mon Sep 17 00:00:00 2001
From: Joshua Ashton <joshua@froggi.es>
Date: Mon, 15 Jul 2024 20:21:16 +0100
Subject: [PATCH 04/19] convar: Add some helpers for std::string convars and
callbacks
---
src/convar.h | 34 ++++++++++++++++++++++++++++++++--
1 file changed, 32 insertions(+), 2 deletions(-)
diff --git a/src/convar.h b/src/convar.h
index c08dccb..f2a0485 100644
--- a/src/convar.h
+++ b/src/convar.h
@@ -20,6 +20,24 @@ namespace gamescope
{
class ConCommand;
+ template <typename T>
+ inline std::string ToString( const T &thing )
+ {
+ return std::to_string( thing );
+ }
+
+ template <>
+ inline std::string ToString( const std::string &sThing )
+ {
+ return sThing;
+ }
+
+ template <>
+ inline std::string ToString( const std::string_view &svThing )
+ {
+ return std::string( svThing );
+ }
+
template <typename T>
inline std::optional<T> Parse( std::string_view chars )
{
@@ -113,11 +131,15 @@ namespace gamescope
{
using ConVarCallbackFunc = std::function<void()>;
public:
- ConVar( std::string_view pszName, T defaultValue = T{}, std::string_view pszDescription = "", ConVarCallbackFunc func = nullptr )
+ ConVar( std::string_view pszName, T defaultValue = T{}, std::string_view pszDescription = "", ConVarCallbackFunc func = nullptr, bool bRunCallbackAtStartup = false )
: ConCommand( pszName, pszDescription, [this]( std::span<std::string_view> pArgs ){ this->InvokeFunc( pArgs ); } )
, m_Value{ defaultValue }
, m_Callback{ func }
{
+ if ( bRunCallbackAtStartup )
+ {
+ RunCallback();
+ }
}
const T& Get() const
@@ -130,6 +152,11 @@ namespace gamescope
{
m_Value = T{ newValue };
+ RunCallback();
+ }
+
+ void RunCallback()
+ {
if ( !m_bInCallback && m_Callback )
{
m_bInCallback = true;
@@ -143,6 +170,9 @@ namespace gamescope
operator T() const { return m_Value; }
+ // SFINAE for std::string...
+ operator std::string_view() const { return m_Value; }
+
template <typename J> bool operator == ( const J &other ) const { return m_Value == other; }
template <typename J> bool operator != ( const J &other ) const { return m_Value != other; }
template <typename J> auto operator <=>( const J &other ) const { return m_Value <=> other; }
@@ -158,7 +188,7 @@ namespace gamescope
{
// We should move to std format for logging and stuff.
// This is kinda gross and grody!
- std::string sValue = std::to_string( m_Value );
+ std::string sValue = ToString( m_Value );
console_log.infof( "%.*s: %.*s\n%.*s",
(int)m_pszName.length(), m_pszName.data(),
(int)sValue.length(), sValue.data(),
--
2.45.2
From 69f94d99082f4b0c5e06c384d65705739608ca2f Mon Sep 17 00:00:00 2001
From: Joshua Ashton <joshua@froggi.es>
Date: Mon, 15 Jul 2024 20:21:30 +0100
Subject: [PATCH 05/19] Utils: Add helpers for std::vector in our Algorithm
helpers
---
src/Utils/Algorithm.h | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/src/Utils/Algorithm.h b/src/Utils/Algorithm.h
index 7290367..eb51a79 100644
--- a/src/Utils/Algorithm.h
+++ b/src/Utils/Algorithm.h
@@ -2,6 +2,7 @@
#include <iterator>
#include <span>
+#include <vector>
namespace gamescope::Algorithm
{
@@ -17,6 +18,18 @@ namespace gamescope::Algorithm
return Begin( span ) + span.size();
}
+ template <typename TObj>
+ constexpr const TObj *Begin( const std::vector<TObj> &vec )
+ {
+ return vec.data();
+ }
+
+ template <typename TObj>
+ constexpr const TObj *End( const std::vector<TObj> &vec )
+ {
+ return Begin( vec ) + vec.size();
+ }
+
template <typename TIter, typename TObj>
constexpr TIter FindSimple( TIter pFirst, TIter pEnd, const TObj &obj )
{
@@ -103,6 +116,12 @@ namespace gamescope::Algorithm
return Find( Begin( span ), End( span ), obj );
}
+ template <typename TIter, typename TObj>
+ constexpr TIter Find( const std::vector<TObj> &vec, const TObj &obj )
+ {
+ return Find( Begin( vec ), End( vec ), obj );
+ }
+
template <typename TIter, typename TObj>
constexpr bool ContainsShortcut( TIter pFirst, TIter pEnd, const TObj &obj )
{
@@ -170,4 +189,10 @@ namespace gamescope::Algorithm
{
return Contains( Begin( span ), End( span ), obj );
}
+
+ template <typename TVectorObj, typename TObj>
+ constexpr bool Contains( const std::vector<TVectorObj> &vec, const TObj &obj )
+ {
+ return Contains( Begin( vec ), End( vec ), obj );
+ }
}
--
2.45.2
From dade66318d852387bf8f1e91427dc7e2b5511826 Mon Sep 17 00:00:00 2001
From: Joshua Ashton <joshua@froggi.es>
Date: Mon, 15 Jul 2024 20:28:05 +0100
Subject: [PATCH 06/19] steamcompmgr: Add filter appids for relative mouse mode
---
src/steamcompmgr.cpp | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp
index f051463..b955a0c 100644
--- a/src/steamcompmgr.cpp
+++ b/src/steamcompmgr.cpp
@@ -92,6 +92,7 @@
#include "commit.h"
#include "BufferMemo.h"
#include "Utils/Process.h"
+#include "Utils/Algorithm.h"
#if HAVE_AVIF
#include "avif/avif.h"
@@ -7022,6 +7023,24 @@ static gamescope::ConCommand cc_launch( "launch", "Launch an application with th
gamescope::ConVar<bool> cv_shutdown_on_primary_child_death( "shutdown_on_primary_child_death", true, "Should gamescope shutdown when the primary application launched in it was shut down?" );
static LogScope s_LaunchLogScope( "launch" );
+static std::vector<uint32_t> s_uRelativeMouseFilteredAppids;
+static gamescope::ConVar<std::string> cv_mouse_relative_filter_appids( "mouse_relative_filter_appids",
+"8400" /* Geometry Wars: Retro Evolved */,
+"Comma separated appids to filter out using relative mouse mode for.",
+[]()
+{
+ std::vector<std::string_view> sFilterAppids = gamescope::Split( cv_mouse_relative_filter_appids, "," );
+ std::vector<uint32_t> uFilterAppids;
+ uFilterAppids.reserve( sFilterAppids.size() );
+ for ( auto &sFilterAppid : sFilterAppids )
+ {
+ std::optional<uint32_t> ouFilterAppid = gamescope::Parse<uint32_t>( sFilterAppid );
+ uFilterAppids.push_back( *ouFilterAppid );
+ }
+
+ s_uRelativeMouseFilteredAppids = std::move( uFilterAppids );
+}, true);
+
void LaunchNestedChildren( char **ppPrimaryChildArgv )
{
std::string sNewPreload;
@@ -7577,7 +7596,13 @@ steamcompmgr_main(int argc, char **argv)
const bool bHasPointerConstraint = wlserver.HasMouseConstraint(); // atomic, no lock needed
- const bool bRelativeMouseMode = bImageEmpty && bHasPointerConstraint;
+ uint32_t uAppId = global_focus.inputFocusWindow
+ ? global_focus.inputFocusWindow->appID
+ : 0;
+
+ const bool bExcludedAppId = uAppId && gamescope::Algorithm::Contains( s_uRelativeMouseFilteredAppids, uAppId );
+
+ const bool bRelativeMouseMode = bImageEmpty && bHasPointerConstraint && !bExcludedAppId;
GetBackend()->GetNestedHints()->SetRelativeMouseMode( bRelativeMouseMode );
}
--
2.45.2
From 6c187b7f69d5f7e7d1a01728c1c22e055a1683f6 Mon Sep 17 00:00:00 2001
From: Joshua Ashton <joshua@froggi.es>
Date: Thu, 18 Jul 2024 00:53:44 +0100
Subject: [PATCH 07/19] steamcompmgr: Add adaptive sync convar
---
src/Backends/OpenVRBackend.cpp | 1 -
src/Backends/WaylandBackend.cpp | 4 ++--
src/main.cpp | 4 ++--
src/steamcompmgr.cpp | 8 ++++----
4 files changed, 8 insertions(+), 9 deletions(-)
diff --git a/src/Backends/OpenVRBackend.cpp b/src/Backends/OpenVRBackend.cpp
index acc84ed..79b05d1 100644
--- a/src/Backends/OpenVRBackend.cpp
+++ b/src/Backends/OpenVRBackend.cpp
@@ -38,7 +38,6 @@ extern int g_nPreferredOutputWidth;
extern int g_nPreferredOutputHeight;
extern bool g_bForceHDR10OutputDebug;
extern bool g_bBorderlessOutputWindow;
-extern bool g_bAllowVRR;
extern gamescope::ConVar<bool> cv_composite_force;
extern bool g_bColorSliderInUse;
diff --git a/src/Backends/WaylandBackend.cpp b/src/Backends/WaylandBackend.cpp
index 6f578a2..3603be7 100644
--- a/src/Backends/WaylandBackend.cpp
+++ b/src/Backends/WaylandBackend.cpp
@@ -40,7 +40,7 @@ extern int g_nPreferredOutputWidth;
extern int g_nPreferredOutputHeight;
extern bool g_bForceHDR10OutputDebug;
extern bool g_bBorderlessOutputWindow;
-extern bool g_bAllowVRR;
+extern gamescope::ConVar<bool> cv_adaptive_sync;
extern gamescope::ConVar<bool> cv_composite_force;
extern bool g_bColorSliderInUse;
@@ -1531,7 +1531,7 @@ namespace gamescope
}
bool CWaylandBackend::IsVRRActive() const
{
- return g_bAllowVRR && m_bHostCompositorIsCurrentlyVRR;
+ return cv_adaptive_sync && m_bHostCompositorIsCurrentlyVRR;
}
bool CWaylandBackend::SupportsPlaneHardwareCursor() const
diff --git a/src/main.cpp b/src/main.cpp
index cd4aeca..da1b516 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -42,7 +42,7 @@
using namespace std::literals;
EStreamColorspace g_ForcedNV12ColorSpace = k_EStreamColorspace_Unknown;
-extern bool g_bAllowVRR;
+extern gamescope::ConVar<bool> cv_adaptive_sync;
const char *gamescope_optstring = nullptr;
const char *g_pOriginalDisplay = nullptr;
@@ -765,7 +765,7 @@ int main(int argc, char **argv)
} else if (strcmp(opt_name, "display-index") == 0) {
g_nNestedDisplayIndex = atoi( optarg );
} else if (strcmp(opt_name, "adaptive-sync") == 0) {
- g_bAllowVRR = true;
+ cv_adaptive_sync = true;
} else if (strcmp(opt_name, "expose-wayland") == 0) {
g_bExposeWayland = true;
} else if (strcmp(opt_name, "backend") == 0) {
diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp
index b955a0c..b8102eb 100644
--- a/src/steamcompmgr.cpp
+++ b/src/steamcompmgr.cpp
@@ -187,7 +187,7 @@ timespec nanos_to_timespec( uint64_t ulNanos )
static void
update_runtime_info();
-bool g_bAllowVRR = false;
+gamescope::ConVar<bool> cv_adaptive_sync( "adaptive_sync", false, "Whether or not adaptive sync is enabled if available." );
uint64_t g_SteamCompMgrLimitedAppRefreshCycle = 16'666'666;
uint64_t g_SteamCompMgrAppRefreshCycle = 16'666'666;
@@ -2229,7 +2229,7 @@ paint_all(bool async)
struct FrameInfo_t frameInfo = {};
frameInfo.applyOutputColorMgmt = g_ColorMgmt.pending.enabled;
frameInfo.outputEncodingEOTF = g_ColorMgmt.pending.outputEncodingEOTF;
- frameInfo.allowVRR = g_bAllowVRR;
+ frameInfo.allowVRR = cv_adaptive_sync;
frameInfo.bFadingOut = fadingOut;
// If the window we'd paint as the base layer is the streaming client,
@@ -5410,7 +5410,7 @@ handle_property_notify(xwayland_ctx_t *ctx, XPropertyEvent *ev)
if ( ev->atom == ctx->atoms.gamescopeVRREnabled )
{
bool enabled = !!get_prop( ctx, ctx->root, ctx->atoms.gamescopeVRREnabled, 0 );
- g_bAllowVRR = enabled;
+ cv_adaptive_sync = enabled;
}
if ( ev->atom == ctx->atoms.gamescopeDisplayForceInternal )
{
@@ -6886,7 +6886,7 @@ void update_vrr_atoms(xwayland_ctx_t *root_ctx, bool force, bool* needs_flush =
// Keep this as a preference, starting with off.
if ( force )
{
- bool wants_vrr = g_bAllowVRR;
+ bool wants_vrr = cv_adaptive_sync;
uint32_t enabled_value = wants_vrr ? 1 : 0;
XChangeProperty(root_ctx->dpy, root_ctx->root, root_ctx->atoms.gamescopeVRREnabled, XA_CARDINAL, 32, PropModeReplace,
(unsigned char *)&enabled_value, 1 );
--
2.45.2
From 1ebfacbb7477437ef295eb821a972ff3cfe992df Mon Sep 17 00:00:00 2001
From: Joshua Ashton <joshua@froggi.es>
Date: Thu, 18 Jul 2024 01:14:19 +0100
Subject: [PATCH 08/19] WaylandBackend: Fix picking output refresh for VRR
displays.
---
src/Backends/WaylandBackend.cpp | 69 ++++++++++++++++++++++++++++++++-
1 file changed, 68 insertions(+), 1 deletion(-)
diff --git a/src/Backends/WaylandBackend.cpp b/src/Backends/WaylandBackend.cpp
index 3603be7..b7f275e 100644
--- a/src/Backends/WaylandBackend.cpp
+++ b/src/Backends/WaylandBackend.cpp
@@ -5,6 +5,7 @@
#include "steamcompmgr.hpp"
#include "edid.h"
#include "Utils/Defer.h"
+#include "Utils/Algorithm.h"
#include "convar.h"
#include "refresh_rate.h"
#include "waitable.h"
@@ -186,8 +187,14 @@ namespace gamescope
std::optional<WaylandPlaneState> GetCurrentState() { std::unique_lock lock( m_PlaneStateLock ); return m_oCurrentPlaneState; }
+ void UpdateVRRRefreshRate();
+
private:
+ void Wayland_Surface_Enter( wl_surface *pSurface, wl_output *pOutput );
+ void Wayland_Surface_Leave( wl_surface *pSurface, wl_output *pOutput );
+ static const wl_surface_listener s_SurfaceListener;
+
void LibDecor_Frame_Configure( libdecor_frame *pFrame, libdecor_configuration *pConfiguration );
void LibDecor_Frame_Close( libdecor_frame *pFrame );
void LibDecor_Frame_Commit( libdecor_frame *pFrame );
@@ -228,12 +235,20 @@ namespace gamescope
frog_color_managed_surface *m_pFrogColorManagedSurface = nullptr;
wp_fractional_scale_v1 *m_pFractionalScale = nullptr;
libdecor_window_state m_eWindowState = LIBDECOR_WINDOW_STATE_NONE;
+ std::vector<wl_output *> m_pOutputs;
bool m_bNeedsDecorCommit = false;
uint32_t m_uFractionalScale = 120;
std::mutex m_PlaneStateLock;
std::optional<WaylandPlaneState> m_oCurrentPlaneState;
};
+ const wl_surface_listener CWaylandPlane::s_SurfaceListener =
+ {
+ .enter = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_Surface_Enter ),
+ .leave = WAYLAND_USERDATA_TO_THIS( CWaylandPlane, Wayland_Surface_Leave ),
+ .preferred_buffer_scale = WAYLAND_NULL(),
+ .preferred_buffer_transform = WAYLAND_NULL(),
+ };
// Can't be const, libdecor api bad...
libdecor_frame_interface CWaylandPlane::s_LibDecorFrameInterface =
{
@@ -540,9 +555,19 @@ namespace gamescope
bool SupportsFormat( uint32_t uDRMFormat ) const;
+ bool HostCompositorIsCurrentlyVRR() const { return m_bHostCompositorIsCurrentlyVRR; }
void SetHostCompositorIsCurrentlyVRR( bool bActive ) { m_bHostCompositorIsCurrentlyVRR = bActive; }
- bool CurrentDisplaySupportsVRR() const { return m_bHostCompositorIsCurrentlyVRR; }
+ WaylandOutputInfo *GetOutputInfo( wl_output *pOutput )
+ {
+ auto iter = m_pOutputs.find( pOutput );
+ if ( iter == m_pOutputs.end() )
+ return nullptr;
+
+ return &iter->second;
+ }
+
+ bool CurrentDisplaySupportsVRR() const { return HostCompositorIsCurrentlyVRR(); }
wl_region *GetFullRegion() const { return m_pFullRegion; }
private:
@@ -819,6 +844,7 @@ namespace gamescope
m_pParent = pParent;
m_pSurface = wl_compositor_create_surface( m_pBackend->GetCompositor() );
wl_surface_set_user_data( m_pSurface, this );
+ wl_surface_add_listener( m_pSurface, &s_SurfaceListener, this );
m_pViewport = wp_viewporter_get_viewport( m_pBackend->GetViewporter(), m_pSurface );