Files
WiVRn/server/compositor/compositor.h

178 lines
4.2 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2026 Patrick Nicolas <[email protected]>
*
* 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 <https://www.gnu.org/licenses/>.
*/
// Copyright 2019-2024, Collabora, Ltd.
// Copyright 2025-2026, NVIDIA CORPORATION.
#pragma once
#include "util/comp_base.h"
#include "util/u_logging.h"
#include "encoder/encoder_settings.h"
#include "foveation.h"
#include "layer_squasher.h"
#include "pacer.h"
#include "utils/wivrn_vk_bundle.h"
#include "main/comp_compositor.h"
#include <array>
#include <atomic>
#include <memory>
#include <thread>
namespace wivrn
{
class wivrn_session;
class video_encoder;
class compositor : public comp_base
{
public:
struct image
{
std::atomic<bool> busy = false;
vk::raii::Semaphore sem;
vk::raii::Fence fence;
vk::raii::CommandBuffer cmd;
image_allocation image;
vk::raii::ImageView view_y;
vk::raii::ImageView view_cbcr;
to_headset::video_stream_data_shard::view_info_t view_info{};
uint64_t frame_index;
};
private:
const u_logging_level log_level;
wivrn_session & session;
vk_bundle vk;
vk::raii::CommandPool cmd_pool;
std::array<encoder_settings, 3> settings;
std::array<image, 2> images;
vk::raii::CommandBuffer cmd;
vk::raii::Fence fence;
std::atomic<float> requested_refresh_rate;
std::atomic<float> refresh_rate;
wivrn::pacer pacer;
layer_squasher squasher;
wivrn::foveation foveation;
std::array<std::unique_ptr<video_encoder>, 3> encoders;
const bool implicit_transfer;
#ifdef __cpp_lib_atomic_lock_free_type_aliases
using status_type = std::atomic_signed_lock_free;
#else
using status_type = std::atomic_int8_t;
#endif
status_type encode_request{-1}; // id of the image to encode
std::jthread encoder_thread;
struct
{
comp_frame waited{.id = -1};
comp_frame rendering{.id = -1};
} frame;
xrt_result_t begin_session(const xrt_begin_session_info * info)
{
return XRT_SUCCESS;
};
xrt_result_t end_session()
{
return XRT_SUCCESS;
};
xrt_result_t predict_frame(int64_t * out_frame_id,
int64_t * out_wake_time_ns,
int64_t * out_predicted_gpu_time_ns,
int64_t * out_predicted_display_time_ns,
int64_t * out_predicted_display_period_ns);
xrt_result_t mark_frame(int64_t frame_id,
xrt_compositor_frame_point point,
int64_t when_ns);
xrt_result_t begin_frame(int64_t frame_id)
{
return XRT_SUCCESS;
}
xrt_result_t discard_frame(int64_t frame_id)
{
return XRT_SUCCESS;
};
xrt_result_t layer_commit(xrt_graphics_sync_handle_t sync_handle);
xrt_result_t get_display_refresh_rate(float * out_display_refresh_rate_hz);
public:
xrt_result_t request_display_refresh_rate(float display_refresh_rate_hz);
private:
void destroy()
{
// do nothing, actually owned by wivrn_session object
}
int acquire_image();
void encoder_work(std::stop_token);
void send_video_stream_description();
public:
using base_t = xrt_compositor;
compositor(wivrn_session &);
~compositor();
xrt_system_compositor_info sys_info() const;
int64_t get_frame_duration() const
{
return pacer.get_frame_duration();
}
float get_requested_refresh_rate() const
{
return requested_refresh_rate;
}
float get_refresh_rate() const
{
return refresh_rate;
}
void set_refresh_rate(float hz);
void set_bitrate(uint32_t);
void update_tracking(const from_headset::tracking &);
void update_foveation_center_override(const from_headset::override_foveation_center &);
void resume();
void on_feedback(const from_headset::feedback &, const clock_offset &);
};
} // namespace wivrn