Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/image/AnimationSurfaceProvider.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
/**
7
 * An ISurfaceProvider for animated images.
8
 */
9
10
#ifndef mozilla_image_AnimationSurfaceProvider_h
11
#define mozilla_image_AnimationSurfaceProvider_h
12
13
#include "FrameAnimator.h"
14
#include "IDecodingTask.h"
15
#include "ISurfaceProvider.h"
16
#include "AnimationFrameBuffer.h"
17
18
namespace mozilla {
19
namespace image {
20
21
/**
22
 * An ISurfaceProvider that manages the decoding of animated images and
23
 * dynamically generates surfaces for the current playback state of the
24
 * animation.
25
 */
26
class AnimationSurfaceProvider final
27
  : public ISurfaceProvider
28
  , public IDecodingTask
29
{
30
public:
31
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnimationSurfaceProvider, override)
32
33
  AnimationSurfaceProvider(NotNull<RasterImage*> aImage,
34
                           const SurfaceKey& aSurfaceKey,
35
                           NotNull<Decoder*> aDecoder,
36
                           size_t aCurrentFrame);
37
38
39
  //////////////////////////////////////////////////////////////////////////////
40
  // ISurfaceProvider implementation.
41
  //////////////////////////////////////////////////////////////////////////////
42
43
public:
44
  // We use the ISurfaceProvider constructor of DrawableSurface to indicate that
45
  // our surfaces are computed lazily.
46
0
  DrawableSurface Surface() override { return DrawableSurface(WrapNotNull(this)); }
47
48
  bool IsFinished() const override;
49
  bool IsFullyDecoded() const override;
50
  size_t LogicalSizeInBytes() const override;
51
  void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
52
                              size_t& aHeapSizeOut,
53
                              size_t& aNonHeapSizeOut,
54
                              size_t& aExtHandlesOut) override;
55
  void Reset() override;
56
  void Advance(size_t aFrame) override;
57
58
protected:
59
  DrawableFrameRef DrawableRef(size_t aFrame) override;
60
  RawAccessFrameRef RawAccessRef(size_t aFrame) override;
61
62
  // Animation frames are always locked. This is because we only want to release
63
  // their memory atomically (due to the surface cache discarding them). If they
64
  // were unlocked, the OS could end up releasing the memory of random frames
65
  // from the middle of the animation, which is not worth the complexity of
66
  // dealing with.
67
0
  bool IsLocked() const override { return true; }
68
0
  void SetLocked(bool) override { }
69
70
71
  //////////////////////////////////////////////////////////////////////////////
72
  // IDecodingTask implementation.
73
  //////////////////////////////////////////////////////////////////////////////
74
75
public:
76
  void Run() override;
77
  bool ShouldPreferSyncRun() const override;
78
79
  // Full decodes are low priority compared to metadata decodes because they
80
  // don't block layout or page load.
81
0
  TaskPriority Priority() const override { return TaskPriority::eLow; }
82
83
private:
84
  virtual ~AnimationSurfaceProvider();
85
86
  void DropImageReference();
87
  void AnnounceSurfaceAvailable();
88
  void FinishDecoding();
89
90
  // @returns Whether or not we should continue decoding.
91
  bool CheckForNewFrameAtYield();
92
93
  // @returns Whether or not we should restart decoding.
94
  bool CheckForNewFrameAtTerminalState();
95
96
  /// The image associated with our decoder.
97
  RefPtr<RasterImage> mImage;
98
99
  /// A mutex to protect mDecoder. Always taken before mFramesMutex.
100
  mutable Mutex mDecodingMutex;
101
102
  /// The decoder used to decode this animation.
103
  RefPtr<Decoder> mDecoder;
104
105
  /// A mutex to protect mFrames. Always taken after mDecodingMutex.
106
  mutable Mutex mFramesMutex;
107
108
  /// The frames of this animation, in order.
109
  AnimationFrameBuffer mFrames;
110
};
111
112
} // namespace image
113
} // namespace mozilla
114
115
#endif // mozilla_image_AnimationSurfaceProvider_h