Sub topic 12
Shader Compilation Jank and Impeller
Assume you can run a Flutter app in profile mode, open DevTools, and read the Performance view. You should also know that the UI thread builds widgets while the raster thread turns those widgets into pixels.
What shader compilation jank is
A shader is a small graphics program that runs on the GPU. Flutter uses shaders for effects such as:
- Gradients and color filters
- Clipping and masking
- Blurs
- Shadows
- Image filters
- Some transitions and complex drawing operations
A shader may need to be compiled before the GPU can use it. Compilation can happen the first time Flutter draws a particular effect. That compilation takes time on the raster path. If it delays a frame beyond its budget, the screen appears to stutter. This is shader compilation jank.
Shader jank often appears during a first navigation, animation, or interaction. Repeating the same action may look smoother because the compiled shader is cached.
How Impeller changes the problem
Impeller is Flutter's rendering engine designed to make rendering work more predictable. Traditional Skia rendering can compile some GPU shaders at runtime. Impeller uses a pipeline that supports ahead-of-time shader preparation for many built-in effects. This reduces the chance that the first frame must pause for shader compilation.
The difference is not simply “Skia is slow and Impeller is fast.” The engines have different trade-offs:
| Rendering approach | Typical shader behavior | Main diagnostic concern |
|---|---|---|
| Skia runtime shaders | Some shaders compile when first used | First-use raster jank and shader cache behavior |
| Impeller | Many built-in shaders are prepared ahead of time | Unsupported effects, expensive raster work, or remaining runtime effects |
Impeller does not make every drawing operation free. Large blur regions, complex clips, oversized shadows, and many translucent layers can still overload the raster thread. Custom runtime shaders can also have their own compilation or execution costs.
Availability and behavior depend on the Flutter version, platform, graphics API, and device. Confirm which renderer the target device uses instead of assuming that all test devices behave alike.
Diagnose a suspected shader hitch
Use a repeatable test:
- Start the app in profile mode on the target device.
- Open DevTools and record the Performance view.
- Perform the action that causes the hitch for the first time.
- Repeat the same action and compare the traces.
- Select the slow frame in the Flutter frames chart.
- Check whether raster time is the part that exceeds the frame budget.
- Inspect Timeline Events for shader, pipeline, or GPU-related work.
- Test the same action on another device or renderer when possible.
A first-use spike that becomes smaller on later runs is evidence of caching or warm-up. It is not proof by itself. The first run may also load images, create layers, or allocate other GPU resources.
Fixes and decision criteria
First, measure the effect in profile mode. Then choose the smallest targeted change:
- Reduce the size or blur radius of a blurred region.
- Avoid animating a large filter or shadow when a simpler effect works.
- Reduce overlapping translucent layers.
- Simplify a custom shader or limit the area it covers.
- Trigger a representative effect before a user-critical animation only when the warm-up cost is acceptable.
- Prefer Impeller on supported production targets when it improves the measured result.
Warm-up can hide a first-use hitch, but it moves work earlier and can increase startup time. A simpler effect often improves both startup and steady-state rendering. Always re-record the same interaction after the change and compare the slowest frames, not only the average frame time.
Exercise
A transition stutters the first time it runs. The selected frame has high raster time, and later runs are smooth. What should you investigate first?
Show answer
Investigate shader or GPU pipeline preparation, then confirm it in Timeline Events. Also check image loading and layer creation, because first-run caching alone does not prove shader compilation.