mirror of
https://github.com/immich-app/immich.git
synced 2026-02-04 08:49:01 +03:00
* platform clients * uppercase http method * fix hot reload * custom user agent * init before app launch * set defaults * move to bootstrap * unrelated change * disable disk cache by default * optimized decoding * remove incremental * android impl * memory optimization * lock approach is slower on ios * conditional cronet * clarify parameter * enable disk cache * set user agent * flutter-side decode * optimized http * fixed locking * refactor * potential race conditions * embedded cronet * refactor, fix capacity handling * fast path for known content length * ios optimizations * re-enable cache * formatting * bump concurrency * clear cache button * fix eviction race condition * add extra cancellation check * tighten dispose * better error handling * fix disposal --------- Co-authored-by: Alex <alex.tran1502@gmail.com>
128 lines
2.9 KiB
Dart
128 lines
2.9 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});
|
|
|
|
void cancel() {
|
|
if (_isCancelled) {
|
|
return;
|
|
}
|
|
_isCancelled = true;
|
|
return _onCancelled();
|
|
}
|
|
|
|
void _onCancelled();
|
|
|
|
Future<ui.FrameInfo?> _fromEncodedPlatformImage(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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|