2018-01-22 17:54:58 +01: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 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"
|
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"
|
2018-07-24 12:03:24 +02:00
|
|
|
#include "core/core_timing_util.h"
|
2018-11-27 00:34:07 +01:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/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"
|
|
|
|
#include "core/hle/service/nvflinger/buffer_queue.h"
|
|
|
|
#include "core/hle/service/nvflinger/nvflinger.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"
|
2018-08-30 16:50:54 +02:00
|
|
|
#include "core/perf_stats.h"
|
2019-04-06 22:02:55 +02:00
|
|
|
#include "core/settings.h"
|
2018-01-22 17:54:58 +01:00
|
|
|
#include "video_core/renderer_base.h"
|
|
|
|
|
2018-04-20 03:41:44 +02:00
|
|
|
namespace Service::NVFlinger {
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2019-04-06 22:02:55 +02:00
|
|
|
constexpr s64 frame_ticks = static_cast<s64>(Core::Timing::BASE_CLOCK_RATE / 60);
|
|
|
|
constexpr s64 frame_ticks_30fps = static_cast<s64>(Core::Timing::BASE_CLOCK_RATE / 30);
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2019-09-22 08:41:34 +02:00
|
|
|
NVFlinger::NVFlinger(Core::System& system) : system(system) {
|
2019-09-21 11:37:03 +02: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);
|
2019-02-21 16:43:26 +01:00
|
|
|
|
2018-01-22 17:54:58 +01:00
|
|
|
// Schedule the screen composition events
|
2019-09-22 08:41:34 +02:00
|
|
|
composition_event = system.CoreTiming().RegisterEvent(
|
|
|
|
"ScreenComposition", [this](u64 userdata, s64 cycles_late) {
|
|
|
|
Compose();
|
|
|
|
const auto ticks =
|
|
|
|
Settings::values.force_30fps_mode ? frame_ticks_30fps : GetNextTicks();
|
|
|
|
this->system.CoreTiming().ScheduleEvent(std::max<s64>(0LL, ticks - cycles_late),
|
|
|
|
composition_event);
|
|
|
|
});
|
|
|
|
|
|
|
|
system.CoreTiming().ScheduleEvent(frame_ticks, composition_event);
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NVFlinger::~NVFlinger() {
|
2019-09-22 08:41:34 +02:00
|
|
|
system.CoreTiming().UnscheduleEvent(composition_event, 0);
|
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) {
|
2019-01-29 20:53:50 +01:00
|
|
|
LOG_DEBUG(Service, "Opening \"{}\" display", name);
|
2018-01-22 17:54:58 +01:00
|
|
|
|
|
|
|
// TODO(Subv): Currently we only support the Default display.
|
|
|
|
ASSERT(name == "Default");
|
|
|
|
|
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()) {
|
|
|
|
return {};
|
|
|
|
}
|
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) {
|
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2019-01-29 20:52:14 +01:00
|
|
|
const u64 layer_id = next_layer_id++;
|
|
|
|
const u32 buffer_queue_id = next_buffer_queue_id++;
|
2019-10-12 15:17:56 +02:00
|
|
|
buffer_queues.emplace_back(system.Kernel(), buffer_queue_id, layer_id);
|
2019-02-21 17:31:53 +01:00
|
|
|
display->CreateLayer(layer_id, buffer_queues.back());
|
2018-01-22 17:54:58 +01:00
|
|
|
return layer_id;
|
|
|
|
}
|
|
|
|
|
2019-02-05 22:20:04 +01: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 16:56:20 +01:00
|
|
|
return layer->GetBufferQueue().GetId();
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 21:57:26 +01:00
|
|
|
Kernel::SharedPtr<Kernel::ReadableEvent> NVFlinger::FindVsyncEvent(u64 display_id) const {
|
2019-02-05 22:20:04 +01:00
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:43:26 +01:00
|
|
|
return display->GetVSyncEvent();
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 17:31:53 +01:00
|
|
|
BufferQueue& NVFlinger::FindBufferQueue(u32 id) {
|
2019-01-29 20:52:14 +01:00
|
|
|
const auto itr = std::find_if(buffer_queues.begin(), buffer_queues.end(),
|
2019-02-21 17:31:53 +01: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 17:54:58 +01:00
|
|
|
|
|
|
|
ASSERT(itr != buffer_queues.end());
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
2019-02-21 16:56:20 +01:00
|
|
|
auto& buffer_queue = layer.GetBufferQueue();
|
2018-01-22 17:54:58 +01:00
|
|
|
|
|
|
|
// Search for a queued buffer and acquire it
|
2019-02-21 16:56:20 +01:00
|
|
|
auto buffer = buffer_queue.AcquireBuffer();
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2018-04-19 02:28:50 +02:00
|
|
|
MicroProfileFlip();
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
if (!buffer) {
|
2018-01-22 17:54:58 +01:00
|
|
|
// There was no queued buffer to draw, render previous frame
|
2019-09-21 11:23:31 +02:00
|
|
|
system.GetPerfStats().EndGameFrame();
|
|
|
|
system.GPU().SwapBuffers({});
|
2018-01-22 17:54:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-29 20:52:14 +01:00
|
|
|
const auto& igbp_buffer = buffer->get().igbp_buffer;
|
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);
|
|
|
|
|
|
|
|
nvdisp->flip(igbp_buffer.gpu_buffer_id, igbp_buffer.offset, igbp_buffer.format,
|
2018-10-30 05:03:25 +01:00
|
|
|
igbp_buffer.width, igbp_buffer.height, igbp_buffer.stride,
|
|
|
|
buffer->get().transform, buffer->get().crop_rect);
|
2018-01-22 17:54:58 +01:00
|
|
|
|
2019-06-04 22:10:07 +02:00
|
|
|
swap_interval = buffer->get().swap_interval;
|
2019-02-21 16:56:20 +01:00
|
|
|
buffer_queue.ReleaseBuffer(buffer->get().slot);
|
2018-01-22 17:54:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 02:53:21 +02:00
|
|
|
s64 NVFlinger::GetNextTicks() const {
|
|
|
|
constexpr s64 max_hertz = 120LL;
|
|
|
|
return (Core::Timing::BASE_CLOCK_RATE * (1LL << swap_interval)) / max_hertz;
|
2019-06-04 22:10:07 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 03:41:44 +02:00
|
|
|
} // namespace Service::NVFlinger
|