Skip to content

Commit

Permalink
CDVD: Add precaching option
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Jun 11, 2024
1 parent e1596c7 commit 7ad27e6
Show file tree
Hide file tree
Showing 27 changed files with 566 additions and 60 deletions.
70 changes: 39 additions & 31 deletions common/ProgressCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,40 +101,48 @@ void ProgressCallback::DisplayFormattedModalInformation(const char* format, ...)
ModalInformation(str.c_str());
}

class NullProgressCallbacks final : public ProgressCallback
{
public:
void PushState() override {}
void PopState() override {}

bool IsCancelled() const override { return false; }
bool IsCancellable() const override { return false; }

void SetCancellable(bool cancellable) override {}
void SetTitle(const char* title) override {}
void SetStatusText(const char* statusText) override {}
void SetProgressRange(u32 range) override {}
void SetProgressValue(u32 value) override {}
void IncrementProgressValue() override {}
void SetProgressState(ProgressState state) override {}

void DisplayError(const char* message) override { Console.Error("%s", message); }
void DisplayWarning(const char* message) override { Console.Warning("%s", message); }
void DisplayInformation(const char* message) override { Console.WriteLn("%s", message); }
void DisplayDebugMessage(const char* message) override { DevCon.WriteLn("%s", message); }

void ModalError(const char* message) override { Console.Error(message); }
bool ModalConfirmation(const char* message) override
namespace
{
class NullProgressCallbacks final : public ProgressCallback
{
Console.WriteLn("%s", message);
return false;
}
void ModalInformation(const char* message) override { Console.WriteLn("%s", message); }
};
public:
void PushState() override {}
void PopState() override {}

bool IsCancelled() const override { return false; }
bool IsCancellable() const override { return false; }

void SetCancellable(bool cancellable) override {}
void SetTitle(const char* title) override {}
void SetStatusText(const char* statusText) override {}
void SetProgressRange(u32 range) override {}
void SetProgressValue(u32 value) override {}
void IncrementProgressValue() override {}
void SetProgressState(ProgressState state) override {}

void DisplayError(const char* message) override { Console.Error("%s", message); }
void DisplayWarning(const char* message) override { Console.Warning("%s", message); }
void DisplayInformation(const char* message) override { Console.WriteLn("%s", message); }
void DisplayDebugMessage(const char* message) override { DevCon.WriteLn("%s", message); }

void ModalError(const char* message) override { Console.Error(message); }
bool ModalConfirmation(const char* message) override
{
Console.WriteLn("%s", message);
return false;
}
void ModalInformation(const char* message) override { Console.WriteLn("%s", message); }
};
} // namespace

static NullProgressCallbacks s_nullProgressCallbacks;
ProgressCallback* ProgressCallback::NullProgressCallback = &s_nullProgressCallbacks;

std::unique_ptr<ProgressCallback> ProgressCallback::CreateNullProgressCallback()
{
return std::make_unique<NullProgressCallbacks>();
}

BaseProgressCallback::BaseProgressCallback()
{
}
Expand Down Expand Up @@ -171,8 +179,8 @@ void BaseProgressCallback::PopState()
// impose the current position into the previous range
const u32 new_progress_value =
(m_progress_range != 0) ?
static_cast<u32>(((float)m_progress_value / (float)m_progress_range) * (float)state->progress_range) :
state->progress_value;
static_cast<u32>(((float)m_progress_value / (float)m_progress_range) * (float)state->progress_range) :
state->progress_value;

m_cancellable = state->cancellable;
m_status_text = std::move(state->status_text);
Expand Down
4 changes: 4 additions & 0 deletions common/ProgressCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#pragma once
#include "Pcsx2Defs.h"

#include <memory>
#include <string>

/**
Expand Down Expand Up @@ -59,6 +61,8 @@ class ProgressCallback

public:
static ProgressCallback* NullProgressCallback;

static std::unique_ptr<ProgressCallback> CreateNullProgressCallback();
};

class BaseProgressCallback : public ProgressCallback
Expand Down
6 changes: 6 additions & 0 deletions pcsx2-gsrunner/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "common/FileSystem.h"
#include "common/MemorySettingsInterface.h"
#include "common/Path.h"
#include "common/ProgressCallback.h"
#include "common/SettingsWrapper.h"
#include "common/StringUtil.h"

Expand Down Expand Up @@ -165,6 +166,11 @@ void Host::SetDefaultUISettings(SettingsInterface& si)
// nothing
}

std::unique_ptr<ProgressCallback> Host::CreateHostProgressCallback()
{
return ProgressCallback::CreateNullProgressCallback();
}

void Host::ReportErrorAsync(const std::string_view title, const std::string_view message)
{
if (!title.empty() && !message.empty())
Expand Down
241 changes: 241 additions & 0 deletions pcsx2-qt/QtHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,247 @@ void Host::SetMouseMode(bool relative_mode, bool hide_cursor)
emit g_emu_thread->onMouseModeRequested(relative_mode, hide_cursor);
}

namespace {
class QtHostProgressCallback final : public BaseProgressCallback
{
public:
QtHostProgressCallback();
~QtHostProgressCallback() override;

__fi const std::string& GetName() const { return m_name; }

void PushState() override;
void PopState() override;

bool IsCancelled() const override;

void SetCancellable(bool cancellable) override;
void SetTitle(const char* title) override;
void SetStatusText(const char* text) override;
void SetProgressRange(u32 range) override;
void SetProgressValue(u32 value) override;

void DisplayError(const char* message) override;
void DisplayWarning(const char* message) override;
void DisplayInformation(const char* message) override;
void DisplayDebugMessage(const char* message) override;

void ModalError(const char* message) override;
bool ModalConfirmation(const char* message) override;
void ModalInformation(const char* message) override;

void SetCancelled();

private:
struct SharedData
{
QProgressDialog* dialog = nullptr;
QString init_title;
QString init_status_text;
std::atomic_bool cancelled{false};
bool cancellable = true;
bool was_fullscreen = false;
};

void EnsureHasData();
static void EnsureDialogVisible(const std::shared_ptr<SharedData>& data);
void Redraw(bool force);

std::string m_name;
std::shared_ptr<SharedData> m_data;
int m_last_progress_percent = -1;
};
}

QtHostProgressCallback::QtHostProgressCallback()
: BaseProgressCallback()
{
}

QtHostProgressCallback::~QtHostProgressCallback()
{
if (m_data)
{
QtHost::RunOnUIThread([data = m_data]() {
if (!data->dialog)
return;

data->dialog->close();
delete data->dialog;
if (data->was_fullscreen)
g_emu_thread->setFullscreen(true, false);
});
}
}

void QtHostProgressCallback::PushState()
{
BaseProgressCallback::PushState();
}

void QtHostProgressCallback::PopState()
{
BaseProgressCallback::PopState();
Redraw(true);
}

void QtHostProgressCallback::SetCancellable(bool cancellable)
{
BaseProgressCallback::SetCancellable(cancellable);
EnsureHasData();
m_data->cancellable = cancellable;
}

void QtHostProgressCallback::SetTitle(const char* title)
{
EnsureHasData();
QtHost::RunOnUIThread([data = m_data, title = QString::fromUtf8(title)]() {
if (data->dialog)
data->dialog->setWindowTitle(title);
else
data->init_title = title;
});
}

void QtHostProgressCallback::SetStatusText(const char* text)
{
BaseProgressCallback::SetStatusText(text);

EnsureHasData();
QtHost::RunOnUIThread([data = m_data, text = QString::fromUtf8(text)]() {
if (data->dialog)
data->dialog->setLabelText(text);
else
data->init_status_text = text;
});
}

void QtHostProgressCallback::SetProgressRange(u32 range)
{
u32 last_range = m_progress_range;

BaseProgressCallback::SetProgressRange(range);

if (m_progress_range != last_range)
Redraw(false);
}

void QtHostProgressCallback::SetProgressValue(u32 value)
{
u32 lastValue = m_progress_value;

BaseProgressCallback::SetProgressValue(value);

if (m_progress_value != lastValue)
Redraw(false);
}

void QtHostProgressCallback::Redraw(bool force)
{
const int percent = static_cast<int>((static_cast<float>(m_progress_value) / static_cast<float>(m_progress_range)) * 100.0f);
if (percent == m_last_progress_percent && !force)
return;

// If this is the emu uthread, we need to process the un-fullscreen message.
if (g_emu_thread->isOnEmuThread())
Host::PumpMessagesOnCPUThread();

m_last_progress_percent = percent;
EnsureHasData();
QtHost::RunOnUIThread([data = m_data, percent]() {
EnsureDialogVisible(data);
data->dialog->setValue(percent);
});
}

void QtHostProgressCallback::DisplayError(const char* message)
{
Console.Error(message);
Host::ReportErrorAsync("Error", message);
}

void QtHostProgressCallback::DisplayWarning(const char* message)
{
Console.Warning(message);
}

void QtHostProgressCallback::DisplayInformation(const char* message)
{
Console.WriteLn(message);
}

void QtHostProgressCallback::DisplayDebugMessage(const char* message)
{
DevCon.WriteLn(message);
}

void QtHostProgressCallback::ModalError(const char* message)
{
Console.Error(message);
Host::ReportErrorAsync("Error", message);
}

bool QtHostProgressCallback::ModalConfirmation(const char* message)
{
return false;
}

void QtHostProgressCallback::ModalInformation(const char* message)
{
Console.WriteLn(message);
}

void QtHostProgressCallback::SetCancelled()
{
// not done here
}

bool QtHostProgressCallback::IsCancelled() const
{
return m_data && m_data->cancelled.load(std::memory_order_acquire);
}

void QtHostProgressCallback::EnsureHasData()
{
if (!m_data)
m_data = std::make_shared<SharedData>();
}

void QtHostProgressCallback::EnsureDialogVisible(const std::shared_ptr<SharedData>& data)
{
pxAssert(data);
if (data->dialog)
return;

data->was_fullscreen = g_emu_thread->isFullscreen();
if (data->was_fullscreen)
g_emu_thread->setFullscreen(false, true);

data->dialog = new QProgressDialog(data->init_status_text,
data->cancellable ? qApp->translate("QtHost", "Cancel") : QString(),
0, 100, g_main_window);
if (data->cancellable)
{
data->dialog->connect(data->dialog, &QProgressDialog::canceled,
[data]() { data->cancelled.store(true, std::memory_order_release); });
}
data->dialog->setWindowIcon(QtHost::GetAppIcon());
data->dialog->setMinimumWidth(400);
data->dialog->show();
data->dialog->raise();
data->dialog->activateWindow();
if (!data->init_title.isEmpty())
{
data->dialog->setWindowTitle(data->init_title);
data->init_title = QString();
}
}

std::unique_ptr<ProgressCallback> Host::CreateHostProgressCallback()
{
return std::make_unique<QtHostProgressCallback>();
}

//////////////////////////////////////////////////////////////////////////
// Hotkeys
//////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion pcsx2-qt/QtHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Q_SLOTS:
void endCapture();
void setAudioOutputVolume(int volume, int fast_forward_volume);
void setAudioOutputMuted(bool muted);

Q_SIGNALS:
bool messageConfirmed(const QString& title, const QString& message);
void statusMessage(const QString& message);
Expand Down
Loading

0 comments on commit 7ad27e6

Please sign in to comment.