Receiving vsync by eventthread
Eventthread is designed to receive vsync Event Notifications and distribute vsync notifications to every registrant interested in the system. Vsync is reported from the underlying hardware driver. for Android interfaces, vsync is an abstract hardware device from the hwc_composer_device on the Hal layer. If the hardware is not supported, hwcomposer creates a thread to simulate the generation of vsync Event Notifications. Once working, vsync will keep notifying the eventthread thread in surfaceflinger like a pulse, which will then be distributed to interested registrants.
Let's take a look at when eventthread was created and started to run.
When the surfaceflinger class is created, its onfirstref function will be called (because it is ultimately inherited from refbase). The Code is as follows:
Because surfaceflinger inherits the Thread class thread (row 130 above) and calls the run function, it means to create a new thread for execution. The thread: Run will create a new thread named surfaceflinger (we will call it surfaceflinger). The thread entry function thread :: _ threadloop will call the threadloop function that is overloaded by the subclass. However, before calling the overloaded threadloop, call the readytorun function that is overloaded by the subclass (only for the first time ). That is to say, the run function call here will start a new thread surfaceflinger, And the readytorun function of surfaceflinger will be called in it to perform initialization, as mentioned above, allocate a shared control block surface_flinger_cblk_t from ashm. For details, see section 11.5.2 above.
After the readytorun function of surfaceflinger, an eventthread (row 300 below) will be created ):
It is required to start execution in onfirstref of its eventthread. See row 52 (see file eventthread. cpp ):
In this way, eventthead starts its thread loop immediately after it is created.
Let's take a look at how the Hal layer sends vsync notifications to eventthread.
In the Hal-layer file Hardware/libhardware/include/hwcomposer. H, a callback routine hwc_procs struct is defined for the underlying calling. It contains two routine functions: invalidate and vsync. The Code is as follows (see the hwcomposer. h file ):
Two struct types are defined in hwcomposer. h of Android: callback callbacks and callback context cb_context. Callbacks inherits from the above hwc_procs. The Code is as follows:
In the hwcomposer constructor, parse the symbols from the library, and then open the hwcomposer hardware module (line 00063 below ), based on its open function, open Hal abstract hardware device mhwc (line 00066 below), and then judge whether the hardware device mhwc supports the registerprocs function (line 00070 below ). If the hardware platform vendor implements this function and assigns it to the registerprocs member of Hal abstract hardware device hwc_composer_device in the open function, the member is not empty, the preceding callback routine hwc_procs is supported. Based on this, the hwcomposer constructor determines whether vsync is supported. The Code is as follows (see the file hwcomposer. cpp ):
When the hardware platform supports vsync, the above line 00072 ~ The value assignment of 00073 and the registration callback routine of row 00074 come from the call of the underlying Hal and will be transferred to the call of the hook_xxx hook function. The vsync In the callback routine hwc_procs points to the hook_vsync hook function. The code for the callback is as follows (see the hwcomposer. cpp file ):
It also calls the following vsync member function (see the file hwcomposer. cpp ):
The meventhandler of line 00125 above is actually a displayhardware object. In hwcomposer, an abstract interface eventhandler is defined and an eventhandler type object reference is maintained. displayhardware implements this interface and implements the init function of displayhardware, create an hwcomposer object and assign it to the eventhandler object reference maintained by hwcomposer (that is, meventhandler, see row 00048 of the hwcomposer constructor above ). Therefore, the call to vsync calls the onvsyncreceived function of displayhardware. The code for the latter is as follows (see the displayhardware. cpp file ):
This will continue to call a handler maintained in displayhardware, which is actually an eventthread object (see eventthread: onfirstref function, specified in this function ). Therefore, the call to vsync actually calls the onvsyncreceived member function of eventthread below (see eventthread. cpp ):
This will wake up the sleep wait of the loop function threadloop of the eventthread thread on the condition.
The threadloop function of eventthread is mainly used to distribute vsync Event Notifications to registrants interested in vsync in the system. It will be described in detail in the next section.
For a hardware platform that does not support reporting notifications by vsync, hwcomposer creates a vsyncthread to simulate vsync sending. After the hwcomposer constructor, it is determined that a vsync thread needs to be simulated and the thread is created (the following line is 92). The Code is as follows:
Part of the Code after threadloop, the thread loop function of vsyncthread, is as follows (see file hwcomposer. cpp ):
The above row 318 sleep for a moment. After waking up, It is simulated to send a vsync Event Notification (row 322 ). The sleep time is determined by the update rate variable mrefreshperiod in hwcomposer. The value is from framebuffer.
Device or system setting information. mrefreshperiod is assigned a value in the hwcomposer constructor. For details, see the code line 55 of the previous constructor. As shown in 11-21, vsync originated from ①. After calling it step by step and executing it to ②, wake up the thread that may be sleeping and distribute it by the thread loop function. From ① To ② is described in this section, and ② is described in the next section.
Figure 11 vsync received and distributed by eventthread
This article is excerpted from the book "in-depth analysis of Android system ".
Yang Changgang
Published by Electronic Industry Publishing House