Commit e0e54098 authored by Markus Stange's avatar Markus Stange
Browse files

Bug 1574592 - Move NativeLayer management out of nsChildView and into...

Bug 1574592 - Move NativeLayer management out of nsChildView and into LayerManagerComposite and RendererOGL. r=mattwoodrow

It looks like a big patch but it's mostly just moved code, with some duplication:
 - Layer creation and destruction moves to LayerManagerComposite and RendererOGL.
 - BasicCompositor IOSurface setup code moves to BasicCompositor.cpp.
 - OpenGL IOSurface setup code moves to CompositorOGL and RenderCompositorOGL.

The duplication is a bit unfortunate but the LayerManagerComposite code will
diverge from the WebRender code soon.

BeginFrame gets a new argument aNativeLayer. This argument will go away again
over the course of this patch queue. But for now, BeginFrame is the best place
to do the layer setup because it's a very close place to PreRender which is
where that code was previously.

I wasn't able to think of a nice way to give CompositorOGL and BasicCompositor
platform-specific behavior without #ifdefs. So now LayerManagerComposite uses
the "cross-platform" NativeLayer interface, but CompositorOGL and
BasicCompositor use NativeLayerCA because they actually need the IOSurface, and
they do that in #ifdef'd code.
Luckily, NativeLayerCA.h can be included in both .cpp files and in .mm files.

Differential Revision: https://phabricator.services.mozilla.com/D42402

--HG--
extra : moz-landing-system : lando
parent c3d94ae2
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -130,6 +130,7 @@ class DataTextureSource;
class CompositingRenderTarget;
class CompositorBridgeParent;
class LayerManagerComposite;
class NativeLayer;
class CompositorOGL;
class CompositorD3D11;
class BasicCompositor;
@@ -422,6 +423,7 @@ class Compositor : public TextureSourceProvider {
                          const gfx::IntRect* aClipRectIn,
                          const gfx::IntRect& aRenderBounds,
                          const nsIntRegion& aOpaqueRegion,
                          NativeLayer* aNativeLayer,
                          gfx::IntRect* aClipRectOut = nullptr,
                          gfx::IntRect* aRenderBoundsOut = nullptr) = 0;

+48 −3
Original line number Diff line number Diff line
@@ -27,6 +27,11 @@
#include <algorithm>
#include "ImageContainer.h"

#ifdef XP_MACOSX
#  include "mozilla/gfx/MacIOSurface.h"
#  include "mozilla/layers/NativeLayerCA.h"
#endif

namespace mozilla {
using namespace mozilla::gfx;

@@ -884,7 +889,7 @@ bool BasicCompositor::BlitRenderTarget(CompositingRenderTarget* aSource,
void BasicCompositor::BeginFrame(
    const nsIntRegion& aInvalidRegion, const gfx::IntRect* aClipRectIn,
    const gfx::IntRect& aRenderBounds, const nsIntRegion& aOpaqueRegion,
    gfx::IntRect* aClipRectOut /* = nullptr */,
    NativeLayer* aNativeLayer, gfx::IntRect* aClipRectOut /* = nullptr */,
    gfx::IntRect* aRenderBoundsOut /* = nullptr */) {
  if (mIsPendingEndRemoteDrawing) {
    // Force to end previous remote drawing.
@@ -926,6 +931,31 @@ void BasicCompositor::BeginFrame(
    mDrawTarget = mTarget;
    mDrawTargetBounds = mTargetBounds;
    bufferMode = BufferMode::BUFFER_NONE;
  } else if (aNativeLayer) {
#ifdef XP_MACOSX
    if (mInvalidRect.IsEmpty()) {
      return;
    }
    NativeLayerCA* nativeLayer = aNativeLayer->AsNativeLayerCA();
    MOZ_RELEASE_ASSERT(nativeLayer, "Unexpected native layer type");
    nativeLayer->SetSurfaceIsFlipped(false);
    CFTypeRefPtr<IOSurfaceRef> surf = nativeLayer->NextSurface();
    if (!surf) {
      return;
    }
    nativeLayer->InvalidateRegionThroughoutSwapchain(mInvalidRegion);
    mInvalidRegion = nativeLayer->CurrentSurfaceInvalidRegion();
    mInvalidRect = mInvalidRegion.GetBounds();
    MOZ_RELEASE_ASSERT(!mInvalidRect.IsEmpty());
    mCurrentNativeLayer = aNativeLayer;
    mCurrentIOSurface = new MacIOSurface(std::move(surf));
    mCurrentIOSurface->Lock(false);
    mDrawTarget = mCurrentIOSurface->GetAsDrawTargetLocked(BackendType::SKIA);
    mDrawTargetBounds = IntRect(IntPoint(0, 0), mDrawTarget->GetSize());
    bufferMode = BufferMode::BUFFER_NONE;
#else
    MOZ_CRASH("Unexpected native layer on this platform");
#endif
  } else {
    LayoutDeviceIntRegion invalidRegion =
        LayoutDeviceIntRegion::FromUnknownRegion(mInvalidRegion);
@@ -967,7 +997,7 @@ void BasicCompositor::BeginFrame(
      CreateRenderTargetForWindow(mInvalidRect, clearRegion, bufferMode);

  if (!target) {
    if (!mTarget) {
    if (!mTarget && !aNativeLayer) {
      mWidget->EndRemoteDrawingInRegion(
          mDrawTarget,
          LayoutDeviceIntRegion::FromUnknownRegion(mInvalidRegion));
@@ -1037,8 +1067,23 @@ void BasicCompositor::EndFrame() {
  // Reset the translation that was applied in CreateRenderTargetForWindow.
  mRenderTarget->mDrawTarget->SetTransform(gfx::Matrix());

  if (mCurrentNativeLayer) {
#ifdef XP_MACOSX
    NativeLayerCA* nativeLayer = mCurrentNativeLayer->AsNativeLayerCA();
    MOZ_RELEASE_ASSERT(nativeLayer, "Unexpected native layer type");
    mDrawTarget = nullptr;
    mRenderTarget = nullptr;
    mCurrentIOSurface->Unlock(false);
    mCurrentIOSurface = nullptr;
    nativeLayer->NotifySurfaceReady();
    mCurrentNativeLayer = nullptr;
#else
    MOZ_CRASH("Unexpected native layer on this platform");
#endif
  } else {
    TryToEndRemoteDrawing();
  }
}

void BasicCompositor::TryToEndRemoteDrawing(bool aForceToEnd) {
  if (mIsDestroyed || !mRenderTarget) {
+16 −1
Original line number Diff line number Diff line
@@ -13,6 +13,10 @@
#include "mozilla/gfx/Triangle.h"
#include "mozilla/gfx/Polygon.h"

#ifdef XP_MACOSX
class MacIOSurface;
#endif

namespace mozilla {
namespace layers {

@@ -115,7 +119,7 @@ class BasicCompositor : public Compositor {
  void BeginFrame(const nsIntRegion& aInvalidRegion,
                  const gfx::IntRect* aClipRectIn,
                  const gfx::IntRect& aRenderBounds,
                  const nsIntRegion& aOpaqueRegion,
                  const nsIntRegion& aOpaqueRegion, NativeLayer* aNativeLayer,
                  gfx::IntRect* aClipRectOut = nullptr,
                  gfx::IntRect* aRenderBoundsOut = nullptr) override;
  void NormalDrawingDone() override;
@@ -189,6 +193,17 @@ class BasicCompositor : public Compositor {
  // The current render target for drawing
  RefPtr<BasicCompositingRenderTarget> mRenderTarget;

  // The native layer that we're currently rendering to, if any.
  // Non-null only between BeginFrame and EndFrame if BeginFrame has been called
  // with a non-null aNativeLayer and mTarget is null.
  RefPtr<NativeLayer> mCurrentNativeLayer;

#ifdef XP_MACOSX
  // The MacIOSurface that we're currently rendering to, if any.
  // Non-null in the same cases as mCurrentNativeLayer.
  RefPtr<MacIOSurface> mCurrentIOSurface;
#endif

  gfx::IntRect mInvalidRect;
  gfx::IntRegion mInvalidRegion;

+24 −4
Original line number Diff line number Diff line
@@ -154,6 +154,11 @@ LayerManagerComposite::LayerManagerComposite(Compositor* aCompositor)
  mTextRenderer = new TextRenderer();
  mDiagnostics = MakeUnique<Diagnostics>();
  MOZ_ASSERT(aCompositor);
  mNativeLayerRoot = aCompositor->GetWidget()->GetNativeLayerRoot();
  if (mNativeLayerRoot) {
    mNativeLayerForEntireWindow = mNativeLayerRoot->CreateLayer();
    mNativeLayerRoot->AppendLayer(mNativeLayerForEntireWindow);
  }

#ifdef USE_SKIA
  mPaintCounter = nullptr;
@@ -172,6 +177,11 @@ void LayerManagerComposite::Destroy() {
    mRoot = nullptr;
    mClonedLayerTreeProperties = nullptr;
    mProfilerScreenshotGrabber.Destroy();
    if (mNativeLayerRoot) {
      mNativeLayerRoot->RemoveLayer(mNativeLayerForEntireWindow);
      mNativeLayerForEntireWindow = nullptr;
      mNativeLayerRoot = nullptr;
    }
    mDestroyed = true;

#ifdef USE_SKIA
@@ -971,16 +981,26 @@ bool LayerManagerComposite::Render(const nsIntRegion& aInvalidRegion,
    mCompositor->SetClearColorToDefault();
  }
#endif

  if (mNativeLayerForEntireWindow) {
    mNativeLayerForEntireWindow->SetRect(bounds);
#ifdef XP_MACOSX
    mNativeLayerForEntireWindow->SetOpaqueRegion(
        mCompositor->GetWidget()->GetOpaqueWidgetRegion().ToUnknownRegion());
#endif
  }

  if (mRoot->GetClipRect()) {
    clipRect = *mRoot->GetClipRect();
    IntRect rect(clipRect.X(), clipRect.Y(), clipRect.Width(),
                 clipRect.Height());
    mCompositor->BeginFrame(aInvalidRegion, &rect, bounds, aOpaqueRegion,
                            nullptr, &actualBounds);
                            mNativeLayerForEntireWindow, nullptr,
                            &actualBounds);
  } else {
    gfx::IntRect rect;
    mCompositor->BeginFrame(aInvalidRegion, nullptr, bounds, aOpaqueRegion,
                            &rect, &actualBounds);
                            mNativeLayerForEntireWindow, &rect, &actualBounds);
    clipRect =
        ParentLayerIntRect(rect.X(), rect.Y(), rect.Width(), rect.Height());
  }
@@ -1224,8 +1244,8 @@ void LayerManagerComposite::RenderToPresentationSurface() {
  IntRect bounds = IntRect::Truncate(0, 0, scale * pageWidth, actualHeight);
  IntRect rect, actualBounds;
  MOZ_ASSERT(mRoot->GetOpacity() == 1);
  mCompositor->BeginFrame(invalid, nullptr, bounds, nsIntRegion(), &rect,
                          &actualBounds);
  mCompositor->BeginFrame(invalid, nullptr, bounds, nsIntRegion(), nullptr,
                          &rect, &actualBounds);

  // The Java side of Fennec sets a scissor rect that accounts for
  // chrome such as the URL bar. Override that so that the entire frame buffer
+2 −0
Original line number Diff line number Diff line
@@ -465,6 +465,8 @@ class LayerManagerComposite final : public HostLayerManager {
  RefPtr<CompositingRenderTarget> mTwoPassTmpTarget;
  CompositorScreenshotGrabber mProfilerScreenshotGrabber;
  RefPtr<TextRenderer> mTextRenderer;
  RefPtr<NativeLayerRoot> mNativeLayerRoot;
  RefPtr<NativeLayer> mNativeLayerForEntireWindow;

#ifdef USE_SKIA
  /**
Loading