-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
In flutter/engine#38905, we added the following API in dart:ui:
Future<Codec> instantiateImageCodecWithSize(
ImmutableBuffer buffer, {
TargetImageSizeCallback? getTargetSize,
});
typedef TargetImageSizeCallback = TargetImageSize Function(
int intrinsicWidth,
int intrinsicHeight,
);This was easily implemented in the VM engine, but in the web engine, in order to know the intrinsic size of the image, we first have to create a codec, decode the first frame, and then extract the intrinsic width & height out of that frame. Then we consult the TargetImageSizeCallback for the target size, and we create another codec against the target size, discarding the first codec.
There's a possible optimization we could make whereby we could return the first codec (rather than discarding it and creating another) if the getTargetSize callback returned a target size that either (a) had both the width & height null, or (b) specified the same size as the intrinsic size.
In order to do this, we'd take advantage of the fact that the CanvasKit codec knows the width & height of the image without needing to get the first frame. We could expose that implementation detail encapsulated inside the web engine (without exposing the width & height to the public Codec API), thus allowing us to avoid getting the image's first frame - and in turn allowing us to return the codec directly rather than discarding it and creating another.