summaryrefslogtreecommitdiffstats
path: root/platform/android/java
diff options
context:
space:
mode:
authormelquiadess <31paths@gmail.com>2024-03-28 13:34:04 +0000
committermelquiadess <31paths@gmail.com>2024-04-04 14:17:28 +0100
commitf09d0331562a91d930170109d849a7bd0ea3e1b3 (patch)
tree4851371ac380fa230c73b8ef2d0512577fd86f8e /platform/android/java
parent7a42afbba050822a66ea5b4c620f6db1b98c49af (diff)
downloadredot-engine-f09d0331562a91d930170109d849a7bd0ea3e1b3.tar.gz
Improve performance by extracting windowManager and getRotatedValues()
Once sensor listeners are registered, onSensorChanged() (and subsequently getRotatedValues()) gets called multiple times per socond. Obtaining WindowManager on each of those calls is superfluous and can be avoided by extracting it to a lazy class val. getRotatedValue() can also be called before checking sensor type, and used for each one of them, resulting in less code repetition.
Diffstat (limited to 'platform/android/java')
-rw-r--r--platform/android/java/lib/src/org/godotengine/godot/Godot.kt35
1 files changed, 15 insertions, 20 deletions
diff --git a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt
index 250680a25b..ce53aeebcb 100644
--- a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt
+++ b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt
@@ -85,6 +85,9 @@ class Godot(private val context: Context) : SensorEventListener {
private val TAG = Godot::class.java.simpleName
}
+ private val windowManager: WindowManager by lazy {
+ requireActivity().getSystemService(Context.WINDOW_SERVICE) as WindowManager
+ }
private val pluginRegistry: GodotPluginRegistry by lazy {
GodotPluginRegistry.getPluginRegistry()
}
@@ -818,11 +821,8 @@ class Godot(private val context: Context) : SensorEventListener {
if (values == null || values.size != 3) {
return null
}
- val display =
- (requireActivity().getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
- val displayRotation = display.rotation
val rotatedValues = FloatArray(3)
- when (displayRotation) {
+ when (windowManager.defaultDisplay.rotation) {
Surface.ROTATION_0 -> {
rotatedValues[0] = values[0]
rotatedValues[1] = values[1]
@@ -851,40 +851,35 @@ class Godot(private val context: Context) : SensorEventListener {
if (renderView == null) {
return
}
+
+ val rotatedValues = getRotatedValues(event.values)
+
when (event.sensor.type) {
Sensor.TYPE_ACCELEROMETER -> {
- getRotatedValues(event.values)?.let { rotatedValues ->
+ rotatedValues?.let {
renderView?.queueOnRenderThread {
- GodotLib.accelerometer(
- -rotatedValues[0], -rotatedValues[1], -rotatedValues[2]
- )
+ GodotLib.accelerometer(-it[0], -it[1], -it[2])
}
}
}
Sensor.TYPE_GRAVITY -> {
- getRotatedValues(event.values)?.let { rotatedValues ->
+ rotatedValues?.let {
renderView?.queueOnRenderThread {
- GodotLib.gravity(
- -rotatedValues[0], -rotatedValues[1], -rotatedValues[2]
- )
+ GodotLib.gravity(-it[0], -it[1], -it[2])
}
}
}
Sensor.TYPE_MAGNETIC_FIELD -> {
- getRotatedValues(event.values)?.let { rotatedValues ->
+ rotatedValues?.let {
renderView?.queueOnRenderThread {
- GodotLib.magnetometer(
- -rotatedValues[0], -rotatedValues[1], -rotatedValues[2]
- )
+ GodotLib.magnetometer(-it[0], -it[1], -it[2])
}
}
}
Sensor.TYPE_GYROSCOPE -> {
- getRotatedValues(event.values)?.let { rotatedValues ->
+ rotatedValues?.let {
renderView?.queueOnRenderThread {
- GodotLib.gyroscope(
- rotatedValues[0], rotatedValues[1], rotatedValues[2]
- )
+ GodotLib.gyroscope(it[0], it[1], it[2])
}
}
}