Skip to content

Commit b079b26

Browse files
committed
No longer obtain multiple read locks on Android NativeWindow
1 parent cbeddb0 commit b079b26

File tree

1 file changed

+30
-11
lines changed
  • src/platform_impl/android

1 file changed

+30
-11
lines changed

src/platform_impl/android/mod.rs

+30-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use ndk::{
1313
native_window::NativeWindow,
1414
};
1515
use ndk_glue::{Event, Rect};
16+
use raw_window_handle::RawWindowHandle;
1617
use std::{
1718
collections::VecDeque,
1819
sync::{Arc, Mutex, RwLock, RwLockReadGuard},
@@ -72,6 +73,7 @@ impl<T: 'static> EventLoop<T> {
7273
Self {
7374
window_target: event_loop::EventLoopWindowTarget {
7475
p: EventLoopWindowTarget {
76+
raw_window_handle: Default::default(),
7577
_marker: std::marker::PhantomData,
7678
},
7779
_marker: std::marker::PhantomData,
@@ -117,6 +119,9 @@ impl<T: 'static> EventLoop<T> {
117119
let native_window_lock = ndk_glue::native_window();
118120
// The window could have gone away before we got the message
119121
if native_window_lock.is_some() {
122+
self.window_target
123+
.p
124+
.update_native_window(native_window_lock.as_ref());
120125
self.native_window_lock = Some(native_window_lock);
121126
call_event_handler!(
122127
event_handler,
@@ -140,6 +145,7 @@ impl<T: 'static> EventLoop<T> {
140145
event::Event::Suspended
141146
);
142147
}
148+
self.window_target.p.update_native_window(None);
143149
}
144150
Event::Pause => self.running = false,
145151
Event::Resume => self.running = true,
@@ -418,10 +424,19 @@ impl<T> Clone for EventLoopProxy<T> {
418424
}
419425

420426
pub struct EventLoopWindowTarget<T: 'static> {
427+
raw_window_handle: Arc<Mutex<Option<RawWindowHandle>>>,
421428
_marker: std::marker::PhantomData<T>,
422429
}
423430

424431
impl<T: 'static> EventLoopWindowTarget<T> {
432+
fn update_native_window(&self, native_window: Option<&NativeWindow>) {
433+
*self.raw_window_handle.lock().unwrap() = native_window.map(|native_window| {
434+
let mut handle = raw_window_handle::android::AndroidHandle::empty();
435+
handle.a_native_window = unsafe { native_window.ptr().as_mut() as *mut _ as *mut _ };
436+
RawWindowHandle::Android(handle)
437+
});
438+
}
439+
425440
pub fn primary_monitor(&self) -> Option<monitor::MonitorHandle> {
426441
Some(monitor::MonitorHandle {
427442
inner: MonitorHandle,
@@ -456,16 +471,20 @@ impl DeviceId {
456471
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
457472
pub struct PlatformSpecificWindowBuilderAttributes;
458473

459-
pub struct Window;
474+
pub struct Window {
475+
raw_window_handle: Arc<Mutex<Option<RawWindowHandle>>>,
476+
}
460477

461478
impl Window {
462479
pub fn new<T: 'static>(
463-
_el: &EventLoopWindowTarget<T>,
480+
el: &EventLoopWindowTarget<T>,
464481
_window_attrs: window::WindowAttributes,
465482
_: PlatformSpecificWindowBuilderAttributes,
466483
) -> Result<Self, error::OsError> {
467484
// FIXME this ignores requested window attributes
468-
Ok(Self)
485+
Ok(Self {
486+
raw_window_handle: Arc::clone(&el.raw_window_handle),
487+
})
469488
}
470489

471490
pub fn id(&self) -> WindowId {
@@ -581,14 +600,14 @@ impl Window {
581600
}
582601

583602
pub fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
584-
let a_native_window = if let Some(native_window) = ndk_glue::native_window().as_ref() {
585-
unsafe { native_window.ptr().as_mut() as *mut _ as *mut _ }
586-
} else {
587-
panic!("Cannot get the native window, it's null and will always be null before Event::Resumed and after Event::Suspended. Make sure you only call this function between those events.");
588-
};
589-
let mut handle = raw_window_handle::android::AndroidHandle::empty();
590-
handle.a_native_window = a_native_window;
591-
raw_window_handle::RawWindowHandle::Android(handle)
603+
self.raw_window_handle
604+
.lock()
605+
.unwrap()
606+
.as_ref()
607+
.expect(
608+
"The window can be obtained only between `Event::Resumed` and `Event::Suspended`!",
609+
)
610+
.clone()
592611
}
593612

594613
pub fn config(&self) -> Configuration {

0 commit comments

Comments
 (0)