Ubuntu 上高效运行 Compton 的实用指南
一 安装与快速启用
- 在 Ubuntu 上安装 Compton:sudo apt update && sudo apt install compton。
- 生成默认配置:mkdir -p ~/.config && cp /etc/xdg/compton.conf ~/.config/compton.conf(若不存在)。
- 前台试运行观察日志:compton --config ~/.config/compton.conf --log-level 1。
- 无报错后设为自启动:在“启动应用程序”添加命令 compton --config ~/.config/compton.conf;或创建 systemd 用户服务(见下文)。
二 高效配置要点
- 选择渲染后端:优先使用 backend = glx(OpenGL),兼容性不佳时再退回 xrender;Wayland 会话下应使用相应 Wayland 合成器而非 Compton。
- 关闭高开销特效:将 shadow = false、禁用 bg_blur / screen_edge_blur、减少动态 opacity/alpha 规则,可显著降低 CPU/GPU 负载。
- 垂直同步策略:根据撕裂与输入延迟取舍,设置 vsync = true/false;笔记本可结合省电策略测试。
- 降低重绘成本:对无需透明的应用设置 opacity-rule = [ “90:class_g = ‘YourApp’” ] 等精细化规则,避免全局透明。
- 稳定性细节:遇到特定应用渲染异常时,可尝试 ignore_glx_glitz = true;多显示器高刷新率下优先保证主屏的同步与帧率稳定。
三 示例最小化配置
# ~/.config/compton.conf
backend = "glx"
vsync = true
shadow = false
fade-in = 0.2
fade-out = 0.2
focus-exclude = [ "class_g = 'slop'" ]
opacity-rule = [
"90:class_g = 'Firefox'",
"90:class_g = 'Alacritty'"
]
- 使用方式:compton --config ~/.config/compton.conf。
- 说明:关闭阴影与模糊、仅对少数应用启用透明度,能在观感与性能间取得平衡。
四 自启动与资源管理
- Systemd 用户服务(推荐):创建文件 ~/.config/systemd/user/compton.service
[Unit]
Description=Compton Window Composer
After=graphical-session.target
[Service]
ExecStart=/usr/bin/compton --config %h/.config/compton.conf --log-level 1
Restart=on-failure
[Install]
WantedBy=graphical-session.target
- 启用:systemctl --user daemon-reload && systemctl --user enable --now compton。
- 临时重载:killall compton && compton --config ~/.config/compton.conf。
- 资源限制:必要时用 cpulimit 限制占用,例如 cpulimit -l 50 -p $(pgrep compton)(仅在出现异常占用时启用)。
五 故障排查与性能验证
- 驱动与后端:确认 OpenGL 驱动正常(glxinfo | grep “OpenGL renderer”),再决定 backend;若 glx 异常,切换 xrender 并逐项关闭特效定位问题。
- 日志与对比:启动时加 –log-level 1 观察警告;用 compton --benchmark 做基线测试,逐项开关特效比对帧率与 CPU 占用。
- 监控工具:结合 top / htop / vmstat / pidstat 观察 compton 进程资源变化,验证优化成效。