enableBackgroundFromAsset method
Enables a renderer-managed background from an asset.
Implementation
@override
Future<void> enableBackgroundFromAsset(String assetPath) async {
final byteData = await services.rootBundle.load(assetPath);
final codec = await ui.instantiateImageCodec(
byteData.buffer.asUint8List(
byteData.offsetInBytes,
byteData.lengthInBytes,
),
);
try {
final frame = await codec.getNextFrame();
final image = frame.image;
try {
final rgba = await image.toByteData();
if (rgba == null) {
throw StateError(
'Failed to convert background image to raw RGBA bytes.',
);
}
final width = image.width;
final height = image.height;
if (_backgroundTexture == null ||
_backgroundTexture!.width != width ||
_backgroundTexture!.height != height) {
_backgroundTexture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible,
width,
height,
enableRenderTargetUsage: false,
);
}
_backgroundTexture!.overwrite(rgba);
} finally {
image.dispose();
}
} finally {
codec.dispose();
}
_onNeedsRender?.call();
}