-
Notifications
You must be signed in to change notification settings - Fork 58
Add RenderBoxView and RBLayer integration #711
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
Open
Kyle-Ye
wants to merge
6
commits into
main
Choose a base branch
from
feature/renderbox
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+294
−73
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
950551a
Update UIKit/AppKit display list
Kyle-Ye 0cd748d
Update RB dependency
Kyle-Ye 150b393
Add RenderBoxView
Kyle-Ye 8c68b80
Fix import issue
Kyle-Ye a2dd3b1
Add DisplayListViewDrawing
Kyle-Ye 86270dc
Update RenderBox dependency
Kyle-Ye 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
27 changes: 0 additions & 27 deletions
27
Sources/OpenSwiftUI/Integration/Hosting/AppKit/View/NSGraphicsView.swift
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
Sources/OpenSwiftUI/Integration/Hosting/AppKit/View/NSInheritedView.swift
This file was deleted.
Oops, something went wrong.
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
148 changes: 148 additions & 0 deletions
148
Sources/OpenSwiftUI/Render/DisplayList/DisplayListViewDrawing.swift
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,148 @@ | ||
| // | ||
| // DisplayListViewDrawing.swift | ||
| // OpenSwiftUI | ||
| // | ||
| // Audited for 6.5.4 | ||
| // Status: WIP | ||
| // ID: 65A81BD07F0108B0485D2E15DE104A75 (SwiftUI) | ||
|
|
||
| #if canImport(Darwin) | ||
|
|
||
| @_spi(DisplayList_ViewSystem) | ||
| import OpenSwiftUICore | ||
| import QuartzCore | ||
| import OpenRenderBoxShims | ||
|
|
||
| #if os(iOS) || os(visionOS) | ||
| import UIKit | ||
| #elseif os(macOS) | ||
| import AppKit | ||
| #endif | ||
|
|
||
| // MARK: - CGDrawingView | ||
|
|
||
| final class CGDrawingView: PlatformGraphicsView, PlatformDrawable { | ||
| var options: PlatformDrawableOptions | ||
|
|
||
| init(options: PlatformDrawableOptions) { | ||
| self.options = options | ||
| super.init(frame: .zero) | ||
| isOpaque = options.isOpaque | ||
| layer.contentsFormat = options.caLayerContentsFormat | ||
| } | ||
|
|
||
| required init?(coder: NSCoder) { | ||
| _openSwiftUIUnreachableCode() | ||
| } | ||
|
|
||
| override class var layerClass: AnyClass { | ||
| CGDrawingLayer.self | ||
| } | ||
|
|
||
| static var allowsContentsMultiplyColor: Bool { | ||
| true | ||
| } | ||
|
|
||
| func update(content: PlatformDrawableContent?, required: Bool) -> Bool { | ||
| let layer = layer as! CGDrawingLayer | ||
| if let content { | ||
| layer.content = content | ||
| } | ||
| layer.setNeedsDisplay() | ||
| return true | ||
| } | ||
|
|
||
| func makeAsyncUpdate(content: PlatformDrawableContent, required: Bool, layer: CALayer, bounds: CGRect) -> (() -> Void)? { | ||
| return nil | ||
| } | ||
|
|
||
| func setContentsScale(_ scale: CGFloat) { | ||
| layer.contentsScale = scale | ||
| } | ||
|
|
||
| func drawForTesting(in displayList: ORBDisplayList) { | ||
| var state = PlatformDrawableContent.State() | ||
| let layer = layer as! CGDrawingLayer | ||
| layer.content.draw(in: displayList, size: bounds.size, state: &state) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - CGDrawingLayer | ||
|
|
||
| private final class CGDrawingLayer: CALayer { | ||
| var content: PlatformDrawableContent = .init() | ||
| var state: PlatformDrawableContent.State = .init() | ||
|
|
||
| override func draw(in ctx: CGContext) { | ||
| content.draw( | ||
| in: ctx, | ||
| size: bounds.size, | ||
| contentsScale: contentsScale, | ||
| state: &state | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - RBDrawingView [WIP] | ||
|
|
||
| final class RBDrawingView: RenderBoxView, PlatformDrawable { | ||
| var options: PlatformDrawableOptions { | ||
| didSet { | ||
| guard oldValue != options else { | ||
| return | ||
| } | ||
| updateOptions() | ||
| } | ||
| } | ||
|
|
||
| private struct State { | ||
| var content: PlatformDrawableContent = .init() | ||
| var renderer: PlatformDrawableContent.State = .init() | ||
| } | ||
|
|
||
| @AtomicBox | ||
| private var state: RBDrawingView.State = .init() | ||
|
|
||
| init(options: PlatformDrawableOptions) { | ||
| self.options = options | ||
| super.init(frame: .zero) | ||
| updateOptions() | ||
| } | ||
|
|
||
| required init?(coder: NSCoder) { | ||
| fatalError() | ||
| } | ||
|
|
||
| private func updateOptions() { | ||
| isOpaque = options.isOpaque | ||
| options.update(rbLayer: layer) | ||
| rendersFirstFrameAsynchronously = options.rendersFirstFrameAsynchronously | ||
| } | ||
|
|
||
| static var allowsContentsMultiplyColor: Bool { | ||
| _openSwiftUIUnimplementedFailure() | ||
| } | ||
|
|
||
| func update(content: PlatformDrawableContent?, required: Bool) -> Bool { | ||
| _openSwiftUIUnimplementedFailure() | ||
|
|
||
| } | ||
|
|
||
| func makeAsyncUpdate(content: PlatformDrawableContent, required: Bool, layer: CALayer, bounds: CGRect) -> (() -> Void)? { | ||
| _openSwiftUIUnimplementedFailure() | ||
|
|
||
| } | ||
|
|
||
| func setContentsScale(_ scale: CGFloat) { | ||
| _openSwiftUIUnimplementedFailure() | ||
|
|
||
| } | ||
|
|
||
| func drawForTesting(in: ORBDisplayList) { | ||
| _openSwiftUIUnimplementedFailure() | ||
|
|
||
| } | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
89 changes: 89 additions & 0 deletions
89
Sources/OpenSwiftUI/Render/DisplayList/RenderBoxView.swift
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,89 @@ | ||
| // | ||
| // RenderBoxView.swift | ||
| // OpenSwiftUI | ||
| // | ||
| // Audited for 6.5.4 | ||
| // Status: Complete | ||
| // ID: 0CB954C9DC99A8A907C58D7882F9389E (SwiftUI) | ||
|
|
||
| #if canImport(Darwin) | ||
| import Foundation | ||
| import QuartzCore | ||
| import CoreAnimation_Private | ||
| import OpenRenderBoxShims | ||
|
|
||
| // MARK: - RenderBoxView | ||
|
|
||
| @objc | ||
| class RenderBoxView: PlatformGraphicsView { | ||
| var rendersFirstFrameAsynchronously: Bool | ||
|
|
||
| override class var layerClass: AnyClass { | ||
| RenderBoxLayer.self | ||
| } | ||
|
|
||
| override init(frame: CGRect) { | ||
| rendersFirstFrameAsynchronously = false | ||
| super.init(frame: frame) | ||
| let layer = layer | ||
| layer.delegate = self | ||
| layer.isOpaque = isOpaque | ||
| } | ||
|
|
||
| required init?(coder: NSCoder) { | ||
| rendersFirstFrameAsynchronously = false | ||
| super.init(coder: coder) | ||
| let layer = layer | ||
| layer.delegate = self | ||
| layer.isOpaque = isOpaque | ||
| } | ||
|
|
||
| deinit { | ||
| (layer as! RenderBoxLayer).waitUntilAsyncRenderingCompleted() | ||
| } | ||
|
|
||
| override var isOpaque: Bool { | ||
| get { super.isOpaque } | ||
| set { | ||
| layer.isOpaque = newValue | ||
| super.isOpaque = newValue | ||
| } | ||
| } | ||
|
|
||
| override func didMoveToWindow() { | ||
| guard let window else { return } | ||
| layer.contentsScale = window.screen.scale | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| setNeedsDisplay() | ||
| } | ||
|
|
||
| override func setNeedsDisplay() { | ||
| layer.setNeedsDisplay() | ||
| } | ||
| } | ||
|
|
||
| extension RenderBoxView: RBLayerDelegate { | ||
| func rbLayer(_ layer: RBLayer, draw inDisplayList: RBDisplayList) { | ||
| } | ||
| } | ||
|
|
||
| // MARK: - RenderBoxLayer | ||
|
|
||
| private class RenderBoxLayer: RBLayer { | ||
| override var needsSynchronousUpdate: Bool { | ||
| get { | ||
| guard super.needsSynchronousUpdate else { | ||
| return false | ||
| } | ||
| guard let delegate = delegate as? RenderBoxView, | ||
| delegate.rendersFirstFrameAsynchronously | ||
| else { | ||
| return true | ||
| } | ||
| return hasBeenCommitted | ||
| } | ||
| set { | ||
| super.needsSynchronousUpdate = newValue | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On macOS,
NSView.layercan benilunless the view is layer-backed (e.g.wantsLayer = true), so touchinglayer.contentsFormat(and laterlayer as! CGDrawingLayer) risks a crash if this view is constructed outside the platform view factory. (This same assumption aboutlayerbeing present also applies toRenderBoxView.)🤖 Was this useful? React with 👍 or 👎