From b349efaa0a42f31a19be4cb418659085f25c8d9e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 28 Dec 2018 02:43:00 +0900 Subject: [PATCH] graphicsdriver/metal: Make main-thread usages more explicit --- internal/graphicsdriver/metal/driver.go | 25 +++++++++++++++++++------ internal/ui/ui_glfw.go | 3 +-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/internal/graphicsdriver/metal/driver.go b/internal/graphicsdriver/metal/driver.go index 5b1e53256..5fee667a0 100644 --- a/internal/graphicsdriver/metal/driver.go +++ b/internal/graphicsdriver/metal/driver.go @@ -243,9 +243,12 @@ func Get() *Driver { } func (d *Driver) SetWindow(window uintptr) { - // Note that [NSApp mainWindow] returns nil when the window is borderless. - // Then the window is needed to be given. - d.window = window + 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() { - d.src = nil + 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() { - i.driver.dst = i + mainthread.Run(func() error { + i.driver.dst = i + return nil + }) } func (i *Image) SetAsSource() { - i.driver.src = i + mainthread.Run(func() error { + i.driver.src = i + return nil + }) } func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) { diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index f8669b1e8..0fe93c1ae 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -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) }