winit_surface_window
A helper library for using existing Android Surfaces (like Presentations) as windows within the Rust ecosystem, compatible with raw-window-handle.
This library allows you to create a window from an existing Android Surface object, which is particularly useful when working with Android Presentation APIs or other scenarios where you need to render to a secondary display or a specific surface.
Features
- Create
SurfaceWindowfrom Android Surface objects - Compatible with
raw-window-handlefor integration with various graphics libraries - Deep integration with winit event loop
- Thread-safe implementation
- Support for Android Presentation API for multi-display management
Core Components
Window Management
SurfaceWindow- Core window abstraction, created from Android SurfacePresentationWindow- Android Presentation window management
Winit Integration
winit_ext- Winit event loop extensions (requireswinit-integrationfeature)
Usage
Add this to your Cargo.toml:
[]
= { = "path/to/winit_surface_window" }
# Enable feature support
[]
= ["winit_surface_window/winit-integration"] # Winit integration support
Complete Usage Example
Here's a complete example using winit event loop:
use AndroidApp;
use ;
use EventLoopBuilderExtAndroid;
use WindowAttributes;
use ;
API Reference
Core Types
SurfaceWindow
The main struct representing an Android Surface as a window. Implements HasRawWindowHandle and HasRawDisplayHandle for compatibility with various graphics libraries.
// Create window from Android Surface
let surface_window = unsafe ;
// Get window dimensions
let width = surface_window.width;
let height = surface_window.height;
PresentationWindow
Android Presentation window manager for creating windows on secondary displays or specific displays.
// Create Presentation on secondary display
let presentation = new_on_secondary_display?;
// Create Presentation on specific display
let presentation = new_on_display?;
// Get available display list
let displays = get_available_displays?;
// Get underlying SurfaceWindow
if let Some = presentation.surface_window
Winit Integration (requires winit-integration feature)
EventLoopInitExt
EventLoop extension for initializing the Presentation system.
// Initialize using AndroidApp
event_loop.init_presentation_android_app?;
// Initialize using NativeActivity
event_loop.init_presentation_system_ndk?;
ActiveEventLoopPresentationExt
Extension for creating Presentations within event handlers.
// Create Presentation on secondary display
let presentation = event_loop.create_presentation_on_secondary_display?;
// Create Presentation on specific display
let presentation = event_loop.create_presentation_on_display?;
// Get available displays
let displays = event_loop.get_available_displays?;
Build Configuration
Android Target
# Basic build
# Enable Winit integration
Important Notes
- Thread Safety: All Presentation-related operations must be executed on the main UI thread.
- Initialization Timing: Ensure Presentation initialization in
Event::Resumedevent, not inRedrawRequested. - Resource Cleanup: Always call
presentation.dismiss()when the application closes to clean up resources. - Android Version: Requires Android API level 23+ support.
- Permission Requirements: May require
SYSTEM_ALERT_WINDOWpermission for secondary display.
License
This project is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.