Repository navigation
fix(apple): cache DrawImageFrom crops and keep more tints of small images - #2817
Merged
Merged
Conversation
…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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On macOS and iOS,
DrawImageFromcut a new CGImage for its rectangle withCGImageCreateWithImageInRectat 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.DrawImageFromon a loaded image now goes through the per-image crop cacheDrawAtlasalready 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.maxCropsPerImage,maxCropPixelsandcropPixels.release()still frees every tint and crop.loadedImage(offscreen drawings) still crop per draw. No other per-draw crop exists ininternal/platform/apple;capture.gocrops once per screen capture.Fixes #2814
Targets
Testing
New tests in
internal/platform/apple/render_test.go, countingCGImageCreateWithImageInRectcalls 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, andrelease()gives every pixel back.Before the fix (on main, fd02adf):
After: all pass, along with the #2807 tests (
TestRenderDrawingAlternatingTintsAndCrops,TestDrawingRendererReleasesTintsAndCrops,TestLoadedImageTintCache) andTestAtlasFastMatchesCoreGraphics.BenchmarkPlaySpriteFramesplays 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:Screenshots
None
Verification
11ea23edfd02adf8go tool nsgo test ./internal/platform/apple/ ./nsgotest/→ 0 (522 passed, 0 failed, 3 skipped)go tool nsgo check -target macos,ios→ 0passChecklist
changelog.d/fragment records the fix; no docs describe the caches.