mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
memory optimization
This commit is contained in:
@@ -34,7 +34,7 @@ abstract class ImageRequest {
|
||||
|
||||
void _onCancelled();
|
||||
|
||||
Future<ui.FrameInfo?> _fromPlatformImage(Map<String, int> info) async {
|
||||
Future<ui.FrameInfo?> _fromPlatformImage(Map<String, int> info, ui.PixelFormat pixelFormat, bool shouldFree) async {
|
||||
final address = info['pointer'];
|
||||
if (address == null) {
|
||||
return null;
|
||||
@@ -42,7 +42,9 @@ abstract class ImageRequest {
|
||||
|
||||
final pointer = Pointer<Uint8>.fromAddress(address);
|
||||
if (_isCancelled) {
|
||||
malloc.free(pointer);
|
||||
if (shouldFree) {
|
||||
malloc.free(pointer);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -58,7 +60,9 @@ abstract class ImageRequest {
|
||||
actualSize = rowBytes * actualHeight;
|
||||
buffer = await ImmutableBuffer.fromUint8List(pointer.asTypedList(actualSize));
|
||||
} finally {
|
||||
malloc.free(pointer);
|
||||
if (shouldFree) {
|
||||
malloc.free(pointer);
|
||||
}
|
||||
}
|
||||
|
||||
if (_isCancelled) {
|
||||
@@ -71,7 +75,7 @@ abstract class ImageRequest {
|
||||
width: actualWidth,
|
||||
height: actualHeight,
|
||||
rowBytes: rowBytes,
|
||||
pixelFormat: ui.PixelFormat.rgba8888,
|
||||
pixelFormat: pixelFormat,
|
||||
);
|
||||
final codec = await descriptor.instantiateCodec();
|
||||
if (_isCancelled) {
|
||||
|
||||
@@ -24,7 +24,7 @@ class LocalImageRequest extends ImageRequest {
|
||||
isVideo: assetType == AssetType.video,
|
||||
);
|
||||
|
||||
final frame = await _fromPlatformImage(info);
|
||||
final frame = await _fromPlatformImage(info, ui.PixelFormat.rgba8888, true);
|
||||
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,12 +14,16 @@ class RemoteImageRequest extends ImageRequest {
|
||||
|
||||
final Map<String, int> info = await remoteImageApi.requestImage(uri, headers: headers, requestId: requestId);
|
||||
|
||||
final frame = await _fromPlatformImage(info);
|
||||
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
|
||||
try {
|
||||
final frame = await _fromPlatformImage(info, ui.PixelFormat.bgra8888, false);
|
||||
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
|
||||
} finally {
|
||||
unawaited(remoteImageApi.releaseImage(requestId));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> _onCancelled() {
|
||||
return localImageApi.cancelRequest(requestId);
|
||||
return remoteImageApi.cancelRequest(requestId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ class ThumbhashImageRequest extends ImageRequest {
|
||||
}
|
||||
|
||||
final Map<String, int> info = await localImageApi.getThumbhash(thumbhash);
|
||||
final frame = await _fromPlatformImage(info);
|
||||
final frame = await _fromPlatformImage(info, ui.PixelFormat.rgba8888, true);
|
||||
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user