Timing issue between GPU rendering thread and hwcomposer
I am suspect that there have timing issue between GPU rendering thread and drm hwcomposer.
Please see below two pictures. We can see SurfaceFlinger will periodically run and AsyncWaitForBufferSwap() will be executed with the same rhythm with SurfaceFlinger, this is same for both two pictures.
The question is for the render thread, it might have different time delta with SurfaceFlinger, in the first picture, renderThread needs to wait for the HWC to release fence, but we can see it needs to wait for about ~10ms until AsyncWaitForBufferSwap return back, from my understanding, this means low level (display driver?) has swapped the buffer and released fence, then the render thread can proceed for its task. But this means it will leave much less time for the frame rendering since most time are spent for waiting for fence, thus it's easily to cause janks.
If we compare to picture 2, the render thread is just coincidence with SurfaceFlinger (and AsyncWaitForBufferSwap), so the render thread doesn't need to wait and directly acquires the buffer fence, in this case, the frame rendering can be finished in short time.
So seem to me, the problem is caused by AsyncWaitForBufferSwap; if it waits for long time for buffer swap and the fence is released, then it will leave much smaller time window for GPU rendering.
Another question is: which component will notify and wake up AsyncWaitForBufferSwap()? I tried to add ATRACE log in the function HwcDisplay::GetReleaseFences(), but cannot prove that HwcDisplay::GetReleaseFences() is the module which notify AsyncWaitForBufferSwap() to run.