updateTexture method

Future<int> updateTexture({
  1. int? width,
  2. int? height,
  3. bool? tunnel,
  4. bool? fit,
})

Release current texture then create a new one for current media, and update textureId.

Texture will be created when media is loaded and mediaInfo.video is not empty. If both width and height are null, texture size is video frame size, otherwise is requested size.

Implementation

Future<int> updateTexture(
    {int? width, int? height, bool? tunnel, bool? fit}) async {
  if ((textureId.value ?? -1) >= 0) {
    await FvpPlatform.instance.releaseTexture(nativeHandle, textureId.value!);
    textureId.value = null;
  }
  final size = await _videoSize.future;
  if (size == null) {
    return -1;
  }
  if (width == null && height == null) {
    // original size
    textureId.value = await FvpPlatform.instance.createTexture(nativeHandle,
        size.width.toInt(), size.height.toInt(), tunnel ?? false);
    return textureId.value!;
  }
  if (width != null && height != null && width > 0 && height > 0) {
    if (fit ?? true) {
      final r = size.width / size.height;
      final w = (height * r).toInt();
      if (w <= width) {
        width = w;
      } else {
        height = (width / r).toInt();
      }
    }
    textureId.value = await FvpPlatform.instance
        .createTexture(nativeHandle, width, height, tunnel ?? false);
    return textureId.value!;
  }
  // release texture if width or height <= 0
  return -1;
}