121 lines
2.8 KiB
C++
121 lines
2.8 KiB
C++
#include "GalaxySDKManager.h"
|
|
#include "VrError.h"
|
|
|
|
GalaxySDKManager::GalaxySDKManager()
|
|
: m_refCount(0)
|
|
, m_bSDKInitialized(false)
|
|
{
|
|
}
|
|
|
|
GalaxySDKManager::~GalaxySDKManager()
|
|
{
|
|
// 确保SDK被反初始化
|
|
if (m_bSDKInitialized) {
|
|
#ifdef _WIN32
|
|
try {
|
|
IGXFactory::GetInstance().Uninit();
|
|
}
|
|
catch (...) {
|
|
// 忽略析构函数中的异常
|
|
}
|
|
#else
|
|
GXCloseLib();
|
|
#endif
|
|
m_bSDKInitialized = false;
|
|
}
|
|
}
|
|
|
|
GalaxySDKManager& GalaxySDKManager::GetInstance()
|
|
{
|
|
static GalaxySDKManager instance;
|
|
return instance;
|
|
}
|
|
|
|
int GalaxySDKManager::InitSDK()
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
// 增加引用计数
|
|
m_refCount++;
|
|
|
|
// 如果已经初始化,直接返回成功
|
|
if (m_bSDKInitialized) {
|
|
return SUCCESS;
|
|
}
|
|
|
|
// 执行SDK初始化
|
|
#ifdef _WIN32
|
|
// Windows 平台:使用 C++ API
|
|
try {
|
|
IGXFactory::GetInstance().Init();
|
|
m_bSDKInitialized = true;
|
|
return SUCCESS;
|
|
}
|
|
catch (CGalaxyException& e) {
|
|
m_refCount--; // 初始化失败,减少引用计数
|
|
return static_cast<int>(e.GetErrorCode());
|
|
}
|
|
catch (...) {
|
|
m_refCount--; // 初始化失败,减少引用计数
|
|
return ERR_CODE(DEV_OPEN_ERR);
|
|
}
|
|
#else
|
|
// Linux/ARM 平台:使用 C API
|
|
GX_STATUS status = GXInitLib();
|
|
if (status == GX_STATUS_SUCCESS) {
|
|
m_bSDKInitialized = true;
|
|
return SUCCESS;
|
|
}
|
|
m_refCount--; // 初始化失败,减少引用计数
|
|
return static_cast<int>(status);
|
|
#endif
|
|
}
|
|
|
|
int GalaxySDKManager::UninitSDK()
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
// 减少引用计数
|
|
if (m_refCount > 0) {
|
|
m_refCount--;
|
|
}
|
|
|
|
// 只有当引用计数为0时才真正反初始化
|
|
if (m_refCount == 0 && m_bSDKInitialized) {
|
|
#ifdef _WIN32
|
|
// Windows 平台:使用 C++ API
|
|
try {
|
|
IGXFactory::GetInstance().Uninit();
|
|
m_bSDKInitialized = false;
|
|
return SUCCESS;
|
|
}
|
|
catch (CGalaxyException& e) {
|
|
return static_cast<int>(e.GetErrorCode());
|
|
}
|
|
catch (...) {
|
|
return ERR_CODE(DEV_CLOSE_ERR);
|
|
}
|
|
#else
|
|
// Linux/ARM 平台:使用 C API
|
|
GX_STATUS status = GXCloseLib();
|
|
if (status == GX_STATUS_SUCCESS) {
|
|
m_bSDKInitialized = false;
|
|
return SUCCESS;
|
|
}
|
|
return static_cast<int>(status);
|
|
#endif
|
|
}
|
|
|
|
return SUCCESS;
|
|
}
|
|
|
|
bool GalaxySDKManager::IsSDKInitialized() const
|
|
{
|
|
return m_bSDKInitialized;
|
|
}
|
|
|
|
int GalaxySDKManager::GetRefCount() const
|
|
{
|
|
return m_refCount.load();
|
|
}
|