yuzu/src/core/hle/service/nvnflinger/nvnflinger.cpp

335 lines
10 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include <algorithm>
#include <optional>
#include "common/assert.h"
#include "common/logging/log.h"
#include "common/microprofile.h"
#include "common/scope_exit.h"
#include "common/settings.h"
#include "common/thread.h"
#include "core/core.h"
#include "core/core_timing.h"
#include "core/hle/kernel/k_readable_event.h"
#include "core/hle/service/nvdrv/devices/nvdisp_disp0.h"
#include "core/hle/service/nvdrv/nvdrv.h"
2023-02-19 20:05:34 +00:00
#include "core/hle/service/nvnflinger/buffer_item_consumer.h"
#include "core/hle/service/nvnflinger/buffer_queue_core.h"
#include "core/hle/service/nvnflinger/hos_binder_driver_server.h"
#include "core/hle/service/nvnflinger/nvnflinger.h"
#include "core/hle/service/nvnflinger/ui/graphic_buffer.h"
#include "core/hle/service/vi/display/vi_display.h"
#include "core/hle/service/vi/layer/vi_layer.h"
#include "core/hle/service/vi/vi_results.h"
#include "video_core/gpu.h"
2022-01-30 09:31:13 +00:00
#include "video_core/host1x/host1x.h"
#include "video_core/host1x/syncpoint_manager.h"
2023-02-19 20:05:34 +00:00
namespace Service::Nvnflinger {
constexpr auto frame_ns = std::chrono::nanoseconds{1000000000 / 60};
2023-02-19 20:05:34 +00:00
void Nvnflinger::SplitVSync(std::stop_token stop_token) {
system.RegisterHostThread();
std::string name = "VSyncThread";
MicroProfileOnThreadCreate(name.c_str());
// Cleanup
SCOPE_EXIT({ MicroProfileOnThreadExit(); });
Common::SetCurrentThreadName(name.c_str());
Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
2022-07-26 21:04:18 +00:00
while (!stop_token.stop_requested()) {
2023-06-20 15:41:38 +00:00
vsync_signal.Wait();
2022-07-26 21:04:18 +00:00
2023-06-20 15:41:38 +00:00
const auto lock_guard = Lock();
Compose();
}
}
2023-02-19 20:05:34 +00:00
Nvnflinger::Nvnflinger(Core::System& system_, HosBinderDriverServer& hos_binder_driver_server_)
: system(system_), service_context(system_, "nvnflinger"),
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);
guard = std::make_shared<std::mutex>();
// Schedule the screen composition events
2022-07-26 21:04:18 +00:00
multi_composition_event = Core::Timing::CreateEvent(
"ScreenComposition",
[this](std::uintptr_t, s64 time,
std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
{ const auto lock_guard = Lock(); }
2023-06-20 15:41:38 +00:00
vsync_signal.Set();
2022-07-26 21:04:18 +00:00
return std::chrono::nanoseconds(GetNextTicks());
});
single_composition_event = Core::Timing::CreateEvent(
2022-07-10 05:59:40 +00:00
"ScreenComposition",
[this](std::uintptr_t, s64 time,
std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
const auto lock_guard = Lock();
2019-09-22 06:41:34 +00:00
Compose();
2022-07-26 21:04:18 +00:00
return std::chrono::nanoseconds(GetNextTicks());
2019-09-22 06:41:34 +00:00
});
if (system.IsMulticore()) {
2022-07-26 21:04:18 +00:00
system.CoreTiming().ScheduleLoopingEvent(frame_ns, frame_ns, multi_composition_event);
vsync_thread = std::jthread([this](std::stop_token token) { SplitVSync(token); });
} else {
2022-07-26 21:04:18 +00:00
system.CoreTiming().ScheduleLoopingEvent(frame_ns, frame_ns, single_composition_event);
}
}
2023-02-19 20:05:34 +00:00
Nvnflinger::~Nvnflinger() {
2022-07-26 21:04:18 +00:00
if (system.IsMulticore()) {
system.CoreTiming().UnscheduleEvent(multi_composition_event, {});
vsync_thread.request_stop();
2023-06-20 15:41:38 +00:00
vsync_signal.Set();
2022-07-26 21:04:18 +00:00
} else {
system.CoreTiming().UnscheduleEvent(single_composition_event, {});
}
2022-10-23 09:45:45 +00:00
ShutdownLayers();
if (nvdrv) {
nvdrv->Close(disp_fd);
}
}
2023-02-19 20:05:34 +00:00
void Nvnflinger::ShutdownLayers() {
for (auto& display : displays) {
for (size_t layer = 0; layer < display.GetNumLayers(); ++layer) {
display.GetLayer(layer).Core().NotifyShutdown();
}
}
}
2023-02-19 20:05:34 +00:00
void Nvnflinger::SetNVDrvInstance(std::shared_ptr<Nvidia::Module> instance) {
nvdrv = std::move(instance);
disp_fd = nvdrv->Open("/dev/nvdisp_disp0");
}
2023-02-19 20:05:34 +00:00
std::optional<u64> Nvnflinger::OpenDisplay(std::string_view name) {
const auto lock_guard = Lock();
2023-02-19 20:05:34 +00:00
LOG_DEBUG(Service_Nvnflinger, "Opening \"{}\" display", name);
const auto itr =
std::find_if(displays.begin(), displays.end(),
[&](const VI::Display& display) { return display.GetName() == name; });
2019-04-06 20:02:55 +00:00
if (itr == displays.end()) {
return std::nullopt;
}
return itr->GetID();
}
2023-02-19 20:05:34 +00:00
bool Nvnflinger::CloseDisplay(u64 display_id) {
2022-10-25 21:42:02 +00:00
const auto lock_guard = Lock();
auto* const display = FindDisplay(display_id);
if (display == nullptr) {
return false;
}
display->Reset();
return true;
}
2023-02-19 20:05:34 +00:00
std::optional<u64> Nvnflinger::CreateLayer(u64 display_id) {
const auto lock_guard = Lock();
auto* const display = FindDisplay(display_id);
if (display == nullptr) {
return std::nullopt;
}
const u64 layer_id = next_layer_id++;
CreateLayerAtId(*display, layer_id);
return layer_id;
}
2023-02-19 20:05:34 +00:00
void Nvnflinger::CreateLayerAtId(VI::Display& display, u64 layer_id) {
const auto buffer_id = next_buffer_queue_id++;
display.CreateLayer(layer_id, buffer_id, nvdrv->container);
}
2023-02-19 20:05:34 +00:00
void Nvnflinger::CloseLayer(u64 layer_id) {
const auto lock_guard = Lock();
for (auto& display : displays) {
display.CloseLayer(layer_id);
}
}
2023-02-19 20:05:34 +00:00
std::optional<u32> Nvnflinger::FindBufferQueueId(u64 display_id, u64 layer_id) {
const auto lock_guard = Lock();
const auto* const layer = FindOrCreateLayer(display_id, layer_id);
if (layer == nullptr) {
return std::nullopt;
}
return layer->GetBinderId();
}
2023-02-19 20:05:34 +00:00
ResultVal<Kernel::KReadableEvent*> Nvnflinger::FindVsyncEvent(u64 display_id) {
const auto lock_guard = Lock();
auto* const display = FindDisplay(display_id);
if (display == nullptr) {
return VI::ResultNotFound;
}
return display->GetVSyncEvent();
}
2023-02-19 20:05:34 +00:00
VI::Display* Nvnflinger::FindDisplay(u64 display_id) {
const auto itr =
std::find_if(displays.begin(), displays.end(),
[&](const VI::Display& display) { return display.GetID() == display_id; });
if (itr == displays.end()) {
return nullptr;
}
return &*itr;
}
2023-02-19 20:05:34 +00:00
const VI::Display* Nvnflinger::FindDisplay(u64 display_id) const {
const auto itr =
std::find_if(displays.begin(), displays.end(),
[&](const VI::Display& display) { return display.GetID() == display_id; });
if (itr == displays.end()) {
return nullptr;
}
return &*itr;
}
2023-02-19 20:05:34 +00:00
VI::Layer* Nvnflinger::FindLayer(u64 display_id, u64 layer_id) {
auto* const display = FindDisplay(display_id);
if (display == nullptr) {
return nullptr;
}
return display->FindLayer(layer_id);
}
2023-02-19 20:05:34 +00:00
const VI::Layer* Nvnflinger::FindLayer(u64 display_id, u64 layer_id) const {
const auto* const display = FindDisplay(display_id);
if (display == nullptr) {
return nullptr;
}
return display->FindLayer(layer_id);
}
2023-02-19 20:05:34 +00:00
VI::Layer* Nvnflinger::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) {
2023-02-19 20:05:34 +00:00
LOG_DEBUG(Service_Nvnflinger, "Layer at id {} not found. Trying to create it.", layer_id);
CreateLayerAtId(*display, layer_id);
return display->FindLayer(layer_id);
}
return layer;
}
2023-02-19 20:05:34 +00:00
void Nvnflinger::Compose() {
for (auto& display : displays) {
// Trigger vsync for this display at the end of drawing
SCOPE_EXIT({ display.SignalVSyncEvent(); });
// Don't do anything for displays without layers.
if (!display.HasLayers())
continue;
// TODO(Subv): Support more than 1 layer.
VI::Layer& layer = display.GetLayer(0);
android::BufferItem buffer{};
const auto status = layer.GetConsumer().AcquireBuffer(&buffer, {}, false);
if (status != android::Status::NoError) {
continue;
}
const auto& igbp_buffer = *buffer.graphic_buffer;
if (!system.IsPoweredOn()) {
return; // We are likely shutting down
}
// 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>(disp_fd);
ASSERT(nvdisp);
2022-01-30 09:31:13 +00:00
guard->unlock();
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(),
2022-02-06 00:16:11 +00:00
static_cast<android::BufferTransformFlags>(buffer.transform), crop_rect,
buffer.fence.fences, buffer.fence.num_fences);
2022-02-06 00:16:11 +00:00
MicroProfileFlip();
2022-01-30 09:31:13 +00:00
guard->lock();
swap_interval = buffer.swap_interval;
layer.GetConsumer().ReleaseBuffer(buffer, android::Fence::NoFence());
}
}
2023-02-19 20:05:34 +00:00
s64 Nvnflinger::GetNextTicks() const {
const auto& settings = Settings::values;
auto speed_scale = 1.f;
if (settings.use_multi_core.GetValue()) {
if (settings.use_speed_limit.GetValue()) {
// Scales the speed based on speed_limit setting on MC. SC is handled by
// SpeedLimiter::DoSpeedLimiting.
speed_scale = 100.f / settings.speed_limit.GetValue();
} else {
// Run at unlocked framerate.
speed_scale = 0.01f;
}
}
if (system.GetNVDECActive() && settings.use_video_framerate.GetValue()) {
// Run at intended presentation rate during video playback.
speed_scale = 1.f;
}
// As an extension, treat nonpositive swap interval as framerate multiplier.
const f32 effective_fps = swap_interval <= 0 ? 120.f * static_cast<f32>(1 - swap_interval)
: 60.f / static_cast<f32>(swap_interval);
return static_cast<s64>(speed_scale * (1000000000.f / effective_fps));
2019-06-04 20:10:07 +00:00
}
2023-02-19 20:05:34 +00:00
} // namespace Service::Nvnflinger