| OLD | NEW |
| (Empty) |
| 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 // Package debug provides GL-based debugging tools for apps. | |
| 6 package debug // import "golang.org/x/mobile/app/debug" | |
| 7 | |
| 8 import ( | |
| 9 "fmt" | |
| 10 "image" | |
| 11 "image/draw" | |
| 12 "log" | |
| 13 "math" | |
| 14 "sync" | |
| 15 "time" | |
| 16 | |
| 17 "code.google.com/p/freetype-go/freetype" | |
| 18 "golang.org/x/mobile/font" | |
| 19 "golang.org/x/mobile/geom" | |
| 20 "golang.org/x/mobile/gl/glutil" | |
| 21 ) | |
| 22 | |
| 23 var lastDraw = time.Now() | |
| 24 | |
| 25 var monofont = freetype.NewContext() | |
| 26 | |
| 27 var fps struct { | |
| 28 sync.Once | |
| 29 *glutil.Image | |
| 30 } | |
| 31 | |
| 32 // TODO(crawshaw): It looks like we need a gl.RegisterInit feature. | |
| 33 // TODO(crawshaw): The gldebug mode needs to complain loudly when GL functions | |
| 34 // are called before init, because often they fail silently. | |
| 35 func fpsInit() { | |
| 36 b := font.Monospace() | |
| 37 f, err := freetype.ParseFont(b) | |
| 38 if err != nil { | |
| 39 panic(err) | |
| 40 } | |
| 41 monofont.SetFont(f) | |
| 42 monofont.SetSrc(image.Black) | |
| 43 monofont.SetHinting(freetype.FullHinting) | |
| 44 | |
| 45 toPx := func(x geom.Pt) int { return int(math.Ceil(float64(geom.Pt(x).Px
()))) } | |
| 46 fps.Image = glutil.NewImage(toPx(50), toPx(12)) | |
| 47 monofont.SetDst(fps.Image.RGBA) | |
| 48 monofont.SetClip(fps.Bounds()) | |
| 49 monofont.SetDPI(72 * float64(geom.PixelsPerPt)) | |
| 50 monofont.SetFontSize(12) | |
| 51 } | |
| 52 | |
| 53 // DrawFPS draws the per second framerate in the bottom-left of the screen. | |
| 54 func DrawFPS() { | |
| 55 fps.Do(fpsInit) | |
| 56 | |
| 57 now := time.Now() | |
| 58 diff := now.Sub(lastDraw) | |
| 59 str := fmt.Sprintf("%.0f FPS", float32(time.Second)/float32(diff)) | |
| 60 draw.Draw(fps.Image, fps.Image.Rect, image.White, image.Point{}, draw.Sr
c) | |
| 61 | |
| 62 ftpt12 := freetype.Pt(0, int(12*geom.PixelsPerPt)) | |
| 63 if _, err := monofont.DrawString(str, ftpt12); err != nil { | |
| 64 log.Printf("DrawFPS: %v", err) | |
| 65 return | |
| 66 } | |
| 67 | |
| 68 fps.Upload() | |
| 69 fps.Draw( | |
| 70 geom.Point{0, geom.Height - 12}, | |
| 71 geom.Point{50, geom.Height - 12}, | |
| 72 geom.Point{0, geom.Height}, | |
| 73 fps.Bounds(), | |
| 74 ) | |
| 75 | |
| 76 lastDraw = now | |
| 77 } | |
| OLD | NEW |