Skip to content

Commit

Permalink
Auto merge of #22832 - paulrouget:capi_tweaks, r=jdm
Browse files Browse the repository at this point in the history
Some tweaks in libsimpleservo C API

- Adding logs.
- Adding OpenGL support for OSX.
- Fixing file reading issue.

With this, I can load Servo into a C app (with the C version of libui + andlabs/libui#405).

<!-- 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/22832)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Feb 7, 2019
2 parents 6c161e5 + 1292db6 commit d029b1a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ports/libsimpleservo/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ serde_json = "1.0"
[target.'cfg(not(target_os = "macos"))'.dependencies]
libc = "0.2"

[target.'cfg(target_os = "macos")'.dependencies]
core-foundation = "0.6"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = "0.3.2"

Expand Down
40 changes: 33 additions & 7 deletions ports/libsimpleservo/api/src/gl_glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::rc::Rc;

pub type ServoGl = Rc<dyn servo::gl::Gl>;
pub type ServoGl = std::rc::Rc<dyn servo::gl::Gl>;

#[cfg(any(target_os = "android", target_os = "windows"))]
#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -56,7 +54,7 @@ pub mod egl {
}

#[cfg(target_os = "windows")]
pub fn init() -> Result<Rc<Gl>, &'static str> {
pub fn init() -> Result<crate::gl_glue::ServoGl, &'static str> {
info!("Loading EGL...");

let dll = b"libEGL.dll\0" as &[u8];
Expand All @@ -77,10 +75,38 @@ pub mod egl {
}
}

#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
#[cfg(any(target_os = "windows", target_os = "linux"))]
pub mod gl {
pub fn init() -> Result<crate::gl_glue::ServoGl, &'static str> {
// FIXME: Add an OpenGL version
unimplemented!()
unimplemented!();
}
}

#[cfg(target_os = "macos")]
pub mod gl {
use core_foundation::base::TCFType;
use core_foundation::bundle::{
CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName,
};
use core_foundation::string::CFString;
use servo::gl::GlFns;
use std::os::raw::c_void;
use std::str;

pub fn init() -> Result<crate::gl_glue::ServoGl, &'static str> {
info!("Loading OpenGL...");
let gl = unsafe {
GlFns::load_with(|addr| {
let symbol_name: CFString = str::FromStr::from_str(addr).unwrap();
let framework_name: CFString = str::FromStr::from_str("com.apple.opengl").unwrap();
let framework =
CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef());
let symbol =
CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef());
symbol as *const c_void
})
};
info!("OpenGL loaded");
Ok(gl)
}
}
1 change: 1 addition & 0 deletions ports/libsimpleservo/capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ bench = false
[dependencies]
simpleservo = { path = "../api" }
log = "0.4"
env_logger = "0.6"

[features]
default = ["unstable", "default-except-unstable"]
Expand Down
3 changes: 3 additions & 0 deletions ports/libsimpleservo/capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#[macro_use]
extern crate log;

use env_logger;
use simpleservo::{self, gl_glue, EventLoopWaker, HostTrait, InitOptions, ServoGlue, SERVO};
use std::ffi::{CStr, CString};
use std::mem;
Expand Down Expand Up @@ -69,6 +70,8 @@ fn init(
wakeup: extern "C" fn(),
callbacks: CHostCallbacks,
) {
crate::env_logger::init();

let args = unsafe { CStr::from_ptr(opts.args) };
let args = args.to_str().map(|s| s.to_string()).ok();

Expand Down

0 comments on commit d029b1a

Please sign in to comment.