From 829408494ddb9da4f0b3d674324529ecc4b0271b Mon Sep 17 00:00:00 2001 From: huiping_guo Date: Thu, 10 Feb 2022 17:49:04 +0900 Subject: [PATCH 1/2] =?UTF-8?q?thread=20block=E3=81=95=E3=82=8C=E3=81=9F?= =?UTF-8?q?=E3=81=A8=E3=81=8D=E3=81=AB=E3=83=A1=E3=83=A2=E3=83=AA=E3=83=BC?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E9=87=8F=E6=80=A5=E3=81=AB=E5=A2=97=E3=81=88?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Kitsunebi/AnimationView.swift | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Sources/Kitsunebi/AnimationView.swift b/Sources/Kitsunebi/AnimationView.swift index 5c75857..75a23fc 100644 --- a/Sources/Kitsunebi/AnimationView.swift +++ b/Sources/Kitsunebi/AnimationView.swift @@ -18,7 +18,7 @@ open class PlayerView: UIView { override open class var layerClass: Swift.AnyClass { return CAMetalLayer.self } - private var gpuLayer: LayerClass { self.layer as! LayerClass } + private lazy var gpuLayer: LayerClass = { fatalError("gpuLayer must be init") }() private let renderQueue: DispatchQueue = .global(qos: .userInitiated) private let commandQueue: MTLCommandQueue private let textureCache: CVMetalTextureCache @@ -60,6 +60,8 @@ open class PlayerView: UIView { super.init(frame: frame) applicationHandler.delegate = self backgroundColor = .clear + + gpuLayer = self.layer as! LayerClass gpuLayer.isOpaque = false gpuLayer.drawsAsynchronously = true gpuLayer.contentsGravity = .resizeAspectFill @@ -84,6 +86,8 @@ open class PlayerView: UIView { super.init(coder: aDecoder) applicationHandler.delegate = self backgroundColor = .clear + + gpuLayer = self.layer as! LayerClass gpuLayer.isOpaque = false gpuLayer.drawsAsynchronously = true gpuLayer.contentsGravity = .resizeAspectFill @@ -192,16 +196,13 @@ open class PlayerView: UIView { extension PlayerView: VideoEngineUpdateDelegate { internal func didOutputFrame(_ frame: Frame) { guard applicationHandler.isActive else { return } - DispatchQueue.main.async { [weak self] in - /// `gpuLayer` must access within main-thread. - guard let nextDrawable = self?.gpuLayer.nextDrawable() else { return } - self?.gpuLayer.drawableSize = frame.size - self?.renderQueue.async { [weak self] in - do { - try self?.renderImage(with: frame, to: nextDrawable) - } catch { - self?.clear(nextDrawable: nextDrawable) - } + guard let nextDrawable = gpuLayer.nextDrawable() else { return } + gpuLayer.drawableSize = frame.size + renderQueue.async { [weak self] in + do { + try self?.renderImage(with: frame, to: nextDrawable) + } catch { + self?.clear(nextDrawable: nextDrawable) } } } From 28c0d3c9678ae2d7311e02d05f21eb6677b9754a Mon Sep 17 00:00:00 2001 From: huiping_guo Date: Thu, 10 Feb 2022 22:37:44 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E3=81=BB=E3=81=8B=E3=81=AEmain=20thread?= =?UTF-8?q?=E4=BD=BF=E3=81=A3=E3=81=A6=E3=82=8B=E3=81=A8=E3=81=93=E3=82=8D?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E3=81=A8=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Kitsunebi/AnimationView.swift | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Sources/Kitsunebi/AnimationView.swift b/Sources/Kitsunebi/AnimationView.swift index 75a23fc..d3f8be8 100644 --- a/Sources/Kitsunebi/AnimationView.swift +++ b/Sources/Kitsunebi/AnimationView.swift @@ -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 @@ -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) } } @@ -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) } } }