Bug 1578501 [wpt PR 18803] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / image / Decoder.h
blob74d19c12341111616f08d7798263d6723e33c844
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"
22 #include "qcms.h"
24 namespace mozilla {
26 namespace Telemetry {
27 enum HistogramID : uint32_t;
28 } // namespace Telemetry
30 namespace image {
32 class imgFrame;
34 struct DecoderFinalStatus final {
35 DecoderFinalStatus(bool aWasMetadataDecode, bool aFinished, bool aHadError,
36 bool aShouldReportError)
37 : mWasMetadataDecode(aWasMetadataDecode),
38 mFinished(aFinished),
39 mHadError(aHadError),
40 mShouldReportError(aShouldReportError) {}
42 /// True if this was a metadata decode.
43 const bool mWasMetadataDecode : 1;
45 /// True if this decoder finished, whether successfully or due to failure.
46 const bool mFinished : 1;
48 /// True if this decoder encountered an error.
49 const bool mHadError : 1;
51 /// True if this decoder encountered the kind of error that should be reported
52 /// to the console.
53 const bool mShouldReportError : 1;
56 struct DecoderTelemetry final {
57 DecoderTelemetry(const Maybe<Telemetry::HistogramID>& aSpeedHistogram,
58 size_t aBytesDecoded, uint32_t aChunkCount,
59 TimeDuration aDecodeTime)
60 : mSpeedHistogram(aSpeedHistogram),
61 mBytesDecoded(aBytesDecoded),
62 mChunkCount(aChunkCount),
63 mDecodeTime(aDecodeTime) {}
65 /// @return our decoder's speed, in KBps.
66 int32_t Speed() const {
67 return mBytesDecoded / (1024 * mDecodeTime.ToSeconds());
70 /// @return our decoder's decode time, in microseconds.
71 int32_t DecodeTimeMicros() { return mDecodeTime.ToMicroseconds(); }
73 /// The per-image-format telemetry ID for recording our decoder's speed, or
74 /// Nothing() if we don't record speed telemetry for this kind of decoder.
75 const Maybe<Telemetry::HistogramID> mSpeedHistogram;
77 /// The number of bytes of input our decoder processed.
78 const size_t mBytesDecoded;
80 /// The number of chunks our decoder's input was divided into.
81 const uint32_t mChunkCount;
83 /// The amount of time our decoder spent inside DoDecode().
84 const TimeDuration mDecodeTime;
87 /**
88 * Interface which owners of an animated Decoder object must implement in order
89 * to use recycling. It allows the decoder to get a handle to the recycled
90 * frames.
92 class IDecoderFrameRecycler {
93 public:
94 /**
95 * Request the next available recycled imgFrame from the recycler.
97 * @param aRecycleRect If a frame is returned, this must be set to the
98 * accumulated dirty rect between the frame being
99 * recycled, and the frame being generated.
101 * @returns The recycled frame, if any is available.
103 virtual RawAccessFrameRef RecycleFrame(gfx::IntRect& aRecycleRect) = 0;
106 class Decoder {
107 public:
108 NS_INLINE_DECL_THREADSAFE_REFCOUNTING_RECORDED(Decoder)
110 explicit Decoder(RasterImage* aImage);
113 * Initialize an image decoder. Decoders may not be re-initialized.
115 * @return NS_OK if the decoder could be initialized successfully.
117 nsresult Init();
120 * Decodes, reading all data currently available in the SourceBuffer.
122 * If more data is needed and @aOnResume is non-null, Decode() will schedule
123 * @aOnResume to be called when more data is available.
125 * @return a LexerResult which may indicate:
126 * - the image has been successfully decoded (TerminalState::SUCCESS), or
127 * - the image has failed to decode (TerminalState::FAILURE), or
128 * - the decoder is yielding until it gets more data
129 * (Yield::NEED_MORE_DATA), or
130 * - the decoder is yielding to allow the caller to access intermediate
131 * output (Yield::OUTPUT_AVAILABLE).
133 LexerResult Decode(IResumable* aOnResume = nullptr);
136 * Terminate this decoder in a failure state, just as if the decoder
137 * implementation had returned TerminalState::FAILURE from DoDecode().
139 * XXX(seth): This method should be removed ASAP; it exists only because
140 * RasterImage::FinalizeDecoder() requires an actual Decoder object as an
141 * argument, so we can't simply tell RasterImage a decode failed except via an
142 * intervening decoder. We'll fix this in bug 1291071.
144 LexerResult TerminateFailure();
147 * Given a maximum number of bytes we're willing to decode, @aByteLimit,
148 * returns true if we should attempt to run this decoder synchronously.
150 bool ShouldSyncDecode(size_t aByteLimit);
153 * Gets the invalidation region accumulated by the decoder so far, and clears
154 * the decoder's invalidation region. This means that each call to
155 * TakeInvalidRect() returns only the invalidation region accumulated since
156 * the last call to TakeInvalidRect().
158 nsIntRect TakeInvalidRect() {
159 nsIntRect invalidRect = mInvalidRect;
160 mInvalidRect.SetEmpty();
161 return invalidRect;
165 * Gets the progress changes accumulated by the decoder so far, and clears
166 * them. This means that each call to TakeProgress() returns only the changes
167 * accumulated since the last call to TakeProgress().
169 Progress TakeProgress() {
170 Progress progress = mProgress;
171 mProgress = NoProgress;
172 return progress;
176 * Returns true if there's any progress to report.
178 bool HasProgress() const {
179 return mProgress != NoProgress || !mInvalidRect.IsEmpty() ||
180 mFinishedNewFrame;
184 * State.
188 * If we're doing a metadata decode, we only decode the image's headers, which
189 * is enough to determine the image's intrinsic size. A metadata decode is
190 * enabled by calling SetMetadataDecode() *before* calling Init().
192 void SetMetadataDecode(bool aMetadataDecode) {
193 MOZ_ASSERT(!mInitialized, "Shouldn't be initialized yet");
194 mMetadataDecode = aMetadataDecode;
196 bool IsMetadataDecode() const { return mMetadataDecode; }
199 * Sets the output size of this decoder. If this is smaller than the intrinsic
200 * size of the image, we'll downscale it while decoding. For memory usage
201 * reasons, upscaling is forbidden and will trigger assertions in debug
202 * builds.
204 * Not calling SetOutputSize() means that we should just decode at the
205 * intrinsic size, whatever it is.
207 * If SetOutputSize() was called, ExplicitOutputSize() can be used to
208 * determine the value that was passed to it.
210 * This must be called before Init() is called.
212 void SetOutputSize(const gfx::IntSize& aSize);
215 * @return the output size of this decoder. If this is smaller than the
216 * intrinsic size, then the image will be downscaled during the decoding
217 * process.
219 * Illegal to call if HasSize() returns false.
221 gfx::IntSize OutputSize() const {
222 MOZ_ASSERT(HasSize());
223 return *mOutputSize;
227 * @return either the size passed to SetOutputSize() or Nothing(), indicating
228 * that SetOutputSize() was not explicitly called.
230 Maybe<gfx::IntSize> ExplicitOutputSize() const;
233 * Sets the expected image size of this decoder. Decoding will fail if this
234 * does not match.
236 void SetExpectedSize(const gfx::IntSize& aSize) {
237 mExpectedSize.emplace(aSize);
241 * Is the image size what was expected, if specified?
243 bool IsExpectedSize() const {
244 return mExpectedSize.isNothing() || *mExpectedSize == Size();
248 * Set an iterator to the SourceBuffer which will feed data to this decoder.
249 * This must always be called before calling Init(). (And only before Init().)
251 * XXX(seth): We should eliminate this method and pass a SourceBufferIterator
252 * to the various decoder constructors instead.
254 void SetIterator(SourceBufferIterator&& aIterator) {
255 MOZ_ASSERT(!mInitialized, "Shouldn't be initialized yet");
256 mIterator.emplace(std::move(aIterator));
259 SourceBuffer* GetSourceBuffer() const { return mIterator->Owner(); }
262 * Should this decoder send partial invalidations?
264 bool ShouldSendPartialInvalidations() const {
265 return !(mDecoderFlags & DecoderFlags::IS_REDECODE);
269 * Should we stop decoding after the first frame?
271 bool IsFirstFrameDecode() const {
272 return bool(mDecoderFlags & DecoderFlags::FIRST_FRAME_ONLY);
276 * @return the number of complete animation frames which have been decoded so
277 * far, if it has changed since the last call to TakeCompleteFrameCount();
278 * otherwise, returns Nothing().
280 Maybe<uint32_t> TakeCompleteFrameCount();
282 // The number of frames we have, including anything in-progress. Thus, this
283 // is only 0 if we haven't begun any frames.
284 uint32_t GetFrameCount() { return mFrameCount; }
286 // Did we discover that the image we're decoding is animated?
287 bool HasAnimation() const { return mImageMetadata.HasAnimation(); }
289 // Error tracking
290 bool HasError() const { return mError; }
291 bool ShouldReportError() const { return mShouldReportError; }
293 // Finalize frames
294 void SetFinalizeFrames(bool aFinalize) { mFinalizeFrames = aFinalize; }
295 bool GetFinalizeFrames() const { return mFinalizeFrames; }
297 /// Did we finish decoding enough that calling Decode() again would be
298 /// useless?
299 bool GetDecodeDone() const {
300 return mReachedTerminalState || mDecodeDone ||
301 (mMetadataDecode && HasSize()) || HasError();
304 /// Are we in the middle of a frame right now? Used for assertions only.
305 bool InFrame() const { return mInFrame; }
307 /// Is the image valid if embedded inside an ICO.
308 virtual bool IsValidICOResource() const { return false; }
310 /// Type of decoder.
311 virtual DecoderType GetType() const { return DecoderType::UNKNOWN; }
313 enum DecodeStyle {
314 PROGRESSIVE, // produce intermediate frames representing the partial
315 // state of the image
316 SEQUENTIAL // decode to final image immediately
320 * Get or set the DecoderFlags that influence the behavior of this decoder.
322 void SetDecoderFlags(DecoderFlags aDecoderFlags) {
323 MOZ_ASSERT(!mInitialized);
324 mDecoderFlags = aDecoderFlags;
326 DecoderFlags GetDecoderFlags() const { return mDecoderFlags; }
329 * Get or set the SurfaceFlags that select the kind of output this decoder
330 * will produce.
332 void SetSurfaceFlags(SurfaceFlags aSurfaceFlags) {
333 MOZ_ASSERT(!mInitialized);
334 mSurfaceFlags = aSurfaceFlags;
336 SurfaceFlags GetSurfaceFlags() const { return mSurfaceFlags; }
338 /// @return true if we know the intrinsic size of the image we're decoding.
339 bool HasSize() const { return mImageMetadata.HasSize(); }
342 * @return the intrinsic size of the image we're decoding.
344 * Illegal to call if HasSize() returns false.
346 gfx::IntSize Size() const {
347 MOZ_ASSERT(HasSize());
348 return mImageMetadata.GetSize();
352 * @return an IntRect which covers the entire area of this image at its
353 * intrinsic size, appropriate for use as a frame rect when the image itself
354 * does not specify one.
356 * Illegal to call if HasSize() returns false.
358 gfx::IntRect FullFrame() const {
359 return gfx::IntRect(gfx::IntPoint(), Size());
363 * @return an IntRect which covers the entire area of this image at its size
364 * after scaling - that is, at its output size.
366 * XXX(seth): This is only used for decoders which are using the old
367 * Downscaler code instead of SurfacePipe, since the old AllocateFrame() and
368 * Downscaler APIs required that the frame rect be specified in output space.
369 * We should remove this once all decoders use SurfacePipe.
371 * Illegal to call if HasSize() returns false.
373 gfx::IntRect FullOutputFrame() const {
374 return gfx::IntRect(gfx::IntPoint(), OutputSize());
377 /// @return final status information about this decoder. Should be called
378 /// after we decide we're not going to run the decoder anymore.
379 DecoderFinalStatus FinalStatus() const;
381 /// @return the metadata we collected about this image while decoding.
382 const ImageMetadata& GetImageMetadata() { return mImageMetadata; }
384 /// @return performance telemetry we collected while decoding.
385 DecoderTelemetry Telemetry() const;
388 * @return a weak pointer to the image associated with this decoder. Illegal
389 * to call if this decoder is not associated with an image.
391 NotNull<RasterImage*> GetImage() const { return WrapNotNull(mImage.get()); }
394 * @return a possibly-null weak pointer to the image associated with this
395 * decoder. May be called even if this decoder is not associated with an
396 * image.
398 RasterImage* GetImageMaybeNull() const { return mImage.get(); }
400 RawAccessFrameRef GetCurrentFrameRef() {
401 return mCurrentFrame ? mCurrentFrame->RawAccessRef() : RawAccessFrameRef();
405 * For use during decoding only. Allows the BlendAnimationFilter to get the
406 * current frame we are producing for its animation parameters.
408 imgFrame* GetCurrentFrame() { return mCurrentFrame.get(); }
411 * For use during decoding only. Allows the BlendAnimationFilter to get the
412 * frame it should be pulling the previous frame data from.
414 const RawAccessFrameRef& GetRestoreFrameRef() const { return mRestoreFrame; }
416 const gfx::IntRect& GetRestoreDirtyRect() const { return mRestoreDirtyRect; }
418 const gfx::IntRect& GetRecycleRect() const { return mRecycleRect; }
420 const gfx::IntRect& GetFirstFrameRefreshArea() const {
421 return mFirstFrameRefreshArea;
424 bool HasFrameToTake() const { return mHasFrameToTake; }
425 void ClearHasFrameToTake() {
426 MOZ_ASSERT(mHasFrameToTake);
427 mHasFrameToTake = false;
430 IDecoderFrameRecycler* GetFrameRecycler() const { return mFrameRecycler; }
431 void SetFrameRecycler(IDecoderFrameRecycler* aFrameRecycler) {
432 mFrameRecycler = aFrameRecycler;
435 protected:
436 friend class AutoRecordDecoderTelemetry;
437 friend class DecoderTestHelper;
438 friend class nsICODecoder;
439 friend class PalettedSurfaceSink;
440 friend class SurfaceSink;
442 virtual ~Decoder();
445 * Internal hooks. Decoder implementations may override these and
446 * only these methods.
448 * BeforeFinishInternal() can be used to detect if decoding is in an
449 * incomplete state, e.g. due to file truncation, in which case it should
450 * return a failing nsresult.
452 virtual nsresult InitInternal();
453 virtual LexerResult DoDecode(SourceBufferIterator& aIterator,
454 IResumable* aOnResume) = 0;
455 virtual nsresult BeforeFinishInternal();
456 virtual nsresult FinishInternal();
457 virtual nsresult FinishWithErrorInternal();
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 Maybe<Downscaler> mDownscaler;
563 /// Color management profile from the ICCP chunk in the image.
564 qcms_profile* mInProfile;
566 /// Color management transform to apply to image data.
567 qcms_transform* mTransform;
569 uint8_t* mImageData; // Pointer to image data in BGRA/X
570 uint32_t mImageDataLength;
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