summaryrefslogtreecommitdiffstats
path: root/gfx/layers/ipc/SharedSurfacesChild.cpp
blob: 55ece9abdf9a13f995fba4e99422cf23cb523f21 (plain)
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/* -*- 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 "SharedSurfacesChild.h"
#include "SharedSurfacesParent.h"
#include "CompositorManagerChild.h"
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/layers/IpcResourceUpdateQueue.h"
#include "mozilla/layers/SourceSurfaceSharedData.h"
#include "mozilla/layers/WebRenderBridgeChild.h"
#include "mozilla/layers/RenderRootStateManager.h"
#include "mozilla/SchedulerGroup.h"
#include "mozilla/StaticPrefs_image.h"

namespace mozilla {
namespace layers {

using namespace mozilla::gfx;

/* static */
UserDataKey SharedSurfacesChild::sSharedKey;

SharedSurfacesChild::ImageKeyData::ImageKeyData(
    RenderRootStateManager* aManager, const wr::ImageKey& aImageKey)
    : mManager(aManager), mImageKey(aImageKey) {}

SharedSurfacesChild::ImageKeyData::ImageKeyData(
    SharedSurfacesChild::ImageKeyData&& aOther)
    : mManager(std::move(aOther.mManager)),
      mDirtyRect(std::move(aOther.mDirtyRect)),
      mImageKey(aOther.mImageKey) {}

SharedSurfacesChild::ImageKeyData& SharedSurfacesChild::ImageKeyData::operator=(
    SharedSurfacesChild::ImageKeyData&& aOther) {
  mManager = std::move(aOther.mManager);
  mDirtyRect = std::move(aOther.mDirtyRect);
  mImageKey = aOther.mImageKey;
  return *this;
}

SharedSurfacesChild::ImageKeyData::~ImageKeyData() = default;

void SharedSurfacesChild::ImageKeyData::MergeDirtyRect(
    const Maybe<IntRect>& aDirtyRect) {
  if (mDirtyRect) {
    if (aDirtyRect) {
      mDirtyRect->UnionRect(mDirtyRect.ref(), aDirtyRect.ref());
    }
  } else {
    mDirtyRect = aDirtyRect;
  }
}

SharedSurfacesChild::SharedUserData::SharedUserData(
    const wr::ExternalImageId& aId)
    : Runnable("SharedSurfacesChild::SharedUserData"),
      mId(aId),
      mShared(false) {}

SharedSurfacesChild::SharedUserData::~SharedUserData() {
  // We may fail to dispatch during shutdown, and since it would be issued on
  // the main thread, it releases the runnable instead of leaking it.
  if (mShared || !mKeys.IsEmpty()) {
    if (NS_IsMainThread()) {
      SharedSurfacesChild::Unshare(mId, mShared, mKeys);
    } else {
      MOZ_ASSERT_UNREACHABLE("Shared resources not released!");
    }
  }
}

/* static */
void SharedSurfacesChild::SharedUserData::Destroy(void* aClosure) {
  MOZ_ASSERT(aClosure);
  RefPtr<SharedUserData> data =
      dont_AddRef(static_cast<SharedUserData*>(aClosure));
  if (data->mShared || !data->mKeys.IsEmpty()) {
    SchedulerGroup::Dispatch(TaskCategory::Other, data.forget());
  }
}

NS_IMETHODIMP SharedSurfacesChild::SharedUserData::Run() {
  SharedSurfacesChild::Unshare(mId, mShared, mKeys);
  mShared = false;
  mKeys.Clear();
  return NS_OK;
}

wr::ImageKey SharedSurfacesChild::SharedUserData::UpdateKey(
    RenderRootStateManager* aManager, wr::IpcResourceUpdateQueue& aResources,
    const Maybe<IntRect>& aDirtyRect) {
  MOZ_ASSERT(aManager);
  MOZ_ASSERT(!aManager->IsDestroyed());

  // We iterate through all of the items to ensure we clean up the old
  // RenderRootStateManager references. Most of the time there will be few
  // entries and this should not be particularly expensive compared to the
  // cost of duplicating image keys. In an ideal world, we would generate a
  // single key for the surface, and it would be usable on all of the
  // renderer instances. For now, we must allocate a key for each WR bridge.
  wr::ImageKey key;
  bool found = false;
  auto i = mKeys.Length();
  while (i > 0) {
    --i;
    ImageKeyData& entry = mKeys[i];
    if (entry.mManager->IsDestroyed()) {
      mKeys.RemoveElementAt(i);
    } else if (entry.mManager == aManager) {
      WebRenderBridgeChild* wrBridge = aManager->WrBridge();
      MOZ_ASSERT(wrBridge);

      // Even if the manager is the same, its underlying WebRenderBridgeChild
      // can change state. If our namespace differs, then our old key has
      // already been discarded.
      bool ownsKey = wrBridge->GetNamespace() == entry.mImageKey.mNamespace;
      if (!ownsKey) {
        entry.mImageKey = wrBridge->GetNextImageKey();
        entry.TakeDirtyRect();
        aResources.AddSharedExternalImage(mId, entry.mImageKey);
      } else {
        entry.MergeDirtyRect(aDirtyRect);
        Maybe<IntRect> dirtyRect = entry.TakeDirtyRect();
        if (dirtyRect) {
          MOZ_ASSERT(mShared);
          aResources.UpdateSharedExternalImage(
              mId, entry.mImageKey, ViewAs<ImagePixel>(dirtyRect.ref()));
        }
      }

      key = entry.mImageKey;
      found = true;
    } else {
      // We don't have the resource update queue for this manager, so just
      // accumulate the dirty rects until it is requested.
      entry.MergeDirtyRect(aDirtyRect);
    }
  }

  if (!found) {
    key = aManager->WrBridge()->GetNextImageKey();
    ImageKeyData data(aManager, key);
    mKeys.AppendElement(std::move(data));
    aResources.AddSharedExternalImage(mId, key);
  }

  return key;
}

/* static */
SourceSurfaceSharedData* SharedSurfacesChild::AsSourceSurfaceSharedData(
    SourceSurface* aSurface) {
  MOZ_ASSERT(aSurface);
  switch (aSurface->GetType()) {
    case SurfaceType::DATA_SHARED:
    case SurfaceType::DATA_RECYCLING_SHARED:
      return static_cast<SourceSurfaceSharedData*>(aSurface);
    default:
      return nullptr;
  }
}

/* static */
nsresult SharedSurfacesChild::ShareInternal(SourceSurfaceSharedData* aSurface,
                                            SharedUserData** aUserData) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aSurface);
  MOZ_ASSERT(aUserData);

  CompositorManagerChild* manager = CompositorManagerChild::GetInstance();
  if (NS_WARN_IF(!manager || !manager->CanSend() || !gfxVars::UseWebRender())) {
    // We cannot try to share the surface, most likely because the GPU process
    // crashed. Ideally, we would retry when it is ready, but the handles may be
    // a scarce resource, which can cause much more serious problems if we run
    // out. Better to copy into a fresh buffer later.
    aSurface->FinishedSharing();
    return NS_ERROR_NOT_INITIALIZED;
  }

  SharedUserData* data =
      static_cast<SharedUserData*>(aSurface->GetUserData(&sSharedKey));
  if (!data) {
    data =
        MakeAndAddRef<SharedUserData>(manager->GetNextExternalImageId()).take();
    aSurface->AddUserData(&sSharedKey, data, SharedUserData::Destroy);
  } else if (!manager->OwnsExternalImageId(data->Id())) {
    // If the id isn't owned by us, that means the bridge was reinitialized, due
    // to the GPU process crashing. All previous mappings have been released.
    data->SetId(manager->GetNextExternalImageId());
  } else if (data->IsShared()) {
    // It has already been shared with the GPU process.
    *aUserData = data;
    return NS_OK;
  }

  // Ensure that the handle doesn't get released until after we have finished
  // sending the buffer to the GPU process and/or reallocating it.
  // FinishedSharing is not a sufficient condition because another thread may
  // decide we are done while we are in the processing of sharing our newly
  // reallocated handle. Once it goes out of scope, it may release the handle.
  SourceSurfaceSharedData::HandleLock lock(aSurface);

  // If we live in the same process, then it is a simple matter of directly
  // asking the parent instance to store a pointer to the same data, no need
  // to map the data into our memory space twice.
  auto pid = manager->OtherPid();
  if (pid == base::GetCurrentProcId()) {
    SharedSurfacesParent::AddSameProcess(data->Id(), aSurface);
    data->MarkShared();
    *aUserData = data;
    return NS_OK;
  }

  // Attempt to share a handle with the GPU process. The handle may or may not
  // be available -- it will only be available if it is either not yet finalized
  // and/or if it has been finalized but never used for drawing in process.
  ipc::SharedMemoryBasic::Handle handle = ipc::SharedMemoryBasic::NULLHandle();
  nsresult rv = aSurface->ShareToProcess(pid, handle);
  if (rv == NS_ERROR_NOT_AVAILABLE) {
    // It is at least as expensive to copy the image to the GPU process if we
    // have already closed the handle necessary to share, but if we reallocate
    // the shared buffer to get a new handle, we can save some memory.
    if (NS_WARN_IF(!aSurface->ReallocHandle())) {
      return NS_ERROR_OUT_OF_MEMORY;
    }

    // Reattempt the sharing of the handle to the GPU process.
    rv = aSurface->ShareToProcess(pid, handle);
  }

  if (NS_WARN_IF(NS_FAILED(rv))) {
    MOZ_ASSERT(rv != NS_ERROR_NOT_AVAILABLE);
    return rv;
  }

  SurfaceFormat format = aSurface->GetFormat();
  MOZ_RELEASE_ASSERT(
      format == SurfaceFormat::B8G8R8X8 || format == SurfaceFormat::B8G8R8A8,
      "bad format");

  data->MarkShared();
  manager->SendAddSharedSurface(
      data->Id(), SurfaceDescriptorShared(aSurface->GetSize(),
                                          aSurface->Stride(), format, handle));
  *aUserData = data;
  return NS_OK;
}

/* static */
void SharedSurfacesChild::Share(SourceSurfaceSharedData* aSurface) {
  MOZ_ASSERT(aSurface);

  // The IPDL actor to do sharing can only be accessed on the main thread so we
  // need to dispatch if off the main thread. However there is no real danger if
  // we end up racing because if it is already shared, this method will do
  // nothing.
  if (!NS_IsMainThread()) {
    class ShareRunnable final : public Runnable {
     public:
      explicit ShareRunnable(SourceSurfaceSharedData* aSurface)
          : Runnable("SharedSurfacesChild::Share"), mSurface(aSurface) {}

      NS_IMETHOD Run() override {
        SharedUserData* unused = nullptr;
        SharedSurfacesChild::ShareInternal(mSurface, &unused);
        return NS_OK;
      }

     private:
      RefPtr<SourceSurfaceSharedData> mSurface;
    };

    SchedulerGroup::Dispatch(TaskCategory::Other,
                             MakeAndAddRef<ShareRunnable>(aSurface));
    return;
  }

  SharedUserData* unused = nullptr;
  SharedSurfacesChild::ShareInternal(aSurface, &unused);
}

/* static */
nsresult SharedSurfacesChild::Share(SourceSurfaceSharedData* aSurface,
                                    RenderRootStateManager* aManager,
                                    wr::IpcResourceUpdateQueue& aResources,
                                    wr::ImageKey& aKey) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aSurface);
  MOZ_ASSERT(aManager);

  // Each time the surface changes, the producers of SourceSurfaceSharedData
  // surfaces promise to increment the invalidation counter each time the
  // surface has changed. We can use this counter to determine whether or not
  // we should update our paired ImageKey.
  Maybe<IntRect> dirtyRect = aSurface->TakeDirtyRect();
  SharedUserData* data = nullptr;
  nsresult rv = SharedSurfacesChild::ShareInternal(aSurface, &data);
  if (NS_SUCCEEDED(rv)) {
    MOZ_ASSERT(data);
    aKey = data->UpdateKey(aManager, aResources, dirtyRect);
  }

  return rv;
}

/* static */
nsresult SharedSurfacesChild::Share(SourceSurface* aSurface,
                                    RenderRootStateManager* aManager,
                                    wr::IpcResourceUpdateQueue& aResources,
                                    wr::ImageKey& aKey) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aSurface);
  MOZ_ASSERT(aManager);

  auto sharedSurface = AsSourceSurfaceSharedData(aSurface);
  if (!sharedSurface) {
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  return Share(sharedSurface, aManager, aResources, aKey);
}

/* static */
nsresult SharedSurfacesChild::Share(ImageContainer* aContainer,
                                    RenderRootStateManager* aManager,
                                    wr::IpcResourceUpdateQueue& aResources,
                                    wr::ImageKey& aKey,
                                    ContainerProducerID aProducerId) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aContainer);
  MOZ_ASSERT(aManager);

  if (aContainer->IsAsync()) {
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  AutoTArray<ImageContainer::OwningImage, 4> images;
  aContainer->GetCurrentImages(&images);
  if (images.IsEmpty()) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  if (aProducerId != kContainerProducerID_Invalid &&
      images[0].mProducerID != aProducerId) {
    // If the producer ID of the surface in the container does not match the
    // expected producer ID, then we do not want to proceed with sharing. This
    // is useful for when callers are unsure if given container is for the same
    // producer / underlying image request.
    return NS_ERROR_FAILURE;
  }

  RefPtr<gfx::SourceSurface> surface = images[0].mImage->GetAsSourceSurface();
  if (!surface) {
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  auto sharedSurface = AsSourceSurfaceSharedData(surface);
  if (!sharedSurface) {
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  SharedSurfacesAnimation* anim = aContainer->GetSharedSurfacesAnimation();
  if (anim) {
    return anim->UpdateKey(sharedSurface, aManager, aResources, aKey);
  }

  return Share(sharedSurface, aManager, aResources, aKey);
}

/* static */
nsresult SharedSurfacesChild::Share(SourceSurface* aSurface,
                                    wr::ExternalImageId& aId) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aSurface);

  auto sharedSurface = AsSourceSurfaceSharedData(aSurface);
  if (!sharedSurface) {
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  // The external image ID does not change with the invalidation counter. The
  // caller of this should be aware of the invalidations of the surface through
  // another mechanism (e.g. imgRequestProxy listener notifications).
  SharedUserData* data = nullptr;
  nsresult rv = ShareInternal(sharedSurface, &data);
  if (NS_SUCCEEDED(rv)) {
    MOZ_ASSERT(data);
    aId = data->Id();
  }

  return rv;
}

/* static */
void SharedSurfacesChild::Unshare(const wr::ExternalImageId& aId,
                                  bool aReleaseId,
                                  nsTArray<ImageKeyData>& aKeys) {
  MOZ_ASSERT(NS_IsMainThread());

  for (const auto& entry : aKeys) {
    if (!entry.mManager->IsDestroyed()) {
      entry.mManager->AddImageKeyForDiscard(entry.mImageKey);
    }
  }

  if (!aReleaseId) {
    // We don't own the external image ID itself.
    return;
  }

  CompositorManagerChild* manager = CompositorManagerChild::GetInstance();
  if (MOZ_UNLIKELY(!manager || !manager->CanSend())) {
    return;
  }

  if (manager->OwnsExternalImageId(aId)) {
    // Only attempt to release current mappings in the compositor process. It is
    // possible we had a surface that was previously shared, the compositor
    // process crashed / was restarted, and then we freed the surface. In that
    // case we know the mapping has already been freed.
    manager->SendRemoveSharedSurface(aId);
  }
}

/* static */ Maybe<wr::ExternalImageId> SharedSurfacesChild::GetExternalId(
    const SourceSurfaceSharedData* aSurface) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aSurface);

  SharedUserData* data =
      static_cast<SharedUserData*>(aSurface->GetUserData(&sSharedKey));
  if (!data || !data->IsShared()) {
    return Nothing();
  }

  return Some(data->Id());
}

/* static */
nsresult SharedSurfacesChild::UpdateAnimation(ImageContainer* aContainer,
                                              SourceSurface* aSurface,
                                              const IntRect& aDirtyRect) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aContainer);
  MOZ_ASSERT(!aContainer->IsAsync());
  MOZ_ASSERT(aSurface);

  // If we aren't using shared surfaces, then is nothing to do.
  auto sharedSurface = SharedSurfacesChild::AsSourceSurfaceSharedData(aSurface);
  if (!sharedSurface) {
    MOZ_ASSERT(!aContainer->GetSharedSurfacesAnimation());
    return NS_ERROR_NOT_IMPLEMENTED;
  }

  SharedSurfacesAnimation* anim = aContainer->EnsureSharedSurfacesAnimation();
  MOZ_ASSERT(anim);

  return anim->SetCurrentFrame(sharedSurface, aDirtyRect);
}

AnimationImageKeyData::AnimationImageKeyData(RenderRootStateManager* aManager,
                                             const wr::ImageKey& aImageKey)
    : SharedSurfacesChild::ImageKeyData(aManager, aImageKey) {}

AnimationImageKeyData::AnimationImageKeyData(AnimationImageKeyData&& aOther)
    : SharedSurfacesChild::ImageKeyData(std::move(aOther)),
      mPendingRelease(std::move(aOther.mPendingRelease)) {}

AnimationImageKeyData& AnimationImageKeyData::operator=(
    AnimationImageKeyData&& aOther) {
  mPendingRelease = std::move(aOther.mPendingRelease);
  SharedSurfacesChild::ImageKeyData::operator=(std::move(aOther));
  return *this;
}

AnimationImageKeyData::~AnimationImageKeyData() = default;

SharedSurfacesAnimation::~SharedSurfacesAnimation() {
  MOZ_ASSERT(mKeys.IsEmpty());
}

void SharedSurfacesAnimation::Destroy() {
  if (!NS_IsMainThread()) {
    nsCOMPtr<nsIRunnable> task =
        NewRunnableMethod("SharedSurfacesAnimation::Destroy", this,
                          &SharedSurfacesAnimation::Destroy);
    SchedulerGroup::Dispatch(TaskCategory::Other, task.forget());
    return;
  }

  if (mKeys.IsEmpty()) {
    return;
  }

  for (const auto& entry : mKeys) {
    MOZ_ASSERT(!entry.mManager->IsDestroyed());
    if (StaticPrefs::image_animated_decode_on_demand_recycle_AtStartup()) {
      entry.mManager->DeregisterAsyncAnimation(entry.mImageKey);
    }
    entry.mManager->AddImageKeyForDiscard(entry.mImageKey);
  }

  mKeys.Clear();
}

void SharedSurfacesAnimation::HoldSurfaceForRecycling(
    AnimationImageKeyData& aEntry, SourceSurfaceSharedData* aSurface) {
  if (aSurface->GetType() != SurfaceType::DATA_RECYCLING_SHARED) {
    return;
  }

  MOZ_ASSERT(StaticPrefs::image_animated_decode_on_demand_recycle_AtStartup());
  aEntry.mPendingRelease.AppendElement(aSurface);
}

nsresult SharedSurfacesAnimation::SetCurrentFrame(
    SourceSurfaceSharedData* aSurface, const gfx::IntRect& aDirtyRect) {
  MOZ_ASSERT(aSurface);

  SharedSurfacesChild::SharedUserData* data = nullptr;
  nsresult rv = SharedSurfacesChild::ShareInternal(aSurface, &data);
  if (NS_FAILED(rv)) {
    return rv;
  }

  MOZ_ASSERT(data);
  mId = data->Id();

  auto i = mKeys.Length();
  while (i > 0) {
    --i;
    AnimationImageKeyData& entry = mKeys[i];
    MOZ_ASSERT(!entry.mManager->IsDestroyed());

    entry.MergeDirtyRect(Some(aDirtyRect));
    Maybe<IntRect> dirtyRect = entry.TakeDirtyRect();
    if (dirtyRect) {
      HoldSurfaceForRecycling(entry, aSurface);
      auto& resourceUpdates = entry.mManager->AsyncResourceUpdates();
      resourceUpdates.UpdateSharedExternalImage(
          mId, entry.mImageKey, ViewAs<ImagePixel>(dirtyRect.ref()));
    }
  }

  return NS_OK;
}

nsresult SharedSurfacesAnimation::UpdateKey(
    SourceSurfaceSharedData* aSurface, RenderRootStateManager* aManager,
    wr::IpcResourceUpdateQueue& aResources, wr::ImageKey& aKey) {
  SharedSurfacesChild::SharedUserData* data = nullptr;
  nsresult rv = SharedSurfacesChild::ShareInternal(aSurface, &data);
  if (NS_FAILED(rv)) {
    return rv;
  }

  MOZ_ASSERT(data);
  if (wr::AsUint64(mId) != wr::AsUint64(data->Id())) {
    mKeys.Clear();
    mId = data->Id();
  }

  // We iterate through all of the items to ensure we clean up the old
  // RenderRootStateManager references. Most of the time there will be few
  // entries and this should not be particularly expensive compared to the
  // cost of duplicating image keys. In an ideal world, we would generate a
  // single key for the surface, and it would be usable on all of the
  // renderer instances. For now, we must allocate a key for each WR bridge.
  bool found = false;
  auto i = mKeys.Length();
  while (i > 0) {
    --i;
    AnimationImageKeyData& entry = mKeys[i];
    MOZ_ASSERT(!entry.mManager->IsDestroyed());
    if (entry.mManager == aManager) {
      WebRenderBridgeChild* wrBridge = aManager->WrBridge();
      MOZ_ASSERT(wrBridge);

      // Even if the manager is the same, its underlying WebRenderBridgeChild
      // can change state. If our namespace differs, then our old key has
      // already been discarded.
      bool ownsKey = wrBridge->GetNamespace() == entry.mImageKey.mNamespace;
      if (!ownsKey) {
        entry.mImageKey = wrBridge->GetNextImageKey();
        HoldSurfaceForRecycling(entry, aSurface);
        aResources.AddSharedExternalImage(mId, entry.mImageKey);
      } else {
        MOZ_ASSERT(entry.mDirtyRect.isNothing());
      }

      aKey = entry.mImageKey;
      found = true;
      break;
    }
  }

  if (!found) {
    aKey = aManager->WrBridge()->GetNextImageKey();
    if (StaticPrefs::image_animated_decode_on_demand_recycle_AtStartup()) {
      aManager->RegisterAsyncAnimation(aKey, this);
    }

    AnimationImageKeyData data(aManager, aKey);
    HoldSurfaceForRecycling(data, aSurface);
    mKeys.AppendElement(std::move(data));
    aResources.AddSharedExternalImage(mId, aKey);
  }

  return NS_OK;
}

void SharedSurfacesAnimation::ReleasePreviousFrame(
    RenderRootStateManager* aManager, const wr::ExternalImageId& aId) {
  MOZ_ASSERT(aManager);

  auto i = mKeys.Length();
  while (i > 0) {
    --i;
    AnimationImageKeyData& entry = mKeys[i];
    MOZ_ASSERT(!entry.mManager->IsDestroyed());
    if (entry.mManager == aManager) {
      size_t k;
      for (k = 0; k < entry.mPendingRelease.Length(); ++k) {
        Maybe<wr::ExternalImageId> extId =
            SharedSurfacesChild::GetExternalId(entry.mPendingRelease[k]);
        if (extId && extId.ref() == aId) {
          break;
        }
      }

      if (k == entry.mPendingRelease.Length()) {
        continue;
      }

      entry.mPendingRelease.RemoveElementsAt(0, k + 1);
      break;
    }
  }
}

void SharedSurfacesAnimation::Invalidate(RenderRootStateManager* aManager) {
  auto i = mKeys.Length();
  while (i > 0) {
    --i;
    AnimationImageKeyData& entry = mKeys[i];
    if (entry.mManager == aManager) {
      mKeys.RemoveElementAt(i);
      break;
    }
  }
}

}  // namespace layers
}  // namespace mozilla