Skip to content

fix(apple): cache DrawImageFrom crops and keep more tints of small images - #2817

Merged
houko merged 1 commit into
mainfrom
fix/apple-image-crop-cache-2814
Dec 1, 2026
Merged

houko merged 1 commit into
mainfrom
fix/apple-image-crop-cache-2814

Conversation

@houko

@houko houko commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

On macOS and iOS, DrawImageFrom cut a new CGImage for its rectangle with CGImageCreateWithImageInRect at every draw, and an image kept only its last 4 tints, so an animated sprite allocated a CGImage per draw and an effect drawn in more than 4 colors re-tinted its whole bitmap at every draw.

  • DrawImageFrom on a loaded image now goes through the per-image crop cache DrawAtlas already used (loadedImage.crop, renamed from the atlas-only fields), keyed by the rectangle in the image's points and the tint. A tinted crop is cut from the cached tint, and the tint is looked up at every draw so tints with cached crops stay most recently used.
  • When a tint leaves the tint cache, its crops are dropped too, so crops never keep evicted tints alive; crops stay within the existing per-image count (1024) and global pixel budget (8M pixels), now named maxCropsPerImage, maxCropPixels and cropPixels.
  • An image keeps as many tints as fit in 4M pixels, between 4 (the old limit, for large images) and 16 (small sprites). release() still frees every tint and crop.
  • Sources without a loadedImage (offscreen drawings) still crop per draw. No other per-draw crop exists in internal/platform/apple; capture.go crops once per screen capture.

Fixes #2814

Targets

  • macOS
  • iOS

Testing

New tests in internal/platform/apple/render_test.go, counting CGImageCreateWithImageInRect calls through its function variable:

  • TestDrawImageFromReusesCrops: two frames of a sprite sheet plus a tinted frame, rendered three times; pixels are correct, no CGImage is cut after the first render, and after the render's cleanup every crop and tint has retain count 1 (only the test's reference) and the global crop pixel count is back to its start.
  • TestDrawImageFromManyTints: a frame drawn in 8 tints per render; pixels match each tint, and after the first render no crop is cut and the tint images are the same.
  • TestLoadedImageTintLimitAndCrops: an 8x8 image keeps 16 tints and a 1024x1024 one keeps 4, an evicted tint takes its crops and their pixels with it, and release() gives every pixel back.

Before the fix (on main, fd02adf):

✗ TestDrawImageFromReusesCrops (0s)
    render_test.go:203: frame 1 cut 3 new CGImages, want none
    render_test.go:203: frame 2 cut 3 new CGImages, want none
✗ TestDrawImageFromManyTints (0s)
    render_test.go:264: frame 1 cut 8 new CGImages, want none
    render_test.go:267: frame 1 re-tinted the image: tints 4, then 4
    render_test.go:264: frame 2 cut 8 new CGImages, want none
    render_test.go:267: frame 2 re-tinted the image: tints 4, then 4

After: all pass, along with the #2807 tests (TestRenderDrawingAlternatingTintsAndCrops, TestDrawingRendererReleasesTintsAndCrops, TestLoadedImageTintCache) and TestAtlasFastMatchesCoreGraphics.

BenchmarkPlaySpriteFrames plays 24 sprite-sheet frame draws in 8 tints per pass on a bitmap canvas and reports the CGImages cut and tinted copies made per pass. It ran on a Mac Studio (M-series, 10 cores) that was also running CI (load average about 60), so the times are noisy; the counts are exact:

ns/op crops/op tints/op B/op allocs/op
main 1,485,125 to 2,659,412 24 24 66,031 2,014
this branch 361,752 to 1,619,602 0 0 9,926 382

Screenshots

None

Verification

  • head: 11ea23ed
  • base: fd02adf8
  • commands:
    • go tool nsgo test ./internal/platform/apple/ ./nsgotest/ → 0 (522 passed, 0 failed, 3 skipped)
    • go tool nsgo check -target macos,ios → 0
  • result: pass
  • not covered: other targets, which do not use this code; the iOS device and simulator jobs (vetted only)

Checklist

  • The title is a Conventional Commit.
  • A changelog.d/ fragment records the fix; no docs describe the caches.
  • No new third-party code, assets or dependencies.

…ages

DrawImageFrom cut a new CGImage for its rectangle at every draw, and an image kept only its last 4 tints, so a sprite-sheet frame drawn every frame allocated a CGImage per draw and an effect drawn in more than 4 colors re-tinted its bitmap at every draw.

Draw sub-rectangles of loaded images through the per-image crop cache DrawAtlas already used, keyed by the rectangle and the tint; a tinted crop is cut from the cached tint, and dropping a tint from the cache drops its crops, so memory stays bounded by the tint limit and the existing crop budget. An image now keeps as many tints as fit in 4M pixels, between 4 for large images and 16 for small ones. Sources without a loadedImage, such as offscreen drawings, still crop per draw.

Fixes #2814
@github-actions github-actions Bot added size/M documentation Improvements or additions to documentation macOS iOS local-ci-pass The head commit declares that its local checks passed bug Something isn't working labels Dec 1, 2026
@houko
houko merged commit 13b8f6b into main Dec 1, 2026
27 of 43 checks passed
@houko
houko deleted the fix/apple-image-crop-cache-2814 branch December 1, 2026 01:08
houko added a commit to nsgo-dev/orbit-academy-game that referenced this pull request Dec 1, 2026
…ayer main

Move to nsgo main (8e5c1cc), which opens the main window maximized from its first frame (nsgo-dev/nsgo#2822) and brings the environment refresh (nsgo-dev/nsgo#2821) and sprite-frame crop cache (nsgo-dev/nsgo#2817) fixes, and to the game layer main (be78552), which builds against it (nsgo-dev/game#2723). Replace maximizeOnce, which waited for the first window frame to toggle maximize, with nsgo.WindowMaximized in main.go, and migrate the one numeric FlexShrink to core.Shrink.
houko added a commit to nsgo-dev/orbit-academy-game that referenced this pull request Dec 1, 2026
…ayer main (#5)

Move to nsgo main (8e5c1cc), which opens the main window maximized from its first frame (nsgo-dev/nsgo#2822) and brings the environment refresh (nsgo-dev/nsgo#2821) and sprite-frame crop cache (nsgo-dev/nsgo#2817) fixes, and to the game layer main (be78552), which builds against it (nsgo-dev/game#2723). Replace maximizeOnce, which waited for the first window frame to toggle maximize, with nsgo.WindowMaximized in main.go, and migrate the one numeric FlexShrink to core.Shrink.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation iOS local-ci-pass The head commit declares that its local checks passed macOS size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Apple canvas: DrawImageFrom creates a CGImage per draw, and the 4-colour tint cache thrashes with many effect colours

1 participant