From 96cf1e2da3080ef8aed48e78627507909b3cee59 Mon Sep 17 00:00:00 2001 From: Paul Rouget Date: Sun, 23 Sep 2018 03:46:23 +0200 Subject: [PATCH] Get the right pixel density on Android --- ports/libsimpleservo/src/api.rs | 5 ++++- ports/libsimpleservo/src/capi.rs | 14 +++++++++----- ports/libsimpleservo/src/jniapi.rs | 4 +++- .../main/java/com/mozilla/servoview/JNIServo.java | 3 ++- .../src/main/java/com/mozilla/servoview/Servo.java | 4 ++-- .../java/com/mozilla/servoview/ServoSurface.java | 2 +- .../main/java/com/mozilla/servoview/ServoView.java | 7 ++++++- 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/ports/libsimpleservo/src/api.rs b/ports/libsimpleservo/src/api.rs index f3d794b6ae8..924dd4b8afd 100644 --- a/ports/libsimpleservo/src/api.rs +++ b/ports/libsimpleservo/src/api.rs @@ -101,6 +101,7 @@ pub fn init( callbacks: Box, width: u32, height: u32, + density: f32, ) -> Result<(), &'static str> { resources::set(Box::new(ResourceReader(readfile))); @@ -136,6 +137,7 @@ pub fn init( host_callbacks: callbacks, width: Cell::new(width), height: Cell::new(height), + density, waker, }); @@ -419,6 +421,7 @@ struct ServoCallbacks { host_callbacks: Box, width: Cell, height: Cell, + density: f32, } impl WindowMethods for ServoCallbacks { @@ -460,7 +463,7 @@ impl WindowMethods for ServoCallbacks { window: (size, TypedPoint2D::new(0, 0)), screen: size, screen_avail: size, - hidpi_factor: TypedScale::new(2.0), + hidpi_factor: TypedScale::new(self.density), } } } diff --git a/ports/libsimpleservo/src/capi.rs b/ports/libsimpleservo/src/capi.rs index dafa49373ef..6c6ae9a489f 100644 --- a/ports/libsimpleservo/src/capi.rs +++ b/ports/libsimpleservo/src/capi.rs @@ -55,7 +55,8 @@ fn init( readfile: extern fn(*const c_char) -> *const c_char, callbacks: CHostCallbacks, width: u32, - height: u32) { + height: u32, + density: f32) { let args = unsafe { CStr::from_ptr(args) }; let args = args.to_str().expect("Can't read string").to_string(); @@ -75,6 +76,7 @@ fn init( callbacks, width, height, + density, ).unwrap(); } @@ -87,9 +89,10 @@ pub extern "C" fn init_with_egl( readfile: extern fn(*const c_char) -> *const c_char, callbacks: CHostCallbacks, width: u32, - height: u32) { + height: u32, + density: f32) { let gl = gl_glue::egl::init().unwrap(); - init(gl, args, url, wakeup, readfile, callbacks, width, height) + init(gl, args, url, wakeup, readfile, callbacks, width, height, density) } #[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))] @@ -101,9 +104,10 @@ pub extern "C" fn init_with_gl( readfile: extern fn(*const c_char) -> *const c_char, callbacks: CHostCallbacks, width: u32, - height: u32) { + height: u32, + density: f32) { let gl = gl_glue::gl::init().unwrap(); - init(gl, args, url, wakeup, readfile, callbacks, width, height) + init(gl, args, url, wakeup, readfile, callbacks, width, height, density) } #[no_mangle] diff --git a/ports/libsimpleservo/src/jniapi.rs b/ports/libsimpleservo/src/jniapi.rs index 95c0d2c4d61..a41dbb61088 100644 --- a/ports/libsimpleservo/src/jniapi.rs +++ b/ports/libsimpleservo/src/jniapi.rs @@ -54,6 +54,7 @@ pub fn Java_com_mozilla_servoview_JNIServo_init( callbacks_obj: JObject, width: jint, height: jint, + density: jfloat, log: jboolean, ) { if log == JNI_TRUE { @@ -102,7 +103,8 @@ pub fn Java_com_mozilla_servoview_JNIServo_init( readfile, callbacks, width as u32, - height as u32) + height as u32, + density as f32) }).or_else(|err| { env.throw(("java/lang/Exception", err)) }).unwrap(); diff --git a/support/android/apk/servoview/src/main/java/com/mozilla/servoview/JNIServo.java b/support/android/apk/servoview/src/main/java/com/mozilla/servoview/JNIServo.java index 462b6c4d0cd..893a5d06568 100644 --- a/support/android/apk/servoview/src/main/java/com/mozilla/servoview/JNIServo.java +++ b/support/android/apk/servoview/src/main/java/com/mozilla/servoview/JNIServo.java @@ -24,7 +24,8 @@ public class JNIServo { String url, String logstr, Callbacks callbacks, - int width, int height, boolean log); + int width, int height, float density, + boolean log); public native void setBatchMode(boolean mode); diff --git a/support/android/apk/servoview/src/main/java/com/mozilla/servoview/Servo.java b/support/android/apk/servoview/src/main/java/com/mozilla/servoview/Servo.java index 61affbaa9c4..5535cc6f4e4 100644 --- a/support/android/apk/servoview/src/main/java/com/mozilla/servoview/Servo.java +++ b/support/android/apk/servoview/src/main/java/com/mozilla/servoview/Servo.java @@ -27,7 +27,7 @@ public class Servo { String args, String url, String logstr, - int width, int height, boolean log) { + int width, int height, float density, boolean log) { mRunCallback = runCallback; @@ -36,7 +36,7 @@ public class Servo { Callbacks cbs = new Callbacks(client, gfxcb); mRunCallback.inGLThread(() -> { - mJNI.init(activity, args, url, logstr, cbs, width, height, log); + mJNI.init(activity, args, url, logstr, cbs, width, height, density, log); }); } diff --git a/support/android/apk/servoview/src/main/java/com/mozilla/servoview/ServoSurface.java b/support/android/apk/servoview/src/main/java/com/mozilla/servoview/ServoSurface.java index d7180e40095..35719904727 100644 --- a/support/android/apk/servoview/src/main/java/com/mozilla/servoview/ServoSurface.java +++ b/support/android/apk/servoview/src/main/java/com/mozilla/servoview/ServoSurface.java @@ -206,7 +206,7 @@ public class ServoSurface { inUIThread(() -> { final boolean showLogs = true; String uri = mInitialUri == null ? null : mInitialUri; - mServo = new Servo(this, surface, mClient, mActivity, mServoArgs, uri, mServoLog, mWidth, mHeight, showLogs); + mServo = new Servo(this, surface, mClient, mActivity, mServoArgs, uri, mServoLog, mWidth, mHeight, 1, showLogs); }); Looper.loop(); diff --git a/support/android/apk/servoview/src/main/java/com/mozilla/servoview/ServoView.java b/support/android/apk/servoview/src/main/java/com/mozilla/servoview/ServoView.java index 895fd59e3c4..0dcb0e91d4b 100644 --- a/support/android/apk/servoview/src/main/java/com/mozilla/servoview/ServoView.java +++ b/support/android/apk/servoview/src/main/java/com/mozilla/servoview/ServoView.java @@ -11,6 +11,7 @@ import android.net.Uri; import android.opengl.GLES31; import android.opengl.GLSurfaceView; import android.util.AttributeSet; +import android.util.DisplayMetrics; import android.util.Log; import android.view.Choreographer; import android.view.GestureDetector; @@ -139,9 +140,13 @@ public class ServoView extends GLSurfaceView final boolean showLogs = true; int width = getWidth(); int height = getHeight(); + DisplayMetrics metrics = new DisplayMetrics(); + mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics); + float density = metrics.density; + inGLThread(() -> { String uri = mInitialUri == null ? null : mInitialUri.toString(); - mServo = new Servo(this, this, mClient, mActivity, mServoArgs, uri, mServoLog, width, height, showLogs); + mServo = new Servo(this, this, mClient, mActivity, mServoArgs, uri, mServoLog, width, height, density, showLogs); }); }