-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
loop.go
261 lines (226 loc) · 5.55 KB
/
loop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package glfw
import (
"fmt"
"runtime"
"sync"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal"
"fyne.io/fyne/v2/internal/app"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/internal/painter"
"github.com/go-gl/glfw/v3.3/glfw"
)
type funcData struct {
f func()
done chan struct{} // Zero allocation signalling channel
}
type drawData struct {
f func()
win *window
done chan struct{} // Zero allocation signalling channel
}
// channel for queuing functions on the main thread
var funcQueue = make(chan funcData)
var drawFuncQueue = make(chan drawData)
var runFlag = false
var runMutex = &sync.Mutex{}
var initOnce = &sync.Once{}
var donePool = &sync.Pool{New: func() interface{} {
return make(chan struct{})
}}
// Arrange that main.main runs on main thread.
func init() {
runtime.LockOSThread()
}
func running() bool {
runMutex.Lock()
defer runMutex.Unlock()
return runFlag
}
// force a function f to run on the main thread
func runOnMain(f func()) {
// If we are on main just execute - otherwise add it to the main queue and wait.
// The "running" variable is normally false when we are on the main thread.
if !running() {
f()
} else {
done := donePool.Get().(chan struct{})
defer donePool.Put(done)
funcQueue <- funcData{f: f, done: done}
<-done
}
}
// force a function f to run on the draw thread
func runOnDraw(w *window, f func()) {
done := donePool.Get().(chan struct{})
defer donePool.Put(done)
drawFuncQueue <- drawData{f: f, win: w, done: done}
<-done
}
func (d *gLDriver) initGLFW() {
initOnce.Do(func() {
err := glfw.Init()
if err != nil {
fyne.LogError("failed to initialise GLFW", err)
return
}
initCursors()
d.startDrawThread()
})
}
func (d *gLDriver) runGL() {
eventTick := time.NewTicker(time.Second / 60)
runMutex.Lock()
runFlag = true
runMutex.Unlock()
d.initGLFW()
fyne.CurrentApp().Lifecycle().(*app.Lifecycle).TriggerStarted()
for {
select {
case <-d.done:
eventTick.Stop()
d.drawDone <- nil // wait for draw thread to stop
glfw.Terminate()
fyne.CurrentApp().Lifecycle().(*app.Lifecycle).TriggerStopped()
return
case f := <-funcQueue:
f.f()
if f.done != nil {
f.done <- struct{}{}
}
case <-eventTick.C:
d.tryPollEvents()
newWindows := []fyne.Window{}
reassign := false
for _, win := range d.windowList() {
w := win.(*window)
if w.viewport == nil {
continue
}
if w.viewport.ShouldClose() {
reassign = true
w.viewLock.Lock()
w.visible = false
v := w.viewport
w.viewLock.Unlock()
// remove window from window list
v.Destroy()
w.destroy(d)
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)
}
}
newWindows = append(newWindows, win)
}
if reassign {
d.windowLock.Lock()
d.windows = newWindows
d.windowLock.Unlock()
if len(newWindows) == 0 {
d.Quit()
}
}
}
}
}
func (d *gLDriver) repaintWindow(w *window) {
canvas := w.canvas
w.RunWithContext(func() {
if w.canvas.EnsureMinSize() {
w.viewLock.Lock()
w.shouldExpand = true
w.viewLock.Unlock()
}
canvas.FreeDirtyTextures()
updateGLContext(w)
canvas.paint(canvas.Size())
w.viewLock.RLock()
view := w.viewport
visible := w.visible
w.viewLock.RUnlock()
if view != nil && visible {
view.SwapBuffers()
}
})
}
func (d *gLDriver) startDrawThread() {
settingsChange := make(chan fyne.Settings)
fyne.CurrentApp().Settings().AddChangeListener(settingsChange)
draw := time.NewTicker(time.Second / 60)
go func() {
runtime.LockOSThread()
for {
select {
case <-d.drawDone:
return
case f := <-drawFuncQueue:
f.win.RunWithContext(f.f)
if f.done != nil {
f.done <- struct{}{}
}
case set := <-settingsChange:
painter.ClearFontCache()
cache.ResetThemeCaches()
app.ApplySettingsWithCallback(set, fyne.CurrentApp(), func(w fyne.Window) {
c, ok := w.Canvas().(*glCanvas)
if !ok {
return
}
c.applyThemeOutOfTreeObjects()
go c.reloadScale()
})
case <-draw.C:
canvasRefreshed := false
for _, win := range d.windowList() {
w := win.(*window)
w.viewLock.RLock()
canvas := w.canvas
closing := w.closing
visible := w.visible
w.viewLock.RUnlock()
if closing || !canvas.IsDirty() || !visible {
continue
}
canvasRefreshed = true
d.repaintWindow(w)
}
cache.Clean(canvasRefreshed)
}
}
}()
}
func (d *gLDriver) tryPollEvents() {
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
}
// refreshWindow requests that the specified window be redrawn
func refreshWindow(w *window) {
w.canvas.SetDirty(true)
}
func updateGLContext(w *window) {
canvas := w.Canvas().(*glCanvas)
size := canvas.Size()
// w.width and w.height are not correct if we are maximised, so figure from canvas
winWidth := float32(internal.ScaleInt(canvas, size.Width)) * canvas.texScale
winHeight := float32(internal.ScaleInt(canvas, size.Height)) * canvas.texScale
canvas.Painter().SetFrameBufferScale(canvas.texScale)
w.canvas.Painter().SetOutputSize(int(winWidth), int(winHeight))
}