-
Notifications
You must be signed in to change notification settings - Fork 5
/
render_throttler.go
85 lines (71 loc) · 2.08 KB
/
render_throttler.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
package main
import (
"time"
"github.com/divan/whispervis/storage"
)
// DefaultRenderThrottleDecay defines a decay period after which throttling should reenabled.
const (
DefaultRenderThrottleDecay = 5 // sec
)
// RenderThrottler is a helper to enable rendering only when it's really need to.
// Rendering can be enabled/requested externally, but will be turned off/slowed down
// after some period of time.
// TODO(divan): also, instead of disabling, might be a good idea to just decrease FPS. Explore this.
type RenderThrottler struct {
isActive bool // switch on/off
needRendering bool // what throttler thinks about next frame render pass
lastUpdate int64
decay int64
}
// NewRenderThrottler returns a new render throttler.
func NewRenderThrottler() *RenderThrottler {
r := &RenderThrottler{
isActive: storage.RT(),
needRendering: true,
lastUpdate: time.Now().Unix(),
decay: DefaultRenderThrottleDecay,
}
return r
}
// Toggle switches render throttler state.
func (r *RenderThrottler) Toggle() {
r.isActive = !r.isActive
storage.SetRT(r.isActive) // TODO: this is bad. global local storage
}
// IsActive returns state of render throttler.
func (r *RenderThrottler) IsActive() bool {
return r.isActive
}
// EnableRendering enables next frames to render.
func (r *RenderThrottler) EnableRendering() {
if !r.isActive {
return
}
r.needRendering = true
r.lastUpdate = time.Now().Unix()
}
// DisableRendering disables next frames from rendering.
func (r *RenderThrottler) DisableRendering() {
if !r.isActive {
return
}
r.needRendering = false
}
// ReenableIfNeeded checks if sufficient time has passed since throttling has been
// disabled, and enables throttling back if so.
func (r *RenderThrottler) ReenableIfNeeded() {
if !r.isActive {
return
}
now := time.Now().Unix()
if r.lastUpdate+r.decay < now {
r.DisableRendering()
}
}
// NeedRendering returns true if next frame should be rendered.
func (r *RenderThrottler) NeedRendering() bool {
if !r.isActive {
return true // always render when throttler is disabled
}
return r.needRendering
}