Fix startup on Android, oops

Forgot that we can't create android-compatible threads in global
constructors, for JNI reasons.
This commit is contained in:
Henrik Rydgård 2025-03-27 01:57:43 +01:00
parent f73df3a4da
commit e5d00ce493
7 changed files with 35 additions and 19 deletions

View file

@ -30,10 +30,11 @@ AttachDetachFunc g_detach;
void AttachThreadToJNI() {
if (g_attach) {
g_attach();
} else {
ERROR_LOG(Log::System, "Couldn't attach thread - g_attach not set");
}
}
void DetachThreadFromJNI() {
if (g_detach) {
g_detach();

View file

@ -45,7 +45,7 @@ public:
void CheckForSas() override;
int EnqueueForSas(u32 address, u32 ptr) override;
void DecodeForSas(s16 *dstData, int *bytesWritten, int *finish) override;
const AtracSasStreamState *StreamStateForSas() const { return context_->info.state == 0x10 ? &sas_ : nullptr; }
const AtracSasStreamState *StreamStateForSas() const override { return context_->info.state == 0x10 ? &sas_ : nullptr; }
u32 GetNextSamples() override;

View file

@ -1352,7 +1352,7 @@ PPGeImage::PPGeImage(std::string_view pspFilename)
PPGeImage::PPGeImage(u32 pngPointer, size_t pngSize)
: filename_(""), png_(pngPointer), size_(pngSize) {
if (!Memory::IsValidRange(this->png_, (u32)this->size_)) {
WARN_LOG(Log::sceGe, "Created PPGeImage from invalid memory range %08x (%08x bytes). Will not be drawn.");
WARN_LOG(Log::sceGe, "Created PPGeImage from invalid memory range %08x (%08x bytes). Will not be drawn.", this->png_, (int)this->size_);
}
}
@ -1374,7 +1374,7 @@ bool PPGeImage::Load() {
_dbg_assert_(size_ < MAX_VALID_IMAGE_SIZE);
const u8 *srcPtr = Memory::GetPointerRange(png_, (u32)size_);
if (!srcPtr) {
ERROR_LOG(Log::sceGe, "Trying to load PPGeImage from invalid range: %08x, %08x bytes", png_, size_);
ERROR_LOG(Log::sceGe, "Trying to load PPGeImage from invalid range: %08x, %08x bytes", png_, (int)size_);
return false;
}
success = pngLoadPtr(srcPtr, size_, &width_, &height_, &textureData);

View file

@ -13,19 +13,31 @@
RecentFilesManager g_recentFiles;
RecentFilesManager::RecentFilesManager() {
thread_ = std::thread([this] {
ThreadFunc();
});
}
RecentFilesManager::RecentFilesManager() {}
RecentFilesManager::~RecentFilesManager() {
{
std::lock_guard<std::mutex> guard(cmdLock_);
cmds_.push(RecentCommand{ RecentCmd::Exit });
cmdCondVar_.notify_one();
if (thread_.joinable()) {
{
std::lock_guard<std::mutex> guard(cmdLock_);
cmds_.push(RecentCommand{ RecentCmd::Exit });
cmdCondVar_.notify_one();
}
thread_.join();
}
thread_.join();
}
void RecentFilesManager::EnsureThread() {
if (thread_.joinable()) {
return;
}
std::lock_guard<std::mutex> guard(cmdLock_);
thread_ = std::thread([this] {
// NOTE: Can't create the thread in the constructor, because at that point,
// JNI attachment doesn't yet work.
SetCurrentThreadName("RecentISOThreadFunc");
AndroidJNIThreadContext jniContext; // destructor detaches
ThreadFunc();
});
}
std::vector<std::string> RecentFilesManager::GetRecentFiles() const {
@ -137,9 +149,6 @@ void RecentFilesManager::Clean() {
}
void RecentFilesManager::ThreadFunc() {
SetCurrentThreadName("RecentISOs");
AndroidJNIThreadContext jniContext; // destructor detaches
while (true) {
RecentCommand cmd;
{

View file

@ -29,8 +29,11 @@ public:
void Clear();
bool ContainsFile(std::string_view filename);
void EnsureThread();
std::vector<std::string> GetRecentFiles() const;
private:
enum class RecentCmd {
Exit,
Clear,
@ -58,7 +61,6 @@ private:
std::thread thread_;
std::mutex cmdLock_;
std::condition_variable cmdCondVar_;
};
// Singleton, don't make more.

View file

@ -1035,7 +1035,8 @@ void DrawAudioDecodersView(ImConfig &cfg, ImControl &control) {
int endSample, loopStart, loopEnd;
ctx->GetSoundSample(&endSample, &loopStart, &loopEnd);
ImGui::ProgressBar((float)pos / (float)endSample, ImVec2(200.0f, 0.0f));
ImGui::Text("Status: %s", AtracStatusToString(ctx->BufferState())); ImGui::Text("cur/end sample: %d/%d/%d", pos, endSample);
ImGui::Text("Status: %s", AtracStatusToString(ctx->BufferState()));
ImGui::Text("cur/end sample: %d/%d", pos, endSample);
}
if (ctx->context_.IsValid()) {
ImGui::Text("ctx addr: ");

View file

@ -108,6 +108,7 @@
#include "Core/Util/GameManager.h"
#include "Core/Util/PortManager.h"
#include "Core/Util/AudioFormat.h"
#include "Core/Util/RecentFiles.h"
#include "Core/WebServer.h"
#include "Core/TiltEventProcessor.h"
@ -341,6 +342,8 @@ void NativeInit(int argc, const char *argv[], const char *savegame_dir, const ch
g_threadManager.Init(cpu_info.num_cores, cpu_info.logical_cpu_count);
g_recentFiles.EnsureThread();
// Make sure UI state is MENU.
ResetUIState();