Permalink
Browse files

Consolidate DPI/sizing handling on Android

  • Loading branch information...
hrydgard committed Aug 7, 2017
1 parent 43de028 commit b87ae5d7db9a3b36b4e542947158938156bf0adc
@@ -103,6 +103,8 @@
float densityDpi;
float refreshRate;
int pixelWidth;
int pixelHeight;
// Functions for the app activity to override to change behaviour.
@@ -149,58 +151,6 @@ String getApplicationLibraryDir(ApplicationInfo application) {
return libdir;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
void GetScreenSizeJB(Point size, boolean real) {
WindowManager w = getWindowManager();
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);
}
}
private View GetSurfaceView() {
if (mGLSurfaceView != null)
return mGLSurfaceView;
if (mSurfaceView != null)
return mSurfaceView;
return null;
}
@SuppressWarnings("deprecation")
public void GetScreenSize(Point size) {
Rect rc = new Rect();
boolean real = useImmersive();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
GetScreenSizeHC(size, real);
} else {
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
size.x = d.getWidth();
size.y = d.getHeight();
}
Log.i(TAG, "Size old way: " + size.x + "x" + size.y);
View surface = GetSurfaceView();
if (surface != null) {
surface.getWindowVisibleDisplayFrame(rc);
size.x = rc.width();
size.y = rc.height();
Log.i(TAG, "Size new way: " + size.x + "x" + size.y);
}
}
public static final int REQUEST_CODE_STORAGE_PERMISSION = 1337;
@TargetApi(23)
@@ -444,21 +394,11 @@ public void onCreate(Bundle savedInstanceState) {
gainAudioFocus(this.audioManager, this.audioFocusChangeListener);
NativeApp.audioInit();
Point outSize = new Point();
GetScreenSize(outSize);
NativeApp.setDisplayParameters(outSize.x, outSize.y, (int)densityDpi, refreshRate);
if (javaGL) {
mGLSurfaceView = new NativeGLView(this);
nativeRenderer = new NativeRenderer(this);
Point sz = new Point();
getDesiredBackbufferSize(sz);
if (sz.x > 0) {
Log.i(TAG, "Requesting fixed size buffer: " + sz.x + "x" + sz.y);
// Auto-calculates new DPI and forwards to the correct call on mGLSurfaceView.getHolder()
nativeRenderer.setFixedSize(sz.x, sz.y, mGLSurfaceView);
}
mGLSurfaceView.setEGLContextClientVersion(2);
mGLSurfaceView.setEGLContextClientVersion(2);
mGLSurfaceView.getHolder().addCallback(NativeActivity.this);
// Setup the GLSurface and ask android for the correct
// Number of bits for r, g, b, a, depth and stencil components
@@ -480,8 +420,7 @@ public void onCreate(Bundle savedInstanceState) {
mGLSurfaceView.setEGLConfigChooser(new NativeEGLConfigChooser());
}
// Tried to mess around with config choosers here but fail completely on Xperia Play.
mGLSurfaceView.setRenderer(nativeRenderer);
mGLSurfaceView.setRenderer(nativeRenderer);
setContentView(mGLSurfaceView);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
@@ -491,48 +430,45 @@ public void onCreate(Bundle savedInstanceState) {
}
}
NativeApp.computeDesiredBackbufferDimensions();
int bbW = NativeApp.getDesiredBackbufferWidth();
int bbH = NativeApp.getDesiredBackbufferHeight();
mSurfaceView = new NativeSurfaceView(NativeActivity.this, bbW, bbH);
mSurfaceView = new NativeSurfaceView(NativeActivity.this);
mSurfaceView.getHolder().addCallback(NativeActivity.this);
Log.i(TAG, "setcontentview before");
setContentView(mSurfaceView);
Log.i(TAG, "setcontentview after");
ensureRenderLoop();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, "Surface created.");
pixelWidth = holder.getSurfaceFrame().width();
pixelHeight = holder.getSurfaceFrame().height();
Log.d(TAG, "Surface created. pixelWidth=" + pixelWidth + ", pixelHeight=" + pixelHeight);
NativeApp.setDisplayParameters(pixelWidth, pixelHeight, (int)densityDpi, refreshRate);
Point sz = new Point();
getDesiredBackbufferSize(sz);
Log.d(TAG, "Setting fixed size " + sz.x + " x " + sz.y);
if (mGLSurfaceView != null) {
mGLSurfaceView.getHolder().setFixedSize(sz.x, sz.y);
} else {
mSurfaceView.getHolder().setFixedSize(sz.x, sz.y);
}
}
//
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (javaGL) {
Log.e(TAG, "JavaGL - should not get into surfaceChanged.");
return;
}
Log.w(TAG, "Surface changed. Resolution: " + width + "x" + height + " Format: " + format);
Point outSize = new Point();
GetScreenSize(outSize);
NativeApp.setDisplayParameters(outSize.x, outSize.y, (int)densityDpi, refreshRate);
NativeApp.backbufferResize(width, height, format);
mSurface = holder.getSurface();
// If we got a surface, this starts the thread. If not, it doesn't.
if (mSurface == null) {
joinRenderLoopThread();
} else {
ensureRenderLoop();
if (!javaGL) {
// If we got a surface, this starts the thread. If not, it doesn't.
if (mSurface == null) {
joinRenderLoopThread();
} else {
ensureRenderLoop();
}
}
}
@@ -697,16 +633,6 @@ public void onConfigurationChanged(Configuration newConfig) {
updateSystemUiVisibility();
}
densityDpi = (float)newConfig.densityDpi;
Point outSize = new Point();
GetScreenSize(outSize);
NativeApp.setDisplayParameters(outSize.x, outSize.y, (int)densityDpi, refreshRate);
if (javaGL) {
Point sz = new Point();
getDesiredBackbufferSize(sz);
if (sz.x > 0) {
mGLSurfaceView.getHolder().setFixedSize(sz.x, sz.y);
}
}
}
//keep this static so we can call this even if we don't
@@ -44,9 +44,6 @@
public static native boolean mouseWheelEvent(float x, float y);
// will only be called between init() and shutdown()
public static native int audioRender(short[] buffer);
// Sensor/input data. These are asynchronous, beware!
public static native boolean touch(float x, float y, int data, int pointerId);
@@ -13,77 +13,27 @@
public class NativeRenderer implements GLSurfaceView.Renderer {
private static String TAG = "NativeRenderer";
private NativeActivity mActivity;
private boolean isDark = false;
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();
}
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);
}
public void onDrawFrame(GL10 unused /*use GLES20*/) {
if (isDark) {
GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_STENCIL_BUFFER_BIT);
} else {
displayRender();
}
displayRender();
}
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
Log.i(TAG, "onSurfaceCreated");
Log.i(TAG, "NativeRenderer: onSurfaceCreated");
// Log.i(TAG, "onSurfaceCreated - EGL context is new or was lost");
// Actually, it seems that it is here we should recreate lost GL objects.
displayInit();
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
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: Scale: " + 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;
}
// NATIVE METHODS
// Note: This also means "device lost" and you should reload
// all buffered objects.
public native void displayInit();
public native void displayResize(int w, int h, int dpi, float refreshRate);
public native void displayRender();
}
@@ -30,7 +30,7 @@
private Controller mController = null;
private boolean isMogaPro = false;
public NativeSurfaceView(NativeActivity activity, int fixedW, int fixedH) {
public NativeSurfaceView(NativeActivity activity) {
super(activity);
Log.i(TAG, "NativeSurfaceView");
@@ -40,14 +40,6 @@ public NativeSurfaceView(NativeActivity activity, int fixedW, int fixedH) {
mController = Controller.getInstance(activity);
// Maybe we need to use this?
if (fixedW != 0 && fixedH != 0) {
Log.i(TAG, "Setting surface holder to use a fixed size of " + fixedW + "x" + fixedH + " pixels");
this.getHolder().setFixedSize(fixedW, fixedH);
} else {
Log.i(TAG, "Using default backbuffer size.");
}
// this.getHolder().setFormat(PixelFormat.RGBA_8888);
try {
@@ -86,14 +86,13 @@ void DrawBuffer::End() {
void DrawBuffer::Flush(bool set_blend_state) {
using namespace Draw;
if (count_ == 0)
return;
if (!pipeline_) {
ELOG("No program set!");
ELOG("DrawBuffer: No program set, skipping flush!");
count_ = 0;
return;
}
if (count_ == 0)
return;
draw_->BindPipeline(pipeline_);
VsTexColUB ub{};
@@ -273,12 +273,13 @@ GLuint ShaderStageToOpenGL(ShaderStage stage) {
class OpenGLShaderModule : public ShaderModule, public GfxResourceHolder {
public:
OpenGLShaderModule(ShaderStage stage) : stage_(stage) {
ILOG("Shader module created (%p)", this);
register_gl_resource_holder(this, "drawcontext_shader_module", 0);
glstage_ = ShaderStageToOpenGL(stage);
}
~OpenGLShaderModule() {
ILOG("Shader module destroyed");
ILOG("Shader module destroyed (%p)", this);
if (shader_)
glDeleteShader(shader_);
unregister_gl_resource_holder(this);

0 comments on commit b87ae5d

Please sign in to comment.