text
stringlengths
0
1.47k
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-ch
kept.restore(f, mem, v)
os.Exit(1)
}()
}
// decodeCost times what one streamed frame costs the device, in the three parts it arrives in:
// decoding the JPEG, scaling it to the panel, and putting the frame up. They are reported apart
// because they scale with different things — the decode with the source's pixels, the write with the
// panel's — so a stream can trade one against the other by sending smaller pictures.
//
// Every file is scaled to fill the whole panel, which is what a video page has to do, so the write is
// the same for all of them and what differs is the decode and the scale. Decoding is timed per file,
// on that file, which is the whole point of separating them.
//
// Note which write this is: fbprobe's own blit writes every pixel and skips nothing, where the daemon's
// Present compares each row against its shadow first. So there is no skipping to time here, and a frame
// is timed whether or not the one before it was the same picture.
//
// The write is fbprobe's own rotate, which is the same class of work as the daemon's: a per-pixel
// pass over the canvas packing each pixel for the panel. It is the closest this can come to the
// daemon's Present without being the daemon.
func decodeCost(f *os.File, mem []byte, v varInfo, line, panelW, panelH int, dir string, iters int) error {
ents, err := os.ReadDir(dir)
if err != nil {
return err
}
var files []string
for _, e := range ents {
if e.IsDir() {
continue
}
if n := strings.ToLower(e.Name()); strings.HasSuffix(n, ".jpg") || strings.HasSuffix(n, ".jpeg") {
files = append(files, filepath.Join(dir, e.Name()))
}
}
if len(files) == 0 {
return fmt.Errorf("no .jpg files in %s", dir)
}
sort.Strings(files)
if iters < 1 {
iters = 1
}
w, h := panelH, panelW // landscape, the way it is composed
canvas := image.NewRGBA(image.Rect(0, 0, w, h))
fmt.Printf("decode: %dx%d panel, %d frame(s) from %s, %d pass(es), frames used in turn\n\n",
w, h, len(files), dir, iters)
var best float64
for _, path := range files {
raw, err := os.ReadFile(path)
if err != nil {
return err
}
cfg, err := jpeg.DecodeConfig(bytes.NewReader(raw))
if err != nil {
return fmt.Errorf("%s: %w", path, err)
}
var decodes, scales, writes, totals []time.Duration
for i := 0; i < iters; i++ {
t0 := time.Now()
img, err := jpeg.Decode(bytes.NewReader(raw))
t1 := time.Now()
if err != nil {
return fmt.Errorf("%s: %w", path, err)
}
xdraw.NearestNeighbor.Scale(canvas, canvas.Bounds(), img, img.Bounds(), draw.Src, nil)
t2 := time.Now()
blit(mem, canvas, line, panelW, panelH, v)
v.Xoffset, v.Yoffset = 0, 0
if err := ioctl(f.Fd(), fbioPanDisplay, unsafe.Pointer(&v)); err != nil {
return fmt.Errorf("FBIOPAN_DISPLAY: %w", err)
}
t3 := time.Now()
decodes = append(decodes, t1.Sub(t0))
scales = append(scales, t2.Sub(t1))
writes = append(writes, t3.Sub(t2))
totals = append(totals, t3.Sub(t0))
}
d, _, _, dmax := spread(append([]time.Duration(nil), decodes...))
_, s50, _, smax := spread(append([]time.Duration(nil), scales...))
_, w50, _, wmax := spread(append([]time.Duration(nil), writes...))
_, t50, _, _ := spread(append([]time.Duration(nil), totals...))
fps := 1 / t50.Seconds()
if fps > best {
best = fps
}
fmt.Printf("%-16s %4dx%-4d %7d B\n", filepath.Base(path), cfg.Width, cfg.Height, len(raw))
fmt.Printf(" decode %v (max %v) scale %v (max %v)\n",
d.Round(time.Millisecond), dmax.Round(time.Millisecond), s50.Round(time.Millisecond), smax.Round(time.Millisecond))
fmt.Printf(" write+pan %v (max %v) total %v -> %.1f frames/s (%.2f MB/s of pictures)\n\n",
w50.Round(time.Millisecond), wmax.Round(time.Millisecond), t50.Round(time.Millisecond), fps, float64(len(raw))*fps/1e6)
}
if best > 0 {