ui: Bug fix: SetVsyncEnabled before Run didn't work on macOS

This commit is contained in:
Hajime Hoshi 2018-12-28 02:02:47 +09:00
parent 7740656054
commit 042b30a516
3 changed files with 44 additions and 5 deletions

View File

@ -233,10 +233,11 @@ type Driver struct {
src *Image src *Image
dst *Image dst *Image
vsync bool
maxImageSize int maxImageSize int
} }
var theDriver Driver var theDriver = Driver{vsync: true}
func Get() *Driver { func Get() *Driver {
return &theDriver return &theDriver
@ -395,7 +396,7 @@ func (d *Driver) Reset() error {
// MTLPixelFormatBGRA10_XR_sRGB. // MTLPixelFormatBGRA10_XR_sRGB.
d.ml.SetPixelFormat(mtl.PixelFormatBGRA8UNorm) d.ml.SetPixelFormat(mtl.PixelFormatBGRA8UNorm)
d.ml.SetMaximumDrawableCount(3) d.ml.SetMaximumDrawableCount(3)
d.ml.SetDisplaySyncEnabled(true) d.ml.SetDisplaySyncEnabled(d.vsync)
replaces := map[string]string{ replaces := map[string]string{
"{{.FilterNearest}}": fmt.Sprintf("%d", graphics.FilterNearest), "{{.FilterNearest}}": fmt.Sprintf("%d", graphics.FilterNearest),
@ -570,7 +571,11 @@ func (d *Driver) ResetSource() {
} }
func (d *Driver) SetVsyncEnabled(enabled bool) { func (d *Driver) SetVsyncEnabled(enabled bool) {
mainthread.Run(func() error {
d.ml.SetDisplaySyncEnabled(enabled) d.ml.SetDisplaySyncEnabled(enabled)
d.vsync = enabled
return nil
})
} }
func (d *Driver) VDirection() graphicsdriver.VDirection { func (d *Driver) VDirection() graphicsdriver.VDirection {

View File

@ -14,16 +14,39 @@
package mainthread package mainthread
var funcs = make(chan func()) import (
"runtime"
"sync"
)
var (
funcs chan func()
running bool
m sync.Mutex
)
func init() {
runtime.LockOSThread()
}
// Loop starts the main-thread loop. // Loop starts the main-thread loop.
// //
// Loop must be called on the main thread. // Loop must be called on the main thread.
func Loop(ch <-chan error) error { func Loop(ch <-chan error) error {
m.Lock()
funcs = make(chan func())
m.Unlock()
for { for {
select { select {
case f := <-funcs: case f := <-funcs:
m.Lock()
running = true
m.Unlock()
f() f()
m.Lock()
running = false
m.Unlock()
case err := <-ch: case err := <-ch:
// ch returns a value not only when an error occur but also it is closed. // ch returns a value not only when an error occur but also it is closed.
return err return err
@ -32,7 +55,19 @@ func Loop(ch <-chan error) error {
} }
// Run calls f on the main thread. // Run calls f on the main thread.
//
// Run can be called even before Loop is called.
//
// Run can be called recursively: Run can be called from the function that are called via Run.
func Run(f func() error) error { func Run(f func() error) error {
// Even if funcs is nil, Run is called from the main thread (e.g. init)
m.Lock()
now := funcs == nil || running
m.Unlock()
if now {
return f()
}
ch := make(chan struct{}) ch := make(chan struct{})
var err error var err error
funcs <- func() { funcs <- func() {

View File

@ -76,7 +76,6 @@ var (
) )
func init() { func init() {
runtime.LockOSThread()
hideConsoleWindowOnWindows() hideConsoleWindowOnWindows()
if err := initialize(); err != nil { if err := initialize(); err != nil {
panic(err) panic(err)