package ui import ( "fmt" "image" "time" "gioui.org/font" "gioui.org/layout" "100gex/internal/theme" ) // FrameBudget is the longest a frame may take: 240 frames a second, as // the fastest common displays refresh. const FrameBudget = time.Second / 240 // statFrames is how many of the latest frames the frame times cover. const statFrames = 60 // idleGap is the longest time between frames that counts towards the // frame rate. Frames are only drawn when something changes, so a longer // gap is the window waiting, not drawing slowly. const idleGap = 250 * time.Millisecond // ring holds the latest statFrames durations. type ring struct { ds [statFrames]time.Duration n int // durations held, up to statFrames next int // where the next one goes } func (r *ring) add(d time.Duration) { r.ds[r.next] = d r.next = (r.next + 1) % statFrames r.n = min(r.n+1, statFrames) } // summary is the average and the largest of the durations held. func (r *ring) summary() (avg, worst time.Duration) { if r.n == 0 { return 0, 0 } var total time.Duration for _, d := range r.ds[:r.n] { total += d worst = max(worst, d) } return total / time.Duration(r.n), worst } // frameStats remembers the latest frames: what each cost to lay out and // hand to the GPU, and how long after the one before it began, which is // what people see. The cost leaves out the GPU drawing the frame and the // compositor showing it; the time between frames includes them. Frames // are only drawn when something changes, so an idle window keeps showing // what its last busy moment was like. type frameStats struct { costs ring intervals ring last time.Time // when the latest frame began } // add records a frame that began at start and cost cost. func (s *frameStats) add(start time.Time, cost time.Duration) { s.costs.add(cost) if !s.last.IsZero() { if gap := start.Sub(s.last); gap < idleGap { s.intervals.add(gap) } } s.last = start } // FrameDrawn tells the UI about the frame it last laid out: when Layout // began, and what it cost until the frame was handed to the GPU. func (u *UI) FrameDrawn(start time.Time, cost time.Duration) { if u.ShowFPS { u.fps.add(start, cost) } } // layoutFPS draws the frame rate and frame times, for the title bar. The // rate is measured from when frames began, while frames follow each other // closely, as when dragging: it is what the window shows, at most the // display's refresh rate. The times are what drawing cost this program, // on average and at worst. It turns red when the worst is over // FrameBudget. func (u *UI) layoutFPS(gtx layout.Context) layout.Dimensions { avg, worst := u.fps.costs.summary() ms := func(d time.Duration) string { return fmt.Sprintf("%.1f", float64(d)/float64(time.Millisecond)) } text := fmt.Sprintf("%s ms avg · %s ms worst", ms(avg), ms(worst)) if gap, slowest := u.fps.intervals.summary(); gap > 0 { text = fmt.Sprintf("%.0f fps, slowest %s ms · ", float64(time.Second)/float64(gap), ms(slowest)) + text } fill, fg := theme.White, theme.Ink if worst > FrameBudget { fill, fg = theme.Red, theme.White } return theme.Box{Fill: fill, Padding: layout.Inset{Top: 3, Bottom: 3, Left: 10, Right: 10}}.Layout(gtx, u.theme.Label(13, text, fg, font.Bold).Layout) } // layoutFPSLayer draws the -fps chip in the window's bottom right corner, // over everything. It changes every frame, so it is a layer of its own: // drawn with the rest of the titlebar, it had all of it drawn every frame, // which is the cost it measures. func (u *UI) layoutFPSLayer(gtx layout.Context) { u.layoutChip(gtx, layerFPS, 0, u.layoutFPS, func(size, window image.Point) image.Point { gap := gtx.Dp(theme.Gap) return window.Sub(size).Sub(image.Pt(gap, gap)) }) }