aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/CAPI/CAPI_HSWDisplay.cpp
blob: ae8e9975948976c2b408d3ac0b4365fd8251a768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/************************************************************************************

Filename    :   CAPI_HSWDisplay.cpp
Content     :   Implements Health and Safety Warning system.
Created     :   July 3, 2014
Authors     :   Paul Pedriana

Copyright   :   Copyright 2014 Oculus VR, Inc. All Rights reserved.

Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License"); 
you may not use the Oculus VR Rift SDK except in compliance with the License, 
which is provided at the time of installation or download, or which 
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-3.1 

Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#include "CAPI_HSWDisplay.h"
#include "CAPI_HMDState.h"
#include "../Kernel/OVR_Log.h"
#include "../Kernel/OVR_String.h"
#include "Textures/healthAndSafety.tga.h" // TGA file as a C array declaration.


//-------------------------------------------------------------------------------------
// ***** HSWDISPLAY_DEBUGGING
//
// Defined as 0 or 1. Enables debugging features of this module.

#if !defined(HSWDISPLAY_DEBUGGING)
    #if defined(AUTHOR_PPEDRIANA)
        #define HSWDISPLAY_DEBUGGING 1
    #else
        #define HSWDISPLAY_DEBUGGING 0
    #endif
#endif

#if HSWDISPLAY_DEBUGGING
    OVR_DISABLE_ALL_MSVC_WARNINGS()
    #include <winsock2.h>
    #include <Windows.h>
    OVR_RESTORE_ALL_MSVC_WARNINGS()
#endif

OVR_DISABLE_MSVC_WARNING(4996) // "This function or variable may be unsafe..."


//-------------------------------------------------------------------------------------
// ***** HSWDISPLAY_DEFAULT_ENABLED
//
// Defined as 0 or 1. 1 is default. If 0 then by default HSWDisplay is disabled.
// Developers can set it to 0 to disable HSW display.
//
#if !defined(HSWDISPLAY_DEFAULT_ENABLED)
    #define HSWDISPLAY_DEFAULT_ENABLED 1
#endif



//-------------------------------------------------------------------------------------
// ***** Experimental C API functions
//

extern "C"
{
    OVR_EXPORT void ovrhmd_EnableHSWDisplaySDKRender(ovrHmd hmd, ovrBool enabled)
    {
        OVR::CAPI::HMDState* pHMDState = (OVR::CAPI::HMDState*)hmd->Handle;

	    if (pHMDState)
	    {
            OVR::CAPI::HSWDisplay* pHSWDisplay = pHMDState->pHSWDisplay;

            if(pHSWDisplay)
                pHSWDisplay->EnableRender((enabled == 0) ? false : true);
        }
    }
}



//-------------------------------------------------------------------------------------
// ***** HSWDisplay implementation
//

namespace OVR { namespace CAPI {


static const time_t HSWDisplayTimeNever = (time_t)0; // Constant which denotes the time of "never", as in the display has never been shown yet.

#define HSWDISPLAY_POLL_INTERVAL            0.400 // Seconds between polling for whether the display should be shown.
#define OVR_KEY_HSWDISPLAYLASTDISPLAYEDTIME "HASWLastDisplayedTime"


#if defined(OVR_BUILD_DEBUG)
    #define HSWDISPLAY_FIRST_DISMISSAL_TIME    4     // Earliest time in seconds until the user can dismiss the display.
    #define HSWDISPLAY_REGULAR_DISMISSAL_TIME  2
#else
    #define HSWDISPLAY_FIRST_DISMISSAL_TIME   15
    #define HSWDISPLAY_REGULAR_DISMISSAL_TIME  6
#endif


HSWDisplay::HSWDisplay(ovrRenderAPIType renderAPIType, ovrHmd hmd, const HMDRenderState& hmdRenderState)
  : Enabled(HSWDISPLAY_DEFAULT_ENABLED ? true : false),
    Displayed(false),
    SDKRendered(false),
    DismissRequested(false),
    RenderEnabled(true),
    StartTime(0.0),
    DismissibleTime(0.0),
    LastPollTime(0.0),
    HMD(hmd), 
    HMDMounted(false),
    HMDNewlyMounted(false),
    RenderAPIType(renderAPIType), 
    RenderState(hmdRenderState)
{
}


HSWDisplay::~HSWDisplay()
{
    // To consider: assert that we are already shut down.
    HSWDisplay::Shutdown();
}


void HSWDisplay::Enable(bool enable)
{
    Enabled = enable;

    if(!enable && Displayed) // If it's visible but should not be...
        Dismiss();
}


void HSWDisplay::EnableRender(bool enable)
{
    RenderEnabled = enable;
}


void HSWDisplay::Display()
{
    HSWDISPLAY_LOG(("[HSWDisplay] Display()"));

    DisplayInternal();

    HMDNewlyMounted = false;
    Displayed = true;
    SDKRendered = RenderEnabled;
    StartTime = ovr_GetTimeInSeconds();

    const time_t lastDisplayedTime = HSWDisplay::GetCurrentProfileLastHSWTime();
    DismissibleTime = StartTime + ((lastDisplayedTime == HSWDisplayTimeNever) ? HSWDISPLAY_FIRST_DISMISSAL_TIME : HSWDISPLAY_REGULAR_DISMISSAL_TIME);

    SetCurrentProfileLastHSWTime(time(NULL));
}


bool HSWDisplay::IsDisplayViewable() const
{
    // This function is called IsDisplayViewable, but currently it refers only to whether the 
    // HMD is mounted on the user's head. 

    return HMDMounted;
}


bool HSWDisplay::Dismiss()
{
    #if HSWDISPLAY_DEBUGGING
        if(GetKeyState(VK_SCROLL) & 0x0001) // If the scroll lock key is toggled on...
            return false;                   // Make it so that the display doesn't dismiss, so we can debug this.
    #endif

    // If dismissal is not requested yet, mark it as such.
    bool newlyRequested = false;

    if(!DismissRequested)
    {
        DismissRequested = true;
        newlyRequested = true;
    }

    // If displayed and time has elapsed, do the dismissal.
    OVR_ASSERT(DismissibleTime <= (ovr_GetTimeInSeconds() + HSWDISPLAY_FIRST_DISMISSAL_TIME)); // Make sure the dismissal time is sane.
    if (Displayed && (ovr_GetTimeInSeconds() >= DismissibleTime))
    {
        DismissInternal();
        Displayed = false;
        DismissRequested = false;
        SDKRendered = false;
        return true;
    }

    if(newlyRequested)
        { HSWDISPLAY_LOG(("[HSWDisplay] Dismiss(): Not permitted yet. Queued for timeout in %.1f seconds.", DismissibleTime - ovr_GetTimeInSeconds())); }

    return false; // Cannot dismiss yet.
}


bool HSWDisplay::TickState(ovrHSWDisplayState *hswDisplayState)
{
    bool newlyDisplayed = false;
    const double currentTime = ovr_GetTimeInSeconds();

    // See if we need to be currently displayed. By design we automatically display but don't automatically dismiss.
    if (Displayed)
    {
        if (DismissRequested) // If dismiss was previously requested, see if it can be executed.
            Dismiss();

        if (Displayed) // If not already dismissed above...
        {
            // We currently have the debug behavior that we permit dismiss very soon after launch.
            #if defined(OVR_BUILD_DEBUG)
                if(currentTime >= (StartTime + 2))
                {
                    DismissibleTime = StartTime;
                    //Dismiss();
                }
            #endif
        }

        if (Displayed) // If not already dismissed above...
        {
            const ovrTrackingState ts = ((OVR::CAPI::HMDState*)HMD->Handle)->PredictedTrackingState(currentTime);

            if (ts.StatusFlags & ovrStatus_OrientationTracked) // If the Accelerometer data is valid...
            {
				const OVR::Vector3f v(ts.HeadPose.LinearAcceleration.x, ts.HeadPose.LinearAcceleration.y, ts.HeadPose.LinearAcceleration.z);

                const float minTapMagnitude = 350.0f; // Empirically determined by some testing.

                if (v.LengthSq() > minTapMagnitude)
                    Dismiss(); // This will do nothing if the display is not present.
            }
        }
    }
    else
    {
        if (Enabled && (currentTime >= (LastPollTime + HSWDISPLAY_POLL_INTERVAL)))
        {
            LastPollTime = currentTime;

            // We need to display if any of the following are true:
            //     - The application is just started in Event Mode while the HMD is mounted (warning display would be viewable) and this app was not spawned from a launcher.
            //     - The current user has never seen the display yet while the HMD is mounted (warning display would be viewable).
            //     - The HMD is newly mounted (or the warning display is otherwise newly viewable).
            //     - The warning display hasn't shown in 24 hours (need to verify this as a requirement).
            // Event Mode refers to when the app is being run in a public demo event such as a trade show.

            OVR::CAPI::HMDState* pHMDState = (OVR::CAPI::HMDState*)HMD->Handle;

            if(pHMDState)
            {
                const time_t lastDisplayedTime = HSWDisplay::GetCurrentProfileLastHSWTime();

                // We currently unilaterally set HMDMounted to true because we don't yet have the ability to detect this. To do: Implement this when possible.
                const bool previouslyMounted = HMDMounted;
                HMDMounted = true;
                HMDNewlyMounted = (!previouslyMounted && HMDMounted); // We set this back to false in the Display function or if the HMD is unmounted before then.

                if((lastDisplayedTime == HSWDisplayTimeNever) || HMDNewlyMounted)
                {
                    if(IsDisplayViewable()) // If the HMD is mounted and otherwise being viewed by the user...
                    {
                        Display();
                        newlyDisplayed = true;
                    }
                }
            }
        }
    }

    if(hswDisplayState)
        GetState(hswDisplayState);

    return newlyDisplayed;
}


void HSWDisplay::GetState(ovrHSWDisplayState *hswDisplayState) const
{
    // Return the state to the caller.
    OVR_ASSERT(hswDisplayState != NULL);
    hswDisplayState->Displayed = Displayed;
    hswDisplayState->StartTime = StartTime;
    hswDisplayState->DismissibleTime = DismissibleTime;
}


void HSWDisplay::Render(ovrEyeType eye, const ovrTexture* eyeTexture)
{
    SDKRendered = true;
    RenderInternal(eye, eyeTexture);
}

// Persist the HSW settings on the server, since it needs to be synchronized across all applications.
// Note that the profile manager singleton cannot be used for this task because it overwrites the global
// settings for which the rift config tool is supposed to be authoritative.  That also would step on the
// settings generated by other rift apps.  The server settings, however, are synchronized for all apps
// and so are appropriate for this task.
static String getHSWTimeKey(const char* userName)
{
    String keyName = "server:";
    keyName += OVR_KEY_HSWDISPLAYLASTDISPLAYEDTIME;
    keyName += ":";
    if (userName)
    {
        keyName += userName;
    }
    return keyName;
}

// Returns HSWDisplayTimeNever (0) if there is no profile or this is the first time we are seeing this profile.
time_t HSWDisplay::GetCurrentProfileLastHSWTime() const
{
    // We store the timeout value in HMDState's pProfile.
    HMDState* pHMDState = (HMDState*)HMD->Handle;

    if (pHMDState)
    {
        const char* profileName = pHMDState->pProfile ? pHMDState->pProfile->GetValue(OVR_KEY_USER) : NULL;

        if (profileName)
        {
            if (LastProfileName == profileName)
            {
                return LastHSWTime;
            }

            LastProfileName = profileName;
            String timeKey = getHSWTimeKey(profileName);
            int lastTime = pHMDState->getIntValue(timeKey.ToCStr(), (int)HSWDisplayTimeNever);

            LastHSWTime = lastTime;
            return lastTime;
        }
    }

    return HSWDisplayTimeNever;
}

void HSWDisplay::SetCurrentProfileLastHSWTime(time_t t)
{
    // The timeout value is stored in HMDState's pProfile.
    HMDState* pHMDState = (HMDState*)HMD->Handle;

    if (pHMDState)
    {
        const char* profileName = pHMDState->pProfile ? pHMDState->pProfile->GetValue(OVR_KEY_USER) : NULL;

        if (profileName)
        {
            LastProfileName = profileName;
            LastHSWTime = (int)t;

            String timeKey = getHSWTimeKey(profileName);
            pHMDState->setIntValue(timeKey.ToCStr(), (int)t);
        }
    }
}


// Generates an appropriate stereo ortho projection matrix.
void HSWDisplay::GetOrthoProjection(const HMDRenderState& RenderState, Matrix4f OrthoProjection[2])
{
    Matrix4f perspectiveProjection[2];
    perspectiveProjection[0] = ovrMatrix4f_Projection(RenderState.EyeRenderDesc[0].Fov, 0.01f, 10000.f, true);
    perspectiveProjection[1] = ovrMatrix4f_Projection(RenderState.EyeRenderDesc[1].Fov, 0.01f, 10000.f, true);

    const float    orthoDistance = HSWDISPLAY_DISTANCE; // This is meters from the camera (viewer) that we place the ortho plane.
    const Vector2f orthoScale0   = Vector2f(1.f) / Vector2f(RenderState.EyeRenderDesc[0].PixelsPerTanAngleAtCenter);
    const Vector2f orthoScale1   = Vector2f(1.f) / Vector2f(RenderState.EyeRenderDesc[1].PixelsPerTanAngleAtCenter);
    
    OrthoProjection[0] = ovrMatrix4f_OrthoSubProjection(perspectiveProjection[0], orthoScale0, orthoDistance, RenderState.EyeRenderDesc[0].ViewAdjust.x);
    OrthoProjection[1] = ovrMatrix4f_OrthoSubProjection(perspectiveProjection[1], orthoScale1, orthoDistance, RenderState.EyeRenderDesc[1].ViewAdjust.x);
}


const uint8_t* HSWDisplay::GetDefaultTexture(size_t& TextureSize)
{
    TextureSize = sizeof(healthAndSafety_tga);
    return healthAndSafety_tga;
}



}} // namespace OVR::CAPI




//-------------------------------------------------------------------------------------
// ***** HSWDisplay factory
//

#if defined (OVR_OS_WIN32)
    #define OVR_D3D_VERSION 9
    #include "D3D1X/CAPI_D3D9_HSWDisplay.h"
    #undef  OVR_D3D_VERSION

    #define OVR_D3D_VERSION 10
    #include "D3D1X/CAPI_D3D10_HSWDisplay.h"
    #undef  OVR_D3D_VERSION

    #define OVR_D3D_VERSION 11
    #include "D3D1X/CAPI_D3D11_HSWDisplay.h"
    #undef  OVR_D3D_VERSION
#endif

#include "GL/CAPI_GL_HSWDisplay.h"


OVR::CAPI::HSWDisplay* OVR::CAPI::HSWDisplay::Factory(ovrRenderAPIType apiType, ovrHmd hmd, const OVR::CAPI::HMDRenderState& renderState)
{
    OVR::CAPI::HSWDisplay* pHSWDisplay = NULL;

    switch (apiType)
    {
        case ovrRenderAPI_None:
            pHSWDisplay = new OVR::CAPI::HSWDisplay(apiType, hmd, renderState);
            break;

        case ovrRenderAPI_OpenGL:
            pHSWDisplay = new OVR::CAPI::GL::HSWDisplay(apiType, hmd, renderState);
            break;

    #if defined(OVR_OS_WIN32)
        case ovrRenderAPI_D3D9:
            pHSWDisplay = new OVR::CAPI::D3D9::HSWDisplay(apiType, hmd, renderState);
            break;

        case ovrRenderAPI_D3D10:
            pHSWDisplay = new OVR::CAPI::D3D10::HSWDisplay(apiType, hmd, renderState);
            break;

        case ovrRenderAPI_D3D11:
            pHSWDisplay = new OVR::CAPI::D3D11::HSWDisplay(apiType, hmd, renderState);
            break;
    #else
        case ovrRenderAPI_D3D9:
        case ovrRenderAPI_D3D10:
        case ovrRenderAPI_D3D11: // Fall through
    #endif

        // Handle unsupported cases.
        case ovrRenderAPI_Android_GLES:
        case ovrRenderAPI_Count: // This is not actually a type.
        default:
            break;
    }

    return pHSWDisplay;
}