Entity input: improved animation cache and rendering

This commit is contained in:
Ali
2022-06-24 02:06:02 +01:00
parent 0f1b382265
commit c112bc5146
37 changed files with 2557 additions and 383 deletions

View File

@@ -6,18 +6,23 @@ import RLottieBinding
import GZip
public func cacheLottieAnimation(data: Data, width: Int, height: Int, writer: AnimationCacheItemWriter) {
let decompressedData = TGGUnzipData(data, 512 * 1024) ?? data
guard let animation = LottieInstance(data: decompressedData, fitzModifier: .none, colorReplacements: nil, cacheKey: "") else {
writer.queue.async {
let decompressedData = TGGUnzipData(data, 1 * 1024 * 1024) ?? data
guard let animation = LottieInstance(data: decompressedData, fitzModifier: .none, colorReplacements: nil, cacheKey: "") else {
writer.finish()
return
}
let frameDuration = 1.0 / Double(animation.frameRate)
for i in 0 ..< animation.frameCount {
if writer.isCancelled {
break
}
writer.add(with: { surface in
animation.renderFrame(with: i, into: surface.argb, width: Int32(surface.width), height: Int32(surface.height), bytesPerRow: Int32(surface.bytesPerRow))
}, proposedWidth: width, proposedHeight: height, duration: frameDuration)
}
writer.finish()
return
}
let size = CGSize(width: width, height: height)
let context = DrawingContext(size: size, scale: 1.0, opaque: false, clear: true)
let frameDuration = 1.0 / Double(animation.frameRate)
for i in 0 ..< animation.frameCount {
animation.renderFrame(with: i, into: context.bytes.assumingMemoryBound(to: UInt8.self), width: Int32(context.scaledSize.width), height: Int32(context.scaledSize.height), bytesPerRow: Int32(context.bytesPerRow))
writer.add(bytes: context.bytes, length: context.length, width: Int(context.scaledSize.width), height: Int(context.scaledSize.height), bytesPerRow: Int(context.bytesPerRow), duration: frameDuration)
}
writer.finish()
}