From 08d812ec0ed9e2f3d0a287ba5c7e0b926ae0f89a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Jim=C3=A9nez=20Moreno?= Date: Mon, 20 May 2019 14:19:08 +0200 Subject: [PATCH] Implement WindowMethods for Android --- Cargo.lock | 1 + ports/libsimpleservo/api/Cargo.toml | 1 + ports/libsimpleservo/api/src/gl_glue.rs | 20 +++++++++++++++---- ports/libsimpleservo/api/src/lib.rs | 26 ++++++++++++++++++++++++- ports/libsimpleservo/jniapi/src/lib.rs | 12 ++++++++---- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0432a88cb4b..e6f4452f7c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4404,6 +4404,7 @@ dependencies = [ "libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "libservo 0.0.1", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "servo-media 0.1.0 (git+https://github.com/servo/media)", "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/ports/libsimpleservo/api/Cargo.toml b/ports/libsimpleservo/api/Cargo.toml index 6dd0c7c014a..241074ac593 100644 --- a/ports/libsimpleservo/api/Cargo.toml +++ b/ports/libsimpleservo/api/Cargo.toml @@ -9,6 +9,7 @@ publish = false [dependencies] libservo = { path = "../../../components/servo" } log = "0.4" +servo-media = { git = "https://github.com/servo/media" } [target.'cfg(not(target_os = "macos"))'.dependencies] libc = "0.2" diff --git a/ports/libsimpleservo/api/src/gl_glue.rs b/ports/libsimpleservo/api/src/gl_glue.rs index 126de03789d..3f69ef15d46 100644 --- a/ports/libsimpleservo/api/src/gl_glue.rs +++ b/ports/libsimpleservo/api/src/gl_glue.rs @@ -18,6 +18,7 @@ pub mod egl { pub type khronos_uint64_t = libc::uint64_t; pub type khronos_ssize_t = libc::c_long; pub type EGLint = libc::int32_t; + pub type EGLContext = *const libc::c_void; pub type EGLNativeDisplayType = *const libc::c_void; pub type EGLNativePixmapType = *const libc::c_void; pub type NativeDisplayType = EGLNativeDisplayType; @@ -26,12 +27,19 @@ pub mod egl { include!(concat!(env!("OUT_DIR"), "/egl_bindings.rs")); - pub fn init() -> Result { + pub struct EGLInitResult { + pub gl_wrapper: crate::gl_glue::ServoGl, + pub gl_context: EGLContext, + pub display: EGLNativeDisplayType, + } + + #[cfg(target_os = "android")] + pub fn init() -> Result { info!("Loading EGL..."); unsafe { let egl = Egl; - let d = egl.GetCurrentDisplay(); - egl.SwapInterval(d, 1); + let display = egl.GetCurrentDisplay(); + egl.SwapInterval(display, 1); let egl = GlesFns::load_with(|addr| { let addr = CString::new(addr.as_bytes()).unwrap(); let addr = addr.as_ptr(); @@ -39,7 +47,11 @@ pub mod egl { egl.GetProcAddress(addr) as *const c_void }); info!("EGL loaded"); - Ok(egl) + Ok(EGLInitResult { + gl_wrapper: egl, + gl_context: Egl.GetCurrentContext(), + display, + }) } } } diff --git a/ports/libsimpleservo/api/src/lib.rs b/ports/libsimpleservo/api/src/lib.rs index 1adf4d14eea..50284dc1111 100644 --- a/ports/libsimpleservo/api/src/lib.rs +++ b/ports/libsimpleservo/api/src/lib.rs @@ -25,7 +25,7 @@ use servo::servo_url::ServoUrl; use servo::webrender_api::{DevicePixel, FramebufferPixel, ScrollLocation}; use servo::webvr::{VRExternalShmemPtr, VRMainThreadHeartbeat, VRService, VRServiceManager}; use servo::{self, gl, BrowserId, Servo}; - +use servo_media::player::context as MediaPlayerContext; use std::cell::RefCell; use std::mem; use std::os::raw::c_void; @@ -48,6 +48,8 @@ pub struct InitOptions { pub density: f32, pub vr_init: VRInitOptions, pub enable_subpixel_text_antialiasing: bool, + pub gl_context_pointer: Option<*const c_void>, + pub native_display_pointer: Option<*const c_void>, } pub enum VRInitOptions { @@ -187,6 +189,8 @@ pub fn init( host_callbacks: callbacks, coordinates: RefCell::new(init_opts.coordinates), density: init_opts.density, + gl_context_pointer: init_opts.gl_context_pointer, + native_display_pointer: init_opts.native_display_pointer, }); let embedder_callbacks = Box::new(ServoEmbedderCallbacks { @@ -583,6 +587,8 @@ struct ServoWindowCallbacks { host_callbacks: Box, coordinates: RefCell, density: f32, + gl_context_pointer: Option<*const libc::c_void>, + native_display_pointer: Option<*const libc::c_void>, } impl EmbedderMethods for ServoEmbedderCallbacks { @@ -643,6 +649,24 @@ impl WindowMethods for ServoWindowCallbacks { hidpi_factor: TypedScale::new(self.density), } } + + fn get_gl_context(&self) -> MediaPlayerContext::GlContext { + match self.gl_context_pointer { + Some(context) => MediaPlayerContext::GlContext::Egl(context as usize), + None => MediaPlayerContext::GlContext::Unknown, + } + } + + fn get_native_display(&self) -> MediaPlayerContext::NativeDisplay { + match self.native_display_pointer { + Some(display) => MediaPlayerContext::NativeDisplay::Egl(display as usize), + None => MediaPlayerContext::NativeDisplay::Unknown, + } + } + + fn get_gl_api(&self) -> MediaPlayerContext::GlApi { + MediaPlayerContext::GlApi::Gles2 + } } struct ResourceReaderInstance; diff --git a/ports/libsimpleservo/jniapi/src/lib.rs b/ports/libsimpleservo/jniapi/src/lib.rs index d342a1a6c29..b392be62f66 100644 --- a/ports/libsimpleservo/jniapi/src/lib.rs +++ b/ports/libsimpleservo/jniapi/src/lib.rs @@ -52,7 +52,7 @@ pub fn Java_org_mozilla_servoview_JNIServo_init( opts: JObject, callbacks_obj: JObject, ) { - let (opts, log, log_str) = match get_options(&env, opts) { + let (mut opts, log, log_str) = match get_options(&env, opts) { Ok((opts, log, log_str)) => (opts, log, log_str), Err(err) => { throw(&env, &err); @@ -104,9 +104,11 @@ pub fn Java_org_mozilla_servoview_JNIServo_init( let wakeup = Box::new(WakeupCallback::new(callbacks_ref.clone(), &env)); let callbacks = Box::new(HostCallbacks::new(callbacks_ref, &env)); - if let Err(err) = - gl_glue::egl::init().and_then(|gl| simpleservo::init(opts, gl, wakeup, callbacks)) - { + if let Err(err) = gl_glue::egl::init().and_then(|egl_init| { + opts.gl_context_pointer = Some(egl_init.gl_context); + opts.native_display_pointer = Some(egl_init.display); + simpleservo::init(opts, egl_init.gl_wrapper, wakeup, callbacks) + }) { throw(&env, err) }; } @@ -726,6 +728,8 @@ fn get_options(env: &JNIEnv, opts: JObject) -> Result<(InitOptions, bool, Option } else { VRInitOptions::VRExternal(vr_pointer) }, + gl_context_pointer: None, + native_display_pointer: None, }; Ok((opts, log, log_str)) }