euv-core 0.48.2

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

#[test]
fn android_ios_direct_gpu_batch_emission_covers_all_primitives_with_zero_cpu_mirror() {
    let mut dl: DisplayList = DisplayList::default();
    dl.set_viewport(Size::new(390.0, 844.0));
    dl.set_scale_factor(3.0);
    dl.push(DrawCommand::Clear(Color::rgb(18, 20, 28)));
    dl.push(DrawCommand::FillRoundedRect {
        rect: Rect::new(16.0, 24.0, 200.0, 80.0),
        color: Color::rgb(40, 80, 220),
        radius: CornerRadius::all(12.0),
        border: Border::new(1.5, Color::rgb(200, 210, 255)),
    });
    dl.push(DrawCommand::LinearGradientRect {
        rect: Rect::new(16.0, 120.0, 180.0, 48.0),
        gradient: LinearGradient::new(90.0, Color::rgb(255, 100, 50), Color::rgb(120, 40, 220)),
        radius: CornerRadius::all(8.0),
    });
    dl.push(DrawCommand::BoxShadow {
        rect: Rect::new(16.0, 24.0, 200.0, 80.0),
        radius: CornerRadius::all(12.0),
        shadow: Shadow::new(0.0, 6.0, 12.0, 0.0, Color::rgba(0, 0, 0, 96)),
    });
    dl.push(DrawCommand::BackdropBlur {
        rect: Rect::new(20.0, 200.0, 240.0, 96.0),
        radius: CornerRadius::all(16.0),
        blur_radius: 14.0,
    });
    dl.push(DrawCommand::TextRun {
        rect: Rect::new(24.0, 44.0, 160.0, 24.0),
        text: "DirectGPU".to_string(),
        style: TextStyle::default(),
    });
    let img: ImageBuffer = ImageBuffer::new(4, 4, vec![255_u8; 4 * 4 * 4]);
    dl.push(DrawCommand::Image {
        rect: Rect::new(24.0, 320.0, 64.0, 64.0),
        buffer: img,
        radius: CornerRadius::all(6.0),
    });
    dl.push(DrawCommand::Circle {
        center: Point::new(120.0, 420.0),
        radius: 18.0,
        color: Color::rgb(34, 197, 94),
    });
    dl.push(DrawCommand::Line {
        from: Point::new(16.0, 460.0),
        to: Point::new(220.0, 472.0),
        color: Color::rgb(245, 158, 11),
        width: 2.0,
    });
    dl.push(DrawCommand::Triangle {
        p0: Point::new(40.0, 500.0),
        p1: Point::new(80.0, 540.0),
        p2: Point::new(20.0, 540.0),
        color: Color::rgb(239, 68, 68),
    });
    let mut atlas: GlyphAtlas = GlyphAtlas::default();
    let batch: DirectGpuFrameBatch =
        compile_display_list_direct_gpu_batch(&dl, Some(&mut atlas), None, 0);
    assert!(!batch.get_cpu_mirror_active());
    assert_eq!(batch.get_upload_strategy(), MobileUploadStrategy::DirectGpu);
    assert_eq!(batch.get_viewport_width(), 1170);
    assert_eq!(batch.get_viewport_height(), 2532);
    assert!(!batch.get_primitives().is_empty());
    assert!(!batch.get_draw_ranges().is_empty());
    assert_eq!(batch.get_backdrop_blurs().len(), 1);
    let glyph_upload: &DirectGpuAtlasUpload = batch
        .glyph_r8_atlas_ref()
        .expect("expected R8 glyph atlas upload");
    assert!(glyph_upload.get_width() > 0 && glyph_upload.get_height() > 0);
    let img_upload: &DirectGpuAtlasUpload = batch
        .image_rgba8_atlas_ref()
        .expect("expected RGBA8 image atlas upload");
    assert!(img_upload.get_width() > 0 && img_upload.get_height() > 0);
    let has_glyph: bool = batch
        .get_primitives()
        .iter()
        .any(|p: &GpuPrimitiveInstance| p.prim_kind() == PrimKind::GlyphAlpha8);
    let has_image: bool = batch
        .get_primitives()
        .iter()
        .any(|p: &GpuPrimitiveInstance| p.prim_kind() == PrimKind::ImageTexture);
    let has_shadow: bool = batch
        .get_primitives()
        .iter()
        .any(|p: &GpuPrimitiveInstance| p.prim_kind() == PrimKind::Shadow);
    let has_vector: bool = batch
        .get_primitives()
        .iter()
        .any(|p: &GpuPrimitiveInstance| p.prim_kind() == PrimKind::VectorShape);
    assert!(has_glyph && has_image && has_shadow && has_vector);
    let attribs: [Gles3InstanceAttribSpec; 5] = gles3_ui_instance_attribs();
    assert_eq!(attribs[0].get_byte_offset(), 0);
    assert_eq!(attribs[1].get_byte_offset(), 16);
    assert_eq!(attribs[2].get_byte_offset(), 32);
    assert_eq!(attribs[3].get_byte_offset(), 40);
    assert_eq!(attribs[4].get_byte_offset(), 56);
    assert_eq!(attribs[4].get_stride(), GPU_PRIMITIVE_INSTANCE_BYTES as i32);
    let gl_sc: [i32; 4] = gles3_scissor_from_top_down(2532, 30, 100, 200, 400);
    assert_eq!(gl_sc, [30, 2032, 200, 400]);
}

#[test]
fn direct_gpu_batch_from_hardware_renderer_batches_has_zero_cpu_mirror() {
    let mut batch: GpuBatch = GpuBatch::default();
    batch.set_scissor(Rect::new(0.0, 0.0, 1080.0, 2400.0));
    batch.set_scroll_offset([0.0, 128.0]);
    let quad: GpuQuadInstance = GpuQuadInstance {
        rect: [16.0, 32.0, 320.0, 96.0],
        clip: [0.0, 0.0, 1080.0, 2400.0],
        color: [0.2, 0.4, 0.9, 1.0],
        secondary_color: [0.0; 4],
        radii: [12.0; 4],
        stroke_or_blur: 0.0,
        angle_rad: 0.0,
        kind: GpuPrimitiveKind::RoundedRect as u32,
        reserved: 0,
    };
    batch.get_mut_instances().push(quad);
    let frame: DirectGpuFrameBatch =
        build_direct_gpu_frame_batch_from_batches(1080, 2400, COLOR_WHITE, &[batch], &[]);
    assert!(!frame.get_cpu_mirror_active());
    assert_eq!(frame.get_upload_strategy(), MobileUploadStrategy::DirectGpu);
    assert_eq!(frame.get_primitives().len(), 1);
    assert_eq!(frame.get_draw_ranges().len(), 1);
    assert_eq!(
        frame.get_instance_upload_bytes(),
        GPU_PRIMITIVE_INSTANCE_BYTES
    );
    assert!((frame.get_draw_ranges()[0].get_scroll_y() - 128.0).abs() < 0.01);
}

#[test]
fn adaptive_upload_strategy_selects_multi_rect_merged_bbox_full_frame_and_none() {
    let distant: [PixelDirtyRect; 2] = [
        PixelDirtyRect::new(12, 16, 96, 36),
        PixelDirtyRect::new(960, 2260, 88, 88),
    ];
    let multi_plan: AdaptiveUploadPlan = select_adaptive_upload_plan(1080, 2400, &distant, false);
    assert_eq!(multi_plan.get_strategy(), MobileUploadStrategy::MultiRect);
    assert_eq!(multi_plan.get_rects().len(), 2);
    assert!(multi_plan.get_total_upload_bytes() < multi_plan.get_merged_bbox_bytes());
    let nearby: [PixelDirtyRect; 2] = [
        PixelDirtyRect::new(100, 200, 120, 40),
        PixelDirtyRect::new(100, 248, 120, 40),
    ];
    let merged_plan: AdaptiveUploadPlan = select_adaptive_upload_plan(1080, 2400, &nearby, false);
    assert_eq!(merged_plan.get_strategy(), MobileUploadStrategy::MergedBbox);
    assert_eq!(merged_plan.get_rects().len(), 1);
    assert!(merged_plan.get_total_upload_bytes() < merged_plan.get_full_frame_bytes());
    let huge: [PixelDirtyRect; 1] = [PixelDirtyRect::new(0, 0, 1080, 2100)];
    let full_plan: AdaptiveUploadPlan = select_adaptive_upload_plan(1080, 2400, &huge, false);
    assert_eq!(full_plan.get_strategy(), MobileUploadStrategy::FullFrame);
    assert_eq!(
        full_plan.get_total_upload_bytes(),
        full_plan.get_full_frame_bytes()
    );
    let forced: AdaptiveUploadPlan = select_adaptive_upload_plan(1080, 2400, &distant, true);
    assert_eq!(forced.get_strategy(), MobileUploadStrategy::FullFrame);
    let empty_plan: AdaptiveUploadPlan = select_adaptive_upload_plan(1080, 2400, &[], false);
    assert_eq!(empty_plan.get_strategy(), MobileUploadStrategy::None);
    assert_eq!(empty_plan.get_total_upload_bytes(), 0);
}

#[test]
fn cpu_mirror_flag_tracks_gpu_direct_vs_gpu_upload_and_software_and_context_loss_failover() {
    let mut ctrl: MobileFailoverController =
        MobileFailoverController::for_preferred(MobilePresentPath::GpuDirect, true, true);
    assert_eq!(ctrl.get_active_path(), MobilePresentPath::GpuDirect);
    assert!(!ctrl.cpu_mirror_active());
    ctrl.mark_frame_presented();
    let shader_action: MobileFailoverAction =
        ctrl.handle_fault(MobileSurfaceFault::ShaderCompileFailed);
    assert_eq!(
        shader_action,
        MobileFailoverAction::KeepLastFrameAndSwitch(MobilePresentPath::GpuUpload)
    );
    assert_eq!(ctrl.get_active_path(), MobilePresentPath::GpuUpload);
    assert!(ctrl.cpu_mirror_active());
    assert_eq!(ctrl.get_blank_frames_prevented(), 1);
    let loss_action: MobileFailoverAction = ctrl.handle_fault(MobileSurfaceFault::ContextLost);
    assert_eq!(
        loss_action,
        MobileFailoverAction::KeepLastFrameAndSwitch(MobilePresentPath::Software)
    );
    assert_eq!(ctrl.get_active_path(), MobilePresentPath::Software);
    assert!(ctrl.cpu_mirror_active());
    assert_eq!(ctrl.get_context_loss_count(), 1);
    assert_eq!(ctrl.get_blank_frames_prevented(), 2);
    assert_eq!(ctrl.get_fallback_transitions(), 2);
    ctrl.set_surface_valid(false);
    let detached_action: MobileFailoverAction = ctrl.handle_fault(MobileSurfaceFault::SurfaceLost);
    assert_eq!(detached_action, MobileFailoverAction::WaitForNewSurface);
}

#[test]
fn mobile_present_telemetry_marks_simulator_unobservable_honestly_and_tracks_144hz_vsync() {
    let mut sim_tel: MobilePresentTelemetry =
        MobilePresentTelemetry::for_host(MobilePresentPath::GpuDirect, 120.0, true);
    assert!(!sim_tel.get_cpu_mirror_active());
    assert_eq!(
        sim_tel.get_upload_strategy(),
        MobileUploadStrategy::DirectGpu
    );
    sim_tel.record_presented_frame(
        MobilePresentPath::GpuDirect,
        MobileUploadStrategy::DirectGpu,
        4096,
        64,
        2,
        Some(123_456_789),
    );
    assert_eq!(
        sim_tel.get_timing_source(),
        MobilePresentTimingSource::SubmissionProxy
    );
    assert_eq!(sim_tel.try_get_last_presented_timestamp_ns(), None);
    assert_eq!(
        sim_tel.try_get_unobservable_reason(),
        Some(MOBILE_TIMING_UNOBSERVABLE_REASON_SIMULATOR)
    );
    let mut dev_tel: MobilePresentTelemetry =
        MobilePresentTelemetry::for_host(MobilePresentPath::GpuDirect, 144.0, false);
    dev_tel.record_vsync_callback(1_000_000_000, Some(1_006_944_444));
    dev_tel.record_vsync_callback(1_006_944_444, Some(1_013_888_888));
    let interval_ms: f64 = dev_tel
        .try_get_last_callback_interval_ms()
        .expect("expected measured 144Hz callback interval");
    assert!((interval_ms - 6.944_444).abs() < 0.01);
    dev_tel.record_presented_frame(
        MobilePresentPath::GpuDirect,
        MobileUploadStrategy::DirectGpu,
        2048,
        32,
        1,
        Some(1_007_100_000),
    );
    assert!(!dev_tel.get_cpu_mirror_active());
    assert_eq!(
        dev_tel.get_timing_source(),
        MobilePresentTimingSource::CompositorMeasured
    );
    assert_eq!(
        dev_tel.try_get_last_presented_timestamp_ns(),
        Some(1_007_100_000)
    );
    assert_eq!(dev_tel.try_get_unobservable_reason(), None);
    dev_tel.record_presented_frame(
        MobilePresentPath::GpuUpload,
        MobileUploadStrategy::MultiRect,
        65_536,
        0,
        1,
        None,
    );
    assert!(dev_tel.get_cpu_mirror_active());
    assert_eq!(
        dev_tel.get_upload_strategy(),
        MobileUploadStrategy::MultiRect
    );
    assert_eq!(
        dev_tel.get_timing_source(),
        MobilePresentTimingSource::Unobservable
    );
    assert_eq!(dev_tel.try_get_last_presented_timestamp_ns(), None);
}