tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

NativeLayerRootRemoteMacParent.mm (7273B)


      1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "mozilla/layers/NativeLayerRootRemoteMacParent.h"
      7 #include "xpcpublic.h"
      8 
      9 namespace mozilla {
     10 namespace layers {
     11 
     12 NativeLayerRootRemoteMacParent::NativeLayerRootRemoteMacParent(
     13    RefPtr<NativeLayerRootCA> aRealNativeLayerRoot)
     14    : mRealNativeLayerRoot(aRealNativeLayerRoot) {
     15  MOZ_ASSERT(mRealNativeLayerRoot);
     16 }
     17 
     18 mozilla::ipc::IPCResult
     19 NativeLayerRootRemoteMacParent::RecvCommitNativeLayerCommands(
     20    nsTArray<NativeLayerCommand>&& aCommands) {
     21  for (auto& command : aCommands) {
     22    switch (command.type()) {
     23      case NativeLayerCommand::TCommandCreateLayer: {
     24        auto& createLayer = command.get_CommandCreateLayer();
     25        HandleCreateLayer(createLayer.ID(), createLayer.Size(),
     26                          createLayer.Opaque());
     27        break;
     28      }
     29 
     30      case NativeLayerCommand::TCommandCreateLayerForExternalTexture: {
     31        auto& createLayerForExternalTexture =
     32            command.get_CommandCreateLayerForExternalTexture();
     33        HandleCreateLayerForExternalTexture(
     34            createLayerForExternalTexture.ID(),
     35            createLayerForExternalTexture.Opaque());
     36        break;
     37      }
     38 
     39      case NativeLayerCommand::TCommandCreateLayerForColor: {
     40        auto& createLayerForColor = command.get_CommandCreateLayerForColor();
     41        HandleCreateLayerForColor(createLayerForColor.ID(),
     42                                  createLayerForColor.Color());
     43        break;
     44      }
     45 
     46      case NativeLayerCommand::TCommandLayerDestroyed: {
     47        auto& layerDestroyed = command.get_CommandLayerDestroyed();
     48        HandleLayerDestroyed(layerDestroyed.ID());
     49        break;
     50      }
     51 
     52      case NativeLayerCommand::TCommandSetLayers: {
     53        auto& setLayers = command.get_CommandSetLayers();
     54        HandleSetLayers(setLayers.IDs());
     55        break;
     56      }
     57 
     58      case NativeLayerCommand::TCommandLayerInfo: {
     59        auto& layerInfo = command.get_CommandLayerInfo();
     60        HandleLayerInfo(layerInfo.ID(), layerInfo.Position(),
     61                        layerInfo.DisplayRect(), layerInfo.ClipRect(),
     62                        layerInfo.RoundedClipRect(), layerInfo.Transform(),
     63                        layerInfo.SamplingFilter(),
     64                        layerInfo.SurfaceIsFlipped());
     65        break;
     66      }
     67 
     68      case NativeLayerCommand::TCommandChangedSurface: {
     69        auto& changedSurface = command.get_CommandChangedSurface();
     70        HandleChangedSurface(changedSurface.ID(),
     71                             std::move(changedSurface.Surface()),
     72                             changedSurface.IsDRM(), changedSurface.IsHDR(),
     73                             changedSurface.Size());
     74        break;
     75      }
     76 
     77      default: {
     78        gfxWarning() << "Unknown NativeLayerCommand.";
     79        break;
     80      }
     81    }
     82  }
     83 
     84  mRealNativeLayerRoot->CommitToScreen();
     85 
     86  return IPC_OK();
     87 }
     88 
     89 mozilla::ipc::IPCResult NativeLayerRootRemoteMacParent::RecvRequestReadback(
     90    IntSize aSize, Shmem* const aPixels) {
     91  if (!xpc::IsInAutomation()) {
     92    return IPC_FAIL(this, "Should only be called from automation.");
     93  }
     94 
     95  // Actually do a snapshot on mRealNativeLayerRoot.
     96  // TODO: we'll probably have to handle higher bit depth formats at some point
     97  // with the upcoming HDR work, but for now assume B8G8R8A8.
     98  auto readbackFormat = gfx::SurfaceFormat::B8G8R8A8;
     99  size_t readbackSize =
    100      aSize.width * aSize.height * gfx::BytesPerPixel(readbackFormat);
    101  Shmem buffer;
    102  if (!AllocShmem(readbackSize, &buffer)) {
    103    return IPC_FAIL(this, "Can't allocate shmem.");
    104  }
    105 
    106  if (!mSnapshotter) {
    107    mSnapshotter = mRealNativeLayerRoot->CreateSnapshotter();
    108    if (!mSnapshotter) {
    109      return IPC_FAIL(this, "Can't create parent-side snapshotter.");
    110    }
    111  }
    112 
    113  if (!mSnapshotter->ReadbackPixels(aSize, readbackFormat,
    114                                    buffer.Range<uint8_t>())) {
    115    return IPC_FAIL(this, "Failed readback.");
    116  }
    117 
    118  *aPixels = buffer;
    119  return IPC_OK();
    120 }
    121 
    122 mozilla::ipc::IPCResult NativeLayerRootRemoteMacParent::RecvFlush() {
    123  // No-op message; when this returns, the other side knows that any
    124  // preceding messages have finished processing.
    125  return IPC_OK();
    126 }
    127 
    128 void NativeLayerRootRemoteMacParent::HandleCreateLayer(uint64_t aID,
    129                                                       IntSize aSize,
    130                                                       bool aOpaque) {
    131  RefPtr<NativeLayerCA> layer =
    132      mRealNativeLayerRoot->CreateLayerForSurfacePresentation(aSize, aOpaque);
    133  mKnownLayers.InsertOrUpdate(aID, layer);
    134 }
    135 
    136 void NativeLayerRootRemoteMacParent::HandleCreateLayerForExternalTexture(
    137    uint64_t aID, bool aOpaque) {
    138  RefPtr<NativeLayer> layer =
    139      mRealNativeLayerRoot->CreateLayerForExternalTexture(aOpaque);
    140  mKnownLayers.InsertOrUpdate(aID, layer);
    141 }
    142 
    143 void NativeLayerRootRemoteMacParent::HandleCreateLayerForColor(
    144    uint64_t aID, DeviceColor aColor) {
    145  RefPtr<NativeLayer> layer = mRealNativeLayerRoot->CreateLayerForColor(aColor);
    146  mKnownLayers.InsertOrUpdate(aID, layer);
    147 }
    148 
    149 void NativeLayerRootRemoteMacParent::HandleLayerDestroyed(uint64_t aID) {
    150  mKnownLayers.Remove(aID);
    151 }
    152 
    153 void NativeLayerRootRemoteMacParent::HandleSetLayers(
    154    const nsTArray<uint64_t>& aIDs) {
    155  nsTArray<RefPtr<NativeLayer>> layers;
    156  for (auto ID : aIDs) {
    157    auto entry = mKnownLayers.MaybeGet(ID);
    158    if (!entry) {
    159      gfxWarning() << "Got a SetLayers for an unknown layer.";
    160      continue;
    161    }
    162 
    163    RefPtr<NativeLayer> layer = *entry;
    164    layers.AppendElement(layer);
    165  }
    166  mRealNativeLayerRoot->SetLayers(layers);
    167 }
    168 
    169 void NativeLayerRootRemoteMacParent::HandleLayerInfo(
    170    uint64_t aID, IntPoint aPosition, IntRect aDisplayRect,
    171    Maybe<IntRect> aClipRect, Maybe<RoundedRect> aRoundedClipRect,
    172    Matrix4x4 aTransform, int8_t aSamplingFilter, bool aSurfaceIsFlipped) {
    173  auto entry = mKnownLayers.MaybeGet(aID);
    174  if (!entry) {
    175    gfxWarning() << "Got a LayerInfo for an unknown layer.";
    176    return;
    177  }
    178 
    179  RefPtr<NativeLayerCA> layer = (*entry)->AsNativeLayerCA();
    180  MOZ_ASSERT(layer, "All of our known layers should be NativeLayerCA.");
    181 
    182  // Set the other properties of layer.
    183  layer->SetPosition(aPosition);
    184  layer->SetDisplayRect(aDisplayRect);
    185  layer->SetClipRect(aClipRect);
    186  layer->SetRoundedClipRect(aRoundedClipRect);
    187  layer->SetTransform(aTransform);
    188  layer->SetSamplingFilter(static_cast<gfx::SamplingFilter>(aSamplingFilter));
    189  layer->SetSurfaceIsFlipped(aSurfaceIsFlipped);
    190 }
    191 
    192 void NativeLayerRootRemoteMacParent::HandleChangedSurface(
    193    uint64_t aID, IOSurfacePort aSurfacePort, bool aIsDRM, bool aIsHDR,
    194    IntSize aSize) {
    195  auto entry = mKnownLayers.MaybeGet(aID);
    196  if (!entry) {
    197    gfxWarning() << "Got a ChangedSurface for an unknown layer.";
    198    return;
    199  }
    200 
    201  RefPtr<NativeLayerCA> layer = (*entry)->AsNativeLayerCA();
    202  MOZ_ASSERT(layer, "All of our known layers should be NativeLayerCA.");
    203 
    204  if (auto surface = aSurfacePort.GetSurface()) {
    205    layer->SetSurfaceToPresent(surface, aSize, aIsDRM, aIsHDR);
    206  }
    207 }
    208 
    209 }  // namespace layers
    210 }  // namespace mozilla