Skip to content

Image (Windows): a Pixels frame recreates its bitmap and is copied twice, which costs more CPU than decoding it #863

Closed

Description

@houko

Context

ImageSource.Pixels (#107) lets an app show its own decoded frames. On Windows each new frame goes through imageSlot.showPixels (internal/platform/windows/image.go:111): it releases the slot's bitmap (releaseAll(&s.bitmap)), converts the whole image.RGBA to premultiplied BGRA with imagedecode.FromPixels, and the Direct2D bitmap is created again when it is drawn (:302-303). Before that, core copies the buffer once more (driver.CopyPixels in core/image.go:36). So every frame pays two full copies, a conversion and a GPU bitmap allocation.

Why it matters

nsgo-pet plays its clips on Windows through this path (nsgo-dev/nsgo-pet#13). Measured on a Windows 11 VM with cumulative CPU time over 20 s windows, a 640x360 clip at 24 fps takes about 30-38% of one core, while decoding the same frames with libvpx accounts for only about 4% of a core (1.7 ms per frame). Smaller pets take 7-30%. Most of the cost is showing the frames, not making them, which matters for an app that stays on screen all day, and for any video, emulator or capture preview built on #107.

Proposal

  • Keep the bitmap while the frame size does not change, and upload a new frame into it (ID2D1Bitmap::CopyFromMemory) instead of creating a bitmap per frame.
  • Convert RGBA to premultiplied BGRA once, straight into the upload buffer, rather than copying in core and converting again in the platform.
  • Optionally accept frames already in premultiplied BGRA so a decoder can write them directly.

Acceptance

  • Showing a 640x360 Pixels source at 24 fps on Windows costs a small fraction of the current CPU time, with no blank or flicker between frames.
  • A size change still works.

Activity

  1. self-assigned this
    on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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