Skip to content

Commit da782cb

Browse files
committed
Add VSync function for iOS/OpenGL
1 parent f634252 commit da782cb

File tree

7 files changed

+21
-20
lines changed

7 files changed

+21
-20
lines changed

Common/TimeUtil.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ double from_time_raw_relative(uint64_t raw_time) {
152152
return (double)raw_time * g_machTimeConversion;
153153
}
154154

155+
double from_mach_time_interval(double interval) {
156+
return interval - g_startTime * g_machTimeConversion;
157+
}
158+
155159
double time_now_unix_utc() {
156160
struct timespec tp;
157161
clock_gettime(CLOCK_REALTIME, &tp);

Common/TimeUtil.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
void TimeInit();
77

8-
// Seconds.
8+
// Seconds from app start.
99
double time_now_d();
1010

1111
// Raw time in nanoseconds.
@@ -15,6 +15,7 @@ uint64_t time_now_raw();
1515
// This is only interesting for Linux, in relation to VK_GOOGLE_display_timing.
1616
double from_time_raw(uint64_t raw_time);
1717
double from_time_raw_relative(uint64_t raw_time);
18+
double from_mach_time_interval(double interval);
1819

1920
// Seconds, Unix UTC time
2021
double time_now_unix_utc();

UI/GamepadEmu.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,15 +816,15 @@ void InitPadLayout(TouchControlConfig *config, DeviceOrientation orientation, fl
816816

817817
if (orientation == DeviceOrientation::Portrait) {
818818
int start_key_X = halfW;
819-
int start_key_Y = yres - bottom_button_Y * scale;
819+
int start_key_Y = yres - bottom_button_Y * scale + 20;
820820
initTouchPos(&config->touchStartKey, start_key_X, start_key_Y);
821821

822822
int select_key_X = halfW;
823823
int select_key_Y = yres - bottom_button_Y * scale - 50;
824824
initTouchPos(&config->touchSelectKey, select_key_X, select_key_Y);
825825

826826
int fast_forward_key_X = halfW;
827-
int fast_forward_key_Y = yres - bottom_button_Y * scale - 100;
827+
int fast_forward_key_Y = yres - bottom_button_Y * scale - 140;
828828
initTouchPos(&config->touchFastForwardKey, fast_forward_key_X, fast_forward_key_Y);
829829
} else {
830830
int start_key_X = halfW + bottom_key_spacing * scale;

UI/NativeApp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,7 @@ void NativeResized() {
15301530
}
15311531

15321532
void NativeVSync(int64_t vsyncId, double frameTime, double expectedPresentationTime) {
1533+
INFO_LOG(Log::System, "NativeVSync called with id %lld, frameTime %f, expectedPresentationTime %f", (long long)vsyncId, frameTime, expectedPresentationTime);
15331534
// TODO: Make use of this.
15341535
}
15351536

ios/ViewController.mm

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ @interface PPSSPPViewControllerGL () {
104104
@property (nonatomic, strong) EAGLContext *glContext;
105105
@property (nonatomic, strong) GLKView *glView;
106106
@property (nonatomic, strong) CADisplayLink *displayLink;
107-
@property (nonatomic, assign) NSTimeInterval lastTimestamp;
108107

109108
@property (nonatomic, strong) EAGLContext* context;
110109

@@ -214,8 +213,6 @@ - (void)viewDidLoad {
214213
}
215214
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
216215

217-
self.lastTimestamp = 0;
218-
219216
[[DisplayManager shared] setupDisplayListener];
220217

221218
UIScreen* screen = [(AppDelegate*)[UIApplication sharedApplication].delegate screen];
@@ -275,29 +272,23 @@ - (void)dealloc {
275272
- (void)setPreferredFramesPerSecond:(NSInteger)preferredFramesPerSecond {
276273
_preferredFramesPerSecond = preferredFramesPerSecond;
277274
if (self.displayLink) {
278-
if (@available(iOS 10.0, *)) {
279-
self.displayLink.preferredFramesPerSecond = (NSInteger)preferredFramesPerSecond;
280-
} else {
281-
self.displayLink.frameInterval = MAX(1, (NSInteger)round(60.0 / preferredFramesPerSecond));
282-
}
275+
self.displayLink.preferredFramesPerSecond = (NSInteger)preferredFramesPerSecond;
283276
}
284277
}
285278

286279
- (void)displayLinkFired:(CADisplayLink *)dl {
287-
// compute delta time
280+
static uint64_t presentId = 0;
281+
presentId++;
282+
288283
NSTimeInterval timestamp = dl.timestamp;
289-
NSTimeInterval delta = 0;
290-
if (self.lastTimestamp > 0) {
291-
delta = timestamp - self.lastTimestamp;
292-
} else {
293-
delta = dl.duration; // fallback
294-
}
295-
self.lastTimestamp = timestamp;
284+
NSTimeInterval targetTimestamp = dl.targetTimestamp;
296285

297286
// Ensure context is current before drawing
298287
[EAGLContext setCurrentContext:self.glContext];
299288

300-
// Trigger GLKView draw
289+
NativeVSync(presentId, from_mach_time_interval(timestamp), from_mach_time_interval(targetTimestamp));
290+
291+
// Trigger GLKView draw, which in turn calls glkView:drawInRect.
301292
[self.glView display];
302293
}
303294

ios/ViewControllerMetal.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "Common/System/Request.h"
1919
#include "Common/GraphicsContext.h"
2020
#include "Common/Thread/ThreadUtil.h"
21+
#include "Common/TimeUtil.h"
2122

2223
#include "Core/Config.h"
2324
#include "Core/ConfigValues.h"

ios/main.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "Common/Profiler/Profiler.h"
3838
#include "Common/Thread/ThreadUtil.h"
3939
#include "Common/System/Display.h"
40+
#include "Common/TimeUtil.h"
4041
#include "Core/Config.h"
4142
#include "Common/Log.h"
4243
#include "Common/Log/LogManager.h"
@@ -648,6 +649,8 @@ int main(int argc, char *argv[]) {
648649
g_iosVersionMajor = 14;
649650
}
650651

652+
TimeInit();
653+
651654
g_logManager.EnableOutput(LogOutput::Stdio);
652655

653656
#if PPSSPP_PLATFORM(IOS_APP_STORE)

0 commit comments

Comments
 (0)