Add some infrastructure to be able to support Android hardware scaling

This commit is contained in:
Henrik Rydgard committed 2014-07-18 20:31:29 +02:00
1 parent 326504baf8
commit 1012f46dee
4 files changed
+89 -22

No files matched your search

@@ -96,6 +96,10 @@ public class NativeActivity extends Activity {
return true;
}
NativeRenderer getRenderer() {
return nativeRenderer;
}
@TargetApi(17)
private void detectOptimalAudioSettings() {
try {
@@ -128,16 +132,29 @@ public class NativeActivity extends Activity {
return libdir;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
void GetScreenSizeHC(Point size) {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
void GetScreenSizeJB(Point size, boolean real) {
WindowManager w = getWindowManager();
w.getDefaultDisplay().getSize(size);
if (real) {
w.getDefaultDisplay().getRealSize(size);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
void GetScreenSizeHC(Point size, boolean real) {
WindowManager w = getWindowManager();
if (real && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
GetScreenSizeJB(size, real);
} else {
w.getDefaultDisplay().getSize(size);
}
}
@SuppressWarnings("deprecation")
void GetScreenSize(Point size) {
public void GetScreenSize(Point size) {
boolean real = useImmersive();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
GetScreenSizeHC(size);
GetScreenSizeHC(size, real);
} else {
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
@@ -185,7 +202,6 @@ public class NativeActivity extends Activity {
String libraryDir = getApplicationLibraryDir(appInfo);
File sdcard = Environment.getExternalStorageDirectory();
@SuppressWarnings("deprecation")
String externalStorageDir = sdcard.getAbsolutePath();
String dataDir = this.getFilesDir().getAbsolutePath();
@@ -251,17 +267,19 @@ public class NativeActivity extends Activity {
}
}
private boolean useImmersive() {
String immersive = NativeApp.queryConfig("immersiveMode");
return immersive.equals("1") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}
@SuppressLint("InlinedApi")
@TargetApi(14)
private void updateSystemUiVisibility() {
String immersive = NativeApp.queryConfig("immersiveMode");
boolean useImmersive = immersive.equals("1") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
int flags = 0;
if (useLowProfileButtons()) {
flags |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
}
if (useImmersive) {
if (useImmersive()) {
flags |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
if (getWindow().getDecorView() != null) {
@@ -280,7 +298,16 @@ public class NativeActivity extends Activity {
}
}
}
// Override this to scale the backbuffer (use the Android hardware scaler)
private void getDesiredBackbufferSize(Point sz) {
// GetScreenSize(sz, useImmersive());
// sz.x /= 2;
// sz.y /= 2;
sz.x = 0;
sz.y = 0;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -307,6 +334,12 @@ public class NativeActivity extends Activity {
mGLSurfaceView = new NativeGLView(this);
nativeRenderer = new NativeRenderer(this);
Point sz = new Point();
getDesiredBackbufferSize(sz);
if (sz.x > 0) {
// Auto-calculates new DPI and forwards to the correct call on mGLSurfaceView.getHolder()
nativeRenderer.setFixedSize(sz.x, sz.y, mGLSurfaceView);
}
mGLSurfaceView.setEGLContextClientVersion(2);
// Setup the GLSurface and ask android for the correct
@@ -356,7 +389,7 @@ public class NativeActivity extends Activity {
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop - do nothing, just let Android switch away");
Log.i(TAG, "onStop - do nothing special");
}
@Override
@@ -393,11 +426,8 @@ public class NativeActivity extends Activity {
if (audioPlayer != null) {
audioPlayer.stop();
}
Log.i(TAG, "nativeapp pause");
NativeApp.pause();
Log.i(TAG, "gl pause");
mGLSurfaceView.onPause();
Log.i(TAG, "onPause returning");
}
@Override
@@ -431,6 +461,12 @@ public class NativeActivity extends Activity {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
updateSystemUiVisibility();
}
Point sz = new Point();
getDesiredBackbufferSize(sz);
if (sz.x > 0) {
mGLSurfaceView.getHolder().setFixedSize(sz.x/2, sz.y/2);
}
}
// We simply grab the first input device to produce an event and ignore all others that are connected.
@@ -1,5 +1,8 @@
package com.henrikrydgard.libnative;
// Note that the display* methods are in NativeRenderer.java
public class NativeApp {
public final static int DEVICE_ID_DEFAULT = 0;
public final static int DEVICE_ID_KEYBOARD = 1;
@@ -22,6 +22,7 @@ public class NativeGLView extends GLSurfaceView implements SensorEventListener,
private static String TAG = "NativeGLView";
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private NativeActivity mActivity;
// Moga controller
private Controller mController = null;
@@ -29,6 +30,7 @@ public class NativeGLView extends GLSurfaceView implements SensorEventListener,
public NativeGLView(NativeActivity activity) {
super(activity);
mActivity = activity;
/*
if (Build.VERSION.SDK_INT >= 11) {
@@ -73,6 +75,8 @@ public class NativeGLView extends GLSurfaceView implements SensorEventListener,
boolean canReadToolType = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
int numTouchesHandled = 0;
float scaleX = (float)mActivity.getRenderer().getDpiScaleX();
float scaleY = (float)mActivity.getRenderer().getDpiScaleY();
for (int i = 0; i < ev.getPointerCount(); i++) {
int pid = ev.getPointerId(i);
int code = 0;
@@ -104,7 +108,7 @@ public class NativeGLView extends GLSurfaceView implements SensorEventListener,
code |= tool << 10; // We use the Android tool type codes
}
// Can't use || due to short circuit evaluation
numTouchesHandled += NativeApp.touch(ev.getX(i), ev.getY(i), code, pid) ? 1 : 0;
numTouchesHandled += NativeApp.touch(scaleX * ev.getX(i), scaleY * ev.getY(i), code, pid) ? 1 : 0;
}
}
return numTouchesHandled > 0;
@@ -4,6 +4,7 @@ import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Point;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.DisplayMetrics;
@@ -17,23 +18,37 @@ public class NativeRenderer implements GLSurfaceView.Renderer {
private int dpi;
private float refreshRate;
private double dpi_scale_x;
private double dpi_scale_y;
int last_width, last_height;
NativeRenderer(NativeActivity act) {
mActivity = act;
DisplayMetrics metrics = new DisplayMetrics();
Display display = act.getWindowManager().getDefaultDisplay();
display.getMetrics(metrics);
dpi = metrics.densityDpi;
refreshRate = display.getRefreshRate();
// Log.i(TAG, "Display name: " + display.getName());
Log.i(TAG, " rate: " + refreshRate + " dpi: " + dpi);
refreshRate = display.getRefreshRate();
}
double getDpiScaleX() {
return dpi_scale_x;
}
double getDpiScaleY() {
return dpi_scale_y;
}
public void setDark(boolean d) {
isDark = d;
}
public void setFixedSize(int xres, int yres, GLSurfaceView surfaceView) {
Log.i(TAG, "Setting surface to fixed size " + xres + "x" + yres);
surfaceView.getHolder().setFixedSize(xres, yres);
}
@Override
public void onDrawFrame(GL10 unused /*use GLES20*/) {
if (isDark) {
@@ -54,8 +69,17 @@ public class NativeRenderer implements GLSurfaceView.Renderer {
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
Log.i(TAG, "onSurfaceChanged");
displayResize(width, height, dpi, refreshRate);
Point sz = new Point();
mActivity.GetScreenSize(sz);
double actualW = sz.x;
double actualH = sz.y;
dpi_scale_x = ((double)width / (double)actualW);
dpi_scale_y = ((double)height / (double)actualH);
Log.i(TAG, "onSurfaceChanged: " + dpi_scale_x + "x" + dpi_scale_y + " (width=" + width + ", actualW=" + actualW);
int scaled_dpi = (int)((double)dpi * dpi_scale_x);
displayResize(width, height, scaled_dpi, refreshRate);
last_width = width;
last_height = height;
}
// Not override, it's custom.