From ee5231f567a354e9320ae1432695a30cea9de9c7 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sat, 15 Oct 2022 23:19:22 -0700 Subject: [PATCH] Only process visualizations when visible Stop visualization processing when the host window is completely occluded, thus reducing background CPU usage levels significantly Signed-off-by: Christopher Snowhill --- SpectrumViewCG.m | 115 +++++++++++++++++++++------------ Visualization/SpectrumViewSK.m | 103 ++++++++++++++++++----------- 2 files changed, 141 insertions(+), 77 deletions(-) diff --git a/SpectrumViewCG.m b/SpectrumViewCG.m index a770b2e73..e43787b92 100644 --- a/SpectrumViewCG.m +++ b/SpectrumViewCG.m @@ -29,6 +29,7 @@ extern NSString *CogPlaybackDidStopNotficiation; BOOL isListening; BOOL observersAdded; BOOL isFullView; + BOOL isOccluded; NSRect initFrame; @@ -58,15 +59,32 @@ extern NSString *CogPlaybackDidStopNotficiation; } - (void)updateVisListening { - if(self.isListening && (![self visibleInWindow] || paused || stopped)) { + if(self.isListening && (![self visibleInWindow] || isOccluded || paused || stopped)) { [self stopTimer]; self.isListening = NO; - } else if(!self.isListening && ([self visibleInWindow] && !stopped && !paused)) { + } else if(!self.isListening && ([self visibleInWindow] && !isOccluded && !stopped && !paused)) { [self startTimer]; self.isListening = YES; } } +- (void)setOccluded:(BOOL)occluded { + isOccluded = occluded; + [self updateVisListening]; +} + +- (void)windowChangedOcclusionState:(NSNotification *)notification { + if([notification object] == self.window) { + BOOL curOccluded = !self.window; + if(!curOccluded) { + curOccluded = !(self.window.occlusionState & NSWindowOcclusionStateVisible); + } + if(curOccluded != isOccluded) { + [self setOccluded:curOccluded]; + } + } +} + - (void)setup { visController = [NSClassFromString(@"VisualizationController") sharedController]; timer = nil; @@ -118,31 +136,39 @@ extern NSString *CogPlaybackDidStopNotficiation; - (void)addObservers { if(!observersAdded) { - [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumBarColor" options:0 context:kSpectrumViewCGContext]; - [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumDotColor" options:0 context:kSpectrumViewCGContext]; + NSUserDefaultsController *sharedUserDefaultsController = [NSUserDefaultsController sharedUserDefaultsController]; + [sharedUserDefaultsController addObserver:self forKeyPath:@"values.spectrumBarColor" options:0 context:kSpectrumViewCGContext]; + [sharedUserDefaultsController addObserver:self forKeyPath:@"values.spectrumDotColor" options:0 context:kSpectrumViewCGContext]; [self addObserver:self forKeyPath:@"self.window.visible" options:0 context:kSpectrumViewCGContext]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(colorsDidChange:) - name:NSSystemColorsDidChangeNotification - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(playbackDidBegin:) - name:CogPlaybackDidBeginNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(playbackDidPause:) - name:CogPlaybackDidPauseNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(playbackDidResume:) - name:CogPlaybackDidResumeNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(playbackDidStop:) - name:CogPlaybackDidStopNotficiation - object:nil]; + NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; + [defaultCenter addObserver:self + selector:@selector(colorsDidChange:) + name:NSSystemColorsDidChangeNotification + object:nil]; + [defaultCenter addObserver:self + selector:@selector(playbackDidBegin:) + name:CogPlaybackDidBeginNotficiation + object:nil]; + [defaultCenter addObserver:self + selector:@selector(playbackDidPause:) + name:CogPlaybackDidPauseNotficiation + object:nil]; + [defaultCenter addObserver:self + selector:@selector(playbackDidResume:) + name:CogPlaybackDidResumeNotficiation + object:nil]; + [defaultCenter addObserver:self + selector:@selector(playbackDidStop:) + name:CogPlaybackDidStopNotficiation + object:nil]; + + [defaultCenter addObserver:self + selector:@selector(windowChangedOcclusionState:) + name:NSWindowDidChangeOcclusionStateNotification + object:nil]; + observersAdded = YES; } } @@ -156,26 +182,33 @@ extern NSString *CogPlaybackDidStopNotficiation; - (void)removeObservers { if(observersAdded) { - [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumBarColor" context:kSpectrumViewCGContext]; - [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumDotColor" context:kSpectrumViewCGContext]; + NSUserDefaultsController *sharedUserDefaultsController = [NSUserDefaultsController sharedUserDefaultsController]; + [sharedUserDefaultsController removeObserver:self forKeyPath:@"values.spectrumBarColor" context:kSpectrumViewCGContext]; + [sharedUserDefaultsController removeObserver:self forKeyPath:@"values.spectrumDotColor" context:kSpectrumViewCGContext]; [self removeObserver:self forKeyPath:@"self.window.visible" context:kSpectrumViewCGContext]; - [[NSNotificationCenter defaultCenter] removeObserver:self - name:NSSystemColorsDidChangeNotification - object:nil]; - [[NSNotificationCenter defaultCenter] removeObserver:self - name:CogPlaybackDidBeginNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] removeObserver:self - name:CogPlaybackDidPauseNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] removeObserver:self - name:CogPlaybackDidResumeNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] removeObserver:self - name:CogPlaybackDidStopNotficiation - object:nil]; + NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; + [defaultCenter removeObserver:self + name:NSSystemColorsDidChangeNotification + object:nil]; + [defaultCenter removeObserver:self + name:CogPlaybackDidBeginNotficiation + object:nil]; + [defaultCenter removeObserver:self + name:CogPlaybackDidPauseNotficiation + object:nil]; + [defaultCenter removeObserver:self + name:CogPlaybackDidResumeNotficiation + object:nil]; + [defaultCenter removeObserver:self + name:CogPlaybackDidStopNotficiation + object:nil]; + + [defaultCenter removeObserver:self + name:NSWindowDidChangeOcclusionStateNotification + object:nil]; + observersAdded = NO; } } diff --git a/Visualization/SpectrumViewSK.m b/Visualization/SpectrumViewSK.m index 59e3768cd..4b36b3816 100644 --- a/Visualization/SpectrumViewSK.m +++ b/Visualization/SpectrumViewSK.m @@ -33,6 +33,7 @@ extern NSString *CogPlaybackDidStopNotficiation; BOOL bandsReset; BOOL cameraControlEnabled; BOOL observersAdded; + BOOL isOccluded; NSColor *backgroundColor; ddb_analyzer_t _analyzer; @@ -90,15 +91,32 @@ extern NSString *CogPlaybackDidStopNotficiation; } - (void)updateVisListening { - if(self.isListening && (![self visibleInWindow] || paused || stopped)) { + if(self.isListening && (![self visibleInWindow] || isOccluded || paused || stopped)) { [self stopTimer]; self.isListening = NO; - } else if(!self.isListening && ([self visibleInWindow] && !stopped && !paused)) { + } else if(!self.isListening && ([self visibleInWindow] && !isOccluded && !stopped && !paused)) { [self startTimer]; self.isListening = YES; } } +- (void)setOccluded:(BOOL)occluded { + isOccluded = occluded; + [self updateVisListening]; +} + +- (void)windowChangedOcclusionState:(NSNotification *)notification { + if([notification object] == self.window) { + BOOL curOccluded = !self.window; + if(!curOccluded) { + curOccluded = !(self.window.occlusionState & NSWindowOcclusionStateVisible); + } + if(curOccluded != isOccluded) { + [self setOccluded:curOccluded]; + } + } +} + - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change @@ -209,28 +227,35 @@ extern NSString *CogPlaybackDidStopNotficiation; - (void)addObservers { if(!observersAdded) { - [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumProjectionMode" options:0 context:kSpectrumViewSKContext]; - [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumBarColor" options:0 context:kSpectrumViewSKContext]; - [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.spectrumDotColor" options:0 context:kSpectrumViewSKContext]; + NSUserDefaultsController *sharedUserDefaultsController = [NSUserDefaultsController sharedUserDefaultsController]; + [sharedUserDefaultsController addObserver:self forKeyPath:@"values.spectrumProjectionMode" options:0 context:kSpectrumViewSKContext]; + [sharedUserDefaultsController addObserver:self forKeyPath:@"values.spectrumBarColor" options:0 context:kSpectrumViewSKContext]; + [sharedUserDefaultsController addObserver:self forKeyPath:@"values.spectrumDotColor" options:0 context:kSpectrumViewSKContext]; [self addObserver:self forKeyPath:@"self.window.visible" options:0 context:kSpectrumViewSKContext]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(playbackDidBegin:) - name:CogPlaybackDidBeginNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(playbackDidPause:) - name:CogPlaybackDidPauseNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(playbackDidResume:) - name:CogPlaybackDidResumeNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(playbackDidStop:) - name:CogPlaybackDidStopNotficiation - object:nil]; + NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; + [defaultCenter addObserver:self + selector:@selector(playbackDidBegin:) + name:CogPlaybackDidBeginNotficiation + object:nil]; + [defaultCenter addObserver:self + selector:@selector(playbackDidPause:) + name:CogPlaybackDidPauseNotficiation + object:nil]; + [defaultCenter addObserver:self + selector:@selector(playbackDidResume:) + name:CogPlaybackDidResumeNotficiation + object:nil]; + [defaultCenter addObserver:self + selector:@selector(playbackDidStop:) + name:CogPlaybackDidStopNotficiation + object:nil]; + + [defaultCenter addObserver:self + selector:@selector(windowChangedOcclusionState:) + name:NSWindowDidChangeOcclusionStateNotification + object:nil]; observersAdded = YES; } @@ -245,24 +270,30 @@ extern NSString *CogPlaybackDidStopNotficiation; - (void)removeObservers { if(observersAdded) { - [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumProjectionMode" context:kSpectrumViewSKContext]; - [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumBarColor" context:kSpectrumViewSKContext]; - [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeyPath:@"values.spectrumDotColor" context:kSpectrumViewSKContext]; + NSUserDefaultsController *sharedUserDefaultsController = [NSUserDefaultsController sharedUserDefaultsController]; + [sharedUserDefaultsController removeObserver:self forKeyPath:@"values.spectrumProjectionMode" context:kSpectrumViewSKContext]; + [sharedUserDefaultsController removeObserver:self forKeyPath:@"values.spectrumBarColor" context:kSpectrumViewSKContext]; + [sharedUserDefaultsController removeObserver:self forKeyPath:@"values.spectrumDotColor" context:kSpectrumViewSKContext]; [self removeObserver:self forKeyPath:@"self.window.visible" context:kSpectrumViewSKContext]; - [[NSNotificationCenter defaultCenter] removeObserver:self - name:CogPlaybackDidBeginNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] removeObserver:self - name:CogPlaybackDidPauseNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] removeObserver:self - name:CogPlaybackDidResumeNotficiation - object:nil]; - [[NSNotificationCenter defaultCenter] removeObserver:self - name:CogPlaybackDidStopNotficiation - object:nil]; + NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; + [defaultCenter removeObserver:self + name:CogPlaybackDidBeginNotficiation + object:nil]; + [defaultCenter removeObserver:self + name:CogPlaybackDidPauseNotficiation + object:nil]; + [defaultCenter removeObserver:self + name:CogPlaybackDidResumeNotficiation + object:nil]; + [defaultCenter removeObserver:self + name:CogPlaybackDidStopNotficiation + object:nil]; + + [defaultCenter removeObserver:self + name:NSWindowDidChangeOcclusionStateNotification + object:nil]; observersAdded = NO; }