Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process events in runloop only when needed #4399

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 3 additions & 6 deletions internal/driver/glfw/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type gLDriver struct {
windowLock sync.RWMutex
windows []fyne.Window
device *glDevice
done chan struct{}
mainDone uint32
drawDone chan struct{}
waitForStart chan struct{}

Expand Down Expand Up @@ -105,10 +105,8 @@ func (d *gLDriver) Quit() {
fyne.CurrentApp().Lifecycle().(*intapp.Lifecycle).TriggerExitedForeground()
}

// Only call close once to avoid panic.
if atomic.CompareAndSwapUint32(&running, 1, 0) {
close(d.done)
}
atomic.StoreUint32(&d.mainDone, 1)
postEmptyEvent()
}

func (d *gLDriver) addWindow(w *window) {
Expand Down Expand Up @@ -173,7 +171,6 @@ func NewGLDriver() *gLDriver {
repository.Register("file", intRepo.NewFileRepository())

return &gLDriver{
done: make(chan struct{}),
drawDone: make(chan struct{}),
waitForStart: make(chan struct{}),
animation: &animation.Runner{},
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/glfw/driver_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (d *gLDriver) CurrentKeyModifiers() fyne.KeyModifier {

func (d *gLDriver) catchTerm() {
terminateSignal := make(chan os.Signal, 1)
signal.Notify(terminateSignal, syscall.SIGINT, syscall.SIGTERM)
signal.Notify(terminateSignal, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)

<-terminateSignal
d.Quit()
Expand Down
142 changes: 77 additions & 65 deletions internal/driver/glfw/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func runOnMain(f func()) {
defer common.DonePool.Put(done)

funcQueue <- funcData{f: f, done: done}
postEmptyEvent()

<-done
}
Expand Down Expand Up @@ -115,87 +116,98 @@ func (d *gLDriver) runGL() {
d.trayStart()
}
fyne.CurrentApp().Lifecycle().(*app.Lifecycle).TriggerStarted()
eventTick := time.NewTicker(time.Second / 60)
for {
select {
case <-d.done:
eventTick.Stop()
d.drawDone <- struct{}{} // wait for draw thread to stop
d.Terminate()

postEmptyEvent()

eventTick := time.NewTicker(time.Second / 60) // TODO: Why does it not work without this?
Copy link
Member Author

@Jacalz Jacalz Nov 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get no window without this ticker and I just don't understand why. If the glfw.WaitEvents() method blocks as it's supposed to, surely this loop won't run more times because we have a ticker (that in theory ought to make sure run less compared to a bare for {})?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying it works with the ticker but not the bare for{}?
If so my guess would be that its blocking the posting of other events that you're expecting will wake it up.
You could test by switching to [glfwWaitEventsTimeout](https://www.glfw.org/docs/3.3/group__window.html#ga605a178db92f1a7f1a925563ef3ea2cf)(0.7); and see if it helps

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying it works with the ticker but not the bare for{}?

Yup. I am worried that glfwWaitEventsTImout will be just as bad as the previous code unless we have a very long timeout.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not meant as a solution, merely more data for debugging

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think postEmtpyEvent at line 120 needs to be in a goroutine so that we have called waitEvents before postEmptyEvent is called. Doing that seems to launch the window in my testing without using the ticker.

The real challenge is figuring out everywhere we would now need to PostEmtpyEvent to wake up the main thread. Especially on ARM Mac where we draw on the main thread it will be a challenge, since the waitEvents call will need to be woken up whenever there's something that needs to be drawn/refreshed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, cool. I'll keep that in mind and look at this once again going forward

for range eventTick.C {
d.waitForEvents()

if atomic.LoadUint32(&d.mainDone) == 1 {
d.drawDone <- struct{}{} // Tell draw thread to stop.
d.terminate()
fyne.CurrentApp().Lifecycle().(*app.Lifecycle).TriggerStopped()
return
}

select {
case f := <-funcQueue:
f.f()
f.done <- struct{}{}
case <-eventTick.C:
d.tryPollEvents()
windowsToRemove := 0
for _, win := range d.windowList() {
w := win.(*window)
if w.viewport == nil {
continue
}
default: // Do not wait for items to populate in the queue.
}

if w.viewport.ShouldClose() {
windowsToRemove++
continue
}
windowsToRemove := 0
for _, win := range d.windowList() {
w := win.(*window)
if w.viewport == nil {
continue
}

w.viewLock.RLock()
expand := w.shouldExpand
fullScreen := w.fullScreen
w.viewLock.RUnlock()

if expand && !fullScreen {
w.fitContent()
w.viewLock.Lock()
shouldExpand := w.shouldExpand
w.shouldExpand = false
view := w.viewport
w.viewLock.Unlock()
if shouldExpand {
view.SetSize(w.shouldWidth, w.shouldHeight)
}
}
if w.viewport.ShouldClose() {
windowsToRemove++
continue
}

if drawOnMainThread {
d.drawSingleFrame()
w.viewLock.RLock()
expand := w.shouldExpand
fullScreen := w.fullScreen
w.viewLock.RUnlock()

if expand && !fullScreen {
w.fitContent()
w.viewLock.Lock()
shouldExpand := w.shouldExpand
w.shouldExpand = false
view := w.viewport
w.viewLock.Unlock()
if shouldExpand {
view.SetSize(w.shouldWidth, w.shouldHeight)
}
}
if windowsToRemove > 0 {
oldWindows := d.windowList()
newWindows := make([]fyne.Window, 0, len(oldWindows)-windowsToRemove)

for _, win := range oldWindows {
w := win.(*window)
if w.viewport == nil {
continue
}

if w.viewport.ShouldClose() {
w.viewLock.Lock()
w.visible = false
v := w.viewport
w.viewLock.Unlock()
if drawOnMainThread {
d.drawSingleFrame()
}
}

if windowsToRemove > 0 {
d.removeWindows(windowsToRemove)
}
}
}

// remove window from window list
v.Destroy()
w.destroy(d)
continue
}
func (d *gLDriver) removeWindows(windowsToRemove int) {
oldWindows := d.windowList()
newWindows := make([]fyne.Window, 0, len(oldWindows)-windowsToRemove)

newWindows = append(newWindows, win)
}
for _, win := range oldWindows {
w := win.(*window)
if w.viewport == nil {
continue
}

d.windowLock.Lock()
d.windows = newWindows
d.windowLock.Unlock()
if w.viewport.ShouldClose() {
w.viewLock.Lock()
w.visible = false
v := w.viewport
w.viewLock.Unlock()

if len(newWindows) == 0 {
d.Quit()
}
}
// remove window from window list
v.Destroy()
w.destroy(d)
continue
}

newWindows = append(newWindows, win)
}

d.windowLock.Lock()
d.windows = newWindows
d.windowLock.Unlock()

if len(newWindows) == 0 {
d.Quit()
}
}

Expand Down
11 changes: 8 additions & 3 deletions internal/driver/glfw/loop_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ func (d *gLDriver) initGLFW() {
})
}

func (d *gLDriver) tryPollEvents() {
// waitForEvents() will block until one or more events occur.
func (*gLDriver) waitForEvents() {
defer func() {
if r := recover(); r != nil {
fyne.LogError(fmt.Sprint("GLFW poll event error: ", r), nil)
}
}()

glfw.PollEvents() // This call blocks while window is being resized, which prevents freeDirtyTextures from being called
glfw.WaitEvents()
}

func (d *gLDriver) Terminate() {
func postEmptyEvent() {
glfw.PostEmptyEvent()
}

func (*gLDriver) terminate() {
glfw.Terminate()
}
10 changes: 7 additions & 3 deletions internal/driver/glfw/loop_goxjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ func (d *gLDriver) initGLFW() {
})
}

func (d *gLDriver) tryPollEvents() {
func (*gLDriver) waitForEvents() {
defer func() {
if r := recover(); r != nil {
fyne.LogError(fmt.Sprint("GLFW poll event error: ", r), nil)
}
}()

glfw.PollEvents() // This call blocks while window is being resized, which prevents freeDirtyTextures from being called
glfw.WaitEvents()
}

func (d *gLDriver) Terminate() {
func postEmptyEvent() {
glfw.PostEmptyEvent()
}

func (*gLDriver) terminate() {
glfw.Terminate()
}