2018-01-22 16:54:58 +00: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 04:03:25 +00:00
|
|
|
#include <optional>
|
2018-01-22 16:54:58 +00:00
|
|
|
|
2018-08-07 12:24:30 +00:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/logging/log.h"
|
2018-04-19 00:28:50 +00:00
|
|
|
#include "common/microprofile.h"
|
2018-01-22 16:54:58 +00:00
|
|
|
#include "common/scope_exit.h"
|
2018-02-14 17:16:39 +00:00
|
|
|
#include "core/core.h"
|
2018-01-22 16:54:58 +00:00
|
|
|
#include "core/core_timing.h"
|
2018-07-24 10:03:24 +00:00
|
|
|
#include "core/core_timing_util.h"
|
2018-11-26 23:34:07 +00:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/readable_event.h"
|
2018-01-22 16:54:58 +00: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 22:00:03 +00:00
|
|
|
#include "core/hle/service/vi/display/vi_display.h"
|
|
|
|
#include "core/hle/service/vi/layer/vi_layer.h"
|
2018-08-30 14:50:54 +00:00
|
|
|
#include "core/perf_stats.h"
|
2018-01-22 16:54:58 +00:00
|
|
|
#include "video_core/renderer_base.h"
|
|
|
|
|
2018-04-20 01:41:44 +00:00
|
|
|
namespace Service::NVFlinger {
|
2018-01-22 16:54:58 +00:00
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
constexpr std::size_t SCREEN_REFRESH_RATE = 60;
|
2019-02-12 17:32:15 +00:00
|
|
|
constexpr u64 frame_ticks = static_cast<u64>(Core::Timing::BASE_CLOCK_RATE / SCREEN_REFRESH_RATE);
|
2018-01-22 16:54:58 +00:00
|
|
|
|
2019-02-19 22:00:03 +00:00
|
|
|
NVFlinger::NVFlinger(Core::Timing::CoreTiming& core_timing)
|
|
|
|
: displays{{0, "Default"}, {1, "External"}, {2, "Edid"}, {3, "Internal"}, {4, "Null"}},
|
|
|
|
core_timing{core_timing} {
|
2018-01-22 16:54:58 +00:00
|
|
|
// Schedule the screen composition events
|
|
|
|
composition_event =
|
2019-02-14 17:42:58 +00:00
|
|
|
core_timing.RegisterEvent("ScreenComposition", [this](u64 userdata, int cycles_late) {
|
2018-01-22 16:54:58 +00:00
|
|
|
Compose();
|
2019-02-14 17:42:58 +00:00
|
|
|
this->core_timing.ScheduleEvent(frame_ticks - cycles_late, composition_event);
|
2018-01-22 16:54:58 +00:00
|
|
|
});
|
|
|
|
|
2019-02-14 17:42:58 +00:00
|
|
|
core_timing.ScheduleEvent(frame_ticks, composition_event);
|
2018-01-22 16:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NVFlinger::~NVFlinger() {
|
2019-02-14 17:42:58 +00:00
|
|
|
core_timing.UnscheduleEvent(composition_event, 0);
|
2018-01-22 16:54:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 13:17:09 +00:00
|
|
|
void NVFlinger::SetNVDrvInstance(std::shared_ptr<Nvidia::Module> instance) {
|
|
|
|
nvdrv = std::move(instance);
|
|
|
|
}
|
|
|
|
|
2019-02-05 21:20:04 +00:00
|
|
|
std::optional<u64> NVFlinger::OpenDisplay(std::string_view name) {
|
2019-01-29 19:53:50 +00:00
|
|
|
LOG_DEBUG(Service, "Opening \"{}\" display", name);
|
2018-01-22 16:54:58 +00:00
|
|
|
|
|
|
|
// TODO(Subv): Currently we only support the Default display.
|
|
|
|
ASSERT(name == "Default");
|
|
|
|
|
2019-01-29 19:52:14 +00:00
|
|
|
const auto itr = std::find_if(displays.begin(), displays.end(),
|
2019-02-19 22:00:03 +00:00
|
|
|
[&](const VI::Display& display) { return display.name == name; });
|
2019-02-05 21:20:04 +00:00
|
|
|
if (itr == displays.end()) {
|
|
|
|
return {};
|
|
|
|
}
|
2018-01-22 16:54:58 +00:00
|
|
|
|
|
|
|
return itr->id;
|
|
|
|
}
|
|
|
|
|
2019-02-05 21:20:04 +00:00
|
|
|
std::optional<u64> NVFlinger::CreateLayer(u64 display_id) {
|
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
2018-01-22 16:54:58 +00:00
|
|
|
|
2019-02-05 21:20:04 +00:00
|
|
|
ASSERT_MSG(display->layers.empty(), "Only one layer is supported per display at the moment");
|
2018-01-22 16:54:58 +00:00
|
|
|
|
2019-01-29 19:52:14 +00:00
|
|
|
const u64 layer_id = next_layer_id++;
|
|
|
|
const u32 buffer_queue_id = next_buffer_queue_id++;
|
2018-01-22 16:54:58 +00:00
|
|
|
auto buffer_queue = std::make_shared<BufferQueue>(buffer_queue_id, layer_id);
|
2019-02-05 21:20:04 +00:00
|
|
|
display->layers.emplace_back(layer_id, buffer_queue);
|
2018-01-22 16:54:58 +00:00
|
|
|
buffer_queues.emplace_back(std::move(buffer_queue));
|
|
|
|
return layer_id;
|
|
|
|
}
|
|
|
|
|
2019-02-05 21:20:04 +00: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 {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return layer->buffer_queue->GetId();
|
2018-01-22 16:54:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 20:57:26 +00:00
|
|
|
Kernel::SharedPtr<Kernel::ReadableEvent> NVFlinger::FindVsyncEvent(u64 display_id) const {
|
2019-02-05 21:20:04 +00:00
|
|
|
auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return display->vsync_event.readable;
|
2018-01-22 16:54:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 04:30:22 +00:00
|
|
|
std::shared_ptr<BufferQueue> NVFlinger::FindBufferQueue(u32 id) const {
|
2019-01-29 19:52:14 +00:00
|
|
|
const auto itr = std::find_if(buffer_queues.begin(), buffer_queues.end(),
|
|
|
|
[&](const auto& queue) { return queue->GetId() == id; });
|
2018-01-22 16:54:58 +00:00
|
|
|
|
|
|
|
ASSERT(itr != buffer_queues.end());
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
|
2019-02-19 22:00:03 +00:00
|
|
|
VI::Display* NVFlinger::FindDisplay(u64 display_id) {
|
|
|
|
const auto itr =
|
|
|
|
std::find_if(displays.begin(), displays.end(),
|
|
|
|
[&](const VI::Display& display) { return display.id == display_id; });
|
2018-01-22 16:54:58 +00:00
|
|
|
|
2019-02-05 21:20:04 +00:00
|
|
|
if (itr == displays.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &*itr;
|
2018-01-22 16:54:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 22:00:03 +00:00
|
|
|
const VI::Display* NVFlinger::FindDisplay(u64 display_id) const {
|
|
|
|
const auto itr =
|
|
|
|
std::find_if(displays.begin(), displays.end(),
|
|
|
|
[&](const VI::Display& display) { return display.id == display_id; });
|
2019-01-30 16:14:05 +00:00
|
|
|
|
2019-02-05 21:20:04 +00:00
|
|
|
if (itr == displays.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &*itr;
|
2019-01-30 16:14:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 22:00:03 +00:00
|
|
|
VI::Layer* NVFlinger::FindLayer(u64 display_id, u64 layer_id) {
|
2019-02-05 21:20:04 +00:00
|
|
|
auto* const display = FindDisplay(display_id);
|
2018-01-22 16:54:58 +00:00
|
|
|
|
2019-02-05 21:20:04 +00:00
|
|
|
if (display == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto itr = std::find_if(display->layers.begin(), display->layers.end(),
|
2019-02-19 22:00:03 +00:00
|
|
|
[&](const VI::Layer& layer) { return layer.id == layer_id; });
|
2018-01-22 16:54:58 +00:00
|
|
|
|
2019-02-05 21:20:04 +00:00
|
|
|
if (itr == display->layers.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &*itr;
|
2018-01-22 16:54:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 22:00:03 +00:00
|
|
|
const VI::Layer* NVFlinger::FindLayer(u64 display_id, u64 layer_id) const {
|
2019-02-05 21:20:04 +00:00
|
|
|
const auto* const display = FindDisplay(display_id);
|
|
|
|
|
|
|
|
if (display == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-01-30 16:14:05 +00:00
|
|
|
|
2019-02-05 21:20:04 +00:00
|
|
|
const auto itr = std::find_if(display->layers.begin(), display->layers.end(),
|
2019-02-19 22:00:03 +00:00
|
|
|
[&](const VI::Layer& layer) { return layer.id == layer_id; });
|
2019-01-30 16:14:05 +00:00
|
|
|
|
2019-02-05 21:20:04 +00:00
|
|
|
if (itr == display->layers.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &*itr;
|
2019-01-30 16:14:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 16:54:58 +00:00
|
|
|
void NVFlinger::Compose() {
|
|
|
|
for (auto& display : displays) {
|
|
|
|
// Trigger vsync for this display at the end of drawing
|
2018-11-27 14:18:29 +00:00
|
|
|
SCOPE_EXIT({ display.vsync_event.writable->Signal(); });
|
2018-01-22 16:54:58 +00:00
|
|
|
|
|
|
|
// Don't do anything for displays without layers.
|
|
|
|
if (display.layers.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO(Subv): Support more than 1 layer.
|
|
|
|
ASSERT_MSG(display.layers.size() == 1, "Max 1 layer per display is supported");
|
|
|
|
|
2019-02-19 22:00:03 +00:00
|
|
|
VI::Layer& layer = display.layers[0];
|
2018-01-22 16:54:58 +00:00
|
|
|
auto& buffer_queue = layer.buffer_queue;
|
|
|
|
|
|
|
|
// Search for a queued buffer and acquire it
|
|
|
|
auto buffer = buffer_queue->AcquireBuffer();
|
|
|
|
|
2018-04-19 00:28:50 +00:00
|
|
|
MicroProfileFlip();
|
|
|
|
|
2018-10-30 04:03:25 +00:00
|
|
|
if (!buffer) {
|
2018-08-03 16:55:58 +00:00
|
|
|
auto& system_instance = Core::System::GetInstance();
|
|
|
|
|
2018-01-22 16:54:58 +00:00
|
|
|
// There was no queued buffer to draw, render previous frame
|
2018-08-30 14:50:54 +00:00
|
|
|
system_instance.GetPerfStats().EndGameFrame();
|
2018-08-03 16:55:58 +00:00
|
|
|
system_instance.Renderer().SwapBuffers({});
|
2018-01-22 16:54:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-29 19:52:14 +00:00
|
|
|
const auto& igbp_buffer = buffer->get().igbp_buffer;
|
2018-01-22 16:54:58 +00: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 04:03:25 +00:00
|
|
|
igbp_buffer.width, igbp_buffer.height, igbp_buffer.stride,
|
|
|
|
buffer->get().transform, buffer->get().crop_rect);
|
2018-01-22 16:54:58 +00:00
|
|
|
|
2018-10-30 04:03:25 +00:00
|
|
|
buffer_queue->ReleaseBuffer(buffer->get().slot);
|
2018-01-22 16:54:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 01:41:44 +00:00
|
|
|
} // namespace Service::NVFlinger
|