Skip to content

Commit

Permalink
Auto merge of #21234 - paulrouget:surface, r=MortimerGoro
Browse files Browse the repository at this point in the history
Android: Introduce ServoSurface

(WIP as it depends on 2 other PRs)

Depends on #21199. Only last commit matters.

r? @MortimerGoro

Please look at ServoSurface.java. The rest is mostly some refactoring to share as much code as possible with ServoView.

<!-- 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/21234)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Aug 1, 2018
2 parents 890d40d + c36b165 commit 733552d
Show file tree
Hide file tree
Showing 9 changed files with 523 additions and 191 deletions.
4 changes: 4 additions & 0 deletions ports/libsimpleservo/src/api.rs
Expand Up @@ -39,6 +39,9 @@ pub trait HostTrait {
/// Will be called from the thread used for the init call.
/// Will be called when the GL buffer has been updated.
fn flush(&self);
/// Will be called before drawing.
/// Time to make the targetted GL context current.
fn make_current(&self);
/// Page starts loading.
/// "Reload button" should be disabled.
/// "Stop button" should be enabled.
Expand Down Expand Up @@ -355,6 +358,7 @@ impl WindowMethods for ServoCallbacks {
_height: Length<u32, DevicePixel>,
) -> bool {
debug!("WindowMethods::prepare_for_composite");
self.host_callbacks.make_current();
true
}

Expand Down
6 changes: 6 additions & 0 deletions ports/libsimpleservo/src/capi.rs
Expand Up @@ -28,6 +28,7 @@ fn call<F>(f: F) where F: Fn(&mut ServoGlue) -> Result<(), &'static str> {
#[repr(C)]
pub struct CHostCallbacks {
pub flush: extern fn(),
pub make_current: extern fn(),
pub on_load_started: extern fn(),
pub on_load_ended: extern fn(),
pub on_title_changed: extern fn(title: *const c_char),
Expand Down Expand Up @@ -223,6 +224,11 @@ impl HostTrait for HostCallbacks {
(self.0.flush)();
}

fn make_current(&self) {
debug!("make_current");
(self.0.make_current)();
}

fn on_load_started(&self) {
debug!("on_load_ended");
(self.0.on_load_started)();
Expand Down
61 changes: 31 additions & 30 deletions ports/libsimpleservo/src/jniapi.rs
Expand Up @@ -36,21 +36,19 @@ where
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_version(env: JNIEnv, _class: JClass) -> jstring {
pub fn Java_com_mozilla_servoview_JNIServo_version(env: JNIEnv, _class: JClass) -> jstring {
let v = api::servo_version();
let output = env.new_string(v).expect("Couldn't create java string");
output.into_inner()
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_init(
pub fn Java_com_mozilla_servoview_JNIServo_init(
env: JNIEnv,
_: JClass,
activity: JObject,
args: JString,
url: JString,
wakeup_obj: JObject,
readfile_obj: JObject,
callbacks_obj: JObject,
width: jint,
height: jint,
Expand Down Expand Up @@ -80,9 +78,11 @@ pub fn Java_com_mozilla_servoview_NativeServo_init(
Some(env.get_string(url).expect("Couldn't get java string").into())
};

let wakeup = Box::new(WakeupCallback::new(wakeup_obj, &env));
let readfile = Box::new(ReadFileCallback::new(readfile_obj, &env));
let callbacks = Box::new(HostCallbacks::new(callbacks_obj, &env));
let callbacks_ref = env.new_global_ref(callbacks_obj).unwrap();

let wakeup = Box::new(WakeupCallback::new(callbacks_ref.clone(), &env));
let readfile = Box::new(ReadFileCallback::new(callbacks_ref.clone(), &env));
let callbacks = Box::new(HostCallbacks::new(callbacks_ref, &env));

gl_glue::egl::init().and_then(|gl| {
api::init(
Expand All @@ -100,7 +100,7 @@ pub fn Java_com_mozilla_servoview_NativeServo_init(
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_setBatchMode(
pub fn Java_com_mozilla_servoview_JNIServo_setBatchMode(
env: JNIEnv,
_: JClass,
batch: jboolean,
Expand All @@ -110,7 +110,7 @@ pub fn Java_com_mozilla_servoview_NativeServo_setBatchMode(
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_resize(
pub fn Java_com_mozilla_servoview_JNIServo_resize(
env: JNIEnv,
_: JClass,
width: jint,
Expand All @@ -121,38 +121,38 @@ pub fn Java_com_mozilla_servoview_NativeServo_resize(
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_performUpdates(env: JNIEnv, _class: JClass) {
pub fn Java_com_mozilla_servoview_JNIServo_performUpdates(env: JNIEnv, _class: JClass) {
debug!("performUpdates");
call(env, |s| s.perform_updates());
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_loadUri(env: JNIEnv, _class: JClass, url: JString) {
pub fn Java_com_mozilla_servoview_JNIServo_loadUri(env: JNIEnv, _class: JClass, url: JString) {
debug!("loadUri");
let url: String = env.get_string(url).unwrap().into();
call(env, |s| s.load_uri(&url));
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_reload(env: JNIEnv, _class: JClass) {
pub fn Java_com_mozilla_servoview_JNIServo_reload(env: JNIEnv, _class: JClass) {
debug!("reload");
call(env, |s| s.reload());
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_goBack(env: JNIEnv, _class: JClass) {
pub fn Java_com_mozilla_servoview_JNIServo_goBack(env: JNIEnv, _class: JClass) {
debug!("goBack");
call(env, |s| s.go_back());
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_goForward(env: JNIEnv, _class: JClass) {
pub fn Java_com_mozilla_servoview_JNIServo_goForward(env: JNIEnv, _class: JClass) {
debug!("goForward");
call(env, |s| s.go_forward());
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_scrollStart(
pub fn Java_com_mozilla_servoview_JNIServo_scrollStart(
env: JNIEnv,
_: JClass,
dx: jint,
Expand All @@ -165,7 +165,7 @@ pub fn Java_com_mozilla_servoview_NativeServo_scrollStart(
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_scrollEnd(
pub fn Java_com_mozilla_servoview_JNIServo_scrollEnd(
env: JNIEnv,
_: JClass,
dx: jint,
Expand All @@ -179,7 +179,7 @@ pub fn Java_com_mozilla_servoview_NativeServo_scrollEnd(


#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_scroll(
pub fn Java_com_mozilla_servoview_JNIServo_scroll(
env: JNIEnv,
_: JClass,
dx: jint,
Expand All @@ -192,7 +192,7 @@ pub fn Java_com_mozilla_servoview_NativeServo_scroll(
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_click(env: JNIEnv, _: JClass, x: jint, y: jint) {
pub fn Java_com_mozilla_servoview_JNIServo_click(env: JNIEnv, _: JClass, x: jint, y: jint) {
debug!("click");
call(env, |s| s.click(x as u32, y as u32));
}
Expand All @@ -203,12 +203,9 @@ pub struct WakeupCallback {
}

impl WakeupCallback {
pub fn new(jobject: JObject, env: &JNIEnv) -> WakeupCallback {
pub fn new(callback: GlobalRef, env: &JNIEnv) -> WakeupCallback {
let jvm = Arc::new(env.get_java_vm().unwrap());
WakeupCallback {
callback: env.new_global_ref(jobject).unwrap(),
jvm,
}
WakeupCallback { callback, jvm }
}
}

Expand All @@ -233,9 +230,9 @@ pub struct ReadFileCallback {
}

impl ReadFileCallback {
pub fn new(jobject: JObject, env: &JNIEnv) -> ReadFileCallback {
pub fn new(callback: GlobalRef, env: &JNIEnv) -> ReadFileCallback {
let jvm = env.get_java_vm().unwrap();
let callback = Mutex::new(env.new_global_ref(jobject).unwrap());
let callback = Mutex::new(callback);
ReadFileCallback { callback, jvm }
}
}
Expand All @@ -260,12 +257,9 @@ impl ReadFileTrait for ReadFileCallback {
}

impl HostCallbacks {
pub fn new(jobject: JObject, env: &JNIEnv) -> HostCallbacks {
pub fn new(callbacks: GlobalRef, env: &JNIEnv) -> HostCallbacks {
let jvm = env.get_java_vm().unwrap();
HostCallbacks {
callbacks: env.new_global_ref(jobject).unwrap(),
jvm,
}
HostCallbacks { callbacks, jvm }
}
}

Expand All @@ -277,6 +271,13 @@ impl HostTrait for HostCallbacks {
.unwrap();
}

fn make_current(&self) {
debug!("make_current");
let env = self.jvm.get_env().unwrap();
env.call_method(self.callbacks.as_obj(), "makeCurrent", "()V", &[])
.unwrap();
}

fn on_load_started(&self) {
debug!("on_load_started");
let env = self.jvm.get_env().unwrap();
Expand Down
Expand Up @@ -20,10 +20,11 @@
import android.widget.ProgressBar;

import com.mozilla.servoview.ServoView;
import com.mozilla.servoview.Servo;

import java.io.File;

public class MainActivity extends Activity implements ServoView.Client {
public class MainActivity extends Activity implements Servo.Client {

private static final String LOGTAG = "MainActivity";

Expand All @@ -49,10 +50,10 @@ protected void onCreate(Bundle savedInstanceState) {
mUrlField = findViewById(R.id.urlfield);
mProgressBar = findViewById(R.id.progressbar);

mServoView.setClient(this);
mBackButton.setEnabled(false);
mFwdButton.setEnabled(false);

mServoView.setClient(this);
mServoView.requestFocus();

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Expand All @@ -66,9 +67,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

String args = getIntent().getStringExtra("servoargs");
if (args != null) {
mServoView.setServoArgs(args);
}
mServoView.setServoArgs(args);

setupUrlField();
}
Expand Down Expand Up @@ -105,18 +104,16 @@ private void loadUrlFromField() {
mServoView.loadUri(Uri.parse(uri));
}

// From activity_main.xml:
public void onReloadClicked(View v) {
mServoView.reload();
}

public void onBackClicked(View v) {
mServoView.goBack();
}

public void onForwardClicked(View v) {
mServoView.goForward();
}

public void onStopClicked(View v) {
mServoView.stop();
}
Expand Down
Expand Up @@ -10,48 +10,65 @@
/**
* Maps /ports/libsimpleservo API
*/
public class NativeServo {
@SuppressWarnings("JniMissingFunction")
public class JNIServo {
JNIServo() {
System.loadLibrary("c++_shared");
System.loadLibrary("simpleservo");
}

public native String version();

public native void init(Activity activity,
String args,
String url,
WakeupCallback wakeup,
ReadFileCallback readfile,
ServoCallbacks callbacks,
Callbacks callbacks,
int width, int height, boolean log);

public native void setBatchMode(boolean mode);

public native void performUpdates();

public native void resize(int width, int height);

public native void reload();

public native void stop();

public native void goBack();

public native void goForward();

public native void loadUri(String uri);

public native void scrollStart(int dx, int dy, int x, int y);

public native void scroll(int dx, int dy, int x, int y);
public native void scrollEnd(int dx, int dy, int x, int y);
public native void click(int x, int y);

NativeServo() {
System.loadLibrary("c++_shared");
System.loadLibrary("simpleservo");
}
public native void scrollEnd(int dx, int dy, int x, int y);

public interface ReadFileCallback {
byte[] readfile(String file);
}
public native void click(int x, int y);

public interface WakeupCallback {
public interface Callbacks {
void wakeup();
}

public interface ServoCallbacks {
void flush();

void makeCurrent();

void onAnimatingChanged(boolean animating);

void onLoadStarted();

void onLoadEnded();

void onTitleChanged(String title);

void onUrlChanged(String url);

void onHistoryChanged(boolean canGoBack, boolean canGoForward);
void onAnimatingChanged(boolean animating);

byte[] readfile(String file);
}
}

0 comments on commit 733552d

Please sign in to comment.