createGpuTexture method

Future<int?> createGpuTexture({
  1. String backend = 'gles',
  2. int width = 1,
  3. int height = 1,
})

(Android only) Create a native GPU texture for zero-copy rendering.

Implementation

Future<int?> createGpuTexture({
  String backend = 'gles',
  int width = 1,
  int height = 1,
}) async {
  if (defaultTargetPlatform != TargetPlatform.android) return null;
  await releaseGpuTexture(graceful: true);
  final Map<dynamic, dynamic>? res = await _channel.invokeMethod(
    'createTexture',
    <String, Object>{
      'backend': backend,
      'width': width < 1 ? 1 : width,
      'height': height < 1 ? 1 : height,
    },
  );
  if (res != null) {
    final textureId = res['textureId'] as int?;
    final surfaceHandle = res['surfaceHandle'] as int?;
    if (textureId == null || surfaceHandle == null || surfaceHandle == 0) {
      if (textureId != null) {
        try {
          await _channel
              .invokeMethod('releaseTexture', {'textureId': textureId});
        } catch (_) {
          // Best effort cleanup only.
        }
      }
      throw PlatformException(
        code: 'NATIVE_WINDOW_ERROR',
        message: 'Failed to acquire native window',
      );
    }
    _gpuTextureId = textureId;
    _gpuSurfaceHandle = surfaceHandle;
    _gpuBackend = backend;
    notifyListeners();
  }
  return _gpuTextureId;
}