Skip to content

fix(platform): draw lines wider than a texture from a window of them on Windows and Linux - #50

Merged
xiaoyumuxi merged 2 commits into
mainfrom
fix/wide-label-texture-window-fork
Nov 20, 2026
Merged

xiaoyumuxi merged 2 commits into
mainfrom
fix/wide-label-texture-window-fork

Conversation

@xiaoyumuxi

Copy link
Copy Markdown

Upstream: vivaui#375 (the same commit on upstream main 54981a3c). This branch is that commit cherry-picked onto the fork's main (d354e3c8). None of the 22 files it changes differ between the two bases. On this branch: go build ./..., go test ./internal/platform/... ./ui/... ./app/... on macOS, and go vet ./internal/platform/... ./app/ for windows and linux on amd64 and arm64 all pass.

What this changes

A text label wider than the 16384 device pixels a texture holds, a monospaced or run-colored line included, is now drawn on Windows and Linux from a window of it, as macOS has drawn it since vivaui#178. A label no texture can hold is left undrawn instead of failing the frame.

  • internal/platform has the rule every renderer uses:
    • LabelTextureLimit/LabelTexturePixels name the bound the backends already used.
    • LabelTextureWidth(height) is the widest texture a label of that height gets. LabelTextureFits tests a size against it without a division, since it is asked of every label every frame.
    • WideLabelWindow is the window viva_label_window in native.m chooses: the visible columns, with as much margin on each side as they are wide, kept while a scroll stays inside the texture. WideTextLabelWindow gets the visible columns from the viewport and the label's clip.
    • ShiftGridSpans moves a grid line's ClearType spans onto the window.
    • No existing function changed, so darwin is untouched.
  • Windows:
    • labelTexture.window takes the target's width. A label one texture holds pays a LabelTextureFits test and keeps the window it had before. A wider line of any kind gets the wide window.
    • grid and gridLCD draw their pieces from -skip, the ClearType spans are planned in the line's columns and shifted onto the texture, and the per-span cut adds label.skip.
    • The Direct2D batch and the label picture key take the same window. needsRaster gathers the conditions draw and prepareLabels tested separately.
    • rasterize leaves a label without a texture instead of returning label texture exceeds bounded size.
  • Linux Vulkan: vulkanLabelTexture keeps the window (skip, wide) and a dropped flag. needsUpload rasterizes a wide line again only when the visible columns leave its texture. A label scrolled out of view, or too tall for any texture, is not rasterized again every frame. textViewport is the frame's content width.
  • Linux CPU:
    • textWindow picks the same window. The soft scene draws a wide line again only when that window moves, and placeLabel offsets the picture by skip.
    • Cairo draws the line translated by -skip. drawRuns skips runs outside the window, and of a printable ASCII run draws only the cells in it, plus one either side for an italic's overhang.

The skip is a whole number of device columns, so every glyph keeps the phase it has in the whole line.

Why

Lithe (1lck/Lithe-IDEA#2227) crashed on Windows when a language server's log was opened. The log shows lines unwrapped, and one of 4096 characters at 8 logical pixels a cell and 150% is 49152 device pixels wide. The Windows renderer rasterized a Mono or Runs label whole, so the frame returned rasterize label N: windows: label texture exceeds bounded size, and the window loop stopped drawing anything again. textServices.draw on Linux had the same bound and the same failure. vivaui#178 fixed this for Metal only.

How it was verified

  • test -z "$(gofmt -l $(git ls-files '*.go'))"

  • go vet ./... on macOS: clean apart from the existing didModifyRange: deprecation warning in native.m.

  • go test ./... -count=1 on macOS (Apple Silicon): 126 packages pass, internal/platform/darwin included. cmd/viva fails identically on 54981a3c without this change:

    • The packaging tests fail with "the program was built for macOS 13.0 or later but [mac] min_system says 12.0", from the macOS 27 SDK's deployment target.
    • TestGeneratedRecipesAreCurrent and TestInstalledRecipesMatchReferenceSource fail because componentgen reports cmd/viva/templates/select.go.txt as stale on main.
  • platform vet: GOOS=windows and GOOS=linux, GOARCH=amd64 and arm64, go vet ./internal/platform/... ./app/. GOOS=windows go build ./... for amd64 and arm64.

  • Windows: run on a real Windows 11 machine (AMD64, NT 10.0.26300, D3D11 hardware device, Go 1.26.3) over SSH.

    • The new tests pass.
    • The whole internal/platform/windows package: 246 pass. The same 25 fail before and after this change: Backdrop, Resize, Redraw, FileDrop, SetTitle and similar tests, which need the interactive desktop an SSH session does not have.
    • On 54981a3c, TestWideMonoLineIsDrawnFromAWindowOfIt and TestLabelNoTextureHoldsDoesNotFailTheFrame, written against the API there, fail with rasterize label 0: windows: label texture exceeds bounded size.
  • Linux: a native arm64 Ubuntu 24.04 container, with Mesa lavapipe for Vulkan and Pango/Cairo for the CPU renderer.

    • The new tests pass on both renderers.
    • The whole internal/platform/linux package: 390 pass before and 394 after; the 4 extra are the new tests. One test fails in both, TestEnginePassesTheTextEngineContract, on the container's fonts.
    • TestSetTitle* were skipped in both runs: they dereference a nil X connection without a display.
    • The new Linux tests use the window and do not build on 54981a3c. There, textServices.draw returns linux: label texture exceeds bounded size for the same line.
  • benchmarks before and after. Base and change were interleaved: 5 rounds, -benchtime 500ms. Median ns/op. allocs/op are the same before and after, except PageSwitch, whose allocations vary from run to run on both sides (batched 1455–1461 before and 1452–1478 after, per-label 1237–1419 before and 1339–1421 after):

    Windows, Ryzen 7 9800X3D before after
    RendererUnchangedFrame 1.0 1.0
    LabelRasterize/latin 41555 39278
    LabelRasterize/cjk 42854 40867
    GridRasterize/grayscale 149899 147830
    GridRasterize/cleartype 96938 95213
    FixedPitchLine/hinted=true 27894 27105
    TerminalScreenUnchanged 945745 921968
    ScrollingTerminalRows/identity 336504 338249
    ScrollingTerminalRows/positional 9082821 8906632
    PageSwitch/batched 3388763 3359405
    PageSwitch/per-label 6969658 7156273
    LabelPicturePageSwitch/cache=true/warm=true 13738 13960
    LabelPicturePageSwitch/cache=false/warm=false 9047556 9119502
    Linux arm64 container before after
    RasterizeSentence/grayscale 30395 28711
    RasterizeSentence/lcd 38848 38062
    GridRasterize/grayscale 611531 616730
    GridRasterize/lcd 633627 630513

    All rows are within the runs' spread. A first version computed the wide window for every label every frame and cost LabelPicturePageSwitch/cache=true/warm=true 8%. The ordinary label's early test (LabelTextureFits, no division) brought that back.

  • stripped binary size: not measured. Nothing new links; the change is internal code.

  • a real window: no interactive window. The renderers were driven offscreen on the machines above.

New tests:

  • internal/platform:
    • TestLabelTextureWidthKeepsTheTextureBounded also checks that LabelTextureFits agrees with LabelTextureWidth at every edge.
    • TestLabelVisibleColumnsCutsTheClipToTheLabel, TestWideLabelWindowKeepsTheVisibleColumnsWithAMargin, TestWideTextLabelWindowFollowsTheViewportAndTheClip and TestShiftGridSpansMovesTheColumnsOntoTheWindow cover the rule.
  • Windows:
    • TestWideLabelWindowFollowsTheViewport: a scroll inside the texture keeps it, and one past it windows again.
    • TestWideMonoLineWindowHoldsThePixelsOfTheLine: a window's rasterized pixels are byte for byte the pixels of the start of the line at the same columns. This covers ClearType on and off, without and with runs, unscrolled and 1000 characters scrolled.
    • TestWideMonoLineIsDrawnFromAWindowOfIt: the same through the whole renderer.
    • TestLabelNoTextureHoldsDoesNotFailTheFrame.
  • Linux:
    • TestVulkanWideLabelWindowFollowsTheViewport.
    • TestWideMonoLineIsDrawnFromAWindowOfIt, run on Vulkan and CPU, opaque and transparent, without and with runs.
    • TestSoftSceneDrawsAWideLineFromWhatTheCanvasShows: after each scroll, the live canvas is byte for byte a fresh paint.
    • TestLabelNoTextureHoldsDoesNotFailTheFrame.

On Windows, TestWideMonoLineIsDrawnFromAWindowOfIt allows one level per channel and the Linux one does not:

  • The rasterized textures are identical, which TestWideMonoLineWindowHoldsThePixelsOfTheLine checks exactly.
  • The D3D11 text sampler is MIN_MAG_MIP_LINEAR. A quad that starts left of the target reaches the same texel centres through a different interpolation, and the hardware's 8-bit filter weights can mix 1/256 of a neighbouring texel in.
  • That already happens upstream for any label texture that starts off-screen. A glyph moved by a device pixel differs by far more than one level.

Visual evidence

  • Changed: a monospaced line wider than 16384 device pixels, such as an unwrapped log line of 4096 characters at 150%. Before, the window stopped drawing on Windows and Linux. Now the line is drawn and scrolls.
  • Reach it by: a Text with Mono and a 4096-character value, in a horizontally scrolling area at 150%.
  • Evidence: the pixel comparisons above. There is no screenshot, because the tests compare against the start of the same line, which one texture holds.
  • On: Windows 11 at 150%, D3D11 offscreen. Linux at 150%, lavapipe and CPU offscreen.
  • Protected by: TestWideMonoLineIsDrawnFromAWindowOfIt (Windows and Linux) and TestWideMonoLineWindowHoldsThePixelsOfTheLine (Windows).

Where the boundary sits

Framework capability. The texture bound belongs to Viva's renderers, and any application that shows a long line reaches it. macOS already handles it in native.m, and this gives Windows and Linux the same window rule from one shared function.

Cost

Nothing for a label one texture holds. It pays one LabelTextureFits comparison per frame, with no division and no allocation, and keeps the window and texture it had. The benchmarks above are unchanged within their spread.

…on Windows and Linux

A monospaced or run-colored line of thousands of characters, such as a
log line shown unwrapped, is wider than the 16384 device pixels a label
texture holds: 4096 cells of 8 pixels at 150% are 49152. The Windows and
Linux renderers rasterized such lines whole, so the frame asked for a
texture past the bound, the error came back from the frame, and the
window drew nothing again. macOS already draws these lines from a window
of them (vivaui#178).

Every renderer now picks that window by the same rule
(platform.WideTextLabelWindow, the one viva_label_window keeps in
native.m): the columns the viewport and the label's clip show, with as
much margin on each side as they are wide, and the texture is kept while
a scroll stays inside it. The skip is a whole number of device columns,
so no glyph moves. Grid lines draw their pieces and ClearType spans from
that skip, and the Direct2D batch, the label picture cache, the Vulkan
uploader and the CPU scene all key on the window.

A label no texture can hold, a line taller than one or an image larger
than one, is now left undrawn instead of failing the frame, so one such
label no longer stops the window drawing the others.

Lithe reported the crash when opening a language server's log on
Windows (1lck/Lithe-IDEA#2227).
@xiaoyumuxi
xiaoyumuxi merged commit 86ce261 into main Nov 20, 2026
17 of 21 checks passed
@1lck 1lck mentioned this pull request Nov 21, 2026
2 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant