Skip to content

Commit

Permalink
Merge pull request #17863 from hrydgard/merge-update-render
Browse files Browse the repository at this point in the history
Merge NativeUpdate and NativeRender, we always call them together.
  • Loading branch information
hrydgard committed Aug 8, 2023
2 parents 8e74907 + bcae36d commit 527e16c
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 60 deletions.
17 changes: 6 additions & 11 deletions Common/System/NativeApp.h
Expand Up @@ -49,21 +49,16 @@ void NativeSetRestarting();
// Retrieve current restarting flag.
bool NativeIsRestarting();

// Called ~sixty times a second, delivers the current input state.
// Main thread.
void NativeUpdate();

// Delivers touch events "instantly", without waiting for the next frame so that NativeUpdate can deliver.
// Useful for triggering audio events, saving a few ms.
// If you don't care about touch latency, just do a no-op implementation of this.
// time is not yet implemented. finger can be from 0 to 7, inclusive.
// Delivers touch/key/axis events "instantly", without waiting for the next frame so that NativeFrame can deliver.
// Some systems like UI will buffer these events internally but at least in gameplay we can get the minimum possible
// input latency - assuming your main loop is architected properly (NativeFrame called from a different thread than input event handling).
void NativeTouch(const TouchInput &touch);
bool NativeKey(const KeyInput &key);
void NativeAxis(const AxisInput &axis);

// Called when it's time to render. If the device can keep up, this
// will also be called sixty times per second. Main thread.
void NativeRender(GraphicsContext *graphicsContext);
// Called when it's process a frame, including rendering. If the device can keep up, this
// will be called sixty times per second. Main thread.
void NativeFrame(GraphicsContext *graphicsContext);

// This should render num_samples 44khz stereo samples.
// Try not to make too many assumptions on the granularity
Expand Down
2 changes: 1 addition & 1 deletion Common/System/Request.h
Expand Up @@ -29,7 +29,7 @@ class RequestManager {
void PostSystemSuccess(int requestId, const char *responseString, int responseValue = 0);
void PostSystemFailure(int requestId);

// This must be called every frame from the beginning of NativeUpdate().
// This must be called every frame from the beginning of NativeFrame().
// This will call the callback of any finished requests.
void ProcessRequests();

Expand Down
5 changes: 2 additions & 3 deletions Core/Core.cpp
Expand Up @@ -210,8 +210,7 @@ void UpdateRunLoop() {
sleep_ms(16);
return;
}
NativeUpdate();
NativeRender(graphicsContext);
NativeFrame(graphicsContext);
}

void KeepScreenAwake() {
Expand Down Expand Up @@ -325,7 +324,7 @@ void Core_ProcessStepping() {
}

// Many platforms, like Android, do not call this function but handle things on their own.
// Instead they simply call NativeRender and NativeUpdate directly.
// Instead they simply call NativeFrame directly.
bool Core_Run(GraphicsContext *ctx) {
System_Notify(SystemNotification::DISASSEMBLY);
while (true) {
Expand Down
66 changes: 33 additions & 33 deletions UI/NativeApp.cpp
Expand Up @@ -151,6 +151,8 @@

#include <Core/HLE/Plugins.h>

void HandleGlobalMessage(const std::string &msg, const std::string &value);

ScreenManager *g_screenManager;
std::string config_filename;

Expand Down Expand Up @@ -1047,14 +1049,43 @@ void RenderOverlays(UIContext *dc, void *userdata) {
}
}

void NativeRender(GraphicsContext *graphicsContext) {
void NativeFrame(GraphicsContext *graphicsContext) {
PROFILE_END_FRAME();

std::vector<PendingMessage> toProcess;
{
std::lock_guard<std::mutex> lock(pendingMutex);
toProcess = std::move(pendingMessages);
pendingMessages.clear();
}

for (const auto &item : toProcess) {
HandleGlobalMessage(item.msg, item.value);
g_screenManager->sendMessage(item.msg.c_str(), item.value.c_str());
}

g_requestManager.ProcessRequests();

// it's ok to call this redundantly with DoFrame from EmuScreen
Achievements::Idle();

g_DownloadManager.Update();
g_screenManager->update();

g_Discord.Update();
g_BackgroundAudio.Play();

g_OSD.Update();

UI::SetSoundEnabled(g_Config.bUISound);

_dbg_assert_(graphicsContext != nullptr);
_dbg_assert_(g_screenManager != nullptr);

g_GameManager.Update();

if (GetUIState() != UISTATE_INGAME) {
// Note: We do this from NativeRender so that the graphics context is
// Note: We do this from NativeFrame so that the graphics context is
// guaranteed valid, to be safe - g_gameInfoCache messes around with textures.
g_BackgroundAudio.Update();
}
Expand Down Expand Up @@ -1206,37 +1237,6 @@ void HandleGlobalMessage(const std::string &msg, const std::string &value) {
}
}

void NativeUpdate() {
PROFILE_END_FRAME();

std::vector<PendingMessage> toProcess;
{
std::lock_guard<std::mutex> lock(pendingMutex);
toProcess = std::move(pendingMessages);
pendingMessages.clear();
}

for (const auto &item : toProcess) {
HandleGlobalMessage(item.msg, item.value);
g_screenManager->sendMessage(item.msg.c_str(), item.value.c_str());
}

g_requestManager.ProcessRequests();

// it's ok to call this redundantly with DoFrame from EmuScreen
Achievements::Idle();

g_DownloadManager.Update();
g_screenManager->update();

g_Discord.Update();
g_BackgroundAudio.Play();

g_OSD.Update();

UI::SetSoundEnabled(g_Config.bUISound);
}

bool NativeIsAtTopLevel() {
// This might need some synchronization?
if (!g_screenManager) {
Expand Down
3 changes: 1 addition & 2 deletions UWP/PPSSPP_UWPMain.cpp
Expand Up @@ -165,7 +165,6 @@ void PPSSPP_UWPMain::CreateWindowSizeDependentResources() {
// Returns true if the frame was rendered and is ready to be displayed.
bool PPSSPP_UWPMain::Render() {
ctx_->GetDrawContext()->HandleEvent(Draw::Event::PRESENTED, 0, 0, nullptr, nullptr);
NativeUpdate();

static bool hasSetThreadName = false;
if (!hasSetThreadName) {
Expand Down Expand Up @@ -212,7 +211,7 @@ bool PPSSPP_UWPMain::Render() {

context->RSSetViewports(1, &viewport);

NativeRender(ctx_.get());
NativeFrame(ctx_.get());
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions android/jni/app-android.cpp
Expand Up @@ -1121,8 +1121,7 @@ extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendRequestResult(JNIEn

void LockedNativeUpdateRender() {
std::lock_guard<std::mutex> renderGuard(renderLock);
NativeUpdate();
NativeRender(graphicsContext);
NativeFrame(graphicsContext);
}

void UpdateRunLoopAndroid(JNIEnv *env) {
Expand Down
3 changes: 1 addition & 2 deletions headless/Headless.cpp
Expand Up @@ -94,8 +94,7 @@ class PrintfLogger : public LogListener {
};

// Temporary hacks around annoying linking errors.
void NativeUpdate() { }
void NativeRender(GraphicsContext *graphicsContext) { }
void NativeFrame(GraphicsContext *graphicsContext) { }
void NativeResized() { }

std::string System_GetProperty(SystemProperty prop) { return ""; }
Expand Down
3 changes: 1 addition & 2 deletions ios/ViewController.mm
Expand Up @@ -238,8 +238,7 @@ - (void)viewDidLoad {

INFO_LOG(SYSTEM, "Emulation thread starting\n");
while (threadEnabled) {
NativeUpdate();
NativeRender(graphicsContext);
NativeFrame(graphicsContext);
}


Expand Down
3 changes: 1 addition & 2 deletions libretro/libretro.cpp
Expand Up @@ -1704,8 +1704,7 @@ void System_Notify(SystemNotification notification) {
}
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3) { return false; }
void System_PostUIMessage(const std::string &message, const std::string &param) {}
void NativeUpdate() {}
void NativeRender(GraphicsContext *graphicsContext) {}
void NativeFrame(GraphicsContext *graphicsContext) {}
void NativeResized() {}

void System_Toast(const char *str) {}
Expand Down
3 changes: 1 addition & 2 deletions unittest/JitHarness.cpp
Expand Up @@ -37,8 +37,7 @@
#include "Core/HLE/HLE.h"

// Temporary hacks around annoying linking errors. Copied from Headless.
void NativeUpdate() { }
void NativeRender(GraphicsContext *graphicsContext) { }
void NativeFrame(GraphicsContext *graphicsContext) { }
void NativeResized() { }

bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2, int param3) { return false; }
Expand Down

0 comments on commit 527e16c

Please sign in to comment.