~eliasnaur/gio

gio/gpu/timer.go -rw-r--r-- 1.3 KiB View raw
7bbe0da0Elias Naur text,font/opentype: make text layout and shaping safe for concurrent use a day ago
                                                                                
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
// SPDX-License-Identifier: Unlicense OR MIT

package gpu

import (
	"time"

	"gioui.org/gpu/backend"
)

type timers struct {
	backend backend.Device
	timers  []*timer
}

type timer struct {
	Elapsed time.Duration
	backend backend.Device
	timer   backend.Timer
	state   timerState
}

type timerState uint8

const (
	timerIdle timerState = iota
	timerRunning
	timerWaiting
)

func newTimers(b backend.Device) *timers {
	return &timers{
		backend: b,
	}
}

func (t *timers) newTimer() *timer {
	if t == nil {
		return nil
	}
	tt := &timer{
		backend: t.backend,
		timer:   t.backend.NewTimer(),
	}
	t.timers = append(t.timers, tt)
	return tt
}

func (t *timer) begin() {
	if t == nil || t.state != timerIdle {
		return
	}
	t.timer.Begin()
	t.state = timerRunning
}

func (t *timer) end() {
	if t == nil || t.state != timerRunning {
		return
	}
	t.timer.End()
	t.state = timerWaiting
}

func (t *timers) ready() bool {
	if t == nil {
		return false
	}
	for _, tt := range t.timers {
		switch tt.state {
		case timerIdle:
			continue
		case timerRunning:
			return false
		}
		d, ok := tt.timer.Duration()
		if !ok {
			return false
		}
		tt.state = timerIdle
		tt.Elapsed = d
	}
	return t.backend.IsTimeContinuous()
}

func (t *timers) release() {
	if t == nil {
		return
	}
	for _, tt := range t.timers {
		tt.timer.Release()
	}
	t.timers = nil
}