2
0
mirror of synced 2025-02-23 23:08:14 +00:00
mobile/exp/app/debug/fps.go

221 lines
3.6 KiB
Go
Raw Normal View History

// Copyright 2014 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.
// +build darwin linux windows
// Package debug provides GL-based debugging tools for apps.
package debug // import "golang.org/x/mobile/exp/app/debug"
import (
"image"
"image/color"
"image/draw"
"time"
"golang.org/x/mobile/event/size"
"golang.org/x/mobile/exp/gl/glutil"
"golang.org/x/mobile/geom"
)
// FPS draws a count of the frames rendered per second.
type FPS struct {
sz size.Event
images *glutil.Images
m *glutil.Image
lastDraw time.Time
// TODO: store *gl.Context
}
// NewFPS creates an FPS tied to the current GL context.
func NewFPS(images *glutil.Images) *FPS {
return &FPS{
lastDraw: time.Now(),
images: images,
}
}
// Draw draws the per second framerate in the bottom-left of the screen.
func (p *FPS) Draw(sz size.Event) {
const imgW, imgH = 7*(fontWidth+1) + 1, fontHeight + 2
if sz.WidthPx == 0 && sz.HeightPx == 0 {
return
}
if p.sz != sz {
p.sz = sz
if p.m != nil {
p.m.Release()
}
p.m = p.images.NewImage(imgW, imgH)
app: use one thread for both GL and other UI C code. This change will break Darwin. I have only built and tested this on desktop linux and Android linux. A follow-up CL will fix Darwin. Currently, OpenGL gets its own thread, and UI C code (e.g. the Android event loop, or the X11 event loop) gets its own thread. This relies on multiple system-provided UI-related C libraries working nicely together, even when running on different threads. Keeping all the C code on the one thread seems more sound. As side-effects: - In package app/debug, DrawFPS now takes an explicit Config. - In package app, some callbacks now take an explicit Config. - In package exp/sprite, Render now takes an explicit Config. - In package event, there are new events (Config, Draw, Lifecycle), and an event filter mechanism to replace multiple app Callbacks. - In package geom, the deprecated Width, Height and PixelsPerPt global variables were removed in favor of an event.Config that is explicitly passed around (and does not require mutex-locking). Converting a geom.Pt to pixels now requires passing a pixelsPerPt. - In package gl, the Do, Start and Stop functions are removed, as well as the need to call Start in its own goroutine. There is no longer a separate GL thread. Instead, package app explicitly performs any GL work (gl.DoWork) when some is available (gl.WorkAvailable). - In package gl/glutil, Image.Draw now takes an explicit Config. Callbacks are no longer executed on 'the UI thread'. Changing the app programming model from callbacks to events (since a channel of events works with select) will be a follow-up change. Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1 Reviewed-on: https://go-review.googlesource.com/11351 Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
}
display := [7]byte{
4: 'F',
5: 'P',
6: 'S',
}
now := time.Now()
f := 0
if dur := now.Sub(p.lastDraw); dur > 0 {
f = int(time.Second / dur)
}
display[2] = '0' + byte((f/1e0)%10)
display[1] = '0' + byte((f/1e1)%10)
display[0] = '0' + byte((f/1e2)%10)
draw.Draw(p.m.RGBA, p.m.RGBA.Bounds(), image.White, image.Point{}, draw.Src)
for i, c := range display {
glyph := glyphs[c]
if len(glyph) != fontWidth*fontHeight {
continue
}
for y := 0; y < fontHeight; y++ {
for x := 0; x < fontWidth; x++ {
if glyph[fontWidth*y+x] == ' ' {
continue
}
p.m.RGBA.SetRGBA((fontWidth+1)*i+x+1, y+1, color.RGBA{A: 0xff})
}
}
}
p.m.Upload()
p.m.Draw(
sz,
geom.Point{0, sz.HeightPt - imgH},
geom.Point{imgW, sz.HeightPt - imgH},
geom.Point{0, sz.HeightPt},
p.m.RGBA.Bounds(),
)
p.lastDraw = now
}
func (f *FPS) Release() {
if f.m != nil {
f.m.Release()
f.m = nil
f.images = nil
}
}
const (
fontWidth = 5
fontHeight = 7
)
// glyphs comes from the 6x10 fixed font from the plan9port:
// https://github.com/9fans/plan9port/tree/master/font/fixed
//
// 6x10 becomes 5x7 because each glyph has a 1-pixel margin plus space for
// descenders.
//
// Its README file says that those fonts were converted from XFree86, and are
// in the public domain.
var glyphs = [256]string{
'0': "" +
" X " +
" X X " +
"X X" +
"X X" +
"X X" +
" X X " +
" X ",
'1': "" +
" X " +
" XX " +
"X X " +
" X " +
" X " +
" X " +
"XXXXX",
'2': "" +
" XXX " +
"X X" +
" X" +
" XX " +
" X " +
"X " +
"XXXXX",
'3': "" +
"XXXXX" +
" X" +
" X " +
" XX " +
" X" +
"X X" +
" XXX ",
'4': "" +
" X " +
" XX " +
" X X " +
"X X " +
"XXXXX" +
" X " +
" X ",
'5': "" +
"XXXXX" +
"X " +
"X XX " +
"XX X" +
" X" +
"X X" +
" XXX ",
'6': "" +
" XX " +
" X " +
"X " +
"X XX " +
"XX X" +
"X X" +
" XXX ",
'7': "" +
"XXXXX" +
" X" +
" X " +
" X " +
" X " +
" X " +
" X ",
'8': "" +
" XXX " +
"X X" +
"X X" +
" XXX " +
"X X" +
"X X" +
" XXX ",
'9': "" +
" XXX " +
"X X" +
"X XX" +
" XX X" +
" X" +
" X " +
" XX ",
'F': "" +
"XXXXX" +
"X " +
"X " +
"XXXX " +
"X " +
"X " +
"X ",
'P': "" +
"XXXX " +
"X X" +
"X X" +
"XXXX " +
"X " +
"X " +
"X ",
'S': "" +
" XXX " +
"X X" +
"X " +
" XXX " +
" X" +
"X X" +
" XXX ",
}