From 2d03662a9ac3137c5b28a16294b0f1001da57abd Mon Sep 17 00:00:00 2001 From: Flyinghead Date: Wed, 3 Apr 2019 18:39:57 +0200 Subject: [PATCH 1/2] android: run the vibrator in a separate thread avoid lag when spamming the touchscreen controls --- .../emulator/emu/VirtualJoystickDelegate.java | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/emu/VirtualJoystickDelegate.java b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/emu/VirtualJoystickDelegate.java index 68f35545b..aeddce39b 100644 --- a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/emu/VirtualJoystickDelegate.java +++ b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/emu/VirtualJoystickDelegate.java @@ -15,7 +15,7 @@ import com.reicast.emulator.periph.InputDeviceManager; import com.reicast.emulator.periph.VJoy; public class VirtualJoystickDelegate { - private Vibrator vib; + private VibratorThread vibratorThread; private boolean editVjoyMode = false; private int selectedVjoyElement = -1; @@ -39,7 +39,10 @@ public class VirtualJoystickDelegate { public VirtualJoystickDelegate(View view) { this.view = view; this.context = view.getContext(); - vib = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); + + vibratorThread = new VibratorThread(context); + vibratorThread.start(); + readCustomVjoyValues(); scaleGestureDetector = new ScaleGestureDetector(context, new OscOnScaleGestureListener()); } @@ -224,8 +227,9 @@ public class VirtualJoystickDelegate { if (y > vjoy[j][1] && y <= (vjoy[j][1] + vjoy[j][3])) { if (vjoy[j][4] >= -2) { if (vjoy[j][5] == 0) - if (!editVjoyMode && Emulator.vibrationDuration > 0) - vib.vibrate(Emulator.vibrationDuration); + if (!editVjoyMode) { + vibratorThread.vibrate(); + } vjoy[j][5] = 2; } @@ -397,4 +401,47 @@ public class VirtualJoystickDelegate { selectedVjoyElement = -1; } } + + private class VibratorThread extends Thread + { + private Vibrator vibrator; + private boolean vibrate = false; + private boolean stopping = false; + + VibratorThread(Context context) { + vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); + } + + @Override + public void run() { + while (!stopping) { + synchronized (this) { + try { + this.wait(); + } catch (InterruptedException e) { + } + if (vibrate) { + vibrator.vibrate(Emulator.vibrationDuration); + vibrate = false; + } + } + } + } + + public void stopVibrator() { + synchronized (this) { + stopping = true; + notify(); + } + } + + public void vibrate() { + if (Emulator.vibrationDuration > 0) { + synchronized (this) { + vibrate = true; + notify(); + } + } + } + } } From cc26e255e04db5f871b80d9eaeb4c27bb5485211 Mon Sep 17 00:00:00 2001 From: Flyinghead Date: Wed, 3 Apr 2019 18:49:40 +0200 Subject: [PATCH 2/2] android: vibrate out of the synchronized section --- .../com/reicast/emulator/emu/VirtualJoystickDelegate.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/emu/VirtualJoystickDelegate.java b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/emu/VirtualJoystickDelegate.java index aeddce39b..e3e142adf 100644 --- a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/emu/VirtualJoystickDelegate.java +++ b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/emu/VirtualJoystickDelegate.java @@ -415,16 +415,20 @@ public class VirtualJoystickDelegate { @Override public void run() { while (!stopping) { + boolean doVibrate; synchronized (this) { + doVibrate = false; try { this.wait(); } catch (InterruptedException e) { } if (vibrate) { - vibrator.vibrate(Emulator.vibrationDuration); + doVibrate = true; vibrate = false; } } + if (doVibrate) + vibrator.vibrate(Emulator.vibrationDuration); } }