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-08-07 12:24:30 +00:00
|
|
|
#include <boost/optional.hpp>
|
2018-01-22 16:54:58 +00:00
|
|
|
|
|
|
|
#include "common/alignment.h"
|
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-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"
|
|
|
|
#include "video_core/renderer_base.h"
|
|
|
|
#include "video_core/video_core.h"
|
|
|
|
|
2018-04-20 01:41:44 +00:00
|
|
|
namespace Service::NVFlinger {
|
2018-01-22 16:54:58 +00:00
|
|
|
|
|
|
|
constexpr size_t SCREEN_REFRESH_RATE = 60;
|
2018-04-30 07:24:27 +00:00
|
|
|
constexpr u64 frame_ticks = static_cast<u64>(CoreTiming::BASE_CLOCK_RATE / SCREEN_REFRESH_RATE);
|
2018-01-22 16:54:58 +00:00
|
|
|
|
|
|
|
NVFlinger::NVFlinger() {
|
|
|
|
// Add the different displays to the list of displays.
|
2018-07-19 15:46:59 +00:00
|
|
|
displays.emplace_back(0, "Default");
|
|
|
|
displays.emplace_back(1, "External");
|
|
|
|
displays.emplace_back(2, "Edid");
|
|
|
|
displays.emplace_back(3, "Internal");
|
2018-01-22 16:54:58 +00:00
|
|
|
|
|
|
|
// Schedule the screen composition events
|
|
|
|
composition_event =
|
2018-08-07 13:03:05 +00:00
|
|
|
CoreTiming::RegisterEvent("ScreenComposition", [this](u64 userdata, int cycles_late) {
|
2018-01-22 16:54:58 +00:00
|
|
|
Compose();
|
|
|
|
CoreTiming::ScheduleEvent(frame_ticks - cycles_late, composition_event);
|
|
|
|
});
|
|
|
|
|
|
|
|
CoreTiming::ScheduleEvent(frame_ticks, composition_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
NVFlinger::~NVFlinger() {
|
|
|
|
CoreTiming::UnscheduleEvent(composition_event, 0);
|
|
|
|
}
|
|
|
|
|
2018-08-07 13:17:09 +00:00
|
|
|
void NVFlinger::SetNVDrvInstance(std::shared_ptr<Nvidia::Module> instance) {
|
|
|
|
nvdrv = std::move(instance);
|
|
|
|
}
|
|
|
|
|
2018-08-07 12:19:24 +00:00
|
|
|
u64 NVFlinger::OpenDisplay(std::string_view name) {
|
2018-07-02 16:13:26 +00:00
|
|
|
LOG_WARNING(Service, "Opening display {}", name);
|
2018-01-22 16:54:58 +00:00
|
|
|
|
|
|
|
// TODO(Subv): Currently we only support the Default display.
|
|
|
|
ASSERT(name == "Default");
|
|
|
|
|
|
|
|
auto itr = std::find_if(displays.begin(), displays.end(),
|
|
|
|
[&](const Display& display) { return display.name == name; });
|
|
|
|
|
|
|
|
ASSERT(itr != displays.end());
|
|
|
|
|
|
|
|
return itr->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 NVFlinger::CreateLayer(u64 display_id) {
|
|
|
|
auto& display = GetDisplay(display_id);
|
|
|
|
|
|
|
|
ASSERT_MSG(display.layers.empty(), "Only one layer is supported per display at the moment");
|
|
|
|
|
|
|
|
u64 layer_id = next_layer_id++;
|
|
|
|
u32 buffer_queue_id = next_buffer_queue_id++;
|
|
|
|
auto buffer_queue = std::make_shared<BufferQueue>(buffer_queue_id, layer_id);
|
|
|
|
display.layers.emplace_back(layer_id, buffer_queue);
|
|
|
|
buffer_queues.emplace_back(std::move(buffer_queue));
|
|
|
|
return layer_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 NVFlinger::GetBufferQueueId(u64 display_id, u64 layer_id) {
|
|
|
|
const auto& layer = GetLayer(display_id, layer_id);
|
|
|
|
return layer.buffer_queue->GetId();
|
|
|
|
}
|
|
|
|
|
|
|
|
Kernel::SharedPtr<Kernel::Event> NVFlinger::GetVsyncEvent(u64 display_id) {
|
|
|
|
const auto& display = GetDisplay(display_id);
|
|
|
|
return display.vsync_event;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<BufferQueue> NVFlinger::GetBufferQueue(u32 id) const {
|
|
|
|
auto itr = std::find_if(buffer_queues.begin(), buffer_queues.end(),
|
|
|
|
[&](const auto& queue) { return queue->GetId() == id; });
|
|
|
|
|
|
|
|
ASSERT(itr != buffer_queues.end());
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Display& NVFlinger::GetDisplay(u64 display_id) {
|
|
|
|
auto itr = std::find_if(displays.begin(), displays.end(),
|
|
|
|
[&](const Display& display) { return display.id == display_id; });
|
|
|
|
|
|
|
|
ASSERT(itr != displays.end());
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer& NVFlinger::GetLayer(u64 display_id, u64 layer_id) {
|
|
|
|
auto& display = GetDisplay(display_id);
|
|
|
|
|
|
|
|
auto itr = std::find_if(display.layers.begin(), display.layers.end(),
|
|
|
|
[&](const Layer& layer) { return layer.id == layer_id; });
|
|
|
|
|
|
|
|
ASSERT(itr != display.layers.end());
|
|
|
|
return *itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NVFlinger::Compose() {
|
|
|
|
for (auto& display : displays) {
|
|
|
|
// Trigger vsync for this display at the end of drawing
|
|
|
|
SCOPE_EXIT({ display.vsync_event->Signal(); });
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
|
|
|
|
Layer& layer = display.layers[0];
|
|
|
|
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-01-22 16:54:58 +00:00
|
|
|
if (buffer == boost::none) {
|
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-03 16:55:58 +00:00
|
|
|
system_instance.perf_stats.EndGameFrame();
|
|
|
|
system_instance.Renderer().SwapBuffers({});
|
2018-01-22 16:54:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& igbp_buffer = buffer->igbp_buffer;
|
|
|
|
|
|
|
|
// 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-07-18 00:11:41 +00:00
|
|
|
igbp_buffer.width, igbp_buffer.height, igbp_buffer.stride, buffer->transform,
|
|
|
|
buffer->crop_rect);
|
2018-01-22 16:54:58 +00:00
|
|
|
|
|
|
|
buffer_queue->ReleaseBuffer(buffer->slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer::Layer(u64 id, std::shared_ptr<BufferQueue> queue) : id(id), buffer_queue(std::move(queue)) {}
|
|
|
|
|
|
|
|
Display::Display(u64 id, std::string name) : id(id), name(std::move(name)) {
|
2018-08-28 16:30:33 +00:00
|
|
|
auto& kernel = Core::System::GetInstance().Kernel();
|
|
|
|
vsync_event = Kernel::Event::Create(kernel, Kernel::ResetType::Pulse, "Display VSync Event");
|
2018-01-22 16:54:58 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 01:41:44 +00:00
|
|
|
} // namespace Service::NVFlinger
|