1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_image_imgFrame_h
8 #define mozilla_image_imgFrame_h
10 #include "mozilla/Maybe.h"
11 #include "mozilla/MemoryReporting.h"
12 #include "mozilla/Monitor.h"
13 #include "mozilla/Move.h"
14 #include "AnimationParams.h"
15 #include "gfxDrawable.h"
16 #include "imgIContainer.h"
17 #include "MainThreadUtils.h"
23 class DrawableFrameRef
;
24 class RawAccessFrameRef
;
26 enum class Opacity
: uint8_t { FULLY_OPAQUE
, SOME_TRANSPARENCY
};
29 typedef gfx::Color Color
;
30 typedef gfx::DataSourceSurface DataSourceSurface
;
31 typedef gfx::DrawTarget DrawTarget
;
32 typedef gfx::SamplingFilter SamplingFilter
;
33 typedef gfx::IntPoint IntPoint
;
34 typedef gfx::IntRect IntRect
;
35 typedef gfx::IntSize IntSize
;
36 typedef gfx::SourceSurface SourceSurface
;
37 typedef gfx::SurfaceFormat SurfaceFormat
;
40 MOZ_DECLARE_REFCOUNTED_TYPENAME(imgFrame
)
41 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(imgFrame
)
46 * Initialize this imgFrame with an empty surface and prepare it for being
47 * written to by a decoder.
49 * This is appropriate for use with decoded images, but it should not be used
50 * when drawing content into an imgFrame, as it may use a different graphics
51 * backend than normal content drawing.
53 nsresult
InitForDecoder(const nsIntSize
& aImageSize
, SurfaceFormat aFormat
,
55 const Maybe
<AnimationParams
>& aAnimParams
,
59 * Reinitialize this imgFrame with the new parameters, but otherwise retain
60 * the underlying buffer.
62 * This is appropriate for use with animated images, where the decoder was
63 * given an IDecoderFrameRecycler object which may yield a recycled imgFrame
64 * that was discarded to save memory.
66 nsresult
InitForDecoderRecycle(const AnimationParams
& aAnimParams
);
69 * Initialize this imgFrame with a new surface and draw the provided
70 * gfxDrawable into it.
72 * This is appropriate to use when drawing content into an imgFrame, as it
73 * uses the same graphics backend as normal content drawing. The downside is
74 * that the underlying surface may not be stored in a volatile buffer on all
75 * platforms, and raw access to the surface (using RawAccessRef()) may be much
76 * more expensive than in the InitForDecoder() case.
78 * aBackend specifies the DrawTarget backend type this imgFrame is supposed
81 nsresult
InitWithDrawable(gfxDrawable
* aDrawable
, const nsIntSize
& aSize
,
82 const SurfaceFormat aFormat
,
83 SamplingFilter aSamplingFilter
,
84 uint32_t aImageFlags
, gfx::BackendType aBackend
);
86 DrawableFrameRef
DrawableRef();
89 * Create a RawAccessFrameRef for the frame.
91 * @param aOnlyFinished If true, only return a valid RawAccessFrameRef if
92 * imgFrame::Finish has been called.
94 RawAccessFrameRef
RawAccessRef(bool aOnlyFinished
= false);
97 * Make this imgFrame permanently available for raw access.
99 * This is irrevocable, and should be avoided whenever possible, since it
100 * prevents this imgFrame from being optimized and makes it impossible for its
101 * volatile buffer to be freed.
103 * It is an error to call this without already holding a RawAccessFrameRef to
106 void SetRawAccessOnly();
108 bool Draw(gfxContext
* aContext
, const ImageRegion
& aRegion
,
109 SamplingFilter aSamplingFilter
, uint32_t aImageFlags
,
112 nsresult
ImageUpdated(const nsIntRect
& aUpdateRect
);
115 * Mark this imgFrame as completely decoded, and set final options.
117 * You must always call either Finish() or Abort() before releasing the last
118 * RawAccessFrameRef pointing to an imgFrame.
120 * @param aFrameOpacity Whether this imgFrame is opaque.
121 * @param aFinalize Finalize the underlying surface (e.g. so that it
122 * may be marked as read only if possible).
124 void Finish(Opacity aFrameOpacity
= Opacity::SOME_TRANSPARENCY
,
125 bool aFinalize
= true);
128 * Mark this imgFrame as aborted. This informs the imgFrame that if it isn't
129 * completely decoded now, it never will be.
131 * You must always call either Finish() or Abort() before releasing the last
132 * RawAccessFrameRef pointing to an imgFrame.
137 * Returns true if this imgFrame has been aborted.
139 bool IsAborted() const;
142 * Returns true if this imgFrame is completely decoded.
144 bool IsFinished() const;
147 * Blocks until this imgFrame is either completely decoded, or is marked as
150 * Note that calling this on the main thread _blocks the main thread_. Be very
151 * careful in your use of this method to avoid excessive main thread jank or
154 void WaitUntilFinished() const;
157 * Returns the number of bytes per pixel this imgFrame requires. This is a
158 * worst-case value that does not take into account the effects of format
159 * changes caused by Optimize(), since an imgFrame is not optimized throughout
162 uint32_t GetBytesPerPixel() const { return 4; }
164 const IntSize
& GetSize() const { return mImageSize
; }
165 IntRect
GetRect() const { return IntRect(IntPoint(0, 0), mImageSize
); }
166 const IntRect
& GetBlendRect() const { return mBlendRect
; }
167 IntRect
GetBoundedBlendRect() const {
168 return mBlendRect
.Intersect(GetRect());
170 FrameTimeout
GetTimeout() const { return mTimeout
; }
171 BlendMethod
GetBlendMethod() const { return mBlendMethod
; }
172 DisposalMethod
GetDisposalMethod() const { return mDisposalMethod
; }
173 bool FormatHasAlpha() const { return mFormat
== SurfaceFormat::B8G8R8A8
; }
174 void GetImageData(uint8_t** aData
, uint32_t* length
) const;
175 uint8_t* GetImageData() const;
177 const IntRect
& GetDirtyRect() const { return mDirtyRect
; }
178 void SetDirtyRect(const IntRect
& aDirtyRect
) { mDirtyRect
= aDirtyRect
; }
180 void SetOptimizable();
182 void FinalizeSurface();
183 already_AddRefed
<SourceSurface
> GetSourceSurface();
185 struct AddSizeOfCbData
{
187 : heap(0), nonHeap(0), handles(0), index(0), externalId(0) {}
196 typedef std::function
<void(AddSizeOfCbData
& aMetadata
)> AddSizeOfCb
;
198 void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf
,
199 const AddSizeOfCb
& aCallback
) const;
205 * Used when the caller desires raw access to the underlying frame buffer.
206 * If the locking succeeds, the data pointer to the start of the buffer is
207 * returned, else it returns nullptr.
209 * @param aOnlyFinished If true, only attempt to lock if imgFrame::Finish has
212 uint8_t* LockImageData(bool aOnlyFinished
);
213 nsresult
UnlockImageData();
214 nsresult
Optimize(gfx::DrawTarget
* aTarget
);
216 void AssertImageDataLocked() const;
218 bool AreAllPixelsWritten() const;
219 nsresult
ImageUpdatedInternal(const nsIntRect
& aUpdateRect
);
220 void GetImageDataInternal(uint8_t** aData
, uint32_t* length
) const;
221 uint32_t GetImageBytesPerRow() const;
222 uint32_t GetImageDataLength() const;
223 void FinalizeSurfaceInternal();
226 * @param aTemporary If true, it will assume the caller does not require a
227 * wrapping RecycleSourceSurface to protect the underlying
228 * surface from recycling. The reference to the surface
229 * must be freed before releasing the main thread context.
231 already_AddRefed
<SourceSurface
> GetSourceSurfaceInternal(bool aTemporary
);
233 struct SurfaceWithFormat
{
234 RefPtr
<gfxDrawable
> mDrawable
;
235 SurfaceFormat mFormat
;
236 SurfaceWithFormat() : mFormat(SurfaceFormat::UNKNOWN
) {}
237 SurfaceWithFormat(gfxDrawable
* aDrawable
, SurfaceFormat aFormat
)
238 : mDrawable(aDrawable
), mFormat(aFormat
) {}
239 SurfaceWithFormat(SurfaceWithFormat
&& aOther
)
240 : mDrawable(std::move(aOther
.mDrawable
)), mFormat(aOther
.mFormat
) {}
241 SurfaceWithFormat
& operator=(SurfaceWithFormat
&& aOther
) {
242 mDrawable
= std::move(aOther
.mDrawable
);
243 mFormat
= aOther
.mFormat
;
246 SurfaceWithFormat
& operator=(const SurfaceWithFormat
& aOther
) = delete;
247 SurfaceWithFormat(const SurfaceWithFormat
& aOther
) = delete;
248 bool IsValid() { return !!mDrawable
; }
251 SurfaceWithFormat
SurfaceForDrawing(bool aDoPartialDecode
, bool aDoTile
,
252 ImageRegion
& aRegion
,
253 SourceSurface
* aSurface
);
256 friend class DrawableFrameRef
;
257 friend class RawAccessFrameRef
;
258 friend class RecyclingSourceSurface
;
259 friend class UnlockImageDataRunnable
;
261 //////////////////////////////////////////////////////////////////////////////
262 // Thread-safe mutable data, protected by mMonitor.
263 //////////////////////////////////////////////////////////////////////////////
265 mutable Monitor mMonitor
;
268 * Surface which contains either a weak or a strong reference to its
269 * underlying data buffer. If it is a weak reference, and there are no strong
270 * references, the buffer may be released due to events such as low memory.
272 RefPtr
<DataSourceSurface
> mRawSurface
;
273 RefPtr
<DataSourceSurface
> mBlankRawSurface
;
276 * Refers to the same data as mRawSurface, but when set, it guarantees that
277 * we hold a strong reference to the underlying data buffer.
279 RefPtr
<DataSourceSurface
> mLockedSurface
;
280 RefPtr
<DataSourceSurface
> mBlankLockedSurface
;
283 * Optimized copy of mRawSurface for the DrawTarget that will render it. This
284 * is unused if the DrawTarget is able to render DataSourceSurface buffers
287 RefPtr
<SourceSurface
> mOptSurface
;
291 //! Number of RawAccessFrameRefs currently alive for this imgFrame.
294 //! Number of RecyclingSourceSurface's currently alive for this imgFrame.
295 int16_t mRecycleLockCount
;
302 //////////////////////////////////////////////////////////////////////////////
303 // Effectively const data, only mutated in the Init methods.
304 //////////////////////////////////////////////////////////////////////////////
306 //! The size of the buffer we are decoding to.
309 //! The contents for the frame, as represented in the encoded image. This may
310 //! differ from mImageSize because it may be a partial frame. For the first
311 //! frame, this means we need to shift the data in place, and for animated
312 //! frames, it likely need to combine with a previous frame to get the full
316 //! This is the region that has changed between this frame and the previous
317 //! frame of an animation. For the first frame, this will be the same as
321 //! The timeout for this frame.
322 FrameTimeout mTimeout
;
324 DisposalMethod mDisposalMethod
;
325 BlendMethod mBlendMethod
;
326 SurfaceFormat mFormat
;
332 * A reference to an imgFrame that holds the imgFrame's surface in memory,
333 * allowing drawing. If you have a DrawableFrameRef |ref| and |if (ref)| returns
334 * true, then calls to Draw() and GetSourceSurface() are guaranteed to succeed.
336 class DrawableFrameRef final
{
337 typedef gfx::DataSourceSurface DataSourceSurface
;
340 DrawableFrameRef() {}
342 explicit DrawableFrameRef(imgFrame
* aFrame
) : mFrame(aFrame
) {
344 MonitorAutoLock
lock(aFrame
->mMonitor
);
346 if (aFrame
->mRawSurface
) {
347 mRef
.emplace(aFrame
->mRawSurface
, DataSourceSurface::READ
);
348 if (!mRef
->IsMapped()) {
353 MOZ_ASSERT(aFrame
->mOptSurface
);
357 DrawableFrameRef(DrawableFrameRef
&& aOther
)
358 : mFrame(aOther
.mFrame
.forget()), mRef(std::move(aOther
.mRef
)) {}
360 DrawableFrameRef
& operator=(DrawableFrameRef
&& aOther
) {
361 MOZ_ASSERT(this != &aOther
, "Self-moves are prohibited");
362 mFrame
= aOther
.mFrame
.forget();
363 mRef
= std::move(aOther
.mRef
);
367 explicit operator bool() const { return bool(mFrame
); }
369 imgFrame
* operator->() {
374 const imgFrame
* operator->() const {
379 imgFrame
* get() { return mFrame
; }
380 const imgFrame
* get() const { return mFrame
; }
388 DrawableFrameRef(const DrawableFrameRef
& aOther
) = delete;
389 DrawableFrameRef
& operator=(const DrawableFrameRef
& aOther
) = delete;
391 RefPtr
<imgFrame
> mFrame
;
392 Maybe
<DataSourceSurface::ScopedMap
> mRef
;
396 * A reference to an imgFrame that holds the imgFrame's surface in memory in a
397 * format appropriate for access as raw data. If you have a RawAccessFrameRef
398 * |ref| and |if (ref)| is true, then calls to GetImageData() is guaranteed to
399 * succeed. This guarantee is stronger than DrawableFrameRef, so everything that
400 * a valid DrawableFrameRef guarantees is also guaranteed by a valid
403 * This may be considerably more expensive than is necessary just for drawing,
404 * so only use this when you need to read or write the raw underlying image data
405 * that the imgFrame holds.
407 * Once all an imgFrame's RawAccessFrameRefs go out of scope, new
408 * RawAccessFrameRefs cannot be created.
410 class RawAccessFrameRef final
{
412 RawAccessFrameRef() : mData(nullptr) {}
414 explicit RawAccessFrameRef(imgFrame
* aFrame
, bool aOnlyFinished
)
415 : mFrame(aFrame
), mData(nullptr) {
416 MOZ_ASSERT(mFrame
, "Need a frame");
418 mData
= mFrame
->LockImageData(aOnlyFinished
);
424 RawAccessFrameRef(RawAccessFrameRef
&& aOther
)
425 : mFrame(aOther
.mFrame
.forget()), mData(aOther
.mData
) {
426 aOther
.mData
= nullptr;
429 ~RawAccessFrameRef() {
431 mFrame
->UnlockImageData();
435 RawAccessFrameRef
& operator=(RawAccessFrameRef
&& aOther
) {
436 MOZ_ASSERT(this != &aOther
, "Self-moves are prohibited");
439 mFrame
->UnlockImageData();
442 mFrame
= aOther
.mFrame
.forget();
443 mData
= aOther
.mData
;
444 aOther
.mData
= nullptr;
449 explicit operator bool() const { return bool(mFrame
); }
451 imgFrame
* operator->() {
456 const imgFrame
* operator->() const {
461 imgFrame
* get() { return mFrame
; }
462 const imgFrame
* get() const { return mFrame
; }
466 mFrame
->UnlockImageData();
472 uint8_t* Data() const { return mData
; }
475 RawAccessFrameRef(const RawAccessFrameRef
& aOther
) = delete;
476 RawAccessFrameRef
& operator=(const RawAccessFrameRef
& aOther
) = delete;
478 RefPtr
<imgFrame
> mFrame
;
483 } // namespace mozilla
485 #endif // mozilla_image_imgFrame_h