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
13 #include "AnimationParams.h"
14 #include "MainThreadUtils.h"
15 #include "gfxDrawable.h"
16 #include "mozilla/layers/SourceSurfaceSharedData.h"
17 #include "mozilla/Maybe.h"
18 #include "mozilla/MemoryReporting.h"
19 #include "mozilla/Monitor.h"
26 class DrawableFrameRef
;
27 class RawAccessFrameRef
;
29 enum class Opacity
: uint8_t { FULLY_OPAQUE
, SOME_TRANSPARENCY
};
32 typedef gfx::SourceSurfaceSharedData SourceSurfaceSharedData
;
33 typedef gfx::DrawTarget DrawTarget
;
34 typedef gfx::SamplingFilter SamplingFilter
;
35 typedef gfx::IntPoint IntPoint
;
36 typedef gfx::IntRect IntRect
;
37 typedef gfx::IntSize IntSize
;
38 typedef gfx::SourceSurface SourceSurface
;
39 typedef gfx::SurfaceFormat SurfaceFormat
;
42 MOZ_DECLARE_REFCOUNTED_TYPENAME(imgFrame
)
43 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(imgFrame
)
48 * Initialize this imgFrame with an empty surface and prepare it for being
49 * written to by a decoder.
51 * This is appropriate for use with decoded images, but it should not be used
52 * when drawing content into an imgFrame, as it may use a different graphics
53 * backend than normal content drawing.
55 nsresult
InitForDecoder(const nsIntSize
& aImageSize
, SurfaceFormat aFormat
,
57 const Maybe
<AnimationParams
>& aAnimParams
,
61 * Reinitialize this imgFrame with the new parameters, but otherwise retain
62 * the underlying buffer.
64 * This is appropriate for use with animated images, where the decoder was
65 * given an IDecoderFrameRecycler object which may yield a recycled imgFrame
66 * that was discarded to save memory.
68 nsresult
InitForDecoderRecycle(const AnimationParams
& aAnimParams
);
71 * Initialize this imgFrame with a new surface and draw the provided
72 * gfxDrawable into it.
74 * This is appropriate to use when drawing content into an imgFrame, as it
75 * uses the same graphics backend as normal content drawing. The downside is
76 * that the underlying surface may not be stored in a volatile buffer on all
77 * platforms, and raw access to the surface (using RawAccessRef()) may be much
78 * more expensive than in the InitForDecoder() case.
80 * aBackend specifies the DrawTarget backend type this imgFrame is supposed
83 nsresult
InitWithDrawable(gfxDrawable
* aDrawable
, const nsIntSize
& aSize
,
84 const SurfaceFormat aFormat
,
85 SamplingFilter aSamplingFilter
,
86 uint32_t aImageFlags
, gfx::BackendType aBackend
);
88 DrawableFrameRef
DrawableRef();
91 * Create a RawAccessFrameRef for the frame.
93 RawAccessFrameRef
RawAccessRef();
95 bool Draw(gfxContext
* aContext
, const ImageRegion
& aRegion
,
96 SamplingFilter aSamplingFilter
, uint32_t aImageFlags
,
99 nsresult
ImageUpdated(const nsIntRect
& aUpdateRect
);
102 * Mark this imgFrame as completely decoded, and set final options.
104 * You must always call either Finish() or Abort() before releasing the last
105 * RawAccessFrameRef pointing to an imgFrame.
107 * @param aFrameOpacity Whether this imgFrame is opaque.
108 * @param aFinalize Finalize the underlying surface (e.g. so that it
109 * may be marked as read only if possible).
111 void Finish(Opacity aFrameOpacity
= Opacity::SOME_TRANSPARENCY
,
112 bool aFinalize
= true);
115 * Mark this imgFrame as aborted. This informs the imgFrame that if it isn't
116 * completely decoded now, it never will be.
118 * You must always call either Finish() or Abort() before releasing the last
119 * RawAccessFrameRef pointing to an imgFrame.
124 * Returns true if this imgFrame has been aborted.
126 bool IsAborted() const;
129 * Returns true if this imgFrame is completely decoded.
131 bool IsFinished() const;
134 * Blocks until this imgFrame is either completely decoded, or is marked as
137 * Note that calling this on the main thread _blocks the main thread_. Be very
138 * careful in your use of this method to avoid excessive main thread jank or
141 void WaitUntilFinished() const;
144 * Returns the number of bytes per pixel this imgFrame requires.
146 uint32_t GetBytesPerPixel() const { return 4; }
148 const IntSize
& GetSize() const { return mImageSize
; }
149 IntRect
GetRect() const { return IntRect(IntPoint(0, 0), mImageSize
); }
150 const IntRect
& GetBlendRect() const { return mBlendRect
; }
151 IntRect
GetBoundedBlendRect() const {
152 return mBlendRect
.Intersect(GetRect());
154 FrameTimeout
GetTimeout() const { return mTimeout
; }
155 BlendMethod
GetBlendMethod() const { return mBlendMethod
; }
156 DisposalMethod
GetDisposalMethod() const { return mDisposalMethod
; }
157 bool FormatHasAlpha() const { return mFormat
== SurfaceFormat::OS_RGBA
; }
158 void GetImageData(uint8_t** aData
, uint32_t* length
) const;
159 uint8_t* GetImageData() const;
161 const IntRect
& GetDirtyRect() const { return mDirtyRect
; }
162 void SetDirtyRect(const IntRect
& aDirtyRect
) { mDirtyRect
= aDirtyRect
; }
164 void FinalizeSurface();
165 already_AddRefed
<SourceSurface
> GetSourceSurface();
167 struct AddSizeOfCbData
: public SourceSurface::SizeOfInfo
{
169 : SourceSurface::SizeOfInfo(), mIndex(0), mFinished(false) {}
175 typedef std::function
<void(AddSizeOfCbData
& aMetadata
)> AddSizeOfCb
;
177 void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf
,
178 const AddSizeOfCb
& aCallback
) const;
183 bool AreAllPixelsWritten() const;
184 nsresult
ImageUpdatedInternal(const nsIntRect
& aUpdateRect
);
185 void GetImageDataInternal(uint8_t** aData
, uint32_t* length
) const;
186 uint32_t GetImageBytesPerRow() const;
187 uint32_t GetImageDataLength() const;
188 void FinalizeSurfaceInternal();
189 already_AddRefed
<SourceSurface
> GetSourceSurfaceInternal();
191 struct SurfaceWithFormat
{
192 RefPtr
<gfxDrawable
> mDrawable
;
193 SurfaceFormat mFormat
;
194 SurfaceWithFormat() : mFormat(SurfaceFormat::UNKNOWN
) {}
195 SurfaceWithFormat(gfxDrawable
* aDrawable
, SurfaceFormat aFormat
)
196 : mDrawable(aDrawable
), mFormat(aFormat
) {}
197 SurfaceWithFormat(SurfaceWithFormat
&& aOther
)
198 : mDrawable(std::move(aOther
.mDrawable
)), mFormat(aOther
.mFormat
) {}
199 SurfaceWithFormat
& operator=(SurfaceWithFormat
&& aOther
) {
200 mDrawable
= std::move(aOther
.mDrawable
);
201 mFormat
= aOther
.mFormat
;
204 SurfaceWithFormat
& operator=(const SurfaceWithFormat
& aOther
) = delete;
205 SurfaceWithFormat(const SurfaceWithFormat
& aOther
) = delete;
206 bool IsValid() { return !!mDrawable
; }
209 SurfaceWithFormat
SurfaceForDrawing(bool aDoPartialDecode
, bool aDoTile
,
210 ImageRegion
& aRegion
,
211 SourceSurface
* aSurface
);
214 friend class DrawableFrameRef
;
215 friend class RawAccessFrameRef
;
216 friend class UnlockImageDataRunnable
;
218 //////////////////////////////////////////////////////////////////////////////
219 // Thread-safe mutable data, protected by mMonitor.
220 //////////////////////////////////////////////////////////////////////////////
222 mutable Monitor mMonitor
;
225 * Used for rasterized images, this contains the raw pixel data.
227 RefPtr
<SourceSurfaceSharedData
> mRawSurface
;
228 RefPtr
<SourceSurfaceSharedData
> mBlankRawSurface
;
231 * Used for vector images that were not rasterized directly. This might be a
232 * blob recording or native surface.
234 RefPtr
<SourceSurface
> mOptSurface
;
242 //////////////////////////////////////////////////////////////////////////////
243 // Effectively const data, only mutated in the Init methods.
244 //////////////////////////////////////////////////////////////////////////////
246 //! The size of the buffer we are decoding to.
249 //! The contents for the frame, as represented in the encoded image. This may
250 //! differ from mImageSize because it may be a partial frame. For the first
251 //! frame, this means we need to shift the data in place, and for animated
252 //! frames, it likely need to combine with a previous frame to get the full
256 //! This is the region that has changed between this frame and the previous
257 //! frame of an animation. For the first frame, this will be the same as
261 //! The timeout for this frame.
262 FrameTimeout mTimeout
;
264 DisposalMethod mDisposalMethod
;
265 BlendMethod mBlendMethod
;
266 SurfaceFormat mFormat
;
272 * A reference to an imgFrame that holds the imgFrame's surface in memory,
273 * allowing drawing. If you have a DrawableFrameRef |ref| and |if (ref)| returns
274 * true, then calls to Draw() and GetSourceSurface() are guaranteed to succeed.
276 class DrawableFrameRef final
{
277 typedef gfx::DataSourceSurface DataSourceSurface
;
280 DrawableFrameRef() {}
282 explicit DrawableFrameRef(imgFrame
* aFrame
) : mFrame(aFrame
) {
284 MonitorAutoLock
lock(aFrame
->mMonitor
);
286 if (aFrame
->mRawSurface
) {
287 mRef
.emplace(aFrame
->mRawSurface
, DataSourceSurface::READ
);
288 if (!mRef
->IsMapped()) {
292 } else if (!aFrame
->mOptSurface
|| !aFrame
->mOptSurface
->IsValid()) {
293 // The optimized surface has become invalid, so we need to redecode.
294 // For example, on Windows, there may have been a device reset, and
295 // all D2D surfaces now need to be recreated.
300 DrawableFrameRef(DrawableFrameRef
&& aOther
)
301 : mFrame(std::move(aOther
.mFrame
)), mRef(std::move(aOther
.mRef
)) {}
303 DrawableFrameRef
& operator=(DrawableFrameRef
&& aOther
) {
304 MOZ_ASSERT(this != &aOther
, "Self-moves are prohibited");
305 mFrame
= std::move(aOther
.mFrame
);
306 mRef
= std::move(aOther
.mRef
);
310 explicit operator bool() const { return bool(mFrame
); }
312 imgFrame
* operator->() {
317 const imgFrame
* operator->() const {
322 imgFrame
* get() { return mFrame
; }
323 const imgFrame
* get() const { return mFrame
; }
331 DrawableFrameRef(const DrawableFrameRef
& aOther
) = delete;
332 DrawableFrameRef
& operator=(const DrawableFrameRef
& aOther
) = delete;
334 RefPtr
<imgFrame
> mFrame
;
335 Maybe
<DataSourceSurface::ScopedMap
> mRef
;
339 * A reference to an imgFrame that holds the imgFrame's surface in memory in a
340 * format appropriate for access as raw data. If you have a RawAccessFrameRef
341 * |ref| and |if (ref)| is true, then calls to GetImageData() is guaranteed to
342 * succeed. This guarantee is stronger than DrawableFrameRef, so everything that
343 * a valid DrawableFrameRef guarantees is also guaranteed by a valid
346 * This may be considerably more expensive than is necessary just for drawing,
347 * so only use this when you need to read or write the raw underlying image data
348 * that the imgFrame holds.
350 * Once all an imgFrame's RawAccessFrameRefs go out of scope, new
351 * RawAccessFrameRefs cannot be created.
353 class RawAccessFrameRef final
{
355 RawAccessFrameRef() : mData(nullptr) {}
357 explicit RawAccessFrameRef(imgFrame
* aFrame
)
358 : mFrame(aFrame
), mData(nullptr) {
359 MOZ_ASSERT(mFrame
, "Need a frame");
361 mData
= mFrame
->GetImageData();
367 RawAccessFrameRef(RawAccessFrameRef
&& aOther
)
368 : mFrame(std::move(aOther
.mFrame
)), mData(aOther
.mData
) {
369 aOther
.mData
= nullptr;
372 ~RawAccessFrameRef() {}
374 RawAccessFrameRef
& operator=(RawAccessFrameRef
&& aOther
) {
375 MOZ_ASSERT(this != &aOther
, "Self-moves are prohibited");
377 mFrame
= std::move(aOther
.mFrame
);
378 mData
= aOther
.mData
;
379 aOther
.mData
= nullptr;
384 explicit operator bool() const { return bool(mFrame
); }
386 imgFrame
* operator->() {
391 const imgFrame
* operator->() const {
396 imgFrame
* get() { return mFrame
; }
397 const imgFrame
* get() const { return mFrame
; }
404 uint8_t* Data() const { return mData
; }
407 RawAccessFrameRef(const RawAccessFrameRef
& aOther
) = delete;
408 RawAccessFrameRef
& operator=(const RawAccessFrameRef
& aOther
) = delete;
410 RefPtr
<imgFrame
> mFrame
;
415 } // namespace mozilla
417 #endif // mozilla_image_imgFrame_h