uidriver/mobile: Reduce mutex to avoid potential deadlock

This commit is contained in:
Hajime Hoshi 2019-08-19 00:30:23 +09:00
parent 6cd74a9f4d
commit 25ac788ee6
2 changed files with 8 additions and 13 deletions

View File

@ -17,8 +17,6 @@
package mobile package mobile
import ( import (
"sync"
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
) )
@ -32,12 +30,11 @@ type Input struct {
cursorY int cursorY int
touches map[int]pos touches map[int]pos
ui *UserInterface ui *UserInterface
m sync.RWMutex
} }
func (i *Input) CursorPosition() (x, y int) { func (i *Input) CursorPosition() (x, y int) {
i.m.RLock() i.ui.m.RLock()
defer i.m.RUnlock() defer i.ui.m.RUnlock()
return i.ui.adjustPosition(i.cursorX, i.cursorY) return i.ui.adjustPosition(i.cursorX, i.cursorY)
} }
@ -62,8 +59,8 @@ func (i *Input) IsGamepadButtonPressed(id int, button driver.GamepadButton) bool
} }
func (i *Input) TouchIDs() []int { func (i *Input) TouchIDs() []int {
i.m.RLock() i.ui.m.RLock()
defer i.m.RUnlock() defer i.ui.m.RUnlock()
if len(i.touches) == 0 { if len(i.touches) == 0 {
return nil return nil
@ -77,8 +74,8 @@ func (i *Input) TouchIDs() []int {
} }
func (i *Input) TouchPosition(id int) (x, y int) { func (i *Input) TouchPosition(id int) (x, y int) {
i.m.RLock() i.ui.m.RLock()
defer i.m.RUnlock() defer i.ui.m.RUnlock()
for tid, pos := range i.touches { for tid, pos := range i.touches {
if id == tid { if id == tid {
@ -105,7 +102,7 @@ func (i *Input) IsMouseButtonPressed(key driver.MouseButton) bool {
} }
func (i *Input) update(touches []*Touch) { func (i *Input) update(touches []*Touch) {
i.m.Lock() i.ui.m.Lock()
i.touches = map[int]pos{} i.touches = map[int]pos{}
for _, t := range touches { for _, t := range touches {
i.touches[t.ID] = pos{ i.touches[t.ID] = pos{
@ -113,7 +110,7 @@ func (i *Input) update(touches []*Touch) {
Y: t.Y, Y: t.Y,
} }
} }
i.m.Unlock() i.ui.m.Unlock()
} }
func (i *Input) ResetForFrame() { func (i *Input) ResetForFrame() {

View File

@ -376,11 +376,9 @@ func (u *UserInterface) screenPaddingImpl() (x0, y0, x1, y1 float64) {
} }
func (u *UserInterface) adjustPosition(x, y int) (int, int) { func (u *UserInterface) adjustPosition(x, y int) (int, int) {
u.m.Lock()
ox, oy, _, _ := u.screenPaddingImpl() ox, oy, _, _ := u.screenPaddingImpl()
s := u.scaleImpl() s := u.scaleImpl()
as := s * getDeviceScale() as := s * getDeviceScale()
u.m.Unlock()
return int(float64(x)/s - ox/as), int(float64(y)/s - oy/as) return int(float64(x)/s - ox/as), int(float64(y)/s - oy/as)
} }