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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "WebRenderUserData.h"
#include "BasicLayers.h"
#include "mozilla/layers/AnimationHelper.h"
#include "mozilla/layers/CompositorBridgeChild.h"
#include "mozilla/layers/ImageClient.h"
#include "mozilla/layers/WebRenderBridgeChild.h"
#include "mozilla/layers/RenderRootStateManager.h"
#include "mozilla/layers/WebRenderMessages.h"
#include "mozilla/layers/IpcResourceUpdateQueue.h"
#include "mozilla/layers/SharedSurfacesChild.h"
#include "mozilla/webgpu/WebGPUChild.h"
#include "nsDisplayListInvalidation.h"
#include "nsIFrame.h"
#include "WebRenderCanvasRenderer.h"
namespace mozilla {
namespace layers {
void WebRenderBackgroundData::AddWebRenderCommands(
wr::DisplayListBuilder& aBuilder) {
aBuilder.PushRect(mBounds, mBounds, true, mColor);
}
/* static */
bool WebRenderUserData::SupportsAsyncUpdate(nsIFrame* aFrame) {
if (!aFrame) {
return false;
}
RefPtr<WebRenderImageData> data = GetWebRenderUserData<WebRenderImageData>(
aFrame, static_cast<uint32_t>(DisplayItemType::TYPE_VIDEO));
if (data) {
return data->IsAsync();
}
return false;
}
/* static */
bool WebRenderUserData::ProcessInvalidateForImage(
nsIFrame* aFrame, DisplayItemType aType, ContainerProducerID aProducerId) {
MOZ_ASSERT(aFrame);
if (!aFrame->HasProperty(WebRenderUserDataProperty::Key())) {
return false;
}
auto type = static_cast<uint32_t>(aType);
RefPtr<WebRenderFallbackData> fallback =
GetWebRenderUserData<WebRenderFallbackData>(aFrame, type);
if (fallback) {
fallback->SetInvalid(true);
aFrame->SchedulePaint();
return true;
}
RefPtr<WebRenderImageData> image =
GetWebRenderUserData<WebRenderImageData>(aFrame, type);
if (image && image->UsingSharedSurface(aProducerId)) {
return true;
}
aFrame->SchedulePaint();
return false;
}
WebRenderUserData::WebRenderUserData(RenderRootStateManager* aManager,
uint32_t aDisplayItemKey, nsIFrame* aFrame)
: mManager(aManager),
mFrame(aFrame),
mDisplayItemKey(aDisplayItemKey),
mTable(aManager->GetWebRenderUserDataTable()),
mUsed(false) {}
WebRenderUserData::WebRenderUserData(RenderRootStateManager* aManager,
nsDisplayItem* aItem)
: mManager(aManager),
mFrame(aItem->Frame()),
mDisplayItemKey(aItem->GetPerFrameKey()),
mTable(aManager->GetWebRenderUserDataTable()),
mUsed(false) {}
WebRenderUserData::~WebRenderUserData() = default;
void WebRenderUserData::RemoveFromTable() { mTable->RemoveEntry(this); }
WebRenderBridgeChild* WebRenderUserData::WrBridge() const {
return mManager->WrBridge();
}
WebRenderImageData::WebRenderImageData(RenderRootStateManager* aManager,
nsDisplayItem* aItem)
: WebRenderUserData(aManager, aItem), mOwnsKey(false) {}
WebRenderImageData::WebRenderImageData(RenderRootStateManager* aManager,
uint32_t aDisplayItemKey,
nsIFrame* aFrame)
: WebRenderUserData(aManager, aDisplayItemKey, aFrame), mOwnsKey(false) {}
WebRenderImageData::~WebRenderImageData() {
ClearImageKey();
if (mPipelineId) {
mManager->RemovePipelineIdForCompositable(mPipelineId.ref());
}
}
bool WebRenderImageData::UsingSharedSurface(
ContainerProducerID aProducerId) const {
if (!mContainer || !mKey || mOwnsKey) {
return false;
}
// If this is just an update with the same image key, then we know that the
// share request initiated an asynchronous update so that we don't need to
// rebuild the scene.
wr::ImageKey key;
nsresult rv = SharedSurfacesChild::Share(
mContainer, mManager, mManager->AsyncResourceUpdates(), key, aProducerId);
return NS_SUCCEEDED(rv) && mKey.ref() == key;
}
void WebRenderImageData::ClearImageKey() {
if (mKey) {
// If we don't own the key, then the owner is responsible for discarding the
// key when appropriate.
if (mOwnsKey) {
mManager->AddImageKeyForDiscard(mKey.value());
if (mTextureOfImage) {
WrBridge()->ReleaseTextureOfImage(mKey.value());
mTextureOfImage = nullptr;
}
}
mKey.reset();
}
mOwnsKey = false;
MOZ_ASSERT(!mTextureOfImage);
}
Maybe<wr::ImageKey> WebRenderImageData::UpdateImageKey(
ImageContainer* aContainer, wr::IpcResourceUpdateQueue& aResources,
bool aFallback) {
MOZ_ASSERT(aContainer);
if (mContainer != aContainer) {
mContainer = aContainer;
}
wr::WrImageKey key;
if (!aFallback) {
nsresult rv = SharedSurfacesChild::Share(aContainer, mManager, aResources,
key, kContainerProducerID_Invalid);
if (NS_SUCCEEDED(rv)) {
// Ensure that any previously owned keys are released before replacing. We
// don't own this key, the surface itself owns it, so that it can be
// shared across multiple elements.
ClearImageKey();
mKey = Some(key);
return mKey;
}
if (rv != NS_ERROR_NOT_IMPLEMENTED) {
// We should be using the shared surface but somehow sharing it failed.
ClearImageKey();
return Nothing();
}
}
CreateImageClientIfNeeded();
if (!mImageClient) {
return Nothing();
}
MOZ_ASSERT(mImageClient->AsImageClientSingle());
ImageClientSingle* imageClient = mImageClient->AsImageClientSingle();
uint32_t oldCounter = imageClient->GetLastUpdateGenerationCounter();
bool ret = imageClient->UpdateImage(aContainer, /* unused */ 0);
RefPtr<TextureClient> currentTexture = imageClient->GetForwardedTexture();
if (!ret || !currentTexture) {
// Delete old key
ClearImageKey();
return Nothing();
}
// Reuse old key if generation is not updated.
if (!aFallback &&
oldCounter == imageClient->GetLastUpdateGenerationCounter() && mKey) {
return mKey;
}
// If we already had a texture and the format hasn't changed, better to reuse
// the image keys than create new ones.
bool useUpdate = mKey.isSome() && !!mTextureOfImage && !!currentTexture &&
mTextureOfImage->GetSize() == currentTexture->GetSize() &&
mTextureOfImage->GetFormat() == currentTexture->GetFormat();
wr::MaybeExternalImageId extId = currentTexture->GetExternalImageKey();
MOZ_RELEASE_ASSERT(extId.isSome());
if (useUpdate) {
MOZ_ASSERT(mKey.isSome());
MOZ_ASSERT(mTextureOfImage);
aResources.PushExternalImageForTexture(
extId.ref(), mKey.ref(), currentTexture, /* aIsUpdate */ true);
} else {
ClearImageKey();
key = WrBridge()->GetNextImageKey();
aResources.PushExternalImageForTexture(extId.ref(), key, currentTexture,
/* aIsUpdate */ false);
mKey = Some(key);
}
mTextureOfImage = currentTexture;
mOwnsKey = true;
return mKey;
}
already_AddRefed<ImageClient> WebRenderImageData::GetImageClient() {
RefPtr<ImageClient> imageClient = mImageClient;
return imageClient.forget();
}
void WebRenderImageData::CreateAsyncImageWebRenderCommands(
mozilla::wr::DisplayListBuilder& aBuilder, ImageContainer* aContainer,
const StackingContextHelper& aSc, const LayoutDeviceRect& aBounds,
const LayoutDeviceRect& aSCBounds, VideoInfo::Rotation aRotation,
const wr::ImageRendering& aFilter, const wr::MixBlendMode& aMixBlendMode,
bool aIsBackfaceVisible) {
MOZ_ASSERT(aContainer->IsAsync());
if (mPipelineId.isSome() && mContainer != aContainer) {
// In this case, we need to remove the existed pipeline and create new one
// because the ImageContainer is changed.
WrBridge()->RemovePipelineIdForCompositable(mPipelineId.ref());
mPipelineId.reset();
}
if (!mPipelineId) {
// Alloc async image pipeline id.
mPipelineId =
Some(WrBridge()->GetCompositorBridgeChild()->GetNextPipelineId());
WrBridge()->AddPipelineIdForAsyncCompositable(
mPipelineId.ref(), aContainer->GetAsyncContainerHandle());
mContainer = aContainer;
}
MOZ_ASSERT(!mImageClient);
// Push IFrame for async image pipeline.
//
// We don't push a stacking context for this async image pipeline here.
// Instead, we do it inside the iframe that hosts the image. As a result,
// a bunch of the calculations normally done as part of that stacking
// context need to be done manually and pushed over to the parent side,
// where it will be done when we build the display list for the iframe.
// That happens in AsyncImagePipelineManager.
wr::LayoutRect r = wr::ToLayoutRect(aBounds);
aBuilder.PushIFrame(r, aIsBackfaceVisible, mPipelineId.ref(),
/*ignoreMissingPipelines*/ false);
WrBridge()->AddWebRenderParentCommand(OpUpdateAsyncImagePipeline(
mPipelineId.value(), aSCBounds, aRotation, aFilter, aMixBlendMode));
}
void WebRenderImageData::CreateImageClientIfNeeded() {
if (!mImageClient) {
mImageClient = ImageClient::CreateImageClient(
CompositableType::IMAGE, WrBridge(), TextureFlags::DEFAULT);
if (!mImageClient) {
return;
}
mImageClient->Connect();
}
}
WebRenderFallbackData::WebRenderFallbackData(RenderRootStateManager* aManager,
nsDisplayItem* aItem)
: WebRenderUserData(aManager, aItem), mInvalid(false) {}
WebRenderFallbackData::~WebRenderFallbackData() { ClearImageKey(); }
void WebRenderFallbackData::SetBlobImageKey(const wr::BlobImageKey& aKey) {
ClearImageKey();
mBlobKey = Some(aKey);
}
Maybe<wr::ImageKey> WebRenderFallbackData::GetImageKey() {
if (mBlobKey) {
return Some(wr::AsImageKey(mBlobKey.value()));
}
if (mImageData) {
return mImageData->GetImageKey();
}
return Nothing();
}
void WebRenderFallbackData::ClearImageKey() {
if (mImageData) {
mImageData->ClearImageKey();
mImageData = nullptr;
}
if (mBlobKey) {
mManager->AddBlobImageKeyForDiscard(mBlobKey.value());
mBlobKey.reset();
}
}
WebRenderImageData* WebRenderFallbackData::PaintIntoImage() {
if (mBlobKey) {
mManager->AddBlobImageKeyForDiscard(mBlobKey.value());
mBlobKey.reset();
}
if (mImageData) {
return mImageData.get();
}
mImageData = MakeAndAddRef<WebRenderImageData>(mManager.get(),
mDisplayItemKey, mFrame);
return mImageData.get();
}
WebRenderAPZAnimationData::WebRenderAPZAnimationData(
RenderRootStateManager* aManager, nsDisplayItem* aItem)
: WebRenderUserData(aManager, aItem),
mAnimationId(AnimationHelper::GetNextCompositorAnimationsId()) {}
WebRenderAnimationData::WebRenderAnimationData(RenderRootStateManager* aManager,
nsDisplayItem* aItem)
: WebRenderUserData(aManager, aItem) {}
WebRenderAnimationData::~WebRenderAnimationData() {
// It may be the case that nsDisplayItem that created this WebRenderUserData
// gets destroyed without getting a chance to discard the compositor animation
// id, so we should do it as part of cleanup here.
uint64_t animationId = mAnimationInfo.GetCompositorAnimationsId();
// animationId might be 0 if mAnimationInfo never held any active animations.
if (animationId) {
mManager->AddCompositorAnimationsIdForDiscard(animationId);
}
}
WebRenderCanvasData::WebRenderCanvasData(RenderRootStateManager* aManager,
nsDisplayItem* aItem)
: WebRenderUserData(aManager, aItem) {}
WebRenderCanvasData::~WebRenderCanvasData() {
if (mCanvasRenderer) {
mCanvasRenderer->ClearCachedResources();
}
}
void WebRenderCanvasData::ClearCanvasRenderer() { mCanvasRenderer = nullptr; }
WebRenderCanvasRendererAsync* WebRenderCanvasData::GetCanvasRenderer() {
return mCanvasRenderer.get();
}
WebRenderCanvasRendererAsync* WebRenderCanvasData::CreateCanvasRenderer() {
mCanvasRenderer = new WebRenderCanvasRendererAsync(mManager);
return mCanvasRenderer.get();
}
void WebRenderCanvasData::SetImageContainer(ImageContainer* aImageContainer) {
mContainer = aImageContainer;
}
ImageContainer* WebRenderCanvasData::GetImageContainer() {
if (!mContainer) {
mContainer = LayerManager::CreateImageContainer();
}
return mContainer;
}
void WebRenderCanvasData::ClearImageContainer() { mContainer = nullptr; }
WebRenderLocalCanvasData::WebRenderLocalCanvasData(
RenderRootStateManager* aManager, nsDisplayItem* aItem)
: WebRenderUserData(aManager, aItem) {}
WebRenderLocalCanvasData::~WebRenderLocalCanvasData() = default;
void WebRenderLocalCanvasData::RequestFrameReadback() {
if (mGpuBridge) {
mGpuBridge->SwapChainPresent(mExternalImageId, mGpuTextureId);
}
}
void WebRenderLocalCanvasData::RefreshExternalImage() {
if (!mDirty) {
return;
}
const ImageIntRect dirtyRect(0, 0, mDescriptor.width, mDescriptor.height);
// Update the WR external image, forcing the composition of a new frame.
mManager->AsyncResourceUpdates().UpdatePrivateExternalImage(
mExternalImageId, mImageKey, mDescriptor, dirtyRect);
mDirty = false;
}
WebRenderRemoteData::WebRenderRemoteData(RenderRootStateManager* aManager,
nsDisplayItem* aItem)
: WebRenderUserData(aManager, aItem) {}
WebRenderRemoteData::~WebRenderRemoteData() {
if (mRemoteBrowser) {
mRemoteBrowser->UpdateEffects(mozilla::dom::EffectsInfo::FullyHidden());
}
}
void DestroyWebRenderUserDataTable(WebRenderUserDataTable* aTable) {
for (auto iter = aTable->Iter(); !iter.Done(); iter.Next()) {
iter.UserData()->RemoveFromTable();
}
delete aTable;
}
} // namespace layers
} // namespace mozilla
|