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

implementation that supports video effects for MTHKView. #1548

Merged
merged 1 commit into from
Aug 27, 2024
Merged
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
23 changes: 22 additions & 1 deletion Sources/View/MTHKView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class MTHKView: MTKView {
return device?.makeCommandQueue()
}()
private var context: CIContext?
private var effects: [any VideoEffect] = .init()

/// Initializes and returns a newly allocated view object with the specified frame rectangle.
public init(frame: CGRect) {
Expand Down Expand Up @@ -86,7 +87,9 @@ public class MTHKView: MTKView {
}
let bounds = CGRect(origin: .zero, size: drawableSize)
var scaledImage: CIImage = displayImage

for effect in effects {
scaledImage = effect.execute(scaledImage)
}
scaledImage = scaledImage
.transformed(by: CGAffineTransform(translationX: translationX, y: translationY))
.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY))
Expand All @@ -95,6 +98,24 @@ public class MTHKView: MTKView {
commandBuffer.present(currentDrawable)
commandBuffer.commit()
}

/// Registers a video effect.
public func registerVideoEffect(_ effect: some VideoEffect) -> Bool {
if effects.contains(where: { $0 === effect }) {
return false
}
effects.append(effect)
return true
}

/// Unregisters a video effect.
public func unregisterVideoEffect(_ effect: some VideoEffect) -> Bool {
if let index = effects.firstIndex(where: { $0 === effect }) {
effects.remove(at: index)
return true
}
return false
}
}

extension MTHKView: MediaMixerOutput {
Expand Down