uidriver/glfw: Replace thread usages with mutex usages for performance

Updates #1358
This commit is contained in:
Hajime Hoshi 2020-11-18 19:33:20 +09:00
parent 14070ee153
commit 91b01ff83d

View File

@ -59,27 +59,25 @@ func (i *Input) CursorPosition() (x, y int) {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return 0, 0 return 0, 0
} }
var cx, cy int
_ = i.ui.t.Call(func() error { i.ui.m.RLock()
cx, cy = i.cursorX, i.cursorY defer i.ui.m.RUnlock()
return nil return i.cursorX, i.cursorY
})
return cx, cy
} }
func (i *Input) GamepadIDs() []driver.GamepadID { func (i *Input) GamepadIDs() []driver.GamepadID {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return nil return nil
} }
var r []driver.GamepadID var r []driver.GamepadID
_ = i.ui.t.Call(func() error { i.ui.m.RLock()
for id, g := range i.gamepads { defer i.ui.m.RUnlock()
if g.valid { for id, g := range i.gamepads {
r = append(r, driver.GamepadID(id)) if g.valid {
} r = append(r, driver.GamepadID(id))
} }
return nil }
})
return r return r
} }
@ -87,106 +85,94 @@ func (i *Input) GamepadSDLID(id driver.GamepadID) string {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return "" return ""
} }
var r string
_ = i.ui.t.Call(func() error { i.ui.m.RLock()
if len(i.gamepads) <= int(id) { defer i.ui.m.RUnlock()
return nil if len(i.gamepads) <= int(id) {
} return ""
r = i.gamepads[id].guid }
return nil return i.gamepads[id].guid
})
return r
} }
func (i *Input) GamepadName(id driver.GamepadID) string { func (i *Input) GamepadName(id driver.GamepadID) string {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return "" return ""
} }
var r string
_ = i.ui.t.Call(func() error { i.ui.m.RLock()
if len(i.gamepads) <= int(id) { defer i.ui.m.RUnlock()
return nil if len(i.gamepads) <= int(id) {
} return ""
r = i.gamepads[id].name }
return nil return i.gamepads[id].name
})
return r
} }
func (i *Input) GamepadAxisNum(id driver.GamepadID) int { func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return 0 return 0
} }
var r int
_ = i.ui.t.Call(func() error { i.ui.m.RLock()
if len(i.gamepads) <= int(id) { defer i.ui.m.RUnlock()
return nil if len(i.gamepads) <= int(id) {
} return 0
r = i.gamepads[id].axisNum }
return nil return i.gamepads[id].axisNum
})
return r
} }
func (i *Input) GamepadAxis(id driver.GamepadID, axis int) float64 { func (i *Input) GamepadAxis(id driver.GamepadID, axis int) float64 {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return 0 return 0
} }
var r float64
_ = i.ui.t.Call(func() error { i.ui.m.RLock()
if len(i.gamepads) <= int(id) { defer i.ui.m.RUnlock()
return nil if len(i.gamepads) <= int(id) {
} return 0
r = i.gamepads[id].axes[axis] }
return nil return i.gamepads[id].axes[axis]
})
return r
} }
func (i *Input) GamepadButtonNum(id driver.GamepadID) int { func (i *Input) GamepadButtonNum(id driver.GamepadID) int {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return 0 return 0
} }
var r int
_ = i.ui.t.Call(func() error { i.ui.m.RLock()
if len(i.gamepads) <= int(id) { defer i.ui.m.RUnlock()
return nil if len(i.gamepads) <= int(id) {
} return 0
r = i.gamepads[id].buttonNum }
return nil return i.gamepads[id].buttonNum
})
return r
} }
func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool { func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.GamepadButton) bool {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return false return false
} }
var r bool
_ = i.ui.t.Call(func() error { i.ui.m.RLock()
if len(i.gamepads) <= int(id) { defer i.ui.m.RUnlock()
return nil if len(i.gamepads) <= int(id) {
} return false
r = i.gamepads[id].buttonPressed[button] }
return nil return i.gamepads[id].buttonPressed[button]
})
return r
} }
func (i *Input) TouchIDs() []driver.TouchID { func (i *Input) TouchIDs() []driver.TouchID {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return nil return nil
} }
var ids []driver.TouchID var ids []driver.TouchID
_ = i.ui.t.Call(func() error { i.ui.m.RLock()
if len(i.touches) == 0 { defer i.ui.m.RUnlock()
return nil if len(i.touches) == 0 {
}
for id := range i.touches {
ids = append(ids, id)
}
return nil return nil
}) }
for id := range i.touches {
ids = append(ids, id)
}
return ids return ids
} }
@ -194,99 +180,83 @@ func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return 0, 0 return 0, 0
} }
var found bool
var p pos i.ui.m.RLock()
_ = i.ui.t.Call(func() error { defer i.ui.m.RUnlock()
for tid, pos := range i.touches { for tid, pos := range i.touches {
if id == tid { if id == tid {
p = pos return pos.X, pos.Y
found = true
break
}
} }
return nil
})
if !found {
return 0, 0
} }
return p.X, p.Y return 0, 0
} }
func (i *Input) RuneBuffer() []rune { func (i *Input) RuneBuffer() []rune {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return nil return nil
} }
var r []rune
_ = i.ui.t.Call(func() error { i.ui.m.RLock()
r = make([]rune, len(i.runeBuffer)) defer i.ui.m.RUnlock()
copy(r, i.runeBuffer) rs := make([]rune, len(i.runeBuffer))
return nil copy(rs, i.runeBuffer)
}) return rs
return r
} }
func (i *Input) resetForFrame() { func (i *Input) resetForFrame() {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return return
} }
_ = i.ui.t.Call(func() error {
i.runeBuffer = i.runeBuffer[:0] i.ui.m.Lock()
i.scrollX, i.scrollY = 0, 0 defer i.ui.m.Unlock()
return nil i.runeBuffer = i.runeBuffer[:0]
}) i.scrollX, i.scrollY = 0, 0
} }
func (i *Input) IsKeyPressed(key driver.Key) bool { func (i *Input) IsKeyPressed(key driver.Key) bool {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return false return false
} }
var r bool
_ = i.ui.t.Call(func() error { i.ui.m.Lock()
if i.keyPressed == nil { defer i.ui.m.Unlock()
i.keyPressed = map[glfw.Key]bool{} if i.keyPressed == nil {
} i.keyPressed = map[glfw.Key]bool{}
gk, ok := driverKeyToGLFWKey[key] }
if ok && i.keyPressed[gk] { gk, ok := driverKeyToGLFWKey[key]
r = true return ok && i.keyPressed[gk]
return nil
}
return nil
})
return r
} }
func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool { func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return false return false
} }
var r bool
_ = i.ui.t.Call(func() error { i.ui.m.Lock()
if i.mouseButtonPressed == nil { defer i.ui.m.Unlock()
i.mouseButtonPressed = map[glfw.MouseButton]bool{} if i.mouseButtonPressed == nil {
i.mouseButtonPressed = map[glfw.MouseButton]bool{}
}
for gb, b := range glfwMouseButtonToMouseButton {
if b != button {
continue
} }
for gb, b := range glfwMouseButtonToMouseButton { if i.mouseButtonPressed[gb] {
if b != button { return true
continue
}
if i.mouseButtonPressed[gb] {
r = true
return nil
}
} }
return nil }
}) return false
return r
} }
func (i *Input) Wheel() (xoff, yoff float64) { func (i *Input) Wheel() (xoff, yoff float64) {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return 0, 0 return 0, 0
} }
_ = i.ui.t.Call(func() error {
xoff, yoff = i.scrollX, i.scrollY i.ui.m.RLock()
return nil defer i.ui.m.RUnlock()
}) return i.scrollX, i.scrollY
return
} }
var glfwMouseButtonToMouseButton = map[glfw.MouseButton]driver.MouseButton{ var glfwMouseButtonToMouseButton = map[glfw.MouseButton]driver.MouseButton{
@ -295,28 +265,23 @@ var glfwMouseButtonToMouseButton = map[glfw.MouseButton]driver.MouseButton{
glfw.MouseButtonMiddle: driver.MouseButtonMiddle, glfw.MouseButtonMiddle: driver.MouseButtonMiddle,
} }
func (i *Input) appendRuneBuffer(char rune) {
// As this function is called from GLFW callbacks, the current thread is main.
if !unicode.IsPrint(char) {
return
}
i.runeBuffer = append(i.runeBuffer, char)
}
func (i *Input) setWheel(xoff, yoff float64) {
// As this function is called from GLFW callbacks, the current thread is main.
i.scrollX = xoff
i.scrollY = yoff
}
// update must be called from the main thread. // update must be called from the main thread.
func (i *Input) update(window *glfw.Window, context driver.UIContext) { func (i *Input) update(window *glfw.Window, context driver.UIContext) {
i.ui.m.Lock()
defer i.ui.m.Unlock()
i.onceCallback.Do(func() { i.onceCallback.Do(func() {
window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) { window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) {
i.appendRuneBuffer(char) // As this function is called from GLFW callbacks, the current thread is main.
if !unicode.IsPrint(char) {
return
}
i.runeBuffer = append(i.runeBuffer, char)
}) })
window.SetScrollCallback(func(w *glfw.Window, xoff float64, yoff float64) { window.SetScrollCallback(func(w *glfw.Window, xoff float64, yoff float64) {
i.setWheel(xoff, yoff) // As this function is called from GLFW callbacks, the current thread is main.
i.scrollX = xoff
i.scrollY = yoff
}) })
}) })
if i.keyPressed == nil { if i.keyPressed == nil {