Skip to content

Commit

Permalink
ほかのmain thread使ってるところ修正とコメント追加
Browse files Browse the repository at this point in the history
  • Loading branch information
huiping_guo committed Feb 10, 2022
1 parent 8294084 commit 28c0d3c
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Sources/Kitsunebi/AnimationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ open class PlayerView: UIView {
override open class var layerClass: Swift.AnyClass {
return CAMetalLayer.self
}
// self.layer main thread使う必要あるので、事前に持つことでmain thread以外のthreadでも使えるように
private lazy var gpuLayer: LayerClass = { fatalError("gpuLayer must be init") }()
private let renderQueue: DispatchQueue = .global(qos: .userInitiated)
private let commandQueue: MTLCommandQueue
Expand Down Expand Up @@ -126,12 +127,9 @@ open class PlayerView: UIView {
}

private func clear() {
DispatchQueue.main.async { [weak self] in
/// `gpuLayer` must access within main-thread.
renderQueue.async { [weak self] in
guard let nextDrawable = self?.gpuLayer.nextDrawable() else { return }
self?.renderQueue.async { [weak self] in
self?.clear(nextDrawable: nextDrawable)
}
self?.clear(nextDrawable: nextDrawable)
}
}

Expand Down Expand Up @@ -196,13 +194,14 @@ open class PlayerView: UIView {
extension PlayerView: VideoEngineUpdateDelegate {
internal func didOutputFrame(_ frame: Frame) {
guard applicationHandler.isActive else { return }
guard let nextDrawable = gpuLayer.nextDrawable() else { return }
gpuLayer.drawableSize = frame.size

renderQueue.async { [weak self] in
guard let self = self, let nextDrawable = self.gpuLayer.nextDrawable() else { return }
self.gpuLayer.drawableSize = frame.size
do {
try self?.renderImage(with: frame, to: nextDrawable)
try self.renderImage(with: frame, to: nextDrawable)
} catch {
self?.clear(nextDrawable: nextDrawable)
self.clear(nextDrawable: nextDrawable)
}
}
}
Expand Down

0 comments on commit 28c0d3c

Please sign in to comment.