|
1 | 1 | use android_activity::{AndroidApp, InputStatus, MainEvent, PollEvent};
|
2 | 2 | use log::info;
|
| 3 | +use ndk::native_window::HardwareBufferFormat; |
3 | 4 |
|
4 | 5 | #[no_mangle]
|
5 | 6 | fn android_main(app: AndroidApp) {
|
@@ -38,6 +39,14 @@ fn android_main(app: AndroidApp) {
|
38 | 39 | }
|
39 | 40 | MainEvent::InitWindow { .. } => {
|
40 | 41 | native_window = app.native_window();
|
| 42 | + if let Some(nw) = &native_window { |
| 43 | + nw.set_buffers_geometry( |
| 44 | + 0, |
| 45 | + 0, |
| 46 | + Some(HardwareBufferFormat::R8G8B8A8_UNORM), |
| 47 | + ) |
| 48 | + .unwrap() |
| 49 | + } |
41 | 50 | redraw_pending = true;
|
42 | 51 | }
|
43 | 52 | MainEvent::TerminateWindow { .. } => {
|
@@ -91,16 +100,15 @@ fn android_main(app: AndroidApp) {
|
91 | 100 | /// responsive, otherwise it will stop delivering input
|
92 | 101 | /// events to us.
|
93 | 102 | fn dummy_render(native_window: &ndk::native_window::NativeWindow) {
|
94 |
| - unsafe { |
95 |
| - let mut buf: ndk_sys::ANativeWindow_Buffer = std::mem::zeroed(); |
96 |
| - let mut rect: ndk_sys::ARect = std::mem::zeroed(); |
97 |
| - ndk_sys::ANativeWindow_lock( |
98 |
| - native_window.ptr().as_ptr() as _, |
99 |
| - &mut buf as _, |
100 |
| - &mut rect as _, |
101 |
| - ); |
102 |
| - // Note: we don't try and touch the buffer since that |
103 |
| - // also requires us to handle various buffer formats |
104 |
| - ndk_sys::ANativeWindow_unlockAndPost(native_window.ptr().as_ptr() as _); |
| 103 | + let mut lock = native_window.lock(None).unwrap(); |
| 104 | + let (w, h) = (lock.width(), lock.height()); |
| 105 | + |
| 106 | + for (y, line) in lock.lines().unwrap().enumerate() { |
| 107 | + let r = y * 255 / h; |
| 108 | + for (x, pixels) in line.chunks_mut(4).enumerate() { |
| 109 | + let g = x * 255 / w; |
| 110 | + pixels[0].write(r as u8); |
| 111 | + pixels[1].write(g as u8); |
| 112 | + } |
105 | 113 | }
|
106 | 114 | }
|
0 commit comments