forked from fyne-io/fyne
/
loop.go
291 lines (249 loc) · 6.52 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package glfw
import (
"runtime"
"sync"
"time"
"github.com/gorustyt/fyne/v2"
"github.com/gorustyt/fyne/v2/internal/app"
"github.com/gorustyt/fyne/v2/internal/cache"
"github.com/gorustyt/fyne/v2/internal/driver/common"
"github.com/gorustyt/fyne/v2/internal/painter"
"github.com/gorustyt/fyne/v2/internal/scale"
)
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
}
type runFlag struct {
sync.Cond
flag bool
}
// channel for queuing functions on the main thread
var funcQueue = make(chan funcData)
var drawFuncQueue = make(chan drawData)
var run = &runFlag{Cond: sync.Cond{L: &sync.Mutex{}}}
var initOnce = &sync.Once{}
// Arrange that main.main runs on main thread.
func init() {
runtime.LockOSThread()
mainGoroutineID = goroutineID()
}
// 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.
run.L.Lock()
running := !run.flag
run.L.Unlock()
if running {
f()
} else {
done := common.DonePool.Get().(chan struct{})
defer common.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()) {
if drawOnMainThread {
runOnMain(func() { w.RunWithContext(f) })
return
}
done := common.DonePool.Get().(chan struct{})
defer common.DonePool.Put(done)
drawFuncQueue <- drawData{f: f, win: w, done: done}
<-done
}
// Preallocate to avoid allocations on every drawSingleFrame.
// Note that the capacity of this slice can only grow,
// but its length will never be longer than the total number of
// window canvases that are dirty on a single frame.
// So its memory impact should be negligible and does not
// need periodic shrinking.
var refreshingCanvases []fyne.Canvas
func (d *gLDriver) drawSingleFrame() {
for _, win := range d.windowList() {
w := win.(*window)
w.viewLock.RLock()
canvas := w.canvas
closing := w.closing
visible := w.visible
w.viewLock.RUnlock()
// CheckDirtyAndClear must be checked after visibility,
// because when a window becomes visible, it could be
// showing old content without a dirty flag set to true.
// Do the clear if and only if the window is visible.
if closing || !visible || !canvas.CheckDirtyAndClear() {
continue
}
d.repaintWindow(w)
refreshingCanvases = append(refreshingCanvases, canvas)
}
cache.CleanCanvases(refreshingCanvases)
// cleanup refreshingCanvases slice
for i := 0; i < len(refreshingCanvases); i++ {
refreshingCanvases[i] = nil
}
refreshingCanvases = refreshingCanvases[:0]
}
func (d *gLDriver) runGL() {
eventTick := time.NewTicker(time.Second / 60)
run.L.Lock()
run.flag = true
run.L.Unlock()
run.Broadcast()
d.initGLFW()
if d.trayStart != nil {
d.trayStart()
}
fyne.CurrentApp().Lifecycle().(*app.Lifecycle).TriggerStarted()
for {
select {
case <-d.done:
eventTick.Stop()
d.drawDone <- nil // wait for draw thread to stop
d.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()
windowsToRemove := 0
for _, win := range d.windowList() {
w := win.(*window)
if w.viewport == nil {
continue
}
if w.viewport.ShouldClose() {
windowsToRemove++
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 drawOnMainThread {
d.drawSingleFrame()
}
}
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()
// 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()
}
}
}
}
}
func (d *gLDriver) repaintWindow(w *window) {
canvas := w.canvas
w.RunWithContext(func() {
if 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)
var drawCh <-chan time.Time
if drawOnMainThread {
drawCh = make(chan time.Time) // don't tick when on M1
} else {
drawCh = time.NewTicker(time.Second / 60).C
}
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 <-drawCh:
d.drawSingleFrame()
}
}
}()
}
// refreshWindow requests that the specified window be redrawn
func refreshWindow(w *window) {
w.canvas.SetDirty()
}
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(scale.ToScreenCoordinate(canvas, size.Width)) * canvas.texScale
winHeight := float32(scale.ToScreenCoordinate(canvas, size.Height)) * canvas.texScale
canvas.Painter().SetFrameBufferScale(canvas.texScale)
w.canvas.Painter().SetOutputSize(int(winWidth), int(winHeight))
}