cranpose 0.0.64

Cranpose runtime and UI facade
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! Android runtime for Compose applications.
//!
//! This module provides the Android event loop implementation with proper
//! lifecycle management, input handling, and rendering coordination.

use crate::launcher::AppSettings;
use cranpose_app_shell::{default_root_key, AppShell};
use cranpose_platform_android::AndroidPlatform;
use cranpose_render_wgpu::WgpuRenderer;
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc,
};

/// GPU resources for the surface (recreated when window is destroyed/created).
struct GpuResources {
    surface: wgpu::Surface<'static>,
    device: Arc<wgpu::Device>,
    config: wgpu::SurfaceConfiguration,
}

/// Pending input event to be processed outside poll_events callback.
/// This prevents blocking the main thread during input event acknowledgment.
enum PendingInput {
    PointerDown(f32, f32),
    PointerUp(f32, f32),
    PointerMove(f32, f32),
}

/// Get display density from Android NDK Configuration.
///
/// Uses the NDK's AConfiguration_getDensity which returns density constants
/// mapped to the standard Android density classes:
/// - mdpi: 1.0 (160 dpi baseline)
/// - hdpi: 1.5 (240 dpi)
/// - xhdpi: 2.0 (320 dpi) - most common modern phones
/// - xxhdpi: 3.0 (480 dpi)
/// - xxxhdpi: 4.0 (640 dpi)
///
/// The factor is calculated as DPI / 160 per Android NDK documentation.
fn get_display_density(app: &android_activity::AndroidApp) -> f32 {
    let config = app.config();
    let density_dpi = config.density(); // Returns Option<u32> with raw DPI value

    // Convert DPI to scale factor (baseline is 160 dpi = 1.0x)
    // e.g., 320 dpi / 160 = 2.0x (xhdpi)
    density_dpi.map(|dpi| dpi as f32 / 160.0).unwrap_or(2.0) // Fallback to xhdpi (2.0) if density unavailable
}

/// Renders a single frame. Returns true if out of memory (should exit).
fn render_once(resources: &mut GpuResources, shell: &mut AppShell<WgpuRenderer>) -> bool {
    shell.update();

    match resources.surface.get_current_texture() {
        Ok(frame) => {
            let view = frame
                .texture
                .create_view(&wgpu::TextureViewDescriptor::default());
            let (width, height) = shell.buffer_size();

            if let Err(e) = shell.renderer().render(&view, width, height) {
                log::error!("Render error: {:?}", e);
            }

            frame.present();
            false
        }
        Err(wgpu::SurfaceError::Lost) | Err(wgpu::SurfaceError::Outdated) => {
            // Reconfigure surface using current size and config
            let (width, height) = shell.buffer_size();
            resources.config.width = width;
            resources.config.height = height;
            resources
                .surface
                .configure(&resources.device, &resources.config);
            false
        }
        Err(wgpu::SurfaceError::OutOfMemory) => {
            log::error!("Out of memory; exiting");
            true
        }
        Err(e) => {
            log::debug!("Surface error: {:?}", e);
            false
        }
    }
}

/// Runs an Android Compose application with wgpu rendering.
///
/// Called by `AppLauncher::run_android()`. This is the framework-level
/// entrypoint that manages the Android lifecycle and event loop.
///
/// **Note:** Applications should use `AppLauncher` instead of calling this directly.
pub fn run(
    app: android_activity::AndroidApp,
    settings: AppSettings,
    content: impl FnMut() + 'static,
) {
    use android_activity::{input::MotionAction, InputStatus, MainEvent, PollEvent};

    // Install panic hook for better crash logging in Logcat
    std::panic::set_hook(Box::new(|panic_info| {
        let location = panic_info
            .location()
            .map(|l| format!("{}:{}:{}", l.file(), l.line(), l.column()))
            .unwrap_or_else(|| "unknown location".to_string());
        let message = panic_info
            .payload()
            .downcast_ref::<&str>()
            .map(|s| *s)
            .or_else(|| {
                panic_info
                    .payload()
                    .downcast_ref::<String>()
                    .map(|s| s.as_str())
            })
            .unwrap_or("Box<dyn Any>");
        log::error!("PANIC at {}: {}", location, message);
    }));

    // Wrap content in Rc<RefCell> for reuse across window recreations
    let content = std::rc::Rc::new(std::cell::RefCell::new(content));

    // App shell (created once, persists across window recreations)
    let mut app_shell: Option<AppShell<WgpuRenderer>> = None;

    // Initialize logging
    android_logger::init_once(
        android_logger::Config::default()
            .with_max_level(log::LevelFilter::Info)
            .with_tag("ComposeRS")
            .with_filter(
                android_logger::FilterBuilder::new()
                    .filter_level(log::LevelFilter::Info)
                    .filter_module("wgpu_core", log::LevelFilter::Warn)
                    .filter_module("wgpu_hal", log::LevelFilter::Warn)
                    .filter_module("naga", log::LevelFilter::Warn)
                    .build(),
            ),
    );

    log::info!("Starting Compose Android Application");

    // Frame wake flag for event-driven rendering
    let need_frame = Arc::new(AtomicBool::new(false));

    // Exit flag for Destroy event (can't break from inside poll_events closure)
    let should_exit = Arc::new(AtomicBool::new(false));

    // Initialize wgpu instance with GL and Vulkan backends
    // Use DISCARD_HAL_LABELS to prevent crash in emulator's Vulkan debug utils
    // (vk_common_SetDebugUtilsObjectNameEXT crashes on null labels)
    let backends = wgpu::Backends::GL | wgpu::Backends::VULKAN;

    let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
        backends,
        flags: wgpu::InstanceFlags::empty(), // No debug/validation - prevents label crash
        ..Default::default()
    });

    // Platform abstraction for density/pointer conversion
    let mut android_platform = AndroidPlatform::new();

    // GPU resources (recreated when window is destroyed/created)
    let mut gpu_resources: Option<GpuResources> = None;

    // Queue for input events (processed outside poll_events to prevent ANR)
    let mut pending_inputs: Vec<PendingInput> = Vec::new();

    // Main event loop
    loop {
        // Dynamic poll duration:
        // - ZERO when dirty, animating, OR pending inputs (immediate processing)
        // - Short timeout (16ms ~= 60fps) otherwise to ensure responsive input handling
        //   (Never use None/infinite wait as it can cause ANR if events arrive between checks)
        let poll_duration = if !pending_inputs.is_empty() {
            Some(std::time::Duration::ZERO) // Process pending inputs immediately
        } else if gpu_resources.is_none() {
            Some(std::time::Duration::from_millis(100)) // No window, poll occasionally
        } else if let Some(shell) = &app_shell {
            if shell.needs_redraw() {
                Some(std::time::Duration::ZERO) // Dirty or animating, tight loop
            } else {
                Some(std::time::Duration::from_millis(16)) // Idle, poll at ~60fps for responsive input
            }
        } else {
            Some(std::time::Duration::from_millis(100))
        };

        app.poll_events(poll_duration, |event| {
            match event {
                PollEvent::Main(main_event) => match main_event {
                    MainEvent::InitWindow { .. } => {
                        log::info!("Window initialized, setting up rendering");

                        if let Some(native_window) = app.native_window() {
                            // Get actual window dimensions
                            let width = native_window.width() as u32;
                            let height = native_window.height() as u32;

                            // Create surface using raw window handle from NativeWindow
                            let surface = unsafe {
                                use raw_window_handle::{
                                    AndroidDisplayHandle, AndroidNdkWindowHandle, RawDisplayHandle,
                                    RawWindowHandle,
                                };

                                let window_handle = AndroidNdkWindowHandle::new(
                                    std::ptr::NonNull::new(native_window.ptr().as_ptr() as *mut _)
                                        .expect("NativeWindow pointer is null"),
                                );
                                let raw_window_handle = RawWindowHandle::AndroidNdk(window_handle);

                                let display_handle = AndroidDisplayHandle::new();
                                let raw_display_handle = RawDisplayHandle::Android(display_handle);

                                let target = wgpu::SurfaceTargetUnsafe::RawHandle {
                                    raw_display_handle,
                                    raw_window_handle,
                                };

                                instance
                                    .create_surface_unsafe(target)
                                    .expect("Failed to create WGPU surface")
                            };

                            // Request adapter
                            let adapter = pollster::block_on(instance.request_adapter(
                                &wgpu::RequestAdapterOptions {
                                    power_preference: wgpu::PowerPreference::HighPerformance,
                                    compatible_surface: Some(&surface),
                                    force_fallback_adapter: false,
                                },
                            ))
                            .expect("Failed to find suitable adapter");

                            let adapter_info = adapter.get_info();
                            log::info!("Found adapter: {:?}", adapter_info.backend);

                            // Request device and queue
                            // Use downlevel limits for broad compatibility, with adapter's actual limits
                            let (device, queue) = pollster::block_on(
                                adapter.request_device(&wgpu::DeviceDescriptor {
                                    label: Some("Android Device"),
                                    required_features: wgpu::Features::empty(),
                                    required_limits: wgpu::Limits::downlevel_defaults()
                                        .using_resolution(adapter.limits()),
                                    experimental_features: wgpu::ExperimentalFeatures::disabled(),
                                    memory_hints: wgpu::MemoryHints::default(),
                                    trace: wgpu::Trace::Off,
                                }),
                            )
                            .expect("Failed to create device");

                            let device = Arc::new(device);
                            let queue = Arc::new(queue);

                            // Get surface capabilities and format
                            let surface_caps = surface.get_capabilities(&adapter);
                            let surface_format = surface_caps
                                .formats
                                .iter()
                                .copied()
                                .find(|f| f.is_srgb())
                                .unwrap_or(surface_caps.formats[0]);

                            // Configure surface
                            let present_mode =
                                crate::present_mode::select_present_mode(&surface_caps);
                            let surface_config = wgpu::SurfaceConfiguration {
                                usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
                                format: surface_format,
                                width,
                                height,
                                present_mode,
                                alpha_mode: surface_caps.alpha_modes[0],
                                view_formats: vec![],
                                desired_maximum_frame_latency: 2,
                            };

                            surface.configure(&device, &surface_config);

                            // Get display density and update platform
                            let density = get_display_density(&app);
                            android_platform.set_scale_factor(density as f64);
                            cranpose_ui::set_density(density);
                            log::info!("Display density: {:.2}x", density);

                            // Create or reuse app shell
                            if app_shell.is_none() {
                                // First initialization - create renderer and app shell
                                let fonts: &[&[u8]] = settings.fonts.unwrap_or(&[]);
                                let mut renderer = WgpuRenderer::new(fonts);
                                renderer.init_gpu(
                                    device.clone(),
                                    queue.clone(),
                                    surface_format,
                                    adapter_info.backend,
                                );
                                renderer.set_root_scale(density);

                                // Create app shell with content closure
                                let content_clone = content.clone();
                                let shell =
                                    AppShell::new(renderer, default_root_key(), move || {
                                        content_clone.borrow_mut()()
                                    });

                                app_shell = Some(shell);

                                // Wire frame waker for event-driven rendering
                                if let Some(shell) = &mut app_shell {
                                    let need_frame = need_frame.clone();
                                    shell.set_frame_waker(move || {
                                        need_frame.store(true, Ordering::Relaxed);
                                    });
                                }

                                log::info!("App shell created");
                            } else {
                                // Window recreated - reinitialize GPU resources
                                if let Some(shell) = &mut app_shell {
                                    shell.renderer().init_gpu(
                                        device.clone(),
                                        queue.clone(),
                                        surface_format,
                                        adapter_info.backend,
                                    );
                                    shell.renderer().set_root_scale(density);
                                    cranpose_ui::set_density(density);
                                    log::info!("Renderer reinitialized with new GPU resources");
                                }
                            }

                            // Set buffer_size and viewport
                            if let Some(shell) = &mut app_shell {
                                shell.set_buffer_size(width, height);

                                let width_dp = width as f32 / density;
                                let height_dp = height as f32 / density;
                                shell.set_viewport(width_dp, height_dp);
                                log::info!(
                                    "Set viewport to {:.1}x{:.1} dp ({}x{} px at {:.2}x density)",
                                    width_dp,
                                    height_dp,
                                    width,
                                    height,
                                    density
                                );
                            }

                            // Store GPU resources
                            gpu_resources = Some(GpuResources {
                                surface,
                                device,
                                config: surface_config,
                            });

                            log::info!("Rendering initialized successfully");
                        }
                    }
                    MainEvent::TerminateWindow { .. } => {
                        log::info!("Window terminated");
                        gpu_resources = None;
                    }
                    MainEvent::WindowResized { .. } => {
                        if let Some(native_window) = app.native_window() {
                            let width = native_window.width() as u32;
                            let height = native_window.height() as u32;

                            let density = get_display_density(&app);
                            android_platform.set_scale_factor(density as f64);
                            cranpose_ui::set_density(density);
                            log::info!(
                                "Window resized to {}x{} at {:.2}x density",
                                width,
                                height,
                                density
                            );

                            if let (Some(resources), Some(shell)) =
                                (&mut gpu_resources, &mut app_shell)
                            {
                                if width > 0 && height > 0 {
                                    resources.config.width = width;
                                    resources.config.height = height;
                                    resources
                                        .surface
                                        .configure(&resources.device, &resources.config);

                                    // Set buffer_size to physical pixels
                                    shell.set_buffer_size(width, height);

                                    // Set viewport to logical dp (marks dirty internally)
                                    let width_dp = width as f32 / density;
                                    let height_dp = height as f32 / density;
                                    shell.set_viewport(width_dp, height_dp);

                                    // Update renderer scale
                                    shell.renderer().set_root_scale(density);
                                    cranpose_ui::set_density(density);
                                }
                            }
                        }
                    }
                    MainEvent::RedrawNeeded { .. } => {
                        if let Some(shell) = &mut app_shell {
                            shell.mark_dirty();
                        }
                    }
                    MainEvent::Pause => {
                        log::info!("App paused");
                    }
                    MainEvent::Resume { .. } => {
                        log::info!("App resumed");
                    }
                    MainEvent::Start => {
                        log::info!("App started");
                    }
                    MainEvent::Stop => {
                        log::info!("App stopped");
                    }
                    MainEvent::SaveState { .. } => {
                        log::info!("Save state requested (hook for future serialization)");
                    }
                    MainEvent::Destroy => {
                        log::info!("App destroy requested, will exit after this event");
                        should_exit.store(true, Ordering::Relaxed);
                    }
                    _ => {}
                },
                // Handle input events to prevent ANR
                _ => {
                    if let Ok(mut iter) = app.input_events_iter() {
                        // Limit how many events we process per poll to prevent blocking
                        // Android ANR timeout is 5 seconds, so we need to return quickly
                        let mut events_processed = 0;
                        const MAX_EVENTS_PER_POLL: usize = 10;

                        loop {
                            if !iter.next(|event| {
                                let handled = match event {
                                    android_activity::input::InputEvent::MotionEvent(
                                        motion_event,
                                    ) => {
                                        // Get pointer position in physical pixels and convert to logical dp
                                        let pointer = motion_event.pointer_at_index(0);
                                        let x_px = pointer.x() as f64;
                                        let y_px = pointer.y() as f64;
                                        let logical = android_platform.pointer_position(x_px, y_px);

                                        match motion_event.action() {
                                            MotionAction::Down | MotionAction::PointerDown => {
                                                println!(
                                                    "[TOUCH] Down at ({:.1}, {:.1})",
                                                    logical.x, logical.y
                                                );
                                                pending_inputs.push(PendingInput::PointerDown(
                                                    logical.x as f32,
                                                    logical.y as f32,
                                                ));
                                            }
                                            MotionAction::Up | MotionAction::PointerUp => {
                                                println!(
                                                    "[TOUCH] Up at ({:.1}, {:.1})",
                                                    logical.x, logical.y
                                                );
                                                pending_inputs.push(PendingInput::PointerUp(
                                                    logical.x as f32,
                                                    logical.y as f32,
                                                ));
                                            }
                                            MotionAction::Move => {
                                                println!(
                                                    "[TOUCH] Move at ({:.1}, {:.1})",
                                                    logical.x, logical.y
                                                );
                                                pending_inputs.push(PendingInput::PointerMove(
                                                    logical.x as f32,
                                                    logical.y as f32,
                                                ));
                                            }
                                            _ => {}
                                        }
                                        true
                                    }
                                    _ => false,
                                };

                                if handled {
                                    InputStatus::Handled
                                } else {
                                    InputStatus::Unhandled
                                }
                            }) {
                                break;
                            }

                            events_processed += 1;
                            if events_processed >= MAX_EVENTS_PER_POLL {
                                // Processed enough events, return to main loop
                                // Remaining events will be processed in next poll
                                break;
                            }
                        }
                    }
                }
            }
        });

        // Process pending input events outside poll_events to prevent ANR
        if !pending_inputs.is_empty() {
            if let Some(shell) = &mut app_shell {
                for input in pending_inputs.drain(..) {
                    match input {
                        PendingInput::PointerDown(x, y) => {
                            shell.set_cursor(x, y);
                            shell.pointer_pressed();
                        }
                        PendingInput::PointerUp(x, y) => {
                            shell.set_cursor(x, y);
                            shell.pointer_released();
                        }
                        PendingInput::PointerMove(x, y) => {
                            shell.set_cursor(x, y);
                        }
                    }
                }
            }
        }

        // Check if app side requested a frame (animations, state changes)
        if need_frame.swap(false, Ordering::Relaxed) {
            if let Some(shell) = &mut app_shell {
                shell.mark_dirty();
            }
        }

        // Check if Destroy event requested exit
        if should_exit.load(Ordering::Relaxed) {
            log::info!("Exiting cleanly after Destroy event");
            break;
        }

        // Render outside event callback if needed
        if let (Some(resources), Some(shell)) = (&mut gpu_resources, &mut app_shell) {
            if shell.needs_redraw() {
                if render_once(resources, shell) {
                    break; // Out of memory, exit
                }
            }
        }
    }
}