| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 3 | * 2013-2026 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 | |||
| 13 | #include "fpp-pch.h" | ||
| 14 | |||
| 15 | #include "DrmCards.h" | ||
| 16 | |||
| 17 | #include <atomic> | ||
| 18 | #include <cmath> // fabs -- CheckAspectMismatch (needed directly for NOPCH builds) | ||
| 19 | #include <chrono> | ||
| 20 | #include <condition_variable> | ||
| 21 | #include <map> | ||
| 22 | #include <mutex> | ||
| 23 | #include <set> | ||
| 24 | |||
| 25 | #include "../log.h" | ||
| 26 | #include "mediaoutput.h" | ||
| 27 | |||
| 28 | #if __has_include(<xf86drm.h>) | ||
| 29 | #include <cerrno> | ||
| 30 | #include <cstring> | ||
| 31 | #include <fcntl.h> | ||
| 32 | #include <sys/ioctl.h> | ||
| 33 | #include <unistd.h> | ||
| 34 | #include <xf86drm.h> | ||
| 35 | #include <xf86drmMode.h> | ||
| 36 | |||
| 37 | namespace DrmCards { | ||
| 38 | |||
| 39 | namespace { | ||
| 40 | struct Card { | ||
| 41 | int fd = -1; | ||
| 42 | int refCount = 0; | ||
| 43 | bool master = false; | ||
| 44 | bool masterWarned = false; | ||
| 45 | std::set<uint32_t> claimed; // driven by a framebuffer output | ||
| 46 | // Held by a live lease or a lease-less holder, counted per holder: the | ||
| 47 | // fallback in AcquireLease() hands a second holder the shared fd for a | ||
| 48 | // connector someone already holds, and with a plain set the first | ||
| 49 | // holder's release cleared the mark the second was still relying on (its | ||
| 50 | // framebuffer output then drew over the video). | ||
| 51 | std::map<uint32_t, int> leased; | ||
| 52 | // Connectors whose lease a teardown still has to release (RetireLease()). | ||
| 53 | std::map<uint32_t, int> retiring; | ||
| 54 | }; | ||
| 55 | // Guards the table and the per-card sets. Never held across a blocking DRM | ||
| 56 | // call that waits on the display. | ||
| 57 | std::mutex s_lock; | ||
| 58 | std::map<std::string, Card> s_cards; | ||
| 59 | // Notified, under s_lock, by every ReleaseLease() of a retiring lease. | ||
| 60 | std::condition_variable s_retired; | ||
| 61 | std::atomic<void (*)(int, uint32_t)> s_mediaHoldListener{ nullptr }; | ||
| 62 | |||
| 63 | // Caller holds drmMasterLock, and not s_lock: the listener asks HeldByMedia(). | ||
| 64 | 67 | void NotifyMediaHold(int fd, uint32_t connectorId) { | |
| 65 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 49 times.
|
67 | if (auto listener = s_mediaHoldListener.load()) { |
| 66 | 18 | listener(fd, connectorId); | |
| 67 | } | ||
| 68 | 67 | } | |
| 69 | |||
| 70 | 6135 | Card* CardForFdLocked(int fd) { | |
| 71 |
1/2✓ Branch 0 taken 7358 times.
✗ Branch 1 not taken.
|
7358 | for (auto& [path, card] : s_cards) { |
| 72 |
2/2✓ Branch 0 taken 6135 times.
✓ Branch 1 taken 1223 times.
|
7358 | if (card.fd == fd) { |
| 73 | 6135 | return &card; | |
| 74 | } | ||
| 75 | } | ||
| 76 | ✗ | return nullptr; | |
| 77 | } | ||
| 78 | |||
| 79 | 2991 | bool EnsureMasterLocked(const std::string& cardPath, Card& card) { | |
| 80 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 2926 times.
|
2991 | if (!card.master) { |
| 81 | 65 | card.master = ioctl(card.fd, DRM_IOCTL_SET_MASTER, 0) == 0; | |
| 82 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
65 | if (!card.master && !card.masterWarned) { |
| 83 | ✗ | LogWarn(VB_MEDIAOUT, "DRM: no DRM master on %s (%s); another process holds it\n", | |
| 84 | cardPath.c_str(), strerror(errno)); | ||
| 85 | ✗ | card.masterWarned = true; | |
| 86 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | } else if (card.master) { |
| 87 | 65 | card.masterWarned = false; | |
| 88 | } | ||
| 89 | } | ||
| 90 | 2991 | return card.master; | |
| 91 | } | ||
| 92 | |||
| 93 | // Master is held only while something here needs it: a framebuffer output | ||
| 94 | // driving the card, or a live lease, whose lessee is master only while its | ||
| 95 | // lessor is. Kept on a card fppd merely opened, it locked out every other | ||
| 96 | // DRM client there: X or a kiosk started after fppd stayed dark, and fbcon | ||
| 97 | // could no longer relight a monitor plugged in later. | ||
| 98 | 194 | void DropMasterIfIdleLocked(Card& card) { | |
| 99 |
7/8✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 117 times.
✓ Branch 3 taken 77 times.
✓ Branch 4 taken 115 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 115 times.
✓ Branch 7 taken 79 times.
|
194 | if (card.master && card.claimed.empty() && card.leased.empty()) { |
| 100 | 115 | ioctl(card.fd, DRM_IOCTL_DROP_MASTER, 0); | |
| 101 | 115 | card.master = false; | |
| 102 | } | ||
| 103 | 194 | } | |
| 104 | |||
| 105 | 222 | int PlaneType(int fd, uint32_t planeId) { | |
| 106 | 222 | int type = -1; | |
| 107 | 222 | drmModeObjectPropertiesPtr props = drmModeObjectGetProperties(fd, planeId, DRM_MODE_OBJECT_PLANE); | |
| 108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 222 times.
|
222 | if (!props) { |
| 109 | ✗ | return type; | |
| 110 | } | ||
| 111 |
4/4✓ Branch 0 taken 312 times.
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 222 times.
✓ Branch 3 taken 90 times.
|
444 | for (uint32_t i = 0; i < props->count_props && type < 0; i++) { |
| 112 | 222 | drmModePropertyPtr prop = drmModeGetProperty(fd, props->props[i]); | |
| 113 |
1/2✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
|
222 | if (prop) { |
| 114 |
1/2✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
|
222 | if (!strcmp(prop->name, "type")) { |
| 115 | 222 | type = (int)props->prop_values[i]; | |
| 116 | } | ||
| 117 | 222 | drmModeFreeProperty(prop); | |
| 118 | } | ||
| 119 | } | ||
| 120 | 222 | drmModeFreeObjectProperties(props); | |
| 121 | 222 | return type; | |
| 122 | } | ||
| 123 | } // namespace | ||
| 124 | |||
| 125 | 1042 | int Acquire(const std::string& cardPath) { | |
| 126 |
1/2✓ Branch 0 taken 1042 times.
✗ Branch 1 not taken.
|
1042 | std::lock_guard<std::mutex> lock(s_lock); |
| 127 |
1/2✓ Branch 0 taken 1042 times.
✗ Branch 1 not taken.
|
1042 | auto it = s_cards.find(cardPath); |
| 128 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1028 times.
|
1042 | if (it != s_cards.end()) { |
| 129 | 14 | it->second.refCount++; | |
| 130 | 14 | return it->second.fd; | |
| 131 | } | ||
| 132 |
1/2✓ Branch 0 taken 1028 times.
✗ Branch 1 not taken.
|
1028 | int fd = open(cardPath.c_str(), O_RDWR | O_CLOEXEC); |
| 133 |
2/2✓ Branch 0 taken 960 times.
✓ Branch 1 taken 68 times.
|
1028 | if (fd < 0) { |
| 134 | 960 | return -1; | |
| 135 | } | ||
| 136 |
1/2✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
|
68 | drmSetClientCap(fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); |
| 137 | 68 | Card card; | |
| 138 | 68 | card.fd = fd; | |
| 139 | 68 | card.refCount = 1; | |
| 140 | // A new fd on a card with no master becomes master by itself. Nothing | ||
| 141 | // is claimed or leased yet, so give it back; EnsureMaster() takes it | ||
| 142 | // when there is something to drive. | ||
| 143 | 68 | card.master = true; | |
| 144 | 68 | DropMasterIfIdleLocked(card); | |
| 145 |
2/4✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
|
68 | s_cards[cardPath] = card; |
| 146 |
1/2✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
|
68 | LogDebug(VB_MEDIAOUT, "DRM: opened %s fd=%d\n", cardPath.c_str(), fd); |
| 147 | 68 | return fd; | |
| 148 | 1042 | } | |
| 149 | |||
| 150 | 61 | void Release(const std::string& cardPath) { | |
| 151 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
61 | std::lock_guard<std::mutex> lock(s_lock); |
| 152 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
61 | auto it = s_cards.find(cardPath); |
| 153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (it == s_cards.end()) { |
| 154 | ✗ | LogWarn(VB_MEDIAOUT, "DRM: release of %s, which was never acquired\n", cardPath.c_str()); | |
| 155 | ✗ | return; | |
| 156 | } | ||
| 157 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 47 times.
|
61 | if (--it->second.refCount > 0) { |
| 158 | 14 | return; | |
| 159 | } | ||
| 160 | // Closing it drops master, revokes any lease left over, and frees every | ||
| 161 | // handle and framebuffer still open on it. | ||
| 162 |
1/2✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
|
47 | close(it->second.fd); |
| 163 |
1/2✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
|
47 | LogDebug(VB_MEDIAOUT, "DRM: closed %s\n", cardPath.c_str()); |
| 164 |
1/2✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
|
47 | s_cards.erase(it); |
| 165 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 14 times.
|
61 | } |
| 166 | |||
| 167 | 2951 | bool EnsureMaster(int fd) { | |
| 168 |
1/2✓ Branch 0 taken 2951 times.
✗ Branch 1 not taken.
|
2951 | std::lock_guard<std::mutex> lock(s_lock); |
| 169 |
1/2✓ Branch 0 taken 3561 times.
✗ Branch 1 not taken.
|
3561 | for (auto& [path, card] : s_cards) { |
| 170 |
2/2✓ Branch 0 taken 2951 times.
✓ Branch 1 taken 610 times.
|
3561 | if (card.fd == fd) { |
| 171 |
1/2✓ Branch 0 taken 2951 times.
✗ Branch 1 not taken.
|
2951 | return EnsureMasterLocked(path, card); |
| 172 | } | ||
| 173 | } | ||
| 174 | ✗ | return ioctl(fd, DRM_IOCTL_SET_MASTER, 0) == 0; | |
| 175 | 2951 | } | |
| 176 | |||
| 177 | 114 | void Claim(int fd, uint32_t objectId) { | |
| 178 |
1/2✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
|
114 | std::lock_guard<std::mutex> lock(s_lock); |
| 179 |
1/2✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
|
114 | if (Card* card = CardForFdLocked(fd)) { |
| 180 |
1/2✓ Branch 0 taken 114 times.
✗ Branch 1 not taken.
|
114 | card->claimed.insert(objectId); |
| 181 | } | ||
| 182 | 114 | } | |
| 183 | |||
| 184 | 99 | void Unclaim(int fd, uint32_t objectId) { | |
| 185 |
1/2✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
|
99 | std::lock_guard<std::mutex> lock(s_lock); |
| 186 |
1/2✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
|
99 | if (Card* card = CardForFdLocked(fd)) { |
| 187 |
1/2✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
|
99 | card->claimed.erase(objectId); |
| 188 | 99 | DropMasterIfIdleLocked(*card); | |
| 189 | } | ||
| 190 | 99 | } | |
| 191 | |||
| 192 | 41 | bool InUse(int fd, uint32_t objectId) { | |
| 193 |
1/2✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 | std::lock_guard<std::mutex> lock(s_lock); |
| 194 | 41 | Card* card = CardForFdLocked(fd); | |
| 195 |
6/10✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 41 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 39 times.
|
82 | return card && (card->claimed.count(objectId) || card->leased.count(objectId)); |
| 196 | 41 | } | |
| 197 | |||
| 198 | 5881 | bool HeldByMedia(int fd, uint32_t objectId) { | |
| 199 |
1/2✓ Branch 0 taken 5881 times.
✗ Branch 1 not taken.
|
5881 | std::lock_guard<std::mutex> lock(s_lock); |
| 200 | 5881 | Card* card = CardForFdLocked(fd); | |
| 201 |
4/6✓ Branch 0 taken 5881 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5881 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 180 times.
✓ Branch 5 taken 5701 times.
|
11762 | return card && card->leased.count(objectId); |
| 202 | 5881 | } | |
| 203 | |||
| 204 | 37 | void SetMediaHoldListener(void (*listener)(int fd, uint32_t connectorId)) { | |
| 205 | 37 | s_mediaHoldListener = listener; | |
| 206 | 37 | } | |
| 207 | |||
| 208 | 1000 | Lease AcquireLease(const std::string& cardPath, int connectorId) { | |
| 209 | 1000 | Lease lease; | |
| 210 |
1/2✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
|
1000 | lease.cardPath = cardPath; |
| 211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1000 times.
|
1000 | if (connectorId <= 0) { |
| 212 | ✗ | return lease; | |
| 213 | } | ||
| 214 | 1000 | lease.connectorId = connectorId; | |
| 215 | { | ||
| 216 | // A consumer restarted after its pipeline died asks for the connector | ||
| 217 | // within milliseconds of the stop, while the old pipeline's teardown | ||
| 218 | // thread still holds its lease. Found held, the connector got no | ||
| 219 | // CRTC and the new kmssink the card's shared fd, the one the | ||
| 220 | // framebuffer outputs flip on. Wait for that release instead; a | ||
| 221 | // teardown wedged past this still falls back as before. Before | ||
| 222 | // drmMasterLock, which the framebuffer outputs' flips take. | ||
| 223 |
1/2✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
|
1000 | std::unique_lock<std::mutex> lock(s_lock); |
| 224 | 1004 | auto retiring = [&cardPath, connectorId]() { | |
| 225 |
1/2✓ Branch 0 taken 1004 times.
✗ Branch 1 not taken.
|
1004 | auto card = s_cards.find(cardPath); |
| 226 |
5/6✓ Branch 0 taken 14 times.
✓ Branch 1 taken 990 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 9 times.
|
1004 | return card != s_cards.end() && card->second.retiring.count((uint32_t)connectorId); |
| 227 | 1000 | }; | |
| 228 |
8/10✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 998 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 999 times.
|
1004 | if (retiring() && !s_retired.wait_for(lock, std::chrono::seconds(2), [&retiring]() { return !retiring(); })) { |
| 229 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | LogWarn(VB_MEDIAOUT, "DRM: %s connector %d is still held by a teardown after 2 s\n", |
| 230 | cardPath.c_str(), connectorId); | ||
| 231 | } | ||
| 232 | 1000 | } | |
| 233 |
1/2✓ Branch 0 taken 1000 times.
✗ Branch 1 not taken.
|
1000 | int fd = Acquire(cardPath); |
| 234 |
2/2✓ Branch 0 taken 960 times.
✓ Branch 1 taken 40 times.
|
1000 | if (fd < 0) { |
| 235 |
1/2✓ Branch 0 taken 960 times.
✗ Branch 1 not taken.
|
960 | LogErr(VB_MEDIAOUT, "DRM: cannot open %s: %s\n", cardPath.c_str(), strerror(errno)); |
| 236 | 960 | return lease; | |
| 237 | } | ||
| 238 | // drmModeCreateLease, and keeping the framebuffer outputs off the card | ||
| 239 | // while it is chosen. | ||
| 240 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | std::unique_lock<std::mutex> masterLock(drmMasterLock); |
| 241 | |||
| 242 | // Master is taken under the same lock that records the lease, so an | ||
| 243 | // Unclaim() in between can't see an idle card and drop it again. | ||
| 244 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | std::unique_lock<std::mutex> lock(s_lock); |
| 245 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | Card& card = s_cards[cardPath]; |
| 246 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | EnsureMasterLocked(cardPath, card); |
| 247 | 116 | auto leasable = [&card](uint32_t id) { return !card.leased.count(id); }; | |
| 248 |
2/4✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
|
38 | auto unused = [&card](uint32_t id) { return !card.leased.count(id) && !card.claimed.count(id); }; |
| 249 | |||
| 250 | // The cached state: a real probe would read the EDID over DDC. | ||
| 251 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | drmModeConnectorPtr conn = drmModeGetConnectorCurrent(fd, connectorId); |
| 252 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | drmModeResPtr res = drmModeGetResources(fd); |
| 253 | 40 | uint32_t crtcId = 0; | |
| 254 | 40 | int crtcIndex = -1; | |
| 255 |
7/10✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 38 times.
✓ Branch 9 taken 2 times.
|
40 | if (conn && res && leasable(connectorId)) { |
| 256 | // The CRTC already driving the connector, even one a framebuffer | ||
| 257 | // output on this connector uses (it stands aside while media plays). | ||
| 258 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | if (conn->encoder_id) { |
| 259 |
2/4✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
|
38 | if (drmModeEncoderPtr enc = drmModeGetEncoder(fd, conn->encoder_id)) { |
| 260 |
4/8✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38 times.
✗ Branch 7 not taken.
|
38 | if (enc->crtc_id && leasable(enc->crtc_id)) { |
| 261 | 38 | crtcId = enc->crtc_id; | |
| 262 | } | ||
| 263 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | drmModeFreeEncoder(enc); |
| 264 | } | ||
| 265 | } | ||
| 266 | // Otherwise one that can drive it and nothing else is using. | ||
| 267 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
38 | for (int e = 0; !crtcId && e < conn->count_encoders; e++) { |
| 268 | ✗ | drmModeEncoderPtr enc = drmModeGetEncoder(fd, conn->encoders[e]); | |
| 269 | ✗ | if (!enc) { | |
| 270 | ✗ | continue; | |
| 271 | } | ||
| 272 | ✗ | for (int i = 0; i < res->count_crtcs && !crtcId; i++) { | |
| 273 | ✗ | if ((enc->possible_crtcs & (1u << i)) && unused(res->crtcs[i])) { | |
| 274 | ✗ | crtcId = res->crtcs[i]; | |
| 275 | } | ||
| 276 | } | ||
| 277 | ✗ | drmModeFreeEncoder(enc); | |
| 278 | } | ||
| 279 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 38 times.
|
91 | for (int i = 0; i < res->count_crtcs; i++) { |
| 280 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 15 times.
|
53 | if (res->crtcs[i] == crtcId) { |
| 281 | 38 | crtcIndex = i; | |
| 282 | } | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | 40 | uint32_t primary = 0, overlay = 0; | |
| 287 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 2 times.
|
40 | if (crtcIndex >= 0) { |
| 288 |
2/4✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
|
38 | if (drmModePlaneResPtr planes = drmModeGetPlaneResources(fd)) { |
| 289 |
2/2✓ Branch 0 taken 412 times.
✓ Branch 1 taken 38 times.
|
450 | for (uint32_t i = 0; i < planes->count_planes; i++) { |
| 290 | 412 | uint32_t pid = planes->planes[i]; | |
| 291 |
1/2✓ Branch 0 taken 412 times.
✗ Branch 1 not taken.
|
412 | drmModePlanePtr plane = drmModeGetPlane(fd, pid); |
| 292 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
|
412 | if (!plane) { |
| 293 | ✗ | continue; | |
| 294 | } | ||
| 295 |
2/2✓ Branch 0 taken 222 times.
✓ Branch 1 taken 190 times.
|
412 | if (plane->possible_crtcs & (1u << crtcIndex)) { |
| 296 |
1/2✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
|
222 | int type = PlaneType(fd, pid); |
| 297 | // The CRTC's own primary, which a modeset needs. | ||
| 298 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | if (!primary && type == DRM_PLANE_TYPE_PRIMARY && |
| 299 |
7/10✓ Branch 0 taken 38 times.
✓ Branch 1 taken 184 times.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 38 times.
✓ Branch 9 taken 184 times.
|
260 | plane->possible_crtcs == (1u << crtcIndex) && leasable(pid)) { |
| 300 | 38 | primary = pid; | |
| 301 |
7/10✓ Branch 0 taken 38 times.
✓ Branch 1 taken 146 times.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 38 times.
✓ Branch 9 taken 146 times.
|
184 | } else if (!overlay && type == DRM_PLANE_TYPE_OVERLAY && unused(pid)) { |
| 302 | // An overlay, not the primary, for the picture: | ||
| 303 | // fbcon may be holding the primary. | ||
| 304 | 38 | overlay = pid; | |
| 305 | } | ||
| 306 | } | ||
| 307 |
1/2✓ Branch 0 taken 412 times.
✗ Branch 1 not taken.
|
412 | drmModeFreePlane(plane); |
| 308 | } | ||
| 309 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | drmModeFreePlaneResources(planes); |
| 310 | } | ||
| 311 | } | ||
| 312 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | if (res) { |
| 313 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | drmModeFreeResources(res); |
| 314 | } | ||
| 315 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | if (conn) { |
| 316 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | drmModeFreeConnector(conn); |
| 317 | } | ||
| 318 | |||
| 319 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 2 times.
|
40 | lease.planeId = overlay ? (int)overlay : -1; |
| 320 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 2 times.
|
40 | if (crtcId) { |
| 321 | 38 | lease.objects[lease.objectCount++] = (uint32_t)connectorId; | |
| 322 | 38 | lease.objects[lease.objectCount++] = crtcId; | |
| 323 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | if (primary) { |
| 324 | 38 | lease.objects[lease.objectCount++] = primary; | |
| 325 | } | ||
| 326 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | if (overlay) { |
| 327 | 38 | lease.objects[lease.objectCount++] = overlay; | |
| 328 | } | ||
| 329 | 38 | uint32_t lessee = 0; | |
| 330 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | int lfd = drmModeCreateLease(fd, lease.objects, lease.objectCount, O_CLOEXEC, &lessee); |
| 331 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | if (lfd >= 0) { |
| 332 | 38 | lease.fd = lfd; | |
| 333 | 38 | lease.isLease = true; | |
| 334 | 38 | lease.lesseeId = lessee; | |
| 335 |
2/2✓ Branch 0 taken 152 times.
✓ Branch 1 taken 38 times.
|
190 | for (int i = 0; i < lease.objectCount; i++) { |
| 336 |
1/2✓ Branch 0 taken 152 times.
✗ Branch 1 not taken.
|
152 | card.leased[lease.objects[i]]++; |
| 337 | } | ||
| 338 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | LogDebug(VB_MEDIAOUT, "DRM: lease %u on %s: connector %d crtc %u primary %u overlay %u\n", |
| 339 | lessee, cardPath.c_str(), connectorId, crtcId, primary, overlay); | ||
| 340 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | lock.unlock(); |
| 341 |
1/2✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
|
38 | NotifyMediaHold(fd, (uint32_t)connectorId); |
| 342 | 38 | return lease; // the card's ref is held until ReleaseLease() | |
| 343 | } | ||
| 344 | ✗ | LogWarn(VB_MEDIAOUT, "DRM: lease on %s connector %d refused (%s); kmssink will share the card's fd\n", | |
| 345 | cardPath.c_str(), connectorId, strerror(errno)); | ||
| 346 | } else { | ||
| 347 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | LogWarn(VB_MEDIAOUT, "DRM: no free CRTC for %s connector %d; kmssink will share the card's fd\n", |
| 348 | cardPath.c_str(), connectorId); | ||
| 349 | } | ||
| 350 | // No lease: kmssink on the shared, master fd, as before leases. Mark the | ||
| 351 | // connector and the overlay it gets as held all the same, so a | ||
| 352 | // framebuffer output there stands aside and no one else takes the plane. | ||
| 353 | // Each holder counts once, so a release drops only its own hold and a | ||
| 354 | // mark another holder of the connector still relies on stays. | ||
| 355 | 2 | lease.objectCount = 0; | |
| 356 | 2 | lease.objects[lease.objectCount++] = (uint32_t)connectorId; | |
| 357 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | card.leased[(uint32_t)connectorId]++; |
| 358 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (overlay) { |
| 359 | ✗ | lease.objects[lease.objectCount++] = overlay; | |
| 360 | ✗ | card.leased[overlay]++; | |
| 361 | } | ||
| 362 | 2 | lease.fd = fd; | |
| 363 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | lock.unlock(); |
| 364 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | NotifyMediaHold(fd, (uint32_t)connectorId); |
| 365 | 2 | return lease; | |
| 366 | 40 | } | |
| 367 | |||
| 368 | 1054 | void RetireLease(Lease& lease) { | |
| 369 |
4/6✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1032 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
|
1054 | if (lease.fd < 0 || lease.connectorId <= 0 || lease.retired) { |
| 370 | 1032 | return; | |
| 371 | } | ||
| 372 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | std::lock_guard<std::mutex> lock(s_lock); |
| 373 |
2/4✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
|
22 | s_cards[lease.cardPath].retiring[(uint32_t)lease.connectorId]++; |
| 374 | 22 | lease.retired = true; | |
| 375 | 22 | } | |
| 376 | |||
| 377 | 1057 | void ReleaseLease(Lease& lease) { | |
| 378 |
2/2✓ Branch 0 taken 1030 times.
✓ Branch 1 taken 27 times.
|
1057 | if (lease.fd < 0) { |
| 379 | 1030 | return; | |
| 380 | } | ||
| 381 | { | ||
| 382 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | std::lock_guard<std::mutex> lock(s_lock); |
| 383 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | auto it = s_cards.find(lease.cardPath); |
| 384 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | if (it != s_cards.end()) { |
| 385 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | auto retiring = it->second.retiring.find((uint32_t)lease.connectorId); |
| 386 |
5/6✓ Branch 0 taken 20 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 7 times.
|
27 | if (lease.retired && retiring != it->second.retiring.end()) { |
| 387 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if (--retiring->second <= 0) { |
| 388 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | it->second.retiring.erase(retiring); |
| 389 | } | ||
| 390 | 20 | s_retired.notify_all(); | |
| 391 | } | ||
| 392 |
2/2✓ Branch 0 taken 102 times.
✓ Branch 1 taken 27 times.
|
129 | for (int i = 0; i < lease.objectCount; i++) { |
| 393 |
1/2✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
|
102 | auto held = it->second.leased.find(lease.objects[i]); |
| 394 |
5/6✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 100 times.
✓ Branch 5 taken 2 times.
|
102 | if (held != it->second.leased.end() && --held->second <= 0) { |
| 395 |
1/2✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
|
100 | it->second.leased.erase(held); |
| 396 | } | ||
| 397 | } | ||
| 398 | 27 | DropMasterIfIdleLocked(it->second); | |
| 399 | } | ||
| 400 | 27 | } | |
| 401 | 27 | int cardFd = -1; | |
| 402 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
|
27 | if (lease.isLease) { |
| 403 | // Revokes the lease and frees what kmssink left behind on it. | ||
| 404 | 25 | close(lease.fd); | |
| 405 | } else { | ||
| 406 | 2 | cardFd = lease.fd; | |
| 407 | } | ||
| 408 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | if (lease.connectorId > 0) { |
| 409 | // The card's fd stays open on our ref until the Release() below. | ||
| 410 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
|
27 | if (cardFd < 0) { |
| 411 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | std::lock_guard<std::mutex> lock(s_lock); |
| 412 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | auto it = s_cards.find(lease.cardPath); |
| 413 |
1/2✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
|
25 | cardFd = it != s_cards.end() ? it->second.fd : -1; |
| 414 | 25 | } | |
| 415 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | if (cardFd >= 0) { |
| 416 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | std::lock_guard<std::mutex> masterLock(drmMasterLock); |
| 417 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | NotifyMediaHold(cardFd, (uint32_t)lease.connectorId); |
| 418 | 27 | } | |
| 419 | } | ||
| 420 | 27 | Release(lease.cardPath); | |
| 421 | 27 | lease = Lease(); | |
| 422 | } | ||
| 423 | |||
| 424 | // kmssink's private gst_video_calculate_device_ratio() (gstkmsutils.c), kept | ||
| 425 | // identical: the question is what kmssink will do, not what the PAR should be. | ||
| 426 | 7 | static bool IsSquarePixelMode(uint32_t width, uint32_t height, uint32_t mmWidth, uint32_t mmHeight) { | |
| 427 | static const int parMap[][2] = { { 1, 1 }, { 16, 15 }, { 11, 10 }, { 54, 59 }, { 64, 45 }, { 5, 3 }, { 4, 3 } }; | ||
| 428 |
4/8✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7 times.
|
7 | if (width == 0 || height == 0 || mmWidth == 0 || mmHeight == 0) { |
| 429 | ✗ | return true; | |
| 430 | } | ||
| 431 | 7 | double ratio = (double)(mmWidth * height) / (double)(mmHeight * width); | |
| 432 | 99 | auto delta = [ratio](int idx, int w) { return std::fabs(ratio - (double)parMap[idx][w] / parMap[idx][!w]); }; | |
| 433 | 7 | double best = delta(0, 0); | |
| 434 | 7 | int index = 0; | |
| 435 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7 times.
|
49 | for (int i = 1; i < (int)(sizeof(parMap) / sizeof(parMap[0])); i++) { |
| 436 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 42 times.
|
126 | for (int j = 0; j < 2; j++) { |
| 437 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 76 times.
|
84 | if (delta(i, j) < best) { |
| 438 | 8 | best = delta(i, j); | |
| 439 | 8 | index = i; | |
| 440 | } | ||
| 441 | } | ||
| 442 | } | ||
| 443 | 7 | return index == 0; | |
| 444 | } | ||
| 445 | |||
| 446 | 3 | AspectMismatch CheckAspectMismatch(const std::string& cardPath, int connectorId) { | |
| 447 | 3 | AspectMismatch r; | |
| 448 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
3 | if (cardPath.empty() || connectorId <= 0) { |
| 449 | ✗ | return r; | |
| 450 | } | ||
| 451 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | int fd = Acquire(cardPath); |
| 452 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (fd < 0) { |
| 453 | ✗ | return r; | |
| 454 | } | ||
| 455 | // Cached: a probe would read the EDID over DDC on the sink's streaming thread. | ||
| 456 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | drmModeConnectorPtr conn = drmModeGetConnectorCurrent(fd, connectorId); |
| 457 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | if (conn && conn->mmWidth && conn->mmHeight) { |
| 458 | // The CRTC's mode, which kmssink read at start. With none, kmssink | ||
| 459 | // sets the mode itself and never calls SetPlane: nothing to blame. | ||
| 460 | 3 | drmModeModeInfo mode = {}; | |
| 461 | 3 | bool haveMode = false; | |
| 462 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (conn->encoder_id) { |
| 463 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | if (drmModeEncoderPtr enc = drmModeGetEncoder(fd, conn->encoder_id)) { |
| 464 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (enc->crtc_id) { |
| 465 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | if (drmModeCrtcPtr crtc = drmModeGetCrtc(fd, enc->crtc_id)) { |
| 466 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (crtc->mode_valid) { |
| 467 | 3 | mode = crtc->mode; | |
| 468 | 3 | haveMode = true; | |
| 469 | } | ||
| 470 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | drmModeFreeCrtc(crtc); |
| 471 | } | ||
| 472 | } | ||
| 473 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | drmModeFreeEncoder(enc); |
| 474 | } | ||
| 475 | } | ||
| 476 |
5/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
|
3 | if (haveMode && !IsSquarePixelMode(mode.hdisplay, mode.vdisplay, conn->mmWidth, conn->mmHeight)) { |
| 477 | 2 | r.found = true; | |
| 478 | 2 | r.mmWidth = conn->mmWidth; | |
| 479 | 2 | r.mmHeight = conn->mmHeight; | |
| 480 | 2 | r.current = { mode.hdisplay, mode.vdisplay, (int)mode.vrefresh }; | |
| 481 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | for (int i = 0; i < conn->count_modes; i++) { |
| 482 | 4 | const drmModeModeInfo& m = conn->modes[i]; | |
| 483 |
5/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2 times.
|
8 | if (!(m.flags & (DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLSCAN)) && |
| 484 | 4 | IsSquarePixelMode(m.hdisplay, m.vdisplay, conn->mmWidth, conn->mmHeight)) { | |
| 485 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | r.squareModes.push_back({ m.hdisplay, m.vdisplay, (int)m.vrefresh }); |
| 486 | } | ||
| 487 | } | ||
| 488 | } | ||
| 489 | } | ||
| 490 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (conn) { |
| 491 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | drmModeFreeConnector(conn); |
| 492 | } | ||
| 493 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | Release(cardPath); |
| 494 | 3 | return r; | |
| 495 | ✗ | } | |
| 496 | |||
| 497 | } // namespace DrmCards | ||
| 498 | |||
| 499 | #else // no libdrm (macOS) | ||
| 500 | |||
| 501 | namespace DrmCards { | ||
| 502 | int Acquire(const std::string&) { return -1; } | ||
| 503 | void Release(const std::string&) {} | ||
| 504 | bool EnsureMaster(int) { return false; } | ||
| 505 | void Claim(int, uint32_t) {} | ||
| 506 | void Unclaim(int, uint32_t) {} | ||
| 507 | bool InUse(int, uint32_t) { return false; } | ||
| 508 | bool HeldByMedia(int, uint32_t) { return false; } | ||
| 509 | void SetMediaHoldListener(void (*)(int, uint32_t)) {} | ||
| 510 | Lease AcquireLease(const std::string& cardPath, int) { | ||
| 511 | Lease l; | ||
| 512 | l.cardPath = cardPath; | ||
| 513 | return l; | ||
| 514 | } | ||
| 515 | void RetireLease(Lease&) {} | ||
| 516 | void ReleaseLease(Lease& lease) { lease = Lease(); } | ||
| 517 | AspectMismatch CheckAspectMismatch(const std::string&, int) { return AspectMismatch(); } | ||
| 518 | } // namespace DrmCards | ||
| 519 | |||
| 520 | #endif | ||
| 521 |