mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
lock approach is slower on ios
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import CoreImage
|
import Accelerate
|
||||||
import CoreVideo
|
|
||||||
import Flutter
|
import Flutter
|
||||||
import MobileCoreServices
|
import MobileCoreServices
|
||||||
import Photos
|
import Photos
|
||||||
@@ -21,7 +20,7 @@ class RemoteImageApiImpl: NSObject, RemoteImageApi {
|
|||||||
private static let delegate = RemoteImageApiDelegate()
|
private static let delegate = RemoteImageApiDelegate()
|
||||||
static let session = {
|
static let session = {
|
||||||
let config = URLSessionConfiguration.default
|
let config = URLSessionConfiguration.default
|
||||||
let thumbnailPath = FileManager.default.temporaryDirectory.appendingPathComponent("thumbnails2", isDirectory: true)
|
let thumbnailPath = FileManager.default.temporaryDirectory.appendingPathComponent("thumbnails", isDirectory: true)
|
||||||
try! FileManager.default.createDirectory(at: thumbnailPath, withIntermediateDirectories: true)
|
try! FileManager.default.createDirectory(at: thumbnailPath, withIntermediateDirectories: true)
|
||||||
config.urlCache = URLCache(
|
config.urlCache = URLCache(
|
||||||
memoryCapacity: 0,
|
memoryCapacity: 0,
|
||||||
@@ -48,20 +47,27 @@ class RemoteImageApiImpl: NSObject, RemoteImageApi {
|
|||||||
|
|
||||||
func cancelRequest(requestId: Int64) {
|
func cancelRequest(requestId: Int64) {
|
||||||
Self.delegate.cancel(requestId: requestId)
|
Self.delegate.cancel(requestId: requestId)
|
||||||
Self.delegate.releasePixelBuffer(requestId: requestId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func releaseImage(requestId: Int64) {
|
func releaseImage(requestId: Int64) throws {}
|
||||||
Self.delegate.releasePixelBuffer(requestId: requestId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class RemoteImageApiDelegate: NSObject, URLSessionDataDelegate {
|
class RemoteImageApiDelegate: NSObject, URLSessionDataDelegate {
|
||||||
private static let requestQueue = DispatchQueue(label: "thumbnail.requests", qos: .userInitiated)
|
private static let requestQueue = DispatchQueue(label: "thumbnail.requests", qos: .userInitiated)
|
||||||
|
private static var rgbaFormat = vImage_CGImageFormat(
|
||||||
|
bitsPerComponent: 8,
|
||||||
|
bitsPerPixel: 32,
|
||||||
|
colorSpace: CGColorSpaceCreateDeviceRGB(),
|
||||||
|
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue),
|
||||||
|
renderingIntent: .perceptual
|
||||||
|
)!
|
||||||
private static var requests = [Int64: RemoteImageRequest]()
|
private static var requests = [Int64: RemoteImageRequest]()
|
||||||
private static var lockedPixelBuffers = [Int64: CVPixelBuffer]()
|
|
||||||
private static let cancelledResult = Result<[String: Int64], any Error>.success([:])
|
private static let cancelledResult = Result<[String: Int64], any Error>.success([:])
|
||||||
private static let ciContext = CIContext(options: [.useSoftwareRenderer: false])
|
private static let decodeOptions = [
|
||||||
|
kCGImageSourceShouldCache: false,
|
||||||
|
kCGImageSourceShouldCacheImmediately: true,
|
||||||
|
kCGImageSourceCreateThumbnailWithTransform: true,
|
||||||
|
] as CFDictionary
|
||||||
|
|
||||||
func urlSession(
|
func urlSession(
|
||||||
_ session: URLSession, dataTask: URLSessionDataTask,
|
_ session: URLSession, dataTask: URLSessionDataTask,
|
||||||
@@ -104,13 +110,11 @@ class RemoteImageApiDelegate: NSObject, URLSessionDataDelegate {
|
|||||||
defer { remove(requestId: requestId) }
|
defer { remove(requestId: requestId) }
|
||||||
|
|
||||||
if let error = error {
|
if let error = error {
|
||||||
if request.isCancelled || (error as NSError).code == NSURLErrorCancelled {
|
|
||||||
return request.completion(Self.cancelledResult)
|
|
||||||
}
|
|
||||||
return request.completion(.failure(error))
|
return request.completion(.failure(error))
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let ciImage = CIImage(data: data as Data, options: [.applyOrientationProperty: true]) else {
|
guard let imageSource = CGImageSourceCreateWithData(data, nil),
|
||||||
|
let cgImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, Self.decodeOptions) else {
|
||||||
return request.completion(.failure(PigeonError(code: "", message: "Failed to decode image for request \(requestId)", details: nil)))
|
return request.completion(.failure(PigeonError(code: "", message: "Failed to decode image for request \(requestId)", details: nil)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,52 +122,24 @@ class RemoteImageApiDelegate: NSObject, URLSessionDataDelegate {
|
|||||||
return request.completion(Self.cancelledResult)
|
return request.completion(Self.cancelledResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
let extent = ciImage.extent
|
do {
|
||||||
let width = Int(extent.width)
|
let buffer = try vImage_Buffer(cgImage: cgImage, format: Self.rgbaFormat)
|
||||||
let height = Int(extent.height)
|
|
||||||
|
|
||||||
guard width > 0 && height > 0 else {
|
if request.isCancelled {
|
||||||
return request.completion(.failure(PigeonError(code: "", message: "Invalid image dimensions \(width)x\(height) for request \(requestId)", details: nil)))
|
buffer.free()
|
||||||
|
return request.completion(Self.cancelledResult)
|
||||||
|
}
|
||||||
|
|
||||||
|
request.completion(
|
||||||
|
.success([
|
||||||
|
"pointer": Int64(Int(bitPattern: buffer.data)),
|
||||||
|
"width": Int64(buffer.width),
|
||||||
|
"height": Int64(buffer.height),
|
||||||
|
"rowBytes": Int64(buffer.rowBytes),
|
||||||
|
]))
|
||||||
|
} catch {
|
||||||
|
return request.completion(.failure(PigeonError(code: "", message: "Failed to convert image for request \(requestId): \(error)", details: nil)))
|
||||||
}
|
}
|
||||||
|
|
||||||
var pixelBuffer: CVPixelBuffer?
|
|
||||||
let attrs: [String: Any] = [
|
|
||||||
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
|
|
||||||
kCVPixelBufferWidthKey as String: width,
|
|
||||||
kCVPixelBufferHeightKey as String: height,
|
|
||||||
kCVPixelBufferIOSurfacePropertiesKey as String: [:],
|
|
||||||
]
|
|
||||||
let status = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, attrs as CFDictionary, &pixelBuffer)
|
|
||||||
|
|
||||||
guard status == kCVReturnSuccess, let pixelBuffer = pixelBuffer else {
|
|
||||||
return request.completion(.failure(PigeonError(code: "", message: "Failed to create pixel buffer for request \(requestId), status: \(status)", details: nil)))
|
|
||||||
}
|
|
||||||
|
|
||||||
if request.isCancelled {
|
|
||||||
return request.completion(Self.cancelledResult)
|
|
||||||
}
|
|
||||||
|
|
||||||
Self.ciContext.render(ciImage, to: pixelBuffer)
|
|
||||||
|
|
||||||
CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly)
|
|
||||||
guard let pointer = CVPixelBufferGetBaseAddress(pixelBuffer) else {
|
|
||||||
CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
|
|
||||||
return request.completion(.failure(PigeonError(code: "", message: "Failed to lock pixel buffer for request \(requestId)", details: nil)))
|
|
||||||
}
|
|
||||||
|
|
||||||
let rowBytes = CVPixelBufferGetBytesPerRow(pixelBuffer)
|
|
||||||
|
|
||||||
Self.requestQueue.sync {
|
|
||||||
Self.lockedPixelBuffers[requestId] = pixelBuffer
|
|
||||||
}
|
|
||||||
|
|
||||||
request.completion(
|
|
||||||
.success([
|
|
||||||
"pointer": Int64(Int(bitPattern: pointer)),
|
|
||||||
"width": Int64(width),
|
|
||||||
"height": Int64(height),
|
|
||||||
"rowBytes": Int64(rowBytes),
|
|
||||||
]))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func get(requestId: Int64) -> RemoteImageRequest? {
|
func get(requestId: Int64) -> RemoteImageRequest? {
|
||||||
@@ -183,9 +159,4 @@ class RemoteImageApiDelegate: NSObject, URLSessionDataDelegate {
|
|||||||
request.isCancelled = true
|
request.isCancelled = true
|
||||||
request.task?.cancel()
|
request.task?.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
func releasePixelBuffer(requestId: Int64) -> Void {
|
|
||||||
guard let pixelBuffer = (Self.requestQueue.sync { Self.lockedPixelBuffers.removeValue(forKey: requestId) }) else { return }
|
|
||||||
CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:ffi';
|
import 'dart:ffi';
|
||||||
|
import 'dart:io';
|
||||||
import 'dart:ui' as ui;
|
import 'dart:ui' as ui;
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
@@ -34,7 +35,7 @@ abstract class ImageRequest {
|
|||||||
|
|
||||||
void _onCancelled();
|
void _onCancelled();
|
||||||
|
|
||||||
Future<ui.FrameInfo?> _fromPlatformImage(Map<String, int> info, ui.PixelFormat pixelFormat, bool shouldFree) async {
|
Future<ui.FrameInfo?> _fromPlatformImage(Map<String, int> info, bool shouldFree) async {
|
||||||
final address = info['pointer'];
|
final address = info['pointer'];
|
||||||
if (address == null) {
|
if (address == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -75,7 +76,7 @@ abstract class ImageRequest {
|
|||||||
width: actualWidth,
|
width: actualWidth,
|
||||||
height: actualHeight,
|
height: actualHeight,
|
||||||
rowBytes: rowBytes,
|
rowBytes: rowBytes,
|
||||||
pixelFormat: pixelFormat,
|
pixelFormat: ui.PixelFormat.rgba8888,
|
||||||
);
|
);
|
||||||
final codec = await descriptor.instantiateCodec();
|
final codec = await descriptor.instantiateCodec();
|
||||||
if (_isCancelled) {
|
if (_isCancelled) {
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class LocalImageRequest extends ImageRequest {
|
|||||||
isVideo: assetType == AssetType.video,
|
isVideo: assetType == AssetType.video,
|
||||||
);
|
);
|
||||||
|
|
||||||
final frame = await _fromPlatformImage(info, ui.PixelFormat.rgba8888, true);
|
final frame = await _fromPlatformImage(info, true);
|
||||||
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
|
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,10 +15,12 @@ class RemoteImageRequest extends ImageRequest {
|
|||||||
final Map<String, int> info = await remoteImageApi.requestImage(uri, headers: headers, requestId: requestId);
|
final Map<String, int> info = await remoteImageApi.requestImage(uri, headers: headers, requestId: requestId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final frame = await _fromPlatformImage(info, ui.PixelFormat.bgra8888, false);
|
final frame = await _fromPlatformImage(info, Platform.isIOS);
|
||||||
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
|
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
|
||||||
} finally {
|
} finally {
|
||||||
unawaited(remoteImageApi.releaseImage(requestId));
|
if (Platform.isAndroid) {
|
||||||
|
unawaited(remoteImageApi.releaseImage(requestId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class ThumbhashImageRequest extends ImageRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Map<String, int> info = await localImageApi.getThumbhash(thumbhash);
|
final Map<String, int> info = await localImageApi.getThumbhash(thumbhash);
|
||||||
final frame = await _fromPlatformImage(info, ui.PixelFormat.rgba8888, true);
|
final frame = await _fromPlatformImage(info, true);
|
||||||
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
|
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user