2021-11-12 04:15:51 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Copyright 2021 yuzu Emulator Project
|
2018-01-22 17:54:58 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
2018-10-30 05:03:25 +01:00
|
|
|
#include <optional>
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2018-08-07 14:24:30 +02:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
2018-04-19 02:28:50 +02:00
|
|
|
#include "common/microprofile.h"
|
2018-01-22 17:54:58 +01:00
|
|
|
#include "common/scope_exit.h"
|
2021-04-15 01:07:40 +02:00
|
|
|
#include "common/settings.h"
|
2020-05-29 21:00:17 +02:00
|
|
|
#include "common/thread.h"
|
2018-02-14 18:16:39 +01:00
|
|
|
#include "core/core.h"
|
2018-01-22 17:54:58 +01:00
|
|
|
#include "core/core_timing.h"
|
2021-01-30 07:48:06 +01:00
|
|
|
#include "core/hle/kernel/k_readable_event.h"
|
2018-01-22 17:54:58 +01:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvdisp_disp0.h"
|
|
|
|
#include "core/hle/service/nvdrv/nvdrv.h"
|
2021-11-12 04:15:51 +01:00
|
|
|
#include "core/hle/service/nvflinger/buffer_item_consumer.h"
|
|
|
|
#include "core/hle/service/nvflinger/buffer_queue_core.h"
|
|
|
|
#include "core/hle/service/nvflinger/hos_binder_driver_server.h"
|
2018-01-22 17:54:58 +01:00
|
|
|
#include "core/hle/service/nvflinger/nvflinger.h"
|
2021-11-12 04:15:51 +01:00
|
|
|
#include "core/hle/service/nvflinger/ui/graphic_buffer.h"
|
2019-02-19 23:00:03 +01:00
|
|
|
#include "core/hle/service/vi/display/vi_display.h"
|
|
|
|
#include "core/hle/service/vi/layer/vi_layer.h"
|
2021-10-02 06:39:57 +02:00
|
|
|
#include "video_core/gpu.h"
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2018-04-20 03:41:44 +02:00
|
|
|
namespace Service::NVFlinger {
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2020-07-16 00:30:06 +02:00
|
|
|
constexpr auto frame_ns = std::chrono::nanoseconds{1000000000 / 60};
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2021-10-02 06:39:57 +02:00
|
|
|
void NVFlinger::SplitVSync(std::stop_token stop_token) {
|
2020-05-29 21:00:17 +02:00
|
|
|
system.RegisterHostThread();
|
|
|
|
std::string name = "yuzu:VSyncThread";
|
|
|
|
MicroProfileOnThreadCreate(name.c_str());
|
2020-12-30 10:34:50 +01:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
SCOPE_EXIT({ MicroProfileOnThreadExit(); });
|
|
|
|
|
2020-05-29 21:00:17 +02:00
|
|
|
Common::SetCurrentThreadName(name.c_str());
|
|
|
|
Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
|
|
|
|
s64 delay = 0;
|
2021-10-02 06:39:57 +02:00
|
|
|
while (!stop_token.stop_requested()) {
|
2020-05-29 21:00:17 +02:00
|
|
|
guard->lock();
|
|
|
|
const s64 time_start = system.CoreTiming().GetGlobalTimeNs().count();
|
|
|
|
Compose();
|
|
|
|
const auto ticks = GetNextTicks();
|
|
|
|
const s64 time_end = system.CoreTiming().GetGlobalTimeNs().count();
|
|
|
|
const s64 time_passed = time_end - time_start;
|
|
|
|
const s64 next_time = std::max<s64>(0, ticks - time_passed - delay);
|
|
|
|
guard->unlock();
|
|
|
|
if (next_time > 0) {
|
2021-10-02 06:39:57 +02:00
|
|
|
std::this_thread::sleep_for(std::chrono::nanoseconds{next_time});
|
2020-05-29 21:00:17 +02:00
|
|
|
}
|
|
|
|
delay = (system.CoreTiming().GetGlobalTimeNs().count() - time_end) - next_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 04:15:51 +01:00
|
|
|
NVFlinger::NVFlinger(Core::System& system_, HosBinderDriverServer& hos_binder_driver_server_)
|
|
|
|
: system(system_), service_context(system_, "nvflinger"),
|
|
|
|
hos_binder_driver_server(hos_binder_driver_server_) {
|
|
|
|
displays.emplace_back(0, "Default", hos_binder_driver_server, service_context, system);
|
|
|
|
displays.emplace_back(1, "External", hos_binder_driver_server, service_context, system);
|
|
|
|
displays.emplace_back(2, "Edid", hos_binder_driver_server, service_context, system);
|
|
|
|
displays.emplace_back(3, "Internal", hos_binder_driver_server, service_context, system);
|
|
|
|
displays.emplace_back(4, "Null", hos_binder_driver_server, service_context, system);
|
2020-02-27 15:47:02 +01:00
|
|
|
guard = std::make_shared<std::mutex>();
|
2019-02-21 16:43:26 +01:00
|
|
|
|
2018-01-22 17:54:58 +01:00
|
|
|
// Schedule the screen composition events
|
2020-07-16 01:14:21 +02:00
|
|
|
composition_event = Core::Timing::CreateEvent(
|
2020-07-28 01:00:41 +02:00
|
|
|
"ScreenComposition", [this](std::uintptr_t, std::chrono::nanoseconds ns_late) {
|
2021-04-26 15:11:33 +02:00
|
|
|
const auto lock_guard = Lock();
|
2019-09-22 08:41:34 +02:00
|
|
|
Compose();
|
2020-07-16 00:30:06 +02:00
|
|
|
|
|
|
|
const auto ticks = std::chrono::nanoseconds{GetNextTicks()};
|
2020-07-16 01:14:21 +02:00
|
|
|
const auto ticks_delta = ticks - ns_late;
|
2020-07-16 00:30:06 +02:00
|
|
|
const auto future_ns = std::max(std::chrono::nanoseconds::zero(), ticks_delta);
|
|
|
|
|
|
|
|
this->system.CoreTiming().ScheduleEvent(future_ns, composition_event);
|
2019-09-22 08:41:34 +02:00
|
|
|
});
|
2020-07-16 00:30:06 +02:00
|
|
|
|
2020-05-29 21:00:17 +02:00
|
|
|
if (system.IsMulticore()) {
|
2021-10-02 06:39:57 +02:00
|
|
|
vsync_thread = std::jthread([this](std::stop_token token) { SplitVSync(token); });
|
2020-05-29 21:00:17 +02:00
|
|
|
} else {
|
2020-07-16 00:30:06 +02:00
|
|
|
system.CoreTiming().ScheduleEvent(frame_ns, composition_event);
|
2020-05-29 21:00:17 +02:00
|
|
|
}
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NVFlinger::~NVFlinger() {
|
2021-10-02 06:39:57 +02:00
|
|
|
if (!system.IsMulticore()) {
|
2020-05-29 21:00:17 +02:00
|
|
|
system.CoreTiming().UnscheduleEvent(composition_event, 0);
|
|
|
|
}
|
2021-11-12 04:15:51 +01:00
|
|
|
|
|
|
|
for (auto& display : displays) {
|
|
|
|
for (size_t layer = 0; layer < display.GetNumLayers(); ++layer) {
|
|
|
|
display.GetLayer(layer).Core().NotifyShutdown();
|
|
|
|
}
|
|
|
|
}
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 15:17:09 +02:00
|
|
|
void NVFlinger::SetNVDrvInstance(std::shared_ptr<Nvidia::Module> instance) {
|
|
|
|
nvdrv = std::move(instance);
|
|
|
|
}
|
|
|
|
|
2019-02-05 22:20:04 +01:00
|
|
|
std::optional<u64> NVFlinger::OpenDisplay(std::string_view name) {
|
2021-04-26 15:11:33 +02:00
|
|
|
const auto lock_guard = Lock();
|
2020-12-11 00:32:52 +01:00
|
|
|
|
2019-01-29 20:53:50 +01:00
|
|
|
LOG_DEBUG(Service, "Opening \"{}\" display", name);
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2019-02-21 16:43:26 +01:00
|
|
|
const auto itr =
|
|
|
|
std::find_if(displays.begin(), displays.end(),
|
|
|
|
[&](const VI::Display& display) { return display.GetName() == name; });
|
2019-04-06 22:02:55 +02:00
|
|
|
|
2019-02-05 22:20:04 +01:00
|
|
|
if (itr == displays.end()) {
|
2020-09-22 23:31:53 +02:00
|
|
|
return std::nullopt;
|
2019-02-05 22:20:04 +01:00
|
|
|
}
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2019-02-21 16:43:26 +01:00
|
|
|
return itr->GetID();
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 22:20:04 +01:00
|
|
|
std::optional<u64> NVFlinger::CreateLayer(u64 display_id) {
|
2021-04-26 15:11:33 +02:00
|
|
|
const auto lock_guard = Lock();
|
2019-02-05 22:20:04 +01:00
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
2020-09-22 23:31:53 +02:00
|
|
|
return std::nullopt;
|
2019-02-05 22:20:04 +01:00
|
|
|
}
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2019-01-29 20:52:14 +01:00
|
|
|
const u64 layer_id = next_layer_id++;
|
2021-05-06 17:20:52 +02:00
|
|
|
CreateLayerAtId(*display, layer_id);
|
|
|
|
return layer_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NVFlinger::CreateLayerAtId(VI::Display& display, u64 layer_id) {
|
2021-11-12 04:15:51 +01:00
|
|
|
const auto buffer_id = next_buffer_queue_id++;
|
|
|
|
display.CreateLayer(layer_id, buffer_id);
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
|
2020-01-04 06:45:06 +01:00
|
|
|
void NVFlinger::CloseLayer(u64 layer_id) {
|
2021-04-26 15:11:33 +02:00
|
|
|
const auto lock_guard = Lock();
|
2020-12-11 00:32:52 +01:00
|
|
|
|
2020-01-04 06:45:06 +01:00
|
|
|
for (auto& display : displays) {
|
|
|
|
display.CloseLayer(layer_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:20:52 +02:00
|
|
|
std::optional<u32> NVFlinger::FindBufferQueueId(u64 display_id, u64 layer_id) {
|
2021-04-26 15:11:33 +02:00
|
|
|
const auto lock_guard = Lock();
|
2021-05-06 17:20:52 +02:00
|
|
|
const auto* const layer = FindOrCreateLayer(display_id, layer_id);
|
2019-02-05 22:20:04 +01:00
|
|
|
|
|
|
|
if (layer == nullptr) {
|
2020-09-22 23:31:53 +02:00
|
|
|
return std::nullopt;
|
2019-02-05 22:20:04 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 04:15:51 +01:00
|
|
|
return layer->GetBinderId();
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
|
2021-04-10 11:34:26 +02:00
|
|
|
Kernel::KReadableEvent* NVFlinger::FindVsyncEvent(u64 display_id) {
|
|
|
|
const auto lock_guard = Lock();
|
2019-02-05 22:20:04 +01:00
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-10 11:34:26 +02:00
|
|
|
return &display->GetVSyncEvent();
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 23:00:03 +01:00
|
|
|
VI::Display* NVFlinger::FindDisplay(u64 display_id) {
|
|
|
|
const auto itr =
|
|
|
|
std::find_if(displays.begin(), displays.end(),
|
2019-02-21 16:43:26 +01:00
|
|
|
[&](const VI::Display& display) { return display.GetID() == display_id; });
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2019-02-05 22:20:04 +01:00
|
|
|
if (itr == displays.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &*itr;
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 23:00:03 +01:00
|
|
|
const VI::Display* NVFlinger::FindDisplay(u64 display_id) const {
|
|
|
|
const auto itr =
|
|
|
|
std::find_if(displays.begin(), displays.end(),
|
2019-02-21 16:43:26 +01:00
|
|
|
[&](const VI::Display& display) { return display.GetID() == display_id; });
|
2019-01-30 17:14:05 +01:00
|
|
|
|
2019-02-05 22:20:04 +01:00
|
|
|
if (itr == displays.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &*itr;
|
2019-01-30 17:14:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 23:00:03 +01:00
|
|
|
VI::Layer* NVFlinger::FindLayer(u64 display_id, u64 layer_id) {
|
2019-02-05 22:20:04 +01:00
|
|
|
auto* const display = FindDisplay(display_id);
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2019-02-05 22:20:04 +01:00
|
|
|
if (display == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:43:26 +01:00
|
|
|
return display->FindLayer(layer_id);
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 23:00:03 +01:00
|
|
|
const VI::Layer* NVFlinger::FindLayer(u64 display_id, u64 layer_id) const {
|
2019-02-05 22:20:04 +01:00
|
|
|
const auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-01-30 17:14:05 +01:00
|
|
|
|
2019-02-21 16:43:26 +01:00
|
|
|
return display->FindLayer(layer_id);
|
2019-01-30 17:14:05 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 17:20:52 +02:00
|
|
|
VI::Layer* NVFlinger::FindOrCreateLayer(u64 display_id, u64 layer_id) {
|
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* layer = display->FindLayer(layer_id);
|
|
|
|
|
|
|
|
if (layer == nullptr) {
|
|
|
|
LOG_DEBUG(Service, "Layer at id {} not found. Trying to create it.", layer_id);
|
|
|
|
CreateLayerAtId(*display, layer_id);
|
|
|
|
return display->FindLayer(layer_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:58 +01:00
|
|
|
void NVFlinger::Compose() {
|
|
|
|
for (auto& display : displays) {
|
|
|
|
// Trigger vsync for this display at the end of drawing
|
2019-02-21 16:43:26 +01:00
|
|
|
SCOPE_EXIT({ display.SignalVSyncEvent(); });
|
2018-01-22 17:54:58 +01:00
|
|
|
|
|
|
|
// Don't do anything for displays without layers.
|
2019-02-21 16:43:26 +01:00
|
|
|
if (!display.HasLayers())
|
2018-01-22 17:54:58 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO(Subv): Support more than 1 layer.
|
2019-02-21 16:43:26 +01:00
|
|
|
VI::Layer& layer = display.GetLayer(0);
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2021-11-12 04:15:51 +01:00
|
|
|
android::BufferItem buffer{};
|
|
|
|
const auto status = layer.GetConsumer().AcquireBuffer(&buffer, 0, false);
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2021-11-12 04:15:51 +01:00
|
|
|
if (status != android::Status::NoError) {
|
2018-01-22 17:54:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-12 04:15:51 +01:00
|
|
|
const auto& igbp_buffer = *buffer.graphic_buffer;
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2020-10-29 01:17:38 +01:00
|
|
|
if (!system.IsPoweredOn()) {
|
|
|
|
return; // We are likely shutting down
|
|
|
|
}
|
|
|
|
|
2019-12-30 13:03:20 +01:00
|
|
|
auto& gpu = system.GPU();
|
2021-11-12 04:15:51 +01:00
|
|
|
const auto& multi_fence = buffer.fence;
|
2020-05-29 21:00:17 +02:00
|
|
|
guard->unlock();
|
2019-09-26 01:43:23 +02:00
|
|
|
for (u32 fence_id = 0; fence_id < multi_fence.num_fences; fence_id++) {
|
|
|
|
const auto& fence = multi_fence.fences[fence_id];
|
2020-10-21 04:07:39 +02:00
|
|
|
gpu.WaitFence(fence.id, fence.value);
|
2019-09-26 01:43:23 +02:00
|
|
|
}
|
2020-05-29 21:00:17 +02:00
|
|
|
guard->lock();
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2020-03-12 15:52:44 +01:00
|
|
|
MicroProfileFlip();
|
|
|
|
|
2018-01-22 17:54:58 +01:00
|
|
|
// Now send the buffer to the GPU for drawing.
|
|
|
|
// TODO(Subv): Support more than just disp0. The display device selection is probably based
|
|
|
|
// on which display we're drawing (Default, Internal, External, etc)
|
|
|
|
auto nvdisp = nvdrv->GetDevice<Nvidia::Devices::nvdisp_disp0>("/dev/nvdisp_disp0");
|
|
|
|
ASSERT(nvdisp);
|
|
|
|
|
2021-11-12 04:15:51 +01:00
|
|
|
Common::Rectangle<int> crop_rect{
|
|
|
|
static_cast<int>(buffer.crop.Left()), static_cast<int>(buffer.crop.Top()),
|
|
|
|
static_cast<int>(buffer.crop.Right()), static_cast<int>(buffer.crop.Bottom())};
|
|
|
|
|
|
|
|
nvdisp->flip(igbp_buffer.BufferId(), igbp_buffer.Offset(), igbp_buffer.ExternalFormat(),
|
|
|
|
igbp_buffer.Width(), igbp_buffer.Height(), igbp_buffer.Stride(),
|
|
|
|
static_cast<android::BufferTransformFlags>(buffer.transform), crop_rect);
|
|
|
|
|
|
|
|
swap_interval = buffer.swap_interval;
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2021-11-12 04:15:51 +01:00
|
|
|
auto fence = android::Fence::NoFence();
|
|
|
|
layer.GetConsumer().ReleaseBuffer(buffer, fence);
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 02:53:21 +02:00
|
|
|
s64 NVFlinger::GetNextTicks() const {
|
2021-07-23 03:03:50 +02:00
|
|
|
static constexpr s64 max_hertz = 120LL;
|
|
|
|
|
|
|
|
const auto& settings = Settings::values;
|
|
|
|
const bool unlocked_fps = settings.disable_fps_limit.GetValue();
|
|
|
|
const s64 fps_cap = unlocked_fps ? static_cast<s64>(settings.fps_cap.GetValue()) : 1;
|
|
|
|
return (1000000000 * (1LL << swap_interval)) / (max_hertz * fps_cap);
|
2019-06-04 22:10:07 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 03:41:44 +02:00
|
|
|
} // namespace Service::NVFlinger
|