Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(windows): keep a change skipped on a past slot pending for the live one

Etienne Lescot27efde0e673b
diff --git a/electron/native/wgc-capture/src/frame_slot_clock.h b/electron/native/wgc-capture/src/frame_slot_clock.h
index fb5b58f19..f13778fef 100644
--- a/electron/native/wgc-capture/src/frame_slot_clock.h
+++ b/electron/native/wgc-capture/src/frame_slot_clock.h
@@ -53,3 +53,29 @@ private:
int64_t originHns_ = -1;
int64_t frames_ = 0;
};
+
+/**
+ * Whether a tick reads the screen back or repeats the last picture.
+ *
+ * A changed screen is read back, an unchanged one repeated (#925). A slot
+ * already in the past (FrameSlotClock::isBehind) repeats either way, and the
+ * change it skipped stays pending until a live slot reads it: WGC delivers a
+ * frame only when the screen changes, so a change seen on a past slot and
+ * followed by a still screen would otherwise never reach the video.
+ */
+class PictureReadback {
+public:
+ // `changed`: a new picture arrived this tick. True when this tick must
+ // read back, which also takes the pending change off the books.
+ bool readBackNow(bool changed, bool slotIsPast) {
+ pending_ = pending_ || changed;
+ if (!pending_ || slotIsPast) {
+ return false;
+ }
+ pending_ = false;
+ return true;
+ }
+
+private:
+ bool pending_ = false;
+};
diff --git a/electron/native/wgc-capture/src/frame_slot_clock_test.cpp b/electron/native/wgc-capture/src/frame_slot_clock_test.cpp
index a75a67b4f..89727bc44 100644
--- a/electron/native/wgc-capture/src/frame_slot_clock_test.cpp
+++ b/electron/native/wgc-capture/src/frame_slot_clock_test.cpp
@@ -51,17 +51,19 @@ struct LoadedTake {
// The writer under load on a screen that changes every tick: a readback costs
// `readbackHns`, a repeat of the last picture `repeatHns`. With
// `repeatPastSlots`, a slot the clock says is already past repeats instead of
-// reading back, as main.cpp does; without it, every tick reads back.
+// reading back, through the same PictureReadback main.cpp asks; without it,
+// every tick reads back.
LoadedTake writeLoadedFor(int fps, int64_t durationHns, int64_t readbackHns, int64_t repeatHns,
bool repeatPastSlots) {
FrameSlotClock clock(fps);
+ PictureReadback readback;
const int64_t periodHns = kSecondHns / fps;
LoadedTake take;
int64_t nowHns = 0;
int64_t pictureAtHns = 0;
while (nowHns < durationHns) {
const int64_t tickStartHns = nowHns;
- if (repeatPastSlots && clock.isBehind(tickStartHns)) {
+ if (!readback.readBackNow(true, repeatPastSlots && clock.isBehind(tickStartHns))) {
nowHns += repeatHns;
} else {
pictureAtHns = tickStartHns;
@@ -132,6 +134,18 @@ int main() {
expectEqual("live slot is not past", owed.isBehind(166'666), 0);
expectEqual("slot past once the next one is due", owed.isBehind(333'333), 1);
+ // A still screen repeats, a changed one reads back once.
+ PictureReadback readback;
+ expectEqual("still screen repeats", readback.readBackNow(false, false), 0);
+ expectEqual("changed screen reads back", readback.readBackNow(true, false), 1);
+ expectEqual("read change is not read again", readback.readBackNow(false, false), 0);
+ // A change that lands on a past slot, then a screen that stays still: WGC
+ // delivers no frame for the live slot, yet the change must reach it.
+ expectEqual("past slot repeats a changed screen", readback.readBackNow(true, true), 0);
+ expectEqual("change skipped on a past slot is read on the live one",
+ readback.readBackNow(false, false), 1);
+ expectEqual("and only once", readback.readBackNow(false, false), 0);
+
// The v2.1.0 candidate's take (#945 follow-up): 10 min at 60 fps under a
// CPU/GPU load, screen 217 ms short of the webcam and the audio at the end.
// A readback that costs 20 ms on a changing screen, against a 16.7 ms
diff --git a/electron/native/wgc-capture/src/main.cpp b/electron/native/wgc-capture/src/main.cpp
index d969c869f..545a1f9e4 100644
--- a/electron/native/wgc-capture/src/main.cpp
+++ b/electron/native/wgc-capture/src/main.cpp
@@ -1108,6 +1108,7 @@ int wmain(int argc, wchar_t* argv[]) {
};
int64_t firstFrameTimestampHns = -1;
int64_t latestFrameTimestampHns = 0;
+ PictureReadback pictureReadback;
while (!control.stopRequested && !encodeFailed) {
// One instant per tick for both clocks: read after the screen readback, the
@@ -1291,7 +1292,8 @@ int wmain(int argc, wchar_t* argv[]) {
// this struct's request: it falls back to the CPU path on
// its own when the GPU path does not fit the machine.
bool captured = false;
- if (!usesDxgiInput && (!pictureChanged || videoSlotIsPast) &&
+ if (!usesDxgiInput &&
+ !pictureReadback.readBackNow(pictureChanged, videoSlotIsPast) &&
encoder.repeatLastVideoSample(frameTimestampHns, videoSample)) {
// Nothing new to read back (#925): the static screen,
// most of a demo, costs no GPU copy and no conversion.
@@ -1299,8 +1301,8 @@ int wmain(int argc, wchar_t* argv[]) {
// screen or not: under load a readback can cost more
// than a period, and a writer that catches up with
// readbacks then never catches up at all (#945). The
- // new picture waits in latestFrameTexture for the
- // slot that is live.
+ // new picture waits in latestFrameTexture, pending,
+ // for the first slot that is live.
captured = true;
} else if (usesDxgiInput) {
captured = encoder.captureDxgiSample(