summaryrefslogtreecommitdiffstats
path: root/gfx/vr/VRShMem.cpp
blob: b7d071cb882119fe1546a7416b2dfae0785599c5 (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "VRShMem.h"

#ifdef MOZILLA_INTERNAL_API
#  include "nsString.h"
#  include "nsXULAppAPI.h"
#endif

#include "gfxVRMutex.h"

#if defined(XP_MACOSX)
#  include <sys/mman.h>
#  include <sys/stat.h> /* For mode constants */
#  include <fcntl.h>    /* For O_* constants */
#elif defined(MOZ_WIDGET_ANDROID)
#  include "GeckoVRManager.h"
#endif

#if !defined(XP_WIN)
#  include <unistd.h>  // for ::sleep
#endif

using namespace mozilla::gfx;

#ifdef XP_WIN
static const char* kShmemName = "moz.gecko.vr_ext." SHMEM_VERSION;
static LPCTSTR kMutexName = TEXT("mozilla::vr::ShmemMutex" SHMEM_VERSION);
#elif defined(XP_MACOSX)
static const char* kShmemName = "/moz.gecko.vr_ext." SHMEM_VERSION;
#endif  //  XP_WIN

#if !defined(MOZ_WIDGET_ANDROID)
namespace {
void YieldThread() {
#  if defined(XP_WIN)
  ::Sleep(0);
#  else
  ::sleep(0);
#  endif
}
}  // anonymous namespace
#endif  // !defined(MOZ_WIDGET_ANDROID)

VRShMem::VRShMem(volatile VRExternalShmem* aShmem, bool aRequiresMutex)
    : mExternalShmem(aShmem),
      mIsSharedExternalShmem(aShmem != nullptr)
#if defined(XP_WIN)
      ,
      mRequiresMutex(aRequiresMutex)
#endif
#if defined(XP_MACOSX)
      ,
      mShmemFD(0)
#elif defined(XP_WIN)
      ,
      mShmemFile(nullptr),
      mMutex(nullptr)
#endif
{
  // Regarding input parameters,
  // - aShmem is null for VRManager or for VRService in multi-proc
  // - aShmem is !null for VRService in-proc (i.e., no VR proc)
}

// Note: This function should only be called for in-proc scenarios, where the
// shared memory is only shared within the same proc (rather than across
// processes). Also, this local heap memory's lifetime is tied to the class.
// Callers to this must ensure that its reference doesn't outlive the owning
// VRShMem instance.
volatile VRExternalShmem* VRShMem::GetExternalShmem() const {
#if defined(XP_MACOSX)
  MOZ_ASSERT(mShmemFD == 0);
#elif defined(XP_WIN)
  MOZ_ASSERT(mShmemFile == nullptr);
#endif
  return mExternalShmem;
}

bool VRShMem::IsDisplayStateShutdown() const {
  // adapted from VRService::Refresh
  // Does this need the mutex for getting .shutdown?
  return mExternalShmem != nullptr &&
         mExternalShmem->state.displayState.shutdown;
}

// This method returns true when there is a Shmem struct allocated and
// when there is a shmem handle from the OS. This implies that the struct
// is mapped to shared memory rather than being allocated on the heap by
// this process.
bool VRShMem::IsCreatedOnSharedMemory() const {
#if defined(XP_MACOSX)
  return HasExternalShmem() && (mShmemFD != 0);
#elif defined(XP_WIN)
  return HasExternalShmem() && (mShmemFile != nullptr);
#else
  // VRShMem does not support system shared memory on remaining platformss
  return false;
#endif
}

// CreateShMem allocates system shared memory for mExternalShmem and
// synchronization primitives to protect it.
// Callers/Processes to CreateShMem should followup with CloseShMem
void VRShMem::CreateShMem(bool aCreateOnSharedMemory) {
  if (HasExternalShmem()) {
    MOZ_ASSERT(mIsSharedExternalShmem && !IsCreatedOnSharedMemory());
    return;
  }
#if defined(XP_WIN)
  if (mMutex == nullptr) {
    mMutex = CreateMutex(nullptr,      // default security descriptor
                         false,        // mutex not owned
                         kMutexName);  // object name
    if (mMutex == nullptr) {
#  ifdef MOZILLA_INTERNAL_API
      nsAutoCString msg;
      msg.AppendPrintf("VRManager CreateMutex error \"%lu\".", GetLastError());
      NS_WARNING(msg.get());
#  endif
      MOZ_ASSERT(false);
      return;
    }
    // At xpcshell extension tests, it creates multiple VRManager
    // instances in plug-contrainer.exe. It causes GetLastError() return
    // `ERROR_ALREADY_EXISTS`. However, even though `ERROR_ALREADY_EXISTS`, it
    // still returns the same mutex handle.
    //
    // https://docs.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-createmutexa
    MOZ_ASSERT(GetLastError() == 0 || GetLastError() == ERROR_ALREADY_EXISTS);
  }
#endif  // XP_WIN
#if !defined(MOZ_WIDGET_ANDROID)
  // The VR Service accesses all hardware from a separate process
  // and replaces the other VRManager when enabled.
  // If the VR process is not enabled, create an in-process VRService.
  if (!aCreateOnSharedMemory) {
    MOZ_ASSERT(mExternalShmem == nullptr);
    // If the VR process is disabled, attempt to create a
    // VR service within the current process on the heap
    mExternalShmem = new VRExternalShmem();
    ClearShMem();
    return;
  }
#endif

  MOZ_ASSERT(aCreateOnSharedMemory);

#if defined(XP_MACOSX)
  if (mShmemFD == 0) {
    mShmemFD =
        shm_open(kShmemName, O_RDWR, S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH);
  }
  if (mShmemFD <= 0) {
    mShmemFD = 0;
    return;
  }

  struct stat sb;
  fstat(mShmemFD, &sb);
  off_t length = sb.st_size;
  if (length < (off_t)sizeof(VRExternalShmem)) {
    // TODO - Implement logging (Bug 1558912)
    CloseShMem();
    return;
  }

  mExternalShmem = (VRExternalShmem*)mmap(NULL, length, PROT_READ | PROT_WRITE,
                                          MAP_SHARED, mShmemFD, 0);
  if (mExternalShmem == MAP_FAILED) {
    // TODO - Implement logging (Bug 1558912)
    mExternalShmem = NULL;
    CloseShMem();
    return;
  }

#elif defined(XP_WIN)
  if (mShmemFile == nullptr) {
    mShmemFile =
        CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0,
                           sizeof(VRExternalShmem), kShmemName);
    MOZ_ASSERT(GetLastError() == 0 || GetLastError() == ERROR_ALREADY_EXISTS);
    MOZ_ASSERT(mShmemFile);
    if (mShmemFile == nullptr) {
      // TODO - Implement logging (Bug 1558912)
      CloseShMem();
      return;
    }
  }

  LARGE_INTEGER length;
  length.QuadPart = sizeof(VRExternalShmem);
  mExternalShmem = (VRExternalShmem*)MapViewOfFile(
      mShmemFile,           // handle to map object
      FILE_MAP_ALL_ACCESS,  // read/write permission
      0, 0, length.QuadPart);

  if (mExternalShmem == nullptr) {
    // TODO - Implement logging (Bug 1558912)
    CloseShMem();
    return;
  }
#elif defined(MOZ_WIDGET_ANDROID)
  MOZ_ASSERT(false,
             "CreateShMem should not be called for Android. Use "
             "CreateShMemForAndroid instead");
#endif
}

// This function sets mExternalShmem in the Android/GeckoView
// scenarios where the host creates it in-memory and VRShMem
// accesses it via GeckVRManager.
void VRShMem::CreateShMemForAndroid() {
#if defined(MOZ_WIDGET_ANDROID) && defined(MOZILLA_INTERNAL_API)
  mExternalShmem =
      (VRExternalShmem*)mozilla::GeckoVRManager::GetExternalContext();
  if (!mExternalShmem) {
    return;
  } else {
    mIsSharedExternalShmem = true;
  }

  int32_t version = -1;
  int32_t size = 0;
  if (pthread_mutex_lock((pthread_mutex_t*)&(mExternalShmem->systemMutex)) ==
      0) {
    version = mExternalShmem->version;
    size = mExternalShmem->size;
    pthread_mutex_unlock((pthread_mutex_t*)&(mExternalShmem->systemMutex));
  } else {
    return;
  }
  if (version != kVRExternalVersion) {
    mExternalShmem = nullptr;
    return;
  }
  if (size != sizeof(VRExternalShmem)) {
    mExternalShmem = nullptr;
    return;
  }
#endif
}

void VRShMem::ClearShMem() {
  if (mExternalShmem != nullptr) {
#ifdef MOZILLA_INTERNAL_API
    // VRExternalShmem is asserted to be POD
    mExternalShmem->Clear();
#else
    memset((void*)mExternalShmem, 0, sizeof(VRExternalShmem));
#endif
  }
}

// The cleanup corresponding to CreateShMem
void VRShMem::CloseShMem() {
#if !defined(MOZ_WIDGET_ANDROID)
  if (!IsCreatedOnSharedMemory()) {
    MOZ_ASSERT(!mIsSharedExternalShmem);
    if (mExternalShmem) {
      delete mExternalShmem;
      mExternalShmem = nullptr;
    }
    return;
  }
#endif
#if defined(XP_MACOSX)
  if (mExternalShmem) {
    munmap((void*)mExternalShmem, sizeof(VRExternalShmem));
    mExternalShmem = NULL;
  }
  if (mShmemFD) {
    close(mShmemFD);
    mShmemFD = 0;
  }
#elif defined(XP_WIN)
  if (mExternalShmem) {
    UnmapViewOfFile((void*)mExternalShmem);
    mExternalShmem = nullptr;
  }
  if (mShmemFile) {
    CloseHandle(mShmemFile);
    mShmemFile = nullptr;
  }
#elif defined(MOZ_WIDGET_ANDROID)
  mExternalShmem = NULL;
#endif

#if defined(XP_WIN)
  if (mMutex) {
    MOZ_ASSERT(mRequiresMutex);
    CloseHandle(mMutex);
    mMutex = nullptr;
  }
#endif
}

// Called to use an existing shmem instance created by another process
// Callers to JoinShMem should call LeaveShMem for cleanup
bool VRShMem::JoinShMem() {
#if defined(XP_WIN)
  if (!mMutex && mRequiresMutex) {
    // Check that there are no errors before making system calls
    MOZ_ASSERT(GetLastError() == 0);

    mMutex = OpenMutex(MUTEX_ALL_ACCESS,  // request full access
                       false,             // handle not inheritable
                       kMutexName);       // object name

    if (mMutex == nullptr) {
#  ifdef MOZILLA_INTERNAL_API
      nsAutoCString msg;
      msg.AppendPrintf("VRService OpenMutex error \"%lu\".", GetLastError());
      NS_WARNING(msg.get());
#  endif
      return false;
    }
    MOZ_ASSERT(GetLastError() == 0);
  }
#endif

  if (HasExternalShmem()) {
    // An ExternalShmem is already set. No need to override and rejoin
    return true;
  }

#if defined(XP_WIN)
  // Opening a file-mapping object by name
  base::ProcessHandle targetHandle =
      OpenFileMappingA(FILE_MAP_ALL_ACCESS,  // read/write access
                       FALSE,                // do not inherit the name
                       kShmemName);          // name of mapping object

  MOZ_ASSERT(GetLastError() == 0);

  LARGE_INTEGER length;
  length.QuadPart = sizeof(VRExternalShmem);
  mExternalShmem = (VRExternalShmem*)MapViewOfFile(
      reinterpret_cast<base::ProcessHandle>(
          targetHandle),    // handle to map object
      FILE_MAP_ALL_ACCESS,  // read/write permission
      0, 0, length.QuadPart);
  MOZ_ASSERT(GetLastError() == 0);

  // TODO - Implement logging (Bug 1558912)
  mShmemFile = targetHandle;
  if (!mExternalShmem) {
    MOZ_ASSERT(mExternalShmem);
    return false;
  }
#else
  // TODO: Implement shmem for other platforms.
  //
  // TODO: ** Does this mean that ShMem only works in Windows for now? If so,
  // let's delete the code from other platforms (Bug 1563234)
  MOZ_ASSERT(false, "JoinShMem not implemented");
#endif
  return true;
}

// The cleanup corresponding to JoinShMem
void VRShMem::LeaveShMem() {
#if defined(XP_WIN)
  // Check that there are no errors before making system calls
  MOZ_ASSERT(GetLastError() == 0);

  if (mShmemFile) {
    ::CloseHandle(mShmemFile);
    mShmemFile = nullptr;
  }
#endif

  if (mExternalShmem != nullptr) {
#if defined(XP_WIN)
    if (IsCreatedOnSharedMemory()) {
      UnmapViewOfFile((void*)mExternalShmem);
      MOZ_ASSERT(GetLastError() == 0);
    }
    // Otherwise, if not created on shared memory, simply null the shared
    // reference to the heap object. The call to CloseShMem will appropriately
    // free the allocation.
#endif
    mExternalShmem = nullptr;
  }
#if defined(XP_WIN)
  if (mMutex) {
    MOZ_ASSERT(mRequiresMutex);
    CloseHandle(mMutex);
    mMutex = nullptr;
  }
#endif
}

void VRShMem::PushBrowserState(VRBrowserState& aBrowserState,
                               bool aNotifyCond) {
  if (!mExternalShmem) {
    return;
  }
#if defined(MOZ_WIDGET_ANDROID)
  if (pthread_mutex_lock((pthread_mutex_t*)&(mExternalShmem->geckoMutex)) ==
      0) {
    memcpy((void*)&(mExternalShmem->geckoState), (void*)&aBrowserState,
           sizeof(VRBrowserState));
    if (aNotifyCond) {
      pthread_cond_signal((pthread_cond_t*)&(mExternalShmem->geckoCond));
    }
    pthread_mutex_unlock((pthread_mutex_t*)&(mExternalShmem->geckoMutex));
  }
#else
  bool status = true;

#  if defined(XP_WIN)
  WaitForMutex lock(mMutex);
  status = lock.GetStatus();
#  endif  // defined(XP_WIN)

  if (status) {
    mExternalShmem->geckoGenerationA = mExternalShmem->geckoGenerationA + 1;
    memcpy((void*)&(mExternalShmem->geckoState), (void*)&aBrowserState,
           sizeof(VRBrowserState));
    mExternalShmem->geckoGenerationB = mExternalShmem->geckoGenerationB + 1;
  }
#endif    // defined(MOZ_WIDGET_ANDROID)
}

void VRShMem::PullBrowserState(mozilla::gfx::VRBrowserState& aState) {
  if (!mExternalShmem) {
    return;
  }
  // Copying the browser state from the shmem is non-blocking
  // on x86/x64 architectures.  Arm requires a mutex that is
  // locked for the duration of the memcpy to and from shmem on
  // both sides.
  // On x86/x64 It is fallable -- If a dirty copy is detected by
  // a mismatch of geckoGenerationA and geckoGenerationB,
  // the copy is discarded and will not replace the last known
  // browser state.

#if defined(MOZ_WIDGET_ANDROID)
  // TODO: This code is out-of-date and fails to compile, as
  // VRService isn't compiled for Android (Bug 1563234)
  /*
  if (pthread_mutex_lock((pthread_mutex_t*)&(mExternalShmem->geckoMutex)) ==
      0) {
    memcpy(&aState, &tmp.geckoState, sizeof(VRBrowserState));
    pthread_mutex_unlock((pthread_mutex_t*)&(mExternalShmem->geckoMutex));
  }
  */
  MOZ_ASSERT(false, "PullBrowserState not implemented");
#else
  bool status = true;
#  if defined(XP_WIN)
  if (mRequiresMutex) {
    // TODO: Is this scoped lock okay? Seems like it should allow some
    // race condition (Bug 1563234)
    WaitForMutex lock(mMutex);
    status = lock.GetStatus();
  }
#  endif  // defined(XP_WIN)
  if (status) {
    VRExternalShmem tmp;
    if (mExternalShmem->geckoGenerationA != mBrowserGeneration) {
      // TODO - (void *) cast removes volatile semantics.
      // The memcpy is not likely to be optimized out, but is theoretically
      // possible.  Suggest refactoring to either explicitly enforce memory
      // order or to use locks.
      memcpy(&tmp, (void*)mExternalShmem, sizeof(VRExternalShmem));
      if (tmp.geckoGenerationA == tmp.geckoGenerationB &&
          tmp.geckoGenerationA != 0) {
        memcpy(&aState, &tmp.geckoState, sizeof(VRBrowserState));
        mBrowserGeneration = tmp.geckoGenerationA;
      }
    }
  }
#endif    // defined(MOZ_WIDGET_ANDROID)
}

void VRShMem::PushSystemState(const mozilla::gfx::VRSystemState& aState) {
  if (!mExternalShmem) {
    return;
  }
  // Copying the VR service state to the shmem is atomic, infallable,
  // and non-blocking on x86/x64 architectures.  Arm requires a mutex
  // that is locked for the duration of the memcpy to and from shmem on
  // both sides.

#if defined(MOZ_WIDGET_ANDROID)
  // TODO: This code is out-of-date and fails to compile, as
  // VRService isn't compiled for Android (Bug 1563234)
  MOZ_ASSERT(false, "JoinShMem not implemented");
  /*
  if (pthread_mutex_lock((pthread_mutex_t*)&(mExternalShmem->systemMutex)) ==
      0) {
    // We are casting away the volatile keyword, which is not accepted by
    // memcpy.  It is possible (although very unlikely) that the compiler
    // may optimize out the memcpy here as memcpy isn't explicitly safe for
    // volatile memory in the C++ standard.
    memcpy((void*)&mExternalShmem->state, &aState, sizeof(VRSystemState));
    pthread_mutex_unlock((pthread_mutex_t*)&(mExternalShmem->systemMutex));
  }
  */
#else
  bool lockState = true;

#  if defined(XP_WIN)
  if (mRequiresMutex) {
    // TODO: Is this scoped lock okay? Seems like it should allow some
    // race condition (Bug 1563234)
    WaitForMutex lock(mMutex);
    lockState = lock.GetStatus();
  }
#  endif  // defined(XP_WIN)

  if (lockState) {
    mExternalShmem->generationA = mExternalShmem->generationA + 1;
    memcpy((void*)&mExternalShmem->state, &aState, sizeof(VRSystemState));
    mExternalShmem->generationB = mExternalShmem->generationB + 1;
  }
#endif    // defined(MOZ_WIDGET_ANDROID)
}

#if defined(MOZ_WIDGET_ANDROID)
void VRShMem::PullSystemState(
    VRDisplayState& aDisplayState, VRHMDSensorState& aSensorState,
    VRControllerState (&aControllerState)[kVRControllerMaxCount],
    bool& aEnumerationCompleted,
    const std::function<bool()>& aWaitCondition /* = nullptr */) {
  if (!mExternalShmem) {
    return;
  }
  bool done = false;
  while (!done) {
    if (pthread_mutex_lock((pthread_mutex_t*)&(mExternalShmem->systemMutex)) ==
        0) {
      while (true) {
        memcpy(&aDisplayState, (void*)&(mExternalShmem->state.displayState),
               sizeof(VRDisplayState));
        memcpy(&aSensorState, (void*)&(mExternalShmem->state.sensorState),
               sizeof(VRHMDSensorState));
        memcpy(aControllerState,
               (void*)&(mExternalShmem->state.controllerState),
               sizeof(VRControllerState) * kVRControllerMaxCount);
        aEnumerationCompleted = mExternalShmem->state.enumerationCompleted;
        if (!aWaitCondition || aWaitCondition()) {
          done = true;
          break;
        }
        // Block current thead using the condition variable until data
        // changes
        pthread_cond_wait((pthread_cond_t*)&mExternalShmem->systemCond,
                          (pthread_mutex_t*)&mExternalShmem->systemMutex);
      }  // while (true)
      pthread_mutex_unlock((pthread_mutex_t*)&(mExternalShmem->systemMutex));
    } else if (!aWaitCondition) {
      // pthread_mutex_lock failed and we are not waiting for a condition to
      // exit from PullState call.
      return;
    }
  }  // while (!done) {
}
#else
void VRShMem::PullSystemState(
    VRDisplayState& aDisplayState, VRHMDSensorState& aSensorState,
    VRControllerState (&aControllerState)[kVRControllerMaxCount],
    bool& aEnumerationCompleted,
    const std::function<bool()>& aWaitCondition /* = nullptr */) {
  MOZ_ASSERT(mExternalShmem);
  if (!mExternalShmem) {
    return;
  }
  while (true) {
    {  // Scope for WaitForMutex
#  if defined(XP_WIN)
      bool status = true;
      WaitForMutex lock(mMutex);
      status = lock.GetStatus();
      if (status) {
#  endif  // defined(XP_WIN)
        VRExternalShmem tmp;
        memcpy(&tmp, (void*)mExternalShmem, sizeof(VRExternalShmem));
        bool isCleanCopy =
            tmp.generationA == tmp.generationB && tmp.generationA != 0;
        if (isCleanCopy) {
          memcpy(&aDisplayState, &tmp.state.displayState,
                 sizeof(VRDisplayState));
          memcpy(&aSensorState, &tmp.state.sensorState,
                 sizeof(VRHMDSensorState));
          memcpy(aControllerState,
                 (void*)&(mExternalShmem->state.controllerState),
                 sizeof(VRControllerState) * kVRControllerMaxCount);
          aEnumerationCompleted = mExternalShmem->state.enumerationCompleted;
          // Check for wait condition
          if (!aWaitCondition || aWaitCondition()) {
            return;
          }
        } else if (!aWaitCondition) {
          // We did not get a clean copy, and we are not waiting for a condition
          // to exit from PullState call.
          return;
        }
        // Yield the thread while polling
        YieldThread();
#  if defined(XP_WIN)
      } else if (!aWaitCondition) {
        // WaitForMutex failed and we are not waiting for a condition to
        // exit from PullState call.
        return;
      }
#  endif  // defined(XP_WIN)
    }  // End: Scope for WaitForMutex
    // Yield the thread while polling
    YieldThread();
  }  // while (!true)
}
#endif    // defined(MOZ_WIDGET_ANDROID)

void VRShMem::PushWindowState(VRWindowState& aState) {
#if defined(XP_WIN)
  if (!mExternalShmem) {
    return;
  }

  bool status = true;
  WaitForMutex lock(mMutex);
  status = lock.GetStatus();
  if (status) {
    memcpy((void*)&(mExternalShmem->windowState), (void*)&aState,
           sizeof(VRWindowState));
  }
#endif  // defined(XP_WIN)
}

void VRShMem::PullWindowState(VRWindowState& aState) {
#if defined(XP_WIN)
  if (!mExternalShmem) {
    return;
  }

  bool status = true;
  WaitForMutex lock(mMutex);
  status = lock.GetStatus();
  if (status) {
    memcpy((void*)&aState, (void*)&(mExternalShmem->windowState),
           sizeof(VRWindowState));
  }
#endif  // defined(XP_WIN)
}

void VRShMem::PushTelemetryState(VRTelemetryState& aState) {
#if defined(XP_WIN)
  if (!mExternalShmem) {
    return;
  }

  bool status = true;
  WaitForMutex lock(mMutex);
  status = lock.GetStatus();
  if (status) {
    memcpy((void*)&(mExternalShmem->telemetryState), (void*)&aState,
           sizeof(VRTelemetryState));
  }
#endif  // defined(XP_WIN)
}
void VRShMem::PullTelemetryState(VRTelemetryState& aState) {
#if defined(XP_WIN)
  if (!mExternalShmem) {
    return;
  }

  bool status = true;
  WaitForMutex lock(mMutex);
  status = lock.GetStatus();
  if (status) {
    memcpy((void*)&aState, (void*)&(mExternalShmem->telemetryState),
           sizeof(VRTelemetryState));
  }
#endif  // defined(XP_WIN)
}

void VRShMem::SendEvent(uint64_t aWindowID,
                        mozilla::gfx::VRFxEventType aEventType,
                        mozilla::gfx::VRFxEventState aEventState) {
  MOZ_ASSERT(!HasExternalShmem());
  if (JoinShMem()) {
    mozilla::gfx::VRWindowState windowState = {0};
    PullWindowState(windowState);
    windowState.windowID = aWindowID;
    windowState.eventType = aEventType;
    windowState.eventState = aEventState;
    PushWindowState(windowState);
    LeaveShMem();

#if defined(XP_WIN)
    // Notify the waiting host process that the data is now available
    HANDLE hSignal = ::OpenEventA(EVENT_ALL_ACCESS,       // dwDesiredAccess
                                  FALSE,                  // bInheritHandle
                                  windowState.signalName  // lpName
    );
    ::SetEvent(hSignal);
    ::CloseHandle(hSignal);
#endif  // defined(XP_WIN)
  }
}

void VRShMem::SendIMEState(uint64_t aWindowID,
                           mozilla::gfx::VRFxEventState aEventState) {
  SendEvent(aWindowID, mozilla::gfx::VRFxEventType::IME, aEventState);
}

void VRShMem::SendFullscreenState(uint64_t aWindowID, bool aFullscreen) {
  SendEvent(aWindowID, mozilla::gfx::VRFxEventType::FULLSCREEN,
            aFullscreen ? mozilla::gfx::VRFxEventState::FULLSCREEN_ENTER
                        : mozilla::gfx::VRFxEventState::FULLSCREEN_EXIT);
}

// Note: this should be called from the VRShMem instance that created
// the external shmem rather than joined it.
void VRShMem::SendShutdowmState(uint64_t aWindowID) {
  MOZ_ASSERT(HasExternalShmem());

  mozilla::gfx::VRWindowState windowState = {0};
  PullWindowState(windowState);
  windowState.windowID = aWindowID;
  windowState.eventType = mozilla::gfx::VRFxEventType::SHUTDOWN;
  PushWindowState(windowState);

#if defined(XP_WIN)
  // Notify the waiting host process that the data is now available
  HANDLE hSignal = ::OpenEventA(EVENT_ALL_ACCESS,       // dwDesiredAccess
                                FALSE,                  // bInheritHandle
                                windowState.signalName  // lpName
  );
  ::SetEvent(hSignal);
  ::CloseHandle(hSignal);
#endif  // defined(XP_WIN)
}