Animation debugging

This commit is contained in:
Ali 2022-02-15 23:40:28 +04:00
parent 05a0d399dd
commit 307f8a841d

View File

@ -95,6 +95,8 @@ public final class CompressedImageRenderer {
private let commandQueue: MTLCommandQueue
private var isRendering: Bool = false
public init?(sharedContext: AnimationCompressor.SharedContext) {
self.sharedContext = sharedContext
self.shared = Shared.shared
@ -114,7 +116,23 @@ public final class CompressedImageRenderer {
if metalLayer.drawableSize != drawableSize {
metalLayer.drawableSize = drawableSize
}
return metalLayer.nextDrawable()
let beginTime = CFAbsoluteTimeGetCurrent()
let drawableRequestDuration: Double
if let drawableRequestTimestamp = self.drawableRequestTimestamp {
drawableRequestDuration = beginTime - drawableRequestTimestamp
if drawableRequestDuration < 1.0 / 60.0 {
return nil
}
} else {
drawableRequestDuration = 0.0
}
self.drawableRequestTimestamp = beginTime
let result = metalLayer.nextDrawable()
let duration = CFAbsoluteTimeGetCurrent() - beginTime
if duration > 1.0 / 60.0 {
print("lag \(duration * 1000.0) ms (\(drawableRequestDuration * 1000.0) ms)")
}
return result
} else {
return nil
}
@ -323,10 +341,11 @@ public final class CompressedImageRenderer {
var storedDrawable: MTLDrawable? = drawable
commandBuffer.addScheduledHandler { _ in
autoreleasepool {
storedDrawable?.present()
storedDrawable = nil
}
}
#if targetEnvironment(simulator)
commandBuffer.addCompletedHandler { _ in
@ -384,6 +403,13 @@ public final class CompressedImageRenderer {
}
public func renderRgb(metalLayer: CALayer, width: Int, height: Int, bytesPerRow: Int, data: Data, completion: @escaping () -> Void) {
if "".isEmpty {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2.0 / 60.0, execute: {
completion()
})
return
}
self.updateRgbTexture(width: width, height: height, bytesPerRow: bytesPerRow, data: data)
guard let rgbTexture = self.rgbTexture else {
@ -420,18 +446,44 @@ public final class CompressedImageRenderer {
renderEncoder.endEncoding()
commandBuffer.present(drawable)
var storedDrawable: MTLDrawable? = drawable
commandBuffer.addScheduledHandler { _ in
autoreleasepool {
storedDrawable?.present()
storedDrawable = nil
}
}
#if targetEnvironment(simulator)
commandBuffer.addCompletedHandler { _ in
DispatchQueue.main.async {
completion()
}
}
#else
if #available(iOS 10.3, *) {
drawable.addPresentedHandler { _ in
DispatchQueue.main.async {
completion()
}
}
} else {
commandBuffer.addCompletedHandler { _ in
DispatchQueue.main.async {
completion()
}
}
}
#endif
commandBuffer.commit()
}
private func updateYuvaTextures(width: Int, height: Int, data: Data) {
if width % 2 != 0 || height % 2 != 0 {
return
}
self.compressedTextures = nil
self.outputTextures = nil
self.rgbTexture = nil
@ -502,22 +554,56 @@ public final class CompressedImageRenderer {
}
public func renderYuva(metalLayer: CALayer, width: Int, height: Int, data: Data, completion: @escaping () -> Void) {
if self.isRendering {
DispatchQueue.main.async {
completion()
}
return
}
self.isRendering = true
DispatchQueue.global().async {
autoreleasepool {
let renderStartTime = CFAbsoluteTimeGetCurrent()
var beginTime: Double = 0.0
var duration: Double = 0.0
beginTime = CFAbsoluteTimeGetCurrent()
self.updateYuvaTextures(width: width, height: height, data: data)
duration = CFAbsoluteTimeGetCurrent() - beginTime
if duration > 1.0 / 60.0 {
print("update textures lag \(duration * 1000.0)")
}
guard let yuvaTextures = self.yuvaTextures else {
DispatchQueue.main.async {
self.isRendering = false
completion()
}
return
}
beginTime = CFAbsoluteTimeGetCurrent()
guard let commandBuffer = self.commandQueue.makeCommandBuffer() else {
DispatchQueue.main.async {
self.isRendering = false
completion()
}
return
}
commandBuffer.label = "MyCommand"
let drawableSize = CGSize(width: CGFloat(yuvaTextures.width), height: CGFloat(yuvaTextures.height))
guard let drawable = self.getNextDrawable(metalLayer: metalLayer, drawableSize: drawableSize) else {
commandBuffer.commit()
DispatchQueue.main.async {
self.isRendering = false
completion()
}
return
}
@ -527,6 +613,10 @@ public final class CompressedImageRenderer {
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 0)
guard let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) else {
DispatchQueue.main.async {
self.isRendering = false
completion()
}
return
}
renderEncoder.label = "MyRenderEncoder"
@ -543,14 +633,52 @@ public final class CompressedImageRenderer {
renderEncoder.endEncoding()
commandBuffer.present(drawable)
var storedDrawable: MTLDrawable? = drawable
commandBuffer.addScheduledHandler { _ in
autoreleasepool {
storedDrawable?.present()
storedDrawable = nil
}
}
#if targetEnvironment(simulator)
commandBuffer.addCompletedHandler { _ in
DispatchQueue.main.async {
self.isRendering = false
completion()
}
}
#else
if #available(iOS 10.3, *) {
commandBuffer.addCompletedHandler { _ in
DispatchQueue.main.async {
self.isRendering = false
}
let renderDuration = CFAbsoluteTimeGetCurrent() - renderStartTime
print("render duration \(renderDuration * 1000.0) ms")
}
drawable.addPresentedHandler { _ in
DispatchQueue.main.async {
completion()
}
}
} else {
commandBuffer.addCompletedHandler { _ in
DispatchQueue.main.async {
self.isRendering = false
completion()
}
}
}
#endif
commandBuffer.commit()
duration = CFAbsoluteTimeGetCurrent() - beginTime
if duration > 1.0 / 60.0 {
print("commit lag \(duration * 1000.0)")
}
}
}
}
}