Bug 1343730 - Part 2: Get the submitframe result from VRDisplay; r?kip, baku draft
authorDaosheng Mu <daoshengmu@gmail.com>
Tue, 23 May 2017 17:13:15 +0800
changeset 583573 2f4e59665f5d8d15025cb7633ccda63b9cb0ab8c
parent 583416 2cb9d263607ada53dbb080e5c4754482d0cd181c
child 630113 e256a507ae8e18287deb175134c547377f1591c8
push id60453
push userbmo:dmu@mozilla.com
push dateWed, 24 May 2017 08:38:51 +0000
reviewerskip, baku
bugs1343730
milestone55.0a1
Bug 1343730 - Part 2: Get the submitframe result from VRDisplay; r?kip, baku MozReview-Commit-ID: 91SqChn6jlO
dom/vr/VRDisplay.cpp
dom/vr/VRDisplay.h
dom/webidl/VRDisplay.webidl
--- a/dom/vr/VRDisplay.cpp
+++ b/dom/vr/VRDisplay.cpp
@@ -7,19 +7,22 @@
 #include "nsWrapperCache.h"
 
 #include "mozilla/dom/Element.h"
 #include "mozilla/dom/ElementBinding.h"
 #include "mozilla/dom/Promise.h"
 #include "mozilla/dom/VRDisplay.h"
 #include "mozilla/HoldDropJSObjects.h"
 #include "mozilla/dom/VRDisplayBinding.h"
+#include "mozilla/Base64.h"
 #include "mozilla/EventStateManager.h"
+#include "mozilla/gfx/DataSurfaceHelpers.h"
 #include "Navigator.h"
 #include "gfxPrefs.h"
+#include "gfxUtils.h"
 #include "gfxVR.h"
 #include "VRDisplayClient.h"
 #include "VRManagerChild.h"
 #include "VRDisplayPresentation.h"
 #include "nsIObserverService.h"
 #include "nsIFrame.h"
 #include "nsISupportsPrimitives.h"
 
@@ -436,16 +439,50 @@ VRDisplay::UpdateFrameInfo()
 bool
 VRDisplay::GetFrameData(VRFrameData& aFrameData)
 {
   UpdateFrameInfo();
   aFrameData.Update(mFrameInfo);
   return true;
 }
 
+bool
+VRDisplay::GetSubmitFrameResult(VRSubmitFrameResult& aResult)
+{
+  if (!mPresentation) {
+    return false;
+  }
+
+  VRSubmitFrameResultInfo resultInfo;
+  mClient->GetSubmitFrameResult(resultInfo);
+  if (!resultInfo.mBase64Image.Length()) {
+    return false;  // The submit frame result is not ready.
+  }
+
+  nsAutoCString decodedImg;
+  if (Base64Decode(resultInfo.mBase64Image, decodedImg) != NS_OK) {
+    MOZ_ASSERT(false, "Failed to do decode base64 images.");
+    return false;
+  }
+
+  const char* srcData = (decodedImg.get());
+  const gfx::IntSize size(resultInfo.mWidth, resultInfo.mHeight);
+  RefPtr<DataSourceSurface> dataSurface = gfx::CreateDataSourceSurfaceFromData(
+                                            size, resultInfo.mFormat, (uint8_t*)srcData,
+                                            StrideForFormatAndWidth(resultInfo.mFormat, resultInfo.mWidth));
+  if (!dataSurface || !dataSurface->IsValid()) {
+    MOZ_ASSERT(false, "dataSurface is null.");
+    return false;
+  }
+
+  nsAutoCString encodedImg(gfxUtils::GetAsDataURI(dataSurface));
+  aResult.Update(resultInfo.mFrameNum, encodedImg);
+  return true;
+}
+
 already_AddRefed<VRPose>
 VRDisplay::GetPose()
 {
   UpdateFrameInfo();
   RefPtr<VRPose> obj = new VRPose(GetParentObject(), mFrameInfo.mVRState);
 
   return obj.forget();
 }
@@ -869,10 +906,59 @@ VRFrameInfo::IsDirty()
 }
 
 void
 VRFrameInfo::Clear()
 {
   mVRState.Clear();
 }
 
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(VRSubmitFrameResult, mParent)
+NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(VRSubmitFrameResult, AddRef)
+NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(VRSubmitFrameResult, Release)
+
+VRSubmitFrameResult::VRSubmitFrameResult(nsISupports* aParent)
+  : mParent(aParent)
+  , mFrameNum(0)
+{
+  mozilla::HoldJSObjects(this);
+}
+
+VRSubmitFrameResult::~VRSubmitFrameResult()
+{
+  mozilla::DropJSObjects(this);
+}
+
+/* static */ already_AddRefed<VRSubmitFrameResult>
+VRSubmitFrameResult::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
+{
+  RefPtr<VRSubmitFrameResult> obj = new VRSubmitFrameResult(aGlobal.GetAsSupports());
+  return obj.forget();
+}
+
+JSObject*
+VRSubmitFrameResult::WrapObject(JSContext* aCx,
+                                JS::Handle<JSObject*> aGivenProto)
+{
+  return VRSubmitFrameResultBinding::Wrap(aCx, this, aGivenProto);
+}
+
+void
+VRSubmitFrameResult::Update(uint32_t aFrameNum, const nsACString& aBase64Image)
+{
+  mFrameNum = aFrameNum;
+  mBase64Image = NS_ConvertASCIItoUTF16(aBase64Image);
+}
+
+double
+VRSubmitFrameResult::FrameNum() const
+{
+  return mFrameNum;
+}
+
+void
+VRSubmitFrameResult::GetBase64Image(nsAString& aImage) const
+{
+  aImage = mBase64Image;
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/vr/VRDisplay.h
+++ b/dom/vr/VRDisplay.h
@@ -266,16 +266,43 @@ protected:
 
 
   gfx::Point3D mEyeTranslation;
   gfx::IntSize mRenderSize;
   JS::Heap<JSObject*> mOffset;
   RefPtr<VRFieldOfView> mFOV;
 };
 
+class VRSubmitFrameResult final : public nsWrapperCache
+{
+public:
+  NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(VRSubmitFrameResult)
+  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(VRSubmitFrameResult)
+
+  explicit VRSubmitFrameResult(nsISupports* aParent);
+  static already_AddRefed<VRSubmitFrameResult> Constructor(const GlobalObject& aGlobal,
+                                                           ErrorResult& aRv);
+
+  void Update(uint32_t aFrameNum, const nsACString& aBase64Image);
+  // WebIDL Members
+  double FrameNum() const;
+  void GetBase64Image(nsAString& aImage) const;
+
+  // WebIDL Boilerplate
+  nsISupports* GetParentObject() const { return mParent; }
+  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
+
+protected:
+  ~VRSubmitFrameResult();
+
+  nsCOMPtr<nsISupports> mParent;
+  nsString mBase64Image;
+  uint32_t mFrameNum;
+};
+
 class VRDisplay final : public DOMEventTargetHelper
                       , public nsIObserver
 {
 public:
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_NSIOBSERVER
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(VRDisplay, DOMEventTargetHelper)
 
@@ -297,16 +324,17 @@ public:
 
   gfx::VRDisplayClient *GetClient() {
     return mClient;
   }
 
   virtual already_AddRefed<VREyeParameters> GetEyeParameters(VREye aEye);
 
   bool GetFrameData(VRFrameData& aFrameData);
+  bool GetSubmitFrameResult(VRSubmitFrameResult& aResult);
   already_AddRefed<VRPose> GetPose();
   void ResetPose();
 
   double DepthNear() {
     return mDepthNear;
   }
 
   double DepthFar() {
--- a/dom/webidl/VRDisplay.webidl
+++ b/dom/webidl/VRDisplay.webidl
@@ -144,16 +144,24 @@ interface VRFrameData {
   [Throws, Pure] readonly attribute Float32Array leftViewMatrix;
 
   [Throws, Pure] readonly attribute Float32Array rightProjectionMatrix;
   [Throws, Pure] readonly attribute Float32Array rightViewMatrix;
 
   [Pure] readonly attribute VRPose pose;
 };
 
+[Constructor,
+ Pref="dom.vr.test.enabled",
+ HeaderFile="mozilla/dom/VRDisplay.h"]
+interface VRSubmitFrameResult {
+  readonly attribute unsigned long frameNum;
+  readonly attribute DOMString? base64Image;
+};
+
 [Pref="dom.vr.enabled",
  HeaderFile="mozilla/dom/VRDisplay.h"]
 interface VREyeParameters {
   /**
    * offset is a 3-component vector representing an offset to
    * translate the eye. This value may vary from frame
    * to frame if the user adjusts their headset ipd.
    */
@@ -215,16 +223,19 @@ interface VRDisplay : EventTarget {
    * MUST return a VRPose with the same values until the next call to
    * submitFrame().
    *
    * The VRPose will contain the position, orientation, velocity,
    * and acceleration of each of these properties.
    */
   [NewObject] VRPose getPose();
 
+  [Pref="dom.vr.test.enabled"]
+  boolean getSubmitFrameResult(VRSubmitFrameResult result);
+
   /**
    * Reset the pose for this display, treating its current position and
    * orientation as the "origin/zero" values. VRPose.position,
    * VRPose.orientation, and VRStageParameters.sittingToStandingTransform may be
    * updated when calling resetPose(). This should be called in only
    * sitting-space experiences.
    */
   void resetPose();