mirror of
https://github.com/immich-app/immich.git
synced 2026-03-05 16:33:46 -05:00
* feat(mobile): add support for encoded image requests in local and remote image APIs * fix(mobile): handle memory cleanup for cancelled image requests * refactor(mobile): simplify memory management and response handling for encoded image requests * fix(mobile): correct formatting in cancellation check for image requests * Apply suggestion from @mertalev Co-authored-by: Mert <101130780+mertalev@users.noreply.github.com> * refactor(mobile): rename 'encoded' parameter to 'preferEncoded' for clarity in image request APIs * fix(mobile): ensure proper resource cleanup for cancelled image requests * refactor(mobile): streamline codec handling by removing unnecessary descriptor disposal in loadCodec request --------- Co-authored-by: Mert <101130780+mertalev@users.noreply.github.com>
144 lines
3.3 KiB
Dart
144 lines
3.3 KiB
Dart
import 'dart:async';
|
|
import 'dart:ffi';
|
|
import 'dart:ui' as ui;
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:ffi/ffi.dart';
|
|
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
|
import 'package:immich_mobile/providers/infrastructure/platform.provider.dart';
|
|
|
|
part 'local_image_request.dart';
|
|
part 'thumbhash_image_request.dart';
|
|
part 'remote_image_request.dart';
|
|
|
|
abstract class ImageRequest {
|
|
static int _nextRequestId = 0;
|
|
|
|
final int requestId = _nextRequestId++;
|
|
bool _isCancelled = false;
|
|
|
|
get isCancelled => _isCancelled;
|
|
|
|
ImageRequest();
|
|
|
|
Future<ImageInfo?> load(ImageDecoderCallback decode, {double scale = 1.0});
|
|
|
|
Future<ui.Codec?> loadCodec();
|
|
|
|
void cancel() {
|
|
if (_isCancelled) {
|
|
return;
|
|
}
|
|
_isCancelled = true;
|
|
return _onCancelled();
|
|
}
|
|
|
|
void _onCancelled();
|
|
|
|
Future<(ui.Codec, ui.ImageDescriptor)?> _codecFromEncodedPlatformImage(int address, int length) async {
|
|
final pointer = Pointer<Uint8>.fromAddress(address);
|
|
if (_isCancelled) {
|
|
malloc.free(pointer);
|
|
return null;
|
|
}
|
|
|
|
final ui.ImmutableBuffer buffer;
|
|
try {
|
|
buffer = await ImmutableBuffer.fromUint8List(pointer.asTypedList(length));
|
|
} finally {
|
|
malloc.free(pointer);
|
|
}
|
|
|
|
if (_isCancelled) {
|
|
buffer.dispose();
|
|
return null;
|
|
}
|
|
|
|
final descriptor = await ui.ImageDescriptor.encoded(buffer);
|
|
buffer.dispose();
|
|
if (_isCancelled) {
|
|
descriptor.dispose();
|
|
return null;
|
|
}
|
|
|
|
final codec = await descriptor.instantiateCodec();
|
|
if (_isCancelled) {
|
|
descriptor.dispose();
|
|
codec.dispose();
|
|
return null;
|
|
}
|
|
|
|
return (codec, descriptor);
|
|
}
|
|
|
|
Future<ui.FrameInfo?> _fromEncodedPlatformImage(int address, int length) async {
|
|
final result = await _codecFromEncodedPlatformImage(address, length);
|
|
if (result == null) return null;
|
|
|
|
final (codec, descriptor) = result;
|
|
if (_isCancelled) {
|
|
descriptor.dispose();
|
|
codec.dispose();
|
|
return null;
|
|
}
|
|
|
|
final frame = await codec.getNextFrame();
|
|
descriptor.dispose();
|
|
codec.dispose();
|
|
if (_isCancelled) {
|
|
frame.image.dispose();
|
|
return null;
|
|
}
|
|
|
|
return frame;
|
|
}
|
|
|
|
Future<ui.FrameInfo?> _fromDecodedPlatformImage(int address, int width, int height, int rowBytes) async {
|
|
final pointer = Pointer<Uint8>.fromAddress(address);
|
|
if (_isCancelled) {
|
|
malloc.free(pointer);
|
|
return null;
|
|
}
|
|
|
|
final size = rowBytes * height;
|
|
final ui.ImmutableBuffer buffer;
|
|
try {
|
|
buffer = await ImmutableBuffer.fromUint8List(pointer.asTypedList(size));
|
|
} finally {
|
|
malloc.free(pointer);
|
|
}
|
|
|
|
if (_isCancelled) {
|
|
buffer.dispose();
|
|
return null;
|
|
}
|
|
|
|
final descriptor = ui.ImageDescriptor.raw(
|
|
buffer,
|
|
width: width,
|
|
height: height,
|
|
rowBytes: rowBytes,
|
|
pixelFormat: ui.PixelFormat.rgba8888,
|
|
);
|
|
buffer.dispose();
|
|
|
|
final codec = await descriptor.instantiateCodec();
|
|
if (_isCancelled) {
|
|
descriptor.dispose();
|
|
codec.dispose();
|
|
return null;
|
|
}
|
|
|
|
final frame = await codec.getNextFrame();
|
|
descriptor.dispose();
|
|
codec.dispose();
|
|
if (_isCancelled) {
|
|
frame.image.dispose();
|
|
return null;
|
|
}
|
|
|
|
return frame;
|
|
}
|
|
}
|