Skip to content

Commit

Permalink
Make it possible to add padding around ServoSurface
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrouget committed Mar 14, 2019
1 parent d8d5921 commit 5296fb1
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 59 deletions.
3 changes: 3 additions & 0 deletions components/compositing/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,9 @@ impl<Window: WindowMethods> IOCompositor<Window> {
&self.embedder_coordinates.framebuffer.to_untyped(),
);

self.window.gl().clear_color(0.0, 0.0, 0.0, 0.0);
self.window.gl().clear(gleam::gl::COLOR_BUFFER_BIT);

// Paint the scene.
// TODO(gw): Take notice of any errors the renderer returns!
self.webrender.render(size).ok();
Expand Down
63 changes: 40 additions & 23 deletions ports/libsimpleservo/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ use servo::compositing::windowing::{
};
use servo::embedder_traits::resources::{self, Resource, ResourceReaderMethods};
use servo::embedder_traits::EmbedderMsg;
use servo::euclid::{TypedPoint2D, TypedScale, TypedSize2D, TypedVector2D};
use servo::euclid::{TypedPoint2D, TypedRect, TypedScale, TypedSize2D, TypedVector2D};
use servo::keyboard_types::{Key, KeyState, KeyboardEvent};
use servo::msg::constellation_msg::TraversalDirection;
use servo::script_traits::{TouchEventType, TouchId};
use servo::servo_config::opts;
use servo::servo_config::prefs::{PrefValue, PREFS};
use servo::servo_url::ServoUrl;
use servo::webrender_api::{DevicePixel, FramebufferPixel, ScrollLocation};
use servo::webvr::{VRExternalShmemPtr, VRMainThreadHeartbeat, VRServiceManager};
use servo::{self, gl, webrender_api, BrowserId, Servo};
use std::cell::{Cell, RefCell};
use servo::{self, gl, BrowserId, Servo};
use std::cell::RefCell;
use std::mem;
use std::os::raw::c_void;
use std::path::PathBuf;
Expand All @@ -41,13 +42,34 @@ pub use servo::embedder_traits::EventLoopWaker;
pub struct InitOptions {
pub args: Option<String>,
pub url: Option<String>,
pub width: u32,
pub height: u32,
pub coordinates: Coordinates,
pub density: f32,
pub vr_pointer: Option<*mut c_void>,
pub enable_subpixel_text_antialiasing: bool,
}

#[derive(Clone, Debug)]
pub struct Coordinates {
pub viewport: TypedRect<i32, DevicePixel>,
pub framebuffer: TypedSize2D<i32, FramebufferPixel>,
}

impl Coordinates {
pub fn new(
x: i32,
y: i32,
width: i32,
height: i32,
fb_width: i32,
fb_height: i32,
) -> Coordinates {
Coordinates {
viewport: TypedRect::new(TypedPoint2D::new(x, y), TypedSize2D::new(width, height)),
framebuffer: TypedSize2D::new(fb_width, fb_height),
}
}
}

/// Callbacks. Implemented by embedder. Called by Servo.
pub trait HostTrait {
/// Will be called from the thread used for the init call.
Expand Down Expand Up @@ -151,8 +173,7 @@ pub fn init(
let callbacks = Rc::new(ServoCallbacks {
gl: gl.clone(),
host_callbacks: callbacks,
width: Cell::new(init_opts.width),
height: Cell::new(init_opts.height),
coordinates: RefCell::new(init_opts.coordinates),
density: init_opts.density,
vr_pointer: init_opts.vr_pointer,
waker,
Expand Down Expand Up @@ -271,10 +292,9 @@ impl ServoGlue {
}

/// Let Servo know that the window has been resized.
pub fn resize(&mut self, width: u32, height: u32) -> Result<(), &'static str> {
pub fn resize(&mut self, coordinates: Coordinates) -> Result<(), &'static str> {
info!("resize");
self.callbacks.width.set(width);
self.callbacks.height.set(height);
*self.callbacks.coordinates.borrow_mut() = coordinates;
self.process_event(WindowEvent::Resize)
}

Expand All @@ -283,7 +303,7 @@ impl ServoGlue {
/// dx/dy are scroll deltas.
pub fn scroll_start(&mut self, dx: f32, dy: f32, x: i32, y: i32) -> Result<(), &'static str> {
let delta = TypedVector2D::new(dx, dy);
let scroll_location = webrender_api::ScrollLocation::Delta(delta);
let scroll_location = ScrollLocation::Delta(delta);
let event = WindowEvent::Scroll(
scroll_location,
TypedPoint2D::new(x, y),
Expand All @@ -297,7 +317,7 @@ impl ServoGlue {
/// dx/dy are scroll deltas.
pub fn scroll(&mut self, dx: f32, dy: f32, x: i32, y: i32) -> Result<(), &'static str> {
let delta = TypedVector2D::new(dx, dy);
let scroll_location = webrender_api::ScrollLocation::Delta(delta);
let scroll_location = ScrollLocation::Delta(delta);
let event = WindowEvent::Scroll(
scroll_location,
TypedPoint2D::new(x, y),
Expand All @@ -311,7 +331,7 @@ impl ServoGlue {
/// dx/dy are scroll deltas.
pub fn scroll_end(&mut self, dx: f32, dy: f32, x: i32, y: i32) -> Result<(), &'static str> {
let delta = TypedVector2D::new(dx, dy);
let scroll_location = webrender_api::ScrollLocation::Delta(delta);
let scroll_location = ScrollLocation::Delta(delta);
let event =
WindowEvent::Scroll(scroll_location, TypedPoint2D::new(x, y), TouchEventType::Up);
self.process_event(event)
Expand Down Expand Up @@ -530,8 +550,7 @@ struct ServoCallbacks {
waker: Box<dyn EventLoopWaker>,
gl: Rc<dyn gl::Gl>,
host_callbacks: Box<dyn HostTrait>,
width: Cell<u32>,
height: Cell<u32>,
coordinates: RefCell<Coordinates>,
density: f32,
vr_pointer: Option<*mut c_void>,
}
Expand Down Expand Up @@ -565,15 +584,13 @@ impl WindowMethods for ServoCallbacks {
}

fn get_coordinates(&self) -> EmbedderCoordinates {
let fb_size = TypedSize2D::new(self.width.get() as i32, self.height.get() as i32);
let pixel_size = TypedSize2D::new(self.width.get() as i32, self.height.get() as i32);
let viewport = webrender_api::DeviceIntRect::new(TypedPoint2D::zero(), pixel_size);
let coords = self.coordinates.borrow();
EmbedderCoordinates {
viewport,
framebuffer: fb_size,
window: (pixel_size, TypedPoint2D::new(0, 0)),
screen: pixel_size,
screen_avail: pixel_size,
viewport: coords.viewport,
framebuffer: coords.framebuffer,
window: (coords.viewport.size, TypedPoint2D::new(0, 0)),
screen: coords.viewport.size,
screen_avail: coords.viewport.size,
hidpi_factor: TypedScale::new(self.density),
}
}
Expand Down
20 changes: 13 additions & 7 deletions ports/libsimpleservo/capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
extern crate log;

use env_logger;
use simpleservo::{self, gl_glue, EventLoopWaker, HostTrait, InitOptions, ServoGlue, SERVO};
use simpleservo::{
self, gl_glue, Coordinates, EventLoopWaker, HostTrait, InitOptions, ServoGlue, SERVO,
};
use std::ffi::{CStr, CString};
use std::mem;
use std::os::raw::{c_char, c_void};
Expand Down Expand Up @@ -48,8 +50,8 @@ pub struct CHostCallbacks {
pub struct CInitOptions {
pub args: *const c_char,
pub url: *const c_char,
pub width: u32,
pub height: u32,
pub width: i32,
pub height: i32,
pub density: f32,
pub vr_pointer: *mut c_void,
pub enable_subpixel_text_antialiasing: bool,
Expand Down Expand Up @@ -79,11 +81,12 @@ fn init(
let url = unsafe { CStr::from_ptr(opts.url) };
let url = url.to_str().map(|s| s.to_string()).ok();

let coordinates = Coordinates::new(0, 0, opts.width, opts.height, opts.width, opts.height);

let opts = InitOptions {
args,
url,
width: opts.width,
height: opts.height,
coordinates,
density: opts.density,
vr_pointer: if opts.vr_pointer.is_null() {
None
Expand Down Expand Up @@ -140,9 +143,12 @@ pub extern "C" fn set_batch_mode(batch: bool) {
}

#[no_mangle]
pub extern "C" fn resize(width: u32, height: u32) {
pub extern "C" fn resize(width: i32, height: i32) {
debug!("resize {}/{}", width, height);
call(|s| s.resize(width, height));
call(|s| {
let coordinates = Coordinates::new(0, 0, width, height, width, height);
s.resize(coordinates)
});
}

#[no_mangle]
Expand Down
59 changes: 42 additions & 17 deletions ports/libsimpleservo/jniapi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use jni::sys::{jboolean, jfloat, jint, jstring, JNI_TRUE};
use jni::{errors, JNIEnv, JavaVM};
use libc::{dup2, pipe, read};
use log::Level;
use simpleservo::{self, gl_glue, EventLoopWaker, HostTrait, InitOptions, ServoGlue, SERVO};
use simpleservo::{
self, gl_glue, Coordinates, EventLoopWaker, HostTrait, InitOptions, ServoGlue, SERVO,
};
use std::os::raw::{c_char, c_int, c_void};
use std::sync::Arc;
use std::thread;
Expand Down Expand Up @@ -129,14 +131,13 @@ pub fn Java_org_mozilla_servoview_JNIServo_deinit(_env: JNIEnv, _class: JClass)
}

#[no_mangle]
pub fn Java_org_mozilla_servoview_JNIServo_resize(
env: JNIEnv,
_: JClass,
width: jint,
height: jint,
) {
debug!("resize {}/{}", width, height);
call(&env, |s| s.resize(width as u32, height as u32));
pub fn Java_org_mozilla_servoview_JNIServo_resize(env: JNIEnv, _: JClass, coordinates: JObject) {
let coords = jni_coords_to_rust_coords(&env, coordinates);
debug!("resize {:#?}", coords);
match coords {
Ok(coords) => call(&env, |s| s.resize(coords.clone())),
Err(error) => throw(&env, &error),
}
}

#[no_mangle]
Expand Down Expand Up @@ -587,6 +588,28 @@ fn new_string(env: &JNIEnv, s: &str) -> Result<jstring, jstring> {
}
}

fn jni_coords_to_rust_coords(env: &JNIEnv, obj: JObject) -> Result<Coordinates, String> {
let x = get_non_null_field(env, obj, "x", "I")?
.i()
.map_err(|_| "x not an int")? as i32;
let y = get_non_null_field(env, obj, "y", "I")?
.i()
.map_err(|_| "y not an int")? as i32;
let width = get_non_null_field(env, obj, "width", "I")?
.i()
.map_err(|_| "width not an int")? as i32;
let height = get_non_null_field(env, obj, "height", "I")?
.i()
.map_err(|_| "height not an int")? as i32;
let fb_width = get_non_null_field(env, obj, "fb_width", "I")?
.i()
.map_err(|_| "fb_width not an int")? as i32;
let fb_height = get_non_null_field(env, obj, "fb_height", "I")?
.i()
.map_err(|_| "fb_height not an int")? as i32;
Ok(Coordinates::new(x, y, width, height, fb_width, fb_height))
}

fn get_field<'a>(
env: &'a JNIEnv,
obj: JObject,
Expand Down Expand Up @@ -638,12 +661,6 @@ fn get_options(env: &JNIEnv, opts: JObject) -> Result<(InitOptions, bool, Option
let args = get_string(env, opts, "args")?;
let url = get_string(env, opts, "url")?;
let log_str = get_string(env, opts, "logStr")?;
let width = get_non_null_field(env, opts, "width", "I")?
.i()
.map_err(|_| "width not an int")? as u32;
let height = get_non_null_field(env, opts, "height", "I")?
.i()
.map_err(|_| "height not an int")? as u32;
let density = get_non_null_field(env, opts, "density", "F")?
.f()
.map_err(|_| "densitiy not a float")? as f32;
Expand All @@ -657,11 +674,19 @@ fn get_options(env: &JNIEnv, opts: JObject) -> Result<(InitOptions, bool, Option
let vr_pointer = get_non_null_field(env, opts, "VRExternalContext", "J")?
.j()
.map_err(|_| "VRExternalContext is not a long")? as *mut c_void;
let coordinates = get_non_null_field(
env,
opts,
"coordinates",
"Lorg/mozilla/servoview/JNIServo$ServoCoordinates;",
)?
.l()
.map_err(|_| "coordinates is not an object")?;
let coordinates = jni_coords_to_rust_coords(&env, coordinates)?;
let opts = InitOptions {
args,
url,
width,
height,
coordinates,
density,
enable_subpixel_text_antialiasing,
vr_pointer: if vr_pointer.is_null() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class JNIServo {

public native void performUpdates();

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

public native void reload();

Expand Down Expand Up @@ -69,15 +69,23 @@ public class JNIServo {
public static class ServoOptions {
public String args;
public String url;
public int width = 0;
public int height = 0;
public ServoCoordinates coordinates;
public float density = 1;
public boolean enableSubpixelTextAntialiasing = true;
public long VRExternalContext = 0;
public String logStr;
public boolean enableLogs = false;
}

public static class ServoCoordinates {
public int x = 0;
public int y = 0;
public int width = 0;
public int height = 0;
public int fb_width = 0;
public int fb_height = 0;
}

public interface Callbacks {
void wakeup();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.concurrent.FutureTask;

import org.freedesktop.gstreamer.GStreamer;
import org.mozilla.servoview.JNIServo.ServoCoordinates;
import org.mozilla.servoview.JNIServo.ServoOptions;

public class Servo {
Expand Down Expand Up @@ -91,8 +92,8 @@ public void setBatchMode(boolean mode) {
mRunCallback.inGLThread(() -> mJNI.setBatchMode(mode));
}

public void resize(int width, int height) {
mRunCallback.inGLThread(() -> mJNI.resize(width, height));
public void resize(ServoCoordinates coords) {
mRunCallback.inGLThread(() -> mJNI.resize(coords));
}

public void refresh() {
Expand Down
Loading

0 comments on commit 5296fb1

Please sign in to comment.