Optimize pattern wallpapers rendering

This commit is contained in:
Ilya Laktyushin
2021-09-02 04:26:06 +03:00
parent 0ffc9d7b98
commit 74092d28e9
9 changed files with 573 additions and 18 deletions

View File

@@ -18,6 +18,7 @@ import WallpaperResources
import GZip
import TelegramUniversalVideoContent
import GradientBackground
import Svg
public func fetchCachedResourceRepresentation(account: Account, resource: MediaResource, representation: CachedMediaResourceRepresentation) -> Signal<CachedMediaResourceRepresentationResult, NoError> {
if let representation = representation as? CachedStickerAJpegRepresentation {
@@ -122,6 +123,14 @@ public func fetchCachedResourceRepresentation(account: Account, resource: MediaR
return fetchMapSnapshotResource(resource: resource)
} else if let resource = resource as? YoutubeEmbedStoryboardMediaResource, let _ = representation as? YoutubeEmbedStoryboardMediaResourceRepresentation {
return fetchYoutubeEmbedStoryboardResource(resource: resource)
} else if let representation = representation as? CachedPreparedPatternWallpaperRepresentation {
return account.postbox.mediaBox.resourceData(resource, option: .complete(waitUntilFetchStatus: false))
|> mapToSignal { data -> Signal<CachedMediaResourceRepresentationResult, NoError> in
if !data.complete {
return .complete()
}
return fetchPreparedPatternWallpaperRepresentation(resource: resource, resourceData: data, representation: representation)
}
}
return .never()
}
@@ -719,3 +728,18 @@ private func fetchAnimatedStickerRepresentation(account: Account, resource: Medi
|> runOn(Queue.concurrentDefaultQueue())
}
private func fetchPreparedPatternWallpaperRepresentation(resource: MediaResource, resourceData: MediaResourceData, representation: CachedPreparedPatternWallpaperRepresentation) -> Signal<CachedMediaResourceRepresentationResult, NoError> {
return Signal({ subscriber in
if let data = try? Data(contentsOf: URL(fileURLWithPath: resourceData.path), options: [.mappedIfSafe]) {
if let unpackedData = TGGUnzipData(data, 2 * 1024 * 1024), let data = prepareSvgImage(unpackedData) {
let path = NSTemporaryDirectory() + "\(Int64.random(in: Int64.min ... Int64.max))"
let url = URL(fileURLWithPath: path)
let _ = try? data.write(to: url)
subscriber.putNext(.temporaryPath(path))
subscriber.putCompletion()
}
}
return EmptyDisposable
}) |> runOn(Queue.concurrentDefaultQueue())
}