/* * WiVRn VR streaming * Copyright (C) 2026 WiVRn Contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #include // Perfetto tracing via the Perfetto SDK. No-op unless built with WIVRN_USE_PERFETTO. // Monado's vk_bundle (C struct from util/vk/vk_helpers.h); used as the // calibration source for converting GPU timestamp-query ticks to host // monotonic ns via vk_convert_timestamps_to_host_ns. struct vk_bundle; namespace wivrn::trace { // The single tracing entry point. Idempotent. Always runs Monado's u_trace_marker_init() // (and, with WIVRN_TRACE_MONADO, bridges WIVRN_TRACING onto Monado's tracing first); Perfetto // tracing is a no-op without WIVRN_USE_PERFETTO or WIVRN_TRACING. WIVRN_TRACING selects the // backend (system / inprocess / system+inprocess) — see wivrn_trace.cpp for the full env-var docs. void init(); // Writes the current in-process trace to its own file and re-arms a fresh session, without // ending the process. Call when a streaming client disconnects (the session pauses, awaiting // reconnect), so each connection leaves its own trace. No-op unless the in-process backend is // active. void flush_session(); // Stops and flushes the in-process session (writing the final trace file) if one is running; // no-op otherwise. Must be called once at process/session teardown. void shutdown(); // True iff WIVRN_TRACING is set in the environment (and WIVRN_USE_PERFETTO // is enabled). Cached after the first read. bool is_enabled(); // Set by the compositor after vk_init_from_given so query-pool ticks can be // converted via monado's calibration cache. Single-threaded init window; // reads happen on encoder threads after streaming starts. void set_calibration_source(::vk_bundle * monado_vk); ::vk_bundle * calibration_source(); // Instant on the feedback track at an explicit monotonic timestamp. // name is copied at emit time (perfetto::DynamicString), so it need not outlive the call. void instant_feedback(const char * name, int64_t time_ns, uint64_t frame, uint8_t stream = -1); // GPU-hardware-timestamped slice on the WiVRn encoder track. // begin_ns/end_ns must come from GPU query pools (gpu_timestamp_pool::collect), // already converted to host monotonic via calibrated timestamps. enum class gpu_track { vulkan_encode, vulkan_image_copy, vulkan_host_copy, vulkan_host_copy_overflow, nvenc_copy, va_copy, }; void gpu_slice(gpu_track which, const char * slice_name, int64_t begin_ns, int64_t end_ns, uint64_t frame, uint8_t stream); enum class cpu_track { encoder, compositor, network, }; // RAII CPU span. Zero-cost when tracing is off. Construct as a local with a // chosen variable name; the span closes at scope exit. class scope { cpu_track which; uint8_t stream; uint64_t frame; const char * name; int64_t begin_ns; bool active; public: scope(cpu_track which, uint8_t stream, uint64_t frame, const char * name); ~scope(); scope(const scope &) = delete; scope & operator=(const scope &) = delete; }; void cpu_instant(cpu_track which, const char * name, uint64_t frame, uint8_t stream); // Non-RAII begin/end pair, for spans whose lifetime crosses function calls // (e.g. video_encoder::SendData where a frame is split across several calls). // Only cpu_begin reads name (copied via perfetto::DynamicString); cpu_end ignores it. void cpu_begin(cpu_track which, uint8_t stream, uint64_t frame, const char * name); void cpu_end(cpu_track which, uint8_t stream, uint64_t frame, const char * name); } // namespace wivrn::trace