Skip to content

Commit

Permalink
feat: 在有小窗时自动暂停fas
Browse files Browse the repository at this point in the history
  • Loading branch information
shadow3aaa committed Jul 8, 2024
1 parent e5ba989 commit 622b7d5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
11 changes: 8 additions & 3 deletions src/framework/scheduler/looper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub struct Looper {
extension: Extension,
mode: Mode,
controller: Controller,
topapp_watcher: TimedWatcher,
windows_watcher: TimedWatcher,
cleaner: Cleaner,
buffers: Buffers,
state: State,
Expand All @@ -92,7 +92,7 @@ impl Looper {
extension,
mode: Mode::Balance,
controller,
topapp_watcher: TimedWatcher::new(),
windows_watcher: TimedWatcher::new(),
cleaner: Cleaner::new(),
buffers: Buffers::new(),
state: State::NotWorking,
Expand Down Expand Up @@ -123,6 +123,11 @@ impl Looper {
#[cfg(feature = "use_ebpf")]
let fas_data = self.recv_message();

if self.windows_watcher.visible_freeform_window() {
self.disable_fas();
continue;
}

if let Some(data) = fas_data {
self.buffer_update(&data);
self.do_policy(target_fps);
Expand Down Expand Up @@ -172,7 +177,7 @@ impl Looper {
fn update_analyzer(&mut self) -> Result<()> {
use crate::framework::utils::get_process_name;

for pid in self.topapp_watcher.top_apps() {
for pid in self.windows_watcher.topapp_pids().iter().copied() {
let pkg = get_process_name(pid)?;
if self.config.need_fas(&pkg) {
self.analyzer.attach_app(pid)?;
Expand Down
4 changes: 2 additions & 2 deletions src/framework/scheduler/looper/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const DELAY_TIME: Duration = Duration::from_secs(3);
impl Looper {
pub fn retain_topapp(&mut self) {
self.buffers.retain(|pid, buffer| {
if self.topapp_watcher.is_topapp(*pid) {
if self.windows_watcher.topapp_pids().contains(pid) {
true
} else {
#[cfg(feature = "use_ebpf")]
Expand Down Expand Up @@ -78,7 +78,7 @@ impl Looper {
}

pub fn buffer_update(&mut self, d: &FasData) {
if !self.topapp_watcher.is_topapp(d.pid) || d.frametime.is_zero() {
if !self.windows_watcher.topapp_pids().contains(&d.pid) || d.frametime.is_zero() {
return;
}

Expand Down
62 changes: 32 additions & 30 deletions src/framework/scheduler/topapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,21 @@ use dumpsys_rs::Dumpsys;

const REFRESH_TIME: Duration = Duration::from_secs(1);

struct Insider {
windows_dumper: Dumpsys,
cache: Vec<i32>,
last_refresh: Instant,
#[derive(Default)]
struct WindowsInfo {
pub visible_freeform_window: bool,
pub pids: Vec<i32>,
}

impl Insider {
pub fn new() -> Self {
Self {
windows_dumper: Dumpsys::new("window").unwrap(),
cache: Vec::new(),
last_refresh: Instant::now(),
}
}
impl WindowsInfo {
pub fn new(dump: String) -> Self {
let pids = Self::parse_top_app(&dump);
let visible_freeform_window = dump.contains("freeform");

pub fn pids(&mut self) -> &Vec<i32> {
if self.last_refresh.elapsed() > REFRESH_TIME {
self.cache = self.get_top_pids().unwrap_or_default();
self.last_refresh = Instant::now();
Self {
visible_freeform_window,
pids,
}

&self.cache
}

fn get_top_pids(&self) -> Option<Vec<i32>> {
let dump = self.windows_dumper.dump(&["visible-apps"]).ok()?;
Some(Self::parse_top_app(&dump))
}

fn parse_top_app(dump: &str) -> Vec<i32> {
Expand All @@ -58,22 +46,36 @@ impl Insider {
}

pub struct TimedWatcher {
insider: Insider,
windows_dumper: Dumpsys,
cache: WindowsInfo,
last_refresh: Instant,
}

impl TimedWatcher {
pub fn new() -> Self {
Self {
insider: Insider::new(),
windows_dumper: Dumpsys::new("window").unwrap(),
cache: WindowsInfo::default(),
last_refresh: Instant::now(),
}
}

pub fn is_topapp(&mut self, pid: i32) -> bool {
self.insider.pids().contains(&pid)
pub fn topapp_pids(&mut self) -> &Vec<i32> {
&self.cache().pids
}

pub fn visible_freeform_window(&mut self) -> bool {
self.cache().visible_freeform_window
}

#[allow(dead_code)]
pub fn top_apps(&mut self) -> impl Iterator<Item = i32> + '_ {
self.insider.pids().iter().copied()
fn cache(&mut self) -> &WindowsInfo {
if self.last_refresh.elapsed() > REFRESH_TIME {
let dump = self.windows_dumper.dump(&["visible-apps"]).unwrap();
self.cache = WindowsInfo::new(dump);

self.last_refresh = Instant::now();
}

&self.cache
}
}

0 comments on commit 622b7d5

Please sign in to comment.