Keep screen info in cache

Currently, we only use screen information (width, height, rotation)
once at initialization, to send the device size to the client.

To be able to scale mouse events, make it accessible in memory. For this
purpose, replace the "static" DeviceUtil to a singleton Device, and
update it on every screen rotation.
This commit is contained in:
Romain Vimont 2018-01-23 14:37:54 +01:00
parent d7b00a9bab
commit 11a60e5767
6 changed files with 86 additions and 44 deletions

View file

@ -23,7 +23,7 @@ SRC := com/genymobile/scrcpy/ScrCpyServer.java \
com/genymobile/scrcpy/ControlEvent.java \
com/genymobile/scrcpy/ControlEventReader.java \
com/genymobile/scrcpy/DesktopConnection.java \
com/genymobile/scrcpy/DeviceUtil.java \
com/genymobile/scrcpy/Device.java \
com/genymobile/scrcpy/EventController.java \
com/genymobile/scrcpy/ScreenInfo.java \
com/genymobile/scrcpy/ScreenStreamer.java \

View file

@ -0,0 +1,70 @@
package com.genymobile.scrcpy;
import android.os.Build;
import android.os.RemoteException;
import android.view.IRotationWatcher;
import com.genymobile.scrcpy.wrappers.InputManager;
import com.genymobile.scrcpy.wrappers.ServiceManager;
public class Device {
public interface RotationListener {
void onRotationChanged(int rotation);
}
private static final Device INSTANCE = new Device();
private final ServiceManager serviceManager = new ServiceManager();
private ScreenInfo screenInfo;
private RotationListener rotationListener;
private Device() {
screenInfo = readScreenInfo();
registerRotationWatcher(new IRotationWatcher.Stub() {
@Override
public void onRotationChanged(int rotation) throws RemoteException {
synchronized (Device.this) {
// update screenInfo cache
screenInfo = screenInfo.withRotation(rotation);
// notify
if (rotationListener != null) {
rotationListener.onRotationChanged(rotation);
}
}
}
});
}
public static Device getInstance() {
return INSTANCE;
}
public synchronized ScreenInfo getScreenInfo() {
if (screenInfo == null) {
screenInfo = readScreenInfo();
}
return screenInfo;
}
private ScreenInfo readScreenInfo() {
return serviceManager.getDisplayManager().getScreenInfo();
}
public static String getDeviceName() {
return Build.MODEL;
}
public InputManager getInputManager() {
return serviceManager.getInputManager();
}
public void registerRotationWatcher(IRotationWatcher rotationWatcher) {
serviceManager.getWindowManager().registerRotationWatcher(rotationWatcher);
}
public synchronized void setRotationListener(RotationListener rotationListener) {
this.rotationListener = rotationListener;
}
}

View file

@ -1,28 +0,0 @@
package com.genymobile.scrcpy;
import android.os.Build;
import android.view.IRotationWatcher;
import com.genymobile.scrcpy.wrappers.InputManager;
import com.genymobile.scrcpy.wrappers.ServiceManager;
public class DeviceUtil {
private static final ServiceManager serviceManager = new ServiceManager();
public static ScreenInfo getScreenInfo() {
return serviceManager.getDisplayManager().getScreenInfo();
}
public static void registerRotationWatcher(IRotationWatcher rotationWatcher) {
serviceManager.getWindowManager().registerRotationWatcher(rotationWatcher);
}
public static String getDeviceName() {
return Build.MODEL;
}
public static InputManager getInputManager() {
return serviceManager.getInputManager();
}
}

View file

@ -24,7 +24,7 @@ public class EventController {
public EventController(DesktopConnection connection) {
this.connection = connection;
inputManager = DeviceUtil.getInputManager();
inputManager = Device.getInstance().getInputManager();
initPointer();
}

View file

@ -7,16 +7,25 @@ public class ScrCpyServer {
private static final String TAG = "scrcpy";
private static void scrcpy() throws IOException {
String deviceName = DeviceUtil.getDeviceName();
ScreenInfo initialScreenInfo = DeviceUtil.getScreenInfo();
String deviceName = Device.getDeviceName();
ScreenInfo initialScreenInfo = Device.getInstance().getScreenInfo();
int width = initialScreenInfo.getLogicalWidth();
int height = initialScreenInfo.getLogicalHeight();
try (DesktopConnection connection = DesktopConnection.open(deviceName, width, height)) {
final ScreenStreamer streamer = new ScreenStreamer(connection);
Device.getInstance().setRotationListener(new Device.RotationListener() {
@Override
public void onRotationChanged(int rotation) {
streamer.reset();
}
});
// asynchronous
startEventController(connection);
try {
// asynchronous
startEventController(connection);
// synchronous
new ScreenStreamer(connection).streamScreen();
streamer.streamScreen();
} catch (IOException e) {
Ln.e("Screen streaming interrupted", e);
}

View file

@ -1,8 +1,5 @@
package com.genymobile.scrcpy;
import android.os.RemoteException;
import android.view.IRotationWatcher;
import java.io.IOException;
import java.io.InterruptedIOException;
@ -13,12 +10,6 @@ public class ScreenStreamer {
public ScreenStreamer(DesktopConnection connection) {
this.connection = connection;
DeviceUtil.registerRotationWatcher(new IRotationWatcher.Stub() {
@Override
public void onRotationChanged(int rotation) throws RemoteException {
reset();
}
});
}
private synchronized ScreenStreamerSession newScreenStreamerSession() {