Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / image / ImageUtils.h
blobf5ed2387cfc7b11b89a038b036be28983fbecc8f
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"
16 #include "nsString.h"
17 #include "nsTArray.h"
19 namespace mozilla {
20 class ErrorResult;
22 namespace gfx {
23 class SourceSurface;
26 namespace image {
27 class Decoder;
28 class imgFrame;
29 class ImageMetadata;
30 class SourceBuffer;
32 /**
33 * The type of decoder; this is usually determined from a MIME type using
34 * DecoderFactory::GetDecoderType() or ImageUtils::GetDecoderType().
36 enum class DecoderType {
37 PNG,
38 GIF,
39 JPEG,
40 BMP,
41 BMP_CLIPBOARD,
42 ICO,
43 ICON,
44 WEBP,
45 AVIF,
46 JXL,
47 UNKNOWN
50 struct DecodeMetadataResult {
51 int32_t mWidth = 0;
52 int32_t mHeight = 0;
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;
64 struct DecodedFrame {
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> {
84 public:
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;
99 #endif
101 virtual ~AnonymousDecoder();
103 protected:
104 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
114 // possible.
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;
125 class ImageUtils {
126 public:
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);
133 private:
134 ImageUtils() = delete;
135 ~ImageUtils() = delete;
138 } // namespace image
139 } // namespace mozilla
141 #endif // mozilla_image_ImageUtils_h