github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/exp/app/debug/fps.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin linux
     6  
     7  // Package debug provides GL-based debugging tools for apps.
     8  package debug // import "github.com/c-darwin/mobile/exp/app/debug"
     9  
    10  import (
    11  	"image"
    12  	"image/color"
    13  	"image/draw"
    14  	"sync"
    15  	"time"
    16  
    17  	"github.com/c-darwin/mobile/event/size"
    18  	"github.com/c-darwin/mobile/exp/gl/glutil"
    19  	"github.com/c-darwin/mobile/geom"
    20  )
    21  
    22  var lastDraw = time.Now()
    23  
    24  var fps struct {
    25  	mu sync.Mutex
    26  	sz size.Event
    27  	m  *glutil.Image
    28  }
    29  
    30  // DrawFPS draws the per second framerate in the bottom-left of the screen.
    31  func DrawFPS(sz size.Event) {
    32  	const imgW, imgH = 7*(fontWidth+1) + 1, fontHeight + 2
    33  
    34  	fps.mu.Lock()
    35  	if fps.sz != sz || fps.m == nil {
    36  		fps.sz = sz
    37  		fps.m = glutil.NewImage(imgW, imgH)
    38  	}
    39  	fps.mu.Unlock()
    40  
    41  	display := [7]byte{
    42  		4: 'F',
    43  		5: 'P',
    44  		6: 'S',
    45  	}
    46  	now := time.Now()
    47  	f := 0
    48  	if dur := now.Sub(lastDraw); dur > 0 {
    49  		f = int(time.Second / dur)
    50  	}
    51  	display[2] = '0' + byte((f/1e0)%10)
    52  	display[1] = '0' + byte((f/1e1)%10)
    53  	display[0] = '0' + byte((f/1e2)%10)
    54  	draw.Draw(fps.m.RGBA, fps.m.RGBA.Bounds(), image.White, image.Point{}, draw.Src)
    55  	for i, c := range display {
    56  		glyph := glyphs[c]
    57  		if len(glyph) != fontWidth*fontHeight {
    58  			continue
    59  		}
    60  		for y := 0; y < fontHeight; y++ {
    61  			for x := 0; x < fontWidth; x++ {
    62  				if glyph[fontWidth*y+x] == ' ' {
    63  					continue
    64  				}
    65  				fps.m.RGBA.SetRGBA((fontWidth+1)*i+x+1, y+1, color.RGBA{A: 0xff})
    66  			}
    67  		}
    68  	}
    69  
    70  	fps.m.Upload()
    71  	fps.m.Draw(
    72  		sz,
    73  		geom.Point{0, sz.HeightPt - imgH},
    74  		geom.Point{imgW, sz.HeightPt - imgH},
    75  		geom.Point{0, sz.HeightPt},
    76  		fps.m.RGBA.Bounds(),
    77  	)
    78  
    79  	lastDraw = now
    80  }
    81  
    82  const (
    83  	fontWidth  = 5
    84  	fontHeight = 7
    85  )
    86  
    87  // glyphs comes from the 6x10 fixed font from the plan9port:
    88  // https://github.com/9fans/plan9port/tree/master/font/fixed
    89  //
    90  // 6x10 becomes 5x7 because each glyph has a 1-pixel margin plus space for
    91  // descenders.
    92  //
    93  // Its README file says that those fonts were converted from XFree86, and are
    94  // in the public domain.
    95  var glyphs = [256]string{
    96  	'0': "" +
    97  		"  X  " +
    98  		" X X " +
    99  		"X   X" +
   100  		"X   X" +
   101  		"X   X" +
   102  		" X X " +
   103  		"  X  ",
   104  	'1': "" +
   105  		"  X  " +
   106  		" XX  " +
   107  		"X X  " +
   108  		"  X  " +
   109  		"  X  " +
   110  		"  X  " +
   111  		"XXXXX",
   112  	'2': "" +
   113  		" XXX " +
   114  		"X   X" +
   115  		"    X" +
   116  		"  XX " +
   117  		" X   " +
   118  		"X    " +
   119  		"XXXXX",
   120  	'3': "" +
   121  		"XXXXX" +
   122  		"    X" +
   123  		"   X " +
   124  		"  XX " +
   125  		"    X" +
   126  		"X   X" +
   127  		" XXX ",
   128  	'4': "" +
   129  		"   X " +
   130  		"  XX " +
   131  		" X X " +
   132  		"X  X " +
   133  		"XXXXX" +
   134  		"   X " +
   135  		"   X ",
   136  	'5': "" +
   137  		"XXXXX" +
   138  		"X    " +
   139  		"X XX " +
   140  		"XX  X" +
   141  		"    X" +
   142  		"X   X" +
   143  		" XXX ",
   144  	'6': "" +
   145  		"  XX " +
   146  		" X   " +
   147  		"X    " +
   148  		"X XX " +
   149  		"XX  X" +
   150  		"X   X" +
   151  		" XXX ",
   152  	'7': "" +
   153  		"XXXXX" +
   154  		"    X" +
   155  		"   X " +
   156  		"   X " +
   157  		"  X  " +
   158  		" X   " +
   159  		" X   ",
   160  	'8': "" +
   161  		" XXX " +
   162  		"X   X" +
   163  		"X   X" +
   164  		" XXX " +
   165  		"X   X" +
   166  		"X   X" +
   167  		" XXX ",
   168  	'9': "" +
   169  		" XXX " +
   170  		"X   X" +
   171  		"X  XX" +
   172  		" XX X" +
   173  		"    X" +
   174  		"   X " +
   175  		" XX  ",
   176  	'F': "" +
   177  		"XXXXX" +
   178  		"X    " +
   179  		"X    " +
   180  		"XXXX " +
   181  		"X    " +
   182  		"X    " +
   183  		"X    ",
   184  	'P': "" +
   185  		"XXXX " +
   186  		"X   X" +
   187  		"X   X" +
   188  		"XXXX " +
   189  		"X    " +
   190  		"X    " +
   191  		"X    ",
   192  	'S': "" +
   193  		" XXX " +
   194  		"X   X" +
   195  		"X    " +
   196  		" XXX " +
   197  		"    X" +
   198  		"X   X" +
   199  		" XXX ",
   200  }