2018-01-22 11:54:58 -05:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#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"
|
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"
|
2018-07-24 06:03:24 -04:00
|
|
|
#include "core/core_timing_util.h"
|
2020-02-11 18:56:24 -05:00
|
|
|
#include "core/hardware_properties.h"
|
2018-11-26 18:34:07 -05:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/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"
|
|
|
|
#include "core/hle/service/nvflinger/buffer_queue.h"
|
|
|
|
#include "core/hle/service/nvflinger/nvflinger.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"
|
2018-08-30 10:50:54 -04:00
|
|
|
#include "core/perf_stats.h"
|
2019-04-06 16:02:55 -04:00
|
|
|
#include "core/settings.h"
|
2018-01-22 11:54:58 -05:00
|
|
|
#include "video_core/renderer_base.h"
|
|
|
|
|
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
|
|
|
|
2020-05-29 15:00:17 -04:00
|
|
|
void NVFlinger::VSyncThread(NVFlinger& nv_flinger) {
|
|
|
|
nv_flinger.SplitVSync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NVFlinger::SplitVSync() {
|
|
|
|
system.RegisterHostThread();
|
|
|
|
std::string name = "yuzu:VSyncThread";
|
|
|
|
MicroProfileOnThreadCreate(name.c_str());
|
|
|
|
Common::SetCurrentThreadName(name.c_str());
|
|
|
|
Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
|
|
|
|
s64 delay = 0;
|
|
|
|
while (is_running) {
|
|
|
|
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) {
|
|
|
|
wait_event->WaitFor(std::chrono::nanoseconds{next_time});
|
|
|
|
}
|
|
|
|
delay = (system.CoreTiming().GetGlobalTimeNs().count() - time_end) - next_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 02:41:34 -04:00
|
|
|
NVFlinger::NVFlinger(Core::System& system) : system(system) {
|
2019-09-21 05:37:03 -04:00
|
|
|
displays.emplace_back(0, "Default", system);
|
|
|
|
displays.emplace_back(1, "External", system);
|
|
|
|
displays.emplace_back(2, "Edid", system);
|
|
|
|
displays.emplace_back(3, "Internal", system);
|
|
|
|
displays.emplace_back(4, "Null", 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
|
2020-07-15 19:14:21 -04:00
|
|
|
composition_event = Core::Timing::CreateEvent(
|
2020-07-27 19:00:41 -04:00
|
|
|
"ScreenComposition", [this](std::uintptr_t, std::chrono::nanoseconds ns_late) {
|
2020-07-25 18:53:25 -04:00
|
|
|
const auto guard = Lock();
|
2019-09-22 02:41:34 -04:00
|
|
|
Compose();
|
2020-07-15 18:30:06 -04:00
|
|
|
|
|
|
|
const auto ticks = std::chrono::nanoseconds{GetNextTicks()};
|
2020-07-15 19:14:21 -04:00
|
|
|
const auto ticks_delta = ticks - ns_late;
|
2020-07-15 18:30:06 -04:00
|
|
|
const auto future_ns = std::max(std::chrono::nanoseconds::zero(), ticks_delta);
|
|
|
|
|
|
|
|
this->system.CoreTiming().ScheduleEvent(future_ns, composition_event);
|
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()) {
|
|
|
|
is_running = true;
|
|
|
|
wait_event = std::make_unique<Common::Event>();
|
|
|
|
vsync_thread = std::make_unique<std::thread>(VSyncThread, std::ref(*this));
|
|
|
|
} else {
|
2020-07-15 18:30:06 -04:00
|
|
|
system.CoreTiming().ScheduleEvent(frame_ns, composition_event);
|
2020-05-29 15:00:17 -04:00
|
|
|
}
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
NVFlinger::~NVFlinger() {
|
2020-05-29 15:00:17 -04:00
|
|
|
if (system.IsMulticore()) {
|
|
|
|
is_running = false;
|
|
|
|
wait_event->Set();
|
|
|
|
vsync_thread->join();
|
|
|
|
vsync_thread.reset();
|
|
|
|
wait_event.reset();
|
|
|
|
} else {
|
|
|
|
system.CoreTiming().UnscheduleEvent(composition_event, 0);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2019-02-05 16:20:04 -05:00
|
|
|
std::optional<u64> NVFlinger::OpenDisplay(std::string_view name) {
|
2019-01-29 14:53:50 -05:00
|
|
|
LOG_DEBUG(Service, "Opening \"{}\" display", name);
|
2018-01-22 11:54:58 -05:00
|
|
|
|
|
|
|
// TODO(Subv): Currently we only support the Default display.
|
|
|
|
ASSERT(name == "Default");
|
|
|
|
|
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()) {
|
|
|
|
return {};
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-02-05 16:20:04 -05:00
|
|
|
std::optional<u64> NVFlinger::CreateLayer(u64 display_id) {
|
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2019-01-29 14:52:14 -05:00
|
|
|
const u64 layer_id = next_layer_id++;
|
|
|
|
const u32 buffer_queue_id = next_buffer_queue_id++;
|
2019-10-12 09:17:56 -04:00
|
|
|
buffer_queues.emplace_back(system.Kernel(), buffer_queue_id, layer_id);
|
2019-02-21 11:31:53 -05:00
|
|
|
display->CreateLayer(layer_id, buffer_queues.back());
|
2018-01-22 11:54:58 -05:00
|
|
|
return layer_id;
|
|
|
|
}
|
|
|
|
|
2020-01-04 00:45:06 -05:00
|
|
|
void NVFlinger::CloseLayer(u64 layer_id) {
|
|
|
|
for (auto& display : displays) {
|
|
|
|
display.CloseLayer(layer_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 16:20:04 -05:00
|
|
|
std::optional<u32> NVFlinger::FindBufferQueueId(u64 display_id, u64 layer_id) const {
|
|
|
|
const auto* const layer = FindLayer(display_id, layer_id);
|
|
|
|
|
|
|
|
if (layer == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:56:20 -05:00
|
|
|
return layer->GetBufferQueue().GetId();
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
|
2019-11-24 20:15:51 -05:00
|
|
|
std::shared_ptr<Kernel::ReadableEvent> NVFlinger::FindVsyncEvent(u64 display_id) const {
|
2019-02-05 16:20:04 -05:00
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:43:26 -05:00
|
|
|
return display->GetVSyncEvent();
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
|
2019-02-21 11:31:53 -05:00
|
|
|
BufferQueue& NVFlinger::FindBufferQueue(u32 id) {
|
2019-01-29 14:52:14 -05:00
|
|
|
const auto itr = std::find_if(buffer_queues.begin(), buffer_queues.end(),
|
2019-02-21 11:31:53 -05:00
|
|
|
[id](const auto& queue) { return queue.GetId() == id; });
|
|
|
|
|
|
|
|
ASSERT(itr != buffer_queues.end());
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BufferQueue& NVFlinger::FindBufferQueue(u32 id) const {
|
|
|
|
const auto itr = std::find_if(buffer_queues.begin(), buffer_queues.end(),
|
|
|
|
[id](const auto& queue) { return queue.GetId() == id; });
|
2018-01-22 11:54:58 -05:00
|
|
|
|
|
|
|
ASSERT(itr != buffer_queues.end());
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
2019-02-21 10:56:20 -05:00
|
|
|
auto& buffer_queue = layer.GetBufferQueue();
|
2018-01-22 11:54:58 -05:00
|
|
|
|
|
|
|
// Search for a queued buffer and acquire it
|
2019-02-21 10:56:20 -05:00
|
|
|
auto buffer = buffer_queue.AcquireBuffer();
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2018-10-30 00:03:25 -04:00
|
|
|
if (!buffer) {
|
2018-01-22 11:54:58 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-29 14:52:14 -05:00
|
|
|
const auto& igbp_buffer = buffer->get().igbp_buffer;
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2019-12-30 07:03:20 -05:00
|
|
|
auto& gpu = system.GPU();
|
2019-09-25 19:43:23 -04:00
|
|
|
const auto& multi_fence = buffer->get().multi_fence;
|
2020-05-29 15:00:17 -04:00
|
|
|
guard->unlock();
|
2019-09-25 19:43:23 -04:00
|
|
|
for (u32 fence_id = 0; fence_id < multi_fence.num_fences; fence_id++) {
|
|
|
|
const auto& fence = multi_fence.fences[fence_id];
|
|
|
|
gpu.WaitFence(fence.id, fence.value);
|
|
|
|
}
|
2020-05-29 15:00:17 -04:00
|
|
|
guard->lock();
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2020-03-12 10:52:44 -04:00
|
|
|
MicroProfileFlip();
|
|
|
|
|
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)
|
|
|
|
auto nvdisp = nvdrv->GetDevice<Nvidia::Devices::nvdisp_disp0>("/dev/nvdisp_disp0");
|
|
|
|
ASSERT(nvdisp);
|
|
|
|
|
|
|
|
nvdisp->flip(igbp_buffer.gpu_buffer_id, igbp_buffer.offset, igbp_buffer.format,
|
2018-10-30 00:03:25 -04:00
|
|
|
igbp_buffer.width, igbp_buffer.height, igbp_buffer.stride,
|
|
|
|
buffer->get().transform, buffer->get().crop_rect);
|
2018-01-22 11:54:58 -05:00
|
|
|
|
2019-06-04 16:10:07 -04:00
|
|
|
swap_interval = buffer->get().swap_interval;
|
2019-02-21 10:56:20 -05:00
|
|
|
buffer_queue.ReleaseBuffer(buffer->get().slot);
|
2018-01-22 11:54:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 20:53:21 -04:00
|
|
|
s64 NVFlinger::GetNextTicks() const {
|
|
|
|
constexpr s64 max_hertz = 120LL;
|
2020-02-24 21:04:12 -05:00
|
|
|
return (1000000000 * (1LL << swap_interval)) / max_hertz;
|
2019-06-04 16:10:07 -04:00
|
|
|
}
|
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
} // namespace Service::NVFlinger
|