File: NativeLayerRootRemoteMacParent.mm

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (210 lines) | stat: -rw-r--r-- 7,273 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * 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/NativeLayerRootRemoteMacParent.h"
#include "xpcpublic.h"

namespace mozilla {
namespace layers {

NativeLayerRootRemoteMacParent::NativeLayerRootRemoteMacParent(
    RefPtr<NativeLayerRootCA> aRealNativeLayerRoot)
    : mRealNativeLayerRoot(aRealNativeLayerRoot) {
  MOZ_ASSERT(mRealNativeLayerRoot);
}

mozilla::ipc::IPCResult
NativeLayerRootRemoteMacParent::RecvCommitNativeLayerCommands(
    nsTArray<NativeLayerCommand>&& 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();
        HandleChangedSurface(changedSurface.ID(),
                             std::move(changedSurface.Surface()),
                             changedSurface.IsDRM(), changedSurface.IsHDR(),
                             changedSurface.Size());
        break;
      }

      default: {
        gfxWarning() << "Unknown NativeLayerCommand.";
        break;
      }
    }
  }

  mRealNativeLayerRoot->CommitToScreen();

  return IPC_OK();
}

mozilla::ipc::IPCResult NativeLayerRootRemoteMacParent::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;
  size_t readbackSize =
      aSize.width * aSize.height * gfx::BytesPerPixel(readbackFormat);
  Shmem buffer;
  if (!AllocShmem(readbackSize, &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<uint8_t>())) {
    return IPC_FAIL(this, "Failed readback.");
  }

  *aPixels = buffer;
  return IPC_OK();
}

mozilla::ipc::IPCResult NativeLayerRootRemoteMacParent::RecvFlush() {
  // No-op message; when this returns, the other side knows that any
  // preceding messages have finished processing.
  return IPC_OK();
}

void NativeLayerRootRemoteMacParent::HandleCreateLayer(uint64_t aID,
                                                       IntSize aSize,
                                                       bool aOpaque) {
  RefPtr<NativeLayerCA> layer =
      mRealNativeLayerRoot->CreateLayerForSurfacePresentation(aSize, aOpaque);
  mKnownLayers.InsertOrUpdate(aID, layer);
}

void NativeLayerRootRemoteMacParent::HandleCreateLayerForExternalTexture(
    uint64_t aID, bool aOpaque) {
  RefPtr<NativeLayer> layer =
      mRealNativeLayerRoot->CreateLayerForExternalTexture(aOpaque);
  mKnownLayers.InsertOrUpdate(aID, layer);
}

void NativeLayerRootRemoteMacParent::HandleCreateLayerForColor(
    uint64_t aID, DeviceColor aColor) {
  RefPtr<NativeLayer> layer = mRealNativeLayerRoot->CreateLayerForColor(aColor);
  mKnownLayers.InsertOrUpdate(aID, layer);
}

void NativeLayerRootRemoteMacParent::HandleLayerDestroyed(uint64_t aID) {
  mKnownLayers.Remove(aID);
}

void NativeLayerRootRemoteMacParent::HandleSetLayers(
    const nsTArray<uint64_t>& aIDs) {
  nsTArray<RefPtr<NativeLayer>> layers;
  for (auto ID : aIDs) {
    auto entry = mKnownLayers.MaybeGet(ID);
    if (!entry) {
      gfxWarning() << "Got a SetLayers for an unknown layer.";
      continue;
    }

    RefPtr<NativeLayer> layer = *entry;
    layers.AppendElement(layer);
  }
  mRealNativeLayerRoot->SetLayers(layers);
}

void NativeLayerRootRemoteMacParent::HandleLayerInfo(
    uint64_t aID, IntPoint aPosition, IntRect aDisplayRect,
    Maybe<IntRect> aClipRect, Maybe<RoundedRect> aRoundedClipRect,
    Matrix4x4 aTransform, int8_t aSamplingFilter, bool aSurfaceIsFlipped) {
  auto entry = mKnownLayers.MaybeGet(aID);
  if (!entry) {
    gfxWarning() << "Got a LayerInfo for an unknown layer.";
    return;
  }

  RefPtr<NativeLayerCA> layer = (*entry)->AsNativeLayerCA();
  MOZ_ASSERT(layer, "All of our known layers should be NativeLayerCA.");

  // Set the other properties of layer.
  layer->SetPosition(aPosition);
  layer->SetDisplayRect(aDisplayRect);
  layer->SetClipRect(aClipRect);
  layer->SetRoundedClipRect(aRoundedClipRect);
  layer->SetTransform(aTransform);
  layer->SetSamplingFilter(static_cast<gfx::SamplingFilter>(aSamplingFilter));
  layer->SetSurfaceIsFlipped(aSurfaceIsFlipped);
}

void NativeLayerRootRemoteMacParent::HandleChangedSurface(
    uint64_t aID, IOSurfacePort aSurfacePort, bool aIsDRM, bool aIsHDR,
    IntSize aSize) {
  auto entry = mKnownLayers.MaybeGet(aID);
  if (!entry) {
    gfxWarning() << "Got a ChangedSurface for an unknown layer.";
    return;
  }

  RefPtr<NativeLayerCA> layer = (*entry)->AsNativeLayerCA();
  MOZ_ASSERT(layer, "All of our known layers should be NativeLayerCA.");

  if (auto surface = aSurfacePort.GetSurface()) {
    layer->SetSurfaceToPresent(surface, aSize, aIsDRM, aIsHDR);
  }
}

}  // namespace layers
}  // namespace mozilla