Skip to content

Commit a4134fa

Browse files
committed
fix(linux/xdgportal): make duplicate check less strict
Previous iteration was exhibiting minor jitter in some games with mouse movement. Also slightly refactor functions to reduce compexity.
1 parent aa9a821 commit a4134fa

1 file changed

Lines changed: 17 additions & 46 deletions

File tree

src/platform/linux/portalgrab.cpp

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,45 +1218,30 @@ namespace portal {
12181218
auto deadline = std::chrono::steady_clock::now() + timeout;
12191219
int retries = 0;
12201220

1221-
while (true) {
1221+
while (std::chrono::steady_clock::now() < deadline) {
12221222
if (!wait_for_frame(deadline)) {
1223-
if (stream_stopped.load()) {
1224-
return platf::capture_e::interrupted;
1225-
}
1226-
return platf::capture_e::timeout;
1223+
return stream_stopped.load() ? platf::capture_e::interrupted : platf::capture_e::timeout;
12271224
}
12281225

12291226
if (!pull_free_image_cb(img_out)) {
12301227
return platf::capture_e::interrupted;
12311228
}
12321229

1233-
const auto img_egl = static_cast<egl::img_descriptor_t *>(img_out.get());
1230+
auto *img_egl = static_cast<egl::img_descriptor_t *>(img_out.get());
12341231
img_egl->reset();
12351232
pipewire.fill_img(img_egl);
12361233

1237-
// Check if we got valid data (either DMA-BUF fd or memory pointer)
1238-
bool is_valid = (img_egl->sd.fds[0] >= 0 || img_egl->data != nullptr);
1239-
// Check for duplicates
1240-
bool is_dup = last_pts.has_value() && is_buffer_redundant(img_egl, last_pts);
1241-
1242-
// Valid & non-duplicate frame
1243-
if (is_valid && !is_dup) {
1244-
// Check deadline
1245-
if (std::chrono::steady_clock::now() >= deadline) {
1246-
return platf::capture_e::timeout;
1247-
}
1248-
1234+
// Check if we got valid data (either DMA-BUF fd or memory pointer), then filter duplicates
1235+
if ((img_egl->sd.fds[0] >= 0 || img_egl->data != nullptr) && !is_buffer_redundant(img_egl)) {
12491236
// Update frame metadata
1250-
update_frame_metadata(img_egl, retries);
1237+
update_metadata(img_egl, retries);
12511238
return platf::capture_e::ok;
12521239
}
12531240

12541241
// No valid frame yet, or it was a duplicate
12551242
retries++;
1256-
if (std::chrono::steady_clock::now() >= deadline) {
1257-
return platf::capture_e::timeout;
1258-
}
12591243
}
1244+
return platf::capture_e::timeout;
12601245
}
12611246

12621247
std::shared_ptr<platf::img_t> alloc_img() override {
@@ -1374,41 +1359,27 @@ namespace portal {
13741359
}
13751360

13761361
private:
1377-
static bool is_buffer_redundant(const egl::img_descriptor_t *img, const std::optional<uint64_t> &last_pts) {
1362+
bool is_buffer_redundant(const egl::img_descriptor_t *img) {
13781363
// Check for corrupted frame
1379-
if (img->pw_flags.has_value()) {
1380-
if (img->pw_flags.value() & SPA_CHUNK_FLAG_CORRUPTED) {
1381-
return true;
1382-
}
1364+
if (img->pw_flags.has_value() && (img->pw_flags.value() & SPA_CHUNK_FLAG_CORRUPTED)) {
1365+
return true;
13831366
}
13841367

13851368
// Damage & PTS checks
13861369
if (img->pts.has_value() && last_pts.has_value()) {
1387-
int64_t delta = (int64_t) img->pts.value() - (int64_t) last_pts.value();
1388-
1389-
// PTS hasn't advanced:
1390-
if (delta <= 0) {
1391-
// Case A: Compositor says "No Damage"
1392-
if (img->pw_damage.has_value() && !img->pw_damage.value()) {
1393-
BOOST_LOG(debug) << "Dropping frame: PTS delta 0 and Damage metadata confirms NO pixels changed."sv;
1394-
return true;
1395-
}
1396-
// Case B: No damage metadata available, but delta is 0
1397-
if (!img->pw_damage.has_value()) {
1398-
return true;
1399-
}
1370+
// If PTS is identical, only drop if damage metadata confirms no change
1371+
if (img->pts.value() == last_pts.value()) {
1372+
return img->pw_damage.has_value() && !img->pw_damage.value();
14001373
}
14011374
}
14021375

14031376
return false;
14041377
}
14051378

1406-
void update_frame_metadata(egl::img_descriptor_t *img_egl, int retries) {
1407-
if (img_egl->seq.has_value() && img_egl->pts.has_value()) {
1408-
last_seq = img_egl->seq.value();
1409-
last_pts = img_egl->pts.value();
1410-
}
1411-
img_egl->sequence = ++sequence;
1379+
void update_metadata(egl::img_descriptor_t *img, int retries) {
1380+
last_seq = img->seq;
1381+
last_pts = img->pts;
1382+
img->sequence = ++sequence;
14121383

14131384
if (retries > 0) {
14141385
BOOST_LOG(debug) << "Processed frame after " << retries << " redundant events."sv;

0 commit comments

Comments
 (0)