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 {
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.funcs = make(chan func())
c.runOnMainThread = runOnMainThread
return c, nil
}
func (c *Context) Loop() {
for f := range c.funcs {
f()
}
}
func (c *Context) RunOnContextThread(f func() error) error {
ch := make(chan struct{})
var err error
c.funcs <- func() {
err = f()
close(ch)
}
<-ch
return err
return c.runOnMainThread(f)
}
func (c *Context) Reset() error {

View File

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