Skip to content

desktop-ui: Threading rework #1906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ares/ares/node/video/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Screen::~Screen() {
}

auto Screen::main(uintptr_t) -> void {
thread::setName("dev.ares.screen");
while(!_kill) {
unique_lock<mutex> lock(_frameMutex);

Expand Down
9 changes: 6 additions & 3 deletions ares/n64/system/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ auto System::game() -> string {
}

auto System::run() -> void {
// not ideal
if(!vulkanLoaded) {
vulkan.load(node);
vulkanLoaded = true;
}
cpu.main();
}

Expand Down Expand Up @@ -134,9 +139,6 @@ auto System::load(Node::System& root, string name) -> bool {
rdp.load(node);
if(_DD()) dd.load(node);
if(model() == Model::Aleck64) aleck64.load(node);
#if defined(VULKAN)
vulkan.load(node);
#endif

initDebugHooks();

Expand Down Expand Up @@ -308,6 +310,7 @@ auto System::unload() -> void {
if(vi.screen) vi.screen->quit(); //stop video thread
#if defined(VULKAN)
vulkan.unload();
vulkanLoaded = false;
#endif
cartridgeSlot.unload();
controllerPort1.unload();
Expand Down
2 changes: 2 additions & 0 deletions ares/n64/system/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct System {
u32 videoFrequency = 48'681'818;
bool dd = false;
} information;

bool vulkanLoaded = false;

auto initDebugHooks() -> void;

Expand Down
2 changes: 1 addition & 1 deletion ares/n64/vi/vi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ auto VI::main() -> void {
io.vcounter = 0;
if(++inactiveCounter >= 200) {
inactiveCounter = 0;
screen->frame();
//screen->frame();
refreshed = true;
}
step(0x800);
Expand Down
2 changes: 1 addition & 1 deletion desktop-ui/desktop-ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ auto nall::main(Arguments arguments) -> void {

Instances::presentation.construct();
Instances::settingsWindow.construct();
Instances::toolsWindow.construct();
Instances::gameBrowserWindow.construct();

program.create();
Instances::toolsWindow.construct(&program.programMutex);
Application::onMain({&Program::main, &program});
Application::run();

Expand Down
12 changes: 12 additions & 0 deletions desktop-ui/emulator/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ auto Emulator::handleLoadResult(LoadResult result) -> void {
}

auto Emulator::load(const string& location) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(inode::exists(location)) locationQueue.append(location);

LoadResult result = load();
Expand All @@ -130,6 +131,7 @@ auto Emulator::load(const string& location) -> bool {
}

auto Emulator::load(shared_pointer<mia::Pak> pak, string& path) -> string {
lock_guard<recursive_mutex> lock(program.programMutex);
string location;
if(locationQueue) {
location = locationQueue.takeFirst(); //pull from the game queue if an entry is available
Expand Down Expand Up @@ -160,6 +162,7 @@ auto Emulator::load(shared_pointer<mia::Pak> pak, string& path) -> string {
}

auto Emulator::loadFirmware(const Firmware& firmware) -> shared_pointer<vfs::file> {
lock_guard<recursive_mutex> lock(program.programMutex);
if(firmware.location.iendsWith(".zip")) {
Decode::ZIP archive;
if(archive.open(firmware.location) && archive.file) {
Expand All @@ -173,6 +176,7 @@ auto Emulator::loadFirmware(const Firmware& firmware) -> shared_pointer<vfs::fil
}

auto Emulator::unload() -> void {
lock_guard<recursive_mutex> lock(program.programMutex);
save();
root->unload();
game = {};
Expand All @@ -182,6 +186,7 @@ auto Emulator::unload() -> void {
}

auto Emulator::load(mia::Pak& node, string name) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto fp = node.pak->read(name)) {
if(auto memory = file::read({node.location, name})) {
fp->read(memory);
Expand All @@ -192,19 +197,22 @@ auto Emulator::load(mia::Pak& node, string name) -> bool {
}

auto Emulator::save(mia::Pak& node, string name) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto memory = node.pak->write(name)) {
return file::write({node.location, name}, {memory->data(), memory->size()});
}
return false;
}

auto Emulator::refresh() -> void {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto screen = root->scan<ares::Node::Video::Screen>("Screen")) {
screen->refresh();
}
}

auto Emulator::setBoolean(const string& name, bool value) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto node = root->scan<ares::Node::Setting::Boolean>(name)) {
node->setValue(value); //setValue() will not call modify() if value has not changed;
node->modify(value); //but that may prevent the initial setValue() from working
Expand All @@ -214,6 +222,7 @@ auto Emulator::setBoolean(const string& name, bool value) -> bool {
}

auto Emulator::setOverscan(bool value) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto screen = root->scan<ares::Node::Video::Screen>("Screen")) {
screen->setOverscan(value);
return true;
Expand All @@ -222,6 +231,7 @@ auto Emulator::setOverscan(bool value) -> bool {
}

auto Emulator::setColorBleed(bool value) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto screen = root->scan<ares::Node::Video::Screen>("Screen")) {
screen->setColorBleed(screen->height() < 720 ? value : false); //only apply to sub-HD content
return true;
Expand All @@ -235,6 +245,7 @@ auto Emulator::error(const string& text) -> void {
}

auto Emulator::input(ares::Node::Input::Input input) -> void {
lock_guard<recursive_mutex> lock(program.inputMutex); //necessary?
//looking up inputs is very time-consuming; skip call if input was called too recently
//note: allow rumble to be polled at full speed to prevent missed motor events
if(!input->cast<ares::Node::Input::Rumble>()) {
Expand Down Expand Up @@ -281,6 +292,7 @@ auto Emulator::input(ares::Node::Input::Input input) -> void {
}

auto Emulator::inputKeyboard(string name) -> bool {
lock_guard<recursive_mutex> lock(program.inputMutex);
for (auto& device : inputManager.devices) {
if (!device->isKeyboard()) continue;

Expand Down
22 changes: 22 additions & 0 deletions desktop-ui/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ VirtualPort virtualPorts[5];
InputManager inputManager;

auto InputMapping::bind() -> void {
lock_guard<recursive_mutex> lock(program.inputMutex);
for(auto& binding : bindings) binding = {};

for(u32 index : range(BindingLimit)) {
Expand Down Expand Up @@ -32,22 +33,26 @@ auto InputMapping::bind() -> void {
}

auto InputMapping::bind(u32 binding, string assignment) -> void {
lock_guard<recursive_mutex> lock(program.inputMutex);
if(binding >= BindingLimit) return;
assignments[binding] = assignment;
bind();
}

auto InputMapping::unbind() -> void {
lock_guard<recursive_mutex> lock(program.inputMutex);
for(u32 binding : range(BindingLimit)) unbind(binding);
}

auto InputMapping::unbind(u32 binding) -> void {
lock_guard<recursive_mutex> lock(program.inputMutex);
if(binding >= BindingLimit) return;
bindings[binding] = {};
assignments[binding] = {};
}

auto InputMapping::Binding::icon() -> multiFactorImage {
lock_guard<recursive_mutex> lock(program.inputMutex);
if(!device && deviceID) return Icon::Device::Joypad;
if(!device) return {};
if(device->isKeyboard()) return Icon::Device::Keyboard;
Expand All @@ -57,6 +62,7 @@ auto InputMapping::Binding::icon() -> multiFactorImage {
}

auto InputMapping::Binding::text() -> string {
lock_guard<recursive_mutex> lock(program.inputMutex);
if(!device && deviceID) return "(disconnected)";
if(!device) return {};
if(groupID >= device->size()) return {};
Expand Down Expand Up @@ -90,6 +96,7 @@ auto InputMapping::Binding::text() -> string {
//

auto InputDigital::bind(u32 binding, shared_pointer<HID::Device> device, u32 groupID, u32 inputID, s16 oldValue, s16 newValue) -> bool {
lock_guard<recursive_mutex> lock(program.inputMutex);
string assignment = {"0x", hex(device->id()), "/", groupID, "/", inputID};

if(device->isNull()) {
Expand Down Expand Up @@ -128,6 +135,7 @@ auto InputDigital::bind(u32 binding, shared_pointer<HID::Device> device, u32 gro
}

auto InputDigital::value() -> s16 {
lock_guard<recursive_mutex> lock(program.inputMutex);
s16 result = 0;

for(auto& binding : bindings) {
Expand Down Expand Up @@ -165,11 +173,13 @@ auto InputDigital::value() -> s16 {
}

auto InputDigital::pressed() -> bool {
lock_guard<recursive_mutex> lock(program.inputMutex);
return value() != 0;
}


auto InputHotkey::value() -> s16 {
lock_guard<recursive_mutex> lock(program.inputMutex);
s16 result = 0;

for(auto& binding : bindings) {
Expand Down Expand Up @@ -210,6 +220,7 @@ auto InputHotkey::value() -> s16 {
//

auto InputAnalog::bind(u32 binding, shared_pointer<HID::Device> device, u32 groupID, u32 inputID, s16 oldValue, s16 newValue) -> bool {
lock_guard<recursive_mutex> lock(program.inputMutex);
string assignment = {"0x", hex(device->id()), "/", groupID, "/", inputID};

if(device->isNull()) {
Expand Down Expand Up @@ -244,6 +255,7 @@ auto InputAnalog::bind(u32 binding, shared_pointer<HID::Device> device, u32 grou
}

auto InputAnalog::value() -> s16 {
lock_guard<recursive_mutex> lock(program.inputMutex);
s32 result = 0;

for(auto& binding : bindings) {
Expand Down Expand Up @@ -274,12 +286,14 @@ auto InputAnalog::value() -> s16 {
}

auto InputAnalog::pressed() -> bool {
lock_guard<recursive_mutex> lock(program.inputMutex);
return value() > 16384;
}

//

auto InputAbsolute::bind(u32 binding, shared_pointer<HID::Device> device, u32 groupID, u32 inputID, s16 oldValue, s16 newValue) -> bool {
lock_guard<recursive_mutex> lock(program.inputMutex);
string assignment = {"0x", hex(device->id()), "/", groupID, "/", inputID};

if(device->isNull()) {
Expand Down Expand Up @@ -310,6 +324,7 @@ auto InputAbsolute::bind(u32 binding, shared_pointer<HID::Device> device, u32 gr
}

auto InputAbsolute::value() -> s16 {
lock_guard<recursive_mutex> lock(program.inputMutex);
s32 result = 0;

for(auto& binding : bindings) {
Expand Down Expand Up @@ -337,6 +352,7 @@ auto InputAbsolute::value() -> s16 {
//

auto InputRelative::bind(u32 binding, shared_pointer<HID::Device> device, u32 groupID, u32 inputID, s16 oldValue, s16 newValue) -> bool {
lock_guard<recursive_mutex> lock(program.inputMutex);
string assignment = {"0x", hex(device->id()), "/", groupID, "/", inputID};

if(device->isNull()) {
Expand Down Expand Up @@ -367,6 +383,7 @@ auto InputRelative::bind(u32 binding, shared_pointer<HID::Device> device, u32 gr
}

auto InputRelative::value() -> s16 {
lock_guard<recursive_mutex> lock(program.inputMutex);
s32 result = 0;

for(auto& binding : bindings) {
Expand Down Expand Up @@ -394,6 +411,7 @@ auto InputRelative::value() -> s16 {
//

auto InputRumble::bind(u32 binding, shared_pointer<HID::Device> device, u32 groupID, u32 inputID, s16 oldValue, s16 newValue) -> bool {
lock_guard<recursive_mutex> lock(program.inputMutex);
string assignment = {"0x", hex(device->id()), "/", groupID, "/", inputID};

if(device->isNull()) {
Expand Down Expand Up @@ -468,10 +486,12 @@ VirtualMouse::VirtualMouse() {
//

auto InputManager::create() -> void {
lock_guard<recursive_mutex> lock(program.inputMutex);
createHotkeys();
}

auto InputManager::bind() -> void {
lock_guard<recursive_mutex> lock(program.inputMutex);
for(auto& port : virtualPorts) {
for(auto& input : port.pad.inputs) input.mapping->bind();
for(auto& input : port.mouse.inputs) input.mapping->bind();
Expand All @@ -480,6 +500,7 @@ auto InputManager::bind() -> void {
}

auto InputManager::poll(bool force) -> void {
lock_guard<recursive_mutex> lock(program.inputMutex);
//polling actual hardware is very time-consuming; skip call if poll was called too recently
auto thisPoll = chrono::millisecond();
if(thisPoll - lastPoll < pollFrequency && !force) return;
Expand All @@ -502,6 +523,7 @@ auto InputManager::poll(bool force) -> void {
}

auto InputManager::eventInput(shared_pointer<HID::Device> device, u32 groupID, u32 inputID, s16 oldValue, s16 newValue) -> void {
lock_guard<recursive_mutex> lock(program.inputMutex);
inputSettings.eventInput(device, groupID, inputID, oldValue, newValue);
hotkeySettings.eventInput(device, groupID, inputID, oldValue, newValue);
}
Loading
Loading