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
|
#include "FrameSchedulingManager.hpp"
#include "../debug/Log.hpp"
#include "../Compositor.hpp"
#include "eventLoop/EventLoopManager.hpp"
static void onPresentTimer(std::shared_ptr<CEventLoopTimer> self, void* data) {
g_pFrameSchedulingManager->onVblankTimer(data);
}
static void onFenceTimer(std::shared_ptr<CEventLoopTimer> self, void* data) {
g_pFrameSchedulingManager->onFenceTimer((CMonitor*)data);
}
void CFrameSchedulingManager::onFenceTimer(CMonitor* pMonitor) {
SSchedulingData* DATA = &m_vSchedulingData.emplace_back(SSchedulingData{pMonitor});
RASSERT(DATA, "No data in fenceTimer");
#ifndef GLES2
GLint syncStatus = 0;
glGetSynciv(DATA->fenceSync, GL_SYNC_STATUS, sizeof(GLint), nullptr, &syncStatus);
bool GPUSignaled = syncStatus == GL_SIGNALED;
if (GPUSignaled)
gpuDone(pMonitor);
#endif
}
void CFrameSchedulingManager::registerMonitor(CMonitor* pMonitor) {
if (dataFor(pMonitor)) {
Debug::log(ERR, "BUG THIS: Attempted to double register to CFrameSchedulingManager");
return;
}
SSchedulingData* DATA = &m_vSchedulingData.emplace_back(SSchedulingData{pMonitor});
#ifdef GLES2
DATA->legacyScheduler = true;
#else
DATA->legacyScheduler = !wlr_backend_is_drm(pMonitor->output->backend);
#endif
DATA->fenceTimer = std::make_shared<CEventLoopTimer>(::onFenceTimer, pMonitor);
DATA->vblankTimer = std::make_shared<CEventLoopTimer>(::onPresentTimer, DATA);
g_pEventLoopManager->addTimer(DATA->fenceTimer);
g_pEventLoopManager->addTimer(DATA->vblankTimer);
}
void CFrameSchedulingManager::unregisterMonitor(CMonitor* pMonitor) {
SSchedulingData* DATA = &m_vSchedulingData.emplace_back(SSchedulingData{pMonitor});
g_pEventLoopManager->removeTimer(DATA->fenceTimer);
g_pEventLoopManager->removeTimer(DATA->vblankTimer);
std::erase_if(m_vSchedulingData, [pMonitor](const auto& d) { return d.pMonitor == pMonitor; });
}
void CFrameSchedulingManager::onFrameNeeded(CMonitor* pMonitor) {
const auto DATA = dataFor(pMonitor);
Debug::log(LOG, "onFrameNeeded");
RASSERT(DATA, "No data in onFrameNeeded");
if (pMonitor->tearingState.activelyTearing || DATA->legacyScheduler)
return;
if (DATA->activelyPushing && DATA->lastPresent.getMillis() < 100) {
DATA->forceFrames = 1;
return;
}
onPresent(pMonitor, nullptr);
}
void CFrameSchedulingManager::gpuDone(CMonitor* pMonitor) {
const auto DATA = dataFor(pMonitor);
Debug::log(LOG, "gpuDone");
RASSERT(DATA, "No data in gpuDone");
if (!DATA->delayed)
return;
Debug::log(LOG, "Missed a frame, rendering instantly");
// delayed frame, let's render immediately, our shit will be presented soon
// if we finish rendering before the next vblank somehow, kernel will be mad, but oh well
g_pHyprRenderer->renderMonitor(DATA->pMonitor);
DATA->delayedFrameSubmitted = true;
}
void CFrameSchedulingManager::registerBuffer(wlr_buffer* pBuffer, CMonitor* pMonitor) {
const auto DATA = dataFor(pMonitor);
Debug::log(LOG, "registerBuffer");
RASSERT(DATA, "No data in registerBuffer");
if (std::find(DATA->buffers.begin(), DATA->buffers.end(), pBuffer) != DATA->buffers.end())
return;
DATA->buffers.push_back(pBuffer);
}
void CFrameSchedulingManager::dropBuffer(wlr_buffer* pBuffer) {
Debug::log(LOG, "dropBuffer");
for (auto& d : m_vSchedulingData) {
std::erase(d.buffers, pBuffer);
}
}
void CFrameSchedulingManager::onFrame(CMonitor* pMonitor) {
const auto DATA = dataFor(pMonitor);
Debug::log(LOG, "onFrame");
RASSERT(DATA, "No data in onFrame");
if (!DATA->legacyScheduler)
return;
renderMonitor(DATA);
}
void CFrameSchedulingManager::onPresent(CMonitor* pMonitor, wlr_output_event_present* presentationData) {
const auto DATA = dataFor(pMonitor);
Debug::log(LOG, "onPresent");
RASSERT(DATA, "No data in onPresent");
if (pMonitor->tearingState.activelyTearing || DATA->legacyScheduler) {
DATA->activelyPushing = false;
return; // don't render
}
if (DATA->delayedFrameSubmitted) {
DATA->delayedFrameSubmitted = false;
return;
}
if (DATA->fenceSync) {
glDeleteSync(DATA->fenceSync);
DATA->fenceSync = nullptr;
}
Debug::log(LOG, "Present: del {}", DATA->delayed);
int forceFrames = DATA->forceFrames + pMonitor->forceFullFrames;
DATA->lastPresent.reset();
// reset state, request a render if necessary
DATA->delayed = false;
if (DATA->forceFrames > 0)
DATA->forceFrames--;
DATA->rendered = false;
DATA->activelyPushing = true;
// check if there is damage
bool hasDamage = pixman_region32_not_empty(&pMonitor->damage.current);
if (!hasDamage) {
for (int i = 0; i < WLR_DAMAGE_RING_PREVIOUS_LEN; ++i) {
hasDamage = hasDamage || pixman_region32_not_empty(&pMonitor->damage.previous[i]);
}
}
if (!hasDamage && forceFrames <= 0) {
DATA->activelyPushing = false;
return;
}
Debug::log(LOG, "render");
uint64_t µsUntilVblank = 0;
if (presentationData) {
timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
const std::chrono::system_clock::duration SINCELASTVBLANK{
std::chrono::duration_cast<std::chrono::system_clock::duration>(std::chrono::seconds{now.tv_sec} + std::chrono::nanoseconds{now.tv_nsec}) -
std::chrono::duration_cast<std::chrono::system_clock::duration>(std::chrono::seconds{presentationData->when->tv_sec} +
std::chrono::nanoseconds{presentationData->when->tv_nsec})};
µsUntilVblank = (presentationData->refresh ? presentationData->refresh / 1000.0 : pMonitor->refreshRate / 1000000.0) -
std::chrono::duration_cast<std::chrono::microseconds>(SINCELASTVBLANK).count();
DATA->nextVblank = std::chrono::system_clock::now() + std::chrono::microseconds{µsUntilVblank};
} else
µsUntilVblank = std::chrono::duration_cast<std::chrono::microseconds>(DATA->nextVblank - std::chrono::system_clock::now()).count();
if (µsUntilVblank > 500)
DATA->vblankTimer->updateTimeout(std::chrono::microseconds(µsUntilVblank - 500));
Debug::log(LOG, "until vblank {}µs", µsUntilVblank);
renderMonitor(DATA);
}
CFrameSchedulingManager::SSchedulingData* CFrameSchedulingManager::dataFor(CMonitor* pMonitor) {
for (auto& d : m_vSchedulingData) {
if (d.pMonitor == pMonitor)
return &d;
}
return nullptr;
}
CFrameSchedulingManager::SSchedulingData* CFrameSchedulingManager::dataFor(wlr_buffer* pBuffer) {
for (auto& d : m_vSchedulingData) {
if (std::find(d.buffers.begin(), d.buffers.end(), pBuffer) != d.buffers.end())
return &d;
}
return nullptr;
}
void CFrameSchedulingManager::renderMonitor(SSchedulingData* data) {
CMonitor* pMonitor = data->pMonitor;
if ((g_pCompositor->m_sWLRSession && !g_pCompositor->m_sWLRSession->active) || !g_pCompositor->m_bSessionActive || g_pCompositor->m_bUnsafeState) {
Debug::log(WARN, "Attempted to render frame on inactive session!");
if (g_pCompositor->m_bUnsafeState && std::ranges::any_of(g_pCompositor->m_vMonitors.begin(), g_pCompositor->m_vMonitors.end(), [&](auto& m) {
return m->output != g_pCompositor->m_pUnsafeOutput->output;
})) {
// restore from unsafe state
g_pCompositor->leaveUnsafeState();
}
return; // cannot draw on session inactive (different tty)
}
if (!pMonitor->m_bEnabled)
return;
g_pHyprRenderer->recheckSolitaryForMonitor(pMonitor);
pMonitor->tearingState.busy = false;
if (pMonitor->tearingState.activelyTearing && pMonitor->solitaryClient /* can be invalidated by a recheck */) {
if (!pMonitor->tearingState.frameScheduledWhileBusy)
return; // we did not schedule a frame yet to be displayed, but we are tearing. Why render?
pMonitor->tearingState.nextRenderTorn = true;
pMonitor->tearingState.frameScheduledWhileBusy = false;
}
data->fenceSync = g_pHyprRenderer->renderMonitor(pMonitor, !data->legacyScheduler);
data->rendered = true;
}
void CFrameSchedulingManager::onVblankTimer(void* data) {
auto DATA = (SSchedulingData*)data;
if (!DATA->rendered) {
// what the fuck?
Debug::log(ERR, "Vblank timer fired without a frame????");
return;
}
#ifndef GLES2
g_pHyprRenderer->makeEGLCurrent();
GLint syncStatus = 0;
glGetSynciv(DATA->fenceSync, GL_SYNC_STATUS, sizeof(GLint), nullptr, &syncStatus);
bool GPUSignaled = syncStatus == GL_SIGNALED;
Debug::log(LOG, "vblank: signaled {}", GPUSignaled);
if (GPUSignaled) {
Debug::log(LOG, "timer nothing");
// cool, we don't need to do anything. Wait for present.
return;
} else {
Debug::log(LOG, "timer delay");
// we missed a vblank :(
DATA->delayed = true;
// start the fence timer
DATA->fenceTimer->updateTimeout(std::chrono::microseconds(850));
return;
}
#endif
}
bool CFrameSchedulingManager::isMonitorUsingLegacyScheduler(CMonitor* pMonitor) {
const auto DATA = dataFor(pMonitor);
if (!DATA)
return true;
return DATA->legacyScheduler;
}
|