Sub topic 12
Shader compilation jank and Impeller
This chapter covers one specific cause of slow raster frames: shaders that are compiled while the app is already running. It explains why the problem shows up on the first run of an animation, how to recognize it in DevTools, and how Impeller removes it. It assumes you can run an app in profile mode, read the frames chart, and tell UI-thread bars from raster-thread bars, as covered in earlier chapters.
What a shader is and why compiling one costs time
A shader is a small program that runs on the GPU. Flutter draws gradients, blurs, clipped shapes, and image effects by handing the GPU a shader along with the geometry to draw. The GPU driver cannot run the shader source directly. It must first compile the shader into machine code for that specific GPU. This is the expensive step, and it can take tens of milliseconds.
The cost depends on when the compile happens:
- Compiled ahead of time, the cost is paid at build time or at app startup, and no frame is affected.
- Compiled on first use, the cost lands inside whichever frame first needs the shader. That frame blows through the frame budget.
The second case is called shader compilation jank. Flutter's older rendering backend, Skia, generated and compiled many shaders lazily at runtime. The jank was a first-run problem: the same animation ran smoothly the second time, because the compiled shader was already cached.
How the problem looks in DevTools
Shader compilation jank has a recognizable signature. It happens on the raster thread, so the raster bar of the affected frame is tall and often red, while the UI bar stays short. The frame also belongs to an animation or screen that is being shown for the first time since launch.
With Skia, DevTools helps you confirm this in two places:
- Frames chart: frames that spent time compiling shaders are marked with a distinct color, separate from ordinary slow raster frames.
- Frame Analysis: the analysis for such a frame reports shader compilation as a likely cause, next to other hints such as expensive operations and saveLayer calls.
The most reliable test is a repeat run:
- Launch the app fresh, in profile mode, on a physical device.
- Trigger the suspect animation once and note the raster time of its worst frame.
- Trigger the same animation again without restarting.
- If the first run shows spikes and the second does not, the cause is a one-time cost, such as shader compilation or image decoding. A cost that repeats on every run is something else, such as an expensive effect.
Reproducing the pattern
The following app opens a translucent route with a blurred backdrop and a gradient. A full-screen BackdropFilter plus a gradient fade-in is a good candidate for first-use cost, because each effect needs its own GPU pipeline.
import 'dart:ui';
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: HomePage()));
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Shader demo')),
body: Center(
child: FilledButton(
onPressed: () => Navigator.of(context).push(
PageRouteBuilder<void>(
opaque: false,
transitionDuration: const Duration(milliseconds: 600),
pageBuilder: (_, __, ___) => const BlurPage(),
transitionsBuilder: (_, animation, __, child) =>
FadeTransition(opacity: animation, child: child),
),
),
child: const Text('Open blurred overlay'),
),
),
);
}
}
class BlurPage extends StatelessWidget {
const BlurPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 12, sigmaY: 12),
child: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Color(0x669C27B0), Color(0x66FF9800)],
),
),
child: Center(
child: FilledButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Close'),
),
),
),
),
);
}
}Run it twice, once with each backend, and compare the first open of the overlay. The flags below apply to flutter run:
flutter run --profile --no-enable-impeller -d <device-id>
flutter run --profile --enable-impeller -d <device-id>Which backend is the default depends on platform and Flutter version, so the explicit flag keeps the comparison honest. Impeller has been the default on iOS for several releases and on Android (Vulkan, with an OpenGL ES fallback) in recent ones. Some platforms may still ignore or reject the opt-out flag. Check the current Flutter documentation for your target before relying on either flag.
What Impeller changes
Impeller is Flutter's newer rendering engine. Its central design decision targets exactly this problem: instead of generating shaders as drawing commands arrive, it ships a fixed, known set of shaders that are compiled when the engine is built. It also builds its GPU pipeline state objects up front, so a first-use animation does not trigger a driver compile.
Compared side by side:
| Aspect | Skia backend | Impeller |
|---|---|---|
| Shader source | Generated at runtime from the drawing commands | A fixed set, built with the engine |
| When the driver compiles | On first use, inside a frame | Before the app draws, not mid-animation |
| Typical symptom | Spikes on the raster thread the first time an effect appears | No first-run compile spikes from shaders |
| Warm-up workaround | Capture and bundle shaders (SkSL warm-up) | Not needed |
| Compile frames in DevTools | Highlighted as shader compilation | Not expected to appear |
The old workaround: SkSL warm-up
Before Impeller, the standard fix was SkSL warm-up. You ran the app with a shader-capture flag, exercised the animations by hand, saved the shaders that were compiled, and bundled that file into release builds. At startup, the engine compiled those shaders in advance.
The approach had real drawbacks. The captured file was specific to a device class and a Flutter version. It had to be regenerated when the UI or the engine changed, and any animation you forgot to exercise stayed janky. If you maintain an older app that still runs on Skia, you may find this workflow in the build scripts. For new work, switching the backend is the maintained answer, and the warm-up tooling is no longer where Flutter's effort goes.
Verifying the fix
Treat the backend switch like any other change in the measure, change, re-measure loop:
- Record the baseline on the Skia backend: the worst raster time on the first open of the overlay, and whether Frame Analysis flags shader compilation.
- Switch to Impeller with the same device, the same build mode, and a fresh app launch.
- Trigger the same interaction once, immediately after launch. Do not warm the app up first, because first-use cost is the thing you are measuring.
- Compare the worst raster frame and check that no frame is flagged for shader compilation.
Changing the backend and the app code at the same time makes the result unreadable. Change one thing per measurement.
What Impeller does not fix
Impeller removes one cause of raster jank. It does not make every raster frame cheap. If a frame is still slow after the switch, look for the other causes you already know how to find:
- Expensive effects: large blurs, many
saveLayercalls (fromOpacity, clipping with anti-aliasing, orShaderMask), and overdraw still cost GPU time on every frame. - Image work: decoding a large image for the first time, or uploading it to the GPU, is also a one-time cost. It looks similar to shader jank in the first-run pattern, but it is not a compile.
- Custom fragment shaders: if you load your own shader with
FragmentProgram, it is a different case. Load the program early, for example ininitStateor before the screen opens, so any work to prepare it does not happen mid-animation.
A quick check separates these cases. If the second run is smooth, the cost is one-time and you can look at image loading and first-use effects. If the second run is just as slow, the effect is expensive on every frame, and Timeline Events and Frame Analysis will point to the drawing operation responsible.
Common mistakes
- Judging on an emulator or in debug mode. Both distort raster timing. Use a physical device in profile mode.
- Warming up before measuring. Opening the animation once to "get ready" hides the exact first-run cost you want to see.
- Assuming the backend. If you did not pass a flag, confirm which backend your device used before drawing conclusions. The engine reports the backend in the startup log.
- Blaming shaders for repeated jank. Compile cost is paid once per shader. Jank that recurs on every run has another cause.
Exercise
A screen's transition shows a 45 ms raster frame the first time you open it after launching the app in profile mode, but opening it again is smooth. The UI bar is short in both runs. What is the likely cause, how would you confirm it with the DevTools tools you know, and what would you change?
Show answer
The pattern (raster thread only, first run only, smooth afterwards) points to a one-time cost on the GPU side, most likely shader compilation under the Skia backend, though a first-time image decode or upload can look the same. Confirm it by checking whether Frame Analysis flags the frame as shader compilation and whether the frame is highlighted as such in the frames chart, and by repeating the launch-and-open test on a physical device. Then rerun with Impeller (--enable-impeller, or the platform default where it applies), without opening the screen beforehand, and compare the worst raster frame. If the spike is gone, the fix worked. If it remains, investigate image loading and expensive effects rather than shaders.
Exercise
Why is it a mistake to change both the rendering backend and your widget code in a single measurement round?
Show answer
If the raster time improves or gets worse, you cannot tell which change caused it. Changing one thing per round keeps the before and after numbers comparable, which is the point of the measure, change, re-measure workflow.