-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.go
675 lines (616 loc) · 16.2 KB
/
window.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// Copyright 2019 The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// based on golang.org/x/exp/shiny:
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package glos
import (
"image"
"image/color"
"image/draw"
"log"
"sync"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/goki/gi/mat32"
"github.com/goki/gi/oswin"
"github.com/goki/gi/oswin/driver/internal/drawer"
"github.com/goki/gi/oswin/driver/internal/event"
"github.com/goki/gi/oswin/gpu"
"github.com/goki/gi/oswin/window"
"github.com/goki/ki/bitflag"
)
type windowImpl struct {
oswin.WindowBase
event.Deque
app *appImpl
glw *glfw.Window
runQueue chan funcRun
publish chan struct{}
publishDone chan struct{}
winClose chan struct{}
winTex *textureImpl
mu sync.Mutex
mainMenu oswin.MainMenu
closeReqFunc func(win oswin.Window)
closeCleanFunc func(win oswin.Window)
drawQuads gpu.BufferMgr
fillQuads gpu.BufferMgr
}
// Handle returns the driver-specific handle for this window.
// Currently, for all platforms, this is *glfw.Window, but that
// cannot always be assumed. Only provided for unforseen emergency use --
// please file an Issue for anything that should be added to Window
// interface.
func (w *windowImpl) Handle() interface{} {
return w.glw
}
func (w *windowImpl) IsClosed() bool {
if w == nil {
return true
}
w.mu.Lock()
defer w.mu.Unlock()
return w.glw == nil
}
func (w *windowImpl) IsVisible() bool {
if w == nil || theApp.noScreens {
return false
}
w.mu.Lock()
defer w.mu.Unlock()
return w.glw != nil && w.winTex != nil && !w.IsMinimized()
}
// Activate() sets this window as the current render target for gpu rendering
// functions, and the current context for gpu state (equivalent to
// MakeCurrentContext on OpenGL).
// If it returns false, then window is not visible / valid and
// nothing further should happen.
// Must call this on app main thread using oswin.TheApp.RunOnMain
//
// oswin.TheApp.RunOnMain(func() {
// if !win.Activate() {
// return
// }
// // do GPU calls here
// })
//
func (w *windowImpl) Activate() bool {
// note: activate is only run on main thread so we don't need to check for mutex
if w == nil || w.glw == nil {
return false
}
w.glw.MakeContextCurrent()
return true
}
// DeActivate() clears the current render target and gpu rendering context.
// Generally more efficient to NOT call this and just be sure to call
// Activate where relevant, so that if the window is already current context
// no switching is required.
// Must call this on app main thread using oswin.TheApp.RunOnMain
func (w *windowImpl) DeActivate() {
glfw.DetachCurrentContext()
}
// must be run on main
func newGLWindow(opts *oswin.NewWindowOptions) (*glfw.Window, error) {
_, _, tool, fullscreen := oswin.WindowFlagsToBool(opts.Flags)
glfw.DefaultWindowHints()
glfw.WindowHint(glfw.Resizable, glfw.True)
glfw.WindowHint(glfw.Visible, glfw.False) // needed to position
glfw.WindowHint(glfw.Focused, glfw.True)
glfw.WindowHint(glfw.ContextVersionMajor, glosGlMajor)
glfw.WindowHint(glfw.ContextVersionMinor, glosGlMinor)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
glfw.WindowHint(glfw.Samples, 0) // don't do multisampling for main window -- only in sub-render
if glosDebug {
glfw.WindowHint(glfw.OpenGLDebugContext, glfw.True)
}
// todo: glfw.Samples -- multisampling
if fullscreen {
glfw.WindowHint(glfw.Maximized, glfw.True)
}
if tool {
glfw.WindowHint(glfw.Decorated, glfw.False)
} else {
glfw.WindowHint(glfw.Decorated, glfw.True)
}
// todo: glfw.Floating for always-on-top -- could set for modal
sc := theApp.screens[0]
sz := opts.Size
if sc.DevicePixelRatio != 1.0 {
sz.X = int(float32(sz.X) / sc.DevicePixelRatio)
sz.Y = int(float32(sz.Y) / sc.DevicePixelRatio)
}
win, err := glfw.CreateWindow(sz.X, sz.Y, opts.GetTitle(), nil, theApp.shareWin)
if err != nil {
return win, err
}
win.SetPos(opts.Pos.X, opts.Pos.Y)
return win, err
}
// for sending window.Event's
func (w *windowImpl) sendWindowEvent(act window.Actions) {
winEv := window.Event{
Action: act,
}
winEv.Init()
w.Send(&winEv)
}
// NextEvent implements the oswin.EventDeque interface.
func (w *windowImpl) NextEvent() oswin.Event {
e := w.Deque.NextEvent()
return e
}
// winLoop is the window's own locked processing loop.
func (w *windowImpl) winLoop() {
outer:
for {
select {
case <-w.winClose:
break outer
case f := <-w.runQueue:
if w.glw == nil {
break outer
}
f.f()
if f.done != nil {
f.done <- true
}
case <-w.publish:
if w.glw == nil {
break outer
}
if !theApp.noScreens {
theApp.RunOnMain(func() {
if !w.Activate() {
return
}
w.glw.SwapBuffers() // note: implicitly does a flush
// note: generally don't need this:
// gpu.Draw.Clear(true, true)
})
w.publishDone <- struct{}{}
}
}
}
}
// RunOnWin runs given function on the window's unique locked thread.
func (w *windowImpl) RunOnWin(f func()) {
if w.IsClosed() {
return
}
done := make(chan bool)
w.runQueue <- funcRun{f: f, done: done}
<-done
}
// GoRunOnWin runs given function on window's unique locked thread and returns immediately
func (w *windowImpl) GoRunOnWin(f func()) {
if w.IsClosed() {
return
}
go func() {
w.runQueue <- funcRun{f: f, done: nil}
}()
}
// Publish does the equivalent of SwapBuffers on OpenGL: pushes the
// current rendered back-buffer to the front (and ensures that any
// ongoing rendering has completed) (see also PublishTex)
func (w *windowImpl) Publish() {
if !w.IsVisible() {
return
}
glfw.PostEmptyEvent()
w.publish <- struct{}{}
<-w.publishDone
glfw.PostEmptyEvent()
}
// PublishTex draws the current WinTex texture to the window and then
// calls Publish() -- this is the typical update call.
func (w *windowImpl) PublishTex() {
if !w.IsVisible() {
return
}
theApp.RunOnMain(func() {
if !w.Activate() || w.winTex == nil {
return
}
w.Copy(image.ZP, w.winTex, w.winTex.Bounds(), oswin.Src, nil)
})
w.Publish()
}
// SendEmptyEvent sends an empty, blank event to this window, which just has
// the effect of pushing the system along during cases when the window
// event loop needs to be "pinged" to get things moving along..
func (w *windowImpl) SendEmptyEvent() {
if w.IsClosed() {
return
}
oswin.SendCustomEvent(w, nil)
glfw.PostEmptyEvent() // for good measure
}
// WinTex() returns the current Texture of the same size as the window that
// is typically used to update the window contents.
// Use the various Drawer and SetSubImage methods to update this Texture, and
// then call PublishTex() to update the window.
// This Texture is automatically resized when the window is resized, and
// when that occurs, existing contents are lost -- a full update of the
// Texture at the current size is required at that point.
func (w *windowImpl) WinTex() oswin.Texture {
return w.winTex
}
// SetWinTexSubImage calls SetSubImage on WinTex with given parameters.
// convenience routine that activates the window context and runs on the
// window's thread.
func (w *windowImpl) SetWinTexSubImage(dp image.Point, src image.Image, sr image.Rectangle) error {
if !w.IsVisible() {
return nil
}
var err error
theApp.RunOnMain(func() {
if !w.Activate() || w.winTex == nil {
return
}
err = w.winTex.SetSubImage(dp, src, sr)
})
return err
}
////////////////////////////////////////////////
// Drawer wrappers
func (w *windowImpl) Draw(src2dst mat32.Mat3, src oswin.Texture, sr image.Rectangle, op draw.Op, opts *oswin.DrawOptions) {
if !w.IsVisible() {
return
}
theApp.RunOnMain(func() {
if !w.Activate() {
return
}
gpu.TheGPU.RenderToWindow()
gpu.Draw.Viewport(image.Rectangle{Max: w.PxSize})
if w.drawQuads == nil {
w.drawQuads = theApp.drawQuadsBuff()
}
sz := w.Size()
theApp.draw(sz, src2dst, src, sr, op, opts, w.drawQuads, true) // true = dest has botZero
})
}
func (w *windowImpl) DrawUniform(src2dst mat32.Mat3, src color.Color, sr image.Rectangle, op draw.Op, opts *oswin.DrawOptions) {
if !w.IsVisible() {
return
}
theApp.RunOnMain(func() {
if !w.Activate() {
return
}
gpu.TheGPU.RenderToWindow()
gpu.Draw.Viewport(image.Rectangle{Max: w.PxSize})
if w.fillQuads == nil {
w.fillQuads = theApp.fillQuadsBuff()
}
sz := w.Size()
theApp.drawUniform(sz, src2dst, src, sr, op, opts, w.fillQuads, true) // true = dest has botZero
})
}
func (w *windowImpl) Copy(dp image.Point, src oswin.Texture, sr image.Rectangle, op draw.Op, opts *oswin.DrawOptions) {
if !w.IsVisible() {
return
}
drawer.Copy(w, dp, src, sr, op, opts)
}
func (w *windowImpl) Scale(dr image.Rectangle, src oswin.Texture, sr image.Rectangle, op draw.Op, opts *oswin.DrawOptions) {
if !w.IsVisible() {
return
}
drawer.Scale(w, dr, src, sr, op, opts)
}
func (w *windowImpl) Fill(dr image.Rectangle, src color.Color, op draw.Op) {
if !w.IsVisible() {
return
}
theApp.RunOnMain(func() {
if !w.Activate() {
return
}
gpu.TheGPU.RenderToWindow()
gpu.Draw.Viewport(image.Rectangle{Max: w.PxSize})
if w.fillQuads == nil {
w.fillQuads = theApp.fillQuadsBuff()
}
sz := w.Size()
theApp.fillRect(sz, dr, src, op, w.fillQuads, true) // true = dest has botZero
})
}
////////////////////////////////////////////////////////////
// Geom etc
func (w *windowImpl) Screen() *oswin.Screen {
if w.Scrn == nil {
w.getScreen()
}
if w.Scrn == nil {
return theApp.screens[0]
}
w.mu.Lock()
defer w.mu.Unlock()
return w.Scrn
}
func (w *windowImpl) Size() image.Point {
return w.PxSize
}
func (w *windowImpl) WinSize() image.Point {
return w.WnSize
}
func (w *windowImpl) Position() image.Point {
w.mu.Lock()
defer w.mu.Unlock()
var ps image.Point
ps.X, ps.Y = w.glw.GetPos()
w.Pos = ps
return ps
}
func (w *windowImpl) PhysicalDPI() float32 {
w.mu.Lock()
defer w.mu.Unlock()
return w.PhysDPI
}
func (w *windowImpl) LogicalDPI() float32 {
w.mu.Lock()
defer w.mu.Unlock()
return w.LogDPI
}
func (w *windowImpl) SetLogicalDPI(dpi float32) {
w.mu.Lock()
defer w.mu.Unlock()
w.LogDPI = dpi
}
func (w *windowImpl) SetTitle(title string) {
if w.IsClosed() {
return
}
w.Titl = title
w.app.RunOnMain(func() {
if w.glw == nil { // by time we got to main, could be diff
return
}
w.glw.SetTitle(title)
})
}
func (w *windowImpl) SetSize(sz image.Point) {
if w.IsClosed() {
return
}
// note: anything run on main only doesn't need lock -- implicit lock
w.app.RunOnMain(func() {
if w.glw == nil { // by time we got to main, could be diff
return
}
w.glw.SetSize(sz.X, sz.Y)
})
}
func (w *windowImpl) SetPos(pos image.Point) {
if w.IsClosed() {
return
}
// note: anything run on main only doesn't need lock -- implicit lock
w.app.RunOnMain(func() {
if w.glw == nil { // by time we got to main, could be diff
return
}
w.glw.SetPos(pos.X, pos.Y)
})
}
func (w *windowImpl) SetGeom(pos image.Point, sz image.Point) {
if w.IsClosed() {
return
}
// note: anything run on main only doesn't need lock -- implicit lock
w.app.RunOnMain(func() {
if w.glw == nil { // by time we got to main, could be diff
return
}
w.glw.SetSize(sz.X, sz.Y)
w.glw.SetPos(pos.X, pos.Y)
})
}
func (w *windowImpl) show() {
if w.IsClosed() {
return
}
// note: anything run on main only doesn't need lock -- implicit lock
w.app.RunOnMain(func() {
if w.glw == nil { // by time we got to main, could be diff
return
}
w.glw.Show()
})
}
func (w *windowImpl) Raise() {
if w.IsClosed() {
return
}
// note: anything run on main only doesn't need lock -- implicit lock
w.app.RunOnMain(func() {
if w.glw == nil { // by time we got to main, could be diff
return
}
if bitflag.HasAtomic(&w.Flag, int(oswin.Minimized)) {
w.glw.Restore()
} else {
w.glw.Focus()
}
})
}
func (w *windowImpl) Minimize() {
if w.IsClosed() {
return
}
// note: anything run on main only doesn't need lock -- implicit lock
w.app.RunOnMain(func() {
if w.glw == nil { // by time we got to main, could be diff
return
}
w.glw.Iconify()
})
}
func (w *windowImpl) SetCloseReqFunc(fun func(win oswin.Window)) {
w.mu.Lock()
defer w.mu.Unlock()
w.closeReqFunc = fun
}
func (w *windowImpl) SetCloseCleanFunc(fun func(win oswin.Window)) {
w.mu.Lock()
defer w.mu.Unlock()
w.closeCleanFunc = fun
}
func (w *windowImpl) CloseReq() {
if theApp.quitting {
w.Close()
}
if w.closeReqFunc != nil {
w.closeReqFunc(w)
} else {
w.Close()
}
}
func (w *windowImpl) CloseClean() {
if w.closeCleanFunc != nil {
w.closeCleanFunc(w)
}
}
func (w *windowImpl) Close() {
// this is actually the final common pathway for closing here
w.mu.Lock()
w.winClose <- struct{}{} // break out of draw loop
w.CloseClean()
// fmt.Printf("sending close event to window: %v\n", w.Nm)
w.sendWindowEvent(window.Close)
theApp.DeleteWin(w)
w.app.RunOnMain(func() {
if w.winTex != nil {
w.winTex.Delete()
w.winTex = nil
}
if w.drawQuads != nil {
w.drawQuads.Delete()
w.drawQuads = nil
}
if w.fillQuads != nil {
w.fillQuads.Delete()
w.fillQuads = nil
}
w.glw.Destroy()
w.glw = nil // marks as closed for all other calls
})
w.mu.Unlock()
}
func (w *windowImpl) SetMousePos(x, y float64) {
if !w.IsVisible() {
return
}
w.mu.Lock()
defer w.mu.Unlock()
w.glw.SetCursorPos(x, y)
}
/////////////////////////////////////////////////////////
// Window Callbacks
func (w *windowImpl) getScreen() {
w.mu.Lock()
mon := w.glw.GetMonitor() // this returns nil for windowed windows -- i.e., most windows
// that is super useless it seems.
if mon != nil {
sc := theApp.ScreenByName(mon.GetName())
if sc != nil {
w.Scrn = sc
w.PhysDPI = sc.PhysicalDPI
} else {
log.Printf("glos getScreen: could not find screen of name: %v\n", mon.GetName())
w.Scrn = theApp.screens[0]
w.PhysDPI = w.Scrn.PhysicalDPI
}
} else {
if len(theApp.screens) > 0 {
w.Scrn = theApp.screens[0]
w.PhysDPI = w.Scrn.PhysicalDPI
}
}
if w.LogDPI == 0 {
w.LogDPI = w.Scrn.LogicalDPI
}
w.mu.Unlock()
}
func (w *windowImpl) moved(gw *glfw.Window, x, y int) {
w.mu.Lock()
w.Pos = image.Point{x, y}
w.mu.Unlock()
w.getScreen()
w.sendWindowEvent(window.Move)
}
func (w *windowImpl) winResized(gw *glfw.Window, width, height int) {
w.updtGeom()
}
func (w *windowImpl) updtGeom() {
w.getScreen()
w.mu.Lock()
var wsz image.Point
wsz.X, wsz.Y = w.glw.GetSize()
w.WnSize = wsz
var fbsz image.Point
fbsz.X, fbsz.Y = w.glw.GetFramebufferSize()
w.PxSize = fbsz
if w.PxSize != w.WnSize {
w.DevPixRatio = float32(w.PxSize.X) / float32(w.WnSize.X)
} else {
w.DevPixRatio = 1
}
if w.DevPixRatio != w.Scrn.DevicePixelRatio {
rr := w.DevPixRatio / w.Scrn.DevicePixelRatio
w.Scrn.PhysicalDPI *= rr
w.Scrn.LogicalDPI *= rr
w.Scrn.DevicePixelRatio = w.DevPixRatio
}
w.PhysDPI = w.Scrn.PhysicalDPI
w.LogDPI = w.Scrn.LogicalDPI
w.mu.Unlock()
if w.Activate() {
w.winTex.SetSize(w.PxSize)
}
w.sendWindowEvent(window.Resize) // this will not get processed until the end
}
func (w *windowImpl) fbResized(gw *glfw.Window, width, height int) {
fbsz := image.Point{width, height}
if w.PxSize != fbsz {
w.updtGeom()
}
}
func (w *windowImpl) closeReq(gw *glfw.Window) {
go w.CloseReq()
}
func (w *windowImpl) refresh(gw *glfw.Window) {
go w.Publish()
}
func (w *windowImpl) focus(gw *glfw.Window, focused bool) {
if focused {
// fmt.Printf("foc win: %v, foc: %v\n", w.Nm, bitflag.HasAtomic(&w.Flag, int(oswin.Focus)))
if w.mainMenu != nil {
w.mainMenu.SetMenu()
}
bitflag.ClearAtomic(&w.Flag, int(oswin.Minimized))
bitflag.SetAtomic(&w.Flag, int(oswin.Focus))
w.sendWindowEvent(window.Focus)
} else {
// fmt.Printf("unfoc win: %v, foc: %v\n", w.Nm, bitflag.HasAtomic(&w.Flag, int(oswin.Focus)))
bitflag.ClearAtomic(&w.Flag, int(oswin.Focus))
w.sendWindowEvent(window.DeFocus)
}
}
func (w *windowImpl) iconify(gw *glfw.Window, iconified bool) {
if iconified {
bitflag.SetAtomic(&w.Flag, int(oswin.Minimized))
bitflag.ClearAtomic(&w.Flag, int(oswin.Focus))
w.sendWindowEvent(window.Minimize)
} else {
bitflag.ClearAtomic(&w.Flag, int(oswin.Minimized))
w.getScreen()
w.sendWindowEvent(window.Minimize)
}
}