ui: Unify the context thread and the UI thread

This commit is contained in:
Hajime Hoshi 2016-07-23 21:22:59 +09:00
parent 8371426888
commit 3553fc55c3
2 changed files with 11 additions and 38 deletions

View File

@ -67,31 +67,18 @@ func init() {
} }
type context struct { type context struct {
funcs chan func() init bool
init bool runOnMainThread func(func() error) error
} }
func NewContext() (*Context, error) { func NewContext(runOnMainThread func(func() error) error) (*Context, error) {
c := &Context{} c := &Context{}
c.funcs = make(chan func()) c.runOnMainThread = runOnMainThread
return c, nil return c, nil
} }
func (c *Context) Loop() {
for f := range c.funcs {
f()
}
}
func (c *Context) RunOnContextThread(f func() error) error { func (c *Context) RunOnContextThread(f func() error) error {
ch := make(chan struct{}) return c.runOnMainThread(f)
var err error
c.funcs <- func() {
err = f()
close(ch)
}
<-ch
return err
} }
func (c *Context) Reset() error { func (c *Context) Reset() error {

View File

@ -70,29 +70,18 @@ func initialize() error {
if err != nil { if err != nil {
return err return err
} }
u := &userInterface{ u := &userInterface{
window: window, window: window,
funcs: make(chan func()), funcs: make(chan func()),
sizeChanged: true, sizeChanged: true,
} }
ch := make(chan error) u.window.MakeContextCurrent()
go func() { glfw.SwapInterval(1)
runtime.LockOSThread() u.context, err = opengl.NewContext(u.runOnMainThread)
u.window.MakeContextCurrent() if err != nil {
glfw.SwapInterval(1)
var err error
u.context, err = opengl.NewContext()
if err != nil {
ch <- err
}
close(ch)
u.context.Loop()
}()
currentUI = u
if err := <-ch; err != nil {
return err return err
} }
currentUI = u
return nil return nil
} }
@ -258,10 +247,7 @@ func (u *userInterface) swapBuffers() error {
if err := u.context.BindScreenFramebuffer(); err != nil { if err := u.context.BindScreenFramebuffer(); err != nil {
return err return err
} }
u.context.RunOnContextThread(func() error { u.window.SwapBuffers()
u.window.SwapBuffers()
return nil
})
return nil return nil
} }