From 4679a5463047bbe81d0156b9bcdd490b7cea71bc Mon Sep 17 00:00:00 2001 From: shogo4405 Date: Mon, 26 Aug 2024 00:05:19 +0900 Subject: [PATCH] implementation that supports video effects for MTHKView. --- Sources/View/MTHKView.swift | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Sources/View/MTHKView.swift b/Sources/View/MTHKView.swift index 20ebcbed8..1f90390cd 100644 --- a/Sources/View/MTHKView.swift +++ b/Sources/View/MTHKView.swift @@ -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) { @@ -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)) @@ -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 {