From dba559aafbc9328ccc3960a2d03f3da4b21cc7c0 Mon Sep 17 00:00:00 2001 From: Snowdream Date: Sat, 16 Jul 2022 20:15:09 -0400 Subject: [PATCH] Make Graphics thread-safety optional --- binding/graphics-binding.cpp | 2 ++ macos/mkxp-z.xcodeproj/project.pbxproj | 8 +++---- src/display/graphics.cpp | 33 +++++++++++++++++++------- src/display/graphics.h | 5 ++-- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/binding/graphics-binding.cpp b/binding/graphics-binding.cpp index 7238aa0..5dce294 100644 --- a/binding/graphics-binding.cpp +++ b/binding/graphics-binding.cpp @@ -356,6 +356,7 @@ DEF_GRA_PROP_B(FixedAspectRatio) DEF_GRA_PROP_B(SmoothScaling) DEF_GRA_PROP_B(IntegerScaling) DEF_GRA_PROP_B(LastMileScaling) +DEF_GRA_PROP_B(Threadsafe) #define INIT_GRA_PROP_BIND(PropName, prop_name_s) \ { \ @@ -407,4 +408,5 @@ void graphicsBindingInit() INIT_GRA_PROP_BIND( SmoothScaling, "smooth_scaling" ); INIT_GRA_PROP_BIND( IntegerScaling, "integer_scaling" ); INIT_GRA_PROP_BIND( LastMileScaling, "last_mile_scaling" ); + INIT_GRA_PROP_BIND( Threadsafe, "thread_safe" ); } diff --git a/macos/mkxp-z.xcodeproj/project.pbxproj b/macos/mkxp-z.xcodeproj/project.pbxproj index e56f874..90cc334 100644 --- a/macos/mkxp-z.xcodeproj/project.pbxproj +++ b/macos/mkxp-z.xcodeproj/project.pbxproj @@ -2955,7 +2955,7 @@ "$(DEPENDENCY_SEARCH_PATH)/include/SDL2", "$(DEPENDENCY_SEARCH_PATH)/include/pixman-1", "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)", - "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)/arm64-darwin", + "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)/-darwin", "$(DEPENDENCY_SEARCH_PATH)/include/AL", "$(DEPENDENCY_SEARCH_PATH)/include/openssl", "$(DEPENDENCY_SEARCH_PATH)/include/uchardet", @@ -3042,7 +3042,7 @@ "$(DEPENDENCY_SEARCH_PATH)/include/SDL2", "$(DEPENDENCY_SEARCH_PATH)/include/pixman-1", "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)", - "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)/arm64-darwin", + "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)/-darwin", "$(DEPENDENCY_SEARCH_PATH)/include/AL", "$(DEPENDENCY_SEARCH_PATH)/include/openssl", "$(DEPENDENCY_SEARCH_PATH)/include/uchardet", @@ -3128,7 +3128,7 @@ "$(DEPENDENCY_SEARCH_PATH)/include/SDL2", "$(DEPENDENCY_SEARCH_PATH)/include/pixman-1", "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)", - "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)/arm64-darwin", + "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)/-darwin", "$(DEPENDENCY_SEARCH_PATH)/include/AL", "$(DEPENDENCY_SEARCH_PATH)/include/uchardet", "$(DEPENDENCY_SEARCH_PATH)/include/openssl", @@ -3209,7 +3209,7 @@ "$(DEPENDENCY_SEARCH_PATH)/include/SDL2", "$(DEPENDENCY_SEARCH_PATH)/include/pixman-1", "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)", - "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)/arm64-darwin", + "$(DEPENDENCY_SEARCH_PATH)/include/ruby-$(MRI_VERSION)/-darwin", "$(DEPENDENCY_SEARCH_PATH)/include/AL", "$(DEPENDENCY_SEARCH_PATH)/include/uchardet", "$(DEPENDENCY_SEARCH_PATH)/include/openssl", diff --git a/src/display/graphics.cpp b/src/display/graphics.cpp index a66c5a8..43cd704 100644 --- a/src/display/graphics.cpp +++ b/src/display/graphics.cpp @@ -760,6 +760,7 @@ struct GraphicsPrivate { SDL_mutex *avgFPSLock; SDL_mutex *glResourceLock; + bool multithreadedMode; /* Global list of all live Disposables * (disposed on reset) */ @@ -769,9 +770,9 @@ struct GraphicsPrivate { : scRes(DEF_SCREEN_W, DEF_SCREEN_H), scSize(scRes), winSize(rtData->config.defScreenW, rtData->config.defScreenH), screen(scRes.x, scRes.y), threadData(rtData), - glCtx(SDL_GL_GetCurrentContext()), frameRate(DEF_FRAMERATE), - frameCount(0), brightness(255), fpsLimiter(frameRate), - useFrameSkip(rtData->config.frameSkip), frozen(false), + glCtx(SDL_GL_GetCurrentContext()), multithreadedMode(true), + frameRate(DEF_FRAMERATE), frameCount(0), brightness(255), + fpsLimiter(frameRate), useFrameSkip(rtData->config.frameSkip), frozen(false), last_update(0), last_avg_update(0), backingScaleFactor(1), integerScaleFactor(0, 0), integerScaleActive(rtData->config.integerScaling.active), integerLastMileScaling(rtData->config.integerScaling.lastMileScaling) { @@ -1048,12 +1049,16 @@ struct GraphicsPrivate { return ret; } - void setLock() { + void setLock(bool force = false) { + if (!(force || multithreadedMode)) return; + SDL_LockMutex(glResourceLock); SDL_GL_MakeCurrent(threadData->window, threadData->glContext); } - void releaseLock() { + void releaseLock(bool force = false) { + if (!(force || multithreadedMode)) return; + SDL_UnlockMutex(glResourceLock); } }; @@ -1532,6 +1537,16 @@ void Graphics::setLastMileScaling(bool value) p->updateScreenResoRatio(p->threadData); } +bool Graphics::getThreadsafe() const +{ + return p->multithreadedMode; +} + +void Graphics::setThreadsafe(bool value) +{ + p->multithreadedMode = value; +} + double Graphics::getScale() const { p->checkResize(); return (double)(p->winSize.y / p->backingScaleFactor) / p->scRes.y; @@ -1583,12 +1598,12 @@ void Graphics::repaintWait(const AtomicFlag &exitCond, bool checkReset) { GLMeta::blitEnd(); } -void Graphics::lock() { - p->setLock(); +void Graphics::lock(bool force) { + p->setLock(force); } -void Graphics::unlock() { - p->releaseLock(); +void Graphics::unlock(bool force) { + p->releaseLock(force); } void Graphics::addDisposable(Disposable *d) { p->dispList.append(d->link); } diff --git a/src/display/graphics.h b/src/display/graphics.h index e32f91e..935b2f3 100644 --- a/src/display/graphics.h +++ b/src/display/graphics.h @@ -77,6 +77,7 @@ public: DECL_ATTR( SmoothScaling, bool ) DECL_ATTR( IntegerScaling, bool ) DECL_ATTR( LastMileScaling, bool ) + DECL_ATTR( Threadsafe, bool ) double averageFrameRate(); /* */ @@ -87,8 +88,8 @@ public: void repaintWait(const AtomicFlag &exitCond, bool checkReset = true); - void lock(); - void unlock(); + void lock(bool force = false); + void unlock(bool force = false); private: Graphics(RGSSThreadData *data);