GCC Code Coverage Report


Directory: ./
File: framebuffer/KMSFrameBuffer.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 589 855 68.9%
Functions: 30 39 76.9%
Branches: 460 1070 43.0%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12 #include "fpp-pch.h"
13
14 #include "KMSFrameBuffer.h"
15
16 #ifdef HAS_KMS_FB
17 #include <libdrm/drm.h>
18 #include <libdrm/drm_fourcc.h>
19 #include <sys/ioctl.h>
20 #include <sys/mman.h>
21 #include <cmath>
22 #include <cstring>
23 #include <cerrno>
24 #include <fcntl.h>
25 #include <linux/netlink.h>
26 #include <poll.h>
27 #include <sys/eventfd.h>
28 #include <sys/socket.h>
29 #include <unistd.h>
30 #include <algorithm>
31 #include <thread>
32
33 #include "../common.h"
34 #include "../common_mini.h"
35 #include "../Warnings.h"
36 #include "../log.h"
37 #include "../mediaoutput/DrmCards.h"
38 #include "../mediaoutput/mediaoutput.h"
39
40 std::atomic_int KMSFrameBuffer::FRAMEBUFFER_COUNT(0);
41 std::vector<CardInfo*> KMSFrameBuffer::CARDS;
42 std::map<uint32_t, KMSFrameBuffer*> KMSFrameBuffer::FLIP_TARGETS;
43 uint32_t KMSFrameBuffer::NEXT_FLIP_TOKEN = 0;
44
45 41 std::string KMSFrameBuffer::ConnectorFullName(int fd, drmModeConnectorPtr conn) {
46 41 const char* typeName = drmModeGetConnectorTypeName(conn->connector_type);
47
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
66 std::string name = typeName ? typeName : "Unknown";
48
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 name += "-" + std::to_string(conn->connector_type_id);
49 41 return name;
50 ✗ }
51
52 // A plugged-in display: HDMI, DVI, DisplayPort and VGA can be unplugged, and a
53 // sink on them answers with an EDID. Panels (DPI, DSI, ...) are there for good.
54 76 static bool IsExternalConnector(uint32_t type) {
55
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 48 times.
76 switch (type) {
56 28 case DRM_MODE_CONNECTOR_HDMIA:
57 case DRM_MODE_CONNECTOR_HDMIB:
58 case DRM_MODE_CONNECTOR_DVII:
59 case DRM_MODE_CONNECTOR_DVID:
60 case DRM_MODE_CONNECTOR_DVIA:
61 case DRM_MODE_CONNECTOR_DisplayPort:
62 case DRM_MODE_CONNECTOR_VGA:
63 28 return true;
64 48 default:
65 48 return false;
66 }
67 }
68
69 // Whether a display is actually on the connector. For an external connector
70 // that takes an EDID: vc4.force_hotplug (the ForceHDMI setting) pins it to
71 // "connected" and the kernel then makes up a few modes (up to 1024x768) even
72 // with nothing plugged in.
73 38 bool KMSFrameBuffer::SinkPresent(int fd, drmModeConnectorPtr conn) {
74
3/6
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 38 times.
38 if (!conn || conn->connection != DRM_MODE_CONNECTED || conn->count_modes == 0) {
75 ✗ return false;
76 }
77
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 14 times.
38 if (!IsExternalConnector(conn->connector_type)) {
78 24 return true;
79 }
80 14 bool edid = false;
81
4/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 3 times.
28 for (int i = 0; i < conn->count_props && !edid; i++) {
82 14 drmModePropertyPtr prop = drmModeGetProperty(fd, conn->props[i]);
83
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (prop) {
84
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 edid = !strcmp(prop->name, "EDID") && conn->prop_values[i] != 0;
85 14 drmModeFreeProperty(prop);
86 }
87 }
88 14 return edid;
89 }
90
91 // The mode forced with video=<connector>:<mode> on the kernel command line,
92 // which is what the Force HDMI Resolution settings write. The kernel flags
93 // the matching probed mode USERDEF, or adds one of its own if none matches.
94 38 static bool CommandLineMode(drmModeConnectorPtr conn, drmModeModeInfo& mode) {
95
3/4
✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 199 times.
✓ Branch 3 taken 38 times.
237 for (int mi = 0; conn && mi < conn->count_modes; mi++) {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199 times.
199 if (conn->modes[mi].type & DRM_MODE_TYPE_USERDEF) {
97 ✗ mode = conn->modes[mi];
98 ✗ return true;
99 }
100 }
101 38 return false;
102 }
103
104 // The mode forced on the kernel command line, else the display's preferred
105 // mode, else its first. Not the preferred mode first: with two displays that
106 // prefer different modes, that ran each in its own one whatever was forced.
107 38 bool KMSFrameBuffer::PickMode(drmModeConnectorPtr conn, drmModeModeInfo& mode) {
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (CommandLineMode(conn, mode)) {
109 ✗ return true;
110 }
111
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 for (int mi = 0; mi < conn->count_modes; mi++) {
112
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (conn->modes[mi].type & DRM_MODE_TYPE_PREFERRED) {
113 38 mode = conn->modes[mi];
114 38 return true;
115 }
116 }
117 ✗ if (conn->count_modes > 0) {
118 ✗ mode = conn->modes[0];
119 ✗ return true;
120 }
121 ✗ return false;
122 }
123
124 // What an external connector runs until a display shows up: CEA-861 1080p60,
125 // which any HDMI sink takes.
126 ✗ static drmModeModeInfo FallbackMode() {
127 ✗ drmModeModeInfo m = {};
128 ✗ m.clock = 148500;
129 ✗ m.hdisplay = 1920;
130 ✗ m.hsync_start = 2008;
131 ✗ m.hsync_end = 2052;
132 ✗ m.htotal = 2200;
133 ✗ m.vdisplay = 1080;
134 ✗ m.vsync_start = 1084;
135 ✗ m.vsync_end = 1089;
136 ✗ m.vtotal = 1125;
137 ✗ m.vrefresh = 60;
138 ✗ m.flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC;
139 ✗ m.type = DRM_MODE_TYPE_DRIVER;
140 ✗ snprintf(m.name, sizeof(m.name), "1920x1080");
141 ✗ return m;
142 }
143
144 // "1920x1080@60 148500kHz h 1920/2008/2052/2200 v 1080/1084/1089/1125 flags 0x5"
145 ✗ static std::string ModeLine(const drmModeModeInfo& m) {
146 char buf[160];
147 ✗ snprintf(buf, sizeof(buf), "%dx%d@%d %ukHz h %d/%d/%d/%d v %d/%d/%d/%d flags 0x%x",
148 ✗ m.hdisplay, m.vdisplay, m.vrefresh, m.clock, m.hdisplay, m.hsync_start, m.hsync_end, m.htotal,
149 ✗ m.vdisplay, m.vsync_start, m.vsync_end, m.vtotal, m.flags);
150 ✗ return buf;
151 }
152
153 ✗ static bool SameTiming(const drmModeModeInfo& a, const drmModeModeInfo& b) {
154 ✗ return a.clock == b.clock && a.hdisplay == b.hdisplay && a.hsync_start == b.hsync_start &&
155 ✗ a.hsync_end == b.hsync_end && a.htotal == b.htotal && a.vdisplay == b.vdisplay &&
156 ✗ a.vsync_start == b.vsync_start && a.vsync_end == b.vsync_end && a.vtotal == b.vtotal &&
157 ✗ a.flags == b.flags;
158 }
159
160 // Up while an output is running with no display on its connector, which on a
161 // show night usually means a projector that is off or lost its signal. Comes
162 // down when the display appears and when the output is torn down; built by one
163 // function so the add and the remove always match.
164 static constexpr int WARNING_ID_NO_DISPLAY = 71;
165 ✗ static std::string NoDisplayWarning(const std::string& connector) {
166 ✗ return "Video output " + connector + ": no display detected - check that the projector or monitor is on and connected";
167 }
168
169 // The flag and the banner move together; caller holds drmMasterLock.
170 38 void KMSFrameBuffer::SetSinkPresentLocked(bool present) {
171 38 m_sinkPresent = present;
172
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
38 if (!present && !m_noDisplayWarned) {
173 ✗ WarningHolder::AddWarning(WARNING_ID_NO_DISPLAY, NoDisplayWarning(m_connectorName));
174 ✗ m_noDisplayWarned = true;
175
2/4
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
38 } else if (present && m_noDisplayWarned) {
176 ✗ WarningHolder::RemoveWarning(WARNING_ID_NO_DISPLAY, NoDisplayWarning(m_connectorName));
177 ✗ m_noDisplayWarned = false;
178 }
179 38 }
180
181 // Only the DRM master gets a real probe; for anyone else the kernel reads back
182 // its cached state, which after a hotplug can be "connected" with no modes -
183 // the HPD interrupt updates the status but not the mode list. The real probe
184 // reads the EDID over DDC and also refreshes the sysfs mode list. The card's
185 // shared fd is master (see DrmCards.h). Caller holds drmMasterLock.
186 38 drmModeConnectorPtr KMSFrameBuffer::ProbeConnectorLocked(int fd, uint32_t connectorId) {
187 38 DrmCards::EnsureMaster(fd);
188 38 return drmModeGetConnector(fd, connectorId);
189 }
190
191 93 bool KMSFrameBuffer::CreateDumbBuffer(int fd, uint32_t width, uint32_t height, uint32_t format, DumbBuffer& buf) {
192 uint32_t bpp;
193
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 17 times.
93 if (format == DRM_FORMAT_RGB888) {
194 76 bpp = 24;
195
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 } else if (format == DRM_FORMAT_RGB565) {
196 17 bpp = 16;
197 } else {
198 ✗ bpp = 32;
199 }
200
201 93 struct drm_mode_create_dumb creq = {};
202 93 creq.width = width;
203 93 creq.height = height;
204 93 creq.bpp = bpp;
205
206
2/4
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
93 if (drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &creq) < 0) {
207 ✗ return false;
208 }
209
210 93 uint32_t handles[4] = { creq.handle, 0, 0, 0 };
211 93 uint32_t pitches[4] = { creq.pitch, 0, 0, 0 };
212 93 uint32_t offsets[4] = { 0, 0, 0, 0 };
213 93 uint32_t fb_id = 0;
214
215
2/4
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
93 if (drmModeAddFB2(fd, width, height, format, handles, pitches, offsets, &fb_id, 0) < 0) {
216 ✗ struct drm_mode_destroy_dumb dreq = {};
217 ✗ dreq.handle = creq.handle;
218 ✗ drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
219 ✗ return false;
220 }
221
222 93 struct drm_mode_map_dumb mreq = {};
223 93 mreq.handle = creq.handle;
224
2/4
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
93 if (drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &mreq) < 0) {
225 ✗ drmModeRmFB(fd, fb_id);
226 ✗ struct drm_mode_destroy_dumb dreq = {};
227 ✗ dreq.handle = creq.handle;
228 ✗ drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
229 ✗ return false;
230 }
231
232 93 void* mapped = mmap(nullptr, creq.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, mreq.offset);
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (mapped == MAP_FAILED) {
234 ✗ drmModeRmFB(fd, fb_id);
235 ✗ struct drm_mode_destroy_dumb dreq = {};
236 ✗ dreq.handle = creq.handle;
237 ✗ drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
238 ✗ return false;
239 }
240
241 93 buf.handle = creq.handle;
242 93 buf.fb_id = fb_id;
243 93 buf.width = width;
244 93 buf.height = height;
245 93 buf.stride = creq.pitch;
246 93 buf.format = format;
247 93 buf.size = creq.size;
248 93 buf.mapped = mapped;
249 93 return true;
250 }
251
252 83 void KMSFrameBuffer::DestroyDumbBuffer(int fd, DumbBuffer& buf) {
253
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 if (buf.mapped) {
254 83 munmap(buf.mapped, buf.size);
255 83 buf.mapped = nullptr;
256 }
257
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 if (buf.fb_id) {
258 83 drmModeRmFB(fd, buf.fb_id);
259 83 buf.fb_id = 0;
260 }
261
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 if (buf.handle) {
262 83 struct drm_mode_destroy_dumb dreq = {};
263 83 dreq.handle = buf.handle;
264
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
265 83 buf.handle = 0;
266 }
267 83 buf = {};
268 83 }
269
270 38 uint32_t KMSFrameBuffer::FindPlaneForCrtc(int fd, uint32_t crtcId, uint32_t format) {
271 38 drmModeResPtr res = drmModeGetResources(fd);
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (!res) {
273 ✗ return 0;
274 }
275
276 38 int crtcIndex = -1;
277
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 for (int i = 0; i < res->count_crtcs; i++) {
278
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 9 times.
47 if (res->crtcs[i] == crtcId) {
279 38 crtcIndex = i;
280 38 break;
281 }
282 }
283 38 drmModeFreeResources(res);
284
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (crtcIndex < 0) {
286 ✗ return 0;
287 }
288
289 38 drmModePlaneResPtr planes = drmModeGetPlaneResources(fd);
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (!planes) {
291 ✗ return 0;
292 }
293
294 38 uint32_t result = 0;
295
3/4
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 38 times.
87 for (uint32_t i = 0; i < planes->count_planes && result == 0; i++) {
296 49 drmModePlanePtr plane = drmModeGetPlane(fd, planes->planes[i]);
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (!plane) {
298 ✗ continue;
299 }
300
301
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 40 times.
49 if (!(plane->possible_crtcs & (1u << crtcIndex))) {
302 9 drmModeFreePlane(plane);
303 9 continue;
304 }
305
306 // Check if already reserved
307 40 bool reserved = false;
308
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 40 times.
83 for (auto* card : CARDS) {
309
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 43 times.
45 for (auto pid : card->reservedPlanes) {
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (pid == plane->plane_id) {
311 ✗ reserved = true;
312 ✗ break;
313 }
314 }
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (reserved) break;
316 }
317 // Or that a media pipeline's lease holds.
318
5/6
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 38 times.
40 if (reserved || DrmCards::InUse(fd, plane->plane_id)) {
319 2 drmModeFreePlane(plane);
320 2 continue;
321 }
322
323
1/2
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
144 for (uint32_t j = 0; j < plane->count_formats; j++) {
324
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 106 times.
144 if (plane->formats[j] == format) {
325 38 result = plane->plane_id;
326 38 break;
327 }
328 }
329 38 drmModeFreePlane(plane);
330 }
331 38 drmModeFreePlaneResources(planes);
332 38 return result;
333 }
334
335 39 KMSFrameBuffer::KMSFrameBuffer() {
336
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 LogDebug(VB_CHANNELOUT, "KMSFrameBuffer::KMSFrameBuffer()\n");
337
338
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 2 times.
39 if (FRAMEBUFFER_COUNT == 0) {
339
2/2
✓ Branch 0 taken 370 times.
✓ Branch 1 taken 37 times.
407 for (int cn = 0; cn < 10; cn++) {
340
1/2
✓ Branch 0 taken 370 times.
✗ Branch 1 not taken.
370 std::string path = "/dev/dri/card" + std::to_string(cn);
341
3/4
✓ Branch 0 taken 370 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 331 times.
370 if (FileExists(path)) {
342 // The card's one fd for all of fppd, which stays DRM master:
343 // media pipelines get leases from it rather than a master of
344 // their own, so nothing has to take turns (DrmCards.h).
345
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 int fd = DrmCards::Acquire(path);
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (fd < 0) {
347 ✗ continue;
348 }
349
2/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
39 CardInfo* info = new CardInfo();
350 39 info->fd = fd;
351
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 info->path = path;
352
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 CARDS.push_back(info);
353 }
354
1/2
✓ Branch 0 taken 370 times.
✗ Branch 1 not taken.
370 }
355
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 StartHotplugMonitor();
356
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 DrmCards::SetMediaHoldListener(&KMSFrameBuffer::MediaHoldChanged);
357 }
358 39 ++FRAMEBUFFER_COUNT;
359
360
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 std::unique_lock<std::mutex> lock(drmMasterLock);
361 39 m_flipToken = ++NEXT_FLIP_TOKEN;
362
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 FLIP_TARGETS[m_flipToken] = this;
363 39 }
364
365 136 KMSFrameBuffer::~KMSFrameBuffer() {
366 // The draw loop writes into the mapped dumb buffers and calls the virtual
367 // SyncDisplay(); it has to be dead before this destructor releases any of
368 // that. ~FrameBuffer() joins far too late - by then the vtable is already
369 // demoted and the card fds below may be closed.
370 68 StopDrawLoop();
371
372 {
373 68 std::unique_lock<std::mutex> lock(drmMasterLock);
374 // Retire our last flip while this object can still take its event.
375 // Past this point one that is still queued is dropped by token.
376
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5 times.
68 if (m_flipPending) {
377
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
58 int vr = m_mode.vrefresh > 0 ? m_mode.vrefresh : 60;
378 58 WaitForPendingFlip((1000 / vr) + 20);
379 }
380 68 FLIP_TARGETS.erase(m_flipToken);
381
382
4/6
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
68 if (m_displayEnabled && m_crtcId && m_planeId) {
383 66 LogInfo(VB_CHANNELOUT, "KMSFrameBuffer::~KMSFrameBuffer() disabling display before destruction\n");
384
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
66 if (DrmCards::EnsureMaster(m_cardFd)) {
385 66 drmModeSetPlane(m_cardFd, m_planeId, m_crtcId, m_fb[m_cPage].fb_id, 0,
386 0, 0, 0, 0, 0, 0, 0, 0);
387 66 m_displayEnabled = false;
388 }
389 }
390 68 }
391
392 // Must run here, not from ~FrameBuffer(): the base destructor can only reach
393 // FrameBuffer::DestroyFrameBuffer(), so the dumb buffers and the connector /
394 // CRTC / plane reservations were never released on delete. It also has to
395 // happen before the card fds are closed below.
396 68 KMSFrameBuffer::DestroyFrameBuffer();
397
398 68 --FRAMEBUFFER_COUNT;
399
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2 times.
68 if (FRAMEBUFFER_COUNT == 0) {
400 64 StopHotplugMonitor();
401
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 32 times.
126 for (auto* card : CARDS) {
402
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
62 if (card->fd >= 0) {
403 62 DrmCards::Release(card->path);
404 }
405
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
62 delete card;
406 }
407 64 CARDS.clear();
408 }
409 136 }
410
411 39 int KMSFrameBuffer::InitializeFrameBuffer(void) {
412 39 LogDebug(VB_CHANNELOUT, "KMSFrameBuffer::InitializeFrameBuffer()\n");
413
414
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 1 times.
42 for (auto* card : CARDS) {
415
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 drmModeResPtr res = drmModeGetResources(card->fd);
416
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 38 times.
41 if (!res) {
417 3 continue;
418 }
419
420
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 for (int ci = 0; ci < res->count_connectors; ci++) {
421
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 drmModeConnectorPtr conn = drmModeGetConnectorCurrent(card->fd, res->connectors[ci]);
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (!conn) {
423 3 continue;
424 }
425
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 std::string cname = ConnectorFullName(card->fd, conn);
426
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 38 times.
41 if (m_device != cname) {
427
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 drmModeFreeConnector(conn);
428 3 continue;
429 }
430
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 drmModeFreeConnector(conn);
431 {
432
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 std::unique_lock<std::mutex> lock(drmMasterLock);
433
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 conn = ProbeConnectorLocked(card->fd, res->connectors[ci]);
434 38 }
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (!conn) {
436 ✗ continue;
437 }
438
439 // An external display that is off or unplugged right now is not a
440 // reason to have no output: run a standard mode until it shows up
441 // and the hotplug handler adopts its own. Before, the output was
442 // simply never created, and only a restart brought it back.
443
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 bool present = SinkPresent(card->fd, conn);
444 38 bool external = IsExternalConnector(conn->connector_type);
445
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
38 if (!present && !external && conn->connection != DRM_MODE_CONNECTED) {
446 ✗ drmModeFreeConnector(conn);
447 ✗ continue;
448 }
449
450 // Check if already reserved
451 38 bool alreadyReserved = false;
452
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 38 times.
40 for (auto cid : card->reservedConnectors) {
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (cid == conn->connector_id) {
454 ✗ alreadyReserved = true;
455 ✗ break;
456 }
457 }
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (alreadyReserved) {
459 ✗ drmModeFreeConnector(conn);
460 ✗ continue;
461 }
462
463 // Find a CRTC for this connector
464 38 uint32_t crtcId = 0;
465
466 // Try the encoder's current CRTC first
467
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (conn->encoder_id) {
468
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 drmModeEncoderPtr enc = drmModeGetEncoder(card->fd, conn->encoder_id);
469
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (enc) {
470 // Check if this CRTC is already reserved
471 38 bool crtcReserved = false;
472
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 38 times.
40 for (auto rid : card->reservedCrtcs) {
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (rid == enc->crtc_id) {
474 ✗ crtcReserved = true;
475 ✗ break;
476 }
477 }
478
3/4
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 1 times.
38 if (!crtcReserved && enc->crtc_id) {
479 37 crtcId = enc->crtc_id;
480 }
481
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 drmModeFreeEncoder(enc);
482 }
483 }
484
485 // If no CRTC yet, try all encoders for this connector
486
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37 times.
38 if (crtcId == 0) {
487
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 for (int ei = 0; ei < conn->count_encoders && crtcId == 0; ei++) {
488
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 drmModeEncoderPtr enc = drmModeGetEncoder(card->fd, conn->encoders[ei]);
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!enc) continue;
490
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 for (int ci2 = 0; ci2 < res->count_crtcs && crtcId == 0; ci2++) {
491
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (enc->possible_crtcs & (1u << ci2)) {
492 1 bool crtcReserved = false;
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 for (auto rid : card->reservedCrtcs) {
494 ✗ if (rid == res->crtcs[ci2]) {
495 ✗ crtcReserved = true;
496 ✗ break;
497 }
498 }
499
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 if (!crtcReserved && !DrmCards::InUse(card->fd, res->crtcs[ci2])) {
500 1 crtcId = res->crtcs[ci2];
501 }
502 }
503 }
504
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 drmModeFreeEncoder(enc);
505 }
506 }
507
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (crtcId == 0) {
509 ✗ drmModeFreeConnector(conn);
510 ✗ drmModeFreeResources(res);
511 ✗ return 0;
512 }
513
514 38 drmModeModeInfo mode = {};
515 38 bool foundMode = false;
516
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
38 if (present || !external) {
517
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 foundMode = PickMode(conn, mode);
518 } else {
519 // A forced connector (video=...e, or ForceHDMI) with no EDID
520 // still carries the command line's mode: run that.
521 ✗ if (!CommandLineMode(conn, mode)) {
522 ✗ mode = FallbackMode();
523 }
524 ✗ foundMode = true;
525 ✗ LogInfo(VB_CHANNELOUT, "KMSFrameBuffer: no display on %s yet, running %dx%d @ %dHz until one appears\n",
526 cname.c_str(), mode.hdisplay, mode.vdisplay, mode.vrefresh);
527 }
528
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (!foundMode) {
530 ✗ drmModeFreeConnector(conn);
531 ✗ drmModeFreeResources(res);
532 ✗ return 0;
533 }
534
535 38 m_mode = mode;
536 38 m_connectorId = conn->connector_id;
537 38 m_crtcId = crtcId;
538 38 m_crtcPipe = 0;
539
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 38 times.
89 for (int ci = 0; ci < res->count_crtcs; ci++) {
540
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 13 times.
51 if (res->crtcs[ci] == crtcId) {
541 38 m_crtcPipe = ci;
542 }
543 }
544
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 m_connectorName = cname;
545 38 m_cardFd = card->fd;
546 38 m_cardInfo = card;
547
548 // The panel refresh caps the sustainable output frame rate: page
549 // flips are vblank-paced, so a sequence faster than this will be
550 // limited to the refresh rate.
551
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 LogInfo(VB_CHANNELOUT, "KMSFrameBuffer: %s mode %dx%d @ %dHz\n",
552 cname.c_str(), mode.hdisplay, mode.vdisplay, mode.vrefresh);
553
554
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 card->reservedConnectors.push_back(m_connectorId);
555
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 card->reservedCrtcs.push_back(m_crtcId);
556
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 DrmCards::Claim(card->fd, m_connectorId);
557
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 DrmCards::Claim(card->fd, m_crtcId);
558
559
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 17 times.
38 if (m_variableRefresh) {
560 // DPI Pixels: drive our own vertical timing. Keep the
561 // connector's horizontal timing and pixel clock (they define the
562 // WS281x bit timing) but set vactive to the caller's requested
563 // scanline count (sized to the longest string) and start at the
564 // maximum refresh rate. DPIPixelsOutput lowers it per-sequence
565 // via SetRefreshRate() by only varying the vertical blanking, so
566 // the framebuffer and its contents never have to be rebuilt.
567
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (m_width == 0) {
568 21 m_width = m_mode.hdisplay;
569 }
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (m_height == 0) {
571 ✗ m_height = m_mode.vdisplay;
572 }
573 21 m_pixelSize = 1;
574
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 ApplyVerticalTiming(m_mode, m_height, 1000000); // clamped to max fps
575 } else {
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (m_width == 0) {
577 ✗ m_width = m_mode.hdisplay;
578 }
579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (m_height == 0) {
580 ✗ m_height = m_mode.vdisplay;
581 }
582
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (m_pixelSize == 0) {
583 17 int mw = m_mode.hdisplay / m_width;
584 17 int mh = m_mode.vdisplay / m_height;
585 17 m_pixelSize = std::min(mw, mh);
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (m_pixelSize < 1) {
587 ✗ m_pixelSize = 1;
588 }
589 17 m_width *= m_pixelSize;
590 17 m_height *= m_pixelSize;
591 }
592 }
593
594 38 m_bpp = 24;
595 38 uint32_t format = DRM_FORMAT_RGB888;
596
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 38 times.
114 for (int x = 0; x < 2; x++) {
597
2/4
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 76 times.
76 if (!CreateDumbBuffer(card->fd, m_width, m_height, format, m_fb[x])) {
598 ✗ format = DRM_FORMAT_XRGB8888;
599 ✗ m_bpp = 32;
600 ✗ if (!CreateDumbBuffer(card->fd, m_width, m_height, format, m_fb[x])) {
601 ✗ LogErr(VB_CHANNELOUT, "Failed to create dumb buffer\n");
602 // Free any buffer(s) already created and undo the
603 // connector/CRTC reservations pushed above so we don't
604 // leak CMA or permanently reserve this output.
605 ✗ for (int y = 0; y < x; y++) {
606 ✗ DestroyDumbBuffer(card->fd, m_fb[y]);
607 ✗ m_pageBuffers[y] = nullptr;
608 }
609 ✗ auto& rc = card->reservedConnectors;
610 ✗ rc.erase(std::remove(rc.begin(), rc.end(), m_connectorId), rc.end());
611 ✗ auto& rcr = card->reservedCrtcs;
612 ✗ rcr.erase(std::remove(rcr.begin(), rcr.end(), m_crtcId), rcr.end());
613 ✗ DrmCards::Unclaim(card->fd, m_connectorId);
614 ✗ DrmCards::Unclaim(card->fd, m_crtcId);
615 ✗ m_connectorId = 0;
616 ✗ m_crtcId = 0;
617 ✗ drmModeFreeConnector(conn);
618 ✗ drmModeFreeResources(res);
619 ✗ return 0;
620 }
621 }
622 76 m_pageBuffers[x] = (uint8_t*)m_fb[x].mapped;
623 }
624
625
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 m_planeId = FindPlaneForCrtc(card->fd, m_crtcId, format);
626
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (m_planeId) {
627
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 card->reservedPlanes.push_back(m_planeId);
628
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 DrmCards::Claim(card->fd, m_planeId);
629 }
630 38 m_pages = 2;
631
632 38 m_cPage = 0;
633 38 m_pPage = 0;
634 38 m_rowStride = m_fb[0].stride;
635 38 m_rowPadding = m_rowStride - (m_width * m_bpp / 8);
636 38 m_pageSize = m_fb[0].size;
637 38 m_bufferSize = m_pageSize;
638
639
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (m_planeId) {
640 38 drmModeSetPlane(m_cardFd, m_planeId, m_crtcId, m_fb[0].fb_id, 0,
641 38 0, 0, m_mode.hdisplay, m_mode.vdisplay,
642
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 0, 0, (uint32_t)m_width << 16, (uint32_t)m_height << 16);
643 }
644
645
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 drmModeFreeConnector(conn);
646
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 drmModeFreeResources(res);
647
648 {
649
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 std::unique_lock<std::mutex> lock(drmMasterLock);
650
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 SetSinkPresentLocked(present);
651 38 m_hotplugReady = true;
652 38 }
653 38 return 1;
654
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 38 times.
41 }
655 ✗ drmModeFreeResources(res);
656 }
657 1 return 0;
658 }
659
660 34 void KMSFrameBuffer::DestroyFrameBuffer(void) {
661 {
662
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 std::unique_lock<std::mutex> lock(drmMasterLock);
663 34 m_hotplugReady = false;
664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (m_noDisplayWarned) {
665 ✗ WarningHolder::RemoveWarning(WARNING_ID_NO_DISPLAY, NoDisplayWarning(m_connectorName));
666 ✗ m_noDisplayWarned = false;
667 }
668 34 }
669
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 34 times.
136 for (int x = 0; x < 3; x++) {
670
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 36 times.
102 if (m_fb[x].handle) {
671 66 DestroyDumbBuffer(m_cardFd, m_fb[x]);
672 66 m_pageBuffers[x] = nullptr;
673 }
674 }
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (m_blankFb.handle) {
676 ✗ DestroyDumbBuffer(m_cardFd, m_blankFb);
677 }
678 34 m_mediaBlanked = false;
679
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 1 times.
34 if (m_cardInfo) {
680
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (m_planeId) {
681 33 auto& planes = m_cardInfo->reservedPlanes;
682
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
33 planes.erase(std::remove(planes.begin(), planes.end(), m_planeId), planes.end());
683 33 DrmCards::Unclaim(m_cardInfo->fd, m_planeId);
684 }
685
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (m_crtcId) {
686 33 auto& crtcs = m_cardInfo->reservedCrtcs;
687
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
33 crtcs.erase(std::remove(crtcs.begin(), crtcs.end(), m_crtcId), crtcs.end());
688 33 DrmCards::Unclaim(m_cardInfo->fd, m_crtcId);
689 }
690
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (m_connectorId) {
691 33 auto& conns = m_cardInfo->reservedConnectors;
692
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
33 conns.erase(std::remove(conns.begin(), conns.end(), m_connectorId), conns.end());
693 33 DrmCards::Unclaim(m_cardInfo->fd, m_connectorId);
694 }
695 }
696
697 34 m_connectorId = 0;
698 34 m_crtcId = 0;
699 34 m_planeId = 0;
700 34 m_cardInfo = nullptr;
701 34 FrameBuffer::DestroyFrameBuffer();
702 34 }
703
704 ✗ void KMSFrameBuffer::SyncLoop() {
705 ✗ if (m_pages == 1) {
706 ✗ return;
707 }
708
709 ✗ while (m_runLoop) {
710 // Allow the producer to catch up if needed
711 ✗ if (!m_dirtyPages[m_cPage] && m_dirtyPages[(m_cPage + 1) % m_pages])
712 ✗ NextPage();
713
714 ✗ if (m_dirtyPages[m_cPage]) {
715 // SyncDisplay() blocks on the vblank completion event, so it paces
716 // this loop to the panel refresh without spinning.
717 ✗ SyncDisplay(true);
718
719 ✗ m_dirtyPages[m_cPage] = 0;
720 ✗ NextPage();
721 } else {
722 // Nothing to flip yet. Sleep briefly instead of busy-spinning so an
723 // idle output doesn't peg a CPU core (the original reason AUTO_SYNC
724 // was abandoned).
725 ✗ std::this_thread::sleep_for(std::chrono::microseconds(250));
726 }
727 }
728 }
729
730 // Whether a media pipeline is showing on this connector, so this output must
731 // leave it alone. Its kmssink holds the connector through a DRM lease from
732 // before the pipeline starts until it is gone, whichever slot it plays on and
733 // whatever the output is called -- the old test compared slot 1's status to
734 // the connector name, which Play Media and "--HDMI--" never matched.
735 5881 bool KMSFrameBuffer::MediaHoldsConnector() const {
736
3/4
✓ Branch 0 taken 5881 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 180 times.
✓ Branch 3 taken 5701 times.
5881 return m_connectorId && DrmCards::HeldByMedia(m_cardFd, m_connectorId);
737 }
738
739 // Take our plane off the CRTC, which stays lit for the video on top of it.
740 // Black would hide the last frame as well, but a plane showing black still
741 // counts against the HVS load: on a Pi 0-3 a framebuffer scaled to the mode
742 // under a full-screen 1080p video is over the kernel's limit, and the video's
743 // first frame fails with ENOSPC. Caller holds drmMasterLock.
744 92 void KMSFrameBuffer::BlankForMediaLocked() {
745
8/10
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 85 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 87 times.
✓ Branch 9 taken 5 times.
92 if (m_mediaBlanked || !m_planeId || !m_displayEnabled || !DrmCards::EnsureMaster(m_cardFd)) {
746 87 return;
747 }
748
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (m_flipPending) {
749
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 int vr = m_mode.vrefresh > 0 ? m_mode.vrefresh : 60;
750 3 WaitForPendingFlip((1000 / vr) + 20);
751 }
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (drmModeSetPlane(m_cardFd, m_planeId, m_crtcId, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) != 0) {
753 // A driver that needs a plane on a lit CRTC: black, then.
754 ✗ LogDebug(VB_CHANNELOUT, "KMSFrameBuffer %s: can't take the plane off (%s), blanking it\n",
755 m_connectorName.c_str(), strerror(errno));
756 ✗ if (!m_blankFb.handle) {
757 ✗ if (!CreateDumbBuffer(m_cardFd, m_fb[0].width, m_fb[0].height, m_fb[0].format, m_blankFb)) {
758 ✗ return;
759 }
760 ✗ memset(m_blankFb.mapped, 0, m_blankFb.size);
761 }
762 ✗ drmModeSetPlane(m_cardFd, m_planeId, m_crtcId, m_blankFb.fb_id, 0,
763 ✗ 0, 0, m_mode.hdisplay, m_mode.vdisplay,
764 ✗ 0, 0, (uint32_t)m_width << 16, (uint32_t)m_height << 16);
765 }
766 5 m_mediaBlanked = true;
767 5 LogInfo(VB_CHANNELOUT, "KMSFrameBuffer %s: media is playing here, standing aside\n", m_connectorName.c_str());
768 }
769
770 // The media is gone. Its kmssink may have left the CRTC in another state, so
771 // put our page back with a plane commit rather than trusting a flip from what
772 // was there before. Caller holds drmMasterLock.
773 5655 void KMSFrameBuffer::ShowAfterMediaLocked() {
774
2/2
✓ Branch 0 taken 5651 times.
✓ Branch 1 taken 4 times.
5655 if (!m_mediaBlanked) {
775 5651 return;
776 }
777 4 m_mediaBlanked = false;
778 4 m_flipPending = false;
779
4/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
8 if (m_displayEnabled && m_planeId && DrmCards::EnsureMaster(m_cardFd) &&
780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 drmModeSetPlane(m_cardFd, m_planeId, m_crtcId, m_fb[m_cPage].fb_id, 0,
781 4 0, 0, m_mode.hdisplay, m_mode.vdisplay,
782 4 0, 0, (uint32_t)m_width << 16, (uint32_t)m_height << 16) != 0) {
783 ✗ m_displayEnabled = false; // relit with a full modeset on the next flip
784 }
785 4 LogInfo(VB_CHANNELOUT, "KMSFrameBuffer %s: media finished, showing output again\n", m_connectorName.c_str());
786 }
787
788 // At once, not on our next flip: an output nothing is sending data to has
789 // none, and would sit under the video at full cost. Caller (DrmCards) holds
790 // drmMasterLock, which also guards FLIP_TARGETS.
791 18 void KMSFrameBuffer::MediaHoldChanged(int fd, uint32_t connectorId) {
792
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 for (auto& [token, fb] : FLIP_TARGETS) {
793
3/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 12 times.
18 if (fb->m_cardFd != fd || fb->m_connectorId != connectorId) {
794 6 continue;
795 }
796
3/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 5 times.
12 if (fb->MediaHoldsConnector()) {
797
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 fb->BlankForMediaLocked();
798 } else {
799
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 fb->ShowAfterMediaLocked();
800 }
801 }
802 18 }
803
804 2821 void KMSFrameBuffer::PageFlipHandler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void* data) {
805 // data is the flip token of the instance that queued this flip, so even if
806 // we drain an event that belongs to another KMSFrameBuffer sharing the same
807 // card fd, the correct instance's pending flag is cleared. Runs inside
808 // drmHandleEvent under drmMasterLock, which also guards FLIP_TARGETS.
809 2821 uint32_t token = (uint32_t)(uintptr_t)data;
810
1/2
✓ Branch 0 taken 2821 times.
✗ Branch 1 not taken.
2821 auto it = FLIP_TARGETS.find(token);
811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2821 times.
2821 if (it == FLIP_TARGETS.end()) {
812 ✗ LogWarn(VB_CHANNELOUT, "KMSFrameBuffer: dropped the flip event of a deleted framebuffer (token %u)\n", token);
813 ✗ return;
814 }
815 2821 KMSFrameBuffer* self = it->second;
816 {
817 2821 self->m_flipPending = false;
818 // "frame" is the vblank sequence number at flip completion; track the
819 // cadence. Runs inside drmHandleEvent under drmMasterLock.
820
3/4
✓ Branch 0 taken 2776 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 2776 times.
✗ Branch 3 not taken.
2821 if (self->m_lastFlipSeq != 0 && frame > self->m_lastFlipSeq) {
821 2776 uint32_t delta = frame - self->m_lastFlipSeq;
822 2776 self->m_vblankDeltaCounts[delta > 4 ? 4 : delta]++;
823
4/4
✓ Branch 0 taken 2735 times.
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 250 times.
✓ Branch 3 taken 2485 times.
2776 if (self->m_prevVblankDelta != 0 && delta != self->m_prevVblankDelta) {
824 250 self->m_vblankCadenceBreaks++;
825 }
826 2776 self->m_prevVblankDelta = delta;
827 }
828 2821 self->m_lastFlipSeq = frame;
829 2821 self->m_lastFlipTimeUS = (uint64_t)sec * 1000000ULL + usec;
830 }
831 }
832
833 2430 void KMSFrameBuffer::WaitForPendingFlip(int timeoutMs, std::unique_lock<std::mutex>* lock) {
834
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2430 times.
2430 if (!m_flipPending) {
835 ✗ return;
836 }
837 2430 drmEventContext evctx = {};
838 2430 evctx.version = 2;
839 2430 evctx.page_flip_handler = &KMSFrameBuffer::PageFlipHandler;
840 2430 evctx.vblank_handler = &KMSFrameBuffer::VBlankHandler;
841
842 // Outputs on a card share its fd, so an event may be another output's
843 // (drmHandleEvent routes it by token): keep waiting for ours (#3171).
844 // With `lock`, sleep unlocked in short slices, read events only under the
845 // lock, and look once more after the deadline before giving up.
846 static constexpr int SLICE_MS = 4;
847
1/2
✓ Branch 0 taken 2430 times.
✗ Branch 1 not taken.
2430 long long deadline = GetMonotonicTimeMS() + timeoutMs;
848
2/2
✓ Branch 0 taken 6002 times.
✓ Branch 1 taken 2430 times.
8432 while (m_flipPending) {
849
1/2
✓ Branch 0 taken 6002 times.
✗ Branch 1 not taken.
6002 long long remaining = deadline - GetMonotonicTimeMS();
850 6002 struct pollfd pfd = {};
851 6002 pfd.fd = m_cardFd;
852 6002 pfd.events = POLLIN;
853
3/4
✓ Branch 0 taken 5960 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 5960 times.
✗ Branch 3 not taken.
6002 if (lock && remaining > 0) {
854
1/2
✓ Branch 0 taken 5960 times.
✗ Branch 1 not taken.
5960 lock->unlock();
855
1/2
✓ Branch 0 taken 5960 times.
✗ Branch 1 not taken.
5960 poll(&pfd, 1, (int)std::min<long long>(remaining, SLICE_MS));
856
1/2
✓ Branch 0 taken 5960 times.
✗ Branch 1 not taken.
5960 lock->lock();
857 }
858 6002 pfd.revents = 0;
859
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 5960 times.
✓ Branch 2 taken 6002 times.
✗ Branch 3 not taken.
6002 int r = poll(&pfd, 1, lock ? 0 : (int)std::max<long long>(remaining, 0));
860
4/8
✓ Branch 0 taken 2821 times.
✓ Branch 1 taken 3181 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2821 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2821 times.
✗ Branch 7 not taken.
8823 bool ok = (r == 0) || (r < 0 && errno == EINTR) ||
861
3/6
✓ Branch 0 taken 2821 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2821 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2821 times.
✗ Branch 5 not taken.
2821 (r > 0 && (pfd.revents & POLLIN) && drmHandleEvent(m_cardFd, &evctx) == 0);
862
4/6
✓ Branch 0 taken 3572 times.
✓ Branch 1 taken 2430 times.
✓ Branch 2 taken 3572 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3572 times.
6002 if (m_flipPending && (!ok || remaining <= 0)) {
863 // Fd error, or nothing in time: give up so a lost event can't wedge us.
864 ✗ m_flipWaitTimeouts++;
865 ✗ m_flipPending = false;
866 }
867 }
868 }
869
870 798 void KMSFrameBuffer::WaitForPageFree() {
871
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 798 times.
798 if (m_pages == 1) {
872 ✗ return;
873 }
874
1/2
✓ Branch 0 taken 798 times.
✗ Branch 1 not taken.
798 std::unique_lock<std::mutex> lock(drmMasterLock);
875
2/2
✓ Branch 0 taken 703 times.
✓ Branch 1 taken 95 times.
798 if (m_flipPending) {
876
1/2
✓ Branch 0 taken 703 times.
✗ Branch 1 not taken.
703 int vr = m_mode.vrefresh > 0 ? m_mode.vrefresh : 60;
877 703 uint32_t timeoutsBefore = m_flipWaitTimeouts;
878
1/2
✓ Branch 0 taken 703 times.
✗ Branch 1 not taken.
703 WaitForPendingFlip((1000 / vr) + 20, &lock);
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 703 times.
703 if (m_flipWaitTimeouts != timeoutsBefore) {
880 // TODO: any event on the card's shared fd ends this wait, not just our
881 // vblank, and the poll holds drmMasterLock (up to 70 ms at 20 Hz).
882 // The completion event never came, so it is unknown whether the
883 // hardware has switched pages yet. A queued flip lands at the next
884 // vblank at the latest, so wait one out before the caller writes
885 // into the page it is about to leave. Ask for the vblank as an
886 // event on OUR crtc (the pipe bits select it; without them this
887 // would be pipe 0, which on a Pi 4 with HDMI up is not the DPI) and
888 // poll for it with the same bound as the flip wait, so a display
889 // whose vblanks have stopped cannot wedge the output thread here.
890 ✗ drmVBlank vbl = {};
891 ✗ unsigned int type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
892 ✗ if (m_crtcPipe > 0) {
893 ✗ type |= (unsigned int)((m_crtcPipe << _DRM_VBLANK_HIGH_CRTC_SHIFT) & _DRM_VBLANK_HIGH_CRTC_MASK);
894 }
895 ✗ vbl.request.type = (drmVBlankSeqType)type;
896 ✗ vbl.request.sequence = 1;
897 ✗ vbl.request.signal = 0;
898 ✗ if (drmWaitVBlank(m_cardFd, &vbl) == 0) {
899 ✗ struct pollfd pfd = {};
900 ✗ pfd.fd = m_cardFd;
901 ✗ pfd.events = POLLIN;
902 ✗ if (poll(&pfd, 1, (1000 / vr) + 20) > 0 && (pfd.revents & POLLIN)) {
903 ✗ drmEventContext evctx = {};
904 ✗ evctx.version = 2;
905 ✗ evctx.page_flip_handler = &KMSFrameBuffer::PageFlipHandler;
906 ✗ evctx.vblank_handler = &KMSFrameBuffer::VBlankHandler;
907 ✗ drmHandleEvent(m_cardFd, &evctx);
908 }
909 }
910 }
911 }
912 798 }
913
914 ✗ void KMSFrameBuffer::VBlankHandler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void* data) {
915 // Nothing to record; the wait in WaitForPageFree() only needs the event
916 // to have been delivered.
917 ✗ }
918
919 2910 void KMSFrameBuffer::SyncDisplay(bool pageChanged) {
920
2/4
✓ Branch 0 taken 2910 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2910 times.
2910 if (!pageChanged || m_pages == 1)
921 85 return;
922
923
1/2
✓ Branch 0 taken 2910 times.
✗ Branch 1 not taken.
2910 std::unique_lock<std::mutex> lock(drmMasterLock);
924
3/4
✓ Branch 0 taken 2910 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 2825 times.
2910 if (MediaHoldsConnector()) {
925
1/2
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
85 BlankForMediaLocked();
926 85 return;
927 }
928
1/2
✓ Branch 0 taken 2825 times.
✗ Branch 1 not taken.
2825 ShowAfterMediaLocked();
929 {
930
1/2
✓ Branch 0 taken 2825 times.
✗ Branch 1 not taken.
2825 long long syncStart = GetMonotonicTimeMS();
931
2/4
✓ Branch 0 taken 2825 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2825 times.
✗ Branch 3 not taken.
2825 if (DrmCards::EnsureMaster(m_cardFd)) {
932 // If the previous flip hasn't retired yet, wait for its vblank
933 // completion event instead of letting this flip return EBUSY and
934 // fall into the expensive SetPlane/SetCrtc modeset path. poll()
935 // sleeps until the flip completes, so this paces us to the panel's
936 // refresh with no busy-waiting. This wait and the alignment sleep
937 // release drmMasterLock, shared by every KMS output (#3171).
938
2/2
✓ Branch 0 taken 1685 times.
✓ Branch 1 taken 1140 times.
2825 if (m_flipPending) {
939
1/2
✓ Branch 0 taken 1685 times.
✗ Branch 1 not taken.
1685 int vr = m_mode.vrefresh > 0 ? m_mode.vrefresh : 60;
940 1685 int timeoutMs = (1000 / vr) + 20; // one refresh interval + slack
941
1/2
✓ Branch 0 taken 1685 times.
✗ Branch 1 not taken.
1685 WaitForPendingFlip(timeoutMs, &lock);
942 }
943
944 // Phase alignment: fppd's software frame clock and the panel's
945 // vblank clock are never exactly the same rate, so the submission
946 // time slowly drifts across the vblank grid. When it straddles a
947 // boundary, scheduling jitter makes alternate frames catch/miss
948 // the vblank and frames display for e.g. 25ms/75ms instead of a
949 // steady 50ms - a visible judder burst until the drift clears the
950 // boundary (confirmed via the vblank-delta counters: 1,3,1,3
951 // bursts exactly at observed stutters). A "nudge only near the
952 // boundary" zone just moves the discontinuity to the zone edge
953 // (confirmed in testing: crossings leaked at the edge while the
954 // phase transited the zone), so instead align EVERY submission to
955 // the middle of the vblank interval - scheduling jitter can never
956 // reach a boundary from there, and the clock drift shows up as
957 // this sleep slowly varying. When the sleep wraps, the
958 // unavoidable one-vblank phase slip happens once, cleanly, per
959 // beat cycle instead of oscillating. Align unconditionally:
960 // gating this on "slack since the last completion" feeds back on
961 // itself (an aligned flip completes later in the cycle, the gate
962 // then skips the next frame, which lands early - producing a
963 // permanent 1,3,1,3 oscillation; confirmed in testing). The
964 // completion timestamps are vblank-quantized, so aligning every
965 // frame pins submissions to the true grid midpoint and is stable
966 // at any frame rate at or below the refresh rate.
967 2825 long long alignSleepUS = 0;
968
3/4
✓ Branch 0 taken 2787 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 2787 times.
✗ Branch 3 not taken.
2825 if (m_lastFlipTimeUS != 0 && m_mode.vrefresh > 0) {
969 struct timespec tsm;
970 2787 clock_gettime(CLOCK_MONOTONIC, &tsm);
971 2787 uint64_t nowUS = (uint64_t)tsm.tv_sec * 1000000ULL + tsm.tv_nsec / 1000;
972 2787 uint64_t periodUS = 1000000ULL / m_mode.vrefresh;
973 // The 60s cap also skips alignment if the driver reported
974 // non-monotonic timestamps - elapsed would be implausible.
975
1/2
✓ Branch 0 taken 2787 times.
✗ Branch 1 not taken.
2787 if (nowUS > m_lastFlipTimeUS &&
976
1/2
✓ Branch 0 taken 2787 times.
✗ Branch 1 not taken.
2787 (nowUS - m_lastFlipTimeUS) < 60000000ULL) {
977 2787 uint64_t phaseUS = (nowUS - m_lastFlipTimeUS) % periodUS;
978 2787 uint64_t sleepUS = (periodUS / 2 + periodUS - phaseUS) % periodUS;
979 // Already within 2ms past mid-interval: close enough,
980 // don't sleep a nearly full period chasing the target.
981
3/4
✓ Branch 0 taken 2787 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2697 times.
✓ Branch 3 taken 90 times.
2787 if (sleepUS > 0 && sleepUS < periodUS - 2000) {
982 2697 m_phaseNudges++;
983 2697 alignSleepUS = sleepUS;
984
1/2
✓ Branch 0 taken 2697 times.
✗ Branch 1 not taken.
2697 lock.unlock();
985
1/2
✓ Branch 0 taken 2697 times.
✗ Branch 1 not taken.
2697 std::this_thread::sleep_for(std::chrono::microseconds(sleepUS));
986
1/2
✓ Branch 0 taken 2697 times.
✗ Branch 1 not taken.
2697 lock.lock();
987 }
988 }
989 }
990
991 // Media may have taken or released the connector while unlocked.
992
2/4
✓ Branch 0 taken 2825 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2825 times.
2825 if (MediaHoldsConnector()) {
993 ✗ BlankForMediaLocked();
994 ✗ return;
995 }
996
1/2
✓ Branch 0 taken 2825 times.
✗ Branch 1 not taken.
2825 ShowAfterMediaLocked();
997
998 2825 bool wasEnabled = m_displayEnabled;
999 2825 int ret = -1;
1000
2/2
✓ Branch 0 taken 2823 times.
✓ Branch 1 taken 2 times.
2825 if (m_displayEnabled) {
1001 2823 ret = drmModePageFlip(m_cardFd, m_crtcId, m_fb[m_cPage].fb_id,
1002
1/2
✓ Branch 0 taken 2823 times.
✗ Branch 1 not taken.
2823 DRM_MODE_PAGE_FLIP_EVENT, (void*)(uintptr_t)m_flipToken);
1003 }
1004
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2823 times.
2825 if (ret) {
1005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (wasEnabled) {
1006 // Flip rejected on an already-enabled display; the modeset
1007 // fallback below costs multiple vblanks.
1008 ✗ m_flipRejects++;
1009 }
1010 // First flip after enable, or the flip was rejected: bring the
1011 // display up with a full modeset/plane commit, then queue a flip.
1012
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!m_displayEnabled) {
1013
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 ModesetLocked();
1014 2 m_displayEnabled = true;
1015 ✗ } else if (m_planeId) {
1016 ✗ drmModeSetPlane(m_cardFd, m_planeId, m_crtcId, m_fb[m_cPage].fb_id, 0,
1017 ✗ 0, 0, m_mode.hdisplay, m_mode.vdisplay,
1018 ✗ 0, 0, (uint32_t)m_width << 16, (uint32_t)m_height << 16);
1019 }
1020 2 ret = drmModePageFlip(m_cardFd, m_crtcId, m_fb[m_cPage].fb_id,
1021
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 DRM_MODE_PAGE_FLIP_EVENT, (void*)(uintptr_t)m_flipToken);
1022 }
1023 2825 m_flipPending = (ret == 0);
1024
1025 // Track flip-path health. A single sync should normally complete
1026 // well inside one refresh interval; count the ones that don't and
1027 // surface them (rate-limited) at Warn level so output stutters can
1028 // be correlated with (or ruled out of) the KMS flip path without
1029 // needing debug logging.
1030
1/2
✓ Branch 0 taken 2825 times.
✗ Branch 1 not taken.
2825 long long syncEnd = GetMonotonicTimeMS();
1031 // The phase-alignment sleep is intentional; don't count it as a
1032 // flip-path delay.
1033 2825 int tookMS = (int)(syncEnd - syncStart) - (int)(alignSleepUS / 1000);
1034
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 2501 times.
2825 if (tookMS > 20) {
1035 324 m_slowSyncs++;
1036 324 m_lastSlowSyncMS = tookMS;
1037
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 322 times.
324 if (tookMS > m_maxSyncMS) {
1038 2 m_maxSyncMS = tookMS;
1039 }
1040 }
1041 // Warn only when a new delay event has occurred since the last
1042 // warning (rate-limited), so each warning line timestamps actual
1043 // trouble rather than repeating stale totals.
1044 2825 uint64_t eventTotal = (uint64_t)m_slowSyncs + m_flipWaitTimeouts + m_flipRejects + m_vblankCadenceBreaks;
1045
2/2
✓ Branch 0 taken 1681 times.
✓ Branch 1 taken 1144 times.
2825 if (eventTotal != m_warnedEventTotal &&
1046
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1666 times.
1681 (syncEnd - m_lastFlipWarnMS) > 10000) {
1047 15 m_lastFlipWarnMS = syncEnd;
1048 15 m_warnedEventTotal = eventTotal;
1049
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 LogWarn(VB_CHANNELOUT, "KMSFrameBuffer %s flip-path event; totals since start: %u syncs >20ms (max %dms, last %dms), %u wait timeouts, %u flip rejects, %u vblank cadence breaks (deltas 1/2/3/4+: %u/%u/%u/%u), %u phase nudges\n",
1050 m_connectorName.c_str(), m_slowSyncs, m_maxSyncMS, m_lastSlowSyncMS, m_flipWaitTimeouts, m_flipRejects,
1051 m_vblankCadenceBreaks,
1052 m_vblankDeltaCounts[1], m_vblankDeltaCounts[2], m_vblankDeltaCounts[3], m_vblankDeltaCounts[4],
1053 m_phaseNudges);
1054 }
1055 }
1056 }
1057
2/2
✓ Branch 0 taken 2825 times.
✓ Branch 1 taken 85 times.
2910 }
1058
1059 ✗ void KMSFrameBuffer::DisableDisplay() {
1060 ✗ std::unique_lock<std::mutex> lock(drmMasterLock);
1061 ✗ if (MediaHoldsConnector()) {
1062 ✗ return;
1063 }
1064 {
1065 ✗ if (DrmCards::EnsureMaster(m_cardFd)) {
1066 ✗ if (m_crtcId && m_planeId) {
1067 ✗ drmModeSetPlane(m_cardFd, m_planeId, m_crtcId, m_fb[m_cPage].fb_id, 0,
1068 0, 0, 0, 0, 0, 0, 0, 0);
1069 ✗ drmModeSetCrtc(m_cardFd, m_crtcId, 0, 0, 0, nullptr, 0, nullptr);
1070 ✗ m_displayEnabled = false;
1071 ✗ m_flipPending = false;
1072 }
1073 }
1074 }
1075 ✗ }
1076
1077 19 int KMSFrameBuffer::ModesetLocked() {
1078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!m_planeId) {
1079 ✗ return drmModeSetCrtc(m_cardFd, m_crtcId, m_fb[m_cPage].fb_id, 0, 0,
1080 ✗ &m_connectorId, 1, &m_mode);
1081 }
1082
1083 // Legacy SetCrtc scans the CRTC's primary plane out at the full mode size,
1084 // so it rejects (ENOSPC) a framebuffer smaller than the mode - and ours is
1085 // only the model times its pixel size, which is often short of the panel.
1086 // Without this, a CRTC that went off could never be turned back on: every
1087 // earlier "modeset" here failed and only worked because the console had
1088 // left the CRTC running. Light it with a blank mode-sized buffer instead,
1089 // put the picture on our (scaling) plane, then take the blank one away.
1090 19 DumbBuffer blank;
1091
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 2 times.
19 bool useBlank = m_fb[m_cPage].width < m_mode.hdisplay || m_fb[m_cPage].height < m_mode.vdisplay;
1092
5/8
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 17 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 19 times.
19 if (useBlank && !CreateDumbBuffer(m_cardFd, m_mode.hdisplay, m_mode.vdisplay, DRM_FORMAT_RGB565, blank) &&
1093 ✗ !CreateDumbBuffer(m_cardFd, m_mode.hdisplay, m_mode.vdisplay, DRM_FORMAT_XRGB8888, blank)) {
1094 ✗ useBlank = false;
1095 }
1096
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
19 if (useBlank) {
1097 17 memset(blank.mapped, 0, blank.size);
1098 }
1099
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
19 int ret = drmModeSetCrtc(m_cardFd, m_crtcId, useBlank ? blank.fb_id : m_fb[m_cPage].fb_id, 0, 0,
1100 &m_connectorId, 1, &m_mode);
1101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret) {
1102 // Still place the plane: that is enough when the CRTC is already
1103 // running this mode, which is all the old code ever managed.
1104 ✗ LogWarn(VB_CHANNELOUT, "KMSFrameBuffer %s: modeset failed: %d (%s)\n", m_connectorName.c_str(), ret, strerror(errno));
1105 }
1106 38 ret = drmModeSetPlane(m_cardFd, m_planeId, m_crtcId, m_fb[m_cPage].fb_id, 0,
1107 19 0, 0, m_mode.hdisplay, m_mode.vdisplay,
1108
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 0, 0, (uint32_t)m_width << 16, (uint32_t)m_height << 16);
1109
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
19 if (useBlank) {
1110 // Unless our plane is the primary (and has just replaced it), the blank
1111 // buffer is still on the primary plane. Take it off explicitly: removing
1112 // an in-use framebuffer can make the kernel turn the whole CRTC off.
1113
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 drmModePlaneResPtr planes = drmModeGetPlaneResources(m_cardFd);
1114
3/4
✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 228 times.
✓ Branch 3 taken 17 times.
245 for (uint32_t i = 0; planes && i < planes->count_planes; i++) {
1115
1/2
✓ Branch 0 taken 228 times.
✗ Branch 1 not taken.
228 drmModePlanePtr plane = drmModeGetPlane(m_cardFd, planes->planes[i]);
1116
2/4
✓ Branch 0 taken 228 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 228 times.
228 if (plane && plane->fb_id == blank.fb_id) {
1117 ✗ drmModeSetPlane(m_cardFd, plane->plane_id, m_crtcId, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1118 }
1119
1/2
✓ Branch 0 taken 228 times.
✗ Branch 1 not taken.
228 if (plane) {
1120
1/2
✓ Branch 0 taken 228 times.
✗ Branch 1 not taken.
228 drmModeFreePlane(plane);
1121 }
1122 }
1123
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (planes) {
1124
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 drmModeFreePlaneResources(planes);
1125 }
1126
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 DestroyDumbBuffer(m_cardFd, blank);
1127 }
1128 19 return ret;
1129 }
1130
1131 17 void KMSFrameBuffer::EnableDisplay() {
1132
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 LogDebug(VB_CHANNELOUT, "KMSFrameBuffer::EnableDisplay() called for %s\n", m_connectorName.c_str());
1133
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 std::unique_lock<std::mutex> lock(drmMasterLock);
1134
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
17 if (MediaHoldsConnector()) {
1135 ✗ LogDebug(VB_CHANNELOUT, " Skipping: media is playing on this connector\n");
1136 ✗ return;
1137 }
1138 {
1139
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 if (DrmCards::EnsureMaster(m_cardFd)) {
1140
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 if (m_crtcId && m_planeId) {
1141
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 int ret = ModesetLocked();
1142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret) {
1143 ✗ LogErr(VB_CHANNELOUT, "KMSFrameBuffer::EnableDisplay set_plane failed: %d\n", ret);
1144 } else {
1145 17 m_displayEnabled = true;
1146
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 LogInfo(VB_CHANNELOUT, "KMSFrameBuffer::EnableDisplay: SUCCESS for %s\n", m_connectorName.c_str());
1147 }
1148 }
1149 } else {
1150 ✗ LogWarn(VB_CHANNELOUT, "KMSFrameBuffer::EnableDisplay: Failed to get DRM master for %s\n", m_connectorName.c_str());
1151 }
1152 }
1153
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 }
1154
1155 // Minimum vertical blanking (scanlines) to keep between vactive and vtotal. The
1156 // RP1 DPI generates a low signal on the data pins during blanking, so this also
1157 // contributes to the WS281x reset gap; the caller (DPIPixelsOutput) additionally
1158 // pads vactive with blank scanlines so the reset is guaranteed regardless.
1159 static constexpr int KMS_MIN_VBLANK_LINES = 3;
1160
1161 21 void KMSFrameBuffer::ApplyVerticalTiming(drmModeModeInfo& mode, int vactive, int fps) {
1162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (mode.htotal == 0) {
1163 ✗ return;
1164 }
1165 21 int minVtotal = vactive + KMS_MIN_VBLANK_LINES;
1166 21 int vtotal = minVtotal;
1167
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (fps > 0) {
1168 // pixel clock is kHz; vtotal = clockHz / (htotal * fps)
1169 21 long long v = llround((double)mode.clock * 1000.0 / ((double)mode.htotal * (double)fps));
1170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (v > minVtotal) {
1171 ✗ vtotal = (int)v;
1172 }
1173 }
1174 21 mode.vdisplay = vactive;
1175 21 mode.vsync_start = vactive + 1;
1176 21 mode.vsync_end = vactive + 2;
1177 21 mode.vtotal = vtotal;
1178 21 mode.vrefresh = (uint32_t)llround((double)mode.clock * 1000.0 / ((double)mode.htotal * (double)vtotal));
1179 21 snprintf(mode.name, sizeof(mode.name), "%dx%d", mode.hdisplay, vactive);
1180 }
1181
1182 21 int KMSFrameBuffer::GetMaxRefreshRate() {
1183
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
21 if (!m_variableRefresh || m_mode.htotal == 0) {
1184 ✗ return 0;
1185 }
1186 21 int minVtotal = m_mode.vdisplay + KMS_MIN_VBLANK_LINES;
1187 21 return (int)((double)m_mode.clock * 1000.0 / ((double)m_mode.htotal * (double)minVtotal));
1188 }
1189
1190 117 bool KMSFrameBuffer::SetRefreshRate(int fps) {
1191
3/6
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 117 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 117 times.
117 if (!m_variableRefresh || fps <= 0 || m_mode.htotal == 0) {
1192 ✗ return false;
1193 }
1194
1195 117 int minVtotal = m_mode.vdisplay + KMS_MIN_VBLANK_LINES;
1196 117 long long v = llround((double)m_mode.clock * 1000.0 / ((double)m_mode.htotal * (double)fps));
1197
1/2
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
117 int newVtotal = (v > minVtotal) ? (int)v : minVtotal;
1198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 if (newVtotal == m_mode.vtotal) {
1199 ✗ return true; // already at this rate
1200 }
1201
1202 // Only the vertical blanking changes; horizontal timing and the pixel clock
1203 // (and therefore the WS bit timing and the framebuffer layout) are untouched.
1204 // Built in a copy: m_mode is what the CRTC is running, which the check
1205 // above and the flip timeouts depend on. Updating it before the modeset
1206 // happened made a refused attempt look applied - the next call returned
1207 // "already at this rate" and the new rate was never set.
1208 117 drmModeModeInfo mode = m_mode;
1209 117 mode.vsync_start = mode.vdisplay + 1;
1210 117 mode.vsync_end = mode.vdisplay + 2;
1211 117 mode.vtotal = newVtotal;
1212 117 mode.vrefresh = (uint32_t)llround((double)mode.clock * 1000.0 / ((double)mode.htotal * (double)newVtotal));
1213
1214
1/2
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
117 std::unique_lock<std::mutex> lock(drmMasterLock);
1215
3/4
✓ Branch 0 taken 117 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
✓ Branch 3 taken 29 times.
117 if (MediaHoldsConnector()) {
1216 // The connector is currently presenting media playback; don't fight it.
1217 // Report "not applied" so the caller's idea of the live rate stays
1218 // honest and it asks again next frame.
1219 88 return false;
1220 }
1221
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 29 times.
29 if (!DrmCards::EnsureMaster(m_cardFd)) {
1222 // Couldn't grab master right now. As above, not applied; the caller
1223 // asks again.
1224 ✗ return false;
1225 }
1226 // Drain any in-flight flip so the modeset starts from a known state.
1227
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 19 times.
29 if (m_flipPending) {
1228
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 int vr = m_mode.vrefresh > 0 ? m_mode.vrefresh : 60;
1229
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 WaitForPendingFlip((1000 / vr) + 20);
1230 }
1231
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 int ret = drmModeSetCrtc(m_cardFd, m_crtcId, m_fb[m_cPage].fb_id, 0, 0,
1232 &m_connectorId, 1, &mode);
1233
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 2 times.
29 if (ret == 0) {
1234 27 m_mode = mode;
1235 27 m_displayEnabled = true;
1236
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (m_planeId) {
1237 27 drmModeSetPlane(m_cardFd, m_planeId, m_crtcId, m_fb[m_cPage].fb_id, 0,
1238 27 0, 0, m_mode.hdisplay, m_mode.vdisplay,
1239
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 0, 0, (uint32_t)m_width << 16, (uint32_t)m_height << 16);
1240 }
1241 // A mode change restarts the vblank stream; clear the cadence trackers so
1242 // the transition isn't miscounted as a stutter.
1243 27 m_flipPending = false;
1244 27 m_lastFlipSeq = 0;
1245 27 m_prevVblankDelta = 0;
1246
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 LogInfo(VB_CHANNELOUT, "KMSFrameBuffer %s: refresh set to %dHz (vtotal=%d)\n",
1247 m_connectorName.c_str(), m_mode.vrefresh, newVtotal);
1248 } else {
1249
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogErr(VB_CHANNELOUT, "KMSFrameBuffer::SetRefreshRate modeset failed: %d (%s)\n", ret, strerror(errno));
1250 }
1251 29 return ret == 0;
1252 117 }
1253
1254 // ---------------------------------------------------------------------------
1255 // Display hotplug
1256 // ---------------------------------------------------------------------------
1257
1258 // Quiet time after the last uevent before acting on a batch. A sink powering
1259 // up can bounce HPD a few times, and acting once on the settled state avoids a
1260 // modeset per bounce.
1261 static constexpr int HOTPLUG_SETTLE_MS = 1000;
1262 // How often an output without a display re-probes for one. Events cover
1263 // most of it, but vc4.force_hotplug (the ForceHDMI setting) makes the kernel
1264 // ignore HPD: a projector switched on later sends nothing, and without this
1265 // the output would run the stand-in mode, under a stale warning, for good.
1266 static constexpr int ABSENT_RECHECK_MS = 10000;
1267
1268 static std::mutex s_hotplugLock; // guards the three below
1269 static std::thread* s_hotplugThread = nullptr;
1270 static int s_hotplugSock = -1;
1271 static int s_hotplugWakeFd = -1;
1272
1273 37 void KMSFrameBuffer::StartHotplugMonitor() {
1274
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 std::unique_lock<std::mutex> lock(s_hotplugLock);
1275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (s_hotplugThread) {
1276 ✗ return;
1277 }
1278 // The kernel's own uevent multicast group (1), not udevd's re-broadcast:
1279 // it needs no udevd and no libudev, and fppd runs as root.
1280 37 int sock = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
1281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (sock < 0) {
1282 ✗ LogWarn(VB_CHANNELOUT, "KMSFrameBuffer: no uevent socket, display hotplug will not be handled: %s\n", strerror(errno));
1283 ✗ return;
1284 }
1285 37 struct sockaddr_nl addr = {};
1286 37 addr.nl_family = AF_NETLINK;
1287 37 addr.nl_groups = 1;
1288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
1289 ✗ LogWarn(VB_CHANNELOUT, "KMSFrameBuffer: could not bind the uevent socket, display hotplug will not be handled: %s\n", strerror(errno));
1290 ✗ close(sock);
1291 ✗ return;
1292 }
1293 37 int wakeFd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
1294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (wakeFd < 0) {
1295 ✗ close(sock);
1296 ✗ return;
1297 }
1298 37 s_hotplugSock = sock;
1299 37 s_hotplugWakeFd = wakeFd;
1300
3/8
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 37 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
37 s_hotplugThread = new std::thread(HotplugMonitorLoop, sock, wakeFd);
1301
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 }
1302
1303 32 void KMSFrameBuffer::StopHotplugMonitor() {
1304
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 std::unique_lock<std::mutex> lock(s_hotplugLock);
1305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!s_hotplugThread) {
1306 ✗ return;
1307 }
1308 32 uint64_t one = 1;
1309
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 write(s_hotplugWakeFd, &one, sizeof(one));
1310
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 s_hotplugThread->join();
1311
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 delete s_hotplugThread;
1312 32 s_hotplugThread = nullptr;
1313
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 close(s_hotplugSock);
1314
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 close(s_hotplugWakeFd);
1315 32 s_hotplugSock = -1;
1316 32 s_hotplugWakeFd = -1;
1317
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 }
1318
1319 37 void KMSFrameBuffer::HotplugMonitorLoop(int sock, int wakeFd) {
1320
2/4
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
37 SetThreadName("FPP-DRMHotplug");
1321
1322 // card path ("/dev/dri/card1") -> connector ids named by the batch's
1323 // events; 0 for an event that named none (a sysfs status write, polled
1324 // connectors). Any event re-probes every output on its card.
1325 37 std::map<std::string, std::set<uint32_t>> pending;
1326 37 long long deadline = 0;
1327
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 long long nextRecheck = GetMonotonicTimeMS() + ABSENT_RECHECK_MS;
1328 char buf[8192];
1329
1330 while (true) {
1331 47 struct pollfd pfds[2] = {};
1332 47 pfds[0].fd = sock;
1333 47 pfds[0].events = POLLIN;
1334 47 pfds[1].fd = wakeFd;
1335 47 pfds[1].events = POLLIN;
1336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 long long wake = deadline ? std::min(deadline, nextRecheck) : nextRecheck;
1337
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 int timeout = (int)std::max(0LL, wake - GetMonotonicTimeMS());
1338
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
47 int r = poll(pfds, 2, timeout);
1339
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
42 if (r < 0 && errno != EINTR) {
1340 ✗ LogWarn(VB_CHANNELOUT, "KMSFrameBuffer: uevent poll failed, display hotplug will no longer be handled: %s\n", strerror(errno));
1341 ✗ return;
1342 }
1343
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 10 times.
42 if (pfds[1].revents & POLLIN) {
1344 32 return;
1345 }
1346
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 if (pfds[0].revents & POLLIN) {
1347 ssize_t len;
1348
3/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
8 while ((len = recv(sock, buf, sizeof(buf) - 1, 0)) > 0) {
1349 // "change@/devices/...\0ACTION=change\0SUBSYSTEM=drm\0HOTPLUG=1\0..."
1350 4 buf[len] = 0;
1351 4 std::string subsystem, devname;
1352 4 bool hotplug = false;
1353 4 uint32_t connectorId = 0;
1354
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 4 times.
44 for (char* p = buf; p < buf + len; p += strlen(p) + 1) {
1355
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 36 times.
40 if (!strncmp(p, "SUBSYSTEM=", 10)) {
1356
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 subsystem = p + 10;
1357
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 32 times.
36 } else if (!strncmp(p, "DEVNAME=", 8)) {
1358
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 devname = p + 8;
1359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 } else if (!strcmp(p, "HOTPLUG=1")) {
1360 ✗ hotplug = true;
1361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 } else if (!strncmp(p, "CONNECTOR=", 10)) {
1362 ✗ connectorId = (uint32_t)strtoul(p + 10, nullptr, 10);
1363 }
1364 }
1365
4/10
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
4 if (subsystem != "drm" || !hotplug || devname.empty()) {
1366 4 continue;
1367 }
1368 ✗ LogDebug(VB_CHANNELOUT, "KMSFrameBuffer: hotplug uevent on %s connector %u\n", devname.c_str(), connectorId);
1369 ✗ pending["/dev/" + devname].insert(connectorId);
1370 ✗ deadline = GetMonotonicTimeMS() + HOTPLUG_SETTLE_MS;
1371
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
8 }
1372 }
1373
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 10 times.
10 if (deadline && GetMonotonicTimeMS() >= deadline) {
1374 ✗ for (auto& [cardPath, connectorIds] : pending) {
1375 ✗ HandleHotplug(cardPath, connectorIds, false);
1376 }
1377 ✗ pending.clear();
1378 ✗ deadline = 0;
1379 }
1380
3/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 4 times.
10 if (GetMonotonicTimeMS() >= nextRecheck) {
1381 6 std::set<std::string> cards;
1382 {
1383
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 std::unique_lock<std::mutex> lock(drmMasterLock);
1384
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 for (auto& [token, fb] : FLIP_TARGETS) {
1385
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (fb->m_hotplugReady && !fb->m_sinkPresent) {
1386 ✗ cards.insert(fb->m_cardInfo->path);
1387 }
1388 }
1389 6 }
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 for (auto& cardPath : cards) {
1391 ✗ HandleHotplug(cardPath, {}, true);
1392 }
1393
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 nextRecheck = GetMonotonicTimeMS() + ABSENT_RECHECK_MS;
1394 6 }
1395 10 }
1396 32 }
1397
1398 ✗ void KMSFrameBuffer::HandleHotplug(const std::string& cardPath, const std::set<uint32_t>& connectorIds, bool onlyAbsent) {
1399 ✗ std::unique_lock<std::mutex> lock(drmMasterLock);
1400 ✗ std::vector<std::pair<uint32_t, std::string>> outputs;
1401 ✗ for (auto& [token, fb] : FLIP_TARGETS) {
1402 ✗ if (fb->m_hotplugReady && fb->m_cardInfo->path == cardPath && !(onlyAbsent && fb->m_sinkPresent)) {
1403 ✗ outputs.emplace_back(token, fb->m_connectorName);
1404 }
1405 }
1406 ✗ for (auto& [token, name] : outputs) {
1407 ✗ auto it = FLIP_TARGETS.find(token);
1408 ✗ if (it == FLIP_TARGETS.end() || !it->second->m_hotplugReady) {
1409 ✗ continue;
1410 }
1411 ✗ KMSFrameBuffer* fb = it->second;
1412
1413 // A real probe (see ProbeConnectorLocked()), so it also refills the
1414 // sysfs mode list that was emptied while the display was away. Every
1415 // output's flips wait out this EDID read, once per hotplug.
1416 ✗ drmModeConnectorPtr conn = ProbeConnectorLocked(fb->m_cardFd, fb->m_connectorId);
1417 ✗ bool present = SinkPresent(fb->m_cardFd, conn);
1418 ✗ drmModeModeInfo mode = {};
1419 ✗ bool haveMode = present && PickMode(conn, mode);
1420 ✗ if (conn) {
1421 ✗ drmModeFreeConnector(conn);
1422 }
1423
1424 ✗ bool wasPresent = fb->m_sinkPresent;
1425 ✗ fb->SetSinkPresentLocked(present);
1426 ✗ if (!present) {
1427 ✗ if (wasPresent) {
1428 ✗ LogInfo(VB_CHANNELOUT, "KMSFrameBuffer %s: display disconnected\n", name.c_str());
1429 }
1430 ✗ continue;
1431 }
1432 // Back after we saw it gone. An event naming this connector while it
1433 // looks present also counts: the sink went away and came back before
1434 // the batch settled, or (with force_hotplug) its EDID changed. A
1435 // card-wide event alone does not: it may be about another connector,
1436 // and a needless modeset blanks this one (or glitches DPI pixels).
1437 ✗ if (wasPresent && !connectorIds.count(fb->m_connectorId)) {
1438 ✗ continue;
1439 }
1440 ✗ LogInfo(VB_CHANNELOUT, "KMSFrameBuffer %s: display %s\n", name.c_str(),
1441 wasPresent ? "re-announced itself" : "reconnected");
1442 ✗ if (fb->MediaHoldsConnector()) {
1443 ✗ LogInfo(VB_CHANNELOUT, "KMSFrameBuffer %s: media owns the connector, not restarting the display\n", name.c_str());
1444 ✗ continue;
1445 }
1446 // Run the display's own mode: the one we have may have been the stand-in
1447 // for a display that wasn't there yet, or this may be another display.
1448 // The framebuffer stays as it is; the plane scales it onto whatever mode
1449 // the CRTC runs. (DPI Pixels sets its own timing, but is never
1450 // external, so never gets here.)
1451 ✗ if (haveMode && !fb->m_variableRefresh && !SameTiming(mode, fb->m_mode)) {
1452 ✗ LogInfo(VB_CHANNELOUT, "KMSFrameBuffer %s: switching to the display's own mode %s (was %s)\n",
1453 name.c_str(), ModeLine(mode).c_str(), ModeLine(fb->m_mode).c_str());
1454 ✗ fb->m_mode = mode;
1455 }
1456 ✗ if (fb->m_displayEnabled) {
1457 ✗ fb->RestartDisplayLocked();
1458 }
1459 // Otherwise it isn't lit yet, and EnableDisplay() or SyncDisplay()
1460 // will light it in m_mode.
1461 }
1462 ✗ }
1463
1464 ✗ void KMSFrameBuffer::RestartDisplayLocked() {
1465 ✗ if (!DrmCards::EnsureMaster(m_cardFd)) {
1466 ✗ LogWarn(VB_CHANNELOUT, "KMSFrameBuffer %s: could not get DRM master to restart the display\n", m_connectorName.c_str());
1467 ✗ return;
1468 }
1469 ✗ if (m_flipPending) {
1470 ✗ int vr = m_mode.vrefresh > 0 ? m_mode.vrefresh : 60;
1471 ✗ WaitForPendingFlip((1000 / vr) + 20);
1472 }
1473 // Off first. SetCrtc with the mode the CRTC already runs is not a modeset
1474 // to the atomic helpers - it just swaps the framebuffer - and it is the
1475 // modeset that restarts the HDMI link the sink has to lock onto.
1476 ✗ if (m_planeId) {
1477 ✗ drmModeSetPlane(m_cardFd, m_planeId, m_crtcId, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1478 }
1479 ✗ drmModeSetCrtc(m_cardFd, m_crtcId, 0, 0, 0, nullptr, 0, nullptr);
1480
1481 ✗ int ret = ModesetLocked();
1482 ✗ if (ret == 0) {
1483 ✗ LogInfo(VB_CHANNELOUT, "KMSFrameBuffer %s: display restarted at %dx%d @ %dHz\n",
1484 m_connectorName.c_str(), m_mode.hdisplay, m_mode.vdisplay, m_mode.vrefresh);
1485 } else {
1486 // Leave it disabled so the next SyncDisplay() tries the modeset again.
1487 ✗ LogWarn(VB_CHANNELOUT, "KMSFrameBuffer %s: display restart failed: %d (%s)\n",
1488 m_connectorName.c_str(), ret, strerror(errno));
1489 }
1490 ✗ m_displayEnabled = (ret == 0);
1491 // The modeset restarts the vblank stream; don't count it as a stutter.
1492 ✗ m_lastFlipSeq = 0;
1493 ✗ m_prevVblankDelta = 0;
1494 ✗ m_lastFlipTimeUS = 0;
1495 }
1496
1497 #endif
1498