Building a performant application
has everything to do with understanding
how the hardware is working.
So you know the best way is to work with it.
And at the core of rendering performance
sits an easily misunderstood hardware process
that governs how fast your application can actually
render.
My name is Colt McAnlis, and if you're
looking to improve your app's rendering performance,
you need to become familiar with a term known as VSYNC.
But first, let's actually take a step back.
Understanding VSYNC starts with understanding
two other simple terms.
The first is refresh rate, and it represents how many times
a screen can update its display per second.
This value is specified in hertz and is
constant based on the hardware.
The second term is frame rate, which
measures how many times your GPU can draw a frame per second--
for example, 30 frames per second or 60 frames per second.
In this case, higher is always better.
Now, refresh rate and frame rate both need
to work together in order to get your images drawn
to your screen.
The GPU will take in your data, draw it,
and then the hardware will push the output of that
to the screen.
This will happen over and over and over again
during the lifetime of your application.
But the problem is that refresh rate and frame rate
aren't actually guaranteed to occur at the same frequency.
Say if your frame rate is actually
faster than your refresh rate, then
you're going to be seeing a few visual problems.
For example, imagine taking a picture
and then rotating the camera about five degrees
and taking another picture.
Now, cut that second photo in half
and glue it on top of the first one.
The two images would be similar, but there'd
be a noticeable difference between the top half
and the bottom half along the cut seam.
This is known as tearing, and this
is what occurs in your application
if the frame rate and the refresh rate are not lining up.
The issue is that your graphics card uses one region of memory
to draw its frames into.
Each new frame will overwrite the previous frame,
outputting data one line at a time, starting at the top.
Now, when the screen comes along to refresh,
it doesn't know what state that buffer is actually in.
As such, it can grab a frame from the GPU that's
not entirely finished yet, meaning
it's got half of the previous frame
and half of the current frame.
A solution to this is known as double buffering.
Effectively, when the GPU is done drawing a frame to memory,
known as the back buffer, it will copy it
to a secondary area of memory known as the frame buffer.
When it draws the next frame, it'll
begin filling in the back buffer while the frame buffer remains
untouched.
Now, when the screen comes along to refresh,
it'll do so from the frame buffer, which
isn't in the middle of a draw operation.
This is where VSYNC comes in.
VSYNC, or vertical synchronization,
will basically keep this copy operation from back buffer
to frame buffer from happening if the screen is
in the middle of refreshing from it.
Having a frame rate higher than a refresh rate is ideal,
since your GPU is refreshing faster than your screen
is reading.
In this scenario, after a successful screen refresh,
your GPU will be VSYNCed, waiting for the next screen
refresh to occur.
In this scenario, your frame rate
has been effectively capped at your device refresh rate.
But what happens if your screen is refreshing
twice as fast as your GPU is drawing?
For example, the GPU is only outputting 30 frames a second,
but the screen is actually refreshing at 60 hertz.
This means that every other refresh
will be sampling from the same frame.
Or rather, the screen will be displaying the same GPU frame
twice in a row.
When this happens intermittently,
you get problems.
For example, if your frame rate is
higher than refresh rate but suddenly dips,
then the user will see a smooth animation broke up
with sudden pauses, followed by the rest
of the smooth animation.
This exact effect manifests itself
to the user in a less than smooth animated experience,
which is often described as jank or hitching or lag or stutter.
And trust me, these are the things
that you actually want to avoid.
Your application should strive to remove these sudden dips
in frame rate by always ensuring that your GPU gets
its data quickly and has time to draw
the contents by the next screen refresh.
But understanding VSYNC is just the start.
Building a high-performance application
requires you to know what's going
on under the hood of your Android device.
And to make sure that you're doing that,
check out the rest of the resources
on the Android Performance Patterns page.
Oh, and don't forget to join our Google+ community to start
building something amazing.
So keep calm, profile your code, and always remember,
perf matters.
[MUSIC PLAYING]