Added KRSurfaceManager

This commit is contained in:
2021-08-16 23:36:05 -07:00
parent 212cea794f
commit f6b3845057
7 changed files with 172 additions and 49 deletions

View File

@@ -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)

View File

@@ -37,6 +37,7 @@
#include "KRAudioSample.h"
#include "KRBundle.h"
#include "KRPresentationThread.h"
#include "KRSurfaceManager.h"
#if defined(ANDROID)
#include <chrono>
@@ -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<KRPresentationThread>(*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<KRSurfaceManager>(*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<std::mutex> 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<HWND>(createWindowSurfaceInfo->hWnd);
std::unique_ptr<KRSurface> info = std::make_unique<KRSurface>(*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<KrSurfaceHandle, std::unique_ptr<KRSurface>>(surfaceHandle, std::move(info)));
m_surfaceHandleMap.insert(std::pair<KrSurfaceMapIndex, KrSurfaceHandle>(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<KrSurfaceHandle, std::unique_ptr<KRSurface>>& KRContext::GetSurfaces()
{
return m_surfaces;
}

View File

@@ -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<KrSurfaceHandle, std::unique_ptr<KRSurface>>& 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<KRSurfaceManager> m_surfaceManager;
KRResource** m_resourceMap;
size_t m_resourceMapSize;
@@ -210,7 +212,6 @@ private:
void createDeviceContexts();
void createDevices();
void destroyDeviceContexts();
void destroySurfaces();
unordered_multimap<std::string, KRResource*> m_resources;
@@ -220,9 +221,6 @@ private:
unordered_map<KrDeviceHandle, std::unique_ptr<KRDevice>> m_devices;
KrDeviceHandle m_topDeviceHandle;
unordered_map<KrSurfaceHandle, std::unique_ptr<KRSurface>> m_surfaces;
KrDeviceHandle m_topSurfaceHandle;
unordered_map<KrSurfaceMapIndex, KrSurfaceHandle> m_surfaceHandleMap;
};

View File

@@ -92,7 +92,7 @@ void KRPresentationThread::renderFrame()
// TODO - We should use fences to eliminate this mutex
const std::lock_guard<std::mutex> surfaceLock(KRContext::g_SurfaceInfoMutex);
unordered_map<KrSurfaceHandle, std::unique_ptr<KRSurface>>& surfaces = m_pContext->GetSurfaces();
unordered_map<KrSurfaceHandle, std::unique_ptr<KRSurface>>& surfaces = m_pContext->getSurfaceManager()->getSurfaces();
for (auto surfaceItr = surfaces.begin(); surfaceItr != surfaces.end(); surfaceItr++) {
KRSurface& surface = *(*surfaceItr).second;

View File

@@ -73,4 +73,4 @@ private:
void renderFrame();
};
#endif // KRPRESENTATIONTHREAD_H
#endif // KRPRESENTATIONTHREAD_H

View File

@@ -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<std::mutex> surfaceLock(KRContext::g_SurfaceInfoMutex);
m_surfaces.clear();
}
#ifdef WIN32
KrResult KRSurfaceManager::create(HWND hWnd, KrSurfaceHandle& surfaceHandle)
{
surfaceHandle = 0;
std::unique_ptr<KRSurface> surface = std::make_unique<KRSurface>(*m_pContext, hWnd);
KrResult initialize_result = surface->initialize();
if (initialize_result != KR_SUCCESS) {
return initialize_result;
}
surfaceHandle = ++m_topSurfaceHandle;
m_surfaces.insert(std::pair<KrSurfaceHandle, std::unique_ptr<KRSurface>>(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<KrSurfaceHandle, std::unique_ptr<KRSurface>>& KRSurfaceManager::getSurfaces()
{
return m_surfaces;
}

59
kraken/KRSurfaceManager.h Normal file
View File

@@ -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<KrSurfaceHandle, std::unique_ptr<KRSurface>>& getSurfaces();
private:
unordered_map<KrSurfaceHandle, std::unique_ptr<KRSurface>> m_surfaces;
KrDeviceHandle m_topSurfaceHandle;
void destroySurfaces();
};
#endif // KRSURFACEMANAGER_H