From f6b3845057c44fb5e807d42f4ee185aa3a4da06d Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Mon, 16 Aug 2021 23:36:05 -0700 Subject: [PATCH] Added KRSurfaceManager --- kraken/CMakeLists.txt | 1 + kraken/KRContext.cpp | 54 +++++-------------- kraken/KRContext.h | 8 ++- kraken/KRPresentationThread.cpp | 2 +- kraken/KRPresentationThread.h | 2 +- kraken/KRSurfaceManager.cpp | 95 +++++++++++++++++++++++++++++++++ kraken/KRSurfaceManager.h | 59 ++++++++++++++++++++ 7 files changed, 172 insertions(+), 49 deletions(-) create mode 100644 kraken/KRSurfaceManager.cpp create mode 100644 kraken/KRSurfaceManager.h diff --git a/kraken/CMakeLists.txt b/kraken/CMakeLists.txt index eea6c01..ab1c6e7 100644 --- a/kraken/CMakeLists.txt +++ b/kraken/CMakeLists.txt @@ -24,6 +24,7 @@ add_sources(KRCollider.cpp) add_sources(KRContext.cpp) add_sources(KRDevice.cpp) add_sources(KRSurface.cpp) +add_sources(KRSurfaceManager.cpp) add_sources(KRStreamer.cpp) IF(APPLE) add_sources(KREngine.mm) diff --git a/kraken/KRContext.cpp b/kraken/KRContext.cpp index b1b6943..c4df353 100755 --- a/kraken/KRContext.cpp +++ b/kraken/KRContext.cpp @@ -37,6 +37,7 @@ #include "KRAudioSample.h" #include "KRBundle.h" #include "KRPresentationThread.h" +#include "KRSurfaceManager.h" #if defined(ANDROID) #include @@ -86,7 +87,6 @@ KRContext::KRContext(const KrInitializeInfo* initializeInfo) , m_vulkanInstance(VK_NULL_HANDLE) , m_resourceMapSize(initializeInfo->resourceMapSize) , m_topDeviceHandle(0) - , m_topSurfaceHandle(0) { m_presentationThread = std::make_unique(*this); m_resourceMap = (KRResource **)malloc(sizeof(KRResource*) * m_resourceMapSize); @@ -114,10 +114,9 @@ KRContext::KRContext(const KrInitializeInfo* initializeInfo) m_pUnknownManager = new KRUnknownManager(*this); m_pShaderManager = new KRShaderManager(*this); m_pSourceManager = new KRSourceManager(*this); + m_surfaceManager = std::make_unique(*this); m_streamingEnabled = true; - - #if defined(_WIN32) || defined(_WIN64) SYSTEM_INFO winSysInfo; @@ -195,7 +194,8 @@ KRContext::~KRContext() { delete m_pBundleManager; m_pBundleManager = NULL; } - destroySurfaces(); + m_surfaceManager.reset(); + destroyDeviceContexts(); if (m_resourceMap) { delete m_resourceMap; @@ -262,6 +262,9 @@ KRShaderManager *KRContext::getShaderManager() { KRSourceManager *KRContext::getSourceManager() { return m_pSourceManager; } +KRSurfaceManager* KRContext::getSurfaceManager() { + return m_surfaceManager.get(); +} KRUnknownManager *KRContext::getUnknownManager() { return m_pUnknownManager; } @@ -816,14 +819,6 @@ KRContext::destroyDeviceContexts() } } -void -KRContext::destroySurfaces() -{ - const std::lock_guard surfaceLock(KRContext::g_SurfaceInfoMutex); - m_surfaces.clear(); - m_surfaceHandleMap.clear(); -} - void KRContext::activateStreamerContext() { @@ -945,17 +940,11 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW #ifdef WIN32 HWND hWnd = static_cast(createWindowSurfaceInfo->hWnd); - std::unique_ptr info = std::make_unique(*this, hWnd); - - KrResult initialize_result = info->initialize(); - if (initialize_result != KR_SUCCESS) { - return initialize_result; + KrSurfaceHandle surfaceHandle = 0; + KrResult result = m_surfaceManager->create(hWnd, surfaceHandle); + if (result != KR_SUCCESS) { + return result; } - - KRDevice* deviceInfo = &GetDeviceInfo(info->m_deviceHandle); - - KrSurfaceHandle surfaceHandle = ++m_topSurfaceHandle; - m_surfaces.insert(std::pair>(surfaceHandle, std::move(info))); m_surfaceHandleMap.insert(std::pair(createWindowSurfaceInfo->surfaceHandle, surfaceHandle)); @@ -982,21 +971,7 @@ KrResult KRContext::deleteWindowSurface(const KrDeleteWindowSurfaceInfo* deleteW KrSurfaceHandle surfaceHandle = (*handleItr).second; m_surfaceHandleMap.erase(handleItr); - auto itr = m_surfaces.find(surfaceHandle); - if (itr == m_surfaces.end()) { - return KR_ERROR_NOT_FOUND; - } - m_surfaces.erase(itr); - return KR_SUCCESS; -} - -KRSurface& KRContext::GetSurfaceInfo(KrSurfaceHandle handle) -{ - auto itr = m_surfaces.find(handle); - if (itr == m_surfaces.end()) { - assert(false); - } - return *m_surfaces[handle]; + return m_surfaceManager->destroy(surfaceHandle); } KRDevice& KRContext::GetDeviceInfo(KrDeviceHandle handle) @@ -1067,8 +1042,3 @@ VkInstance& KRContext::GetVulkanInstance() { return m_vulkanInstance; } - -unordered_map>& KRContext::GetSurfaces() -{ - return m_surfaces; -} diff --git a/kraken/KRContext.h b/kraken/KRContext.h index 0c785ca..bddcef7 100755 --- a/kraken/KRContext.h +++ b/kraken/KRContext.h @@ -44,6 +44,7 @@ #include "KRUnknownManager.h" #include "KRShaderManager.h" #include "KRSourceManager.h" +#include "KRSurfaceManager.h" #include "KRStreamer.h" #include "KRDevice.h" #include "KRSurface.h" @@ -109,6 +110,7 @@ public: KRUnknownManager *getUnknownManager(); KRShaderManager *getShaderManager(); KRSourceManager *getSourceManager(); + KRSurfaceManager* getSurfaceManager(); KRCamera *createCamera(int width, int height); @@ -160,7 +162,6 @@ public: KRDevice& GetDeviceInfo(KrDeviceHandle handle); KRSurface& GetSurfaceInfo(KrSurfaceHandle handle); - unordered_map>& GetSurfaces(); VkInstance& GetVulkanInstance(); KrSurfaceHandle GetBestDeviceForSurface(const VkSurfaceKHR& surface); @@ -182,6 +183,7 @@ private: KRUnknownManager *m_pUnknownManager; KRShaderManager *m_pShaderManager; KRSourceManager *m_pSourceManager; + std::unique_ptr m_surfaceManager; KRResource** m_resourceMap; size_t m_resourceMapSize; @@ -210,7 +212,6 @@ private: void createDeviceContexts(); void createDevices(); void destroyDeviceContexts(); - void destroySurfaces(); unordered_multimap m_resources; @@ -220,9 +221,6 @@ private: unordered_map> m_devices; KrDeviceHandle m_topDeviceHandle; - unordered_map> m_surfaces; - KrDeviceHandle m_topSurfaceHandle; - unordered_map m_surfaceHandleMap; }; diff --git a/kraken/KRPresentationThread.cpp b/kraken/KRPresentationThread.cpp index 045c053..d9c041e 100644 --- a/kraken/KRPresentationThread.cpp +++ b/kraken/KRPresentationThread.cpp @@ -92,7 +92,7 @@ void KRPresentationThread::renderFrame() // TODO - We should use fences to eliminate this mutex const std::lock_guard surfaceLock(KRContext::g_SurfaceInfoMutex); - unordered_map>& surfaces = m_pContext->GetSurfaces(); + unordered_map>& surfaces = m_pContext->getSurfaceManager()->getSurfaces(); for (auto surfaceItr = surfaces.begin(); surfaceItr != surfaces.end(); surfaceItr++) { KRSurface& surface = *(*surfaceItr).second; diff --git a/kraken/KRPresentationThread.h b/kraken/KRPresentationThread.h index 3615915..4f15a7a 100644 --- a/kraken/KRPresentationThread.h +++ b/kraken/KRPresentationThread.h @@ -73,4 +73,4 @@ private: void renderFrame(); }; -#endif // KRPRESENTATIONTHREAD_H \ No newline at end of file +#endif // KRPRESENTATIONTHREAD_H diff --git a/kraken/KRSurfaceManager.cpp b/kraken/KRSurfaceManager.cpp new file mode 100644 index 0000000..32ba909 --- /dev/null +++ b/kraken/KRSurfaceManager.cpp @@ -0,0 +1,95 @@ +// +// KRSurfaceManager.cpp +// Kraken Engine +// +// Copyright 2021 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KRSurfaceManager.h" + + +KRSurfaceManager::KRSurfaceManager(KRContext& context) + : KRContextObject(context) + , m_topSurfaceHandle(0) +{ +} + +KRSurfaceManager::~KRSurfaceManager() +{ + destroySurfaces(); +} + +void KRSurfaceManager::destroySurfaces() +{ + const std::lock_guard surfaceLock(KRContext::g_SurfaceInfoMutex); + m_surfaces.clear(); +} + +#ifdef WIN32 +KrResult KRSurfaceManager::create(HWND hWnd, KrSurfaceHandle& surfaceHandle) +{ + surfaceHandle = 0; + + std::unique_ptr surface = std::make_unique(*m_pContext, hWnd); + + KrResult initialize_result = surface->initialize(); + if (initialize_result != KR_SUCCESS) { + return initialize_result; + } + + surfaceHandle = ++m_topSurfaceHandle; + m_surfaces.insert(std::pair>(surfaceHandle, std::move(surface))); + + return KR_SUCCESS; +} + +#endif + +KrResult KRSurfaceManager::destroy(KrSurfaceHandle& surfaceHandle) +{ + auto itr = m_surfaces.find(surfaceHandle); + if (itr == m_surfaces.end()) { + return KR_ERROR_NOT_FOUND; + } + m_surfaces.erase(itr); + return KR_SUCCESS; +} + +KRSurface& KRSurfaceManager::get(KrSurfaceHandle surfaceHandle) +{ + auto itr = m_surfaces.find(surfaceHandle); + if (itr == m_surfaces.end()) { + assert(false); + } + return *m_surfaces[surfaceHandle]; +} + + +unordered_map>& KRSurfaceManager::getSurfaces() +{ + return m_surfaces; +} diff --git a/kraken/KRSurfaceManager.h b/kraken/KRSurfaceManager.h new file mode 100644 index 0000000..9f72eea --- /dev/null +++ b/kraken/KRSurfaceManager.h @@ -0,0 +1,59 @@ +// +// KRSurfaceManager.h +// Kraken Engine +// +// Copyright 2021 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KREngine-common.h" + +#include "KRContext.h" +#include "KRSurface.h" + +#ifndef KRSURFACEMANAGER_H +#define KRSURFACEMANAGER_H + +class KRSurfaceManager : KRContextObject +{ +public: + KRSurfaceManager(KRContext& context); + ~KRSurfaceManager(); +#ifdef WIN32 + KrResult create(HWND hWnd, KrSurfaceHandle& surfaceHandle); +#endif + KRSurface& get(KrSurfaceHandle surfaceHandle); + KrResult destroy(KrSurfaceHandle& surfaceHandle); + unordered_map>& getSurfaces(); + +private: + unordered_map> m_surfaces; + KrDeviceHandle m_topSurfaceHandle; + + void destroySurfaces(); +}; + +#endif // KRSURFACEMANAGER_H