mirror of
				https://github.com/slint-ui/slint.git
				synced 2025-11-03 21:24:17 +00:00 
			
		
		
		
	Simplify OpenGL examples to use platform builder API to enforce a GL supported renderer
This commit is contained in:
		
							parent
							
								
									687e4ce95b
								
							
						
					
					
						commit
						df55e5563b
					
				
					 2 changed files with 89 additions and 85 deletions
				
			
		| 
						 | 
					@ -398,51 +398,53 @@ impl DemoRenderer {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn main() {
 | 
					fn main() {
 | 
				
			||||||
 | 
					    let platform = slint::platform::PlatformBuilder::new()
 | 
				
			||||||
 | 
					        .with_opengl_api(slint::OpenGLAPI::GLES(None))
 | 
				
			||||||
 | 
					        .build()
 | 
				
			||||||
 | 
					        .expect("Unable to create Slint backend with OpenGL ES renderer");
 | 
				
			||||||
 | 
					    slint::platform::set_platform(platform).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let app = App::new().unwrap();
 | 
					    let app = App::new().unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut underlay = None;
 | 
					    let mut underlay = None;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let app_weak = app.as_weak();
 | 
					    let app_weak = app.as_weak();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if let Err(error) = app.window().set_rendering_notifier(move |state, graphics_api| {
 | 
					    app.window()
 | 
				
			||||||
        // eprintln!("rendering state {:#?}", state);
 | 
					        .set_rendering_notifier(move |state, graphics_api| {
 | 
				
			||||||
 | 
					            // eprintln!("rendering state {:#?}", state);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        match state {
 | 
					            match state {
 | 
				
			||||||
            slint::RenderingState::RenderingSetup => {
 | 
					                slint::RenderingState::RenderingSetup => {
 | 
				
			||||||
                let context = match graphics_api {
 | 
					                    let context = match graphics_api {
 | 
				
			||||||
                    slint::GraphicsAPI::NativeOpenGL { get_proc_address } => unsafe {
 | 
					                        slint::GraphicsAPI::NativeOpenGL { get_proc_address } => unsafe {
 | 
				
			||||||
                        glow::Context::from_loader_function_cstr(|s| get_proc_address(s))
 | 
					                            glow::Context::from_loader_function_cstr(|s| get_proc_address(s))
 | 
				
			||||||
                    },
 | 
					                        },
 | 
				
			||||||
                    _ => return,
 | 
					                        _ => return,
 | 
				
			||||||
                };
 | 
					                    };
 | 
				
			||||||
                underlay = Some(DemoRenderer::new(context))
 | 
					                    underlay = Some(DemoRenderer::new(context))
 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            slint::RenderingState::BeforeRendering => {
 | 
					 | 
				
			||||||
                if let (Some(underlay), Some(app)) = (underlay.as_mut(), app_weak.upgrade()) {
 | 
					 | 
				
			||||||
                    let texture = underlay.render(
 | 
					 | 
				
			||||||
                        app.get_selected_red(),
 | 
					 | 
				
			||||||
                        app.get_selected_green(),
 | 
					 | 
				
			||||||
                        app.get_selected_blue(),
 | 
					 | 
				
			||||||
                        app.get_requested_texture_width() as u32,
 | 
					 | 
				
			||||||
                        app.get_requested_texture_height() as u32,
 | 
					 | 
				
			||||||
                    );
 | 
					 | 
				
			||||||
                    app.set_texture(slint::Image::from(texture));
 | 
					 | 
				
			||||||
                    app.window().request_redraw();
 | 
					 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					                slint::RenderingState::BeforeRendering => {
 | 
				
			||||||
 | 
					                    if let (Some(underlay), Some(app)) = (underlay.as_mut(), app_weak.upgrade()) {
 | 
				
			||||||
 | 
					                        let texture = underlay.render(
 | 
				
			||||||
 | 
					                            app.get_selected_red(),
 | 
				
			||||||
 | 
					                            app.get_selected_green(),
 | 
				
			||||||
 | 
					                            app.get_selected_blue(),
 | 
				
			||||||
 | 
					                            app.get_requested_texture_width() as u32,
 | 
				
			||||||
 | 
					                            app.get_requested_texture_height() as u32,
 | 
				
			||||||
 | 
					                        );
 | 
				
			||||||
 | 
					                        app.set_texture(slint::Image::from(texture));
 | 
				
			||||||
 | 
					                        app.window().request_redraw();
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                slint::RenderingState::AfterRendering => {}
 | 
				
			||||||
 | 
					                slint::RenderingState::RenderingTeardown => {
 | 
				
			||||||
 | 
					                    drop(underlay.take());
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                _ => {}
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            slint::RenderingState::AfterRendering => {}
 | 
					        })
 | 
				
			||||||
            slint::RenderingState::RenderingTeardown => {
 | 
					        .expect("Unable to set rendering notifier");
 | 
				
			||||||
                drop(underlay.take());
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            _ => {}
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }) {
 | 
					 | 
				
			||||||
        match error {
 | 
					 | 
				
			||||||
            slint::SetRenderingNotifierError::Unsupported => eprintln!("This example requires the use of the GL backend. Please run with the environment variable SLINT_BACKEND=GL set."),
 | 
					 | 
				
			||||||
            _ => unreachable!()
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        std::process::exit(1);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    app.run().unwrap();
 | 
					    app.run().unwrap();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -176,67 +176,69 @@ pub fn main() {
 | 
				
			||||||
    #[cfg(all(debug_assertions, target_arch = "wasm32"))]
 | 
					    #[cfg(all(debug_assertions, target_arch = "wasm32"))]
 | 
				
			||||||
    console_error_panic_hook::set_once();
 | 
					    console_error_panic_hook::set_once();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let platform = slint::platform::PlatformBuilder::new()
 | 
				
			||||||
 | 
					        .with_opengl_api(slint::OpenGLAPI::GLES(None))
 | 
				
			||||||
 | 
					        .build()
 | 
				
			||||||
 | 
					        .expect("Unable to create Slint backend with OpenGL ES renderer");
 | 
				
			||||||
 | 
					    slint::platform::set_platform(platform).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let app = App::new().unwrap();
 | 
					    let app = App::new().unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut underlay = None;
 | 
					    let mut underlay = None;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let app_weak = app.as_weak();
 | 
					    let app_weak = app.as_weak();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if let Err(error) = app.window().set_rendering_notifier(move |state, graphics_api| {
 | 
					    app.window()
 | 
				
			||||||
        // eprintln!("rendering state {:#?}", state);
 | 
					        .set_rendering_notifier(move |state, graphics_api| {
 | 
				
			||||||
 | 
					            // eprintln!("rendering state {:#?}", state);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        match state {
 | 
					            match state {
 | 
				
			||||||
            slint::RenderingState::RenderingSetup => {
 | 
					                slint::RenderingState::RenderingSetup => {
 | 
				
			||||||
                let context = match graphics_api {
 | 
					                    let context = match graphics_api {
 | 
				
			||||||
                    #[cfg(not(target_arch = "wasm32"))]
 | 
					                        #[cfg(not(target_arch = "wasm32"))]
 | 
				
			||||||
                    slint::GraphicsAPI::NativeOpenGL { get_proc_address } => unsafe {
 | 
					                        slint::GraphicsAPI::NativeOpenGL { get_proc_address } => unsafe {
 | 
				
			||||||
                        glow::Context::from_loader_function_cstr(|s| get_proc_address(s))
 | 
					                            glow::Context::from_loader_function_cstr(|s| get_proc_address(s))
 | 
				
			||||||
                    },
 | 
					                        },
 | 
				
			||||||
                    #[cfg(target_arch = "wasm32")]
 | 
					                        #[cfg(target_arch = "wasm32")]
 | 
				
			||||||
                    slint::GraphicsAPI::WebGL { canvas_element_id, context_type } => {
 | 
					                        slint::GraphicsAPI::WebGL { canvas_element_id, context_type } => {
 | 
				
			||||||
                        use wasm_bindgen::JsCast;
 | 
					                            use wasm_bindgen::JsCast;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        let canvas = web_sys::window()
 | 
					                            let canvas = web_sys::window()
 | 
				
			||||||
                            .unwrap()
 | 
					                                .unwrap()
 | 
				
			||||||
                            .document()
 | 
					                                .document()
 | 
				
			||||||
                            .unwrap()
 | 
					                                .unwrap()
 | 
				
			||||||
                            .get_element_by_id(canvas_element_id)
 | 
					                                .get_element_by_id(canvas_element_id)
 | 
				
			||||||
                            .unwrap()
 | 
					                                .unwrap()
 | 
				
			||||||
                            .dyn_into::<web_sys::HtmlCanvasElement>()
 | 
					                                .dyn_into::<web_sys::HtmlCanvasElement>()
 | 
				
			||||||
                            .unwrap();
 | 
					                                .unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        let webgl1_context = canvas
 | 
					                            let webgl1_context = canvas
 | 
				
			||||||
                            .get_context(context_type)
 | 
					                                .get_context(context_type)
 | 
				
			||||||
                            .unwrap()
 | 
					                                .unwrap()
 | 
				
			||||||
                            .unwrap()
 | 
					                                .unwrap()
 | 
				
			||||||
                            .dyn_into::<web_sys::WebGl2RenderingContext>()
 | 
					                                .dyn_into::<web_sys::WebGl2RenderingContext>()
 | 
				
			||||||
                            .unwrap();
 | 
					                                .unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        glow::Context::from_webgl2_context(webgl1_context)
 | 
					                            glow::Context::from_webgl2_context(webgl1_context)
 | 
				
			||||||
                    }
 | 
					                        }
 | 
				
			||||||
                    _ => return,
 | 
					                        _ => return,
 | 
				
			||||||
                };
 | 
					                    };
 | 
				
			||||||
                underlay = Some(EGLUnderlay::new(context))
 | 
					                    underlay = Some(EGLUnderlay::new(context))
 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            slint::RenderingState::BeforeRendering => {
 | 
					 | 
				
			||||||
                if let (Some(underlay), Some(app)) = (underlay.as_mut(), app_weak.upgrade()) {
 | 
					 | 
				
			||||||
                    underlay.render(app.get_rotation_enabled());
 | 
					 | 
				
			||||||
                    app.window().request_redraw();
 | 
					 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					                slint::RenderingState::BeforeRendering => {
 | 
				
			||||||
 | 
					                    if let (Some(underlay), Some(app)) = (underlay.as_mut(), app_weak.upgrade()) {
 | 
				
			||||||
 | 
					                        underlay.render(app.get_rotation_enabled());
 | 
				
			||||||
 | 
					                        app.window().request_redraw();
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                slint::RenderingState::AfterRendering => {}
 | 
				
			||||||
 | 
					                slint::RenderingState::RenderingTeardown => {
 | 
				
			||||||
 | 
					                    drop(underlay.take());
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                _ => {}
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            slint::RenderingState::AfterRendering => {}
 | 
					        })
 | 
				
			||||||
            slint::RenderingState::RenderingTeardown => {
 | 
					        .expect("Unable to set rendering notifier");
 | 
				
			||||||
                drop(underlay.take());
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            _ => {}
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }) {
 | 
					 | 
				
			||||||
        match error {
 | 
					 | 
				
			||||||
            slint::SetRenderingNotifierError::Unsupported => eprintln!("This example requires the use of the GL backend. Please run with the environment variable SLINT_BACKEND=GL set."),
 | 
					 | 
				
			||||||
            _ => unreachable!()
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        std::process::exit(1);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    app.run().unwrap();
 | 
					    app.run().unwrap();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue