From f0b695041ec8a5f7585fbffa2a3cf369e9e98020 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Sat, 23 Dec 2023 14:09:49 +0100 Subject: [PATCH] Nested surfaces! --- android_native_surface/src/lib.rs | 35 ++++++++++++++++++++++++--- android_native_surface/src/support.rs | 3 ++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/android_native_surface/src/lib.rs b/android_native_surface/src/lib.rs index c6769b1..df2155b 100644 --- a/android_native_surface/src/lib.rs +++ b/android_native_surface/src/lib.rs @@ -20,7 +20,7 @@ use ndk::{ hardware_buffer::HardwareBufferUsage, hardware_buffer_format::HardwareBufferFormat, media::image_reader::ImageReader, - native_window::NativeWindow, + native_window::{NativeWindow, NativeWindowTransform}, surface_control::{SurfaceControl, SurfaceTransaction}, surface_texture::SurfaceTexture, }; @@ -29,6 +29,10 @@ use raw_window_handle::{AndroidDisplayHandle, HasRawWindowHandle, RawDisplayHand mod support; fn render_to_native_window(og_window: NativeWindow) { + dbg!(SurfaceControl::create_from_window( + &og_window, + CStr::from_bytes_with_nul(b"OG\0").unwrap(), + )); dbg!(&og_window); // TODO: EGL can update the format of the window by choosing a different format, // but not if this producer (Surface/NativeWindow) comes from an ImageReader. @@ -131,8 +135,8 @@ fn render_to_native_window(og_window: NativeWindow) { let renderer = support::Renderer::new(&gl_display); renderer.resize(gl_window.window.width(), gl_window.window.height()); - dbg!(i.acquire_next_image()); - dbg!(unsafe { i.acquire_next_image_async() }); + // dbg!(i.acquire_next_image()); + // dbg!(unsafe { i.acquire_next_image_async() }); let draw = Instant::now(); renderer.draw(); @@ -162,7 +166,30 @@ fn render_to_native_window(og_window: NativeWindow) { // let img = img.unwrap(); dbg!(&img); dbg!(&fence); - t.set_buffer(&sc, &img.hardware_buffer().unwrap(), fence); + // t.set_buffer(&sc, &img.hardware_buffer().unwrap(), fence); + let hw = img.hardware_buffer().unwrap(); + t.set_buffer(&sc, &hw, fence); + for i in 0..5 { + let nested = + SurfaceControl::create(&sc, CStr::from_bytes_with_nul(b"nested\0").unwrap()).unwrap(); + t.set_buffer(&nested, &hw, None); + let left = og_window.width() / 5 * i; + let top = i * 100; + t.set_position(&nested, left, top); + t.set_scale( + &nested, + 512.0 / (og_window.width() as f32), + 512.0 / (og_window.height() as f32), + ); + t.set_buffer_transform(&nested, NativeWindowTransform::ROTATE_270); + t.set_buffer_alpha(&nested, i as f32 / 10.0 + 0.5); + // t.set_visibility(&nested, ndk::surface_control::Visibility::Show); + } + // if let Some(fence) = fence { + // img.delete_async(fence); + // } else { + // drop(img); + // } t.apply(); let drop_ = Instant::now(); diff --git a/android_native_surface/src/support.rs b/android_native_surface/src/support.rs index cd3e21a..a391ba6 100644 --- a/android_native_surface/src/support.rs +++ b/android_native_surface/src/support.rs @@ -212,7 +212,8 @@ impl Renderer { self.gl.BindVertexArray(self.vao); self.gl.BindBuffer(gl::ARRAY_BUFFER, self.vbo); - self.gl.ClearColor(0.1, 0.1, 0.1, 0.9); + // Keep it transparent + // self.gl.ClearColor(0.1, 0.1, 0.1, 0.0); self.gl.Clear(gl::COLOR_BUFFER_BIT); self.gl.DrawArrays(gl::TRIANGLES, 0, 3); }