Bug 1465619 - Part 9. Use redesigned AnimationFrameBuffer interface and retaining buffer. r=tnikkel
authorAndrew Osmond <aosmond@mozilla.com>
Mon, 04 Jun 2018 08:23:00 -0400
changeset 498815 bd9168e1e48ce09404664e250fdc151d301b98db
parent 498814 ef6369b9a1f929ea65bfbb1adb63ceb42a6bba3f
child 498816 453d21db2a306f4800e8be4d77f6d61000cb9a0c
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstnikkel
bugs1465619
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1465619 - Part 9. Use redesigned AnimationFrameBuffer interface and retaining buffer. r=tnikkel This patch makes AnimationSurfaceProvider use the new abstractions provided by AnimationFrameBuffer and AnimationFrameRetainedBuffer to provide storage and lifetime management of decoders and the produced frames. We initially start out with an implementation that will just keep every frame forever, like our historical behaviour. The next patch will add support for discarding. Differential Revision: https://phabricator.services.mozilla.com/D7514
image/AnimationFrameBuffer.cpp
image/AnimationFrameBuffer.h
image/AnimationSurfaceProvider.cpp
image/AnimationSurfaceProvider.h
--- a/image/AnimationFrameBuffer.cpp
+++ b/image/AnimationFrameBuffer.cpp
@@ -4,10 +4,155 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "AnimationFrameBuffer.h"
 #include "mozilla/Move.h"             // for Move
 
 namespace mozilla {
 namespace image {
 
+AnimationFrameRetainedBuffer::AnimationFrameRetainedBuffer(size_t aThreshold,
+                                                           size_t aBatch,
+                                                           size_t aStartFrame)
+  : AnimationFrameBuffer(aBatch, aStartFrame)
+  , mThreshold(aThreshold)
+{
+  // To simplify the code, we have the assumption that the threshold for
+  // entering discard-after-display mode is at least twice the batch size (since
+  // that is the most frames-pending-decode we will request) + 1 for the current
+  // frame. That way the redecoded frames being inserted will never risk
+  // overlapping the frames we will discard due to the animation progressing.
+  // That may cause us to use a little more memory than we want but that is an
+  // acceptable tradeoff for simplicity.
+  size_t minThreshold = 2 * mBatch + 1;
+  if (mThreshold < minThreshold) {
+    mThreshold = minThreshold;
+  }
+
+  // The maximum number of frames we should ever have decoded at one time is
+  // twice the batch. That is a good as number as any to start our decoding at.
+  mPending = mBatch * 2;
+}
+
+bool
+AnimationFrameRetainedBuffer::InsertInternal(RefPtr<imgFrame>&& aFrame)
+{
+  // We should only insert new frames if we actually asked for them.
+  MOZ_ASSERT(!mSizeKnown);
+  MOZ_ASSERT(mFrames.Length() < mThreshold);
+
+  mFrames.AppendElement(std::move(aFrame));
+  MOZ_ASSERT(mSize == mFrames.Length());
+  return mSize < mThreshold;
+}
+
+bool
+AnimationFrameRetainedBuffer::ResetInternal()
+{
+  // If we haven't crossed the threshold, then we know by definition we have
+  // not discarded any frames. If we previously requested more frames, but
+  // it would have been more than we would have buffered otherwise, we can
+  // stop the decoding after one more frame.
+  if (mPending > 1 && mSize >= mBatch * 2 + 1) {
+    MOZ_ASSERT(!mSizeKnown);
+    mPending = 1;
+  }
+
+  // Either the decoder is still running, or we have enough frames already.
+  // No need for us to restart it.
+  return false;
+}
+
+bool
+AnimationFrameRetainedBuffer::MarkComplete(const gfx::IntRect& aFirstFrameRefreshArea)
+{
+  MOZ_ASSERT(!mSizeKnown);
+  mSizeKnown = true;
+  mPending = 0;
+  mFrames.Compact();
+  return false;
+}
+
+void
+AnimationFrameRetainedBuffer::AdvanceInternal()
+{
+  // We should not have advanced if we never inserted.
+  MOZ_ASSERT(!mFrames.IsEmpty());
+  // We only want to change the current frame index if we have advanced. This
+  // means either a higher frame index, or going back to the beginning.
+  size_t framesLength = mFrames.Length();
+  // We should never have advanced beyond the frame buffer.
+  MOZ_ASSERT(mGetIndex < framesLength);
+  // We should never advance if the current frame is null -- it needs to know
+  // the timeout from it at least to know when to advance.
+  MOZ_ASSERT_IF(mGetIndex > 0, mFrames[mGetIndex - 1]);
+  MOZ_ASSERT_IF(mGetIndex == 0, mFrames[framesLength - 1]);
+  // The owner should have already accessed the next frame, so it should also
+  // be available.
+  MOZ_ASSERT(mFrames[mGetIndex]);
+
+  if (!mSizeKnown) {
+    // Calculate how many frames we have requested ahead of the current frame.
+    size_t buffered = mPending + framesLength - mGetIndex - 1;
+    if (buffered < mBatch) {
+      // If we have fewer frames than the batch size, then ask for more. If we
+      // do not have any pending, then we know that there is no active decoding.
+      mPending += mBatch;
+    }
+  }
+}
+
+imgFrame*
+AnimationFrameRetainedBuffer::Get(size_t aFrame, bool aForDisplay)
+{
+  // We should not have asked for a frame if we never inserted.
+  if (mFrames.IsEmpty()) {
+    MOZ_ASSERT_UNREACHABLE("Calling Get() when we have no frames");
+    return nullptr;
+  }
+
+  // If we don't have that frame, return an empty frame ref.
+  if (aFrame >= mFrames.Length()) {
+    return nullptr;
+  }
+
+  // If we have space for the frame, it should always be available.
+  if (!mFrames[aFrame]) {
+    MOZ_ASSERT_UNREACHABLE("Calling Get() when frame is unavailable");
+    return nullptr;
+  }
+
+  // If we are advancing on behalf of the animation, we don't expect it to be
+  // getting any frames (besides the first) until we get the desired frame.
+  MOZ_ASSERT(aFrame == 0 || mAdvance == 0);
+  return mFrames[aFrame].get();
+}
+
+bool
+AnimationFrameRetainedBuffer::IsFirstFrameFinished() const
+{
+  return !mFrames.IsEmpty() && mFrames[0]->IsFinished();
+}
+
+bool
+AnimationFrameRetainedBuffer::IsLastInsertedFrame(imgFrame* aFrame) const
+{
+  return !mFrames.IsEmpty() && mFrames.LastElement().get() == aFrame;
+}
+
+void
+AnimationFrameRetainedBuffer::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
+                                                     const AddSizeOfCb& aCallback)
+{
+  size_t i = 0;
+  for (const RefPtr<imgFrame>& frame : mFrames) {
+    ++i;
+    frame->AddSizeOfExcludingThis(aMallocSizeOf,
+      [&](AddSizeOfCbData& aMetadata) {
+        aMetadata.index = i;
+        aCallback(aMetadata);
+      }
+    );
+  }
+}
+
 } // namespace image
 } // namespace mozilla
--- a/image/AnimationFrameBuffer.h
+++ b/image/AnimationFrameBuffer.h
@@ -6,12 +6,386 @@
 #ifndef mozilla_image_AnimationFrameBuffer_h
 #define mozilla_image_AnimationFrameBuffer_h
 
 #include "ISurfaceProvider.h"
 
 namespace mozilla {
 namespace image {
 
+/**
+ * An AnimationFrameBuffer owns the frames outputted by an animated image
+ * decoder as well as directing its owner on how to drive the decoder,
+ * whether to produce more or to stop.
+ *
+ * This should be subclassed by the different types of queues, depending on
+ * what behaviour is desired.
+ */
+class AnimationFrameBuffer
+{
+public:
+  enum class InsertStatus : uint8_t
+  {
+    YIELD, // No more frames required at this time.
+    CONTINUE, // Continue decoding more frames.
+    DISCARD_YIELD, // Crossed threshold, switch to discarding structure
+                   // and stop decoding more frames.
+    DISCARD_CONTINUE // Crossed threshold, switch to discarding structure
+                     // and continue decoding more frames.
+  };
+
+  /**
+   * @param aBatch      Number of frames we request to be decoded each time it
+   *                    decides we need more.
+   *
+   * @param aStartFrame The starting frame for the animation. The frame buffer
+   *                    will auto-advance (and thus keep the decoding pipeline
+   *                    going) until it has reached this frame. Useful when the
+   *                    animation was progressing, but the surface was
+   *                    discarded, and we had to redecode.
+   */
+  AnimationFrameBuffer(size_t aBatch, size_t aStartFrame)
+    : mSize(0)
+    , mBatch(aBatch)
+    , mGetIndex(0)
+    , mAdvance(aStartFrame)
+    , mPending(0)
+    , mSizeKnown(false)
+    , mMayDiscard(false)
+    , mRedecodeError(false)
+    , mRecycling(false)
+  {
+    if (mBatch > SIZE_MAX/4) {
+      // Batch size is so big, we will just end up decoding the whole animation.
+      mBatch = SIZE_MAX/4;
+    } else if (mBatch < 1) {
+      // Never permit a batch size smaller than 1. We always want to be asking
+      // for at least one frame to start.
+      mBatch = 1;
+    }
+  }
+
+  AnimationFrameBuffer(const AnimationFrameBuffer& aOther)
+    : mSize(aOther.mSize)
+    , mBatch(aOther.mBatch)
+    , mGetIndex(aOther.mGetIndex)
+    , mAdvance(aOther.mAdvance)
+    , mPending(aOther.mPending)
+    , mSizeKnown(aOther.mSizeKnown)
+    , mMayDiscard(aOther.mMayDiscard)
+    , mRedecodeError(aOther.mRedecodeError)
+    , mRecycling(aOther.mRecycling)
+  { }
+
+  virtual ~AnimationFrameBuffer()
+  { }
+
+  /**
+   * @returns True if frames post-advance may be discarded and redecoded on
+   *          demand, else false.
+   */
+  bool MayDiscard() const { return mMayDiscard; }
+
+  /**
+   * @returns True if frames post-advance may be reused after displaying, else
+   *          false. Implies MayDiscard().
+   */
+  bool IsRecycling() const
+  {
+    MOZ_ASSERT_IF(mRecycling, mMayDiscard);
+    return mRecycling;
+  }
+
+  /**
+   * @returns True if the frame buffer was ever marked as complete. This implies
+   *          that the total number of frames is known and may be gotten from
+   *          Frames().Length().
+   */
+  bool SizeKnown() const { return mSizeKnown; }
+
+  /**
+   * @returns The total number of frames in the animation. If SizeKnown() is
+   *          true, then this is a constant, else it is just the total number of
+   *          frames we have decoded thus far.
+   */
+  size_t Size() const { return mSize; }
+
+  /**
+   * @returns True if encountered an error during redecode which should cause
+   *          the caller to stop inserting frames.
+   */
+  bool HasRedecodeError() const { return mRedecodeError; }
+
+  /**
+   * @returns The current frame index we have advanced to.
+   */
+  size_t Displayed() const { return mGetIndex; }
+
+  /**
+   * @returns Outstanding frames desired from the decoder.
+   */
+  size_t PendingDecode() const { return mPending; }
+
+  /**
+   * @returns Outstanding frames to advance internally.
+   */
+  size_t PendingAdvance() const { return mAdvance; }
+
+  /**
+   * @returns Number of frames we request to be decoded each time it decides we
+   *          need more.
+   */
+  size_t Batch() const { return mBatch; }
+
+  /**
+   * Resets the currently displayed frame of the frame buffer to the beginning.
+   *
+   * @returns True if the caller should restart the decoder.
+   */
+  bool Reset()
+  {
+    mGetIndex = 0;
+    mAdvance = 0;
+    return ResetInternal();
+  }
+
+  /**
+   * Advance the currently displayed frame of the frame buffer. If it reaches
+   * the end, it will loop back to the beginning. It should not be called unless
+   * a call to Get has returned a valid frame for the next frame index.
+   *
+   * As we advance, the number of frames we have buffered ahead of the current
+   * will shrink. Once that becomes too few, we will request a batch-sized set
+   * of frames to be decoded from the decoder.
+   *
+   * @param aExpectedFrame  The frame we expect to have advanced to. This is
+   *                        used for confirmation purposes (e.g. asserts).
+   *
+   * @returns True if the caller should restart the decoder.
+   */
+  bool AdvanceTo(size_t aExpectedFrame)
+  {
+    MOZ_ASSERT(mAdvance == 0);
+
+    if (++mGetIndex == mSize && mSizeKnown) {
+      mGetIndex = 0;
+    }
+    MOZ_ASSERT(mGetIndex == aExpectedFrame);
+
+    bool hasPending = mPending > 0;
+    AdvanceInternal();
+    // Restart the decoder if we transitioned from no pending frames being
+    // decoded, to some pending frames to be decoded.
+    return !hasPending && mPending > 0;
+  }
+
+  /**
+   * Inserts a frame into the frame buffer.
+   *
+   * Once we have a sufficient number of frames buffered relative to the
+   * currently displayed frame, it will return YIELD to indicate the caller
+   * should stop decoding. Otherwise it will return CONTINUE.
+   *
+   * If we cross the threshold, it will return DISCARD or DISCARD_CONTINUE, to
+   * indicate that the caller should switch to a new queue type.
+   *
+   * @param aFrame      The frame to insert into the buffer.
+   *
+   * @returns True if the decoder should decode another frame.
+   */
+  InsertStatus Insert(RefPtr<imgFrame>&& aFrame)
+  {
+    MOZ_ASSERT(mPending > 0);
+    MOZ_ASSERT(aFrame);
+
+    --mPending;
+    if (!mSizeKnown) {
+      ++mSize;
+    }
+
+    bool retain = InsertInternal(std::move(aFrame));
+
+    if (mAdvance > 0 && mSize > 1) {
+      --mAdvance;
+      ++mGetIndex;
+      AdvanceInternal();
+    }
+
+    if (!retain) {
+      return mPending > 0 ? InsertStatus::DISCARD_CONTINUE
+                          : InsertStatus::DISCARD_YIELD;
+    }
+
+    return mPending > 0 ? InsertStatus::CONTINUE : InsertStatus::YIELD;
+  }
+
+  /**
+   * Access a specific frame from the frame buffer. It should generally access
+   * frames in sequential order, increasing in tandem with AdvanceTo calls. The
+   * first frame may be accessed at any time. The access order should start with
+   * the same value as that given in Initialize (aStartFrame).
+   *
+   * @param aFrame      The frame index to access.
+   *
+   * @returns The frame, if available.
+   */
+  virtual imgFrame* Get(size_t aFrame, bool aForDisplay) = 0;
+
+  /**
+   * @returns True if the first frame of the animation (not of the queue) is
+   *          available/finished, else false.
+   */
+  virtual bool IsFirstFrameFinished() const = 0;
+
+  /**
+   * @returns True if the last inserted frame matches the given frame, else
+   *          false.
+   */
+  virtual bool IsLastInsertedFrame(imgFrame* aFrame) const = 0;
+
+  /**
+   * This should be called after the last frame has been inserted. If the buffer
+   * is discarding old frames, it may request more frames to be decoded. In this
+   * case that means the decoder should start again from the beginning. This
+   * return value should be used in preference to that of the Insert call.
+   *
+   * @returns True if the decoder should decode another frame.
+   */
+  virtual bool MarkComplete(const gfx::IntRect& aFirstFrameRefreshArea) = 0;
+
+  typedef ISurfaceProvider::AddSizeOfCbData AddSizeOfCbData;
+  typedef ISurfaceProvider::AddSizeOfCb AddSizeOfCb;
+
+  /**
+   * Accumulate the total cost of all the frames in the buffer.
+   */
+  virtual void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
+                                      const AddSizeOfCb& aCallback) = 0;
+
+  /**
+   * Request a recycled frame buffer, and if available, set aRecycleRect to be
+   * the dirty rect between the contents of the recycled frame, and the restore
+   * frame (e.g. what we composite on top of) for the next frame to be created.
+   *
+   * @returns The frame to be recycled, if available.
+   */
+  virtual RawAccessFrameRef RecycleFrame(gfx::IntRect& aRecycleRect)
+  {
+    MOZ_ASSERT(!mRecycling);
+    return RawAccessFrameRef();
+  }
+
+protected:
+  /**
+   * Perform the actual insertion of the given frame into the underlying buffer
+   * representation. mGetIndex shall be the index of the frame we are inserting,
+   * and mSize and mPending have already been adjusted as needed.
+   *
+   * @returns True if the caller should continue as normal, false if the discard
+   *          threshold was crossed and we should change queue types.
+   */
+  virtual bool InsertInternal(RefPtr<imgFrame>&& aFrame) = 0;
+
+  /**
+   * Advance from the current frame to the immediately adjacent next frame.
+   * mGetIndex shall be the the index of the new current frame after advancing.
+   * mPending may be adjusted to request more frames.
+   */
+  virtual void AdvanceInternal() = 0;
+
+  /**
+   * Discard any frames as necessary for the reset. mPending may be adjusted to
+   * request more frames.
+   *
+   * @returns True if the caller should resume decoding new frames, else false.
+   */
+  virtual bool ResetInternal() = 0;
+
+  // The total number of frames in the animation. If mSizeKnown is true, it is
+  // the actual total regardless of how many frames are available, otherwise it
+  // is the total number of inserted frames.
+  size_t mSize;
+
+  // The minimum number of frames that we want buffered ahead of the display.
+  size_t mBatch;
+
+  // The sequential index of the frame we have advanced to.
+  size_t mGetIndex;
+
+  // The number of frames we need to auto-advance to synchronize with the caller.
+  size_t mAdvance;
+
+  // The number of frames to decode before we stop.
+  size_t mPending;
+
+  // True if the total number of frames for the animation is known.
+  bool mSizeKnown;
+
+  // True if this buffer may discard frames.
+  bool mMayDiscard;
+
+  // True if we encountered an error while redecoding.
+  bool mRedecodeError;
+
+  // True if this buffer is recycling frames.
+  bool mRecycling;
+};
+
+/**
+ * An AnimationFrameRetainedBuffer will retain all of the frames inserted into
+ * it. Once it crosses its maximum number of frames, it will recommend
+ * conversion to a discarding queue.
+ */
+class AnimationFrameRetainedBuffer final : public AnimationFrameBuffer
+{
+public:
+
+  /**
+   * @param aThreshold  Maximum number of frames that may be stored in the frame
+   *                    buffer before it may discard already displayed frames.
+   *                    Once exceeded, it will discard the previous frame to the
+   *                    current frame whenever Advance is called. It always
+   *                    retains the first frame.
+   *
+   * @param aBatch      See AnimationFrameBuffer::AnimationFrameBuffer.
+   *
+   * @param aStartFrame See AnimationFrameBuffer::AnimationFrameBuffer.
+   */
+  AnimationFrameRetainedBuffer(size_t aThreshold, size_t aBatch, size_t aCurrentFrame);
+
+  /**
+   * @returns Maximum number of frames before we start discarding previous
+   *          frames post-advance.
+   */
+  size_t Threshold() const { return mThreshold; }
+
+  /**
+   * @returns The frames of this animation, in order. Each element will always
+   *          contain a valid frame.
+   */
+  const nsTArray<RefPtr<imgFrame>>& Frames() const { return mFrames; }
+
+  imgFrame* Get(size_t aFrame, bool aForDisplay) override;
+  bool IsFirstFrameFinished() const override;
+  bool IsLastInsertedFrame(imgFrame* aFrame) const override;
+  bool MarkComplete(const gfx::IntRect& aFirstFrameRefreshArea) override;
+  void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
+                              const AddSizeOfCb& aCallback) override;
+
+private:
+  friend class AnimationFrameDiscardingQueue;
+  friend class AnimationFrameRecyclingQueue;
+
+  bool InsertInternal(RefPtr<imgFrame>&& aFrame) override;
+  void AdvanceInternal() override;
+  bool ResetInternal() override;
+
+  // The frames of this animation, in order.
+  nsTArray<RefPtr<imgFrame>> mFrames;
+
+  // The maximum number of frames we can have before discarding.
+  size_t mThreshold;
+};
+
 } // namespace image
 } // namespace mozilla
 
 #endif // mozilla_image_AnimationFrameBuffer_h
--- a/image/AnimationSurfaceProvider.cpp
+++ b/image/AnimationSurfaceProvider.cpp
@@ -41,17 +41,17 @@ AnimationSurfaceProvider::AnimationSurfa
   // Calculate how many frames we need to decode in this animation before we
   // enter decode-on-demand mode.
   IntSize frameSize = aSurfaceKey.Size();
   size_t threshold =
     (size_t(gfxPrefs::ImageAnimatedDecodeOnDemandThresholdKB()) * 1024) /
     (pixelSize * frameSize.width * frameSize.height);
   size_t batch = gfxPrefs::ImageAnimatedDecodeOnDemandBatchSize();
 
-  mFrames.Initialize(threshold, batch, aCurrentFrame);
+  mFrames.reset(new AnimationFrameRetainedBuffer(threshold, batch, aCurrentFrame));
 }
 
 AnimationSurfaceProvider::~AnimationSurfaceProvider()
 {
   DropImageReference();
 }
 
 void
@@ -77,51 +77,51 @@ AnimationSurfaceProvider::Reset()
     MutexAutoLock lock(mFramesMutex);
 
     // If we have not crossed the threshold, we know we haven't discarded any
     // frames, and thus we know it is safe move our display index back to the
     // very beginning. It would be cleaner to let the frame buffer make this
     // decision inside the AnimationFrameBuffer::Reset method, but if we have
     // crossed the threshold, we need to hold onto the decoding mutex too. We
     // should avoid blocking the main thread on the decoder threads.
-    mayDiscard = mFrames.MayDiscard();
+    mayDiscard = mFrames->MayDiscard();
     if (!mayDiscard) {
-      restartDecoder = mFrames.Reset();
+      restartDecoder = mFrames->Reset();
     }
   }
 
   if (mayDiscard) {
     // We are over the threshold and have started discarding old frames. In
     // that case we need to seize the decoding mutex. Thankfully we know that
     // we are in the process of decoding at most the batch size frames, so
     // this should not take too long to acquire.
     MutexAutoLock lock(mDecodingMutex);
 
     // Recreate the decoder so we can regenerate the frames again.
     mDecoder = DecoderFactory::CloneAnimationDecoder(mDecoder);
     MOZ_ASSERT(mDecoder);
 
     MutexAutoLock lock2(mFramesMutex);
-    restartDecoder = mFrames.Reset();
+    restartDecoder = mFrames->Reset();
   }
 
   if (restartDecoder) {
     DecodePool::Singleton()->AsyncRun(this);
   }
 }
 
 void
 AnimationSurfaceProvider::Advance(size_t aFrame)
 {
   bool restartDecoder;
 
   {
     // Typical advancement of a frame.
     MutexAutoLock lock(mFramesMutex);
-    restartDecoder = mFrames.AdvanceTo(aFrame);
+    restartDecoder = mFrames->AdvanceTo(aFrame);
   }
 
   if (restartDecoder) {
     DecodePool::Singleton()->AsyncRun(this);
   }
 }
 
 DrawableFrameRef
@@ -129,63 +129,57 @@ AnimationSurfaceProvider::DrawableRef(si
 {
   MutexAutoLock lock(mFramesMutex);
 
   if (Availability().IsPlaceholder()) {
     MOZ_ASSERT_UNREACHABLE("Calling DrawableRef() on a placeholder");
     return DrawableFrameRef();
   }
 
-  imgFrame* frame = mFrames.Get(aFrame);
+  imgFrame* frame = mFrames->Get(aFrame, /* aForDisplay */ true);
   if (!frame) {
     return DrawableFrameRef();
   }
 
   return frame->DrawableRef();
 }
 
 already_AddRefed<imgFrame>
 AnimationSurfaceProvider::GetFrame(size_t aFrame)
 {
   MutexAutoLock lock(mFramesMutex);
 
   if (Availability().IsPlaceholder()) {
     MOZ_ASSERT_UNREACHABLE("Calling GetFrame() on a placeholder");
-    return RawAccessFrameRef();
+    return nullptr;
   }
 
-  RefPtr<imgFrame> frame = mFrames.Get(aFrame);
+  RefPtr<imgFrame> frame = mFrames->Get(aFrame, /* aForDisplay */ false);
   MOZ_ASSERT_IF(frame, frame->IsFinished());
   return frame.forget();
 }
 
 bool
 AnimationSurfaceProvider::IsFinished() const
 {
   MutexAutoLock lock(mFramesMutex);
 
   if (Availability().IsPlaceholder()) {
     MOZ_ASSERT_UNREACHABLE("Calling IsFinished() on a placeholder");
     return false;
   }
 
-  if (mFrames.Frames().IsEmpty()) {
-    MOZ_ASSERT_UNREACHABLE("Calling IsFinished() when we have no frames");
-    return false;
-  }
-
-  // As long as we have at least one finished frame, we're finished.
-  return mFrames.Frames()[0]->IsFinished();
+  return mFrames->IsFirstFrameFinished();
 }
 
 bool
 AnimationSurfaceProvider::IsFullyDecoded() const
 {
   MutexAutoLock lock(mFramesMutex);
-  return mFrames.SizeKnown() && !mFrames.MayDiscard();
+  return mFrames->SizeKnown() && !mFrames->MayDiscard();
 }
 
 size_t
 AnimationSurfaceProvider::LogicalSizeInBytes() const
 {
   // When decoding animated images, we need at most three live surfaces: the
   // composited surface, the previous composited surface for
   // DisposalMethod::RESTORE_PREVIOUS, and the surface we're currently decoding
@@ -204,29 +198,17 @@ AnimationSurfaceProvider::LogicalSizeInB
 void
 AnimationSurfaceProvider::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
                                                  const AddSizeOfCb& aCallback)
 {
   // Note that the surface cache lock is already held here, and then we acquire
   // mFramesMutex. For this method, this ordering is unavoidable, which means
   // that we must be careful to always use the same ordering elsewhere.
   MutexAutoLock lock(mFramesMutex);
-
-  size_t i = 0;
-  for (const RawAccessFrameRef& frame : mFrames.Frames()) {
-    ++i;
-    if (frame) {
-      frame->AddSizeOfExcludingThis(aMallocSizeOf,
-        [&](AddSizeOfCbData& aMetadata) {
-          aMetadata.index = i;
-          aCallback(aMetadata);
-        }
-      );
-    }
-  }
+  mFrames->AddSizeOfExcludingThis(aMallocSizeOf, aCallback);
 }
 
 void
 AnimationSurfaceProvider::Run()
 {
   MutexAutoLock lock(mDecodingMutex);
 
   if (!mDecoder) {
@@ -287,41 +269,57 @@ AnimationSurfaceProvider::Run()
 
 bool
 AnimationSurfaceProvider::CheckForNewFrameAtYield()
 {
   mDecodingMutex.AssertCurrentThreadOwns();
   MOZ_ASSERT(mDecoder);
 
   bool justGotFirstFrame = false;
-  bool continueDecoding;
+  bool continueDecoding = false;
 
   {
     MutexAutoLock lock(mFramesMutex);
 
     // Try to get the new frame from the decoder.
-    RawAccessFrameRef frame = mDecoder->GetCurrentFrameRef();
+    RefPtr<imgFrame> frame = mDecoder->GetCurrentFrame();
     MOZ_ASSERT(mDecoder->HasFrameToTake());
     mDecoder->ClearHasFrameToTake();
 
     if (!frame) {
       MOZ_ASSERT_UNREACHABLE("Decoder yielded but didn't produce a frame?");
       return true;
     }
 
     // We should've gotten a different frame than last time.
-    MOZ_ASSERT_IF(!mFrames.Frames().IsEmpty(),
-                  mFrames.Frames().LastElement().get() != frame.get());
+    MOZ_ASSERT(!mFrames->IsLastInsertedFrame(frame));
 
     // Append the new frame to the list.
-    continueDecoding = mFrames.Insert(std::move(frame));
+    AnimationFrameBuffer::InsertStatus status =
+      mFrames->Insert(std::move(frame));
+    switch (status) {
+      case AnimationFrameBuffer::InsertStatus::DISCARD_CONTINUE:
+        continueDecoding = true;
+        MOZ_FALLTHROUGH;
+      case AnimationFrameBuffer::InsertStatus::DISCARD_YIELD:
+        RequestFrameDiscarding();
+        break;
+      case AnimationFrameBuffer::InsertStatus::CONTINUE:
+        continueDecoding = true;
+        break;
+      case AnimationFrameBuffer::InsertStatus::YIELD:
+        break;
+      default:
+        MOZ_ASSERT_UNREACHABLE("Unhandled insert status!");
+        break;
+    }
 
     // We only want to handle the first frame if it is the first pass for the
     // animation decoder. The owning image will be cleared after that.
-    size_t frameCount = mFrames.Frames().Length();
+    size_t frameCount = mFrames->Size();
     if (frameCount == 1 && mImage) {
       justGotFirstFrame = true;
     }
   }
 
   if (justGotFirstFrame) {
     AnnounceSurfaceAvailable();
   }
@@ -338,53 +336,72 @@ AnimationSurfaceProvider::CheckForNewFra
   bool justGotFirstFrame = false;
   bool continueDecoding;
 
   {
     MutexAutoLock lock(mFramesMutex);
 
     // The decoder may or may not have a new frame for us at this point. Avoid
     // reinserting the same frame again.
-    RawAccessFrameRef frame = mDecoder->GetCurrentFrameRef();
+    RefPtr<imgFrame> frame = mDecoder->GetCurrentFrame();
 
     // If the decoder didn't finish a new frame (ie if, after starting the
     // frame, it got an error and aborted the frame and the rest of the decode)
     // that means it won't be reporting it to the image or FrameAnimator so we
     // should ignore it too, that's what HasFrameToTake tracks basically.
     if (!mDecoder->HasFrameToTake()) {
-      frame = RawAccessFrameRef();
+      frame = nullptr;
     } else {
       MOZ_ASSERT(frame);
       mDecoder->ClearHasFrameToTake();
     }
 
-    if (!frame || (!mFrames.Frames().IsEmpty() &&
-                   mFrames.Frames().LastElement().get() == frame.get())) {
-      return mFrames.MarkComplete();
+    if (!frame || mFrames->IsLastInsertedFrame(frame)) {
+      return mFrames->MarkComplete(mDecoder->GetFirstFrameRefreshArea());
     }
 
     // Append the new frame to the list.
-    mFrames.Insert(std::move(frame));
-    continueDecoding = mFrames.MarkComplete();
+    AnimationFrameBuffer::InsertStatus status = mFrames->Insert(std::move(frame));
+    switch (status) {
+      case AnimationFrameBuffer::InsertStatus::DISCARD_CONTINUE:
+      case AnimationFrameBuffer::InsertStatus::DISCARD_YIELD:
+        RequestFrameDiscarding();
+        break;
+      case AnimationFrameBuffer::InsertStatus::CONTINUE:
+      case AnimationFrameBuffer::InsertStatus::YIELD:
+        break;
+      default:
+        MOZ_ASSERT_UNREACHABLE("Unhandled insert status!");
+        break;
+    }
+
+    continueDecoding =
+      mFrames->MarkComplete(mDecoder->GetFirstFrameRefreshArea());
 
     // We only want to handle the first frame if it is the first pass for the
     // animation decoder. The owning image will be cleared after that.
-    if (mFrames.Frames().Length() == 1 && mImage) {
+    if (mFrames->Size() == 1 && mImage) {
       justGotFirstFrame = true;
     }
   }
 
   if (justGotFirstFrame) {
     AnnounceSurfaceAvailable();
   }
 
   return continueDecoding;
 }
 
 void
+AnimationSurfaceProvider::RequestFrameDiscarding()
+{
+  MOZ_ASSERT_UNREACHABLE("Missing implementation");
+}
+
+void
 AnimationSurfaceProvider::AnnounceSurfaceAvailable()
 {
   mFramesMutex.AssertNotCurrentThreadOwns();
   MOZ_ASSERT(mImage);
 
   // We just got the first frame; let the surface cache know. We deliberately do
   // this outside of mFramesMutex to avoid a potential deadlock with
   // AddSizeOfExcludingThis(), since otherwise we'd be acquiring mFramesMutex
@@ -404,17 +421,17 @@ AnimationSurfaceProvider::FinishDecoding
     NotifyDecodeComplete(WrapNotNull(mImage), WrapNotNull(mDecoder));
   }
 
   // Determine if we need to recreate the decoder, in case we are discarding
   // frames and need to loop back to the beginning.
   bool recreateDecoder;
   {
     MutexAutoLock lock(mFramesMutex);
-    recreateDecoder = !mFrames.HasRedecodeError() && mFrames.MayDiscard();
+    recreateDecoder = !mFrames->HasRedecodeError() && mFrames->MayDiscard();
   }
 
   if (recreateDecoder) {
     mDecoder = DecoderFactory::CloneAnimationDecoder(mDecoder);
     MOZ_ASSERT(mDecoder);
   } else {
     mDecoder = nullptr;
   }
--- a/image/AnimationSurfaceProvider.h
+++ b/image/AnimationSurfaceProvider.h
@@ -5,16 +5,18 @@
 
 /**
  * An ISurfaceProvider for animated images.
  */
 
 #ifndef mozilla_image_AnimationSurfaceProvider_h
 #define mozilla_image_AnimationSurfaceProvider_h
 
+#include "mozilla/UniquePtr.h"
+
 #include "FrameAnimator.h"
 #include "IDecodingTask.h"
 #include "ISurfaceProvider.h"
 #include "AnimationFrameBuffer.h"
 
 namespace mozilla {
 namespace image {
 
@@ -79,16 +81,17 @@ public:
   TaskPriority Priority() const override { return TaskPriority::eLow; }
 
 private:
   virtual ~AnimationSurfaceProvider();
 
   void DropImageReference();
   void AnnounceSurfaceAvailable();
   void FinishDecoding();
+  void RequestFrameDiscarding();
 
   // @returns Whether or not we should continue decoding.
   bool CheckForNewFrameAtYield();
 
   // @returns Whether or not we should restart decoding.
   bool CheckForNewFrameAtTerminalState();
 
   /// The image associated with our decoder.
@@ -99,15 +102,15 @@ private:
 
   /// The decoder used to decode this animation.
   RefPtr<Decoder> mDecoder;
 
   /// A mutex to protect mFrames. Always taken after mDecodingMutex.
   mutable Mutex mFramesMutex;
 
   /// The frames of this animation, in order.
-  AnimationFrameBuffer mFrames;
+  UniquePtr<AnimationFrameBuffer> mFrames;
 };
 
 } // namespace image
 } // namespace mozilla
 
 #endif // mozilla_image_AnimationSurfaceProvider_h