text
stringlengths
1
372
<topic_end>
<topic_start>
shader compilation jank
info note
to learn how to use the performance view
(part of flutter DevTools)
for debugging performance issues,
see using the performance view.
if the animations on your mobile app appear to be janky,
but only on the first run,
this is likely due to shader compilation.
flutter’s long term solution to
shader compilation jank is impeller,
which is in the stable release for iOS
and in preview behind a flag on android.
while we work on making impeller fully production ready,
you can mitigate shader compilation jank by bundling
precompiled shaders with an iOS app.
unfortunately, this approach doesn’t work well on android
due to precompiled shaders being device or GPU-specific.
the android hardware ecosystem is large enough that the
GPU-specific precompiled shaders bundled with an application
will work on only a small subset of devices,
and will likely make jank worse on the other devices,
or even create rendering errors.
also, note that we aren’t planning to make
improvements to the developer experience for creating
precompiled shaders described below. instead,
we are focusing our energies on the more robust
solution to this problem that impeller offers.
<topic_end>
<topic_start>
what is shader compilation jank?
a shader is a piece of code that runs on a
GPU (graphics processing unit).
when the skia graphics backend that flutter uses for rendering
sees a new sequence of draw commands for the first time,
it sometimes generates and compiles a
custom GPU shader for that sequence of commands.
this allows that sequence and potentially similar sequences
to render as fast as possible.
unfortunately, skia’s shader generation and compilation
happens in sequence with the frame workload.
the compilation could cost up to a few hundred milliseconds
whereas a smooth frame needs to be drawn within 16 milliseconds
for a 60 fps (frame-per-second) display.
therefore, a compilation could cause tens of frames
to be missed, and drop the fps from 60 to 6.
this is compilation jank.
after the compilation is complete,
the animation should be smooth.
on the other hand, impeller generates and compiles all
necessary shaders when we build the flutter engine.
therefore apps running on impeller already have
all the shaders they need, and the shaders can be used
without introducing jank into animations.
definitive evidence for the presence of shader compilation jank
is to set GrGLProgramBuilder::finalize in the tracing
with --trace-skia enabled.
the following screenshot shows an example timeline tracing.
<topic_end>
<topic_start>
what do we mean by “first run”?
on iOS, “first run” means that the user might see
jank when an animation first occurs every time
the user opens the app from scratch.
<topic_end>
<topic_start>
how to use SkSL warmup
flutter provides command line tools
for app developers to collect shaders that might be needed
for end-users in the SkSL (skia shader language) format.
the SkSL shaders can then be packaged into the app,
and get warmed up (pre-compiled) when an end-user first
opens the app, thereby reducing the compilation
jank in later animations.
use the following instructions to collect
and package the SkSL shaders:
run the app with --cache-sksl turned on
to capture shaders in SkSL:
if the same app has been previously run
without --cache-sksl, then the
--purge-persistent-cache flag might be needed:
this flag removes older non-SkSL shader caches that
could interfere with SkSL shader capturing.
it also purges the SkSL shaders so use it only on the first
--cache-sksl run.
play with the app to trigger as many animations
as needed; particularly those with compilation jank.
press m at the command line of flutter run to
write the captured SkSL shaders into a file named something like
flutter_01.sksl.json.
for best results,
capture SkSL shaders on an actual iOS device.
a shader captured on a simulator isn’t likely to work correctly
on actual hardware.
build the app with SkSL warm-up using the following,
as appropriate:
if it’s built for a driver test like test_driver/app.dart,
make sure to also specify --target=test_driver/app.dart