ui: Sleep when vsync doesn't work (#692)

This commit is contained in:
Hajime Hoshi 2018-09-30 14:43:53 +09:00
parent 2923bec0dc
commit 25a5f1dd83
2 changed files with 24 additions and 0 deletions

View File

@ -625,12 +625,33 @@ func (u *userInterface) loop(g GraphicsContext) error {
if err := u.update(g); err != nil { if err := u.update(g); err != nil {
return err return err
} }
u.m.Lock()
vsync := u.vsync
u.m.Unlock()
// The bound framebuffer must be the original screen framebuffer // The bound framebuffer must be the original screen framebuffer
// before swapping buffers. // before swapping buffers.
opengl.GetContext().BindScreenFramebuffer() opengl.GetContext().BindScreenFramebuffer()
_ = u.runOnMainThread(func() error { _ = u.runOnMainThread(func() error {
if !vsync {
u.swapBuffers() u.swapBuffers()
return nil return nil
}
n1 := time.Now().UnixNano()
u.swapBuffers()
n2 := time.Now().UnixNano()
d := time.Duration(n2 - n1)
// On macOS Mojave, vsync might not work (#692).
// As a tempoarry fix, just wait for a millisecond not to consume CPU too much.
const threshold = 4 * time.Millisecond // 250 [Hz]
if d < threshold {
time.Sleep(threshold - d)
}
return nil
}) })
} }
} }

3
run.go
View File

@ -38,6 +38,9 @@ const FPS = DefaultTPS
// CurrentFPS returns the current number of FPS (frames per second), that represents // CurrentFPS returns the current number of FPS (frames per second), that represents
// how many swapping buffer happens per second. // how many swapping buffer happens per second.
// //
// On some environments, CurrentFPS doesn't return a reliable value since vsync doesn't work well there.
// If you want to measure the application's speed, Use CurrentTPS.
//
// CurrentFPS is concurrent-safe. // CurrentFPS is concurrent-safe.
func CurrentFPS() float64 { func CurrentFPS() float64 {
return clock.CurrentFPS() return clock.CurrentFPS()