Skip to content

Commit

Permalink
Merge pull request #1548 from shogo4405/feature/mthkview-effect
Browse files Browse the repository at this point in the history
implementation that supports video effects for MTHKView.
  • Loading branch information
shogo4405 authored Aug 27, 2024
2 parents c8705c5 + 4679a54 commit ff51e0e
Showing 1 changed file with 22 additions and 1 deletion.
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

0 comments on commit ff51e0e

Please sign in to comment.