camera: image rwlock optimization

This commit is contained in:
ardocrat 2024-05-15 14:50:17 +03:00
parent 957b5e58ae
commit 5f976e9166
2 changed files with 17 additions and 16 deletions

View file

@ -13,10 +13,11 @@
// limitations under the License. // limitations under the License.
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::sync::{Arc, RwLock}; use std::sync::Arc;
use parking_lot::RwLock;
use jni::JNIEnv; use jni::JNIEnv;
use jni::objects::{JByteArray, JObject, JString, JValue}; use jni::objects::{JByteArray, JObject, JString, JValue};
use winit::platform::android::activity::AndroidApp; use winit::platform::android::activity::AndroidApp;
use crate::gui::platform::PlatformCallbacks; use crate::gui::platform::PlatformCallbacks;
@ -84,7 +85,7 @@ impl PlatformCallbacks for Android {
fn start_camera(&self) { fn start_camera(&self) {
// Clear image. // Clear image.
let mut w_image = LAST_CAMERA_IMAGE.write().unwrap(); let mut w_image = LAST_CAMERA_IMAGE.write();
*w_image = None; *w_image = None;
// Start camera. // Start camera.
self.call_java_method("startCamera", "()V", &[]).unwrap(); self.call_java_method("startCamera", "()V", &[]).unwrap();
@ -94,12 +95,12 @@ impl PlatformCallbacks for Android {
// Stop camera. // Stop camera.
self.call_java_method("stopCamera", "()V", &[]).unwrap(); self.call_java_method("stopCamera", "()V", &[]).unwrap();
// Clear image. // Clear image.
let mut w_image = LAST_CAMERA_IMAGE.write().unwrap(); let mut w_image = LAST_CAMERA_IMAGE.write();
*w_image = None; *w_image = None;
} }
fn camera_image(&self) -> Option<(Vec<u8>, u32)> { fn camera_image(&self) -> Option<(Vec<u8>, u32)> {
let r_image = LAST_CAMERA_IMAGE.read().unwrap(); let r_image = LAST_CAMERA_IMAGE.read();
if r_image.is_some() { if r_image.is_some() {
return Some(r_image.clone().unwrap()); return Some(r_image.clone().unwrap());
} }
@ -134,7 +135,6 @@ pub extern "C" fn Java_mw_gri_android_MainActivity_onCameraImage(
) { ) {
let arr = unsafe { JByteArray::from_raw(buff) }; let arr = unsafe { JByteArray::from_raw(buff) };
let image : Vec<u8> = env.convert_byte_array(arr).unwrap(); let image : Vec<u8> = env.convert_byte_array(arr).unwrap();
if let Ok(mut w_image) = LAST_CAMERA_IMAGE.write() { let mut w_image = LAST_CAMERA_IMAGE.write();
*w_image = Some((image, rotation as u32)); *w_image = Some((image, rotation as u32));
}
} }

View file

@ -13,7 +13,8 @@
// limitations under the License. // limitations under the License.
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::sync::{Arc, RwLock}; use std::sync::Arc;
use parking_lot::RwLock;
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering}; use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
use std::thread; use std::thread;
use eye::hal::PlatformContext; use eye::hal::PlatformContext;
@ -56,7 +57,7 @@ impl PlatformCallbacks for Desktop {
fn start_camera(&self) { fn start_camera(&self) {
// Clear image. // Clear image.
{ {
let mut w_image = LAST_CAMERA_IMAGE.write().unwrap(); let mut w_image = LAST_CAMERA_IMAGE.write();
*w_image = None; *w_image = None;
} }
@ -107,17 +108,17 @@ impl PlatformCallbacks for Desktop {
// Get data from frame. // Get data from frame.
if let Ok(frame_data) = frame { if let Ok(frame_data) = frame {
// Save image. // Save image.
let mut w_image = LAST_CAMERA_IMAGE.write().unwrap(); let mut w_image = LAST_CAMERA_IMAGE.write();
*w_image = Some((frame_data.to_vec(), 0)); *w_image = Some((frame_data.to_vec(), 0));
} else { } else {
// Clear image. // Clear image.
let mut w_image = LAST_CAMERA_IMAGE.write().unwrap(); let mut w_image = LAST_CAMERA_IMAGE.write();
*w_image = None; *w_image = None;
break; break;
} }
} else { } else {
// Clear image. // Clear image.
let mut w_image = LAST_CAMERA_IMAGE.write().unwrap(); let mut w_image = LAST_CAMERA_IMAGE.write();
*w_image = None; *w_image = None;
break; break;
} }
@ -131,14 +132,14 @@ impl PlatformCallbacks for Desktop {
// Stop camera. // Stop camera.
self.stop_camera.store(true, Ordering::Relaxed); self.stop_camera.store(true, Ordering::Relaxed);
// Clear image. // Clear image.
let mut w_image = LAST_CAMERA_IMAGE.write().unwrap(); let mut w_image = LAST_CAMERA_IMAGE.write();
*w_image = None; *w_image = None;
} }
fn camera_image(&self) -> Option<(Vec<u8>, u32)> { fn camera_image(&self) -> Option<(Vec<u8>, u32)> {
let r_image = LAST_CAMERA_IMAGE.read().unwrap(); let r_image = LAST_CAMERA_IMAGE.read();
if r_image.is_some() { if r_image.is_some() {
return Some(r_image.clone().unwrap()); return r_image.clone();
} }
None None
} }