Eventthread distribution to vsync
As mentioned above, eventthread will distribute them to interested registrants after receiving vsync. the distribution process is completed in its thread loop threadloop function. You can also read the following section to learn about how registrants are notified about vsync and what registrants are interested in the system. Then, read back to this section.
Interested registrants first need to create a connection to eventthread. The connection to eventthread will be saved to the eventthread list. The code for creating a connection is as follows (line 56 below, see file eventthread. cpp ):
Because the connection parent class contains refbase, the overloaded onfirstref function will be called, and the latter will call the registerdisplayeventconnection function itself (that is, "connection) register to eventthread (row 62 above). That is to say, when each connection is created, the connection must be registered to eventthread so that eventthread can send a vsync Event Notification to itself. The connection may fail, so the eventthread maintains a weak pointer list for the connection. After registering a new connection, you need to notify (row 63) to loop the threadloop so that it will wake up from sleep.
The code before the threadloop function of eventthread is as follows:
In the embedded do-while loop (the above rows are 135 ~ 184). If the registrant specifies that vsync needs to be reported (that is, the count in the connection at row 146 is greater than 0), and The vsync timestamp is not 0 (row 153 ), the do-while loop is displayed, and the vsync event is ready to be distributed. If no distribution is required and the original timestamp is not 0, it indicates that vsync is still active, therefore, disable vsync (row 157). If the timestamp is 0, vsync may have been inactive, but if distribution is required, vsync (row 169) is activated ); if the screen is closed (no need to refresh the output, vsync is closed) and distribution is required, the time after sleep for a moment is used as the vsync timestamp (rows 178, 179 ), then, in the next while loop, go to the rows 153 and 160, and jump out of the while loop, indicating that the vsync event is simulated by the Software. Otherwise, there are two scenarios: screen opening and screen disabling, but no distribution is required, at this time, the system enters the sleep waiting state (row 182 ). When the screen is turned on, vsync will soon arrive (unless vsync is disabled), or new interested parties register, or the Registrar request changes the vsync Distribution frequency (see the next section), which will wake up the sleep wait at 182 of the row. When the screen is closed and vsync is not distributed, the eventthread thread can be sleep without waking up until the screen is awakened and the do-while loop starts again.
After jumping out of the first nested loop, it indicates that vsync needs to distribute the report. In this case, add the counter to 1 (the following row is 187) and record the timestamp information (row 188 ); then, in the for loop (row 192 ~ 215) check whether the registrant has expired (row 196), Report Frequency (whether to report to a registrant this time, row 200, 201), and so on. If you need to report, add the registrant to the displayeventconnections list (row 213 ). In row 216, if the list to be reported is empty, continue the while loop. Otherwise, execute the command to distribute vsync Event Notifications.
The following code is the code after threadloop. First, prepare the displayevent event (currently only vsync), including the type, timestamp, and other information (row 219 ~ 221 ). Then, in the for loop (row 224 ~ 246), send displayevent (vsync) Events (row 228) one by one to connections in the queue to be processed ). This completes the distribution of registrants of interest.
It can be seen that threadloop first determines whether to distribute the next vsync Based on the registrant and the device (whether the screen is closed). In the absence of distribution (such as when the screen is closed and there is no receiver) or wait until the next hardware or software simulation vsync arrives after the task is completed, it enters the sleep waiting state; when it is awakened (such as the arrival of vsync), if you need to report distribution, add the receiver to the queue list to be processed, and distribute the event to the registered receiver through the postevent function of the connection.
This article is excerpted from the book "in-depth analysis of Android system ".
Yang Changgang
Published by Electronic Industry Publishing House