Bug 1467571 [wpt PR 11385] - Make manifest's parsers quicker, a=testonly
[gecko.git] / image / Decoder.h
blobd022f7efd33b64a50a69e5cf1309d5edfa508c5e
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 "Downscaler.h"
17 #include "ImageMetadata.h"
18 #include "Orientation.h"
19 #include "SourceBuffer.h"
20 #include "StreamingLexer.h"
21 #include "SurfaceFlags.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
35 DecoderFinalStatus(bool aWasMetadataDecode,
36 bool aFinished,
37 bool aHadError,
38 bool aShouldReportError)
39 : mWasMetadataDecode(aWasMetadataDecode)
40 , mFinished(aFinished)
41 , mHadError(aHadError)
42 , mShouldReportError(aShouldReportError)
43 { }
45 /// True if this was a metadata decode.
46 const bool mWasMetadataDecode : 1;
48 /// True if this decoder finished, whether successfully or due to failure.
49 const bool mFinished : 1;
51 /// True if this decoder encountered an error.
52 const bool mHadError : 1;
54 /// True if this decoder encountered the kind of error that should be reported
55 /// to the console.
56 const bool mShouldReportError : 1;
59 struct DecoderTelemetry final
61 DecoderTelemetry(const Maybe<Telemetry::HistogramID>& aSpeedHistogram,
62 size_t aBytesDecoded,
63 uint32_t aChunkCount,
64 TimeDuration aDecodeTime)
65 : mSpeedHistogram(aSpeedHistogram)
66 , mBytesDecoded(aBytesDecoded)
67 , mChunkCount(aChunkCount)
68 , mDecodeTime(aDecodeTime)
69 { }
71 /// @return our decoder's speed, in KBps.
72 int32_t Speed() const
74 return mBytesDecoded / (1024 * mDecodeTime.ToSeconds());
77 /// @return our decoder's decode time, in microseconds.
78 int32_t DecodeTimeMicros() { return mDecodeTime.ToMicroseconds(); }
80 /// The per-image-format telemetry ID for recording our decoder's speed, or
81 /// Nothing() if we don't record speed telemetry for this kind of decoder.
82 const Maybe<Telemetry::HistogramID> mSpeedHistogram;
84 /// The number of bytes of input our decoder processed.
85 const size_t mBytesDecoded;
87 /// The number of chunks our decoder's input was divided into.
88 const uint32_t mChunkCount;
90 /// The amount of time our decoder spent inside DoDecode().
91 const TimeDuration mDecodeTime;
94 class Decoder
96 public:
97 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Decoder)
99 explicit Decoder(RasterImage* aImage);
102 * Initialize an image decoder. Decoders may not be re-initialized.
104 * @return NS_OK if the decoder could be initialized successfully.
106 nsresult Init();
109 * Decodes, reading all data currently available in the SourceBuffer.
111 * If more data is needed and @aOnResume is non-null, Decode() will schedule
112 * @aOnResume to be called when more data is available.
114 * @return a LexerResult which may indicate:
115 * - the image has been successfully decoded (TerminalState::SUCCESS), or
116 * - the image has failed to decode (TerminalState::FAILURE), or
117 * - the decoder is yielding until it gets more data (Yield::NEED_MORE_DATA), or
118 * - the decoder is yielding to allow the caller to access intermediate
119 * output (Yield::OUTPUT_AVAILABLE).
121 LexerResult Decode(IResumable* aOnResume = nullptr);
124 * Terminate this decoder in a failure state, just as if the decoder
125 * implementation had returned TerminalState::FAILURE from DoDecode().
127 * XXX(seth): This method should be removed ASAP; it exists only because
128 * RasterImage::FinalizeDecoder() requires an actual Decoder object as an
129 * argument, so we can't simply tell RasterImage a decode failed except via an
130 * intervening decoder. We'll fix this in bug 1291071.
132 LexerResult TerminateFailure();
135 * Given a maximum number of bytes we're willing to decode, @aByteLimit,
136 * returns true if we should attempt to run this decoder synchronously.
138 bool ShouldSyncDecode(size_t aByteLimit);
141 * Gets the invalidation region accumulated by the decoder so far, and clears
142 * the decoder's invalidation region. This means that each call to
143 * TakeInvalidRect() returns only the invalidation region accumulated since
144 * the last call to TakeInvalidRect().
146 nsIntRect TakeInvalidRect()
148 nsIntRect invalidRect = mInvalidRect;
149 mInvalidRect.SetEmpty();
150 return invalidRect;
154 * Gets the progress changes accumulated by the decoder so far, and clears
155 * them. This means that each call to TakeProgress() returns only the changes
156 * accumulated since the last call to TakeProgress().
158 Progress TakeProgress()
160 Progress progress = mProgress;
161 mProgress = NoProgress;
162 return progress;
166 * Returns true if there's any progress to report.
168 bool HasProgress() const
170 return mProgress != NoProgress || !mInvalidRect.IsEmpty() || mFinishedNewFrame;
174 * State.
178 * If we're doing a metadata decode, we only decode the image's headers, which
179 * is enough to determine the image's intrinsic size. A metadata decode is
180 * enabled by calling SetMetadataDecode() *before* calling Init().
182 void SetMetadataDecode(bool aMetadataDecode)
184 MOZ_ASSERT(!mInitialized, "Shouldn't be initialized yet");
185 mMetadataDecode = aMetadataDecode;
187 bool IsMetadataDecode() const { return mMetadataDecode; }
190 * Sets the output size of this decoder. If this is smaller than the intrinsic
191 * size of the image, we'll downscale it while decoding. For memory usage
192 * reasons, upscaling is forbidden and will trigger assertions in debug
193 * builds.
195 * Not calling SetOutputSize() means that we should just decode at the
196 * intrinsic size, whatever it is.
198 * If SetOutputSize() was called, ExplicitOutputSize() can be used to
199 * determine the value that was passed to it.
201 * This must be called before Init() is called.
203 void SetOutputSize(const gfx::IntSize& aSize);
206 * @return the output size of this decoder. If this is smaller than the
207 * intrinsic size, then the image will be downscaled during the decoding
208 * process.
210 * Illegal to call if HasSize() returns false.
212 gfx::IntSize OutputSize() const { MOZ_ASSERT(HasSize()); return *mOutputSize; }
215 * @return either the size passed to SetOutputSize() or Nothing(), indicating
216 * that SetOutputSize() was not explicitly called.
218 Maybe<gfx::IntSize> ExplicitOutputSize() const;
221 * Sets the expected image size of this decoder. Decoding will fail if this
222 * does not match.
224 void SetExpectedSize(const gfx::IntSize& aSize)
226 mExpectedSize.emplace(aSize);
230 * Is the image size what was expected, if specified?
232 bool IsExpectedSize() const
234 return mExpectedSize.isNothing() || *mExpectedSize == Size();
238 * Set an iterator to the SourceBuffer which will feed data to this decoder.
239 * This must always be called before calling Init(). (And only before Init().)
241 * XXX(seth): We should eliminate this method and pass a SourceBufferIterator
242 * to the various decoder constructors instead.
244 void SetIterator(SourceBufferIterator&& aIterator)
246 MOZ_ASSERT(!mInitialized, "Shouldn't be initialized yet");
247 mIterator.emplace(std::move(aIterator));
250 SourceBuffer* GetSourceBuffer() const
252 return mIterator->Owner();
256 * Should this decoder send partial invalidations?
258 bool ShouldSendPartialInvalidations() const
260 return !(mDecoderFlags & DecoderFlags::IS_REDECODE);
264 * Should we stop decoding after the first frame?
266 bool IsFirstFrameDecode() const
268 return bool(mDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY);
272 * @return the number of complete animation frames which have been decoded so
273 * far, if it has changed since the last call to TakeCompleteFrameCount();
274 * otherwise, returns Nothing().
276 Maybe<uint32_t> TakeCompleteFrameCount();
278 // The number of frames we have, including anything in-progress. Thus, this
279 // is only 0 if we haven't begun any frames.
280 uint32_t GetFrameCount() { return mFrameCount; }
282 // Did we discover that the image we're decoding is animated?
283 bool HasAnimation() const { return mImageMetadata.HasAnimation(); }
285 // Error tracking
286 bool HasError() const { return mError; }
287 bool ShouldReportError() const { return mShouldReportError; }
289 // Finalize frames
290 void SetFinalizeFrames(bool aFinalize) { mFinalizeFrames = aFinalize; }
291 bool GetFinalizeFrames() const { return mFinalizeFrames; }
293 /// Did we finish decoding enough that calling Decode() again would be useless?
294 bool GetDecodeDone() const
296 return mReachedTerminalState || mDecodeDone ||
297 (mMetadataDecode && HasSize()) || HasError();
300 /// Are we in the middle of a frame right now? Used for assertions only.
301 bool InFrame() const { return mInFrame; }
303 /// Is the image valid if embedded inside an ICO.
304 virtual bool IsValidICOResource() const
306 return false;
309 /// Type of decoder.
310 virtual DecoderType GetType() const
312 return DecoderType::UNKNOWN;
315 enum DecodeStyle {
316 PROGRESSIVE, // produce intermediate frames representing the partial
317 // state of the image
318 SEQUENTIAL // decode to final image immediately
322 * Get or set the DecoderFlags that influence the behavior of this decoder.
324 void SetDecoderFlags(DecoderFlags aDecoderFlags)
326 MOZ_ASSERT(!mInitialized);
327 mDecoderFlags = aDecoderFlags;
329 DecoderFlags GetDecoderFlags() const { return mDecoderFlags; }
332 * Get or set the SurfaceFlags that select the kind of output this decoder
333 * will produce.
335 void SetSurfaceFlags(SurfaceFlags aSurfaceFlags)
337 MOZ_ASSERT(!mInitialized);
338 mSurfaceFlags = aSurfaceFlags;
340 SurfaceFlags GetSurfaceFlags() const { return mSurfaceFlags; }
342 /// @return true if we know the intrinsic size of the image we're decoding.
343 bool HasSize() const { return mImageMetadata.HasSize(); }
346 * @return the intrinsic size of the image we're decoding.
348 * Illegal to call if HasSize() returns false.
350 gfx::IntSize Size() const
352 MOZ_ASSERT(HasSize());
353 return mImageMetadata.GetSize();
357 * @return an IntRect which covers the entire area of this image at its
358 * intrinsic size, appropriate for use as a frame rect when the image itself
359 * does not specify one.
361 * Illegal to call if HasSize() returns false.
363 gfx::IntRect FullFrame() const
365 return gfx::IntRect(gfx::IntPoint(), Size());
369 * @return an IntRect which covers the entire area of this image at its size
370 * after scaling - that is, at its output size.
372 * XXX(seth): This is only used for decoders which are using the old
373 * Downscaler code instead of SurfacePipe, since the old AllocateFrame() and
374 * Downscaler APIs required that the frame rect be specified in output space.
375 * We should remove this once all decoders use SurfacePipe.
377 * Illegal to call if HasSize() returns false.
379 gfx::IntRect FullOutputFrame() const
381 return gfx::IntRect(gfx::IntPoint(), OutputSize());
384 /// @return final status information about this decoder. Should be called
385 /// after we decide we're not going to run the decoder anymore.
386 DecoderFinalStatus FinalStatus() const;
388 /// @return the metadata we collected about this image while decoding.
389 const ImageMetadata& GetImageMetadata() { return mImageMetadata; }
391 /// @return performance telemetry we collected while decoding.
392 DecoderTelemetry Telemetry() const;
395 * @return a weak pointer to the image associated with this decoder. Illegal
396 * to call if this decoder is not associated with an image.
398 NotNull<RasterImage*> GetImage() const { return WrapNotNull(mImage.get()); }
401 * @return a possibly-null weak pointer to the image associated with this
402 * decoder. May be called even if this decoder is not associated with an
403 * image.
405 RasterImage* GetImageMaybeNull() const { return mImage.get(); }
407 RawAccessFrameRef GetCurrentFrameRef()
409 return mCurrentFrame ? mCurrentFrame->RawAccessRef()
410 : RawAccessFrameRef();
413 bool HasFrameToTake() const { return mHasFrameToTake; }
414 void ClearHasFrameToTake() {
415 MOZ_ASSERT(mHasFrameToTake);
416 mHasFrameToTake = false;
419 protected:
420 friend class AutoRecordDecoderTelemetry;
421 friend class nsICODecoder;
422 friend class PalettedSurfaceSink;
423 friend class SurfaceSink;
425 virtual ~Decoder();
428 * Internal hooks. Decoder implementations may override these and
429 * only these methods.
431 * BeforeFinishInternal() can be used to detect if decoding is in an
432 * incomplete state, e.g. due to file truncation, in which case it should
433 * return a failing nsresult.
435 virtual nsresult InitInternal();
436 virtual LexerResult DoDecode(SourceBufferIterator& aIterator,
437 IResumable* aOnResume) = 0;
438 virtual nsresult BeforeFinishInternal();
439 virtual nsresult FinishInternal();
440 virtual nsresult FinishWithErrorInternal();
443 * @return the per-image-format telemetry ID for recording this decoder's
444 * speed, or Nothing() if we don't record speed telemetry for this kind of
445 * decoder.
447 virtual Maybe<Telemetry::HistogramID> SpeedHistogram() const { return Nothing(); }
451 * Progress notifications.
454 // Called by decoders when they determine the size of the image. Informs
455 // the image of its size and sends notifications.
456 void PostSize(int32_t aWidth,
457 int32_t aHeight,
458 Orientation aOrientation = Orientation());
460 // Called by decoders if they determine that the image has transparency.
462 // This should be fired as early as possible to allow observers to do things
463 // that affect content, so it's necessarily pessimistic - if there's a
464 // possibility that the image has transparency, for example because its header
465 // specifies that it has an alpha channel, we fire PostHasTransparency
466 // immediately. PostFrameStop's aFrameOpacity argument, on the other hand, is
467 // only used internally to ImageLib. Because PostFrameStop isn't delivered
468 // until the entire frame has been decoded, decoders may take into account the
469 // actual contents of the frame and give a more accurate result.
470 void PostHasTransparency();
472 // Called by decoders if they determine that the image is animated.
474 // @param aTimeout The time for which the first frame should be shown before
475 // we advance to the next frame.
476 void PostIsAnimated(FrameTimeout aFirstFrameTimeout);
478 // Called by decoders when they end a frame. Informs the image, sends
479 // notifications, and does internal book-keeping.
480 // Specify whether this frame is opaque as an optimization.
481 // For animated images, specify the disposal, blend method and timeout for
482 // this frame.
483 void PostFrameStop(Opacity aFrameOpacity = Opacity::SOME_TRANSPARENCY);
486 * Called by the decoders when they have a region to invalidate. We may not
487 * actually pass these invalidations on right away.
489 * @param aRect The invalidation rect in the coordinate system of the unscaled
490 * image (that is, the image at its intrinsic size).
491 * @param aRectAtOutputSize If not Nothing(), the invalidation rect in the
492 * coordinate system of the scaled image (that is,
493 * the image at our output size). This must
494 * be supplied if we're downscaling during decode.
496 void PostInvalidation(const gfx::IntRect& aRect,
497 const Maybe<gfx::IntRect>& aRectAtOutputSize = Nothing());
499 // Called by the decoders when they have successfully decoded the image. This
500 // may occur as the result of the decoder getting to the appropriate point in
501 // the stream, or by us calling FinishInternal().
503 // May not be called mid-frame.
505 // For animated images, specify the loop count. -1 means loop forever, 0
506 // means a single iteration, stopping on the last frame.
507 void PostDecodeDone(int32_t aLoopCount = 0);
510 * Allocates a new frame, making it our current frame if successful.
512 * If a non-paletted frame is desired, pass 0 for aPaletteDepth.
514 nsresult AllocateFrame(const gfx::IntSize& aOutputSize,
515 const gfx::IntRect& aFrameRect,
516 gfx::SurfaceFormat aFormat,
517 uint8_t aPaletteDepth = 0,
518 const Maybe<AnimationParams>& aAnimParams = Nothing());
520 private:
521 /// Report that an error was encountered while decoding.
522 void PostError();
525 * CompleteDecode() finishes up the decoding process after Decode() determines
526 * that we're finished. It records final progress and does all the cleanup
527 * that's possible off-main-thread.
529 void CompleteDecode();
531 /// @return the number of complete frames we have. Does not include the
532 /// current frame if it's unfinished.
533 uint32_t GetCompleteFrameCount()
535 if (mFrameCount == 0) {
536 return 0;
539 return mInFrame ? mFrameCount - 1 : mFrameCount;
542 RawAccessFrameRef AllocateFrameInternal(const gfx::IntSize& aOutputSize,
543 const gfx::IntRect& aFrameRect,
544 gfx::SurfaceFormat aFormat,
545 uint8_t aPaletteDepth,
546 const Maybe<AnimationParams>& aAnimParams,
547 imgFrame* aPreviousFrame);
549 protected:
550 Maybe<Downscaler> mDownscaler;
552 uint8_t* mImageData; // Pointer to image data in either Cairo or 8bit format
553 uint32_t mImageDataLength;
554 uint32_t* mColormap; // Current colormap to be used in Cairo format
555 uint32_t mColormapSize;
557 private:
558 RefPtr<RasterImage> mImage;
559 Maybe<SourceBufferIterator> mIterator;
560 RawAccessFrameRef mCurrentFrame;
561 ImageMetadata mImageMetadata;
562 gfx::IntRect mInvalidRect; // Tracks an invalidation region in the current frame.
563 Maybe<gfx::IntSize> mOutputSize; // The size of our output surface.
564 Maybe<gfx::IntSize> mExpectedSize; // The expected size of the image.
565 Progress mProgress;
567 uint32_t mFrameCount; // Number of frames, including anything in-progress
568 FrameTimeout mLoopLength; // Length of a single loop of this image.
569 gfx::IntRect mFirstFrameRefreshArea; // The area of the image that needs to
570 // be invalidated when the animation loops.
572 // Telemetry data for this decoder.
573 TimeDuration mDecodeTime;
575 DecoderFlags mDecoderFlags;
576 SurfaceFlags mSurfaceFlags;
578 bool mInitialized : 1;
579 bool mMetadataDecode : 1;
580 bool mHaveExplicitOutputSize : 1;
581 bool mInFrame : 1;
582 bool mFinishedNewFrame : 1; // True if PostFrameStop() has been called since
583 // the last call to TakeCompleteFrameCount().
584 // Has a new frame that AnimationSurfaceProvider can take. Unfortunately this
585 // has to be separate from mFinishedNewFrame because the png decoder yields a
586 // new frame before calling PostFrameStop().
587 bool mHasFrameToTake : 1;
588 bool mReachedTerminalState : 1;
589 bool mDecodeDone : 1;
590 bool mError : 1;
591 bool mShouldReportError : 1;
592 bool mFinalizeFrames : 1;
595 } // namespace image
596 } // namespace mozilla
598 #endif // mozilla_image_Decoder_h