summaryrefslogtreecommitdiffstats
path: root/image/AnimationFrameBuffer.h
blob: b812fe4630e91a3bca910e86749e87acc263c4bb (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
/* -*- Mode: C++; tab-width: 2; 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/. */

#ifndef mozilla_image_AnimationFrameBuffer_h
#define mozilla_image_AnimationFrameBuffer_h

#include "ISurfaceProvider.h"
#include <deque>

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 The first frame refresh area. This is used instead of the dirty
   * rect for the last frame when transitioning back to the first frame.
   */
  const gfx::IntRect& FirstFrameRefreshArea() const {
    return mFirstFrameRefreshArea;
  }

  /**
   * @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_YIELD 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;
    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 first frame refresh area. This is used instead of the dirty rect for
  /// the last frame when transitioning back to the first frame.
  gfx::IntRect mFirstFrameRefreshArea;

  // 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;
};

/**
 * An AnimationFrameDiscardingQueue will only retain up to mBatch * 2 frames.
 * When the animation advances, it will discard the old current frame.
 */
class AnimationFrameDiscardingQueue : public AnimationFrameBuffer {
 public:
  explicit AnimationFrameDiscardingQueue(AnimationFrameRetainedBuffer&& aQueue);

  imgFrame* Get(size_t aFrame, bool aForDisplay) final;
  bool IsFirstFrameFinished() const final;
  bool IsLastInsertedFrame(imgFrame* aFrame) const final;
  bool MarkComplete(const gfx::IntRect& aFirstFrameRefreshArea) override;
  void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
                              const AddSizeOfCb& aCallback) override;

  const std::deque<RefPtr<imgFrame>>& Display() const { return mDisplay; }
  const imgFrame* FirstFrame() const { return mFirstFrame; }
  size_t PendingInsert() const { return mInsertIndex; }

 protected:
  bool InsertInternal(RefPtr<imgFrame>&& aFrame) override;
  void AdvanceInternal() override;
  bool ResetInternal() override;

  /// The sequential index of the frame we inserting next.
  size_t mInsertIndex;

  /// Queue storing frames to be displayed by the animator. The first frame in
  /// the queue is the currently displayed frame.
  std::deque<RefPtr<imgFrame>> mDisplay;

  /// The first frame which is never discarded, and preferentially reused.
  RefPtr<imgFrame> mFirstFrame;
};

/**
 * An AnimationFrameRecyclingQueue will only retain up to mBatch * 2 frames.
 * When the animation advances, it will place the old current frame into a
 * recycling queue to be reused for a future allocation. This only works for
 * animated images where we decoded full sized frames into their own buffers,
 * so that the buffers are all identically sized and contain the complete frame
 * data.
 */
class AnimationFrameRecyclingQueue final
    : public AnimationFrameDiscardingQueue {
 public:
  explicit AnimationFrameRecyclingQueue(AnimationFrameRetainedBuffer&& aQueue);

  void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
                              const AddSizeOfCb& aCallback) override;

  RawAccessFrameRef RecycleFrame(gfx::IntRect& aRecycleRect) override;

  struct RecycleEntry {
    explicit RecycleEntry(const gfx::IntRect& aDirtyRect)
        : mDirtyRect(aDirtyRect) {}

    RecycleEntry(RecycleEntry&& aOther)
        : mFrame(std::move(aOther.mFrame)), mDirtyRect(aOther.mDirtyRect) {}

    RecycleEntry& operator=(RecycleEntry&& aOther) {
      mFrame = std::move(aOther.mFrame);
      mDirtyRect = aOther.mDirtyRect;
      return *this;
    }

    RecycleEntry(const RecycleEntry& aOther) = delete;
    RecycleEntry& operator=(const RecycleEntry& aOther) = delete;

    RefPtr<imgFrame> mFrame;  // The frame containing the buffer to recycle.
    gfx::IntRect mDirtyRect;  // The dirty rect of the frame itself.
  };

  const std::deque<RecycleEntry>& Recycle() const { return mRecycle; }

 protected:
  void AdvanceInternal() override;
  bool ResetInternal() override;

  /// Queue storing frames to be recycled by the decoder to produce its future
  /// frames. May contain up to mBatch frames, where the last frame in the queue
  /// is adjacent to the first frame in the mDisplay queue.
  std::deque<RecycleEntry> mRecycle;

  /// Force recycled frames to use the first frame refresh area as their dirty
  /// rect. This is used when we are recycling frames from the end of an
  /// animation to produce frames at the beginning of an animation.
  bool mForceUseFirstFrameRefreshArea;
};

}  // namespace image
}  // namespace mozilla

#endif  // mozilla_image_AnimationFrameBuffer_h