fix mac render memory, dispose old decoded image (#8140)

Signed-off-by: 21pages <sunboeasy@gmail.com>
This commit is contained in:
21pages
2024-05-27 09:27:30 +08:00
committed by GitHub
parent e7f0f0ff8d
commit 0442f7012b
4 changed files with 82 additions and 24 deletions

View File

@@ -5,7 +5,7 @@ import 'package:flutter/widgets.dart';
import 'package:flutter_hbb/common.dart';
Future<ui.Image> decodeImageFromPixels(
Future<ui.Image?> decodeImageFromPixels(
Uint8List pixels,
int width,
int height,
@@ -18,36 +18,74 @@ Future<ui.Image> decodeImageFromPixels(
}) async {
if (targetWidth != null) {
assert(allowUpscaling || targetWidth <= width);
if (!(allowUpscaling || targetWidth <= width)) {
print("not allow upscaling but targetWidth > width");
return null;
}
}
if (targetHeight != null) {
assert(allowUpscaling || targetHeight <= height);
}
final ui.ImmutableBuffer buffer =
await ui.ImmutableBuffer.fromUint8List(pixels);
onPixelsCopied?.call();
final ui.ImageDescriptor descriptor = ui.ImageDescriptor.raw(
buffer,
width: width,
height: height,
rowBytes: rowBytes,
pixelFormat: format,
);
if (!allowUpscaling) {
if (targetWidth != null && targetWidth > descriptor.width) {
targetWidth = descriptor.width;
}
if (targetHeight != null && targetHeight > descriptor.height) {
targetHeight = descriptor.height;
if (!(allowUpscaling || targetHeight <= height)) {
print("not allow upscaling but targetHeight > height");
return null;
}
}
final ui.Codec codec = await descriptor.instantiateCodec(
targetWidth: targetWidth,
targetHeight: targetHeight,
);
final ui.ImmutableBuffer buffer;
try {
buffer = await ui.ImmutableBuffer.fromUint8List(pixels);
onPixelsCopied?.call();
} catch (e) {
return null;
}
final ui.ImageDescriptor descriptor;
try {
descriptor = ui.ImageDescriptor.raw(
buffer,
width: width,
height: height,
rowBytes: rowBytes,
pixelFormat: format,
);
if (!allowUpscaling) {
if (targetWidth != null && targetWidth > descriptor.width) {
targetWidth = descriptor.width;
}
if (targetHeight != null && targetHeight > descriptor.height) {
targetHeight = descriptor.height;
}
}
} catch (e) {
print("ImageDescriptor.raw failed: $e");
buffer.dispose();
return null;
}
final ui.Codec codec;
try {
codec = await descriptor.instantiateCodec(
targetWidth: targetWidth,
targetHeight: targetHeight,
);
} catch (e) {
print("instantiateCodec failed: $e");
buffer.dispose();
descriptor.dispose();
return null;
}
final ui.FrameInfo frameInfo;
try {
frameInfo = await codec.getNextFrame();
} catch (e) {
print("getNextFrame failed: $e");
codec.dispose();
buffer.dispose();
descriptor.dispose();
return null;
}
final ui.FrameInfo frameInfo = await codec.getNextFrame();
codec.dispose();
buffer.dispose();
descriptor.dispose();