Get the right pixel density on Android

This commit is contained in:
Paul Rouget 2018-09-23 03:46:23 +02:00
parent 7287ea4c85
commit 96cf1e2da3
7 changed files with 27 additions and 12 deletions

View file

@ -101,6 +101,7 @@ pub fn init(
callbacks: Box<HostTrait>,
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<HostTrait>,
width: Cell<u32>,
height: Cell<u32>,
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),
}
}
}

View file

@ -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]

View file

@ -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();

View file

@ -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);

View file

@ -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);
});
}

View file

@ -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();

View file

@ -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);
});
}