2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
2021-11-11 22:15:51 -05:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2018-01-22 11:54:58 -05:00
|
|
|
|
|
|
|
#include <algorithm>
|
2018-10-30 00:03:25 -04:00
|
|
|
#include <optional>
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2018-08-07 08:24:30 -04:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
2018-04-18 20:28:50 -04:00
|
|
|
#include "common/microprofile.h"
|
2018-01-22 11:54:58 -05:00
|
|
|
#include "common/scope_exit.h"
|
2021-04-14 19:07:40 -04:00
|
|
|
#include "common/settings.h"
|
2020-05-29 15:00:17 -04:00
|
|
|
#include "common/thread.h"
|
2018-02-14 12:16:39 -05:00
|
|
|
#include "core/core.h"
|
2018-01-22 11:54:58 -05:00
|
|
|
#include "core/core_timing.h"
|
2021-01-30 01:48:06 -05:00
|
|
|
#include "core/hle/kernel/k_readable_event.h"
|
2018-01-22 11:54:58 -05:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvdisp_disp0.h"
|
|
|
|
#include "core/hle/service/nvdrv/nvdrv.h"
|
2021-11-11 22:15:51 -05: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 11:54:58 -05:00
|
|
|
#include "core/hle/service/nvflinger/nvflinger.h"
|
2021-11-11 22:15:51 -05:00
|
|
|
#include "core/hle/service/nvflinger/ui/graphic_buffer.h"
|
2019-02-19 17:00:03 -05:00
|
|
|
#include "core/hle/service/vi/display/vi_display.h"
|
|
|
|
#include "core/hle/service/vi/layer/vi_layer.h"
|
2022-09-25 21:20:36 -04:00
|
|
|
#include "core/hle/service/vi/vi_results.h"
|
2021-10-02 00:39:57 -04:00
|
|
|
#include "video_core/gpu.h"
|
2022-01-30 04:31:13 -05:00
|
|
|
#include "video_core/host1x/host1x.h"
|
|
|
|
#include "video_core/host1x/syncpoint_manager.h"
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
namespace Service::NVFlinger {
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2020-07-15 18:30:06 -04:00
|
|
|
constexpr auto frame_ns = std::chrono::nanoseconds{1000000000 / 60};
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2021-10-02 00:39:57 -04:00
|
|
|
void NVFlinger::SplitVSync(std::stop_token stop_token) {
|
2020-05-29 15:00:17 -04:00
|
|
|
system.RegisterHostThread();
|
2022-10-03 18:43:56 -04:00
|
|
|
std::string name = "VSyncThread";
|
2020-05-29 15:00:17 -04:00
|
|
|
MicroProfileOnThreadCreate(name.c_str());
|
2020-12-30 04:34:50 -05:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
SCOPE_EXIT({ MicroProfileOnThreadExit(); });
|
|
|
|
|
2020-05-29 15:00:17 -04:00
|
|
|
Common::SetCurrentThreadName(name.c_str());
|
|
|
|
Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
|
2022-07-26 17:04:18 -04:00
|
|
|
|
2021-10-02 00:39:57 -04:00
|
|
|
while (!stop_token.stop_requested()) {
|
2022-07-26 17:04:18 -04:00
|
|
|
vsync_signal.wait(false);
|
|
|
|
vsync_signal.store(false);
|
|
|
|
|
2020-05-29 15:00:17 -04:00
|
|
|
guard->lock();
|
2022-07-26 17:04:18 -04:00
|
|
|
|
2020-05-29 15:00:17 -04:00
|
|
|
Compose();
|
2022-07-26 17:04:18 -04:00
|
|
|
|
2020-05-29 15:00:17 -04:00
|
|
|
guard->unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:15:51 -05: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 09:47:02 -05:00
|
|
|
guard = std::make_shared<std::mutex>();
|
2019-02-21 10:43:26 -05:00
|
|
|
|
2018-01-22 11:54:58 -05:00
|
|
|
// Schedule the screen composition events
|
2022-07-26 17:04:18 -04:00
|
|
|
multi_composition_event = Core::Timing::CreateEvent(
|
|
|
|
"ScreenComposition",
|
|
|
|
[this](std::uintptr_t, s64 time,
|
|
|
|
std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
|
|
|
|
vsync_signal.store(true);
|
|
|
|
vsync_signal.notify_all();
|
|
|
|
return std::chrono::nanoseconds(GetNextTicks());
|
|
|
|
});
|
|
|
|
|
|
|
|
single_composition_event = Core::Timing::CreateEvent(
|
2022-07-10 01:59:40 -04:00
|
|
|
"ScreenComposition",
|
|
|
|
[this](std::uintptr_t, s64 time,
|
|
|
|
std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
|
2021-04-26 09:11:33 -04:00
|
|
|
const auto lock_guard = Lock();
|
2019-09-22 02:41:34 -04:00
|
|
|
Compose();
|
2020-07-15 18:30:06 -04:00
|
|
|
|
2022-07-26 17:04:18 -04:00
|
|
|
return std::chrono::nanoseconds(GetNextTicks());
|
2019-09-22 02:41:34 -04:00
|
|
|
});
|
2020-07-15 18:30:06 -04:00
|
|
|
|
2020-05-29 15:00:17 -04:00
|
|
|
if (system.IsMulticore()) {
|
2022-07-26 17:04:18 -04:00
|
|
|
system.CoreTiming().ScheduleLoopingEvent(frame_ns, frame_ns, multi_composition_event);
|
2021-10-02 00:39:57 -04:00
|
|
|
vsync_thread = std::jthread([this](std::stop_token token) { SplitVSync(token); });
|
2020-05-29 15:00:17 -04:00
|
|
|
} else {
|
2022-07-26 17:04:18 -04:00
|
|
|
system.CoreTiming().ScheduleLoopingEvent(frame_ns, frame_ns, single_composition_event);
|
2020-05-29 15:00:17 -04:00
|
|
|
}
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
NVFlinger::~NVFlinger() {
|
2022-07-26 17:04:18 -04:00
|
|
|
if (system.IsMulticore()) {
|
|
|
|
system.CoreTiming().UnscheduleEvent(multi_composition_event, {});
|
|
|
|
vsync_thread.request_stop();
|
|
|
|
vsync_signal.store(true);
|
|
|
|
vsync_signal.notify_all();
|
|
|
|
} else {
|
|
|
|
system.CoreTiming().UnscheduleEvent(single_composition_event, {});
|
2020-05-29 15:00:17 -04:00
|
|
|
}
|
2021-11-11 22:15:51 -05:00
|
|
|
|
2022-10-23 05:45:45 -04:00
|
|
|
ShutdownLayers();
|
|
|
|
|
|
|
|
if (nvdrv) {
|
|
|
|
nvdrv->Close(disp_fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NVFlinger::ShutdownLayers() {
|
2021-11-11 22:15:51 -05:00
|
|
|
for (auto& display : displays) {
|
|
|
|
for (size_t layer = 0; layer < display.GetNumLayers(); ++layer) {
|
|
|
|
display.GetLayer(layer).Core().NotifyShutdown();
|
|
|
|
}
|
|
|
|
}
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
|
2018-08-07 09:17:09 -04:00
|
|
|
void NVFlinger::SetNVDrvInstance(std::shared_ptr<Nvidia::Module> instance) {
|
|
|
|
nvdrv = std::move(instance);
|
2021-11-04 20:44:11 -04:00
|
|
|
disp_fd = nvdrv->Open("/dev/nvdisp_disp0");
|
2018-08-07 09:17:09 -04:00
|
|
|
}
|
|
|
|
|
2019-02-05 16:20:04 -05:00
|
|
|
std::optional<u64> NVFlinger::OpenDisplay(std::string_view name) {
|
2021-04-26 09:11:33 -04:00
|
|
|
const auto lock_guard = Lock();
|
2020-12-10 18:32:52 -05:00
|
|
|
|
2022-04-02 01:55:44 -04:00
|
|
|
LOG_DEBUG(Service_NVFlinger, "Opening \"{}\" display", name);
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2019-02-21 10:43:26 -05:00
|
|
|
const auto itr =
|
|
|
|
std::find_if(displays.begin(), displays.end(),
|
|
|
|
[&](const VI::Display& display) { return display.GetName() == name; });
|
2019-04-06 16:02:55 -04:00
|
|
|
|
2019-02-05 16:20:04 -05:00
|
|
|
if (itr == displays.end()) {
|
2020-09-22 17:31:53 -04:00
|
|
|
return std::nullopt;
|
2019-02-05 16:20:04 -05:00
|
|
|
}
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2019-02-21 10:43:26 -05:00
|
|
|
return itr->GetID();
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
|
2022-10-25 17:42:02 -04:00
|
|
|
bool NVFlinger::CloseDisplay(u64 display_id) {
|
|
|
|
const auto lock_guard = Lock();
|
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
display->Reset();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-05 16:20:04 -05:00
|
|
|
std::optional<u64> NVFlinger::CreateLayer(u64 display_id) {
|
2021-04-26 09:11:33 -04:00
|
|
|
const auto lock_guard = Lock();
|
2019-02-05 16:20:04 -05:00
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
2020-09-22 17:31:53 -04:00
|
|
|
return std::nullopt;
|
2019-02-05 16:20:04 -05:00
|
|
|
}
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2019-01-29 14:52:14 -05:00
|
|
|
const u64 layer_id = next_layer_id++;
|
2021-05-06 11:20:52 -04:00
|
|
|
CreateLayerAtId(*display, layer_id);
|
|
|
|
return layer_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NVFlinger::CreateLayerAtId(VI::Display& display, u64 layer_id) {
|
2021-11-11 22:15:51 -05:00
|
|
|
const auto buffer_id = next_buffer_queue_id++;
|
2022-06-27 00:39:57 -04:00
|
|
|
display.CreateLayer(layer_id, buffer_id, nvdrv->container);
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
|
2020-01-04 00:45:06 -05:00
|
|
|
void NVFlinger::CloseLayer(u64 layer_id) {
|
2021-04-26 09:11:33 -04:00
|
|
|
const auto lock_guard = Lock();
|
2020-12-10 18:32:52 -05:00
|
|
|
|
2020-01-04 00:45:06 -05:00
|
|
|
for (auto& display : displays) {
|
|
|
|
display.CloseLayer(layer_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 11:20:52 -04:00
|
|
|
std::optional<u32> NVFlinger::FindBufferQueueId(u64 display_id, u64 layer_id) {
|
2021-04-26 09:11:33 -04:00
|
|
|
const auto lock_guard = Lock();
|
2021-05-06 11:20:52 -04:00
|
|
|
const auto* const layer = FindOrCreateLayer(display_id, layer_id);
|
2019-02-05 16:20:04 -05:00
|
|
|
|
|
|
|
if (layer == nullptr) {
|
2020-09-22 17:31:53 -04:00
|
|
|
return std::nullopt;
|
2019-02-05 16:20:04 -05:00
|
|
|
}
|
|
|
|
|
2021-11-11 22:15:51 -05:00
|
|
|
return layer->GetBinderId();
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
|
2022-09-25 21:20:36 -04:00
|
|
|
ResultVal<Kernel::KReadableEvent*> NVFlinger::FindVsyncEvent(u64 display_id) {
|
2021-04-10 05:34:26 -04:00
|
|
|
const auto lock_guard = Lock();
|
2019-02-05 16:20:04 -05:00
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
2022-09-25 21:20:36 -04:00
|
|
|
return VI::ResultNotFound;
|
2019-02-05 16:20:04 -05:00
|
|
|
}
|
|
|
|
|
2022-09-25 21:20:36 -04:00
|
|
|
return display->GetVSyncEvent();
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
|
2019-02-19 17:00:03 -05:00
|
|
|
VI::Display* NVFlinger::FindDisplay(u64 display_id) {
|
|
|
|
const auto itr =
|
|
|
|
std::find_if(displays.begin(), displays.end(),
|
2019-02-21 10:43:26 -05:00
|
|
|
[&](const VI::Display& display) { return display.GetID() == display_id; });
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2019-02-05 16:20:04 -05:00
|
|
|
if (itr == displays.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &*itr;
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
|
2019-02-19 17:00:03 -05:00
|
|
|
const VI::Display* NVFlinger::FindDisplay(u64 display_id) const {
|
|
|
|
const auto itr =
|
|
|
|
std::find_if(displays.begin(), displays.end(),
|
2019-02-21 10:43:26 -05:00
|
|
|
[&](const VI::Display& display) { return display.GetID() == display_id; });
|
2019-01-30 11:14:05 -05:00
|
|
|
|
2019-02-05 16:20:04 -05:00
|
|
|
if (itr == displays.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &*itr;
|
2019-01-30 11:14:05 -05:00
|
|
|
}
|
|
|
|
|
2019-02-19 17:00:03 -05:00
|
|
|
VI::Layer* NVFlinger::FindLayer(u64 display_id, u64 layer_id) {
|
2019-02-05 16:20:04 -05:00
|
|
|
auto* const display = FindDisplay(display_id);
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2019-02-05 16:20:04 -05:00
|
|
|
if (display == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:43:26 -05:00
|
|
|
return display->FindLayer(layer_id);
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
|
2019-02-19 17:00:03 -05:00
|
|
|
const VI::Layer* NVFlinger::FindLayer(u64 display_id, u64 layer_id) const {
|
2019-02-05 16:20:04 -05:00
|
|
|
const auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-01-30 11:14:05 -05:00
|
|
|
|
2019-02-21 10:43:26 -05:00
|
|
|
return display->FindLayer(layer_id);
|
2019-01-30 11:14:05 -05:00
|
|
|
}
|
|
|
|
|
2021-05-06 11:20:52 -04: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) {
|
2022-04-02 01:55:44 -04:00
|
|
|
LOG_DEBUG(Service_NVFlinger, "Layer at id {} not found. Trying to create it.", layer_id);
|
2021-05-06 11:20:52 -04:00
|
|
|
CreateLayerAtId(*display, layer_id);
|
|
|
|
return display->FindLayer(layer_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2018-01-22 11:54:58 -05:00
|
|
|
void NVFlinger::Compose() {
|
|
|
|
for (auto& display : displays) {
|
|
|
|
// Trigger vsync for this display at the end of drawing
|
2019-02-21 10:43:26 -05:00
|
|
|
SCOPE_EXIT({ display.SignalVSyncEvent(); });
|
2018-01-22 11:54:58 -05:00
|
|
|
|
|
|
|
// Don't do anything for displays without layers.
|
2019-02-21 10:43:26 -05:00
|
|
|
if (!display.HasLayers())
|
2018-01-22 11:54:58 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO(Subv): Support more than 1 layer.
|
2019-02-21 10:43:26 -05:00
|
|
|
VI::Layer& layer = display.GetLayer(0);
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2021-11-11 22:15:51 -05:00
|
|
|
android::BufferItem buffer{};
|
2022-03-20 00:52:08 -04:00
|
|
|
const auto status = layer.GetConsumer().AcquireBuffer(&buffer, {}, false);
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2021-11-11 22:15:51 -05:00
|
|
|
if (status != android::Status::NoError) {
|
2018-01-22 11:54:58 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:15:51 -05:00
|
|
|
const auto& igbp_buffer = *buffer.graphic_buffer;
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2020-10-28 20:17:38 -04:00
|
|
|
if (!system.IsPoweredOn()) {
|
|
|
|
return; // We are likely shutting down
|
|
|
|
}
|
|
|
|
|
2018-01-22 11:54:58 -05: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)
|
2021-11-04 20:44:11 -04:00
|
|
|
auto nvdisp = nvdrv->GetDevice<Nvidia::Devices::nvdisp_disp0>(disp_fd);
|
2018-01-22 11:54:58 -05:00
|
|
|
ASSERT(nvdisp);
|
|
|
|
|
2022-01-30 04:31:13 -05:00
|
|
|
guard->unlock();
|
2021-11-11 22:15:51 -05: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(),
|
2022-02-05 19:16:11 -05:00
|
|
|
static_cast<android::BufferTransformFlags>(buffer.transform), crop_rect,
|
|
|
|
buffer.fence.fences, buffer.fence.num_fences);
|
2021-11-11 22:15:51 -05:00
|
|
|
|
2022-02-05 19:16:11 -05:00
|
|
|
MicroProfileFlip();
|
2022-01-30 04:31:13 -05:00
|
|
|
guard->lock();
|
|
|
|
|
2021-11-11 22:15:51 -05:00
|
|
|
swap_interval = buffer.swap_interval;
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2022-11-28 19:15:24 -05:00
|
|
|
layer.GetConsumer().ReleaseBuffer(buffer, android::Fence::NoFence());
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 20:53:21 -04:00
|
|
|
s64 NVFlinger::GetNextTicks() const {
|
2021-07-22 21:03:50 -04:00
|
|
|
static constexpr s64 max_hertz = 120LL;
|
|
|
|
|
|
|
|
const auto& settings = Settings::values;
|
2022-06-26 16:58:06 -04:00
|
|
|
auto speed_scale = 1.f;
|
2022-07-16 01:14:00 -04:00
|
|
|
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;
|
|
|
|
}
|
2022-06-26 16:58:06 -04:00
|
|
|
}
|
2022-07-16 16:26:47 -04:00
|
|
|
|
|
|
|
const auto next_ticks = ((1000000000 * (1LL << swap_interval)) / max_hertz);
|
|
|
|
|
|
|
|
return static_cast<s64>(speed_scale * static_cast<float>(next_ticks));
|
2019-06-04 16:10:07 -04:00
|
|
|
}
|
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
} // namespace Service::NVFlinger
|