Perf: Optimize macOS screen capture with CGDisplayStream

Core optimization:
- Use CGDisplayStream instead of per-frame CGDisplayCreateImage
- Push model: CPU sleeps when screen is static (condition_variable wait)
- IOSurface capture avoids expensive image creation per frame
- ~47% CPU reduction during active remote desktop (45% → 24%)

Additional optimizations:
- vImageVerticalReflect (SIMD) replaces manual row-by-row flip
- Cache CGColorSpaceRef to avoid per-frame creation/release
- Cache tempBuffer to avoid per-frame memory allocation
- Throttle getCursorTypeIndex to 250ms (Accessibility API is expensive)

Bug fixes:
- Fix unreliable screen capture permission check (use actual capture test)
- Improve permission logging

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
yuanyuanxiang
2026-05-03 23:18:30 +02:00
parent b732f841d0
commit 92f3df8464
7 changed files with 483 additions and 43 deletions

103
macos/install.sh Normal file
View File

@@ -0,0 +1,103 @@
#!/bin/bash
# macOS Ghost Client 安装脚本
# 用法: ./install.sh [ghost路径]
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
GHOST_SRC="${1:-$SCRIPT_DIR/build/bin/ghost}"
GHOST_DST="/usr/local/bin/ghost"
PLIST_DST="/Library/LaunchDaemons/com.ghost.client.plist"
echo "=== Ghost Client 安装程序 ==="
echo "源文件: $GHOST_SRC"
# 检查源文件
if [ ! -f "$GHOST_SRC" ]; then
echo ""
echo "错误: 找不到 $GHOST_SRC"
echo ""
echo "请先编译: ./build.sh"
echo ""
echo "或指定路径: $0 <ghost可执行文件路径>"
exit 1
fi
set -e
# 1. 停止旧服务(只停止安装目录的,不影响调试目录)
echo "[1/6] 停止旧服务..."
sudo launchctl unload "$PLIST_DST" 2>/dev/null || true
sudo pkill -9 -f "$GHOST_DST" 2>/dev/null || true
# 2. 复制程序
echo "[2/6] 安装程序到 $GHOST_DST..."
sudo cp "$GHOST_SRC" "$GHOST_DST"
sudo chmod +x "$GHOST_DST"
# 3. 清除隔离属性
echo "[3/6] 清除隔离属性..."
sudo xattr -cr "$GHOST_DST"
# 4. 签名
echo "[4/6] 签名程序..."
sudo codesign --force --deep --sign - "$GHOST_DST"
# 5. 创建 launchd plist
echo "[5/6] 创建 launchd 服务..."
sudo tee "$PLIST_DST" > /dev/null << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.ghost.client</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/ghost</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/var/log/ghost.log</string>
<key>StandardErrorPath</key>
<string>/var/log/ghost.log</string>
</dict>
</plist>
EOF
sudo chown root:wheel "$PLIST_DST"
sudo chmod 644 "$PLIST_DST"
# 6. 完成
echo "[6/6] 安装完成!"
echo ""
echo "========================================"
echo "重要: 首次运行需要授权系统权限"
echo "========================================"
echo ""
echo "请执行以下步骤:"
echo ""
echo "1. 手动运行以触发权限请求:"
echo " $GHOST_DST"
echo ""
echo "2. 授权后按 Ctrl+C 停止程序(权限需重启生效)"
echo ""
echo "3. 启动服务:"
echo " sudo launchctl load $PLIST_DST"
echo ""
echo "如未弹出授权对话框,手动添加:"
echo " 系统设置 > 隐私与安全性 > 屏幕录制 > 添加 ghost"
echo " 系统设置 > 隐私与安全性 > 辅助功能 > 添加 ghost"
echo ""
echo "常用命令:"
echo " 启动: sudo launchctl start com.ghost.client"
echo " 停止: sudo launchctl stop com.ghost.client"
echo " 卸载: sudo launchctl unload $PLIST_DST"
echo " 日志: tail -f /var/log/ghost.log"
echo ""