Skip to content

Pixel buffers: let an Image show an app-decoded image.RGBA every frame, under overlays (video player frames) #107

Closed

Description

@houko

Context

The roadmap already plans this as "Pixel buffers" (docs/roadmap.md:40, NSH #1561): "An Image shows an image.RGBA the app draws each frame without encoding it: the desktops copy its pixels, the other backends take a PNG made once". No issue tracks it yet. This one does, with a concrete use case and what blocks it today.

What an app can do on upstream main (3f45b10):

  • core.ImageSource (driver.ImageSource, internal/driver/mount.go:1009-1021) is a URI or encoded Data bytes. The bytes are sniffed as PNG, JPEG, GIF, WebP or SVG (internal/platform/imagedecode/imagedecode.go:156-170) and decoded asynchronously in a decode slot (docs/architecture/images.md:36). Raw pixels cannot be handed over.
  • A changed source clears the view until the new decode lands. On Apple a non-quiet load shows decoded{} first (internal/platform/apple/image.go:538-541). On Windows and Linux a changed source resets the slot, which frees its bitmap or cairo surface (internal/platform/windows/image.go:97-106, :128-130; internal/platform/linux/image.go:69-80, :94-96). Encoded frames fed through SourceFunc therefore blank between frames, and each frame also fires OnLoadStart/OnLoad and runs the fade.
  • Two sources count as the same when their Data slices start at the same address and have the same length (internal/platform/apple/image.go:402-404, internal/platform/windows/image.go:117-120, internal/platform/linux/image.go:84-87). Caches use the same key (internal/driver/mount.go:1046-1048). If an app reuses one buffer for every frame, as decoders normally do, the view never redraws. If it allocates a new buffer for each frame, it fills the caches with frames.
  • Canvas doesn't help. Drawing.DrawImage/DrawImageIn/DrawImageFrom take the same ImageSource (internal/driver/canvas.go:922-942), and canvas images "load as image views' do" and the canvas "draws nothing in its place until then" (docs/architecture/canvas.md:70).
  • Pacing is already possible: Scope.RequestAnimationFrame (core/frame.go:29) calls back at each display frame. What is missing is a cheap way to hand the view a frame at that moment.
  • The current workarounds each lose something. core.Video hands a file to the platform player, so the app no longer decodes: it loses ffmpeg's container and codec coverage and a frame-exact clock. A registered Native component could draw frames itself, but on Windows a component's child window "sits above everything the window draws" (internal/platform/windows/component.go:12-13), so it hides subtitles and controls drawn over it.

Why it matters for real apps

Kando (open source at github.com/yldm-tech/kando) is a desktop video player for macOS, Windows and Linux. It decodes video with ffmpeg into RGBA frames at most 960 px wide. It reuses one buffer and wraps it as an *image.RGBA for each frame (packages/media/frames.go:17, :136-137). It paces those frames against its own audio clock and draws subtitles, transport controls and a frosted-glass panel over the picture. Its current UI framework asks the app's decoder for the next frame, an image plus how long it shows, then paces and draws the result (apps/kando/playback.go:525). ffmpeg is a hard requirement. Users open mp4, m4v, mov, mkv, webm, avi and flv files (apps/kando/dialogs.go:12), and the platform players don't all open those (MKV, AVI and FLV on macOS). Subtitle highlighting and looping a single line need the exact timestamp of the frame on screen. On NSGo the picture has no route that is both frame-paced and drawn under NSGo views on all three desktops, and this blocks a migration.

Other apps need the same thing: emulators, camera or capture previews, remote-desktop and screen-share viewers, live plots and software renderers.

Minimal repro of the only path available today (type-checks against main). A 960x540 frame is encoded to PNG 30 times a second and shown under a text overlay. Going by the code paths above, the picture blanks between frames on every desktop, and each frame pays a PNG encode plus a full decode. If the PNG were written into a reused slice of unchanged length, the view would not update at all.

package main

import (
	"bytes"
	"image"
	"image/png"
	"time"

	"github.com/nsgo-dev/nsgo"
	"github.com/nsgo-dev/nsgo/core"
)

func main() { nsgo.Run(Root, nsgo.WindowSize(960, 540)) }

func Root(cx *core.Scope) core.Node {
	frame := image.NewRGBA(image.Rect(0, 0, 960, 540))
	src := core.NewSignalFunc(cx, core.ImageSource{}, nil)
	n := 0
	var next func()
	next = func() {
		n++
		for i := range frame.Pix { // stands in for a decoder writing the next frame
			frame.Pix[i] = byte(n * 8)
		}
		var b bytes.Buffer
		_ = png.Encode(&b, frame) // the only way in today: encode, then the backend decodes it again
		src.Set(core.ImageSource{Data: b.Bytes(), Size: core.Size{Width: 960, Height: 540}})
		cx.AfterFunc(33*time.Millisecond, next)
	}
	next()
	return core.View{Class: "flex-1 bg-black", Children: []core.Node{
		core.Image{Class: "absolute inset-0", SourceFunc: src.Get, ResizeMode: core.Contain},
		core.Text{Class: "absolute bottom-8 inset-x-0 text-center text-white text-2xl", Content: "A subtitle line over the picture"},
	}}
}

Proposal

Build the roadmap row as part of ImageSource, so Image and Canvas get it together:

type ImageSource struct {
	URI  string
	Data []byte
	// Pixels, when set, is the image itself: nothing loads or decodes. The desktops copy its pixels when the view commits; the other backends take a PNG made once per Version.
	Pixels *image.RGBA
	// Version tells frames written into the same Pixels apart: the same Pixels with a different Version is redrawn in place.
	Version uint64
	// Size, Scales, Variants as today
}

An app paces frames itself and bumps the version:

frame := image.NewRGBA(image.Rect(0, 0, w, h))
version := core.NewSignal(cx, uint64(0))
n := uint64(0)
var tick func(at time.Time)
tick = func(at time.Time) {
	if dec.WriteDue(at, frame) { // the app's decoder writes into frame.Pix when a new frame is due
		n++
		version.Set(n)
	}
	cx.RequestAnimationFrame(tick)
}
cx.RequestAnimationFrame(tick)

core.Image{ResizeMode: core.Contain, SourceFunc: func() core.ImageSource {
	return core.ImageSource{Pixels: frame, Version: version.Get()}
}}

Semantics to pin down and document in docs/architecture/images.md:

  • When Pixels or Version changes, the image is swapped in place. The previous frame stays until the new one is shown. There is no OnLoadStart/OnProgress/OnLoad, no FadeDuration, and no cache entry. ResizeMode, ObjectPosition, corners, tint and blur apply as they do for any image.
  • Ownership: state when the copy is taken, so the app knows when it may write the next frame into the same buffer, for example once the commit that carried the new Version returns, or from the next RequestAnimationFrame callback.
  • Several versions committed within one display frame coalesce to the last, and a hidden or minimized window copies nothing.
  • A Pixels image is an ordinary view in the tree, unlike a Native component, so views mounted after it draw above it on every desktop.

Acceptance

  • On macOS, Windows and Linux, an Image whose Pixels is rewritten and whose Version is bumped in every RequestAnimationFrame shows each frame with no blank frame in between, at 960x540 and 1920x1080 at 60 fps, with its cost recorded as in docs/architecture/performance.md.
  • Reusing the same *image.RGBA with a new Version redraws. The same Version does not.
  • Views mounted after the image (text, controls, a view with a backdrop blur where the backend draws one) appear above it on all three desktops.
  • Pixel frames fire no load events and no fade, and they add nothing to the image caches.
  • Drawing.DrawImageIn draws a Pixels source the same way, without the "draws nothing until it loads" gap.
  • iOS, Android and the web show a Pixels source through a PNG made once per Version, as the roadmap row says.
  • nsgotest reports the size and Version an image shows, so app tests can assert that a frame reached the view.
  • docs/architecture/images.md documents the field, the ownership rule and the per-backend copy path, and the roadmap row is closed.

Activity

  1. houko commented on Nov 17, 2026

    @houko
    CollaboratorAuthor

    Another use case for this, with one extra requirement: the frame's alpha must survive.

    Transparent animations are often shipped as VP9 WebM with an alpha channel, which no platform player in NSGo shows with its transparency (#569). The desktop pet "Big Blue Fish" (github.com/Bai-Cute/big-blue-fish-desktop-pet) is an example: 106 clips at 640x360, 24 fps, about 5 s each, decoded with a bundled ffmpeg into BGRA frames that are drawn over a transparent window. On NSGo the natural port is the app decoding with ffmpeg/libvpx and handing each frame to an Image through ImageSource.Pixels.

    For that to work:

    • A Pixels frame with non-opaque pixels must be composited with its alpha (Go's image.RGBA is alpha-premultiplied), not drawn opaque like Windows' video frames are today (internal/platform/windows/video.go:762, d2dAlphaIgnore).
    • Swapping frames at 24-60 fps must not blank between them, as the issue already asks, since a blank frame is visible as a flicker over the desktop behind a transparent window.

    Acceptance addition: a 640x360 frame whose left half is alpha 0 and right half alpha 255, drawn over a coloured view, shows the colour through the left half on every desktop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions