Repository navigation
Fix stale Metal drawable after terminal layer realization #6057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d06295c
test: reproduce stale Metal drawable resize
austinywang de4d4cf
fix: retry terminal drawable resize after Metal layer realization
austinywang ea4d3ef
Merge remote-tracking branch 'origin/main' into issue-6041-stale-meta…
austinywang 9fdb5d2
fix: keep drawable retry within file budget
austinywang 5266355
fix: reconcile drawable when Metal layer attaches
austinywang f863c31
Merge remote-tracking branch 'origin/main' into issue-6041-stale-meta…
austinywang 5b8768a
fix: use MainActor for drawable layer refresh
austinywang c23e5db
test: tighten stale Metal drawable repro
austinywang fac6eec
Merge remote-tracking branch 'origin/main' into issue-6041-stale-meta…
austinywang 5e510d4
fix: keep drawable repro within file budget
austinywang 6509ee5
fix: count only queued drawable retries
austinywang 574040b
fix: use single drawable retry path
austinywang 4e3e61e
fix: reconcile late Metal layer realization
austinywang 90fd5fe
fix: use main actor for drawable retry
austinywang 2e51813
fix: rearm drawable retry fallback
austinywang 0b1070f
merge: resolve conflicts with main
austinywang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import AppKit | ||
| import QuartzCore | ||
| import Testing | ||
|
|
||
| #if canImport(cmux_DEV) | ||
| @testable import cmux_DEV | ||
| #elseif canImport(cmux) | ||
| @testable import cmux | ||
| #endif | ||
|
|
||
| @MainActor | ||
| @Suite | ||
| struct GhosttyDrawableSizeRetryTests { | ||
| @Test func reconcilesDrawableAfterFullSizeUpdateRunsBeforeMetalLayerRealizes() throws { | ||
| _ = NSApplication.shared | ||
|
|
||
| let initialSize = CGSize(width: 800, height: 600) | ||
| let targetSize = CGSize(width: 1296, height: 893) | ||
| let initialFrame = NSRect(origin: .zero, size: initialSize) | ||
| let terminalSurface = TerminalSurface( | ||
| tabId: UUID(), | ||
| context: GHOSTTY_SURFACE_CONTEXT_SPLIT, | ||
| configTemplate: nil, | ||
| workingDirectory: nil | ||
| ) | ||
| let hostedView = terminalSurface.hostedView | ||
| let window = NSWindow( | ||
| contentRect: initialFrame, | ||
| styleMask: [.titled, .closable], | ||
| backing: .buffered, | ||
| defer: false | ||
| ) | ||
| window.isReleasedWhenClosed = false | ||
| defer { | ||
| window.orderOut(nil) | ||
| window.close() | ||
| } | ||
|
|
||
| let contentView = try #require(window.contentView) | ||
| hostedView.frame = initialFrame | ||
| hostedView.autoresizingMask = [.width, .height] | ||
| contentView.addSubview(hostedView) | ||
|
|
||
| window.makeKeyAndOrderFront(nil) | ||
| window.displayIfNeeded() | ||
| contentView.layoutSubtreeIfNeeded() | ||
| hostedView.layoutSubtreeIfNeeded() | ||
| _ = hostedView.reconcileGeometryNow() | ||
|
|
||
| let surfaceView = try #require(findGhosttyNSView(in: hostedView)) | ||
| let initialDrawableSize = surfaceView.convertToBacking(initialFrame).size | ||
| _ = surfaceView.forceRefreshSurface() | ||
| #expect(surfaceView.layer is CAMetalLayer) | ||
| #expect(surfaceView.debugLastDrawableSizeForTesting() == initialDrawableSize) | ||
| drainDeferredSurfaceSizeRetry(on: surfaceView) | ||
| #expect(!surfaceView.debugDeferredSurfaceSizeRetryQueuedForTesting()) | ||
|
|
||
| let nonMetalLayer = CALayer() | ||
| nonMetalLayer.contentsScale = window.backingScaleFactor | ||
| surfaceView.layer = nonMetalLayer | ||
| #expect(!surfaceView.debugDeferredSurfaceSizeRetryQueuedForTesting()) | ||
|
|
||
| let targetFrame = NSRect(origin: .zero, size: targetSize) | ||
| window.setFrame(targetFrame, display: false) | ||
| hostedView.frame = targetFrame | ||
| surfaceView.frame = targetFrame | ||
| #expect(surfaceView.bounds.size == targetSize) | ||
|
|
||
| let expectedDrawableSize = surfaceView.convertToBacking(targetFrame).size | ||
| #expect(expectedDrawableSize.width > 0) | ||
| #expect(expectedDrawableSize.height > 0) | ||
| #expect(expectedDrawableSize != initialDrawableSize) | ||
|
|
||
| _ = surfaceView.debugUpdateSurfaceSizeForTesting(targetSize) | ||
|
|
||
| #expect(surfaceView.debugLastDrawableSizeForTesting() == initialDrawableSize) | ||
| #expect(surfaceView.debugDeferredSurfaceSizeRetryQueuedForTesting()) | ||
|
|
||
| let realizedLayer = try #require(surfaceView.makeBackingLayer() as? CAMetalLayer) | ||
| realizedLayer.contentsScale = window.backingScaleFactor | ||
| realizedLayer.masksToBounds = true | ||
| realizedLayer.drawableSize = initialDrawableSize | ||
| surfaceView.layer = realizedLayer | ||
|
|
||
| let deadline = Date().addingTimeInterval(0.5) | ||
| while realizedLayer.drawableSize != expectedDrawableSize && Date() < deadline { | ||
| RunLoop.current.run(until: Date().addingTimeInterval(0.01)) | ||
| } | ||
|
|
||
| #expect(realizedLayer.drawableSize == expectedDrawableSize) | ||
| } | ||
|
|
||
| private func findGhosttyNSView(in view: NSView) -> GhosttyNSView? { | ||
| if let view = view as? GhosttyNSView { | ||
| return view | ||
| } | ||
|
|
||
| for subview in view.subviews { | ||
| if let match = findGhosttyNSView(in: subview) { | ||
| return match | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| private func drainDeferredSurfaceSizeRetry(on surfaceView: GhosttyNSView) { | ||
| let deadline = Date().addingTimeInterval(0.5) | ||
| while surfaceView.debugDeferredSurfaceSizeRetryQueuedForTesting() && Date() < deadline { | ||
| RunLoop.current.run(until: Date().addingTimeInterval(0.01)) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.