High CPU Usage on Touch Events
From Android Game Development Wiki
Contents |
[edit] The Problem
After the Touch Event Flooding problem was reported and supposedly fixed in Android version 2.0 many people still had issues with high CPU usage while the touch screen was touched, munching away on their frame rates. The problem was first reported in this [1] thread on the Android developer group. A subsequent thread on the Android platform group was then posted where a Google engineer shed some light on the issue.
The problem can not be solved by inserting a Thread.sleep() in the Java side touch event handler as claimed in various places. While no detailed explanation of the problem is available it is suspected to be a combination of driver and kernel issues. Christopher Tate, Google engineer, remarked that he suspects that the touch panel driver is firing too many events, and while they might not reach the Java side due to a Thread.sleep() they still have to be processed.
Changing the priority of the game/rendering thread also does not solve the problem.
Finally a bug was filed on the Android bug tracker.
The problem is especially bad on first generation devices like the G1, Dream or Hero where a rendering-bound game running perfectly fine at 30fps can drop to 12fps when the screen is being touched. On newer devices the problem is less pronounced due to higher CPU clock speeds an other factors. It can however still impact the frame rate of a game or real-time application.
The problem seems to have been somewhat solved in Android version 2.2 (Froyo). However, first generation devices which suffer the most from this issue are not likely to receive an update to Froyo.
A complete rewrite of the responsible system parts with improved performance is expected to be available in Android version 3.0 (Gingerbread) according to the bug tracker.
The following video illustrates the problem:
[edit] The Solution
There is no known solution to this problem. Setting the Java thread priority via Thread.currentThread().setPriority(Thread.MAX_PRIORITY) does give you a little bit more performance but the effect is still noticeable at lower framerates (e.g. 30fps).
[edit] Sample Code
The Android Game Development Wiki sample project includes a simple example at http://code.google.com/p/agd-wiki-samples/source/browse/trunk/src/com/badlogic/agdwikisamples/TouchCpuUsageSample.java that demonstrates the issue. On first generation devices the effect is most pronounced eating up nearly 70% of CPU cycles when dragging a finger across the screen. On newer devices the effect is less extreme, coming in at 30-35% CPU usage on a Droid running 2.0.1 for example. Sleeping in the touch handler does help a tiny bit but will not work with GPU intensive scenes. Note that the CPU usage is meassured every second via /proc/stat so the readings might be a bit erratic. The overall tendency is clearly visible though.
[edit] See Also
[edit] External Links
Initial thread on the Android developer group
Follow up thread on the Android platform group with responses from Google engineers