-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimentally render to an Android surface
- Loading branch information
Showing
10 changed files
with
221 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 30 additions & 2 deletions
32
android/gradle/demo/src/main/java/com/example/demo/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,43 @@ | ||
package com.example.demo | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.SurfaceHolder | ||
import android.view.SurfaceView | ||
import android.view.View | ||
import androidx.appcompat.app.AppCompatActivity | ||
import org.maplibre_rs.MapLibreRs | ||
|
||
// Currently not used. Instead the NativeActivity referenced in AndroidManifest.xml is used. | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
var mSurfaceView1: SurfaceView? = null | ||
var mSurfaceHolder1: SurfaceHolder? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
MapLibreRs.android_main() | ||
|
||
mSurfaceView1 = findViewById<View>(R.id.surfaceView1) as SurfaceView | ||
mSurfaceHolder1 = mSurfaceView1!!.getHolder() | ||
|
||
mSurfaceHolder1!!.addCallback(object : SurfaceHolder.Callback { | ||
override fun surfaceCreated(p0: SurfaceHolder) { | ||
Log.v("TAG", "surfaceCreated") | ||
MapLibreRs.android_main(p0.surface) | ||
} | ||
|
||
override fun surfaceChanged(p0: SurfaceHolder, p1: Int, p2: Int, p3: Int) { | ||
Log.v( | ||
"TAG", "surfaceChanged format=" + p1 + ", width=" + p2 + ", height=" | ||
+ p3 | ||
) | ||
} | ||
|
||
override fun surfaceDestroyed(p0: SurfaceHolder) { | ||
Log.v("TAG", "surfaceDestroyed") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
android/gradle/lib/src/main/java/org/maplibre_rs/MapLibreRs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use jni::objects::JObject; | ||
use jni::JNIEnv; | ||
use maplibre::error::Error; | ||
use maplibre::io::scheduler::ScheduleMethod; | ||
use maplibre::io::source_client::HTTPClient; | ||
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}; | ||
|
||
pub struct AndroidNativeWindow { | ||
window: NativeWindow, | ||
} | ||
|
||
pub struct AndroidMapWindowConfig<'a> { | ||
env: JNIEnv<'a>, | ||
surface: JObject<'a>, | ||
} | ||
|
||
unsafe impl raw_window_handle::HasRawWindowHandle for AndroidNativeWindow { | ||
fn raw_window_handle(&self) -> RawWindowHandle { | ||
let mut handle = AndroidNdkHandle::empty(); | ||
handle.a_native_window = unsafe { self.window.ptr().as_mut() as *mut _ as *mut _ }; | ||
RawWindowHandle::AndroidNdk(handle) | ||
} | ||
} | ||
|
||
impl<'a> AndroidMapWindowConfig<'a> { | ||
pub fn new(env: JNIEnv<'a>, surface: JObject<'a>) -> Self { | ||
Self { env, surface } | ||
} | ||
} | ||
|
||
impl<'a> MapWindowConfig for AndroidMapWindowConfig<'a> { | ||
type MapWindow = AndroidMapWindow<'a>; | ||
} | ||
|
||
pub struct AndroidMapWindow<'a> { | ||
window: AndroidNativeWindow, | ||
phantom: PhantomData<&'a u32>, | ||
} | ||
|
||
impl AndroidMapWindow<'_> { | ||
pub fn take_event_loop(&mut self) -> Option<()> { | ||
Some(()) | ||
} | ||
} | ||
|
||
impl<'a, MWC, SM, HC> Runnable<MWC, SM, HC> for AndroidMapWindow<'a> | ||
where | ||
MWC: MapWindowConfig<MapWindow = AndroidMapWindow<'a>>, | ||
SM: ScheduleMethod, | ||
HC: HTTPClient, | ||
{ | ||
fn run(mut self, mut map_state: MapState<MWC, SM, HC>, max_frames: Option<u64>) { | ||
for i in 0..100 { | ||
map_state.update_and_redraw(); | ||
sleep(Duration::from_millis(16)) | ||
} | ||
|
||
match map_state.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(); | ||
} | ||
} | ||
|
||
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() | ||
} | ||
|
||
fn inner(&self) -> &Self::Window { | ||
&self.window | ||
} | ||
} |