From a679ac7e6e672453bf7849fb7ab9f3de3ccda7dc Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sun, 1 Nov 2026 00:59:06 +0100 Subject: [PATCH] Gesture, Ui, project: Crafter.Graphics at 3f646ec, whose key repeat fires once at the delay MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pinned library's key repeat counted the compositor's delay as missed periods: lastFireTime was the press itself and the catch-up loop fired once per period since it, so the first repeat after the delay was a burst of floor(delay / period) events — with rate 25 and delay 600 a held Backspace deleted sixteen characters at the delay mark and a held letter put fifteen copies in the field, from a hardware keyboard and from an on-screen keyboard that sends Backspace as key events; a rate above 1000 made the period zero and the loop endless. Crafter.Graphics 3f646ec lands the first repeat at the delay, keeps the period at a millisecond or more and caps the catch-up after a stalled frame; the app's own repeat timer and the frame path's TickKeyRepeats need nothing more. The pin moves from 6f471ca to the library's master of 2026-11-01, eight commits on: the swapchain sized for its present mode and frames in flight keyed by frame, the font atlas's SDF decoded in atlas pixels and glyphs that find no room given a cell at the next frame, Win32 linking and titles and a Win32 test's stand-in dropped, the library's AGENTS template, the key repeat. One contract changes with a4be6f1: once the atlas evicts, a glyph item is good for the frame it was shaped for, so item buffers must be one set per frame in flight (the library's usage contract since; its HelloUI shows it). The Canvas's layers keep Window::numFrames sets of their quad, circle and glyph buffers and fill the one at UIBuildArgs::frameIdx, where one set had served the three frames paced by frame callbacks — about 3 MB of host-visible memory in all; the descriptor heap grows to hold their slots. The atlas's contract changed with the eviction: a glyph that finds no free cell draws blank and gets one when the next frame begins — a frame this app, drawing on demand, has to ask for: the layers count the glyphs drawn blank (FontAtlas::Lookup, not Resident) and Build asks for another frame while glyphs wait, until the frames in flight plus one have passed without a cell gained — a cell comes from glyphs not drawn in those frames, so a screen change needs that many before the cells it displaced come free (FrameWait in Gesture, beside the fling's frame rule, added with this, its use — restarted by every redraw request and by each frame of a fling, which changes what is drawn as a request does — and tested there). The other contracts this app relies on hold at the new commit: the disable ends the composition and clears the shown record, CaptureNextFrame writes its PNG inside the frame that records the copy, a touch's up precedes the synthesised release, the deletion queue still wants the app's drain when a window ends (DrainDeletions), libevdev stays vendored; the licence texts are unchanged. --- interfaces/ImsdMessages-Gesture.cppm | 37 ++++++++++- interfaces/ImsdMessages.Ui-App.cppm | 13 ++++ interfaces/ImsdMessages.Ui-Canvas.cppm | 89 +++++++++++++++++--------- project.cpp | 2 +- tests/Gesture/main.cpp | 8 +++ 5 files changed, 117 insertions(+), 32 deletions(-) diff --git a/interfaces/ImsdMessages-Gesture.cppm b/interfaces/ImsdMessages-Gesture.cppm index 40c9d6b..6415865 100644 --- a/interfaces/ImsdMessages-Gesture.cppm +++ b/interfaces/ImsdMessages-Gesture.cppm @@ -3,8 +3,9 @@ /* ImsdMessages:Gesture — what a finger or a mouse did, decided from raw -pointer phases, and the scroll physics of a list. Pure arithmetic over the -app's own point and box types, so the tests feed it by hand. +pointer phases, the scroll physics of a list, and the frames the on-demand +window still owes (a fling's, the font atlas's wait). Pure arithmetic over +the app's own point and box types, so the tests feed it by hand. */ export module ImsdMessages:Gesture; import std; @@ -232,4 +233,36 @@ export namespace ImsdMessages { } return HitTarget{ *id }; } + + // The frames a screen owes the font atlas. A glyph that found no cell + // drew blank and gets one when a later frame begins — from the glyphs + // not drawn in the last `patience` frames (the frames in flight plus + // one) — and a frame comes only when asked for: Again(waiting) after + // each frame asks for another while glyphs wait, until `patience` + // frames in a row gained no cell, when the screen needs more cells than + // the atlas holds. A change in the count — a gain, a screen change — + // starts the count over; Reset does on any other change of what is + // drawn (the app's every redraw request but the wait's own, and each + // frame of a fling). + struct FrameWait { + std::uint32_t patience = 4; + std::uint32_t last = 0; // the previous frame's waiting glyphs + std::uint32_t gainless = 0; // frames in a row that gained no cell + + bool Again(std::uint32_t waiting) { + if (waiting == 0) { + Reset(); + return false; + } + if (last == 0 || waiting != last) gainless = 0; + else ++gainless; + last = waiting; + return gainless < patience; + } + + void Reset() { + last = 0; + gainless = 0; + } + }; } diff --git a/interfaces/ImsdMessages.Ui-App.cppm b/interfaces/ImsdMessages.Ui-App.cppm index 5facb67..467c3bd 100644 --- a/interfaces/ImsdMessages.Ui-App.cppm +++ b/interfaces/ImsdMessages.Ui-App.cppm @@ -538,7 +538,12 @@ export namespace ImsdMessages::Ui { if (onLog) onLog(line); } + // A redraw asked for: whatever changed may have changed what is + // drawn, so the atlas wait starts over (Build's own request does not + // come through here; a fling's frames, which do not either, restart + // it in Build). void Dirty() { + frameWait.Reset(); if (frames) frames->RequestRedraw(); } @@ -608,6 +613,7 @@ export namespace ImsdMessages::Ui { const float dt = lastFrame.time_since_epoch().count() == 0 ? 0.0f : std::chrono::duration(frameTime - lastFrame).count(); lastFrame = frameTime; if (frames->Animating()) { + frameWait.Reset(); // a fling's frame changes what is drawn, as a request through Dirty does: the atlas wait starts over if (!ActiveScroll().Tick(std::min(dt, 0.05f))) frames->SetAnimating(false); } if (fresh) { @@ -637,6 +643,12 @@ export namespace ImsdMessages::Ui { overflowLogged = true; Log("a layer ran out of items this frame; the buffers are too small for this screen"); } + // A glyph that found no room in the atlas drew blank and gets a + // cell when a later frame begins — from glyphs not drawn in the + // last framesInFlight + 1 frames — and only a request brings a + // frame, frames being drawn on demand: FrameWait's rule (Gesture), + // the request not through Dirty, which would start the wait over. + if (frameWait.Again(c.GlyphsWaiting())) frames->RequestRedraw(); } ComposeState* FocusedField() { @@ -986,6 +998,7 @@ export namespace ImsdMessages::Ui { guint hideIdle = 0; // a close's Hide, deferred to the loop std::chrono::steady_clock::time_point lastFrame{}; bool overflowLogged = false; + FrameWait frameWait{ .patience = Canvas::framesInFlight + 1 }; // the frames owed to the atlas while glyphs wait for a cell std::filesystem::path screenshotPath; std::optional> downSub; std::optional> upSub; diff --git a/interfaces/ImsdMessages.Ui-Canvas.cppm b/interfaces/ImsdMessages.Ui-Canvas.cppm index d2d2ac5..8c7fa09 100644 --- a/interfaces/ImsdMessages.Ui-Canvas.cppm +++ b/interfaces/ImsdMessages.Ui-Canvas.cppm @@ -62,37 +62,48 @@ export namespace ImsdMessages::Ui { } }; - // One compositing layer: its own item buffers, one fused dispatch. + // One compositing layer: its own item buffers — one set per frame in + // flight, the frame's own filled — and one fused dispatch. A frame still + // on the GPU reads its items as it runs, and the atlas evicts glyphs: a + // later frame written into the same buffers could name cells that frame + // drew other glyphs in (Crafter.Graphics a4be6f1, the library's usage + // contract since). class Layer { public: void Create(Crafter::UIRenderer& ui, Crafter::FontAtlas& atlas, std::uint32_t quadCap, std::uint32_t circleCap, std::uint32_t glyphCap) { constexpr VkBufferUsageFlags usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; constexpr VkMemoryPropertyFlags memory = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; - quads.Create(usage, memory, quadCap); - circles.Create(usage, memory, circleCap); - glyphs.Create(usage, memory, glyphCap); - quadSlot.emplace(ui.RegisterBuffer(quads)); - circleSlot.emplace(ui.RegisterBuffer(circles)); - glyphSlot.emplace(ui.RegisterBuffer(glyphs)); + for (Items& f : frames) { + f.quads.Create(usage, memory, quadCap); + f.circles.Create(usage, memory, circleCap); + f.glyphs.Create(usage, memory, glyphCap); + f.quadSlot.emplace(ui.RegisterBuffer(f.quads)); + f.circleSlot.emplace(ui.RegisterBuffer(f.circles)); + f.glyphSlot.emplace(ui.RegisterBuffer(f.glyphs)); + } this->quadCap = quadCap; this->circleCap = circleCap; this->glyphCap = glyphCap; - buf.quads = quads.value; buf.quadCount = &quadCount; buf.quadCap = quadCap; - buf.glyphs = glyphs.value; buf.glyphCount = &glyphCount; buf.glyphCap = glyphCap; buf.atlas = &atlas; buf.renderer = &ui; } - void Begin() { + // The frame's set of buffers: everything recorded until Submit goes + // into it. + void Begin(std::uint32_t frameIdx) { + current = frameIdx; + buf.quads = frames[current].quads.value; + buf.glyphs = frames[current].glyphs.value; quadCount = 0; circleCount = 0; glyphCount = 0; clip = { 0.0f, 0.0f, 1e9f, 1e9f }; overflow = false; + glyphsWaiting = 0; } void SetClip(Box b) { clip = { b.x, b.y, b.w, b.h }; } @@ -104,7 +115,7 @@ export namespace ImsdMessages::Ui { overflow = true; return; } - quads.value[quadCount++] = Crafter::QuadItem{ b.x, b.y, b.w, b.h, c[0], c[1], c[2], c[3], radius, radius, radius, radius, outline, outlineColor[0], outlineColor[1], outlineColor[2] }; + frames[current].quads.value[quadCount++] = Crafter::QuadItem{ b.x, b.y, b.w, b.h, c[0], c[1], c[2], c[3], radius, radius, radius, radius, outline, outlineColor[0], outlineColor[1], outlineColor[2] }; } void Circle(Point center, float radius, Color c) { @@ -112,7 +123,7 @@ export namespace ImsdMessages::Ui { overflow = true; return; } - circles.value[circleCount++] = Crafter::CircleItem{ center.x, center.y, radius, 0, c[0], c[1], c[2], c[3], 0, 0, 0, 0 }; + frames[current].circles.value[circleCount++] = Crafter::CircleItem{ center.x, center.y, radius, 0, c[0], c[1], c[2], c[3], 0, 0, 0, 0 }; } // One line; returns its advance. @@ -120,26 +131,39 @@ export namespace ImsdMessages::Ui { const std::uint32_t before = glyphCount; const float advance = Crafter::DrawText(buf, text, x, baselineY, font, px, c, align); if (glyphCount == before && !text.empty() && glyphCount >= glyphCap) overflow = true; + // A glyph with ink that found no room in the atlas drew blank and + // gets a cell when the next frame begins (the library's contract): + // counted, so the frame is asked for. + for (std::size_t i = 0; i < text.size();) { + const std::uint32_t cp = DecodeUtf8(text, i); + if (const Crafter::Glyph* g = buf.atlas->Lookup(font, cp); g && g->w > 0 && !g->Resident()) ++glyphsWaiting; + } return advance; } void Submit(VkCommandBuffer cmd, Crafter::UIRenderer& ui) { - if (quadCount) quads.FlushDevice(cmd, VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); - if (circleCount) circles.FlushDevice(cmd, VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); - if (glyphCount) glyphs.FlushDevice(cmd, VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); + Items& f = frames[current]; + if (quadCount) f.quads.FlushDevice(cmd, VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); + if (circleCount) f.circles.FlushDevice(cmd, VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); + if (glyphCount) f.glyphs.FlushDevice(cmd, VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); if (!quadCount && !circleCount && !glyphCount) return; - ui.DispatchFused(cmd, { *quadSlot, quadCount, clip }, { *circleSlot, circleCount, clip }, {}, { *glyphSlot, glyphCount, clip }); + ui.DispatchFused(cmd, { *f.quadSlot, quadCount, clip }, { *f.circleSlot, circleCount, clip }, {}, { *f.glyphSlot, glyphCount, clip }); } bool Overflowed() const noexcept { return overflow; } + std::uint32_t GlyphsWaiting() const noexcept { return glyphsWaiting; } private: - Crafter::GraphicsBuffer quads; - Crafter::GraphicsBuffer circles; - Crafter::GraphicsBuffer glyphs; - std::optional quadSlot; - std::optional circleSlot; - std::optional glyphSlot; + struct Items { + Crafter::GraphicsBuffer quads; + Crafter::GraphicsBuffer circles; + Crafter::GraphicsBuffer glyphs; + std::optional quadSlot; + std::optional circleSlot; + std::optional glyphSlot; + }; + std::array frames; + std::uint32_t current = 0; std::uint32_t quadCap = 0; std::uint32_t circleCap = 0; std::uint32_t glyphCap = 0; @@ -148,6 +172,7 @@ export namespace ImsdMessages::Ui { std::uint32_t glyphCount = 0; std::array clip = { 0.0f, 0.0f, 1e9f, 1e9f }; bool overflow = false; + std::uint32_t glyphsWaiting = 0; // glyphs drawn blank this frame for want of an atlas cell Crafter::UIBuffer buf; }; @@ -198,7 +223,7 @@ export namespace ImsdMessages::Ui { window.emplace(widthDp, heightDp, title); window->SetAppId(appId); VkCommandBuffer init = window->StartInit(); - heap.Initialize(16, 32, 4); + heap.Initialize(16, 48, 4); // buffers: four layers, three buffers each, per frame in flight (36 at three frames; the rest spare) window->descriptorHeap = &heap; font.emplace(assetDir / "NotoSans-Regular.ttf"); atlas.Initialize(init); @@ -209,7 +234,7 @@ export namespace ImsdMessages::Ui { content.Create(ui, atlas, 2048, 256, 16384); chrome.Create(ui, atlas, 256, 32, 2048); overlay.Create(ui, atlas, 128, 16, 1024); - buildSub.emplace(&ui.onBuild, [this](Crafter::UIBuildArgs a) { Frame(a.cmd); }); + buildSub.emplace(&ui.onBuild, [this](Crafter::UIBuildArgs a) { Frame(a.cmd, a.frameIdx); }); wrapper.emplace([this](std::uint32_t cp, float px) { return static_cast(font->AdvanceUnits(cp)) * font->ScaleForSize(px); }); window->FinishInit(); } @@ -263,12 +288,18 @@ export namespace ImsdMessages::Ui { // True when any layer dropped items this frame (buffers too small). bool Overflowed() const noexcept { return background.Overflowed() || content.Overflowed() || chrome.Overflowed() || overlay.Overflowed(); } + // Glyphs drawn blank this frame for want of an atlas cell: they get + // one when a later frame begins, from glyphs not drawn in the last + // framesInFlight + 1 frames (Gesture's FrameWait asks for the frames). + std::uint32_t GlyphsWaiting() const noexcept { return background.GlyphsWaiting() + content.GlyphsWaiting() + chrome.GlyphsWaiting() + overlay.GlyphsWaiting(); } + static constexpr std::uint32_t framesInFlight = Crafter::Window::numFrames; + private: - void Frame(VkCommandBuffer cmd) { - background.Begin(); - content.Begin(); - chrome.Begin(); - overlay.Begin(); + void Frame(VkCommandBuffer cmd, std::uint32_t frameIdx) { + background.Begin(frameIdx); + content.Begin(frameIdx); + chrome.Begin(frameIdx); + overlay.Begin(frameIdx); background.Quad({ 0, 0, Width(), Height() }, theme.bg); if (onDraw) onDraw(cmd); background.Submit(cmd, ui); diff --git a/project.cpp b/project.cpp index 7eeb28d..b4f59c1 100644 --- a/project.cpp +++ b/project.cpp @@ -85,7 +85,7 @@ extern "C" Configuration CrafterBuildProject(std::span a // The window library. Configuration* graphics = local ? LocalProject({ .projectFile = "../Crafter/Crafter.Graphics/project.cpp", .args = depArgs }) - : GitProject({ .source = { .url = "https://forgejo.catcrafts.net/Catcrafts/Crafter.Graphics.git", .commit = "6f471ca6b1f83a7b9e0f653e1f73fa428596cb00" }, .args = depArgs }); + : GitProject({ .source = { .url = "https://forgejo.catcrafts.net/Catcrafts/Crafter.Graphics.git", .commit = "3f646ec42bee39056549098b77aceed7a00a26a4" }, .args = depArgs }); // imsd-messages-core — numbers, time, the model, word wrap, gestures, // the message field's editing, the store and the sync logic: std + diff --git a/tests/Gesture/main.cpp b/tests/Gesture/main.cpp index cc18ed4..2f13ee4 100644 --- a/tests/Gesture/main.cpp +++ b/tests/Gesture/main.cpp @@ -115,5 +115,13 @@ int main() { Check(TargetAt(frame, { 5, 85 }, 1000, keys, 100000, snapped) != TargetAt(frame, { 5, 85 }, 1000, keys, 100000, drawn), "the frame that moved another bubble under the point names it: the release matches no press"); Check(TargetAt(frame, { 5, 65 }, 1000, keys, 100000, snapped) == TargetAt(frame, { 5, 85 }, 1000, keys, 100000, drawn), "the same message, moved, still names itself"); + FrameWait wait{ .patience = 4 }; + Check(!wait.Again(0), "no glyph waits: no frame owed"); + Check(wait.Again(5) && wait.Again(5) && wait.Again(5) && wait.Again(5), "glyphs waiting: a frame is owed by the first sighting and by three gainless ones after it (the cells of the screen before come free at the third)"); + Check(!wait.Again(5), "the fourth gainless frame ends it: the screen needs more cells than the atlas holds"); + wait.Reset(); + Check(wait.Again(5) && wait.Again(3) && wait.Again(3) && wait.Again(1) && !wait.Again(0), "a gain starts the count over; none waiting ends it"); + Check(wait.Again(2) && wait.Again(7) && wait.Again(7) && wait.Again(7) && wait.Again(7) && !wait.Again(7), "a changed count (a screen change) starts the count over"); + return failures == 0 ? 0 : 1; }