Skip to content

Commit f09d033

Browse files
committed
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.
1 parent 7a42afb commit f09d033

File tree

1 file changed

+15
-20
lines changed
  • platform/android/java/lib/src/org/godotengine/godot

1 file changed

+15
-20
lines changed

platform/android/java/lib/src/org/godotengine/godot/Godot.kt

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class Godot(private val context: Context) : SensorEventListener {
8585
private val TAG = Godot::class.java.simpleName
8686
}
8787

88+
private val windowManager: WindowManager by lazy {
89+
requireActivity().getSystemService(Context.WINDOW_SERVICE) as WindowManager
90+
}
8891
private val pluginRegistry: GodotPluginRegistry by lazy {
8992
GodotPluginRegistry.getPluginRegistry()
9093
}
@@ -818,11 +821,8 @@ class Godot(private val context: Context) : SensorEventListener {
818821
if (values == null || values.size != 3) {
819822
return null
820823
}
821-
val display =
822-
(requireActivity().getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
823-
val displayRotation = display.rotation
824824
val rotatedValues = FloatArray(3)
825-
when (displayRotation) {
825+
when (windowManager.defaultDisplay.rotation) {
826826
Surface.ROTATION_0 -> {
827827
rotatedValues[0] = values[0]
828828
rotatedValues[1] = values[1]
@@ -851,40 +851,35 @@ class Godot(private val context: Context) : SensorEventListener {
851851
if (renderView == null) {
852852
return
853853
}
854+
855+
val rotatedValues = getRotatedValues(event.values)
856+
854857
when (event.sensor.type) {
855858
Sensor.TYPE_ACCELEROMETER -> {
856-
getRotatedValues(event.values)?.let { rotatedValues ->
859+
rotatedValues?.let {
857860
renderView?.queueOnRenderThread {
858-
GodotLib.accelerometer(
859-
-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]
860-
)
861+
GodotLib.accelerometer(-it[0], -it[1], -it[2])
861862
}
862863
}
863864
}
864865
Sensor.TYPE_GRAVITY -> {
865-
getRotatedValues(event.values)?.let { rotatedValues ->
866+
rotatedValues?.let {
866867
renderView?.queueOnRenderThread {
867-
GodotLib.gravity(
868-
-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]
869-
)
868+
GodotLib.gravity(-it[0], -it[1], -it[2])
870869
}
871870
}
872871
}
873872
Sensor.TYPE_MAGNETIC_FIELD -> {
874-
getRotatedValues(event.values)?.let { rotatedValues ->
873+
rotatedValues?.let {
875874
renderView?.queueOnRenderThread {
876-
GodotLib.magnetometer(
877-
-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]
878-
)
875+
GodotLib.magnetometer(-it[0], -it[1], -it[2])
879876
}
880877
}
881878
}
882879
Sensor.TYPE_GYROSCOPE -> {
883-
getRotatedValues(event.values)?.let { rotatedValues ->
880+
rotatedValues?.let {
884881
renderView?.queueOnRenderThread {
885-
GodotLib.gyroscope(
886-
rotatedValues[0], rotatedValues[1], rotatedValues[2]
887-
)
882+
GodotLib.gyroscope(it[0], it[1], it[2])
888883
}
889884
}
890885
}

0 commit comments

Comments
 (0)