/* 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 "mozilla/layers/NativeLayerRootRemoteWaylandParent.h" #include "mozilla/CheckedInt.h" #include "mozilla/gfx/Logging.h" #include "mozilla/layers/NativeLayerWayland.h" #include "mozilla/widget/DMABufSurface.h" #include "mozilla/widget/WaylandBuffer.h" #include "xpcpublic.h" namespace mozilla { namespace layers { NativeLayerRootRemoteWaylandParent::NativeLayerRootRemoteWaylandParent( RefPtr aRealNativeLayerRoot) : mRealNativeLayerRoot(aRealNativeLayerRoot) { MOZ_ASSERT(mRealNativeLayerRoot); } mozilla::ipc::IPCResult NativeLayerRootRemoteWaylandParent::RecvCommitNativeLayerCommands( nsTArray&& aCommands) { for (auto& command : aCommands) { switch (command.type()) { case NativeLayerCommand::TCommandCreateLayer: { auto& createLayer = command.get_CommandCreateLayer(); HandleCreateLayer(createLayer.ID(), createLayer.Size(), createLayer.Opaque()); break; } case NativeLayerCommand::TCommandCreateLayerForExternalTexture: { auto& createLayerForExternalTexture = command.get_CommandCreateLayerForExternalTexture(); HandleCreateLayerForExternalTexture( createLayerForExternalTexture.ID(), createLayerForExternalTexture.Opaque()); break; } case NativeLayerCommand::TCommandCreateLayerForColor: { auto& createLayerForColor = command.get_CommandCreateLayerForColor(); HandleCreateLayerForColor(createLayerForColor.ID(), createLayerForColor.Color()); break; } case NativeLayerCommand::TCommandLayerDestroyed: { auto& layerDestroyed = command.get_CommandLayerDestroyed(); HandleLayerDestroyed(layerDestroyed.ID()); break; } case NativeLayerCommand::TCommandSetLayers: { auto& setLayers = command.get_CommandSetLayers(); HandleSetLayers(setLayers.IDs()); break; } case NativeLayerCommand::TCommandLayerInfo: { auto& layerInfo = command.get_CommandLayerInfo(); HandleLayerInfo(layerInfo.ID(), layerInfo.Position(), layerInfo.DisplayRect(), layerInfo.ClipRect(), layerInfo.RoundedClipRect(), layerInfo.Transform(), layerInfo.samplingFilter(), layerInfo.SurfaceIsFlipped()); break; } case NativeLayerCommand::TCommandChangedSurface: { auto& changedSurface = command.get_CommandChangedSurface(); auto& transfer = changedSurface.Transfer(); auto& transferWayland = transfer.get_SurfaceTransferWayland(); HandleChangedSurface(changedSurface.ID(), transferWayland.Surface(), changedSurface.IsDRM(), changedSurface.IsHDR(), changedSurface.Size()); break; } default: { gfxWarning() << "Unknown NativeLayerCommand."; break; } } } mRealNativeLayerRoot->CommitToScreen(); return IPC_OK(); } mozilla::ipc::IPCResult NativeLayerRootRemoteWaylandParent::RecvRequestReadback( IntSize aSize, Shmem* const aPixels) { if (!xpc::IsInAutomation()) { return IPC_FAIL(this, "Should only be called from automation."); } // Actually do a snapshot on mRealNativeLayerRoot. // TODO: we'll probably have to handle higher bit depth formats at some point // with the upcoming HDR work, but for now assume B8G8R8A8. auto readbackFormat = gfx::SurfaceFormat::B8G8R8A8; auto readbackSize = (CheckedUint32(aSize.width) * aSize.height * gfx::BytesPerPixel(readbackFormat)); if (!readbackSize.isValid()) { return IPC_FAIL(this, "Invalid readback size."); } Shmem buffer; if (!AllocShmem(readbackSize.value(), &buffer)) { return IPC_FAIL(this, "Can't allocate shmem."); } if (!mSnapshotter) { mSnapshotter = mRealNativeLayerRoot->CreateSnapshotter(); if (!mSnapshotter) { return IPC_FAIL(this, "Can't create parent-side snapshotter."); } } if (!mSnapshotter->ReadbackPixels(aSize, readbackFormat, buffer.Range())) { return IPC_FAIL(this, "Failed readback."); } *aPixels = buffer; return IPC_OK(); } mozilla::ipc::IPCResult NativeLayerRootRemoteWaylandParent::RecvFlush() { // No-op message; when this returns, the other side knows that any // preceding messages have finished processing. return IPC_OK(); } void NativeLayerRootRemoteWaylandParent::HandleCreateLayer(uint64_t aID, IntSize aSize, bool aOpaque) { RefPtr layer = mRealNativeLayerRoot->CreateLayerForSurfacePresentation(aSize, aOpaque); mKnownLayers.InsertOrUpdate(aID, layer); } void NativeLayerRootRemoteWaylandParent::HandleCreateLayerForExternalTexture( uint64_t aID, bool aOpaque) { // External images are rendered in the GPU process too, so they arrive here // as surfaces to present. The size is set with each surface. RefPtr layer = mRealNativeLayerRoot->CreateLayerForSurfacePresentation(IntSize(), aOpaque); mKnownLayers.InsertOrUpdate(aID, layer); } void NativeLayerRootRemoteWaylandParent::HandleCreateLayerForColor( uint64_t aID, DeviceColor aColor) { RefPtr layer = mRealNativeLayerRoot->CreateLayerForColor(aColor); mKnownLayers.InsertOrUpdate(aID, layer); } void NativeLayerRootRemoteWaylandParent::HandleLayerDestroyed(uint64_t aID) { mKnownLayers.Remove(aID); } void NativeLayerRootRemoteWaylandParent::HandleSetLayers( const nsTArray& aIDs) { nsTArray> layers; for (auto ID : aIDs) { auto entry = mKnownLayers.MaybeGet(ID); if (!entry) { gfxWarning() << "Got a SetLayers for an unknown layer."; continue; } RefPtr layer = *entry; layers.AppendElement(layer); } mRealNativeLayerRoot->SetLayers(layers); } void NativeLayerRootRemoteWaylandParent::HandleLayerInfo( uint64_t aID, IntPoint aPosition, IntRect aDisplayRect, Maybe aClipRect, Maybe aRoundedClipRect, Matrix4x4 aTransform, SamplingFilter aSamplingFilter, bool aSurfaceIsFlipped) { auto entry = mKnownLayers.MaybeGet(aID); if (!entry) { gfxWarning() << "Got a LayerInfo for an unknown layer."; return; } RefPtr layer = (*entry)->AsNativeLayerWayland(); MOZ_ASSERT(layer, "All of our known layers should be NativeLayerWayland."); // Set the other properties of layer. layer->SetPosition(aPosition); layer->SetDisplayRect(aDisplayRect); layer->SetClipRect(aClipRect); layer->SetRoundedClipRect(aRoundedClipRect); layer->SetTransform(aTransform); layer->SetSamplingFilter(aSamplingFilter); layer->SetSurfaceIsFlipped(aSurfaceIsFlipped); } void NativeLayerRootRemoteWaylandParent::HandleChangedSurface( uint64_t aID, const Maybe& aDesc, bool aIsDRM, bool aIsHDR, IntSize aSize) { // Import the surface first, even if the layer is unknown, so that the // reference the GPU process holds while the surface is in flight is always // released. Our imported surface holds its own reference until the Wayland // compositor releases the buffer, which tells the GPU process when it can // reuse it. RefPtr buffer; if (aDesc) { if (RefPtr surface = DMABufSurface::CreateDMABufSurface(SurfaceDescriptor(*aDesc))) { surface->GlobalRefRelease(); buffer = widget::WaylandBuffer::Create(surface); } else { gfxCriticalNoteOnce << "Failed to import DMABufSurface from GPU process."; } } auto entry = mKnownLayers.MaybeGet(aID); if (!entry) { gfxWarning() << "Got a ChangedSurface for an unknown layer."; return; } NativeLayerWayland* layerWayland = (*entry)->AsNativeLayerWayland(); if (RefPtr layer = layerWayland ? layerWayland->AsNativeLayerWaylandPresentation() : nullptr) { layer->SetBufferToPresent(std::move(buffer), aSize); } else { gfxWarning() << "Got a ChangedSurface for a layer that can't present."; } } } // namespace layers } // namespace mozilla