graphicsdriver/metal: Make main-thread usages more explicit

This commit is contained in:
Hajime Hoshi 2018-12-28 02:43:00 +09:00
parent 86bda42417
commit b349efaa0a
2 changed files with 20 additions and 8 deletions

View File

@ -243,9 +243,12 @@ func Get() *Driver {
}
func (d *Driver) SetWindow(window uintptr) {
mainthread.Run(func() error {
// Note that [NSApp mainWindow] returns nil when the window is borderless.
// Then the window is needed to be given.
d.window = window
return nil
})
}
func (d *Driver) SetVertices(vertices []float32, indices []uint16) {
@ -565,7 +568,10 @@ func (d *Driver) Draw(indexLen int, indexOffset int, mode graphics.CompositeMode
}
func (d *Driver) ResetSource() {
mainthread.Run(func() error {
d.src = nil
return nil
})
}
func (d *Driver) SetVsyncEnabled(enabled bool) {
@ -591,6 +597,7 @@ type Image struct {
texture mtl.Texture
}
// viewportSize must be called from the main thread.
func (i *Image) viewportSize() (int, int) {
if i.screen {
return i.width, i.height
@ -643,11 +650,17 @@ func (i *Image) Pixels() ([]byte, error) {
}
func (i *Image) SetAsDestination() {
mainthread.Run(func() error {
i.driver.dst = i
return nil
})
}
func (i *Image) SetAsSource() {
mainthread.Run(func() error {
i.driver.src = i
return nil
})
}
func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {

View File

@ -594,10 +594,9 @@ func Run(width, height int, scale float64, title string, g GraphicsContext, main
y := my + (v.Height-h)/3
x, y = adjustWindowPosition(x, y)
u.window.SetPos(x, y)
u.setWindowToDriver()
return nil
})
u.setWindowToDriver()
return u.loop(g)
}