Bug 1669129 - [devtools] Enable devtools.overflow.debugging.enabled. r=jdescottes
[gecko.git] / image / Decoder.h
blobf8d628b45fbc166c48714d16d3e9e43b4aba3ba9
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/. */
6 #ifndef mozilla_image_Decoder_h
7 #define mozilla_image_Decoder_h
9 #include "FrameAnimator.h"
10 #include "RasterImage.h"
11 #include "mozilla/Maybe.h"
12 #include "mozilla/NotNull.h"
13 #include "mozilla/RefPtr.h"
14 #include "AnimationParams.h"
15 #include "DecoderFlags.h"
16 #include "ImageMetadata.h"
17 #include "Orientation.h"
18 #include "SourceBuffer.h"
19 #include "StreamingLexer.h"
20 #include "SurfaceFlags.h"
21 #include "qcms.h"
23 namespace mozilla {
25 namespace Telemetry {
26 enum HistogramID : uint32_t;
27 } // namespace Telemetry
29 namespace image {
31 class imgFrame;
33 struct DecoderFinalStatus final {
34 DecoderFinalStatus(bool aWasMetadataDecode, bool aFinished, bool aHadError,
35 bool aShouldReportError)
36 : mWasMetadataDecode(aWasMetadataDecode),
37 mFinished(aFinished),
38 mHadError(aHadError),
39 mShouldReportError(aShouldReportError) {}
41 /// True if this was a metadata decode.
42 const bool mWasMetadataDecode : 1;
44 /// True if this decoder finished, whether successfully or due to failure.
45 const bool mFinished : 1;
47 /// True if this decoder encountered an error.
48 const bool mHadError : 1;
50 /// True if this decoder encountered the kind of error that should be reported
51 /// to the console.
52 const bool mShouldReportError : 1;
55 struct DecoderTelemetry final {
56 DecoderTelemetry(const Maybe<Telemetry::HistogramID>& aSpeedHistogram,
57 size_t aBytesDecoded, uint32_t aChunkCount,
58 TimeDuration aDecodeTime)
59 : mSpeedHistogram(aSpeedHistogram),
60 mBytesDecoded(aBytesDecoded),
61 mChunkCount(aChunkCount),
62 mDecodeTime(aDecodeTime) {}
64 /// @return our decoder's speed, in KBps.
65 int32_t Speed() const {
66 return mBytesDecoded / (1024 * mDecodeTime.ToSeconds());
69 /// @return our decoder's decode time, in microseconds.
70 int32_t DecodeTimeMicros() { return mDecodeTime.ToMicroseconds(); }
72 /// The per-image-format telemetry ID for recording our decoder's speed, or
73 /// Nothing() if we don't record speed telemetry for this kind of decoder.
74 const Maybe<Telemetry::HistogramID> mSpeedHistogram;
76 /// The number of bytes of input our decoder processed.
77 const size_t mBytesDecoded;
79 /// The number of chunks our decoder's input was divided into.
80 const uint32_t mChunkCount;
82 /// The amount of time our decoder spent inside DoDecode().
83 const TimeDuration mDecodeTime;
86 /**
87 * Interface which owners of an animated Decoder object must implement in order
88 * to use recycling. It allows the decoder to get a handle to the recycled
89 * frames.
91 class IDecoderFrameRecycler {
92 public:
93 /**
94 * Request the next available recycled imgFrame from the recycler.
96 * @param aRecycleRect If a frame is returned, this must be set to the
97 * accumulated dirty rect between the frame being
98 * recycled, and the frame being generated.
100 * @returns The recycled frame, if any is available.
102 virtual RawAccessFrameRef RecycleFrame(gfx::IntRect& aRecycleRect) = 0;
105 class Decoder {
106 public:
107 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Decoder)
109 explicit Decoder(RasterImage* aImage);
112 * Initialize an image decoder. Decoders may not be re-initialized.
114 * @return NS_OK if the decoder could be initialized successfully.
116 nsresult Init();
119 * Decodes, reading all data currently available in the SourceBuffer.
121 * If more data is needed and @aOnResume is non-null, Decode() will schedule
122 * @aOnResume to be called when more data is available.
124 * @return a LexerResult which may indicate:
125 * - the image has been successfully decoded (TerminalState::SUCCESS), or
126 * - the image has failed to decode (TerminalState::FAILURE), or
127 * - the decoder is yielding until it gets more data
128 * (Yield::NEED_MORE_DATA), or
129 * - the decoder is yielding to allow the caller to access intermediate
130 * output (Yield::OUTPUT_AVAILABLE).
132 LexerResult Decode(IResumable* aOnResume = nullptr);
135 * Terminate this decoder in a failure state, just as if the decoder
136 * implementation had returned TerminalState::FAILURE from DoDecode().
138 * XXX(seth): This method should be removed ASAP; it exists only because
139 * RasterImage::FinalizeDecoder() requires an actual Decoder object as an
140 * argument, so we can't simply tell RasterImage a decode failed except via an
141 * intervening decoder. We'll fix this in bug 1291071.
143 LexerResult TerminateFailure();
146 * Given a maximum number of bytes we're willing to decode, @aByteLimit,
147 * returns true if we should attempt to run this decoder synchronously.
149 bool ShouldSyncDecode(size_t aByteLimit);
152 * Gets the invalidation region accumulated by the decoder so far, and clears
153 * the decoder's invalidation region. This means that each call to
154 * TakeInvalidRect() returns only the invalidation region accumulated since
155 * the last call to TakeInvalidRect().
157 nsIntRect TakeInvalidRect() {
158 nsIntRect invalidRect = mInvalidRect;
159 mInvalidRect.SetEmpty();
160 return invalidRect;
164 * Gets the progress changes accumulated by the decoder so far, and clears
165 * them. This means that each call to TakeProgress() returns only the changes
166 * accumulated since the last call to TakeProgress().
168 Progress TakeProgress() {
169 Progress progress = mProgress;
170 mProgress = NoProgress;
171 return progress;
175 * Returns true if there's any progress to report.
177 bool HasProgress() const {
178 return mProgress != NoProgress || !mInvalidRect.IsEmpty() ||
179 mFinishedNewFrame;
183 * State.
187 * If we're doing a metadata decode, we only decode the image's headers, which
188 * is enough to determine the image's intrinsic size. A metadata decode is
189 * enabled by calling SetMetadataDecode() *before* calling Init().
191 void SetMetadataDecode(bool aMetadataDecode) {
192 MOZ_ASSERT(!mInitialized, "Shouldn't be initialized yet");
193 mMetadataDecode = aMetadataDecode;
195 bool IsMetadataDecode() const { return mMetadataDecode; }
198 * Sets the output size of this decoder. If this is smaller than the intrinsic
199 * size of the image, we'll downscale it while decoding. For memory usage
200 * reasons, upscaling is forbidden and will trigger assertions in debug
201 * builds.
203 * Not calling SetOutputSize() means that we should just decode at the
204 * intrinsic size, whatever it is.
206 * If SetOutputSize() was called, ExplicitOutputSize() can be used to
207 * determine the value that was passed to it.
209 * This must be called before Init() is called.
211 void SetOutputSize(const gfx::IntSize& aSize);
214 * @return the output size of this decoder. If this is smaller than the
215 * intrinsic size, then the image will be downscaled during the decoding
216 * process.
218 * Illegal to call if HasSize() returns false.
220 gfx::IntSize OutputSize() const {
221 MOZ_ASSERT(HasSize());
222 return *mOutputSize;
226 * @return either the size passed to SetOutputSize() or Nothing(), indicating
227 * that SetOutputSize() was not explicitly called.
229 Maybe<gfx::IntSize> ExplicitOutputSize() const;
232 * Sets the expected image size of this decoder. Decoding will fail if this
233 * does not match.
235 void SetExpectedSize(const gfx::IntSize& aSize) {
236 mExpectedSize.emplace(aSize);
240 * Is the image size what was expected, if specified?
242 bool IsExpectedSize() const {
243 return mExpectedSize.isNothing() || *mExpectedSize == Size();
247 * Set an iterator to the SourceBuffer which will feed data to this decoder.
248 * This must always be called before calling Init(). (And only before Init().)
250 * XXX(seth): We should eliminate this method and pass a SourceBufferIterator
251 * to the various decoder constructors instead.
253 void SetIterator(SourceBufferIterator&& aIterator) {
254 MOZ_ASSERT(!mInitialized, "Shouldn't be initialized yet");
255 mIterator.emplace(std::move(aIterator));
258 SourceBuffer* GetSourceBuffer() const { return mIterator->Owner(); }
261 * Should this decoder send partial invalidations?
263 bool ShouldSendPartialInvalidations() const {
264 return !(mDecoderFlags & DecoderFlags::IS_REDECODE);
268 * Should we stop decoding after the first frame?
270 bool IsFirstFrameDecode() const {
271 return bool(mDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY);
275 * @return the number of complete animation frames which have been decoded so
276 * far, if it has changed since the last call to TakeCompleteFrameCount();
277 * otherwise, returns Nothing().
279 Maybe<uint32_t> TakeCompleteFrameCount();
281 // The number of frames we have, including anything in-progress. Thus, this
282 // is only 0 if we haven't begun any frames.
283 uint32_t GetFrameCount() { return mFrameCount; }
285 // Did we discover that the image we're decoding is animated?
286 bool HasAnimation() const { return mImageMetadata.HasAnimation(); }
288 // Error tracking
289 bool HasError() const { return mError; }
290 bool ShouldReportError() const { return mShouldReportError; }
292 // Finalize frames
293 void SetFinalizeFrames(bool aFinalize) { mFinalizeFrames = aFinalize; }
294 bool GetFinalizeFrames() const { return mFinalizeFrames; }
296 /// Did we finish decoding enough that calling Decode() again would be
297 /// useless?
298 bool GetDecodeDone() const {
299 return mReachedTerminalState || mDecodeDone ||
300 (mMetadataDecode && HasSize()) || HasError();
303 /// Are we in the middle of a frame right now? Used for assertions only.
304 bool InFrame() const { return mInFrame; }
306 /// Is the image valid if embedded inside an ICO.
307 virtual bool IsValidICOResource() const { return false; }
309 /// Type of decoder.
310 virtual DecoderType GetType() const { return DecoderType::UNKNOWN; }
312 enum DecodeStyle {
313 PROGRESSIVE, // produce intermediate frames representing the partial
314 // state of the image
315 SEQUENTIAL // decode to final image immediately
319 * Get or set the DecoderFlags that influence the behavior of this decoder.
321 void SetDecoderFlags(DecoderFlags aDecoderFlags) {
322 MOZ_ASSERT(!mInitialized);
323 mDecoderFlags = aDecoderFlags;
325 DecoderFlags GetDecoderFlags() const { return mDecoderFlags; }
328 * Get or set the SurfaceFlags that select the kind of output this decoder
329 * will produce.
331 void SetSurfaceFlags(SurfaceFlags aSurfaceFlags);
332 SurfaceFlags GetSurfaceFlags() const { return mSurfaceFlags; }
334 /// @return true if we know the intrinsic size of the image we're decoding.
335 bool HasSize() const { return mImageMetadata.HasSize(); }
338 * @return the intrinsic size of the image we're decoding.
340 * Illegal to call if HasSize() returns false.
342 gfx::IntSize Size() const {
343 MOZ_ASSERT(HasSize());
344 return mImageMetadata.GetSize();
348 * @return an IntRect which covers the entire area of this image at its
349 * intrinsic size, appropriate for use as a frame rect when the image itself
350 * does not specify one.
352 * Illegal to call if HasSize() returns false.
354 gfx::IntRect FullFrame() const {
355 return gfx::IntRect(gfx::IntPoint(), Size());
359 * @return an IntRect which covers the entire area of this image at its size
360 * after scaling - that is, at its output size.
362 * XXX(seth): This is only used for decoders which are using the old
363 * Downscaler code instead of SurfacePipe, since the old AllocateFrame() and
364 * Downscaler APIs required that the frame rect be specified in output space.
365 * We should remove this once all decoders use SurfacePipe.
367 * Illegal to call if HasSize() returns false.
369 gfx::IntRect FullOutputFrame() const {
370 return gfx::IntRect(gfx::IntPoint(), OutputSize());
373 /// @return final status information about this decoder. Should be called
374 /// after we decide we're not going to run the decoder anymore.
375 DecoderFinalStatus FinalStatus() const;
377 /// @return the metadata we collected about this image while decoding.
378 const ImageMetadata& GetImageMetadata() { return mImageMetadata; }
380 /// @return performance telemetry we collected while decoding.
381 DecoderTelemetry Telemetry() const;
384 * @return a weak pointer to the image associated with this decoder. Illegal
385 * to call if this decoder is not associated with an image.
387 NotNull<RasterImage*> GetImage() const { return WrapNotNull(mImage.get()); }
390 * @return a possibly-null weak pointer to the image associated with this
391 * decoder. May be called even if this decoder is not associated with an
392 * image.
394 RasterImage* GetImageMaybeNull() const { return mImage.get(); }
396 RawAccessFrameRef GetCurrentFrameRef() {
397 return mCurrentFrame ? mCurrentFrame->RawAccessRef() : RawAccessFrameRef();
401 * For use during decoding only. Allows the BlendAnimationFilter to get the
402 * current frame we are producing for its animation parameters.
404 imgFrame* GetCurrentFrame() { return mCurrentFrame.get(); }
407 * For use during decoding only. Allows the BlendAnimationFilter to get the
408 * frame it should be pulling the previous frame data from.
410 const RawAccessFrameRef& GetRestoreFrameRef() const { return mRestoreFrame; }
412 const gfx::IntRect& GetRestoreDirtyRect() const { return mRestoreDirtyRect; }
414 const gfx::IntRect& GetRecycleRect() const { return mRecycleRect; }
416 const gfx::IntRect& GetFirstFrameRefreshArea() const {
417 return mFirstFrameRefreshArea;
420 bool HasFrameToTake() const { return mHasFrameToTake; }
421 void ClearHasFrameToTake() {
422 MOZ_ASSERT(mHasFrameToTake);
423 mHasFrameToTake = false;
426 IDecoderFrameRecycler* GetFrameRecycler() const { return mFrameRecycler; }
427 void SetFrameRecycler(IDecoderFrameRecycler* aFrameRecycler) {
428 mFrameRecycler = aFrameRecycler;
431 protected:
432 friend class AutoRecordDecoderTelemetry;
433 friend class DecoderTestHelper;
434 friend class nsBMPDecoder;
435 friend class nsICODecoder;
436 friend class PalettedSurfaceSink;
437 friend class SurfaceSink;
439 virtual ~Decoder();
442 * Internal hooks. Decoder implementations may override these and
443 * only these methods.
445 * BeforeFinishInternal() can be used to detect if decoding is in an
446 * incomplete state, e.g. due to file truncation, in which case it should
447 * return a failing nsresult.
449 virtual nsresult InitInternal();
450 virtual LexerResult DoDecode(SourceBufferIterator& aIterator,
451 IResumable* aOnResume) = 0;
452 virtual nsresult BeforeFinishInternal();
453 virtual nsresult FinishInternal();
454 virtual nsresult FinishWithErrorInternal();
456 qcms_profile* GetCMSOutputProfile() const;
457 qcms_transform* GetCMSsRGBTransform(gfx::SurfaceFormat aFormat) const;
460 * @return the per-image-format telemetry ID for recording this decoder's
461 * speed, or Nothing() if we don't record speed telemetry for this kind of
462 * decoder.
464 virtual Maybe<Telemetry::HistogramID> SpeedHistogram() const {
465 return Nothing();
469 * Progress notifications.
472 // Called by decoders when they determine the size of the image. Informs
473 // the image of its size and sends notifications.
474 void PostSize(int32_t aWidth, int32_t aHeight,
475 Orientation aOrientation = Orientation());
477 // Called by decoders if they determine that the image has transparency.
479 // This should be fired as early as possible to allow observers to do things
480 // that affect content, so it's necessarily pessimistic - if there's a
481 // possibility that the image has transparency, for example because its header
482 // specifies that it has an alpha channel, we fire PostHasTransparency
483 // immediately. PostFrameStop's aFrameOpacity argument, on the other hand, is
484 // only used internally to ImageLib. Because PostFrameStop isn't delivered
485 // until the entire frame has been decoded, decoders may take into account the
486 // actual contents of the frame and give a more accurate result.
487 void PostHasTransparency();
489 // Called by decoders if they determine that the image is animated.
491 // @param aTimeout The time for which the first frame should be shown before
492 // we advance to the next frame.
493 void PostIsAnimated(FrameTimeout aFirstFrameTimeout);
495 // Called by decoders when they end a frame. Informs the image, sends
496 // notifications, and does internal book-keeping.
497 // Specify whether this frame is opaque as an optimization.
498 // For animated images, specify the disposal, blend method and timeout for
499 // this frame.
500 void PostFrameStop(Opacity aFrameOpacity = Opacity::SOME_TRANSPARENCY);
503 * Called by the decoders when they have a region to invalidate. We may not
504 * actually pass these invalidations on right away.
506 * @param aRect The invalidation rect in the coordinate system of the unscaled
507 * image (that is, the image at its intrinsic size).
508 * @param aRectAtOutputSize If not Nothing(), the invalidation rect in the
509 * coordinate system of the scaled image (that is,
510 * the image at our output size). This must
511 * be supplied if we're downscaling during decode.
513 void PostInvalidation(
514 const gfx::IntRect& aRect,
515 const Maybe<gfx::IntRect>& aRectAtOutputSize = Nothing());
517 // Called by the decoders when they have successfully decoded the image. This
518 // may occur as the result of the decoder getting to the appropriate point in
519 // the stream, or by us calling FinishInternal().
521 // May not be called mid-frame.
523 // For animated images, specify the loop count. -1 means loop forever, 0
524 // means a single iteration, stopping on the last frame.
525 void PostDecodeDone(int32_t aLoopCount = 0);
528 * Allocates a new frame, making it our current frame if successful.
530 nsresult AllocateFrame(const gfx::IntSize& aOutputSize,
531 gfx::SurfaceFormat aFormat,
532 const Maybe<AnimationParams>& aAnimParams = Nothing());
534 private:
535 /// Report that an error was encountered while decoding.
536 void PostError();
539 * CompleteDecode() finishes up the decoding process after Decode() determines
540 * that we're finished. It records final progress and does all the cleanup
541 * that's possible off-main-thread.
543 void CompleteDecode();
545 /// @return the number of complete frames we have. Does not include the
546 /// current frame if it's unfinished.
547 uint32_t GetCompleteFrameCount() {
548 if (mFrameCount == 0) {
549 return 0;
552 return mInFrame ? mFrameCount - 1 : mFrameCount;
555 RawAccessFrameRef AllocateFrameInternal(
556 const gfx::IntSize& aOutputSize, gfx::SurfaceFormat aFormat,
557 const Maybe<AnimationParams>& aAnimParams,
558 RawAccessFrameRef&& aPreviousFrame);
560 protected:
561 /// Color management profile from the ICCP chunk in the image.
562 qcms_profile* mInProfile;
564 /// Color management transform to apply to image data.
565 qcms_transform* mTransform;
567 uint8_t* mImageData; // Pointer to image data in BGRA/X
568 uint32_t mImageDataLength;
570 uint32_t mCMSMode;
572 private:
573 RefPtr<RasterImage> mImage;
574 Maybe<SourceBufferIterator> mIterator;
575 IDecoderFrameRecycler* mFrameRecycler;
577 // The current frame the decoder is producing.
578 RawAccessFrameRef mCurrentFrame;
580 // The complete frame to combine with the current partial frame to produce
581 // a complete current frame.
582 RawAccessFrameRef mRestoreFrame;
584 ImageMetadata mImageMetadata;
586 gfx::IntRect
587 mInvalidRect; // Tracks new rows as the current frame is decoded.
588 gfx::IntRect mRestoreDirtyRect; // Tracks an invalidation region between the
589 // restore frame and the previous frame.
590 gfx::IntRect mRecycleRect; // Tracks an invalidation region between the
591 // recycled frame and the current frame.
592 Maybe<gfx::IntSize> mOutputSize; // The size of our output surface.
593 Maybe<gfx::IntSize> mExpectedSize; // The expected size of the image.
594 Progress mProgress;
596 uint32_t mFrameCount; // Number of frames, including anything in-progress
597 FrameTimeout mLoopLength; // Length of a single loop of this image.
598 gfx::IntRect
599 mFirstFrameRefreshArea; // The area of the image that needs to
600 // be invalidated when the animation loops.
602 // Telemetry data for this decoder.
603 TimeDuration mDecodeTime;
605 DecoderFlags mDecoderFlags;
606 SurfaceFlags mSurfaceFlags;
608 bool mInitialized : 1;
609 bool mMetadataDecode : 1;
610 bool mHaveExplicitOutputSize : 1;
611 bool mInFrame : 1;
612 bool mFinishedNewFrame : 1; // True if PostFrameStop() has been called since
613 // the last call to TakeCompleteFrameCount().
614 // Has a new frame that AnimationSurfaceProvider can take. Unfortunately this
615 // has to be separate from mFinishedNewFrame because the png decoder yields a
616 // new frame before calling PostFrameStop().
617 bool mHasFrameToTake : 1;
618 bool mReachedTerminalState : 1;
619 bool mDecodeDone : 1;
620 bool mError : 1;
621 bool mShouldReportError : 1;
622 bool mFinalizeFrames : 1;
625 } // namespace image
626 } // namespace mozilla
628 #endif // mozilla_image_Decoder_h