Skip to content
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

Optimize ScreenObject CPU usage. #1576

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/Screen/ScreenObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ open class ScreenObject {
case bottom
}

enum BlendMode {
case normal
case alpha
}

/// The screen object container that contains this screen object
public internal(set) weak var parent: ScreenObjectContainer?

Expand Down Expand Up @@ -71,6 +76,10 @@ open class ScreenObject {
/// Specifies the alignment position along the horizontal axis.
public var horizontalAlignment: HorizontalAlignment = .left

var blendMode: BlendMode {
.alpha
}

var shouldInvalidateLayout = true

/// Creates a screen object.
Expand Down Expand Up @@ -220,6 +229,13 @@ public final class VideoTrackScreenObject: ScreenObject, ChromaKeyProcessable {
}
}

override var blendMode: ScreenObject.BlendMode {
if 0.0 < cornerRadius || chromaKeyColor != nil {
return .alpha
}
return .normal
}

private var queue: TypedBlockQueue<CMSampleBuffer>?
private var effects: [VideoEffect] = .init()

Expand Down
23 changes: 17 additions & 6 deletions Sources/Screen/ScreenRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public protocol ScreenRenderer: AnyObject {

final class ScreenRendererByCPU: ScreenRenderer {
static let noFlags = vImage_Flags(kvImageNoFlags)
static let doNotTile = vImage_Flags(kvImageDoNotTile)

var bounds: CGRect = .init(origin: .zero, size: Screen.size)

Expand Down Expand Up @@ -165,12 +166,22 @@ final class ScreenRendererByCPU: ScreenRenderer {

switch pixelFormatType {
case kCVPixelFormatType_32ARGB:
vImageAlphaBlend_ARGB8888(
&image,
&destination,
&destination,
vImage_Flags(kvImageDoNotTile)
)
switch screenObject.blendMode {
case .normal:
vImageCopyBuffer(
&image,
&destination,
4,
Self.doNotTile
)
case .alpha:
vImageAlphaBlend_ARGB8888(
&image,
&destination,
&destination,
Self.doNotTile
)
}
default:
break
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/Screen/StreamScreenObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public final class StreamScreenObject: ScreenObject, ChromaKeyProcessable {
}
}

override var blendMode: ScreenObject.BlendMode {
if 0.0 < cornerRadius || chromaKeyColor != nil {
return .alpha
}
return .normal
}

override public func makeBounds(_ size: CGSize) -> CGRect {
guard parent != nil, let image = sampleBuffer?.formatDescription?.dimensions.size else {
return super.makeBounds(size)
Expand Down