Bug 1440038 - Use SharedRGBImage in MediaEngineTabVideoSource r=pehrsons
authorsotaro <sotaro.ikeda.g@gmail.com>
Fri, 01 Mar 2019 08:45:50 +0900
changeset 519761 10df6785552dcb1aaac3dfbbd1b6fe099fee3437
parent 519760 c59e07cae80fcf325ff695b5e24e4aa823f034fd
child 519762 ac3671d0724b559483cad2aa0996c31db440772f
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspehrsons
bugs1440038
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1440038 - Use SharedRGBImage in MediaEngineTabVideoSource r=pehrsons Differential Revision: https://phabricator.services.mozilla.com/D21177
dom/media/webrtc/MediaEngineTabVideoSource.cpp
dom/media/webrtc/MediaEngineTabVideoSource.h
--- a/dom/media/webrtc/MediaEngineTabVideoSource.cpp
+++ b/dom/media/webrtc/MediaEngineTabVideoSource.cpp
@@ -2,16 +2,18 @@
  * 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 "MediaEngineTabVideoSource.h"
 
 #include "mozilla/gfx/2D.h"
 #include "mozilla/gfx/DataSurfaceHelpers.h"
+#include "mozilla/layers/SharedRGBImage.h"
+#include "mozilla/layers/TextureClient.h"
 #include "mozilla/RefPtr.h"
 #include "mozilla/UniquePtrExtensions.h"
 #include "mozilla/dom/BindingDeclarations.h"
 #include "nsGlobalWindow.h"
 #include "nsIDocShell.h"
 #include "nsIPresShell.h"
 #include "nsPresContext.h"
 #include "gfxContext.h"
@@ -244,17 +246,17 @@ void MediaEngineTabVideoSource::Pull(
   {
     MutexAutoLock lock(mMutex);
     if (mState == kReleased) {
       // We end the track before setting the state to released.
       return;
     }
     if (mState == kStarted) {
       image = mImage;
-      imageSize = mImageSize;
+      imageSize = mImage ? mImage->GetSize() : IntSize();
     }
   }
 
   StreamTime delta = aDesiredTime - aEndOfAppendedData;
   MOZ_ASSERT(delta > 0);
 
   // nullptr images are allowed
   segment.AppendFrame(image.forget(), delta, imageSize, aPrincipalHandle);
@@ -300,45 +302,57 @@ void MediaEngineTabVideoSource::Draw() {
       const float scaleWidth = (float)mBufWidthMax / (float)deviceWidth;
       const float scaleHeight = (float)mBufHeightMax / (float)deviceHeight;
       const float scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
 
       size = IntSize((int)(scale * deviceWidth), (int)(scale * deviceHeight));
     }
   }
 
-  uint32_t stride =
-      StrideForFormatAndWidth(SurfaceFormat::X8R8G8B8_UINT32, size.width);
-
-  if (mDataSize < static_cast<size_t>(stride * size.height)) {
-    mDataSize = stride * size.height;
-    mData = MakeUniqueFallible<unsigned char[]>(mDataSize);
-  }
-  if (!mData) {
-    return;
-  }
-
   nsCOMPtr<nsIPresShell> presShell;
   if (mWindow) {
     nsIDocShell* docshell = mWindow->GetDocShell();
     if (docshell) {
       presShell = docshell->GetPresShell();
     }
     if (!presShell) {
       return;
     }
   }
 
-  RefPtr<layers::ImageContainer> container =
-      layers::LayerManager::CreateImageContainer(
-          layers::ImageContainer::ASYNCHRONOUS);
-  RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForData(
-      gfxPlatform::GetPlatform()->GetSoftwareBackend(), mData.get(), size,
-      stride, SurfaceFormat::B8G8R8X8, true);
+  if (!mImageContainer) {
+    mImageContainer = layers::LayerManager::CreateImageContainer(
+        layers::ImageContainer::ASYNCHRONOUS);
+  }
+
+  RefPtr<layers::SharedRGBImage> rgbImage = mImageContainer->CreateSharedRGBImage();
+  if (!rgbImage) {
+    NS_WARNING("Failed to create SharedRGBImage");
+    return;
+  }
+  if (!rgbImage->Allocate(size, SurfaceFormat::B8G8R8X8)) {
+    NS_WARNING("Failed to allocate a shared image");
+    return;
+  }
+
+  RefPtr<layers::TextureClient> texture = rgbImage->GetTextureClient(/* aForwarder */nullptr);
+  if (!texture) {
+    NS_WARNING("Failed to allocate TextureClient");
+    return;
+  }
+
+  layers::TextureClientAutoLock autoLock(texture, layers::OpenMode::OPEN_WRITE_ONLY);
+  if (!autoLock.Succeeded()) {
+    NS_WARNING("Failed to lock TextureClient");
+    return;
+  }
+
+  RefPtr<gfx::DrawTarget> dt = texture->BorrowDrawTarget();
   if (!dt || !dt->IsValid()) {
+    NS_WARNING("Failed to borrow DrawTarget");
     return;
   }
 
   if (mWindow) {
     RefPtr<gfxContext> context = gfxContext::CreateOrNull(dt);
     MOZ_ASSERT(context);  // already checked the draw target above
     context->SetMatrix(context->CurrentMatrix().PreScale(
         (((float)size.width) / mViewportWidth),
@@ -354,27 +368,18 @@ void MediaEngineTabVideoSource::Draw() {
              nsPresContext::CSSPixelsToAppUnits((float)mViewportWidth),
              nsPresContext::CSSPixelsToAppUnits((float)mViewportHeight));
     NS_ENSURE_SUCCESS_VOID(
         presShell->RenderDocument(r, renderDocFlags, bgColor, context));
   } else {
     dt->ClearRect(Rect(0, 0, size.width, size.height));
   }
 
-  RefPtr<SourceSurface> surface = dt->Snapshot();
-  if (!surface) {
-    return;
-  }
-
-  RefPtr<layers::SourceSurfaceImage> image =
-      new layers::SourceSurfaceImage(size, surface);
-
   MutexAutoLock lock(mMutex);
-  mImage = image;
-  mImageSize = size;
+  mImage = rgbImage;
 }
 
 nsresult MediaEngineTabVideoSource::FocusOnSelectedSource(
     const RefPtr<const AllocationHandle>& aHandle) {
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 nsresult MediaEngineTabVideoSource::Stop(
--- a/dom/media/webrtc/MediaEngineTabVideoSource.h
+++ b/dom/media/webrtc/MediaEngineTabVideoSource.h
@@ -6,16 +6,20 @@
 #include "ImageContainer.h"
 #include "nsITimer.h"
 #include "mozilla/Mutex.h"
 #include "mozilla/UniquePtr.h"
 #include "nsITabSource.h"
 
 namespace mozilla {
 
+namespace layers {
+class ImageContainer;
+}
+
 class MediaEngineTabVideoSource : public MediaEngineSource {
  public:
   MediaEngineTabVideoSource();
 
   nsString GetName() const override;
   nsCString GetUUID() const override;
 
   bool GetScary() const override { return true; }
@@ -100,31 +104,30 @@ class MediaEngineTabVideoSource : public
   int32_t mBufHeightMax = 0;
   int64_t mWindowId = 0;
   bool mScrollWithPage = 0;
   int32_t mViewportOffsetX = 0;
   int32_t mViewportOffsetY = 0;
   int32_t mViewportWidth = 0;
   int32_t mViewportHeight = 0;
   int32_t mTimePerFrame = 0;
-  UniquePtr<unsigned char[]> mData;
-  size_t mDataSize = 0;
+  RefPtr<layers::ImageContainer> mImageContainer;
+
   nsCOMPtr<nsPIDOMWindowOuter> mWindow;
   // If this is set, we will run despite mWindow == nullptr.
   bool mBlackedoutWindow = false;
   // Current state of this source.
   // Written on owning thread *and* under mMutex.
   // Can be read on owning thread *or* under mMutex.
   MediaEngineSourceState mState = kReleased;
   // mStream and mTrackID are set in SetTrack() to keep track of what to end
   // in Deallocate().
   // Owning thread only.
   RefPtr<SourceMediaStream> mStream;
   TrackID mTrackID = TRACK_NONE;
-  // mImage and mImageSize is Protected by mMutex.
-  RefPtr<layers::SourceSurfaceImage> mImage;
-  gfx::IntSize mImageSize;
+  // mImage is Protected by mMutex.
+  RefPtr<layers::Image> mImage;
   nsCOMPtr<nsITimer> mTimer;
   Mutex mMutex;
   nsCOMPtr<nsITabSource> mTabSource;
 };
 
 }  // namespace mozilla