An Android app with a non-blocking main thread in your application can still get frame drops. This seems odd as we know that drawing a frame involves several components. Our code will only run in the first component.

The UI thread will handle the app-side of things and records the drawing commands. A Native thread (RenderThread) will take these recordings and do the actual rendering. The GPU will then execute the graphics commands and SurfaceFlinger will compose your apps buffer with everything else on screen.

This can be confusing when trying to solve dropped frames. Slowness in your onDraw() and an overloaded GPU will look the same to your users, but need different solutions. Moving your code to a background thread will only help with one of these issues. To find out which issue you have, follow your frame through the pipeline.

What the UI thread does

Every app has a main thread, usually called the UI thread. It owns the main message queue and runs lifecycle callbacks, input events and anything posted to the main looper. For the View system it also applies property changes, measures and lays out the hierarchy, runs animation callbacks, calls View.draw() and your onDraw(), and records drawing operations for hardware-accelerated rendering.

The last job is easy to overlook. With hardware acceleration, onDraw() does not rasterize anything. Your Canvas calls are recorded into a display list, and each View keeps its own (a RenderNode). Here is a simple custom View:

class ProgressView(context: Context) : View(context) {

private val paint = Paint(Paint.ANTI_ALIAS_FLAG)

private var progress = 0.5f

override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)

canvas.drawCircle(width / 2f, height / 2f, width * progress / 2f, paint)
}
}

When invalidate() schedules a redraw, the framework calls onDraw() during the next frame traversal, on the UI thread, and the circle is recorded rather than painted.
That is why a custom View can cause jank while the GPU is almost idle.

If onDraw() does heavy calculation, allocates large temporary objects or loops over thousands of elements, the UI thread spends its time there, and the GPU cannot work on a frame nobody has submitted.

The Android rendering performance docs describe the same split: a recording phase on the UI thread, then a drawing phase on RenderThread.

What RenderThread does

RenderThread is a native thread in the hardware-accelerated rendering path. It does not run your UI code. It takes the display lists the UI thread recorded, replays them through HWUI (Android’s Skia-based renderer), and sends the resulting commands to the graphics driver.

So the three stages divide up like this:

  1. The UI thread decides what the UI looks like and records it.
  2. RenderThread replays the recording and submits graphics commands.
  3. The GPU rasterizes and shades.

In a system trace the two CPU stages appear as Record View#draw() on the main thread and DrawFrame on RenderThread. Splitting them lets Android overlap frame preparation with rendering instead of pushing everything through the app's thread.

RenderThread also does work that can be expensive even when the Canvas calls looked cheap. On many devices a large Path is rasterized on the CPU and uploaded to the GPU, and clipPath() can add costly rendering work. A harmless-looking call in app code can turn into the bottleneck one stage later.

RenderThread can also run some animations on its own. ViewPropertyAnimator and ripple effects can keep going while the main thread is busy, which is one reason they stay smooth.

One frame, start to finish

Take a user scrolling a RecyclerView. The scroll moves the content and may bring new items into view. The frame goes through these stages:

The stages overlap across frames, and a frame with nothing to change skips most of them. In order:

  1. A VSync signal arrives and Choreographer runs the frame callbacks on the main thread's looper. Input, animations and the View traversal all run from there.
  2. The UI thread processes state changes, does layout if something changed, and records drawing operations. Not every frame needs a full measure and layout pass. If only a drawing property such as translationX or alpha changes, the framework can usually skip layout and often reuse the existing display list. That is why animating translationX is cheaper than animating a margin, which forces layout on every frame.
  3. The UI thread hands the recorded work to RenderThread. RenderThread processes it, builds the graphics commands and submits them to the driver.
  4. The GPU executes the commands. This is asynchronous from the CPU’s point of view: submitting commands does not mean the GPU has finished them.
  5. SurfaceFlinger composes the app’s buffer with the system surfaces and presents the result. The app does not control this last stage.

Finishing View.draw() therefore does not mean the frame is on screen. Rendering, buffer availability, composition and presentation still have to happen.

Why the threads wait for each other

The two threads cannot run completely independently.

The UI thread owns the UI state, and RenderThread needs a consistent snapshot of it.

In a trace you will see this as syncFrameState: the main thread is blocked while RenderThread copies the state it needs. This is deliberate, because letting both threads touch shared state at once would produce inconsistent frames. Large bitmaps can make the sync slow, since they may have to be moved into GPU-accessible memory before they can be drawn.

RenderThread can also wait on the compositor.
When it requests a buffer or hands one back (dequeueBuffer, eglSwapBuffers), it blocks if the GPU or SurfaceFlinger is behind. None of these waits is a bug in itself. A thread that looks idle in a trace may simply be waiting for the stage in front of it, so look at the whole frame before blaming one thread.

Why a RenderThread does not prevent jank

A common assumption is that a dedicated rendering thread should make expensive UI work harmless. It does not, because RenderThread can only draw what the UI thread has already recorded. Three cases show the difference.

Expensive drawing code. Suppose onDraw() looks like this:

override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)

val points = generateComplexGeometry()

points.forEach { point ->
canvas.drawCircle(point.x, point.y, point.radius, paint)
}

}

If generateComplexGeometry() is slow, it runs on the UI thread every frame. RenderThread waits for a display list that arrives late, the GPU does almost nothing, and the frame still misses its deadline. Compute the geometry once, cache it, and recompute only when its inputs change.

Expensive draw commands. Now the UI thread records only a few commands, in well under a millisecond, but they include a large clipPath or a big Path. Recording finishes quickly and DrawFrame then runs long. Moving your own code to a worker thread changes nothing here, because the cost sits in the commands themselves. Simplify them.

A saturated GPU. Both CPU threads finish on time and the GPU is late. Large textures, overdraw and heavy shaders are the usual causes. Optimizing onDraw() will not help, because the delay comes after submission.

The symptoms on screen can be identical, so check where the time actually goes:

Moving work off the main thread helps only in the first row.

What changes with Jetpack Compose

Compose introduces new semantics for describing and updating your UI, but uses the same underlying graphics pipeline. The pipeline consists of three stages: Compose, Layout and Draw. All of these stages execute on the UI thread, and the Draw stage writes to the same render nodes. So the same split applies: expensive composition or layout delays frame preparation, expensive draw commands slow RenderThread. A busy or saturated GPU will cause the frame to be delayed after all app work has been completed.

Compose allows you to specify which stage a state read occurs in. State reads during composition will cause the composition to be re-run. The same read inside a graphicsLayer lambda only re-runs the draw phase:

val alpha = animateFloatAsState(if (visible) 1f else 0f)

Box(Modifier.graphicsLayer { this.alpha = alpha.value }) // no recomposition per frame

To determine whether the issue is caused by Compose or the overall pipeline, enable composition tracing and take a system trace.

Finding the bottleneck in a trace

Android Studio’s profiler and Perfetto can both record a system trace, which shows the main thread, RenderThread, GPU completion and frame events. A practical order of work:

  1. Reproduce the interaction that janks and record the trace.
  2. On Android 12 (API 31) and later, find the late frame in the Janky Frames track. Perfetto’s FrameTimeline compares the expected and actual presentation time, so it tells you whether the frame reached the display when it should have.
  3. Look at the main thread for a long Choreographer#doFrame, including layout and draw.
  4. Look at RenderThread for a long DrawFrame.
  5. Check when the GPU work completed.

Do not stop at the first long slice.

Long RenderThread slices do not necessarily indicate that the GPU is slow. Likewise, the GPU finishing late does not necessarily indicate that there is an issue with the UI thread.

If you want to catch regressions without manually tracing then Macrobenchmark’s FrameTimingMetric and JankStats library will automatically provide you with information about the frame timings.

The frame interval is 16.7ms at 60Hz, 11.1ms at 90Hz and 8.3ms at 120Hz.
This is the time between refreshes and doesn’t account for the time it takes to schedule. Synchronise, render and composition take part of it.
In addition, frames are pipelined. Therefore the time that a thread spends on a frame is not the amount of delay that there is between touching the screen and seeing the outcome.

Measure the whole frame lifecycle instead of treating 16 ms as a budget for your code.


Android UI thread vs. RenderThread: what actually happens when a frame is drawn was originally published in ProAndroidDev on Medium, where people are continuing the conversation by highlighting and responding to this story.