From 7c6c406ff6ba494bcfac901ff99069524f5f01f6 Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Sat, 21 Nov 2026 20:19:47 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=AB=96=E5=B1=8F=E5=AE=9E=E4=BE=8B?= =?UTF-8?q?=E7=9A=84=E7=BC=A9=E7=95=A5=E5=9B=BE=E8=BD=AC=E6=AD=A3=20(Refs?= =?UTF-8?q?=20ModAVD/ModAVD#247)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 竖屏实例的共享内存帧是没转的横画布,缩略图侧着;按配置的方向顺时针转 90°(对应客体 ROTATION_270)。adb screencap 本来就是正的,不动。转失败退回 adb。 --- internal/ui/device.go | 9 +++++++- internal/ui/shmshot.go | 27 +++++++++++++++++++++++ internal/ui/shmshot_test.go | 43 +++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/internal/ui/device.go b/internal/ui/device.go index 43f105f..441f3c6 100644 --- a/internal/ui/device.go +++ b/internal/ui/device.go @@ -12,6 +12,7 @@ import ( "time" "modavd/internal/adb" + "modavd/internal/avd" "modavd/internal/emu" "modavd/internal/errs" ) @@ -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 } diff --git a/internal/ui/shmshot.go b/internal/ui/shmshot.go index c99dd9f..21c10fb 100644 --- a/internal/ui/shmshot.go +++ b/internal/ui/shmshot.go @@ -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) { diff --git a/internal/ui/shmshot_test.go b/internal/ui/shmshot_test.go index 6cbc96b..7e1cc58 100644 --- a/internal/ui/shmshot_test.go +++ b/internal/ui/shmshot_test.go @@ -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{