DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (56e7b9127e89)

VCS Links

Line Code
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 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
/* -*- 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/. */

#if defined(XP_WIN)
#include "CompositorD3D11.h"
#include "TextureD3D11.h"
#include "mozilla/gfx/DeviceManagerDx.h"
#elif defined(XP_MACOSX)
#include "mozilla/gfx/MacIOSurface.h"
#endif

#include "mozilla/Base64.h"
#include "mozilla/gfx/DataSurfaceHelpers.h"
#include "gfxPrefs.h"
#include "gfxUtils.h"
#include "gfxVRPuppet.h"
#include "VRManager.h"
#include "VRThread.h"

#include "mozilla/dom/GamepadEventTypes.h"
#include "mozilla/dom/GamepadBinding.h"

// See CompositorD3D11Shaders.h
namespace mozilla {
namespace layers {
struct ShaderBytes { const void* mData; size_t mLength; };
extern ShaderBytes sRGBShader;
extern ShaderBytes sLayerQuadVS;
} // namespace layers
} // namespace mozilla

using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::gfx::impl;
using namespace mozilla::layers;

// Reminder: changing the order of these buttons may break web content
static const uint64_t kPuppetButtonMask[] = {
  1,
  2,
  4,
  8
};
static const uint32_t kNumPuppetButtonMask = sizeof(kPuppetButtonMask) /
                                             sizeof(uint64_t);
static const uint32_t kNumPuppetAxis = 3;
static const uint32_t kNumPuppetHaptcs = 1;

VRDisplayPuppet::VRDisplayPuppet()
 : VRDisplayLocal(VRDeviceType::Puppet)
 , mIsPresenting(false)
 , mSensorState{}
{
  MOZ_COUNT_CTOR_INHERITED(VRDisplayPuppet, VRDisplayLocal);

  VRDisplayState& state = mDisplayInfo.mDisplayState;
  strncpy(state.mDisplayName, "Puppet HMD", kVRDisplayNameMaxLen);
  state.mIsConnected = true;
  state.mIsMounted = false;
  state.mCapabilityFlags = VRDisplayCapabilityFlags::Cap_None |
                           VRDisplayCapabilityFlags::Cap_Orientation |
                           VRDisplayCapabilityFlags::Cap_Position |
                           VRDisplayCapabilityFlags::Cap_External |
                           VRDisplayCapabilityFlags::Cap_Present |
                           VRDisplayCapabilityFlags::Cap_StageParameters;
  state.mEyeResolution.width = 1836; // 1080 * 1.7
  state.mEyeResolution.height = 2040; // 1200 * 1.7

  // SteamVR gives the application a single FOV to use; it's not configurable as with Oculus
  for (uint32_t eye = 0; eye < 2; ++eye) {
    state.mEyeTranslation[eye].x = 0.0f;
    state.mEyeTranslation[eye].y = 0.0f;
    state.mEyeTranslation[eye].z = 0.0f;
    state.mEyeFOV[eye] = VRFieldOfView(45.0, 45.0, 45.0, 45.0);
  }

  // default: 1m x 1m space, 0.75m high in seated position
  state.mStageSize.width = 1.0f;
  state.mStageSize.height = 1.0f;

  state.mSittingToStandingTransform[0] = 1.0f;
  state.mSittingToStandingTransform[1] = 0.0f;
  state.mSittingToStandingTransform[2] = 0.0f;
  state.mSittingToStandingTransform[3] = 0.0f;

  state.mSittingToStandingTransform[4] = 0.0f;
  state.mSittingToStandingTransform[5] = 1.0f;
  state.mSittingToStandingTransform[6] = 0.0f;
  state.mSittingToStandingTransform[7] = 0.0f;

  state.mSittingToStandingTransform[8] = 0.0f;
  state.mSittingToStandingTransform[9] = 0.0f;
  state.mSittingToStandingTransform[10] = 1.0f;
  state.mSittingToStandingTransform[11] = 0.0f;

  state.mSittingToStandingTransform[12] = 0.0f;
  state.mSittingToStandingTransform[13] = 0.75f;
  state.mSittingToStandingTransform[14] = 0.0f;
  state.mSittingToStandingTransform[15] = 1.0f;

  gfx::Quaternion rot;

  mSensorState.flags |= VRDisplayCapabilityFlags::Cap_Orientation;
  mSensorState.pose.orientation[0] = rot.x;
  mSensorState.pose.orientation[1] = rot.y;
  mSensorState.pose.orientation[2] = rot.z;
  mSensorState.pose.orientation[3] = rot.w;
  mSensorState.pose.angularVelocity[0] = 0.0f;
  mSensorState.pose.angularVelocity[1] = 0.0f;
  mSensorState.pose.angularVelocity[2] = 0.0f;

  mSensorState.flags |= VRDisplayCapabilityFlags::Cap_Position;
  mSensorState.pose.position[0] = 0.0f;
  mSensorState.pose.position[1] = 0.0f;
  mSensorState.pose.position[2] = 0.0f;
  mSensorState.pose.linearVelocity[0] = 0.0f;
  mSensorState.pose.linearVelocity[1] = 0.0f;
  mSensorState.pose.linearVelocity[2] = 0.0f;
}

VRDisplayPuppet::~VRDisplayPuppet()
{
  MOZ_COUNT_DTOR_INHERITED(VRDisplayPuppet, VRDisplayLocal);
}

void
VRDisplayPuppet::SetDisplayInfo(const VRDisplayInfo& aDisplayInfo)
{
  // We are only interested in the eye and mount info of the display info.
  VRDisplayState& state = mDisplayInfo.mDisplayState;
  state.mEyeResolution = aDisplayInfo.mDisplayState.mEyeResolution;
  state.mIsMounted = aDisplayInfo.mDisplayState.mIsMounted;
  memcpy(&state.mEyeFOV, &aDisplayInfo.mDisplayState.mEyeFOV,
         sizeof(state.mEyeFOV[0]) * VRDisplayState::NumEyes);
  memcpy(&state.mEyeTranslation, &aDisplayInfo.mDisplayState.mEyeTranslation,
         sizeof(state.mEyeTranslation[0]) * VRDisplayState::NumEyes);
}

void
VRDisplayPuppet::Destroy()
{
  StopPresentation();
}

void
VRDisplayPuppet::ZeroSensor()
{
}

VRHMDSensorState
VRDisplayPuppet::GetSensorState()
{
  mSensorState.inputFrameID = mDisplayInfo.mFrameId;

  Matrix4x4 matHeadToEye[2];
  for (uint32_t eye = 0; eye < 2; ++eye) {
    matHeadToEye[eye].PreTranslate(mDisplayInfo.GetEyeTranslation(eye));
  }
  mSensorState.CalcViewMatrices(matHeadToEye);

  return mSensorState;
}

void
VRDisplayPuppet::SetSensorState(const VRHMDSensorState& aSensorState)
{
  memcpy(&mSensorState, &aSensorState, sizeof(mSensorState));
}

void
VRDisplayPuppet::StartPresentation()
{
  if (mIsPresenting) {
    return;
  }
  mIsPresenting = true;

#if defined(XP_WIN)
  if (!CreateD3DObjects()) {
    return;
  }

  if (FAILED(mDevice->CreateVertexShader(sLayerQuadVS.mData,
                      sLayerQuadVS.mLength, nullptr, &mQuadVS))) {
    NS_WARNING("Failed to create vertex shader for Puppet");
    return;
  }

  if (FAILED(mDevice->CreatePixelShader(sRGBShader.mData,
                      sRGBShader.mLength, nullptr, &mQuadPS))) {
    NS_WARNING("Failed to create pixel shader for Puppet");
    return;
  }

  CD3D11_BUFFER_DESC cBufferDesc(sizeof(layers::VertexShaderConstants),
                                 D3D11_BIND_CONSTANT_BUFFER,
                                 D3D11_USAGE_DYNAMIC,
                                 D3D11_CPU_ACCESS_WRITE);

  if (FAILED(mDevice->CreateBuffer(&cBufferDesc, nullptr, getter_AddRefs(mVSConstantBuffer)))) {
    NS_WARNING("Failed to vertex shader constant buffer for Puppet");
    return;
  }

  cBufferDesc.ByteWidth = sizeof(layers::PixelShaderConstants);
  if (FAILED(mDevice->CreateBuffer(&cBufferDesc, nullptr, getter_AddRefs(mPSConstantBuffer)))) {
    NS_WARNING("Failed to pixel shader constant buffer for Puppet");
    return;
  }

  CD3D11_SAMPLER_DESC samplerDesc(D3D11_DEFAULT);
  if (FAILED(mDevice->CreateSamplerState(&samplerDesc, getter_AddRefs(mLinearSamplerState)))) {
    NS_WARNING("Failed to create sampler state for Puppet");
    return;
  }

  D3D11_INPUT_ELEMENT_DESC layout[] =
  {
    { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  };

  if (FAILED(mDevice->CreateInputLayout(layout,
                                        sizeof(layout) / sizeof(D3D11_INPUT_ELEMENT_DESC),
                                        sLayerQuadVS.mData,
                                        sLayerQuadVS.mLength,
                                        getter_AddRefs(mInputLayout)))) {
    NS_WARNING("Failed to create input layout for Puppet");
    return;
  }

  Vertex vertices[] = { { { 0.0, 0.0 } },{ { 1.0, 0.0 } },{ { 0.0, 1.0 } },{ { 1.0, 1.0 } } };
  CD3D11_BUFFER_DESC bufferDesc(sizeof(vertices), D3D11_BIND_VERTEX_BUFFER);
  D3D11_SUBRESOURCE_DATA data;
  data.pSysMem = (void*)vertices;

  if (FAILED(mDevice->CreateBuffer(&bufferDesc, &data, getter_AddRefs(mVertexBuffer)))) {
    NS_WARNING("Failed to create vertex buffer for Puppet");
    return;
  }

  memset(&mVSConstants, 0, sizeof(mVSConstants));
  memset(&mPSConstants, 0, sizeof(mPSConstants));
#endif // XP_WIN
}

void
VRDisplayPuppet::StopPresentation()
{
  if (!mIsPresenting) {
    return;
  }

  mIsPresenting = false;
}

#if defined(XP_WIN)
bool
VRDisplayPuppet::UpdateConstantBuffers()
{
  HRESULT hr;
  D3D11_MAPPED_SUBRESOURCE resource;
  resource.pData = nullptr;

  hr = mContext->Map(mVSConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource);
  if (FAILED(hr) || !resource.pData) {
    return false;
  }
  *(VertexShaderConstants*)resource.pData = mVSConstants;
  mContext->Unmap(mVSConstantBuffer, 0);
  resource.pData = nullptr;

  hr = mContext->Map(mPSConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource);
  if (FAILED(hr) || !resource.pData) {
    return false;
  }
  *(PixelShaderConstants*)resource.pData = mPSConstants;
  mContext->Unmap(mPSConstantBuffer, 0);

  ID3D11Buffer *buffer = mVSConstantBuffer;
  mContext->VSSetConstantBuffers(0, 1, &buffer);
  buffer = mPSConstantBuffer;
  mContext->PSSetConstantBuffers(0, 1, &buffer);
  return true;
}

bool
VRDisplayPuppet::SubmitFrame(ID3D11Texture2D* aSource,
                             const IntSize& aSize,
                             const gfx::Rect& aLeftEyeRect,
                             const gfx::Rect& aRightEyeRect)
{
  MOZ_ASSERT(mSubmitThread->GetThread() == NS_GetCurrentThread());
  if (!mIsPresenting) {
    return false;
  }

  if (!CreateD3DObjects()) {
    return false;
  }
  AutoRestoreRenderState restoreState(this);
  if (!restoreState.IsSuccess()) {
    return false;
  }

  VRManager *vm = VRManager::Get();
  MOZ_ASSERT(vm);

  switch (gfxPrefs::VRPuppetSubmitFrame()) {
    case 0:
      // The VR frame is not displayed.
      break;
    case 1:
    {
      // The frames are submitted to VR compositor are decoded
      // into a base64Image and dispatched to the DOM side.
      D3D11_TEXTURE2D_DESC desc;
      aSource->GetDesc(&desc);
      MOZ_ASSERT(desc.Format == DXGI_FORMAT_B8G8R8A8_UNORM,
                 "Only support B8G8R8A8_UNORM format.");
      // Map the staging resource
      ID3D11Texture2D* mappedTexture = nullptr;
      D3D11_MAPPED_SUBRESOURCE mapInfo;
      HRESULT hr = mContext->Map(aSource,
                                 0,  // Subsource
                                 D3D11_MAP_READ,
                                 0,  // MapFlags
                                 &mapInfo);

      if (FAILED(hr)) {
        // If we can't map this texture, copy it to a staging resource.
        if (hr == E_INVALIDARG) {
          D3D11_TEXTURE2D_DESC desc2;
          desc2.Width = desc.Width;
          desc2.Height = desc.Height;
          desc2.MipLevels = desc.MipLevels;
          desc2.ArraySize = desc.ArraySize;
          desc2.Format = desc.Format;
          desc2.SampleDesc = desc.SampleDesc;
          desc2.Usage = D3D11_USAGE_STAGING;
          desc2.BindFlags = 0;
          desc2.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
          desc2.MiscFlags = 0;

          ID3D11Texture2D* stagingTexture = nullptr;
          hr = mDevice->CreateTexture2D(&desc2, nullptr, &stagingTexture);
          if (FAILED(hr)) {
            MOZ_ASSERT(false, "Failed to create a staging texture");
            return false;
          }
          // Copy the texture to a staging resource
          mContext->CopyResource(stagingTexture, aSource);
          // Map the staging resource
          hr = mContext->Map(stagingTexture,
                             0,  // Subsource
                             D3D11_MAP_READ,
                             0,  // MapFlags
                             &mapInfo);
          if (FAILED(hr)) {
            MOZ_ASSERT(false, "Failed to map staging texture");
          }
          mappedTexture = stagingTexture;
        } else {
          MOZ_ASSERT(false, "Failed to map staging texture");
          return false;
        }
      } else {
        mappedTexture = aSource;
      }
      // Ideally, we should convert the srcData to a PNG image and decode it
      // to a Base64 string here, but the GPU process does not have the privilege to
      // access the image library. So, we have to convert the RAW image data
      // to a base64 string and forward it to let the content process to
      // do the image conversion.
      const char* srcData = static_cast<const char*>(mapInfo.pData);
      VRSubmitFrameResultInfo result;
      result.mFormat = SurfaceFormat::B8G8R8A8;
      result.mWidth = desc.Width;
      result.mHeight = desc.Height;
      result.mFrameNum = mDisplayInfo.mFrameId;
      // If the original texture size is not pow of 2, the data will not be tightly strided.
      // We have to copy the pixels by rows.
      nsCString rawString;
      for (uint32_t i = 0; i < desc.Height; i++) {
        rawString += Substring(srcData + i * mapInfo.RowPitch,
                               desc.Width * 4);
      }
      mContext->Unmap(mappedTexture, 0);

      if (Base64Encode(rawString, result.mBase64Image) != NS_OK) {
        MOZ_ASSERT(false, "Failed to encode base64 images.");
      }
      // Dispatch the base64 encoded string to the DOM side. Then, it will be decoded
      // and convert to a PNG image there.
      MessageLoop* loop = VRListenerThreadHolder::Loop();
      loop->PostTask(NewRunnableMethod<const uint32_t, VRSubmitFrameResultInfo>(
        "VRManager::DispatchSubmitFrameResult",
        vm, &VRManager::DispatchSubmitFrameResult, mDisplayInfo.mDisplayID, result
      ));
      break;
    }
    case 2:
    {
      // The VR compositor sumbmit frame to the screen window,
      // the current coordinate is at (0, 0, width, height).
      Matrix viewMatrix = Matrix::Translation(-1.0, 1.0);
      viewMatrix.PreScale(2.0f / float(aSize.width), 2.0f / float(aSize.height));
      viewMatrix.PreScale(1.0f, -1.0f);
      Matrix4x4 projection = Matrix4x4::From2D(viewMatrix);
      projection._33 = 0.0f;

      Matrix transform2d;
      gfx::Matrix4x4 transform = gfx::Matrix4x4::From2D(transform2d);

      const float posX = 0.0f, posY = 0.0f;
      D3D11_VIEWPORT viewport;
      viewport.MinDepth = 0.0f;
      viewport.MaxDepth = 1.0f;
      viewport.Width = aSize.width;
      viewport.Height = aSize.height;
      viewport.TopLeftX = posX;
      viewport.TopLeftY = posY;

      D3D11_RECT scissor;
      scissor.left = posX;
      scissor.right = aSize.width + posX;
      scissor.top = posY;
      scissor.bottom = aSize.height + posY;

      memcpy(&mVSConstants.layerTransform, &transform._11, sizeof(mVSConstants.layerTransform));
      memcpy(&mVSConstants.projection, &projection._11, sizeof(mVSConstants.projection));
      mVSConstants.renderTargetOffset[0] = 0.0f;
      mVSConstants.renderTargetOffset[1] = 0.0f;
      mVSConstants.layerQuad = Rect(0.0f, 0.0f, aSize.width, aSize.height);
      mVSConstants.textureCoords = Rect(0.0f, 1.0f, 1.0f, -1.0f);

      mPSConstants.layerOpacity[0] = 1.0f;

      ID3D11Buffer* vbuffer = mVertexBuffer;
      UINT vsize = sizeof(Vertex);
      UINT voffset = 0;
      mContext->IASetVertexBuffers(0, 1, &vbuffer, &vsize, &voffset);
      mContext->IASetIndexBuffer(nullptr, DXGI_FORMAT_R16_UINT, 0);
      mContext->IASetInputLayout(mInputLayout);
      mContext->RSSetViewports(1, &viewport);
      mContext->RSSetScissorRects(1, &scissor);
      mContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
      mContext->VSSetShader(mQuadVS, nullptr, 0);
      mContext->PSSetShader(mQuadPS, nullptr, 0);

      RefPtr<ID3D11ShaderResourceView> srView;
      HRESULT hr = mDevice->CreateShaderResourceView(aSource, nullptr, getter_AddRefs(srView));
      if (FAILED(hr) || !srView) {
        gfxWarning() << "Could not create shader resource view for Puppet: " << hexa(hr);
        return false;
      }
      ID3D11ShaderResourceView* viewPtr = srView.get();
      mContext->PSSetShaderResources(0 /* 0 == TexSlot::RGB */, 1, &viewPtr);
      // XXX Use Constant from TexSlot in CompositorD3D11.cpp?

      ID3D11SamplerState *sampler = mLinearSamplerState;
      mContext->PSSetSamplers(0, 1, &sampler);

      if (!UpdateConstantBuffers()) {
        NS_WARNING("Failed to update constant buffers for Puppet");
        return false;
      }
      mContext->Draw(4, 0);
      break;
    }
  }

  // We will always return false for gfxVRPuppet to ensure that the fallback "watchdog"
  // code in VRDisplayHost::NotifyVSync() throttles the render loop.  This "watchdog" will
  // result in a refresh rate that is quite low compared to real hardware, but should be
  // sufficient for non-performance oriented tests.  If we wish to simulate realistic frame
  // rates with VRDisplayPuppet, we should block here for the appropriate amount of time and
  // return true to indicate that we have blocked.
  return false;
}

#elif defined(XP_MACOSX)

bool
VRDisplayPuppet::SubmitFrame(MacIOSurface* aMacIOSurface,
                             const IntSize& aSize,
                             const gfx::Rect& aLeftEyeRect,
                             const gfx::Rect& aRightEyeRect)
{
  MOZ_ASSERT(mSubmitThread->GetThread() == NS_GetCurrentThread());
  if (!mIsPresenting || !aMacIOSurface) {
    return false;
  }

  VRManager* vm = VRManager::Get();
  MOZ_ASSERT(vm);

  switch (gfxPrefs::VRPuppetSubmitFrame()) {
    case 0:
      // The VR frame is not displayed.
      break;
    case 1:
    {
      // The frames are submitted to VR compositor are decoded
      // into a base64Image and dispatched to the DOM side.
      RefPtr<SourceSurface> surf = aMacIOSurface->GetAsSurface();
      RefPtr<DataSourceSurface> dataSurf = surf ? surf->GetDataSurface() :
                                           nullptr;
      if (dataSurf) {
        // Ideally, we should convert the srcData to a PNG image and decode it
        // to a Base64 string here, but the GPU process does not have the privilege to
        // access the image library. So, we have to convert the RAW image data
        // to a base64 string and forward it to let the content process to
        // do the image conversion.
        DataSourceSurface::MappedSurface map;
        if (!dataSurf->Map(gfx::DataSourceSurface::MapType::READ, &map)) {
          MOZ_ASSERT(false, "Read DataSourceSurface fail.");
          return false;
        }
        const uint8_t* srcData = map.mData;
        const auto& surfSize = dataSurf->GetSize();
        VRSubmitFrameResultInfo result;
        result.mFormat = SurfaceFormat::B8G8R8A8;
        result.mWidth = surfSize.width;
        result.mHeight = surfSize.height;
        result.mFrameNum = mDisplayInfo.mFrameId;
        // If the original texture size is not pow of 2, the data will not be tightly strided.
        // We have to copy the pixels by rows.
        nsCString rawString;
        for (int32_t i = 0; i < surfSize.height; i++) {
          rawString += Substring((const char*)(srcData) + i * map.mStride,
                                  surfSize.width * 4);
        }
        dataSurf->Unmap();

        if (Base64Encode(rawString, result.mBase64Image) != NS_OK) {
          MOZ_ASSERT(false, "Failed to encode base64 images.");
        }
        // Dispatch the base64 encoded string to the DOM side. Then, it will be decoded
        // and convert to a PNG image there.
        MessageLoop* loop = VRListenerThreadHolder::Loop();
        loop->PostTask(NewRunnableMethod<const uint32_t, VRSubmitFrameResultInfo>(
          "VRManager::DispatchSubmitFrameResult",
          vm, &VRManager::DispatchSubmitFrameResult, mDisplayInfo.mDisplayID, result
        ));
      }
      break;
    }
    case 2:
    {
      MOZ_ASSERT(false, "No support for showing VR frames on MacOSX yet.");
      break;
    }
  }

  return false;
}

#elif defined(MOZ_WIDGET_ANDROID)

bool
VRDisplayPuppet::SubmitFrame(const mozilla::layers::SurfaceTextureDescriptor& aDescriptor,
                             const gfx::Rect& aLeftEyeRect,
                             const gfx::Rect& aRightEyeRect)
{
  MOZ_ASSERT(mSubmitThread->GetThread() == NS_GetCurrentThread());
  return false;
}

#endif

void
VRDisplayPuppet::Refresh()
{
  // We update mIsConneced once per refresh.
  mDisplayInfo.mDisplayState.mIsConnected = true;
}

VRControllerPuppet::VRControllerPuppet(dom::GamepadHand aHand, uint32_t aDisplayID)
  : VRControllerHost(VRDeviceType::Puppet, aHand, aDisplayID)
  , mButtonPressState(0)
  , mButtonTouchState(0)
{
  MOZ_COUNT_CTOR_INHERITED(VRControllerPuppet, VRControllerHost);
  VRControllerState& state = mControllerInfo.mControllerState;
  strncpy(state.controllerName, "Puppet Gamepad", kVRControllerNameMaxLen);
  state.numButtons = kNumPuppetButtonMask;
  state.numAxes = kNumPuppetAxis;
  state.numHaptics = kNumPuppetHaptcs;
}

VRControllerPuppet::~VRControllerPuppet()
{
  MOZ_COUNT_DTOR_INHERITED(VRControllerPuppet, VRControllerHost);
}

void
VRControllerPuppet::SetButtonPressState(uint32_t aButton, bool aPressed)
{
  const uint64_t buttonMask = kPuppetButtonMask[aButton];
  uint64_t pressedBit = GetButtonPressed();

  if (aPressed) {
    pressedBit |= kPuppetButtonMask[aButton];
  } else if (pressedBit & buttonMask) {
    // this button was pressed but is released now.
    uint64_t mask = 0xff ^ buttonMask;
    pressedBit &= mask;
  }

  mButtonPressState = pressedBit;
}

uint64_t
VRControllerPuppet::GetButtonPressState()
{
  return mButtonPressState;
}

void
VRControllerPuppet::SetButtonTouchState(uint32_t aButton, bool aTouched)
{
  const uint64_t buttonMask = kPuppetButtonMask[aButton];
  uint64_t touchedBit = GetButtonTouched();

  if (aTouched) {
    touchedBit |= kPuppetButtonMask[aButton];
  } else if (touchedBit & buttonMask) {
    // this button was touched but is released now.
    uint64_t mask = 0xff ^ buttonMask;
    touchedBit &= mask;
  }

  mButtonTouchState = touchedBit;
}

uint64_t
VRControllerPuppet::GetButtonTouchState()
{
  return mButtonTouchState;
}

void
VRControllerPuppet::SetAxisMoveState(uint32_t aAxis, double aValue)
{
  MOZ_ASSERT((sizeof(mAxisMoveState) / sizeof(float)) == kNumPuppetAxis);
  MOZ_ASSERT(aAxis <= kNumPuppetAxis);

  mAxisMoveState[aAxis] = aValue;
}

double
VRControllerPuppet::GetAxisMoveState(uint32_t aAxis)
{
  return mAxisMoveState[aAxis];
}

void
VRControllerPuppet::SetPoseMoveState(const dom::GamepadPoseState& aPose)
{
  mPoseState = aPose;
}

const dom::GamepadPoseState&
VRControllerPuppet::GetPoseMoveState()
{
  return mPoseState;
}

float
VRControllerPuppet::GetAxisMove(uint32_t aAxis)
{
  return mControllerInfo.mControllerState.axisValue[aAxis];
}

void
VRControllerPuppet::SetAxisMove(uint32_t aAxis, float aValue)
{
  mControllerInfo.mControllerState.axisValue[aAxis] = aValue;
}

VRSystemManagerPuppet::VRSystemManagerPuppet()
  : mPuppetDisplayCount(0)
  , mPuppetDisplayInfo{}
  , mPuppetDisplaySensorState{}
{
}

/*static*/ already_AddRefed<VRSystemManagerPuppet>
VRSystemManagerPuppet::Create()
{
  if (!gfxPrefs::VREnabled() || !gfxPrefs::VRPuppetEnabled()) {
    return nullptr;
  }

  RefPtr<VRSystemManagerPuppet> manager = new VRSystemManagerPuppet();
  return manager.forget();
}

void
VRSystemManagerPuppet::Destroy()
{
  Shutdown();
}

void
VRSystemManagerPuppet::Shutdown()
{
  mPuppetHMDs.Clear();
}

void
VRSystemManagerPuppet::NotifyVSync()
{
  VRSystemManager::NotifyVSync();

  for (const auto& display: mPuppetHMDs) {
    display->Refresh();
  }
}

uint32_t
VRSystemManagerPuppet::CreateTestDisplay()
{
  if (mPuppetDisplayCount >= kMaxPuppetDisplays) {
    MOZ_ASSERT(false);
    return mPuppetDisplayCount;
  }
  return mPuppetDisplayCount++;
}

void
VRSystemManagerPuppet::ClearTestDisplays()
{
  mPuppetDisplayCount = 0;
}

void
VRSystemManagerPuppet::Enumerate()
{
  while (mPuppetHMDs.Length() < mPuppetDisplayCount) {
    VRDisplayPuppet* puppetDisplay = new VRDisplayPuppet();
    uint32_t deviceID = mPuppetHMDs.Length();
    puppetDisplay->SetDisplayInfo(mPuppetDisplayInfo[deviceID]);
    puppetDisplay->SetSensorState(mPuppetDisplaySensorState[deviceID]);
    mPuppetHMDs.AppendElement(puppetDisplay);
  }
  while (mPuppetHMDs.Length() > mPuppetDisplayCount) {
    mPuppetHMDs.RemoveLastElement();
  }
}

void
VRSystemManagerPuppet::SetPuppetDisplayInfo(const uint32_t& aDeviceID,
                                            const VRDisplayInfo& aDisplayInfo)
{
  if (aDeviceID >= mPuppetDisplayCount) {
    MOZ_ASSERT(false);
    return;
  }
  /**
   * Even if mPuppetHMDs.Length() <= aDeviceID, we need to
   * update mPuppetDisplayInfo[aDeviceID].  In the case that
   * a puppet display is added and SetPuppetDisplayInfo is
   * immediately called, mPuppetHMDs may not be populated yet.
   * VRSystemManagerPuppet::Enumerate() will initialize
   * the VRDisplayPuppet later using mPuppetDisplayInfo.
   */
  mPuppetDisplayInfo[aDeviceID] = aDisplayInfo;
  if (mPuppetHMDs.Length() > aDeviceID) {
    /**
     * In the event that the VRDisplayPuppet has already been
     * created, we update it directly.
     */
    mPuppetHMDs[aDeviceID]->SetDisplayInfo(aDisplayInfo);
  }
}

void
VRSystemManagerPuppet::SetPuppetDisplaySensorState(const uint32_t& aDeviceID,
                                                   const VRHMDSensorState& aSensorState)
{
  if (aDeviceID >= mPuppetDisplayCount) {
    MOZ_ASSERT(false);
    return;
  }
  /**
   * Even if mPuppetHMDs.Length() <= aDeviceID, we need to
   * update mPuppetDisplaySensorState[aDeviceID].  In the case that
   * a puppet display is added and SetPuppetDisplaySensorState is
   * immediately called, mPuppetHMDs may not be populated yet.
   * VRSystemManagerPuppet::Enumerate() will initialize
   * the VRDisplayPuppet later using mPuppetDisplaySensorState.
   */
  mPuppetDisplaySensorState[aDeviceID] = aSensorState;
  if (mPuppetHMDs.Length() > aDeviceID) {
    /**
     * In the event that the VRDisplayPuppet has already been
     * created, we update it directly.
     */
    mPuppetHMDs[aDeviceID]->SetSensorState(aSensorState);
  }
}

void
VRSystemManagerPuppet::GetHMDs(nsTArray<RefPtr<VRDisplayHost>>& aHMDResult)
{
  for (auto display: mPuppetHMDs) {
    aHMDResult.AppendElement(display);
  }
}

bool
VRSystemManagerPuppet::GetIsPresenting()
{
  for (const auto& display: mPuppetHMDs) {
    const VRDisplayInfo& displayInfo(display->GetDisplayInfo());
    if (displayInfo.GetPresentingGroups() != kVRGroupNone) {
      return true;
    }
  }
  return false;
}

void
VRSystemManagerPuppet::HandleInput()
{
  RefPtr<impl::VRControllerPuppet> controller;
  for (uint32_t i = 0; i < mPuppetController.Length(); ++i) {
    controller = mPuppetController[i];
    for (uint32_t j = 0; j < kNumPuppetButtonMask; ++j) {
      HandleButtonPress(i, j, kPuppetButtonMask[j], controller->GetButtonPressState(),
                        controller->GetButtonTouchState());
    }
    controller->SetButtonPressed(controller->GetButtonPressState());
    controller->SetButtonTouched(controller->GetButtonTouchState());

    for (uint32_t j = 0; j < kNumPuppetAxis; ++j) {
      HandleAxisMove(i, j, controller->GetAxisMoveState(j));
    }
    HandlePoseTracking(i, controller->GetPoseMoveState(), controller);
  }
}

void
VRSystemManagerPuppet::HandleButtonPress(uint32_t aControllerIdx,
                                         uint32_t aButton,
                                         uint64_t aButtonMask,
                                         uint64_t aButtonPressed,
                                         uint64_t aButtonTouched)
{
  RefPtr<impl::VRControllerPuppet> controller(mPuppetController[aControllerIdx]);
  MOZ_ASSERT(controller);
  const uint64_t pressedDiff = (controller->GetButtonPressed() ^ aButtonPressed);
  const uint64_t touchedDiff = (controller->GetButtonTouched() ^ aButtonTouched);

  if (!pressedDiff && !touchedDiff) {
    return;
  }

   if (pressedDiff & aButtonMask
      || touchedDiff & aButtonMask) {
    // diff & (aButtonPressed, aButtonTouched) would be true while a new button pressed or
    // touched event, otherwise it is an old event and needs to notify
    // the button has been released.
    NewButtonEvent(aControllerIdx, aButton, aButtonMask & aButtonPressed,
                   aButtonMask & aButtonPressed,
                   (aButtonMask & aButtonPressed) ? 1.0L : 0.0L);
  }
}

void
VRSystemManagerPuppet::HandleAxisMove(uint32_t aControllerIdx, uint32_t aAxis,
                                      float aValue)
{
  RefPtr<impl::VRControllerPuppet> controller(mPuppetController[aControllerIdx]);
  MOZ_ASSERT(controller);

  if (controller->GetAxisMove(aAxis) != aValue) {
    NewAxisMove(aControllerIdx, aAxis, aValue);
    controller->SetAxisMove(aAxis, aValue);
  }
}

void
VRSystemManagerPuppet::HandlePoseTracking(uint32_t aControllerIdx,
                                          const dom::GamepadPoseState& aPose,
                                          VRControllerHost* aController)
{
  MOZ_ASSERT(aController);
  if (aPose != aController->GetPose()) {
    aController->SetPose(aPose);
    NewPoseState(aControllerIdx, aPose);
  }
}

void
VRSystemManagerPuppet::VibrateHaptic(uint32_t aControllerIdx,
                                     uint32_t aHapticIndex,
                                     double aIntensity,
                                     double aDuration,
                                     const VRManagerPromise& aPromise)
{
}

void
VRSystemManagerPuppet::StopVibrateHaptic(uint32_t aControllerIdx)
{
}

void
VRSystemManagerPuppet::GetControllers(nsTArray<RefPtr<VRControllerHost>>& aControllerResult)
{
  aControllerResult.Clear();
  for (uint32_t i = 0; i < mPuppetController.Length(); ++i) {
    aControllerResult.AppendElement(mPuppetController[i]);
  }
}

void
VRSystemManagerPuppet::ScanForControllers()
{
  // We make sure VRSystemManagerPuppet has two controllers
  // for each display
  const uint32_t newControllerCount = mPuppetHMDs.Length() * 2;

  if (newControllerCount != mControllerCount) {
    RemoveControllers();

    // Re-adding controllers to VRControllerManager.
    for (const auto& display: mPuppetHMDs) {
      uint32_t displayID = display->GetDisplayInfo().GetDisplayID();
      for (uint32_t i = 0; i < 2; i++) {
        dom::GamepadHand hand = (i % 2) ? dom::GamepadHand::Right :
                                          dom::GamepadHand::Left;
        RefPtr<VRControllerPuppet> puppetController;
        puppetController = new VRControllerPuppet(hand, displayID);
        mPuppetController.AppendElement(puppetController);

        // Not already present, add it.
        AddGamepad(puppetController->GetControllerInfo());
        ++mControllerCount;
      }
    }
  }
}

void
VRSystemManagerPuppet::RemoveControllers()
{
  // controller count is changed, removing the existing gamepads first.
  for (uint32_t i = 0; i < mPuppetController.Length(); ++i) {
    RemoveGamepad(i);
  }
  mPuppetController.Clear();
  mControllerCount = 0;
}