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

This reverts commit 042b30a516.
This commit is contained in:
Hajime Hoshi 2018-12-28 02:18:38 +09:00
parent 7956e0ebbc
commit c76ce28065
3 changed files with 5 additions and 44 deletions

View File

@ -233,11 +233,10 @@ type Driver struct {
src *Image src *Image
dst *Image dst *Image
vsync bool
maxImageSize int maxImageSize int
} }
var theDriver = Driver{vsync: true} var theDriver Driver
func Get() *Driver { func Get() *Driver {
return &theDriver return &theDriver
@ -396,7 +395,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(d.vsync) d.ml.SetDisplaySyncEnabled(true)
replaces := map[string]string{ replaces := map[string]string{
"{{.FilterNearest}}": fmt.Sprintf("%d", graphics.FilterNearest), "{{.FilterNearest}}": fmt.Sprintf("%d", graphics.FilterNearest),
@ -571,11 +570,7 @@ 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,39 +14,16 @@
package mainthread package mainthread
import ( var funcs = make(chan func())
"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
@ -55,19 +32,7 @@ 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,6 +76,7 @@ var (
) )
func init() { func init() {
runtime.LockOSThread()
hideConsoleWindowOnWindows() hideConsoleWindowOnWindows()
if err := initialize(); err != nil { if err := initialize(); err != nil {
panic(err) panic(err)