Merge pull request #90000 from melquiadess/improve-performance-of-sensor-readings

Android: Improve performance of sensor readings
This commit is contained in:
Rémi Verschelde 2024-04-04 17:09:17 +02:00
commit 1c571f991d
No known key found for this signature in database
GPG Key ID: C3336907360768E1
1 changed files with 15 additions and 20 deletions

View File

@ -85,6 +85,9 @@ class Godot(private val context: Context) : SensorEventListener {
private val TAG = Godot::class.java.simpleName 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 { private val pluginRegistry: GodotPluginRegistry by lazy {
GodotPluginRegistry.getPluginRegistry() GodotPluginRegistry.getPluginRegistry()
} }
@ -818,11 +821,8 @@ class Godot(private val context: Context) : SensorEventListener {
if (values == null || values.size != 3) { if (values == null || values.size != 3) {
return null return null
} }
val display =
(requireActivity().getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
val displayRotation = display.rotation
val rotatedValues = FloatArray(3) val rotatedValues = FloatArray(3)
when (displayRotation) { when (windowManager.defaultDisplay.rotation) {
Surface.ROTATION_0 -> { Surface.ROTATION_0 -> {
rotatedValues[0] = values[0] rotatedValues[0] = values[0]
rotatedValues[1] = values[1] rotatedValues[1] = values[1]
@ -851,40 +851,35 @@ class Godot(private val context: Context) : SensorEventListener {
if (renderView == null) { if (renderView == null) {
return return
} }
val rotatedValues = getRotatedValues(event.values)
when (event.sensor.type) { when (event.sensor.type) {
Sensor.TYPE_ACCELEROMETER -> { Sensor.TYPE_ACCELEROMETER -> {
getRotatedValues(event.values)?.let { rotatedValues -> rotatedValues?.let {
renderView?.queueOnRenderThread { renderView?.queueOnRenderThread {
GodotLib.accelerometer( GodotLib.accelerometer(-it[0], -it[1], -it[2])
-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]
)
} }
} }
} }
Sensor.TYPE_GRAVITY -> { Sensor.TYPE_GRAVITY -> {
getRotatedValues(event.values)?.let { rotatedValues -> rotatedValues?.let {
renderView?.queueOnRenderThread { renderView?.queueOnRenderThread {
GodotLib.gravity( GodotLib.gravity(-it[0], -it[1], -it[2])
-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]
)
} }
} }
} }
Sensor.TYPE_MAGNETIC_FIELD -> { Sensor.TYPE_MAGNETIC_FIELD -> {
getRotatedValues(event.values)?.let { rotatedValues -> rotatedValues?.let {
renderView?.queueOnRenderThread { renderView?.queueOnRenderThread {
GodotLib.magnetometer( GodotLib.magnetometer(-it[0], -it[1], -it[2])
-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]
)
} }
} }
} }
Sensor.TYPE_GYROSCOPE -> { Sensor.TYPE_GYROSCOPE -> {
getRotatedValues(event.values)?.let { rotatedValues -> rotatedValues?.let {
renderView?.queueOnRenderThread { renderView?.queueOnRenderThread {
GodotLib.gyroscope( GodotLib.gyroscope(it[0], it[1], it[2])
rotatedValues[0], rotatedValues[1], rotatedValues[2]
)
} }
} }
} }