ovr_sdk

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