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/. */
7 * An interface for objects which can either store a surface or dynamically
8 * generate one, and various implementations.
11 #ifndef mozilla_image_ISurfaceProvider_h
12 #define mozilla_image_ISurfaceProvider_h
14 #include "mozilla/Attributes.h"
15 #include "mozilla/Maybe.h"
16 #include "mozilla/MemoryReporting.h"
17 #include "mozilla/NotNull.h"
18 #include "mozilla/TimeStamp.h"
19 #include "mozilla/gfx/2D.h"
22 #include "SurfaceCache.h"
28 class DrawableSurface
;
31 * An interface for objects which can either store a surface or dynamically
34 class ISurfaceProvider
{
36 // Subclasses may or may not be XPCOM classes, so we just require that they
37 // implement AddRef and Release.
38 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
40 /// @return key data used for identifying which image this ISurfaceProvider is
41 /// associated with in the surface cache.
42 ImageKey
GetImageKey() const { return mImageKey
; }
44 /// @return key data used to uniquely identify this ISurfaceProvider's cache
45 /// entry in the surface cache.
46 const SurfaceKey
& GetSurfaceKey() const { return mSurfaceKey
; }
48 /// @return a (potentially lazily computed) drawable reference to a surface.
49 virtual DrawableSurface
Surface();
51 /// @return true if DrawableRef() will return a completely decoded surface.
52 virtual bool IsFinished() const = 0;
54 /// @return true if the underlying decoder is currently fully decoded. For
55 /// animated images, this means that at least every frame has been decoded
56 /// at least once. It does not guarantee that all of the frames are present,
57 /// as the surface provider has the option to discard as it deems necessary.
58 virtual bool IsFullyDecoded() const { return IsFinished(); }
60 /// @return the number of bytes of memory this ISurfaceProvider is expected to
61 /// require. Optimizations may result in lower real memory usage. Trivial
62 /// overhead is ignored. Because this value is used in bookkeeping, it's
63 /// important that it be constant over the lifetime of this object.
64 virtual size_t LogicalSizeInBytes() const = 0;
66 typedef imgFrame::AddSizeOfCbData AddSizeOfCbData
;
67 typedef imgFrame::AddSizeOfCb AddSizeOfCb
;
69 /// @return the actual number of bytes of memory this ISurfaceProvider is
70 /// using. May vary over the lifetime of the ISurfaceProvider. The default
71 /// implementation is appropriate for static ISurfaceProviders.
72 virtual void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf
,
73 const AddSizeOfCb
& aCallback
) {
74 DrawableFrameRef ref
= DrawableRef(/* aFrame = */ 0);
79 ref
->AddSizeOfExcludingThis(aMallocSizeOf
, aCallback
);
82 virtual void Reset() {}
83 virtual void Advance(size_t aFrame
) {}
85 /// @return the availability state of this ISurfaceProvider, which indicates
86 /// whether DrawableRef() could successfully return a surface. Should only be
87 /// called from SurfaceCache code as it relies on SurfaceCache for
89 AvailabilityState
& Availability() { return mAvailability
; }
90 const AvailabilityState
& Availability() const { return mAvailability
; }
93 ISurfaceProvider(const ImageKey aImageKey
, const SurfaceKey
& aSurfaceKey
,
94 AvailabilityState aAvailability
)
95 : mImageKey(aImageKey
),
96 mSurfaceKey(aSurfaceKey
),
97 mAvailability(aAvailability
) {
98 MOZ_ASSERT(aImageKey
, "Must have a valid image key");
101 virtual ~ISurfaceProvider() {}
103 /// @return an eagerly computed drawable reference to a surface. For
104 /// dynamically generated animation surfaces, @aFrame specifies the 0-based
105 /// index of the desired frame.
106 virtual DrawableFrameRef
DrawableRef(size_t aFrame
) = 0;
108 /// @return an imgFrame at the 0-based index of the desired frame, as
109 /// specified by @aFrame. Only applies for animated images.
110 virtual already_AddRefed
<imgFrame
> GetFrame(size_t aFrame
) {
111 MOZ_ASSERT_UNREACHABLE("Surface provider does not support direct access!");
115 /// @return true if this ISurfaceProvider is locked. (@see SetLocked())
116 /// Should only be called from SurfaceCache code as it relies on SurfaceCache
117 /// for synchronization.
118 virtual bool IsLocked() const = 0;
120 /// If @aLocked is true, hint that this ISurfaceProvider is in use and it
121 /// should avoid releasing its resources. Should only be called from
122 /// SurfaceCache code as it relies on SurfaceCache for synchronization.
123 virtual void SetLocked(bool aLocked
) = 0;
126 friend class CachedSurface
;
127 friend class DrawableSurface
;
129 const ImageKey mImageKey
;
130 const SurfaceKey mSurfaceKey
;
131 AvailabilityState mAvailability
;
135 * A reference to a surface (stored in an imgFrame) that holds the surface in
136 * memory, guaranteeing that it can be drawn. If you have a DrawableSurface
137 * |surf| and |if (surf)| returns true, then calls to |surf->Draw()| and
138 * |surf->GetSourceSurface()| are guaranteed to succeed.
140 * Note that the surface may be computed lazily, so a DrawableSurface should not
141 * be dereferenced (i.e., operator->() should not be called) until you're
142 * sure that you want to draw it.
144 class MOZ_STACK_CLASS DrawableSurface final
{
146 DrawableSurface() : mHaveSurface(false) {}
148 explicit DrawableSurface(DrawableFrameRef
&& aDrawableRef
)
149 : mDrawableRef(std::move(aDrawableRef
)),
150 mHaveSurface(bool(mDrawableRef
)) {}
152 explicit DrawableSurface(NotNull
<ISurfaceProvider
*> aProvider
)
153 : mProvider(aProvider
), mHaveSurface(true) {}
155 DrawableSurface(DrawableSurface
&& aOther
)
156 : mDrawableRef(std::move(aOther
.mDrawableRef
)),
157 mProvider(std::move(aOther
.mProvider
)),
158 mHaveSurface(aOther
.mHaveSurface
) {
159 aOther
.mHaveSurface
= false;
162 DrawableSurface
& operator=(DrawableSurface
&& aOther
) {
163 MOZ_ASSERT(this != &aOther
, "Self-moves are prohibited");
164 mDrawableRef
= std::move(aOther
.mDrawableRef
);
165 mProvider
= std::move(aOther
.mProvider
);
166 mHaveSurface
= aOther
.mHaveSurface
;
167 aOther
.mHaveSurface
= false;
172 * If this DrawableSurface is dynamically generated from an animation, attempt
173 * to seek to frame @aFrame, where @aFrame is a 0-based index into the frames
174 * of the animation. Otherwise, nothing will blow up at runtime, but we assert
175 * in debug builds, since calling this in an unexpected situation probably
178 * @return a successful result if we could obtain frame @aFrame. Note that
179 * |mHaveSurface| being true means that we're guaranteed to have *some* frame,
180 * so the caller can dereference this DrawableSurface even if Seek() fails,
181 * but while nothing will blow up, the frame won't be the one they expect.
183 nsresult
Seek(size_t aFrame
) {
184 MOZ_ASSERT(mHaveSurface
, "Trying to seek an empty DrawableSurface?");
187 MOZ_ASSERT_UNREACHABLE("Trying to seek a static DrawableSurface?");
188 return NS_ERROR_FAILURE
;
191 mDrawableRef
= mProvider
->DrawableRef(aFrame
);
193 return mDrawableRef
? NS_OK
: NS_ERROR_FAILURE
;
196 already_AddRefed
<imgFrame
> GetFrame(size_t aFrame
) {
197 MOZ_ASSERT(mHaveSurface
, "Trying to get on an empty DrawableSurface?");
200 MOZ_ASSERT_UNREACHABLE("Trying to get on a static DrawableSurface?");
204 return mProvider
->GetFrame(aFrame
);
209 MOZ_ASSERT_UNREACHABLE("Trying to reset a static DrawableSurface?");
216 void Advance(size_t aFrame
) {
218 MOZ_ASSERT_UNREACHABLE("Trying to advance a static DrawableSurface?");
222 mProvider
->Advance(aFrame
);
225 bool IsFullyDecoded() const {
227 MOZ_ASSERT_UNREACHABLE(
228 "Trying to check decoding state of a static DrawableSurface?");
232 return mProvider
->IsFullyDecoded();
235 explicit operator bool() const { return mHaveSurface
; }
236 imgFrame
* operator->() { return DrawableRef().get(); }
239 DrawableSurface(const DrawableSurface
& aOther
) = delete;
240 DrawableSurface
& operator=(const DrawableSurface
& aOther
) = delete;
242 DrawableFrameRef
& DrawableRef() {
243 MOZ_ASSERT(mHaveSurface
);
245 // If we weren't created with a DrawableFrameRef directly, we should've been
246 // created with an ISurfaceProvider which can give us one. Note that if
247 // Seek() has been called, we'll already have a DrawableFrameRef, so we
248 // won't need to get one here.
250 MOZ_ASSERT(mProvider
);
251 mDrawableRef
= mProvider
->DrawableRef(/* aFrame = */ 0);
254 MOZ_ASSERT(mDrawableRef
);
258 DrawableFrameRef mDrawableRef
;
259 RefPtr
<ISurfaceProvider
> mProvider
;
263 // Surface() is implemented here so that DrawableSurface's definition is
264 // visible. This default implementation eagerly obtains a DrawableFrameRef for
265 // the first frame and is intended for static ISurfaceProviders.
266 inline DrawableSurface
ISurfaceProvider::Surface() {
267 return DrawableSurface(DrawableRef(/* aFrame = */ 0));
271 * An ISurfaceProvider that stores a single surface.
273 class SimpleSurfaceProvider final
: public ISurfaceProvider
{
275 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SimpleSurfaceProvider
, override
)
277 SimpleSurfaceProvider(const ImageKey aImageKey
, const SurfaceKey
& aSurfaceKey
,
278 NotNull
<imgFrame
*> aSurface
)
279 : ISurfaceProvider(aImageKey
, aSurfaceKey
,
280 AvailabilityState::StartAvailable()),
282 MOZ_ASSERT(aSurfaceKey
.Size() == mSurface
->GetSize());
285 bool IsFinished() const override
{ return mSurface
->IsFinished(); }
287 size_t LogicalSizeInBytes() const override
{
288 gfx::IntSize size
= mSurface
->GetSize();
289 return size
.width
* size
.height
* mSurface
->GetBytesPerPixel();
293 DrawableFrameRef
DrawableRef(size_t aFrame
) override
{
294 MOZ_ASSERT(aFrame
== 0,
295 "Requesting an animation frame from a SimpleSurfaceProvider?");
296 return mSurface
->DrawableRef();
299 bool IsLocked() const override
{ return bool(mLockRef
); }
301 void SetLocked(bool aLocked
) override
{
302 if (aLocked
== IsLocked()) {
303 return; // Nothing changed.
306 // If we're locked, hold a DrawableFrameRef to |mSurface|, which will keep
307 // any volatile buffer it owns in memory.
308 mLockRef
= aLocked
? mSurface
->DrawableRef() : DrawableFrameRef();
312 virtual ~SimpleSurfaceProvider() {}
314 NotNull
<RefPtr
<imgFrame
>> mSurface
;
315 DrawableFrameRef mLockRef
;
319 } // namespace mozilla
321 #endif // mozilla_image_ISurfaceProvider_h