Skip to content

Commit

Permalink
Cleanup GLFW init to avoid needing a sync.Once
Browse files Browse the repository at this point in the history
This replaces my previous PR which had to be reverted due to an issue with the application not having started. This cleans up some of the init code to avoid calling init multiple times (when starting and when creating a new windows).
It also includes an extra change to set up the device field more coherently.

Replaces fyne-io#4177
  • Loading branch information
Jacalz committed Nov 18, 2023
1 parent 802f92b commit 49fe2b2
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 32 deletions.
5 changes: 5 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ func newAppWithDriver(d fyne.Driver, id string) fyne.App {
repository.Register("http", httpHandler)
repository.Register("https", httpHandler)

// Run .Init() after app has been set up.
if driver, ok := d.(interface{ Init() }); ok {
driver.Init()
}

return newApp
}

Expand Down
5 changes: 1 addition & 4 deletions internal/driver/glfw/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ func (d *gLDriver) AbsolutePositionForObject(co fyne.CanvasObject) fyne.Position
}

func (d *gLDriver) Device() fyne.Device {
if d.device == nil {
d.device = &glDevice{}
}

return d.device
}

Expand Down Expand Up @@ -177,5 +173,6 @@ func NewGLDriver() *gLDriver {
drawDone: make(chan struct{}),
waitForStart: make(chan struct{}),
animation: &animation.Runner{},
device: &glDevice{},
}
}
3 changes: 0 additions & 3 deletions internal/driver/glfw/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package glfw

import (
"runtime"
"sync"
"sync/atomic"
"time"

Expand All @@ -29,7 +28,6 @@ type drawData struct {
var funcQueue = make(chan funcData)
var drawFuncQueue = make(chan drawData)
var running uint32 // atomic bool, 0 or 1
var initOnce = &sync.Once{}

// Arrange that main.main runs on main thread.
func init() {
Expand Down Expand Up @@ -110,7 +108,6 @@ func (d *gLDriver) runGL() {
}
close(d.waitForStart) // Signal that execution can continue.

d.initGLFW()
if d.trayStart != nil {
d.trayStart()
}
Expand Down
21 changes: 10 additions & 11 deletions internal/driver/glfw/loop_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ import (
"github.com/go-gl/glfw/v3.3/glfw"
)

func (d *gLDriver) initGLFW() {
initOnce.Do(func() {
err := glfw.Init()
if err != nil {
fyne.LogError("failed to initialise GLFW", err)
return
}

initCursors()
d.startDrawThread()
})
// Init makes the driver ready to run. Needs to be called after app is created.
func (d *gLDriver) Init() {
err := glfw.Init()
if err != nil {
fyne.LogError("failed to initialise GLFW", err)
return
}

initCursors()
d.startDrawThread()
}

func (d *gLDriver) tryPollEvents() {
Expand Down
19 changes: 9 additions & 10 deletions internal/driver/glfw/loop_goxjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ import (
glfw "github.com/fyne-io/glfw-js"
)

func (d *gLDriver) initGLFW() {
initOnce.Do(func() {
err := glfw.Init(gl.ContextWatcher)
if err != nil {
fyne.LogError("failed to initialise GLFW", err)
return
}

d.startDrawThread()
})
// Init makes the driver ready to run. Needs to be called after app is created.
func (d *gLDriver) Init() {
err := glfw.Init(gl.ContextWatcher)
if err != nil {
fyne.LogError("failed to initialise GLFW", err)
return
}

d.startDrawThread()
}

func (d *gLDriver) tryPollEvents() {
Expand Down
4 changes: 1 addition & 3 deletions internal/driver/glfw/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,13 +948,11 @@ func (d *gLDriver) CreateWindow(title string) fyne.Window {
}

func (d *gLDriver) createWindow(title string, decorate bool) fyne.Window {
var ret *window
if title == "" {
title = defaultTitle
}
var ret *window
runOnMain(func() {
d.initGLFW()

ret = &window{title: title, decorate: decorate, driver: d}
// This queue is destroyed when the window is closed.
ret.InitEventQueue()
Expand Down
1 change: 0 additions & 1 deletion internal/driver/glfw/window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func init() {
// TestMain makes sure that our driver is running on the main thread.
// This must be done for some of our tests to function correctly.
func TestMain(m *testing.M) {
d.initGLFW()
go func() {
// Wait for GLFW loop to be running.
// If we try to create windows before the context is created, this will fail with an exception.
Expand Down

0 comments on commit 49fe2b2

Please sign in to comment.