diff --git a/desmume/src/frontend/cocoa/cocoa_GPU.h b/desmume/src/frontend/cocoa/cocoa_GPU.h index 70ce3bc78..972e6889a 100644 --- a/desmume/src/frontend/cocoa/cocoa_GPU.h +++ b/desmume/src/frontend/cocoa/cocoa_GPU.h @@ -44,7 +44,7 @@ typedef std::map DisplayLinkFlushTimeLimitMap; GPUClientFetchObject *GPUFetchObject; pthread_rwlock_t *_rwlockFramebuffer[2]; pthread_mutex_t *_mutexOutputList; - pthread_mutex_t _mutexFlushVideo; + pthread_mutex_t _mutexDisplayLinkLists; NSMutableArray *_cdsOutputList; volatile int32_t numberViewsUsingDirectToCPUFiltering; @@ -70,7 +70,6 @@ typedef std::map DisplayLinkFlushTimeLimitMap; - (void) finishAllDisplayViewsAtIndex:(const u8)bufferIndex; - (void) flushAllDisplaysOnDisplayLink:(CVDisplayLinkRef)displayLink timeStamp:(const CVTimeStamp *)timeStamp; -- (BOOL) isDisplayLinkRunningUsingID:(CGDirectDisplayID)displayID; - (void) displayLinkStartUsingID:(CGDirectDisplayID)displayID; - (void) displayLinkListUpdate; diff --git a/desmume/src/frontend/cocoa/cocoa_GPU.mm b/desmume/src/frontend/cocoa/cocoa_GPU.mm index f3458f6e1..59252b191 100644 --- a/desmume/src/frontend/cocoa/cocoa_GPU.mm +++ b/desmume/src/frontend/cocoa/cocoa_GPU.mm @@ -876,7 +876,7 @@ public: pthread_rwlock_init(_rwlockFramebuffer[0], NULL); pthread_rwlock_init(_rwlockFramebuffer[1], NULL); - pthread_mutex_init(&_mutexFlushVideo, NULL); + pthread_mutex_init(&_mutexDisplayLinkLists, NULL); GPUFetchObject = nil; _mutexOutputList = NULL; @@ -911,24 +911,27 @@ public: pthread_cond_destroy(&_condSignalFetch); pthread_mutex_destroy(&_mutexFetchExecute); - pthread_mutex_lock(&_mutexFlushVideo); + pthread_mutex_lock(&_mutexDisplayLinkLists); - for (DisplayLinksActiveMap::iterator it = _displayLinksActiveList.begin(); it != _displayLinksActiveList.end(); ++it) + while (_displayLinksActiveList.size() > 0) { + DisplayLinksActiveMap::iterator it = _displayLinksActiveList.begin(); CGDirectDisplayID displayID = it->first; CVDisplayLinkRef displayLinkRef = it->second; + if (CVDisplayLinkIsRunning(displayLinkRef)) { CVDisplayLinkStop(displayLinkRef); - CVDisplayLinkRelease(displayLinkRef); } + CVDisplayLinkRelease(displayLinkRef); + _displayLinksActiveList.erase(displayID); _displayLinkFlushTimeList.erase(displayID); } - pthread_mutex_unlock(&_mutexFlushVideo); - pthread_mutex_destroy(&_mutexFlushVideo); + pthread_mutex_unlock(&_mutexDisplayLinkLists); + pthread_mutex_destroy(&_mutexDisplayLinkLists); pthread_mutex_t *currentMutex = _mutexOutputList; @@ -1066,8 +1069,6 @@ public: pthread_mutex_unlock(currentMutex); } - pthread_mutex_lock(&_mutexFlushVideo); - if (didFlushOccur) { // Set the new time limit to 8 seconds after the current time. @@ -1077,45 +1078,25 @@ public: { CVDisplayLinkStop(displayLink); } - - pthread_mutex_unlock(&_mutexFlushVideo); -} - -- (BOOL) isDisplayLinkRunningUsingID:(CGDirectDisplayID)displayID -{ - CVDisplayLinkRef displayLink = NULL; - - pthread_mutex_lock(&_mutexFlushVideo); - - if (_displayLinksActiveList.find(displayID) != _displayLinksActiveList.end()) - { - displayLink = _displayLinksActiveList[displayID]; - } - - const BOOL isRunning = ( (displayLink != NULL) && CVDisplayLinkIsRunning(displayLink) ) ? YES : NO; - - pthread_mutex_unlock(&_mutexFlushVideo); - - return isRunning; } - (void) displayLinkStartUsingID:(CGDirectDisplayID)displayID { CVDisplayLinkRef displayLink = NULL; - pthread_mutex_lock(&_mutexFlushVideo); + pthread_mutex_lock(&_mutexDisplayLinkLists); if (_displayLinksActiveList.find(displayID) != _displayLinksActiveList.end()) { displayLink = _displayLinksActiveList[displayID]; } - if (displayLink != NULL) + if ( (displayLink != NULL) && !CVDisplayLinkIsRunning(displayLink) ) { CVDisplayLinkStart(displayLink); } - pthread_mutex_unlock(&_mutexFlushVideo); + pthread_mutex_unlock(&_mutexDisplayLinkLists); } - (void) displayLinkListUpdate @@ -1124,7 +1105,7 @@ public: NSArray *screenList = [NSScreen screens]; std::set screenActiveDisplayIDsList; - pthread_mutex_lock(&_mutexFlushVideo); + pthread_mutex_lock(&_mutexDisplayLinkLists); // Add new CGDirectDisplayIDs for new screens for (size_t i = 0; i < [screenList count]; i++) @@ -1152,7 +1133,7 @@ public: } // Remove old CGDirectDisplayIDs for screens that no longer exist - for (DisplayLinksActiveMap::iterator it = _displayLinksActiveList.begin(); it != _displayLinksActiveList.end(); ++it) + for (DisplayLinksActiveMap::iterator it = _displayLinksActiveList.begin(); it != _displayLinksActiveList.end(); ) { CGDirectDisplayID displayID = it->first; CVDisplayLinkRef displayLinkRef = it->second; @@ -1162,15 +1143,28 @@ public: if (CVDisplayLinkIsRunning(displayLinkRef)) { CVDisplayLinkStop(displayLinkRef); - CVDisplayLinkRelease(displayLinkRef); } + CVDisplayLinkRelease(displayLinkRef); + _displayLinksActiveList.erase(displayID); _displayLinkFlushTimeList.erase(displayID); + + if (_displayLinksActiveList.empty()) + { + break; + } + else + { + it = _displayLinksActiveList.begin(); + continue; + } } + + ++it; } - pthread_mutex_unlock(&_mutexFlushVideo); + pthread_mutex_unlock(&_mutexDisplayLinkLists); } - (void) signalFetchAtIndex:(uint8_t)index diff --git a/desmume/src/frontend/cocoa/userinterface/MacMetalDisplayView.mm b/desmume/src/frontend/cocoa/userinterface/MacMetalDisplayView.mm index f2d0a72c0..f8f33365a 100644 --- a/desmume/src/frontend/cocoa/userinterface/MacMetalDisplayView.mm +++ b/desmume/src/frontend/cocoa/userinterface/MacMetalDisplayView.mm @@ -2200,11 +2200,7 @@ void MacMetalDisplayView::SetAllowViewFlushes(bool allowFlushes) { CGDirectDisplayID displayID = (CGDirectDisplayID)this->GetDisplayViewID(); MacClientSharedObject *sharedData = ((MacMetalDisplayPresenter *)this->_presenter)->GetSharedData(); - - if (![sharedData isDisplayLinkRunningUsingID:displayID]) - { - [sharedData displayLinkStartUsingID:displayID]; - } + [sharedData displayLinkStartUsingID:displayID]; } void MacMetalDisplayView::FlushView() diff --git a/desmume/src/frontend/cocoa/userinterface/MacOGLDisplayView.mm b/desmume/src/frontend/cocoa/userinterface/MacOGLDisplayView.mm index 0a497f162..b3ff019a0 100644 --- a/desmume/src/frontend/cocoa/userinterface/MacOGLDisplayView.mm +++ b/desmume/src/frontend/cocoa/userinterface/MacOGLDisplayView.mm @@ -485,11 +485,7 @@ void MacOGLDisplayView::SetAllowViewFlushes(bool allowFlushes) { CGDirectDisplayID displayID = (CGDirectDisplayID)this->GetDisplayViewID(); MacClientSharedObject *sharedData = ((MacOGLDisplayPresenter *)this->_presenter)->GetSharedData(); - - if (![sharedData isDisplayLinkRunningUsingID:displayID]) - { - [sharedData displayLinkStartUsingID:displayID]; - } + [sharedData displayLinkStartUsingID:displayID]; } void MacOGLDisplayView::SetUseVerticalSync(const bool useVerticalSync)