Auto merge of #21774 - paulrouget:pixel-density-android, r=jdm

Get the right pixel density on Android

Fix #21605 and fix #21504

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21774)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-09-24 01:46:29 -04:00 committed by GitHub
commit c5708ce801
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 27 additions and 12 deletions

View file

@ -101,6 +101,7 @@ pub fn init(
callbacks: Box<HostTrait>, callbacks: Box<HostTrait>,
width: u32, width: u32,
height: u32, height: u32,
density: f32,
) -> Result<(), &'static str> { ) -> Result<(), &'static str> {
resources::set(Box::new(ResourceReader(readfile))); resources::set(Box::new(ResourceReader(readfile)));
@ -136,6 +137,7 @@ pub fn init(
host_callbacks: callbacks, host_callbacks: callbacks,
width: Cell::new(width), width: Cell::new(width),
height: Cell::new(height), height: Cell::new(height),
density,
waker, waker,
}); });
@ -419,6 +421,7 @@ struct ServoCallbacks {
host_callbacks: Box<HostTrait>, host_callbacks: Box<HostTrait>,
width: Cell<u32>, width: Cell<u32>,
height: Cell<u32>, height: Cell<u32>,
density: f32,
} }
impl WindowMethods for ServoCallbacks { impl WindowMethods for ServoCallbacks {
@ -460,7 +463,7 @@ impl WindowMethods for ServoCallbacks {
window: (size, TypedPoint2D::new(0, 0)), window: (size, TypedPoint2D::new(0, 0)),
screen: size, screen: size,
screen_avail: 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, readfile: extern fn(*const c_char) -> *const c_char,
callbacks: CHostCallbacks, callbacks: CHostCallbacks,
width: u32, width: u32,
height: u32) { height: u32,
density: f32) {
let args = unsafe { CStr::from_ptr(args) }; let args = unsafe { CStr::from_ptr(args) };
let args = args.to_str().expect("Can't read string").to_string(); let args = args.to_str().expect("Can't read string").to_string();
@ -75,6 +76,7 @@ fn init(
callbacks, callbacks,
width, width,
height, height,
density,
).unwrap(); ).unwrap();
} }
@ -87,9 +89,10 @@ pub extern "C" fn init_with_egl(
readfile: extern fn(*const c_char) -> *const c_char, readfile: extern fn(*const c_char) -> *const c_char,
callbacks: CHostCallbacks, callbacks: CHostCallbacks,
width: u32, width: u32,
height: u32) { height: u32,
density: f32) {
let gl = gl_glue::egl::init().unwrap(); 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"))] #[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, readfile: extern fn(*const c_char) -> *const c_char,
callbacks: CHostCallbacks, callbacks: CHostCallbacks,
width: u32, width: u32,
height: u32) { height: u32,
density: f32) {
let gl = gl_glue::gl::init().unwrap(); 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] #[no_mangle]

View file

@ -54,6 +54,7 @@ pub fn Java_com_mozilla_servoview_JNIServo_init(
callbacks_obj: JObject, callbacks_obj: JObject,
width: jint, width: jint,
height: jint, height: jint,
density: jfloat,
log: jboolean, log: jboolean,
) { ) {
if log == JNI_TRUE { if log == JNI_TRUE {
@ -102,7 +103,8 @@ pub fn Java_com_mozilla_servoview_JNIServo_init(
readfile, readfile,
callbacks, callbacks,
width as u32, width as u32,
height as u32) height as u32,
density as f32)
}).or_else(|err| { }).or_else(|err| {
env.throw(("java/lang/Exception", err)) env.throw(("java/lang/Exception", err))
}).unwrap(); }).unwrap();

View file

@ -24,7 +24,8 @@ public class JNIServo {
String url, String url,
String logstr, String logstr,
Callbacks callbacks, Callbacks callbacks,
int width, int height, boolean log); int width, int height, float density,
boolean log);
public native void setBatchMode(boolean mode); public native void setBatchMode(boolean mode);

View file

@ -27,7 +27,7 @@ public class Servo {
String args, String args,
String url, String url,
String logstr, String logstr,
int width, int height, boolean log) { int width, int height, float density, boolean log) {
mRunCallback = runCallback; mRunCallback = runCallback;
@ -36,7 +36,7 @@ public class Servo {
Callbacks cbs = new Callbacks(client, gfxcb); Callbacks cbs = new Callbacks(client, gfxcb);
mRunCallback.inGLThread(() -> { 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(() -> { inUIThread(() -> {
final boolean showLogs = true; final boolean showLogs = true;
String uri = mInitialUri == null ? null : mInitialUri; 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(); Looper.loop();

View file

@ -11,6 +11,7 @@ import android.net.Uri;
import android.opengl.GLES31; import android.opengl.GLES31;
import android.opengl.GLSurfaceView; import android.opengl.GLSurfaceView;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.Choreographer; import android.view.Choreographer;
import android.view.GestureDetector; import android.view.GestureDetector;
@ -139,9 +140,13 @@ public class ServoView extends GLSurfaceView
final boolean showLogs = true; final boolean showLogs = true;
int width = getWidth(); int width = getWidth();
int height = getHeight(); int height = getHeight();
DisplayMetrics metrics = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
float density = metrics.density;
inGLThread(() -> { inGLThread(() -> {
String uri = mInitialUri == null ? null : mInitialUri.toString(); 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);
}); });
} }