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_ImageUtils_h
7 #define mozilla_image_ImageUtils_h
9 #include "FrameTimeout.h"
10 #include "mozilla/image/SurfaceFlags.h"
11 #include "mozilla/Assertions.h"
12 #include "mozilla/Maybe.h"
13 #include "mozilla/MozPromise.h"
14 #include "mozilla/RefPtr.h"
15 #include "mozilla/ThreadSafeWeakPtr.h"
33 * The type of decoder; this is usually determined from a MIME type using
34 * DecoderFactory::GetDecoderType() or ImageUtils::GetDecoderType().
36 enum class DecoderType
{
50 struct DecodeMetadataResult
{
53 int32_t mRepetitions
= -1;
54 uint32_t mFrameCount
= 0;
55 bool mAnimated
= false;
56 bool mFrameCountComplete
= true;
59 struct DecodeFrameCountResult
{
60 uint32_t mFrameCount
= 0;
61 bool mFinished
= false;
65 RefPtr
<gfx::SourceSurface
> mSurface
;
66 FrameTimeout mTimeout
;
69 struct DecodeFramesResult
{
70 nsTArray
<DecodedFrame
> mFrames
;
71 bool mFinished
= false;
74 using DecodeMetadataPromise
= MozPromise
<DecodeMetadataResult
, nsresult
, true>;
75 using DecodeFrameCountPromise
=
76 MozPromise
<DecodeFrameCountResult
, nsresult
, true>;
77 using DecodeFramesPromise
= MozPromise
<DecodeFramesResult
, nsresult
, true>;
79 class AnonymousMetadataDecoderTask
;
80 class AnonymousFrameCountDecoderTask
;
81 class AnonymousFramesDecoderTask
;
83 class AnonymousDecoder
: public SupportsThreadSafeWeakPtr
<AnonymousDecoder
> {
85 virtual RefPtr
<DecodeMetadataPromise
> DecodeMetadata() = 0;
87 virtual void Destroy() = 0;
89 virtual RefPtr
<DecodeFrameCountPromise
> DecodeFrameCount(
90 uint32_t aKnownFrameCount
) = 0;
92 virtual RefPtr
<DecodeFramesPromise
> DecodeFrames(size_t aCount
) = 0;
94 virtual void CancelDecodeFrames() = 0;
96 #ifdef MOZ_REFCOUNTED_LEAK_CHECKING
97 virtual const char* typeName() const = 0;
98 virtual size_t typeSize() const = 0;
101 virtual ~AnonymousDecoder();
106 // Returns true if successfully initialized else false.
107 virtual bool Initialize(RefPtr
<Decoder
>&& aDecoder
) = 0;
109 virtual void OnMetadata(const ImageMetadata
* aMetadata
) = 0;
111 virtual void OnFrameCount(uint32_t aFrameCount
, bool aComplete
) = 0;
113 // Returns true if the caller should continue decoding more frames if
115 virtual bool OnFrameAvailable(RefPtr
<imgFrame
>&& aFrame
,
116 RefPtr
<gfx::SourceSurface
>&& aSurface
) = 0;
118 virtual void OnFramesComplete() = 0;
120 friend class AnonymousMetadataDecoderTask
;
121 friend class AnonymousFrameCountDecoderTask
;
122 friend class AnonymousFramesDecoderTask
;
127 static already_AddRefed
<AnonymousDecoder
> CreateDecoder(
128 SourceBuffer
* aSourceBuffer
, DecoderType aType
,
129 const Maybe
<gfx::IntSize
>& aOutputSize
, SurfaceFlags aSurfaceFlags
);
131 static DecoderType
GetDecoderType(const nsACString
& aMimeType
);
134 ImageUtils() = delete;
135 ~ImageUtils() = delete;
139 } // namespace mozilla
141 #endif // mozilla_image_ImageUtils_h