imgFrame.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  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/. */
  6. #ifndef mozilla_image_imgFrame_h
  7. #define mozilla_image_imgFrame_h
  8. #include "mozilla/Maybe.h"
  9. #include "mozilla/MemoryReporting.h"
  10. #include "mozilla/Monitor.h"
  11. #include "mozilla/Move.h"
  12. #include "mozilla/VolatileBuffer.h"
  13. #include "AnimationParams.h"
  14. #include "gfxDrawable.h"
  15. #include "imgIContainer.h"
  16. #include "MainThreadUtils.h"
  17. namespace mozilla {
  18. namespace image {
  19. class ImageRegion;
  20. class DrawableFrameRef;
  21. class RawAccessFrameRef;
  22. enum class Opacity : uint8_t {
  23. FULLY_OPAQUE,
  24. SOME_TRANSPARENCY
  25. };
  26. /**
  27. * AnimationData contains all of the information necessary for using an imgFrame
  28. * as part of an animation.
  29. *
  30. * It includes pointers to the raw image data of the underlying imgFrame, but
  31. * does not own that data. A RawAccessFrameRef for the underlying imgFrame must
  32. * outlive the AnimationData for it to remain valid.
  33. */
  34. struct AnimationData
  35. {
  36. AnimationData(uint8_t* aRawData, uint32_t aPaletteDataLength,
  37. FrameTimeout aTimeout, const nsIntRect& aRect,
  38. BlendMethod aBlendMethod, const Maybe<gfx::IntRect>& aBlendRect,
  39. DisposalMethod aDisposalMethod, bool aHasAlpha)
  40. : mRawData(aRawData)
  41. , mPaletteDataLength(aPaletteDataLength)
  42. , mTimeout(aTimeout)
  43. , mRect(aRect)
  44. , mBlendMethod(aBlendMethod)
  45. , mBlendRect(aBlendRect)
  46. , mDisposalMethod(aDisposalMethod)
  47. , mHasAlpha(aHasAlpha)
  48. { }
  49. uint8_t* mRawData;
  50. uint32_t mPaletteDataLength;
  51. FrameTimeout mTimeout;
  52. nsIntRect mRect;
  53. BlendMethod mBlendMethod;
  54. Maybe<gfx::IntRect> mBlendRect;
  55. DisposalMethod mDisposalMethod;
  56. bool mHasAlpha;
  57. };
  58. class imgFrame
  59. {
  60. typedef gfx::Color Color;
  61. typedef gfx::DataSourceSurface DataSourceSurface;
  62. typedef gfx::DrawTarget DrawTarget;
  63. typedef gfx::SamplingFilter SamplingFilter;
  64. typedef gfx::IntPoint IntPoint;
  65. typedef gfx::IntRect IntRect;
  66. typedef gfx::IntSize IntSize;
  67. typedef gfx::SourceSurface SourceSurface;
  68. typedef gfx::SurfaceFormat SurfaceFormat;
  69. public:
  70. MOZ_DECLARE_REFCOUNTED_TYPENAME(imgFrame)
  71. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(imgFrame)
  72. imgFrame();
  73. /**
  74. * Initialize this imgFrame with an empty surface and prepare it for being
  75. * written to by a decoder.
  76. *
  77. * This is appropriate for use with decoded images, but it should not be used
  78. * when drawing content into an imgFrame, as it may use a different graphics
  79. * backend than normal content drawing.
  80. */
  81. nsresult InitForDecoder(const nsIntSize& aImageSize,
  82. const nsIntRect& aRect,
  83. SurfaceFormat aFormat,
  84. uint8_t aPaletteDepth = 0,
  85. bool aNonPremult = false,
  86. const Maybe<AnimationParams>& aAnimParams = Nothing());
  87. nsresult InitForDecoder(const nsIntSize& aSize,
  88. SurfaceFormat aFormat,
  89. uint8_t aPaletteDepth = 0)
  90. {
  91. nsIntRect frameRect(0, 0, aSize.width, aSize.height);
  92. AnimationParams animParams { frameRect, FrameTimeout::Forever(),
  93. /* aFrameNum */ 1, BlendMethod::OVER,
  94. DisposalMethod::NOT_SPECIFIED };
  95. return InitForDecoder(aSize, frameRect,
  96. aFormat, aPaletteDepth, false, Some(animParams));
  97. }
  98. /**
  99. * Initialize this imgFrame with a new surface and draw the provided
  100. * gfxDrawable into it.
  101. *
  102. * This is appropriate to use when drawing content into an imgFrame, as it
  103. * uses the same graphics backend as normal content drawing. The downside is
  104. * that the underlying surface may not be stored in a volatile buffer on all
  105. * platforms, and raw access to the surface (using RawAccessRef()) may be much
  106. * more expensive than in the InitForDecoder() case.
  107. *
  108. * aBackend specifies the DrawTarget backend type this imgFrame is supposed
  109. * to be drawn to.
  110. */
  111. nsresult InitWithDrawable(gfxDrawable* aDrawable,
  112. const nsIntSize& aSize,
  113. const SurfaceFormat aFormat,
  114. SamplingFilter aSamplingFilter,
  115. uint32_t aImageFlags,
  116. gfx::BackendType aBackend);
  117. DrawableFrameRef DrawableRef();
  118. RawAccessFrameRef RawAccessRef();
  119. /**
  120. * Make this imgFrame permanently available for raw access.
  121. *
  122. * This is irrevocable, and should be avoided whenever possible, since it
  123. * prevents this imgFrame from being optimized and makes it impossible for its
  124. * volatile buffer to be freed.
  125. *
  126. * It is an error to call this without already holding a RawAccessFrameRef to
  127. * this imgFrame.
  128. */
  129. void SetRawAccessOnly();
  130. bool Draw(gfxContext* aContext, const ImageRegion& aRegion,
  131. SamplingFilter aSamplingFilter, uint32_t aImageFlags);
  132. nsresult ImageUpdated(const nsIntRect& aUpdateRect);
  133. /**
  134. * Mark this imgFrame as completely decoded, and set final options.
  135. *
  136. * You must always call either Finish() or Abort() before releasing the last
  137. * RawAccessFrameRef pointing to an imgFrame.
  138. *
  139. * @param aFrameOpacity Whether this imgFrame is opaque.
  140. */
  141. void Finish(Opacity aFrameOpacity = Opacity::SOME_TRANSPARENCY);
  142. /**
  143. * Mark this imgFrame as aborted. This informs the imgFrame that if it isn't
  144. * completely decoded now, it never will be.
  145. *
  146. * You must always call either Finish() or Abort() before releasing the last
  147. * RawAccessFrameRef pointing to an imgFrame.
  148. */
  149. void Abort();
  150. /**
  151. * Returns true if this imgFrame has been aborted.
  152. */
  153. bool IsAborted() const;
  154. /**
  155. * Returns true if this imgFrame is completely decoded.
  156. */
  157. bool IsFinished() const;
  158. /**
  159. * Blocks until this imgFrame is either completely decoded, or is marked as
  160. * aborted.
  161. *
  162. * Note that calling this on the main thread _blocks the main thread_. Be very
  163. * careful in your use of this method to avoid excessive main thread jank or
  164. * deadlock.
  165. */
  166. void WaitUntilFinished() const;
  167. /**
  168. * Returns the number of bytes per pixel this imgFrame requires. This is a
  169. * worst-case value that does not take into account the effects of format
  170. * changes caused by Optimize(), since an imgFrame is not optimized throughout
  171. * its lifetime.
  172. */
  173. uint32_t GetBytesPerPixel() const { return GetIsPaletted() ? 1 : 4; }
  174. const IntSize& GetImageSize() const { return mImageSize; }
  175. const IntRect& GetRect() const { return mFrameRect; }
  176. IntSize GetSize() const { return mFrameRect.Size(); }
  177. const IntRect& GetBlendRect() const { return mBlendRect; }
  178. IntRect GetBoundedBlendRect() const { return mBlendRect.Intersect(mFrameRect); }
  179. FrameTimeout GetTimeout() const { return mTimeout; }
  180. BlendMethod GetBlendMethod() const { return mBlendMethod; }
  181. DisposalMethod GetDisposalMethod() const { return mDisposalMethod; }
  182. bool FormatHasAlpha() const { return mFormat == SurfaceFormat::B8G8R8A8; }
  183. void GetImageData(uint8_t** aData, uint32_t* length) const;
  184. uint8_t* GetImageData() const;
  185. bool GetIsPaletted() const;
  186. void GetPaletteData(uint32_t** aPalette, uint32_t* length) const;
  187. uint32_t* GetPaletteData() const;
  188. uint8_t GetPaletteDepth() const { return mPaletteDepth; }
  189. AnimationData GetAnimationData() const;
  190. bool GetCompositingFailed() const;
  191. void SetCompositingFailed(bool val);
  192. void SetOptimizable();
  193. already_AddRefed<SourceSurface> GetSourceSurface();
  194. void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf, size_t& aHeapSizeOut,
  195. size_t& aNonHeapSizeOut) const;
  196. private: // methods
  197. ~imgFrame();
  198. nsresult LockImageData();
  199. nsresult UnlockImageData();
  200. bool CanOptimizeOpaqueImage();
  201. nsresult Optimize(gfx::DrawTarget* aTarget);
  202. void AssertImageDataLocked() const;
  203. bool AreAllPixelsWritten() const;
  204. nsresult ImageUpdatedInternal(const nsIntRect& aUpdateRect);
  205. void GetImageDataInternal(uint8_t** aData, uint32_t* length) const;
  206. uint32_t GetImageBytesPerRow() const;
  207. uint32_t GetImageDataLength() const;
  208. already_AddRefed<SourceSurface> GetSourceSurfaceInternal();
  209. uint32_t PaletteDataLength() const
  210. {
  211. return mPaletteDepth ? (size_t(1) << mPaletteDepth) * sizeof(uint32_t)
  212. : 0;
  213. }
  214. struct SurfaceWithFormat {
  215. RefPtr<gfxDrawable> mDrawable;
  216. SurfaceFormat mFormat;
  217. SurfaceWithFormat() { }
  218. SurfaceWithFormat(gfxDrawable* aDrawable, SurfaceFormat aFormat)
  219. : mDrawable(aDrawable), mFormat(aFormat)
  220. { }
  221. bool IsValid() { return !!mDrawable; }
  222. };
  223. SurfaceWithFormat SurfaceForDrawing(bool aDoPartialDecode,
  224. bool aDoTile,
  225. ImageRegion& aRegion,
  226. SourceSurface* aSurface);
  227. private: // data
  228. friend class DrawableFrameRef;
  229. friend class RawAccessFrameRef;
  230. friend class UnlockImageDataRunnable;
  231. //////////////////////////////////////////////////////////////////////////////
  232. // Thread-safe mutable data, protected by mMonitor.
  233. //////////////////////////////////////////////////////////////////////////////
  234. mutable Monitor mMonitor;
  235. RefPtr<DataSourceSurface> mImageSurface;
  236. RefPtr<SourceSurface> mOptSurface;
  237. RefPtr<VolatileBuffer> mVBuf;
  238. VolatileBufferPtr<uint8_t> mVBufPtr;
  239. nsIntRect mDecoded;
  240. //! Number of RawAccessFrameRefs currently alive for this imgFrame.
  241. int32_t mLockCount;
  242. bool mHasNoAlpha;
  243. bool mAborted;
  244. bool mFinished;
  245. bool mOptimizable;
  246. //////////////////////////////////////////////////////////////////////////////
  247. // Effectively const data, only mutated in the Init methods.
  248. //////////////////////////////////////////////////////////////////////////////
  249. IntSize mImageSize;
  250. IntRect mFrameRect;
  251. IntRect mBlendRect;
  252. //! The timeout for this frame.
  253. FrameTimeout mTimeout;
  254. DisposalMethod mDisposalMethod;
  255. BlendMethod mBlendMethod;
  256. SurfaceFormat mFormat;
  257. // The palette and image data for images that are paletted, since Cairo
  258. // doesn't support these images.
  259. // The paletted data comes first, then the image data itself.
  260. // Total length is PaletteDataLength() + GetImageDataLength().
  261. uint8_t* mPalettedImageData;
  262. uint8_t mPaletteDepth;
  263. bool mNonPremult;
  264. //////////////////////////////////////////////////////////////////////////////
  265. // Main-thread-only mutable data.
  266. //////////////////////////////////////////////////////////////////////////////
  267. bool mCompositingFailed;
  268. };
  269. /**
  270. * A reference to an imgFrame that holds the imgFrame's surface in memory,
  271. * allowing drawing. If you have a DrawableFrameRef |ref| and |if (ref)| returns
  272. * true, then calls to Draw() and GetSourceSurface() are guaranteed to succeed.
  273. */
  274. class DrawableFrameRef final
  275. {
  276. public:
  277. DrawableFrameRef() { }
  278. explicit DrawableFrameRef(imgFrame* aFrame)
  279. : mFrame(aFrame)
  280. , mRef(aFrame->mVBuf)
  281. {
  282. if (mRef.WasBufferPurged()) {
  283. mFrame = nullptr;
  284. mRef = nullptr;
  285. }
  286. }
  287. DrawableFrameRef(DrawableFrameRef&& aOther)
  288. : mFrame(aOther.mFrame.forget())
  289. , mRef(Move(aOther.mRef))
  290. { }
  291. DrawableFrameRef& operator=(DrawableFrameRef&& aOther)
  292. {
  293. MOZ_ASSERT(this != &aOther, "Self-moves are prohibited");
  294. mFrame = aOther.mFrame.forget();
  295. mRef = Move(aOther.mRef);
  296. return *this;
  297. }
  298. explicit operator bool() const { return bool(mFrame); }
  299. imgFrame* operator->()
  300. {
  301. MOZ_ASSERT(mFrame);
  302. return mFrame;
  303. }
  304. const imgFrame* operator->() const
  305. {
  306. MOZ_ASSERT(mFrame);
  307. return mFrame;
  308. }
  309. imgFrame* get() { return mFrame; }
  310. const imgFrame* get() const { return mFrame; }
  311. void reset()
  312. {
  313. mFrame = nullptr;
  314. mRef = nullptr;
  315. }
  316. private:
  317. DrawableFrameRef(const DrawableFrameRef& aOther) = delete;
  318. RefPtr<imgFrame> mFrame;
  319. VolatileBufferPtr<uint8_t> mRef;
  320. };
  321. /**
  322. * A reference to an imgFrame that holds the imgFrame's surface in memory in a
  323. * format appropriate for access as raw data. If you have a RawAccessFrameRef
  324. * |ref| and |if (ref)| is true, then calls to GetImageData() and
  325. * GetPaletteData() are guaranteed to succeed. This guarantee is stronger than
  326. * DrawableFrameRef, so everything that a valid DrawableFrameRef guarantees is
  327. * also guaranteed by a valid RawAccessFrameRef.
  328. *
  329. * This may be considerably more expensive than is necessary just for drawing,
  330. * so only use this when you need to read or write the raw underlying image data
  331. * that the imgFrame holds.
  332. *
  333. * Once all an imgFrame's RawAccessFrameRefs go out of scope, new
  334. * RawAccessFrameRefs cannot be created.
  335. */
  336. class RawAccessFrameRef final
  337. {
  338. public:
  339. RawAccessFrameRef() { }
  340. explicit RawAccessFrameRef(imgFrame* aFrame)
  341. : mFrame(aFrame)
  342. {
  343. MOZ_ASSERT(mFrame, "Need a frame");
  344. if (NS_FAILED(mFrame->LockImageData())) {
  345. mFrame->UnlockImageData();
  346. mFrame = nullptr;
  347. }
  348. }
  349. RawAccessFrameRef(RawAccessFrameRef&& aOther)
  350. : mFrame(aOther.mFrame.forget())
  351. { }
  352. ~RawAccessFrameRef()
  353. {
  354. if (mFrame) {
  355. mFrame->UnlockImageData();
  356. }
  357. }
  358. RawAccessFrameRef& operator=(RawAccessFrameRef&& aOther)
  359. {
  360. MOZ_ASSERT(this != &aOther, "Self-moves are prohibited");
  361. if (mFrame) {
  362. mFrame->UnlockImageData();
  363. }
  364. mFrame = aOther.mFrame.forget();
  365. return *this;
  366. }
  367. explicit operator bool() const { return bool(mFrame); }
  368. imgFrame* operator->()
  369. {
  370. MOZ_ASSERT(mFrame);
  371. return mFrame.get();
  372. }
  373. const imgFrame* operator->() const
  374. {
  375. MOZ_ASSERT(mFrame);
  376. return mFrame;
  377. }
  378. imgFrame* get() { return mFrame; }
  379. const imgFrame* get() const { return mFrame; }
  380. void reset()
  381. {
  382. if (mFrame) {
  383. mFrame->UnlockImageData();
  384. }
  385. mFrame = nullptr;
  386. }
  387. private:
  388. RawAccessFrameRef(const RawAccessFrameRef& aOther) = delete;
  389. RefPtr<imgFrame> mFrame;
  390. };
  391. } // namespace image
  392. } // namespace mozilla
  393. #endif // mozilla_image_imgFrame_h