forked from ValveSoftware/steamos-compositor
/
main.cpp
307 lines (258 loc) · 5.91 KB
/
main.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
#include <X11/Xlib.h>
#include <thread>
#include <mutex>
#include <vector>
#include <cstring>
#include <sys/capability.h>
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>
#include <float.h>
#include "main.hpp"
#include "steamcompmgr.hpp"
#include "drm.hpp"
#include "rendervulkan.hpp"
#include "sdlwindow.hpp"
#include "wlserver.hpp"
#include "gpuvis_trace_utils.h"
#if HAVE_PIPEWIRE
#include "pipewire.hpp"
#endif
std::atomic< bool > g_bRun{true};
int g_nNestedWidth = 0;
int g_nNestedHeight = 0;
int g_nNestedRefresh = 0;
int g_nNestedUnfocusedRefresh = 0;
uint32_t g_nOutputWidth = 0;
uint32_t g_nOutputHeight = 0;
int g_nOutputRefresh = 60;
bool g_bFullscreen = false;
bool g_bIsNested = false;
bool g_bFilterGameWindow = true;
bool g_bBorderlessOutputWindow = false;
bool g_bTakeScreenshot = false;
bool g_bNiceCap = false;
int g_nOldNice = 0;
int g_nNewNice = 0;
float g_flMaxWindowScale = FLT_MAX;
bool g_bIntegerScale = false;
pthread_t g_mainThread;
int BIsNested()
{
return g_bIsNested == true;
}
static int initOutput(void);
static void steamCompMgrThreadRun(int argc, char **argv);
int main(int argc, char **argv)
{
int o;
bool bSleepAtStartup = false;
while ((o = getopt(argc, argv, GAMESCOPE_OPTIONS)) != -1)
{
switch (o) {
case 'w':
g_nNestedWidth = atoi( optarg );
break;
case 'h':
g_nNestedHeight = atoi( optarg );
break;
case 'W':
g_nOutputWidth = atoi( optarg );
break;
case 'H':
g_nOutputHeight = atoi( optarg );
break;
case 'r':
g_nNestedRefresh = atoi( optarg );
break;
case 'o':
g_nNestedUnfocusedRefresh = atoi( optarg );
break;
case 'm':
g_flMaxWindowScale = atof( optarg );
break;
case 'i':
g_bIntegerScale = true;
break;
case 's':
bSleepAtStartup = true;
break;
case 'L':
g_bUseLayers = false;
break;
case 'd':
g_bDebugLayers = true;
break;
case 'n':
g_bFilterGameWindow = false;
break;
case 'b':
g_bBorderlessOutputWindow = true;
break;
case 'f':
g_bFullscreen = true;
break;
case 'O':
g_sOutputName = optarg;
break;
default:
break;
}
}
if ( g_nOutputHeight == 0 )
{
if ( g_nOutputWidth != 0 )
{
fprintf( stderr, "Cannot specify -W without -H\n" );
return 1;
}
g_nOutputHeight = 720;
}
if ( g_nOutputWidth == 0 )
g_nOutputWidth = g_nOutputHeight * 16 / 9;
cap_t caps;
caps = cap_get_proc();
cap_flag_value_t nicecapvalue = CAP_CLEAR;
if ( caps != nullptr )
{
cap_get_flag( caps, CAP_SYS_NICE, CAP_EFFECTIVE, &nicecapvalue );
if ( nicecapvalue == CAP_SET )
{
g_bNiceCap = true;
errno = 0;
int nOldNice = nice( 0 );
if ( nOldNice != -1 && errno == 0 )
{
g_nOldNice = nOldNice;
}
errno = 0;
int nNewNice = nice( -20 );
if ( nNewNice != -1 && errno == 0 )
{
g_nNewNice = nNewNice;
}
}
}
if ( g_bNiceCap == false )
{
fprintf( stderr, "No CAP_SYS_NICE, falling back to regular-priority compute and threads.\nPerformance will be affected.\n" );
}
if ( gpuvis_trace_init() != -1 )
{
fprintf( stderr, "Tracing is enabled\n");
}
if ( bSleepAtStartup == true )
{
sleep( 2 );
}
XInitThreads();
g_mainThread = pthread_self();
if ( getenv("DISPLAY") != NULL || getenv("WAYLAND_DISPLAY") != NULL )
{
g_bIsNested = true;
}
if ( wlsession_init() != 0 )
{
fprintf( stderr, "Failed to initialize Wayland session\n" );
return 1;
}
if ( initOutput() != 0 )
{
fprintf( stderr, "Failed to initialize output\n" );
return 1;
}
if ( !vulkan_init() )
{
fprintf( stderr, "Failed to initialize Vulkan\n" );
return 1;
}
// TODO: get the DRM device from the Vulkan device
if ( g_bIsNested == false && g_vulkanHasDrmDevId )
{
struct stat drmStat = {};
if ( fstat( g_DRM.fd, &drmStat ) != 0 )
{
perror( "fstat failed on DRM FD" );
return 1;
}
if ( drmStat.st_rdev != g_vulkanDrmDevId )
{
fprintf( stderr, "Mismatch between DRM and Vulkan devices\n" );
return 1;
}
}
// Prevent our clients from connecting to the parent compositor
unsetenv("WAYLAND_DISPLAY");
// If DRM format modifiers aren't supported, prevent our clients from using
// DCC, as this can cause tiling artifacts.
if ( !g_vulkanSupportsModifiers )
{
const char *pchR600Debug = getenv( "R600_DEBUG" );
if ( pchR600Debug == nullptr )
{
setenv( "R600_DEBUG", "nodcc", 1 );
}
else if ( strstr( pchR600Debug, "nodcc" ) == nullptr )
{
std::string strPreviousR600Debug = pchR600Debug;
strPreviousR600Debug.append( ",nodcc" );
setenv( "R600_DEBUG", strPreviousR600Debug.c_str(), 1 );
}
}
if ( g_nNestedHeight == 0 )
{
if ( g_nNestedWidth != 0 )
{
fprintf( stderr, "Cannot specify -w without -h\n" );
return 1;
}
g_nNestedWidth = g_nOutputWidth;
g_nNestedHeight = g_nOutputHeight;
}
if ( g_nNestedWidth == 0 )
g_nNestedWidth = g_nNestedHeight * 16 / 9;
if ( wlserver_init(argc, argv, g_bIsNested == true ) != 0 )
{
fprintf( stderr, "Failed to initialize wlserver\n" );
return 1;
}
setenv("DISPLAY", wlserver_get_nested_display_name(), 1);
setenv("GAMESCOPE_WAYLAND_DISPLAY", wlserver_get_wl_display_name(), 1);
#if HAVE_PIPEWIRE
if ( !init_pipewire() )
{
fprintf( stderr, "Warning: failed to setup PipeWire, screen capture won't be available\n" );
}
#endif
std::thread steamCompMgrThread( steamCompMgrThreadRun, argc, argv );
steamCompMgrThread.detach();
wlserver_run();
}
static void steamCompMgrThreadRun(int argc, char **argv)
{
steamcompmgr_main( argc, argv );
pthread_kill( g_mainThread, SIGINT );
}
static int initOutput(void)
{
if ( g_bIsNested == true )
{
return sdlwindow_init() == false;
}
else
{
return init_drm( &g_DRM, nullptr );
}
}
void wayland_commit(struct wlr_surface *surf, struct wlr_buffer *buf)
{
{
std::lock_guard<std::mutex> lock( wayland_commit_lock );
ResListEntry_t newEntry = {
.surf = surf,
.buf = buf,
};
wayland_commit_queue.push_back( newEntry );
}
nudge_steamcompmgr();
}