nuclear@0: /************************************************************************************ nuclear@0: nuclear@0: Filename : OVR_System.cpp nuclear@0: Content : General kernel initialization/cleanup, including that nuclear@0: of the memory allocator. nuclear@0: Created : September 19, 2012 nuclear@0: Notes : nuclear@0: nuclear@0: Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. nuclear@0: nuclear@0: Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); nuclear@0: you may not use the Oculus VR Rift SDK except in compliance with the License, nuclear@0: which is provided at the time of installation or download, or which nuclear@0: otherwise accompanies this software in either electronic or hard copy form. nuclear@0: nuclear@0: You may obtain a copy of the License at nuclear@0: nuclear@0: http://www.oculusvr.com/licenses/LICENSE-3.2 nuclear@0: nuclear@0: Unless required by applicable law or agreed to in writing, the Oculus VR SDK nuclear@0: distributed under the License is distributed on an "AS IS" BASIS, nuclear@0: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. nuclear@0: See the License for the specific language governing permissions and nuclear@0: limitations under the License. nuclear@0: nuclear@0: ************************************************************************************/ nuclear@0: nuclear@0: #include "OVR_System.h" nuclear@0: #include "OVR_Threads.h" nuclear@0: #include "OVR_Timer.h" nuclear@0: #include "../Displays/OVR_Display.h" nuclear@0: #ifdef OVR_OS_WIN32 nuclear@0: #include "../Displays/OVR_Win32_ShimFunctions.h" nuclear@0: #endif nuclear@0: nuclear@0: namespace OVR { nuclear@0: nuclear@0: #ifdef OVR_OS_WIN32 nuclear@0: extern bool anyRiftsInExtendedMode(); nuclear@0: #endif nuclear@0: nuclear@0: // Stack of destroy listeners (push/pop semantics) nuclear@0: static SystemSingletonInternal *SystemShutdownListenerStack = 0; nuclear@0: static Lock stackLock; nuclear@0: static bool DisplayShimInitialized = false; nuclear@0: nuclear@0: void SystemSingletonInternal::PushDestroyCallbacks() nuclear@0: { nuclear@0: Lock::Locker locker(&stackLock); nuclear@0: nuclear@0: // Push listener onto the stack nuclear@0: NextSingleton = SystemShutdownListenerStack; nuclear@0: SystemShutdownListenerStack = this; nuclear@0: } nuclear@0: nuclear@0: void System::DirectDisplayInitialize() nuclear@0: { nuclear@0: #ifdef OVR_OS_WIN32 nuclear@0: // Set up display code for Windows nuclear@0: Win32::DisplayShim::GetInstance(); nuclear@0: nuclear@0: // This code will look for the first display. If it's a display nuclear@0: // that's extending the destkop, the code will assume we're in nuclear@0: // compatibility mode. Compatibility mode prevents shim loading nuclear@0: // and renders only to extended Rifts. nuclear@0: // If we find a display and it's application exclusive, nuclear@0: // we load the shim so we can render to it. nuclear@0: // If no display is available, we revert to whatever the nuclear@0: // driver tells us we're in nuclear@0: nuclear@0: bool anyExtendedRifts = anyRiftsInExtendedMode() || Display::InCompatibilityMode( false ); nuclear@0: nuclear@0: DisplayShimInitialized = Win32::DisplayShim::GetInstance().Initialize(anyExtendedRifts); nuclear@0: #endif nuclear@0: } nuclear@0: nuclear@0: bool System::DirectDisplayEnabled() nuclear@0: { nuclear@0: return DisplayShimInitialized; nuclear@0: } nuclear@0: nuclear@0: // Initializes System core, installing allocator. nuclear@0: void System::Init(Log* log, Allocator *palloc) nuclear@0: { nuclear@0: if (!Allocator::GetInstance()) nuclear@0: { nuclear@0: Log::SetGlobalLog(log); nuclear@0: Timer::initializeTimerSystem(); nuclear@0: Allocator::setInstance(palloc); nuclear@0: Display::Initialize(); nuclear@0: DirectDisplayInitialize(); nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: OVR_DEBUG_LOG(("System::Init failed - duplicate call.")); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: void System::Destroy() nuclear@0: { nuclear@0: if (Allocator::GetInstance()) nuclear@0: { nuclear@0: #ifdef OVR_OS_WIN32 nuclear@0: Win32::DisplayShim::GetInstance().Shutdown(); nuclear@0: #endif nuclear@0: nuclear@0: // Invoke all of the post-finish callbacks (normal case) nuclear@0: for (SystemSingletonInternal *listener = SystemShutdownListenerStack; listener; listener = listener->NextSingleton) nuclear@0: { nuclear@0: listener->OnThreadDestroy(); nuclear@0: } nuclear@0: nuclear@0: #ifdef OVR_ENABLE_THREADS nuclear@0: // Wait for all threads to finish; this must be done so that memory nuclear@0: // allocator and all destructors finalize correctly. nuclear@0: Thread::FinishAllThreads(); nuclear@0: #endif nuclear@0: nuclear@0: // Invoke all of the post-finish callbacks (normal case) nuclear@0: for (SystemSingletonInternal *next, *listener = SystemShutdownListenerStack; listener; listener = next) nuclear@0: { nuclear@0: next = listener->NextSingleton; nuclear@0: nuclear@0: listener->OnSystemDestroy(); nuclear@0: } nuclear@0: nuclear@0: SystemShutdownListenerStack = 0; nuclear@0: nuclear@0: // Shutdown heap and destroy SysAlloc singleton, if any. nuclear@0: Allocator::GetInstance()->onSystemShutdown(); nuclear@0: Allocator::setInstance(0); nuclear@0: nuclear@0: Timer::shutdownTimerSystem(); nuclear@0: Log::SetGlobalLog(Log::GetDefaultLog()); nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: OVR_DEBUG_LOG(("System::Destroy failed - System not initialized.")); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // Returns 'true' if system was properly initialized. nuclear@0: bool System::IsInitialized() nuclear@0: { nuclear@0: return Allocator::GetInstance() != 0; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: } // namespace OVR