Skip to content

CoreGraphics: Use single-buffering#343

Merged
madsmtm merged 7 commits into
masterfrom
apple-buffering
Mar 24, 2026
Merged

CoreGraphics: Use single-buffering#343
madsmtm merged 7 commits into
masterfrom
apple-buffering

Conversation

@madsmtm

@madsmtm madsmtm commented Mar 5, 2026

Copy link
Copy Markdown
Member

We currently create a new Vec each time next_buffer is called on macOS/iOS, which is inefficient, we should instead re-use the previous buffer.

Tested on:

  • macOS 15.7.3 M2
  • macOS 10.12.6 Intel
  • iOS simulator

Should help with #83.
Extracted from #329 (since that one may be more difficult to land).

@madsmtm madsmtm added enhancement New feature or request DS - CoreGraphics macOS/iOS/tvOS/watchOS/visionOS backend labels Mar 5, 2026
@madsmtm madsmtm added this to the Softbuffer v0.5 milestone Mar 5, 2026
Comment thread src/backends/cg.rs Outdated
@madsmtm madsmtm changed the title CoreGraphics: Use double-buffering CoreGraphics: Use single-buffering Mar 18, 2026
Allows us to get by with a single buffer, since it properly signals us
when the buffer is and isn't in use.
@madsmtm

madsmtm commented Mar 18, 2026

Copy link
Copy Markdown
Member Author

I found a way to get a reliable signal from CG for when QuartzCore is reading from the buffer (CGDataProvider::new_direct), so I've updated the PR to use that (which, incidentally, also means we only need a single buffer).

@nicoburns

Copy link
Copy Markdown

I found a way to get a reliable signal from CG for when QuartzCore is reading from the buffer (CGDataProvider::new_direct), so I've updated the PR to use that (which, incidentally, also means we only need a single buffer).

That seems like a big win. I was onboard with triple buffering if it was required, but that would have been very memory intensive for large surfaces.

@madsmtm madsmtm merged commit cadb780 into master Mar 24, 2026
40 checks passed
@madsmtm madsmtm deleted the apple-buffering branch March 24, 2026 15:57
flukejones added a commit to flukejones/softbuffer that referenced this pull request Jun 9, 2026
Back the CoreGraphics buffer with an IOSurface instead of CGDataProvider.
IOSurface is shared zero-copy with the compositor, avoiding the internal
copy QuartzCore did on present.

- keep single-buffer model; block on `is_in_use` in next_buffer before
  writing, since the compositor reads the surface directly (no internal
  copy to rely on)
- stride uses surface.bytes_per_row (may be padded for alignment)
- present sets CALayer.contents to the IOSurface, disabling actions to
  avoid the contents fade
- scale content to fill via kCAGravityResize + nearest-neighbour filters
- IOSurface supports Opaque|Premultiplied only; update supports_alpha_mode
  and AlphaMode platform docs accordingly
- swap deps: drop CGDataProvider/CGImage, add objc2-io-surface + CF
  Number/String/Dictionary features

Replaces the branch's earlier triple/unbounded-buffer commits; upstream
moved to single-buffering (rust-windowing#343) while rust-windowing#329 kept multi-buffering.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DS - CoreGraphics macOS/iOS/tvOS/watchOS/visionOS backend enhancement New feature or request

Development

Successfully merging this pull request may close these issues.

3 participants