Skip to content

Commit

Permalink
Merge pull request #1576 from shogo4405/feature/stabilization
Browse files Browse the repository at this point in the history
Optimize ScreenObject CPU usage.
  • Loading branch information
shogo4405 authored Sep 22, 2024
2 parents 2c7c328 + 8eddc35 commit bcf128d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
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

0 comments on commit bcf128d

Please sign in to comment.