Use the more accurate sleep function in the UI throttling

This commit is contained in:
Henrik Rydgård 2024-05-12 22:18:59 +02:00
parent 7cc4a0f3a3
commit c29eb5097b
3 changed files with 28 additions and 9 deletions

View File

@ -256,6 +256,9 @@ double Instant::ElapsedSeconds() const {
#define SLEEP_LOG_ENABLED 0
void sleep_ms(int ms, const char *reason) {
if (ms <= 0) {
return;
}
#if SLEEP_LOG_ENABLED
INFO_LOG(Log::System, "Sleep %d ms: %s", ms, reason);
#endif
@ -271,6 +274,9 @@ void sleep_ms(int ms, const char *reason) {
}
void sleep_us(int us, const char *reason) {
if (us <= 0) {
return;
}
#if SLEEP_LOG_ENABLED
INFO_LOG(Log::System, "Sleep %d us: %s", us, reason);
#endif

View File

@ -817,8 +817,6 @@ public:
private:
std::string text_;
std::string rightText_;
bool choiceStyle_ = false;
};
class AbstractChoiceWithValueDisplay : public Choice {
@ -1094,7 +1092,7 @@ public:
private:
std::string text_;
ImageID atlasImage_;
ImageSizeMode sizeMode_;
ImageSizeMode sizeMode_; // TODO: Not actually used yet.
float scale_ = 1.0f;
};

View File

@ -1248,12 +1248,27 @@ void NativeFrame(GraphicsContext *graphicsContext) {
g_BackgroundAudio.Play();
float refreshRate = System_GetPropertyFloat(SYSPROP_DISPLAY_REFRESH_RATE);
// Simple throttling to not burn the GPU in the menu.
// TODO: This is only necessary in MAILBOX or IMMEDIATE presentation modes.
double diffTime = time_now_d() - startTime;
int sleepTimeUs = (int)(1000000 * ((1.0 / refreshRate) - diffTime));
if (sleepTimeUs > 0)
sleep_us(sleepTimeUs, "fallback-throttle");
static double lastTime = 0.0;
if (lastTime > 0.0) {
double now = time_now_d();
// Simple throttling to not burn the GPU in the menu.
// TODO: This should move into NativeFrame.
double diffTime = now - lastTime;
int sleepTimeUs = (int)(1000000 * ((1.0 / refreshRate) - diffTime));
// printf("sleep: %0.3f ms (diff: %0.3f) %f\n", (double)sleepTimeUs / 1000, diffTime * 1000.0, refreshRate);
// If presentation mode is FIFO, we don't need to sleep a lot, we'll be throttled by
// presentation. But still, let's sleep a bit.
// Actually, for some reason this increases latency a lot, to the degree that the UI
// gets hard to use.. Commenting out for now.
// if (g_frameTiming.PresentMode() == Draw::PresentMode::FIFO) {
// sleepTimeUs = std::min(2000, sleepTimeUs); // 2 ms
// }
if (sleepTimeUs > 0)
sleep_us(sleepTimeUs, "fallback-throttle");
}
lastTime = time_now_d();
}
}