Skip to content

Commit

Permalink
Fixes after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
maxammann committed Jun 2, 2022
1 parent 707c193 commit 5dbff6d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
4 changes: 2 additions & 2 deletions android/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ maplibre-winit = { path = "../maplibre-winit", version = "0.0.1" }
env_logger = "0.9"
log = "0.4.16"
#ndk-glue = "0.5.0" # version is required by winit
ndk-glue = { git = "https://github.com/rust-windowing/android-ndk-rs.git", branch = "native-window-jni-surface" }
ndk = { git = "https://github.com/rust-windowing/android-ndk-rs.git", branch = "native-window-jni-surface" }
ndk-glue = { git = "https://github.com/rust-windowing/android-ndk-rs.git", rev = "7e33384" }
ndk = { git = "https://github.com/rust-windowing/android-ndk-rs.git", rev = "7e33384" }
jni = "0.19.0"
raw-window-handle = "0.4"
libc = "0.2"
Expand Down
11 changes: 8 additions & 3 deletions android/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,18 @@ pub extern "system" fn Java_org_maplibre_1rs_MapLibreRs_android_1main(
}

run_multithreaded(async {
MapBuilder::new()
let mut map = MapBuilder::new()
.with_map_window_config(AndroidMapWindowConfig::new(env, surface))
.with_http_client(ReqwestHttpClient::new(None))
.with_schedule_method(TokioScheduleMethod::new())
.with_wgpu_settings(WgpuSettings {
backends: Some(Backends::VULKAN),
..WgpuSettings::default()
})
.build()
.initialize()
.await
.run()
.await;
map.map_schedule_mut().late_init().await;
map.run()
})
}
62 changes: 31 additions & 31 deletions android/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use jni::objects::JObject;
use jni::JNIEnv;
use maplibre::error::Error;
use maplibre::io::scheduler::ScheduleMethod;
use maplibre::io::source_client::HTTPClient;
use maplibre::io::source_client::HttpClient;
use maplibre::map_schedule::InteractiveMapSchedule;
use ndk::native_window::NativeWindow;
use raw_window_handle::{AndroidNdkHandle, RawWindowHandle};
use std::marker::PhantomData;
use std::thread::sleep;
use std::time::Duration;

use maplibre::map_state::MapState;
use maplibre::window::{MapWindow, MapWindowConfig, Runnable, WindowSize};
use maplibre::window::{EventLoop, HeadedMapWindow, MapWindow, MapWindowConfig, WindowSize};

pub struct AndroidNativeWindow {
window: NativeWindow,
Expand All @@ -37,6 +37,18 @@ impl<'a> AndroidMapWindowConfig<'a> {

impl<'a> MapWindowConfig for AndroidMapWindowConfig<'a> {
type MapWindow = AndroidMapWindow<'a>;

fn create(&self) -> Self::MapWindow {
let window = unsafe {
NativeWindow::from_surface(self.env.get_native_interface(), self.surface.into_inner())
}
.unwrap();

Self::MapWindow {
window: AndroidNativeWindow { window },
phantom: Default::default(),
}
}
}

pub struct AndroidMapWindow<'a> {
Expand All @@ -50,58 +62,46 @@ impl AndroidMapWindow<'_> {
}
}

impl<'a, MWC, SM, HC> Runnable<MWC, SM, HC> for AndroidMapWindow<'a>
impl<'a, MWC, SM, HC> EventLoop<MWC, SM, HC> for AndroidMapWindow<'a>
where
MWC: MapWindowConfig<MapWindow = AndroidMapWindow<'a>>,
SM: ScheduleMethod,
HC: HTTPClient,
HC: HttpClient,
{
fn run(mut self, mut map_state: MapState<MWC, SM, HC>, max_frames: Option<u64>) {
fn run(
mut self,
mut map_schedule: InteractiveMapSchedule<MWC, SM, HC>,
max_frames: Option<u64>,
) {
for i in 0..100 {
map_state.update_and_redraw();
map_schedule.update_and_redraw();
sleep(Duration::from_millis(16))
}

match map_state.update_and_redraw() {
match map_schedule.update_and_redraw() {
Ok(_) => {}
Err(Error::Render(e)) => {
eprintln!("{}", e);
}
e => eprintln!("{:?}", e),
};

map_state.recreate_surface(&self);
let size = self.size();
map_state.resize(size.width(), size.height()); // FIXME: Resumed is also called when the app launches for the first time. Instead of first using a "fake" inner_size() in State::new we should initialize with a proper size from the beginning
map_state.resume();
map_schedule.resize(size.width(), size.height()); // FIXME: Resumed is also called when the app launches for the first time. Instead of first using a "fake" inner_size() in State::new we should initialize with a proper size from the beginning
map_schedule.resume(&self);
}
}

impl<'a> MapWindow for AndroidMapWindow<'a> {
type EventLoop = ();
type Window = AndroidNativeWindow;
type MapWindowConfig = AndroidMapWindowConfig<'a>;

fn create(map_window_config: &Self::MapWindowConfig) -> Self {
let window = unsafe {
NativeWindow::from_surface(
map_window_config.env.get_native_interface(),
map_window_config.surface.into_inner(),
)
}
.unwrap();

Self {
window: AndroidNativeWindow { window },
phantom: Default::default(),
}
}

fn size(&self) -> WindowSize {
WindowSize::new(100, 100).unwrap()
}
}

impl<'a> HeadedMapWindow for AndroidMapWindow<'a> {
type RawWindow = AndroidNativeWindow;

fn inner(&self) -> &Self::Window {
fn inner(&self) -> &Self::RawWindow {
&self.window
}
}
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
set shell := ["bash", "-c"]

export NIGHTLY_TOOLCHAIN := "nightly-2022-04-04-x86_64-unknown-linux-gnu"
export CARGO_TERM_COLOR := "always"
export RUST_BACKTRACE := "1"

install-clippy:
rustup component add clippy
Expand Down
2 changes: 1 addition & 1 deletion maplibre/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait HeadedMapWindow: MapWindow {

/// A configuration for a window which determines the corresponding implementation of a
/// [`MapWindow`] and is able to create it.
pub trait MapWindowConfig: 'static {
pub trait MapWindowConfig {
type MapWindow: MapWindow;

fn create(&self) -> Self::MapWindow;
Expand Down

0 comments on commit 5dbff6d

Please sign in to comment.