Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/ui/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"modavd/internal/adb"
"modavd/internal/avd"
"modavd/internal/emu"
"modavd/internal/errs"
)
Expand Down Expand Up @@ -154,7 +155,13 @@ func (s *server) captureOnce(ctx context.Context, i int, skipShm bool) (data []b
}
if !skipShm && s.frame != nil {
if png, ferr := s.frame(s.root.Dir, s.mgr.Serial(i)); ferr == nil && bytes.HasPrefix(png, pngSignature) {
return png, shotSourceShm, false, nil
// a portrait instance's shared frame is the wide canvas, not turned (adb screencap is)
if in, gerr := avd.Get(s.root, i); gerr == nil && in.Orientation() == avd.OrientationPortrait {
png, ferr = turnPortrait(png)
}
if ferr == nil {
return png, shotSourceShm, false, nil
}
}
shmFailed = true
}
Expand Down
27 changes: 27 additions & 0 deletions internal/ui/shmshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ import (
"image/png"
)

// turnPortrait turns a landscape PNG 90 degrees clockwise, undoing the guest's ROTATION_270 (user_rotation=3) of a
// portrait instance: the emulator's shared frame is the wide canvas as is, with the picture's top at the canvas's
// left edge, while adb screencap already comes out upright. A frame that is not wider than tall is returned as is.
func turnPortrait(data []byte) ([]byte, error) {
src, err := png.Decode(bytes.NewReader(data))
if err != nil {
return nil, err
}
b := src.Bounds()
w, h := b.Dx(), b.Dy()
if w <= h {
return data, nil
}
dst := image.NewRGBA(image.Rect(0, 0, h, w))
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
dst.Set(h-1-y, x, src.At(b.Min.X+x, b.Min.Y+y))
}
}
var out bytes.Buffer
enc := png.Encoder{CompressionLevel: png.BestSpeed}
if err := enc.Encode(&out, dst); err != nil {
return nil, err
}
return out.Bytes(), nil
}

// bgraPNG encodes a packed top-down BGRA frame (stride bytes per row) as PNG. The emulator's alpha is not meaningful
// for a screen, so the picture is written opaque.
func bgraPNG(pix []byte, width, height, stride int) ([]byte, error) {
Expand Down
43 changes: 43 additions & 0 deletions internal/ui/shmshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,53 @@ package ui

import (
"bytes"
"image"
"image/color"
"image/png"
"testing"
)

func TestTurnPortrait(t *testing.T) {
enc := func(w, h int, set map[[2]int]color.RGBA) []byte {
img := image.NewRGBA(image.Rect(0, 0, w, h))
for p, c := range set {
img.SetRGBA(p[0], p[1], c)
}
var b bytes.Buffer
if err := png.Encode(&b, img); err != nil {
t.Fatal(err)
}
return b.Bytes()
}
red, green := color.RGBA{255, 0, 0, 255}, color.RGBA{0, 255, 0, 255}
// 3x2 canvas: red at the top-left corner, green at the top-right one
data := enc(3, 2, map[[2]int]color.RGBA{{0, 0}: red, {2, 0}: green})
out, err := turnPortrait(data)
if err != nil {
t.Fatal(err)
}
img, err := png.Decode(bytes.NewReader(out))
if err != nil {
t.Fatal(err)
}
if b := img.Bounds(); b.Dx() != 2 || b.Dy() != 3 {
t.Fatalf("bounds %v", b)
}
// clockwise: top-left goes to top-right, top-right to bottom-right
at := func(x, y int) color.RGBA { return color.RGBAModel.Convert(img.At(x, y)).(color.RGBA) }
if at(1, 0) != red || at(1, 2) != green {
t.Errorf("red at (1,0) = %v, green at (1,2) = %v", at(1, 0), at(1, 2))
}
// already upright: untouched
tall := enc(2, 3, nil)
if got, err := turnPortrait(tall); err != nil || !bytes.Equal(got, tall) {
t.Errorf("portrait frame changed: %v", err)
}
if _, err := turnPortrait([]byte("garbage")); err == nil {
t.Error("garbage accepted")
}
}

func TestBGRAPNG(t *testing.T) {
// 2x2, stride 12 (4 bytes of padding per row): the padding must not leak into the picture
pix := []byte{
Expand Down
Loading