File: NativeLayerRemoteMac.mm

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; 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 (294 lines) | stat: -rw-r--r-- 8,766 bytes parent folder | download
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* -*- 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/NativeLayerRemoteMac.h"

#include <algorithm>
#include <utility>

#include "CFTypeRefPtr.h"
#include "gfxUtils.h"
#include "GLBlitHelper.h"
#ifdef XP_MACOSX
#  include "GLContextCGL.h"
#else
#  include "GLContextEAGL.h"
#endif
#include "GLContextProvider.h"
#include "MozFramebuffer.h"
#include "mozilla/gfx/Swizzle.h"
#include "mozilla/glean/GfxMetrics.h"
#include "mozilla/webrender/RenderMacIOSurfaceTextureHost.h"
#include "NativeLayerCA.h"
#include "ScopedGLHelpers.h"

namespace mozilla {
namespace layers {

using gfx::IntPoint;
using gfx::IntRect;
using gfx::IntRegion;
using gfx::IntSize;
using gfx::Matrix4x4;

NativeLayerRemoteMac::NativeLayerRemoteMac(
    const IntSize& aSize, bool aIsOpaque,
    SurfacePoolHandleCA* aSurfacePoolHandle)
    : mIsOpaque(aIsOpaque) {
  // We need a surface handler for this type of layer.
  mSurfaceHandler.emplace(aSize, aSurfacePoolHandle);
}

NativeLayerRemoteMac::NativeLayerRemoteMac(bool aIsOpaque)
    : mIsOpaque(aIsOpaque) {}

NativeLayerRemoteMac::NativeLayerRemoteMac(gfx::DeviceColor aColor)
    : mColor(Some(aColor)), mIsOpaque(aColor.a >= 1.0f) {}

NativeLayerRemoteMac::~NativeLayerRemoteMac() {
  if (mCommandQueue) {
    mCommandQueue->AppendCommand(mozilla::layers::CommandLayerDestroyed(
        reinterpret_cast<uint64_t>(this)));
  }
}

void NativeLayerRemoteMac::AttachExternalImage(
    wr::RenderTextureHost* aExternalImage) {
  MOZ_ASSERT(XRE_IsGPUProcess());
  MOZ_ASSERT(!mSurfaceHandler);

  wr::RenderMacIOSurfaceTextureHost* texture =
      aExternalImage->AsRenderMacIOSurfaceTextureHost();
  MOZ_ASSERT(texture);

  auto externalImage = texture->GetSurface()->GetIOSurfaceRef();
  mExternalImage = externalImage;

  auto texSize = texture->GetSize(0);
  mSize = texSize;

  auto displayRect = IntRect(IntPoint{}, mSize);
  bool changedDisplayRect = !mDisplayRect.IsEqualInterior(displayRect);
  mDisplayRect = displayRect;

  bool isDRM = aExternalImage->IsFromDRMSource();
  bool changedIsDRM = mIsDRM != isDRM;
  mIsDRM = isDRM;

  bool isHDR = false;
  MacIOSurface* macIOSurface = texture->GetSurface();
  if (macIOSurface->GetYUVColorSpace() == gfx::YUVColorSpace::BT2020) {
    // BT2020 colorSpace is a signifier of HDR.
    isHDR = true;
  }

  if (macIOSurface->GetColorDepth() == gfx::ColorDepth::COLOR_10) {
    // 10-bit color is a signifier of HDR.
    isHDR = true;
  }
  mIsHDR = isHDR;

  mDirtyLayerInfo |= changedDisplayRect;
  mSnapshotLayer.mMutatedFrontSurface = true;
  mSnapshotLayer.mMutatedSize |= changedDisplayRect;
  mSnapshotLayer.mMutatedDisplayRect |= changedDisplayRect;
  mSnapshotLayer.mMutatedIsDRM |= changedIsDRM;
  mDirtyChangedSurface = true;
}

GpuFence* NativeLayerRemoteMac::GetGpuFence() { return nullptr; }

IntSize NativeLayerRemoteMac::GetSize() {
  if (mSurfaceHandler) {
    return mSurfaceHandler->Size();
  }
  return mSize;
}

void NativeLayerRemoteMac::SetPosition(const IntPoint& aPosition) {
  if (mPosition != aPosition) {
    mDirtyLayerInfo = true;
    mSnapshotLayer.mMutatedPosition = true;
    mPosition = aPosition;
  }
}

IntPoint NativeLayerRemoteMac::GetPosition() { return mPosition; }

void NativeLayerRemoteMac::SetTransform(const Matrix4x4& aTransform) {
  MOZ_ASSERT(aTransform.IsRectilinear());

  if (mTransform != aTransform) {
    mDirtyLayerInfo = true;
    mSnapshotLayer.mMutatedTransform = true;
    mTransform = aTransform;
  }
}

void NativeLayerRemoteMac::SetSamplingFilter(
    gfx::SamplingFilter aSamplingFilter) {
  if (mSamplingFilter != aSamplingFilter) {
    mDirtyLayerInfo = true;
    mSnapshotLayer.mMutatedSamplingFilter = true;
    mSamplingFilter = aSamplingFilter;
  }
}

gfx::SamplingFilter NativeLayerRemoteMac::SamplingFilter() {
  return mSamplingFilter;
}

Matrix4x4 NativeLayerRemoteMac::GetTransform() { return mTransform; }

IntRect NativeLayerRemoteMac::GetRect() {
  IntSize size = mSize;
  if (mSurfaceHandler) {
    size = mSurfaceHandler->Size();
  }
  return IntRect(mPosition, size);
}

bool NativeLayerRemoteMac::IsOpaque() { return mIsOpaque; }

void NativeLayerRemoteMac::SetClipRect(const Maybe<gfx::IntRect>& aClipRect) {
  if (mClipRect != aClipRect) {
    mDirtyLayerInfo = true;
    mClipRect = aClipRect;
  }
}

Maybe<gfx::IntRect> NativeLayerRemoteMac::ClipRect() { return mClipRect; }

void NativeLayerRemoteMac::SetRoundedClipRect(
    const Maybe<gfx::RoundedRect>& aRoundedClipRect) {
  if (mRoundedClipRect != aRoundedClipRect) {
    mDirtyLayerInfo = true;
    mSnapshotLayer.mMutatedRoundedClipRect = true;
    mRoundedClipRect = aRoundedClipRect;
  }
}

Maybe<gfx::RoundedRect> NativeLayerRemoteMac::RoundedClipRect() {
  return mRoundedClipRect;
}

gfx::IntRect NativeLayerRemoteMac::CurrentSurfaceDisplayRect() {
  if (mSurfaceHandler) {
    return mSurfaceHandler->DisplayRect();
  }
  return mDisplayRect;
}

void NativeLayerRemoteMac::SetSurfaceIsFlipped(bool aIsFlipped) {
  if (SurfaceIsFlipped() != aIsFlipped) {
    mDirtyLayerInfo = true;
    mSnapshotLayer.mMutatedSurfaceIsFlipped = true;
    if (mSurfaceHandler) {
      mSurfaceHandler->SetSurfaceIsFlipped(aIsFlipped);
    } else {
      mSurfaceIsFlipped = aIsFlipped;
    }
  }
}

bool NativeLayerRemoteMac::SurfaceIsFlipped() {
  if (mSurfaceHandler) {
    return mSurfaceHandler->SurfaceIsFlipped();
  }
  return mSurfaceIsFlipped;
}

RefPtr<gfx::DrawTarget> NativeLayerRemoteMac::NextSurfaceAsDrawTarget(
    const IntRect& aDisplayRect, const IntRegion& aUpdateRegion,
    gfx::BackendType aBackendType) {
  MOZ_ASSERT(mSurfaceHandler);
  return mSurfaceHandler->NextSurfaceAsDrawTarget(aDisplayRect, aUpdateRegion,
                                                  aBackendType);
}

Maybe<GLuint> NativeLayerRemoteMac::NextSurfaceAsFramebuffer(
    const IntRect& aDisplayRect, const IntRegion& aUpdateRegion,
    bool aNeedsDepth) {
  MOZ_ASSERT(mSurfaceHandler);
  return mSurfaceHandler->NextSurfaceAsFramebuffer(aDisplayRect, aUpdateRegion,
                                                   aNeedsDepth);
}

Maybe<SurfaceWithInvalidRegion> NativeLayerRemoteMac::FrontSurface() {
  if (mSurfaceHandler) {
    return mSurfaceHandler->FrontSurface();
  }
  if (mExternalImage) {
    return Some(SurfaceWithInvalidRegion{mExternalImage, GetRect()});
  }
  return Nothing();
}

void NativeLayerRemoteMac::NotifySurfaceReady() {
  MOZ_ASSERT(mSurfaceHandler);

  // When we call the surface handler NotifySurfaceReady(), we get a
  // return value indicating if the display rect has changed. Set our
  // dirty flag (covering all non-surface changes) if that is true.
  bool changedDisplayRect = mSurfaceHandler->NotifySurfaceReady();
  mDirtyLayerInfo |= changedDisplayRect;
  mDirtyChangedSurface = true;
  mSnapshotLayer.mMutatedFrontSurface = true;
}

void NativeLayerRemoteMac::DiscardBackbuffers() {
  MOZ_ASSERT(mSurfaceHandler);
  mSurfaceHandler->DiscardBackbuffers();
}

void NativeLayerRemoteMac::FlushDirtyLayerInfoToCommandQueue() {
  auto ID = reinterpret_cast<uint64_t>(this);

  if (mDirtyChangedSurface) {
    IOSurfacePort surfacePort;
    auto surfaceWithInvalidRegion = FrontSurface();
    if (surfaceWithInvalidRegion) {
      surfacePort =
          IOSurfacePort::FromSurface(surfaceWithInvalidRegion->mSurface);
    }

    mCommandQueue->AppendCommand(mozilla::layers::CommandChangedSurface(
        ID, std::move(surfacePort), IsDRM(), IsHDR(), GetSize()));
    mDirtyChangedSurface = false;
  }

  if (mDirtyLayerInfo) {
    mCommandQueue->AppendCommand(mozilla::layers::CommandLayerInfo(
        ID, GetPosition(), CurrentSurfaceDisplayRect(), ClipRect(),
        RoundedClipRect(), GetTransform(),
        static_cast<int8_t>(SamplingFilter()), SurfaceIsFlipped()));
    mDirtyLayerInfo = false;
  }
}

void NativeLayerRemoteMac::UpdateSnapshotLayer() {
  CFTypeRefPtr<IOSurfaceRef> surface;
  if (auto frontSurface = FrontSurface()) {
    surface = frontSurface->mSurface;
  }

  IntRect rect = GetRect();
  IntRect displayRect = CurrentSurfaceDisplayRect();

  bool specializeVideo = false;
  bool isVideo = false;
  mSnapshotLayer.ApplyChanges(
      NativeLayerCAUpdateType::All, rect.Size(), mIsOpaque, rect.TopLeft(),
      mTransform, displayRect, mClipRect, mRoundedClipRect, mBackingScale,
      mSurfaceIsFlipped, mSamplingFilter, specializeVideo, surface, mColor,
      mIsDRM, isVideo);
}

CALayer* NativeLayerRemoteMac::CALayerForSnapshot() {
  return mSnapshotLayer.UnderlyingCALayer();
}

}  // namespace layers
}  // namespace mozilla