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);
|
||||
}
|
||||
|
||||
|
||||
+23
@@ -103,4 +103,27 @@ class RemoteImageApi {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> releaseImage(int requestId) async {
|
||||
final String pigeonVar_channelName =
|
||||
'dev.flutter.pigeon.immich_mobile.RemoteImageApi.releaseImage$pigeonVar_messageChannelSuffix';
|
||||
final BasicMessageChannel<Object?> pigeonVar_channel = BasicMessageChannel<Object?>(
|
||||
pigeonVar_channelName,
|
||||
pigeonChannelCodec,
|
||||
binaryMessenger: pigeonVar_binaryMessenger,
|
||||
);
|
||||
final Future<Object?> pigeonVar_sendFuture = pigeonVar_channel.send(<Object?>[requestId]);
|
||||
final List<Object?>? pigeonVar_replyList = await pigeonVar_sendFuture as List<Object?>?;
|
||||
if (pigeonVar_replyList == null) {
|
||||
throw _createConnectionError(pigeonVar_channelName);
|
||||
} else if (pigeonVar_replyList.length > 1) {
|
||||
throw PlatformException(
|
||||
code: pigeonVar_replyList[0]! as String,
|
||||
message: pigeonVar_replyList[1] as String?,
|
||||
details: pigeonVar_replyList[2],
|
||||
);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user