ovr_sdk

annotate LibOVR/Src/Kernel/OVR_System.cpp @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
rev   line source
nuclear@0 1 /************************************************************************************
nuclear@0 2
nuclear@0 3 Filename : OVR_System.cpp
nuclear@0 4 Content : General kernel initialization/cleanup, including that
nuclear@0 5 of the memory allocator.
nuclear@0 6 Created : September 19, 2012
nuclear@0 7 Notes :
nuclear@0 8
nuclear@0 9 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
nuclear@0 10
nuclear@0 11 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
nuclear@0 12 you may not use the Oculus VR Rift SDK except in compliance with the License,
nuclear@0 13 which is provided at the time of installation or download, or which
nuclear@0 14 otherwise accompanies this software in either electronic or hard copy form.
nuclear@0 15
nuclear@0 16 You may obtain a copy of the License at
nuclear@0 17
nuclear@0 18 http://www.oculusvr.com/licenses/LICENSE-3.2
nuclear@0 19
nuclear@0 20 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
nuclear@0 21 distributed under the License is distributed on an "AS IS" BASIS,
nuclear@0 22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nuclear@0 23 See the License for the specific language governing permissions and
nuclear@0 24 limitations under the License.
nuclear@0 25
nuclear@0 26 ************************************************************************************/
nuclear@0 27
nuclear@0 28 #include "OVR_System.h"
nuclear@0 29 #include "OVR_Threads.h"
nuclear@0 30 #include "OVR_Timer.h"
nuclear@0 31 #include "../Displays/OVR_Display.h"
nuclear@0 32 #ifdef OVR_OS_WIN32
nuclear@0 33 #include "../Displays/OVR_Win32_ShimFunctions.h"
nuclear@0 34 #endif
nuclear@0 35
nuclear@0 36 namespace OVR {
nuclear@0 37
nuclear@0 38 #ifdef OVR_OS_WIN32
nuclear@0 39 extern bool anyRiftsInExtendedMode();
nuclear@0 40 #endif
nuclear@0 41
nuclear@0 42 // Stack of destroy listeners (push/pop semantics)
nuclear@0 43 static SystemSingletonInternal *SystemShutdownListenerStack = 0;
nuclear@0 44 static Lock stackLock;
nuclear@0 45 static bool DisplayShimInitialized = false;
nuclear@0 46
nuclear@0 47 void SystemSingletonInternal::PushDestroyCallbacks()
nuclear@0 48 {
nuclear@0 49 Lock::Locker locker(&stackLock);
nuclear@0 50
nuclear@0 51 // Push listener onto the stack
nuclear@0 52 NextSingleton = SystemShutdownListenerStack;
nuclear@0 53 SystemShutdownListenerStack = this;
nuclear@0 54 }
nuclear@0 55
nuclear@0 56 void System::DirectDisplayInitialize()
nuclear@0 57 {
nuclear@0 58 #ifdef OVR_OS_WIN32
nuclear@0 59 // Set up display code for Windows
nuclear@0 60 Win32::DisplayShim::GetInstance();
nuclear@0 61
nuclear@0 62 // This code will look for the first display. If it's a display
nuclear@0 63 // that's extending the destkop, the code will assume we're in
nuclear@0 64 // compatibility mode. Compatibility mode prevents shim loading
nuclear@0 65 // and renders only to extended Rifts.
nuclear@0 66 // If we find a display and it's application exclusive,
nuclear@0 67 // we load the shim so we can render to it.
nuclear@0 68 // If no display is available, we revert to whatever the
nuclear@0 69 // driver tells us we're in
nuclear@0 70
nuclear@0 71 bool anyExtendedRifts = anyRiftsInExtendedMode() || Display::InCompatibilityMode( false );
nuclear@0 72
nuclear@0 73 DisplayShimInitialized = Win32::DisplayShim::GetInstance().Initialize(anyExtendedRifts);
nuclear@0 74 #endif
nuclear@0 75 }
nuclear@0 76
nuclear@0 77 bool System::DirectDisplayEnabled()
nuclear@0 78 {
nuclear@0 79 return DisplayShimInitialized;
nuclear@0 80 }
nuclear@0 81
nuclear@0 82 // Initializes System core, installing allocator.
nuclear@0 83 void System::Init(Log* log, Allocator *palloc)
nuclear@0 84 {
nuclear@0 85 if (!Allocator::GetInstance())
nuclear@0 86 {
nuclear@0 87 Log::SetGlobalLog(log);
nuclear@0 88 Timer::initializeTimerSystem();
nuclear@0 89 Allocator::setInstance(palloc);
nuclear@0 90 Display::Initialize();
nuclear@0 91 DirectDisplayInitialize();
nuclear@0 92 }
nuclear@0 93 else
nuclear@0 94 {
nuclear@0 95 OVR_DEBUG_LOG(("System::Init failed - duplicate call."));
nuclear@0 96 }
nuclear@0 97 }
nuclear@0 98
nuclear@0 99 void System::Destroy()
nuclear@0 100 {
nuclear@0 101 if (Allocator::GetInstance())
nuclear@0 102 {
nuclear@0 103 #ifdef OVR_OS_WIN32
nuclear@0 104 Win32::DisplayShim::GetInstance().Shutdown();
nuclear@0 105 #endif
nuclear@0 106
nuclear@0 107 // Invoke all of the post-finish callbacks (normal case)
nuclear@0 108 for (SystemSingletonInternal *listener = SystemShutdownListenerStack; listener; listener = listener->NextSingleton)
nuclear@0 109 {
nuclear@0 110 listener->OnThreadDestroy();
nuclear@0 111 }
nuclear@0 112
nuclear@0 113 #ifdef OVR_ENABLE_THREADS
nuclear@0 114 // Wait for all threads to finish; this must be done so that memory
nuclear@0 115 // allocator and all destructors finalize correctly.
nuclear@0 116 Thread::FinishAllThreads();
nuclear@0 117 #endif
nuclear@0 118
nuclear@0 119 // Invoke all of the post-finish callbacks (normal case)
nuclear@0 120 for (SystemSingletonInternal *next, *listener = SystemShutdownListenerStack; listener; listener = next)
nuclear@0 121 {
nuclear@0 122 next = listener->NextSingleton;
nuclear@0 123
nuclear@0 124 listener->OnSystemDestroy();
nuclear@0 125 }
nuclear@0 126
nuclear@0 127 SystemShutdownListenerStack = 0;
nuclear@0 128
nuclear@0 129 // Shutdown heap and destroy SysAlloc singleton, if any.
nuclear@0 130 Allocator::GetInstance()->onSystemShutdown();
nuclear@0 131 Allocator::setInstance(0);
nuclear@0 132
nuclear@0 133 Timer::shutdownTimerSystem();
nuclear@0 134 Log::SetGlobalLog(Log::GetDefaultLog());
nuclear@0 135 }
nuclear@0 136 else
nuclear@0 137 {
nuclear@0 138 OVR_DEBUG_LOG(("System::Destroy failed - System not initialized."));
nuclear@0 139 }
nuclear@0 140 }
nuclear@0 141
nuclear@0 142 // Returns 'true' if system was properly initialized.
nuclear@0 143 bool System::IsInitialized()
nuclear@0 144 {
nuclear@0 145 return Allocator::GetInstance() != 0;
nuclear@0 146 }
nuclear@0 147
nuclear@0 148
nuclear@0 149 } // namespace OVR