Skip to content

Commit

Permalink
Remove the winit feature (#271)
Browse files Browse the repository at this point in the history
Now that we can use raw_window_handle, drop the winit feature. This
should make the dependency graph a little bit less complicated for
crates that depend on surfman.

A few changes:

 - The Wayland implementation needs the size when creating a native
   widget from a raw window handle, so add that parameters
   Unfortunately, it doesn't look like there is another option here.
 - Rename create_native_widget_from_rwh to
   create_native_widget_from_raw_window_handle. There already exists
   API that uses the `raw_window_handle` terminology.
 - Try to fix the build of the android-example, which depended on
   the old winit and now depends on the new one.

This is a breaking API change, so the next release should be a major
version bump.
  • Loading branch information
mrobinson authored Jan 26, 2024
1 parent 43c7e9e commit 3f09ecc
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 375 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
matrix:
os: [macos-latest, ubuntu-22.04, windows-latest]
rust: [stable]
features: ["", "--features 'chains sm-winit sm-raw-window-handle'"]
features: ["", "--features 'chains sm-raw-window-handle'"]
target: ["default"]
include:
# rust stable
Expand Down
16 changes: 5 additions & 11 deletions android-example/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@ panic = "abort"
panic = "abort"

[dependencies]
android_logger = "0.8"
android_logger = "0.13"
euclid = { version = "0.22" }
gl = "0.14"
jni = "0.13"
jni = "0.21"
log = "0.4"
winit = { version = "0.28.1", features = [ "android-native-activity" ] }

[dependencies.euclid]
version = "0.22"
features = []

[dependencies.surfman]
path = "../../surfman"
features = ["sm-test"]
surfman = { path = "../../surfman", features = [ "sm-test" ] }
winit = { version = "0.29.10", features = [ "android-native-activity", "rwh_05" ] }
21 changes: 13 additions & 8 deletions android-example/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::threads::App;

use android_logger::Config;
use euclid::default::Size2D;
use jni::objects::{GlobalRef, JByteBuffer, JClass, JObject, JValue};
use jni::objects::{GlobalRef, JByteBuffer, JClass, JObject, JValue, JValueGen};
use jni::{JNIEnv, JavaVM};
use log::Level;
use std::cell::{Cell, RefCell};
Expand All @@ -35,7 +35,7 @@ pub unsafe extern "system" fn Java_org_mozilla_surfmanthreadsexample_SurfmanThre
) {
ATTACHED_TO_JNI.with(|attached_to_jni| attached_to_jni.set(true));

android_logger::init_once(Config::default().with_min_level(Level::Trace));
android_logger::init_once(Config::default());

let window_size = Size2D::new(width, height);

Expand Down Expand Up @@ -166,20 +166,25 @@ impl ResourceLoader for JavaResourceLoader {
});

let loader = self.loader.as_obj();
let env = self.vm.get_env().unwrap();
let mut env = self.vm.get_env().unwrap();
match env
.call_method(
loader,
"slurp",
"(Ljava/lang/String;)Ljava/nio/ByteBuffer;",
&[JValue::Object(*env.new_string(filename).unwrap())],
&[JValue::Object(&env.new_string(filename).unwrap())],
)
.unwrap()
{
JValue::Object(object) => {
Ok(JValueGen::Object(object)) => {
let byte_buffer = JByteBuffer::from(object);
dest.extend_from_slice(env.get_direct_buffer_address(byte_buffer).unwrap());
}
unsafe {
let slice = std::slice::from_raw_parts(
env.get_direct_buffer_address(&byte_buffer).unwrap(),
env.get_direct_buffer_capacity(&byte_buffer).unwrap(),
);
dest.extend_from_slice(slice);
}
},
_ => panic!("Unexpected return value!"),
}
}
Expand Down
5 changes: 1 addition & 4 deletions surfman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ cfg_aliases = "0.1.0"

[features]
chains = ["fnv", "sparkle"]
default = ["sm-winit", "sm-raw-window-handle"]
default = ["sm-raw-window-handle"]
sm-angle = []
sm-angle-builtin = ["mozangle"]
sm-angle-default = ["sm-angle"]
sm-no-wgl = ["sm-angle-default"]
sm-test = []
sm-wayland-default = []
sm-winit = ["winit"]
sm-x11 = ["x11"]
sm-raw-window-handle = ["raw-window-handle"]

Expand All @@ -38,7 +37,6 @@ libc = "0.2"
log = "0.4"
sparkle = { version = "0.1", optional = true }
osmesa-sys = { version = "0.1", optional = true }
winit = { version = "0.28.1", optional = true }
raw-window-handle = { version = "0.5", optional = true }

[dev-dependencies]
Expand Down Expand Up @@ -79,4 +77,3 @@ winapi = { version = "0.3", features = ["d3d11", "libloaderapi", "winbase", "win

[target.'cfg(target_os = "android")'.dependencies]
raw-window-handle = "0.5"
winit = { version = "0.28.1", features = [ "android-native-activity" ] }
5 changes: 4 additions & 1 deletion surfman/examples/chaos_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ fn main() {

window.set_visible(true);

let window_size = window.inner_size();
let window_size = Size2D::new(window_size.width as i32, window_size.height as i32);
let handle = window.window_handle().unwrap();
let native_widget = connection
.create_native_widget_from_winit_window(&window)
.create_native_widget_from_raw_window_handle(handle.as_raw(), window_size);
.unwrap();

let surface_type = SurfaceType::Widget { native_widget };
Expand Down
10 changes: 8 additions & 2 deletions surfman/examples/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use winit::{
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder
};
use winit::raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};

pub mod common;

Expand Down Expand Up @@ -99,9 +100,14 @@ fn main() {

window.set_visible(true);

let connection = Connection::from_winit_window(&window).unwrap();
let raw_display_handle = window.raw_display_handle();
let connection = Connection::from_raw_display_handle(raw_display_handle).unwrap();

let window_size = window.inner_size();
let window_size = Size2D::new(window_size.width as i32, window_size.height as i32);
let raw_window_handle = window.raw_window_handle();
let native_widget = connection
.create_native_widget_from_winit_window(&window)
.create_native_widget_from_raw_window_handle(raw_window_handle, window_size)
.unwrap();
let adapter = connection.create_low_power_adapter().unwrap();
let mut device = connection.create_device(&adapter).unwrap();
Expand Down
19 changes: 2 additions & 17 deletions surfman/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ use euclid::default::Size2D;

use std::os::raw::c_void;

#[cfg(feature = "sm-winit")]
use winit::window::Window;

/// Methods relating to display server connections.
pub trait Connection: Sized {
/// The adapter type associated with this connection.
Expand Down Expand Up @@ -57,25 +54,12 @@ pub trait Connection: Sized {
native_device: Self::NativeDevice,
) -> Result<Self::Device, Error>;

/// Opens the display connection corresponding to the given `winit` window.
#[cfg(feature = "sm-winit")]
fn from_winit_window(window: &Window) -> Result<Self, Error>;

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
fn from_raw_display_handle(
raw_handle: raw_window_handle::RawDisplayHandle,
) -> Result<Self, Error>;

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
#[cfg(feature = "sm-winit")]
fn create_native_widget_from_winit_window(
&self,
window: &Window,
) -> Result<Self::NativeWidget, Error>;

/// Creates a native widget from a raw pointer
unsafe fn create_native_widget_from_ptr(
&self,
Expand All @@ -85,8 +69,9 @@ pub trait Connection: Sized {

/// Create a native widget type from the given `raw_window_handle::RawWindowHandle`.
#[cfg(feature = "sm-raw-window-handle")]
fn create_native_widget_from_rwh(
fn create_native_widget_from_raw_window_handle(
&self,
window: raw_window_handle::RawWindowHandle,
size: Size2D<i32>,
) -> Result<Self::NativeWidget, Error>;
}
2 changes: 0 additions & 2 deletions surfman/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ pub enum Error {
IncompatibleAdapter,
/// The native widget type does not match the supplied device.
IncompatibleNativeWidget,
/// The `winit` window is incompatible with this backend.
IncompatibleWinitWindow,
/// The `raw display handle` is incompatible with this backend.
IncompatibleRawDisplayHandle,
/// The native context does not match the supplied device.
Expand Down
23 changes: 3 additions & 20 deletions surfman/src/implementation/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ use euclid::default::Size2D;

use std::os::raw::c_void;

#[cfg(feature = "sm-winit")]
use winit::window::Window;

#[deny(unconditional_recursion)]
impl ConnectionInterface for Connection {
type Adapter = Adapter;
Expand Down Expand Up @@ -73,12 +70,6 @@ impl ConnectionInterface for Connection {
Connection::create_device_from_native_device(self, native_device)
}

#[inline]
#[cfg(feature = "sm-winit")]
fn from_winit_window(window: &Window) -> Result<Connection, Error> {
Connection::from_winit_window(window)
}

#[inline]
#[cfg(feature = "sm-raw-window-handle")]
fn from_raw_display_handle(
Expand All @@ -87,15 +78,6 @@ impl ConnectionInterface for Connection {
Connection::from_raw_display_handle(raw_handle)
}

#[inline]
#[cfg(feature = "sm-winit")]
fn create_native_widget_from_winit_window(
&self,
window: &Window,
) -> Result<NativeWidget, Error> {
Connection::create_native_widget_from_winit_window(self, window)
}

#[inline]
unsafe fn create_native_widget_from_ptr(
&self,
Expand All @@ -107,10 +89,11 @@ impl ConnectionInterface for Connection {

#[inline]
#[cfg(feature = "sm-raw-window-handle")]
fn create_native_widget_from_rwh(
fn create_native_widget_from_raw_window_handle(
&self,
window: raw_window_handle::RawWindowHandle,
size: Size2D<i32>,
) -> Result<NativeWidget, Error> {
Connection::create_native_widget_from_rwh(self, window)
Connection::create_native_widget_from_raw_window_handle(self, window, size)
}
}
32 changes: 2 additions & 30 deletions surfman/src/platform/android/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ use euclid::default::Size2D;

use std::os::raw::c_void;

#[cfg(feature = "sm-winit")]
use winit::window::Window;

/// A connection to the display server.
#[derive(Clone)]
pub struct Connection;
Expand Down Expand Up @@ -100,13 +97,6 @@ impl Connection {
})
}

/// Opens the display connection corresponding to the given `winit` window.
#[cfg(feature = "sm-winit")]
#[inline]
pub fn from_winit_window(_: &Window) -> Result<Connection, Error> {
Ok(Connection)
}

/// Opens the display connection corresponding to the given raw display handle.
#[cfg(feature = "sm-raw-window-handle")]
pub fn from_raw_display_handle(
Expand All @@ -115,25 +105,6 @@ impl Connection {
Ok(Connection)
}

/// Creates a native widget type from the given `winit` window.
///
/// This type can be later used to create surfaces that render to the window.
#[cfg(feature = "sm-winit")]
#[inline]
pub fn create_native_widget_from_winit_window(
&self,
window: &Window,
) -> Result<NativeWidget, Error> {
use raw_window_handle::HasRawWindowHandle;
use raw_window_handle::RawWindowHandle::AndroidNdk;
match window.raw_window_handle() {
AndroidNdk(handle) => Ok(NativeWidget {
native_window: handle.a_native_window as *mut _,
}),
_ => Err(Error::IncompatibleNativeWidget),
}
}

/// Create a native widget from a raw pointer
pub unsafe fn create_native_widget_from_ptr(
&self,
Expand All @@ -148,9 +119,10 @@ impl Connection {
/// Create a native widget type from the given `raw_window_handle::RawWindowHandle`.
#[cfg(feature = "sm-raw-window-handle")]
#[inline]
pub fn create_native_widget_from_rwh(
pub fn create_native_widget_from_raw_window_handle(
&self,
raw_handle: raw_window_handle::RawWindowHandle,
_size: Size2D<i32>,
) -> Result<NativeWidget, Error> {
use raw_window_handle::RawWindowHandle::AndroidNdk;

Expand Down
Loading

0 comments on commit 3f09ecc

Please sign in to comment.